diff --git a/tools/mips/Makefile b/tools/mips/Makefile new file mode 100644 index 0000000..c2dba55 --- /dev/null +++ b/tools/mips/Makefile @@ -0,0 +1,26 @@ +CFLAGS=-O2 +CC=gcc +PREFIX=/usr/local +all: romgen ramgen flashgen flashgen_tlb packhex + +romgen: ./src/romgen.c + $(CC) $(CFLAGS) ./src/romgen.c -lm -o ./romgen + +ramgen: ./src/ramgen.c + $(CC) $(CFLAGS) ./src/ramgen.c -lm -o ./ramgen + +flashgen: ./src/flashgen.c + $(CC) $(CFLAGS) ./src/flashgen.c -lm -o ./flashgen + +flashgen_tlb: ./src/flashgen.c + $(CC) $(CFLAGS) -DSDRAM_BASE=0x80000000 -DFLASH_BASE_MEM=0x88000000 ./src/flashgen.c -lm -o ./flashgen_tlb + +packhex: ./src/packhex.c + $(CC) $(CFLAGS) ./src/packhex.c -lm -o ./packhex + +install: + cp romgen $(PREFIX)/bin + cp ramgen $(PREFIX)/bin + cp flashgen $(PREFIX)/bin +clean: + rm -rf romgen* ramgen* flashgen* diff --git a/tools/mips/flashgen b/tools/mips/flashgen deleted file mode 100755 index 7eeb8d3..0000000 Binary files a/tools/mips/flashgen and /dev/null differ diff --git a/tools/mips/flashgen_tlb b/tools/mips/flashgen_tlb deleted file mode 100755 index 6a2ec5e..0000000 Binary files a/tools/mips/flashgen_tlb and /dev/null differ diff --git a/tools/mips/packhex b/tools/mips/packhex deleted file mode 100755 index 23148f2..0000000 Binary files a/tools/mips/packhex and /dev/null differ diff --git a/tools/mips/ramgen b/tools/mips/ramgen deleted file mode 100755 index 76f517f..0000000 Binary files a/tools/mips/ramgen and /dev/null differ diff --git a/tools/mips/romgen b/tools/mips/romgen deleted file mode 100755 index 16bef3c..0000000 Binary files a/tools/mips/romgen and /dev/null differ diff --git a/tools/mips/src/flashgen.c b/tools/mips/src/flashgen.c new file mode 100644 index 0000000..66c031b --- /dev/null +++ b/tools/mips/src/flashgen.c @@ -0,0 +1,187 @@ +#include +#include +#include +#include +#include + +#ifndef SDRAM_BASE +#define SDRAM_BASE 0x40000000 +#endif +#ifndef FLASH_BASE_MEM +#define FLASH_BASE_MEM 0x00000000 +#endif + +#define MAGIC 0x4A464931 // "JFI1" in big-endian; + +uint8_t _g_bytes[4] = {0x12, 0x34, 0x56, 0x78}; +uint8_t _g_bytes2[4] = {0}; + +int IsEndianBig(void) +{ + int i, result; + + uint8_t *pBuf8; + uint16_t *pBuf16; + uint32_t *pBuf32; + uint16_t halve; + uint32_t word; + + + halve = 0x1234; + word = 0x12345678; + + result = -1; + + for (i=0; i < 4; i++) + _g_bytes2[i] = _g_bytes[i]; + + pBuf8 = (uint8_t*)&word; + if (((*pBuf8+0) == 0x12) && (*(pBuf8+1) == 0x34) && (*(pBuf8+2) == 0x56) && (*(pBuf8+3) == 0x78)) + { + pBuf8 = (uint8_t*)&halve; + if (((*pBuf8+0) == 0x12) && (*(pBuf8+1) == 0x34)) + { + result = 1; + } + } + + pBuf8 = (uint8_t*)&word; + if (((*pBuf8+0) == 0x78) && (*(pBuf8+1) == 0x56) && (*(pBuf8+2) == 0x34) && (*(pBuf8+3) == 0x12)) + { + pBuf8 = (uint8_t*)&halve; + if (((*pBuf8+0) == 0x34) && (*(pBuf8+1) == 0x12)) + { + result = 0; + } + } + return result; +} + +uint32_t conv_endian32(uint32_t src) +{ + uint32_t dst; + + dst = (0xFF000000 & (src << 24)) + | (0x00FF0000 & (src << 8)) + | (0x0000FF00 & (src >> 8)) + | (0x000000FF & (src >> 24)); + + return dst; +}; + +// -------------------------------------------------------------- +typedef struct _sflash_img_hdr_t +{ + uint32_t magic; + uint32_t target_addr; + uint32_t img_offset; + uint32_t img_size; + uint32_t hdr_next; + uint8_t img_name[128]; + uint8_t res[108]; + +} flash_img_hdr_t; + +// -------------------------------------------------------------- +void basename(char *pSrc, char *pDst) +{ + int i, size; + + size = strlen(pSrc); + + while(pSrc[size] != '.') + size--; + + for (i=0; i < size; i++) + pDst[i] = pSrc[i]; + + pDst[i] = 0; + +} + +// -------------------------------------------------------------- +int main(int argc, char *argv[]) +{ + char *pFilenameIn; + char name_flash[1024]; + char name_prj[1024]; + char *pBuf; + uint32_t *pBuf32; + flash_img_hdr_t img_hdr = {0}; + + FILE *pFile; + int filesize; + long start, end; + int i; + + if (argc < 2) + { + fprintf(stderr, "Usage: flashgen [-{EL|EB}]\n"); + return 1; + } + + pFilenameIn = argv[1]; + pFile = fopen(pFilenameIn, "rb"); + if (!pFile) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + basename(pFilenameIn, name_prj); + sprintf(name_flash, "%s.flash.bin", name_prj); + + // determine filesize + fseek(pFile, 0, SEEK_SET); + start = ftell(pFile); + fseek(pFile, 0, SEEK_END); + end = ftell(pFile); + fseek(pFile, 0, SEEK_SET); + filesize = (end-start); + + pBuf = (char*)malloc(filesize); + filesize = fread(pBuf, 1, filesize, pFile); + + fclose(pFile); + + pFile = fopen(name_flash, "wb"); + if (!pFile) + { + fprintf(stderr, "Error opening file %s\n", name_flash); + return 1; + } + + img_hdr.magic = MAGIC; + + img_hdr.target_addr = SDRAM_BASE; + img_hdr.img_size = filesize; + img_hdr.img_offset = FLASH_BASE_MEM + sizeof(flash_img_hdr_t); + img_hdr.hdr_next = FLASH_BASE_MEM; + + if (argc == 3) + { + if ((argv[2][0] == '-') && (toupper(argv[2][1]) == 'E') && (toupper(argv[2][2]) == 'B')) + { + if (1 == IsEndianBig()) + { + img_hdr.magic = conv_endian32(MAGIC); + pBuf32 = (uint32_t*)pBuf; + for (i=0; i < filesize/4; i ++) + pBuf32[i] = pBuf32[i]; // don't convert data + } + else + { + pBuf32 = (uint32_t*)pBuf; + for (i=0; i < filesize/4; i ++) + pBuf32[i] = conv_endian32(pBuf32[i]); // convert data + } + } + } + fwrite(&img_hdr, sizeof(flash_img_hdr_t), 1, pFile); + fwrite(pBuf, 1, filesize, pFile); + + fclose(pFile); + + return 0; +} + diff --git a/tools/mips/src/packhex.c b/tools/mips/src/packhex.c new file mode 100644 index 0000000..c00093b --- /dev/null +++ b/tools/mips/src/packhex.c @@ -0,0 +1,568 @@ + +/***** P A C K H E X . C ************************************************ + * + * Packhex is a hex-file compaction utility. It attempts to concatenate + * hex records to produce more size-efficient packaging. + * + * Limitations: Input files must be correctly formatted. This utility + * is not robust enough to detect hex-record formatting + * errors. + * + * Published: May 1993 Embedded Systems Programming magazine + * "Creating Faster Hex Files" + * + * URL: ESP magazine: http://www.embedded.com + * Source Code: ftp://ftp.mfi.com/pub/espmag/1993/pakhex.zip + * + * Author: Mark Gringrich + * + * Compiler: Microsoft C 6.0 + * cl /F 1000 packhex.c + * + * + * $Id: packhex.c,v 1.9 2004/04/20 07:07:08 ralf Exp $ + * + **************************************************************************/ + + +/* #define SMALLER_RECORDS */ +#ifdef SMALLER_RECORDS +#define MAX_LEN_S1_RECS 128 +#define MAX_LEN_S2_RECS 128 +#define MAX_LEN_S3_RECS 128 +#else +#define MAX_LEN_S1_RECS 252 +#define MAX_LEN_S2_RECS 251 +#define MAX_LEN_S3_RECS 250 +#endif + + +/*--------------------------------- includes ---------------------------------*/ + +#include +#include +#include + +#if 0 +#include "config.h" + +#ifndef VMS +#ifndef HAVE_STRERROR +extern int sys_nerr; +extern char *sys_errlist[]; + +#define strerror( _err ) \ + ((_err) < sys_nerr) ? sys_errlist [(_err)] : "unknown error" + +#else /* HAVE_STRERROR */ +char *strerror (); +#endif +#else /* VMS */ +char *strerror (int,...); +#endif +#endif + +#if defined(__unix__) && !defined(EXIT_FAILURE) +#define EXIT_FAILURE -1 +#define EXIT_SUCCESS 0 +#endif + +/*--------------------------------- defines ----------------------------------*/ + +#define YES 1 +#define MAX_LINE_SIZE 600 +#define EOS '\0' + + +/*---------------------------------- macros ----------------------------------*/ + +/* Convert ASCII hexadecimal digit to value. */ + +#define HEX_DIGIT( C ) ( ( ( ( C ) > '9' ) ? ( C ) + 25 : ( C ) ) & 0xF ) + + +/*--------------------------------- typedefs ---------------------------------*/ + +typedef unsigned char Boolean; +typedef unsigned char Uchar; +typedef unsigned int Uint; +typedef unsigned long Ulong; + +typedef struct /* Functions and constant returning Hex-record vital stats. */ +{ + Boolean ( *is_data_record )( char * ); + Ulong ( *get_address )( char * ); + Uint ( *get_data_count )( char * ); + const Uint max_data_count; + char *( *get_data_start )( char * ); + void ( *put_data_record )( Uint, Ulong, char * ); +} Rec_vitals; + + +/*--------------------------- function prototypes ----------------------------*/ + +Rec_vitals * identify_first_data_record( char *, int ); +Ulong get_ndigit_hex( char *, int ); + + +/*----------------------------- Intel Hex format -----------------------------*/ + +/* + * Intel Hex data-record layout + * + * :aabbbbccd...dee + * + * : - header character + * aa - record data byte count, a 2-digit hex value + * bbbb - record address, a 4-digit hex value + * cc - record type, a 2-digit hex value: + * "00" is a data record + * "01" is an end-of-data record + * "02" is an extended-address record + * "03" is a start record + * d...d - data (always an even number of chars) + * ee - record checksum, a 2-digit hex value + * checksum = 2's complement + * [ (sum of bytes: aabbbbccd...d) modulo 256 ] + */ + + +Boolean is_intel_data_rec( char * rec_str ) +{ + return( ( rec_str[ 0 ] == ':' ) && ( rec_str[ 8 ] == '0' ) ); +} + +Uint get_intel_rec_data_count( char * rec_str ) +{ + return( ( Uint ) get_ndigit_hex( rec_str + 1, 2 ) ); +} + +Ulong get_intel_rec_address( char * rec_str ) +{ + return( get_ndigit_hex( rec_str + 3, 4 ) ); +} + +char * get_intel_rec_data_start( char * rec_str ) +{ + return( rec_str + 9 ); +} + +void put_intel_data_rec( Uint count, Ulong address, char * data_str ) +{ + char *ptr; + Uint sum = count + ( address >> 8 & 0xff ) + ( address & 0xff ); + + for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) + sum += ( Uint ) get_ndigit_hex( ptr, 2 ); + + printf( + ":%02X%04lX00%s%02X\n", count, address, data_str, (~sum + 1) & 0xff + ); +} + + +Rec_vitals intel_hex = +{ + is_intel_data_rec, + get_intel_rec_address, + get_intel_rec_data_count, + 255, /* Maximum data bytes in a record. */ + get_intel_rec_data_start, + put_intel_data_rec +}; + + +/*------------------------- Motorola S1-record format ------------------------*/ + +/* + * Motorola S-record data-record layout + * + * Sabbc...cd...dee + * + * S - header character + * a - record type, a 1-digit value: + * "0" is a header record + * "1" is a 2-byte-address data record + * "2" is a 3-byte-address data record + * "3" is a 4-byte-address data record + * "7" is a 4-byte-address end-of-data record + * "8" is a 3-byte-address end-of-data record + * "9" is a 2-byte-address end-of-data record + * bb - record length in bytes, a 2-digit hex value + * (record length doesn't count the header/type + * chars and checksum byte) + * c...c - record address, a 4-, 6-, or 8-digit value, + * depending on record type + * d...d - data (always an even number of chars) + * ee - record checksum, a 2-digit hex value + * checksum = 1's complement + * [ (sum of all bytes: bbc..cd...d) modulo 256 ] + */ + +#define S1_COUNT_OFFSET 3 + + +Boolean is_moto_s1_data_rec( char * rec_str ) +{ + return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '1' ) ); +} + +Uint get_moto_s1_rec_data_count( char * rec_str ) +{ + return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S1_COUNT_OFFSET ); +} + +Ulong get_moto_s1_rec_address( char * rec_str ) +{ + return( get_ndigit_hex( rec_str + 4, 4 ) ); +} + +char * get_moto_s1_rec_data_start( char * rec_str ) +{ + return( rec_str + 8 ); +} + +void put_moto_s1_data_rec( Uint count, Ulong address, char * data_str ) +{ + char *ptr; + Uint sum = S1_COUNT_OFFSET + count + + ( address >> 8 & 0xff ) + ( address & 0xff ); + + for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) + sum += ( Uint ) get_ndigit_hex( ptr, 2 ); + + printf( + "S1%02X%04lX%s%02X\n", + count + S1_COUNT_OFFSET, address, data_str, ~sum & 0xff + ); +} + + +Rec_vitals motorola_s1_rec = +{ + is_moto_s1_data_rec, + get_moto_s1_rec_address, + get_moto_s1_rec_data_count, + MAX_LEN_S1_RECS, /* Maximum data bytes in a record. */ + get_moto_s1_rec_data_start, + put_moto_s1_data_rec +}; + + +/*------------------------- Motorola S2-record format ------------------------*/ + +#define S2_COUNT_OFFSET 4 + +Boolean is_moto_s2_data_rec( char * rec_str ) +{ + return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '2' ) ); +} + +Uint get_moto_s2_rec_data_count( char * rec_str ) +{ + return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S2_COUNT_OFFSET ); +} + +Ulong get_moto_s2_rec_address( char * rec_str ) +{ + return( get_ndigit_hex( rec_str + 4, 6 ) ); +} + +char * get_moto_s2_rec_data_start( char * rec_str ) +{ + return( rec_str + 10 ); +} + +void put_moto_s2_data_rec( Uint count, Ulong address, char * data_str ) +{ + char *ptr; + Uint sum = S2_COUNT_OFFSET + count + ( address >> 16 & 0xff ) + + ( address >> 8 & 0xff ) + + ( address & 0xff ); + + for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) + sum += ( Uint ) get_ndigit_hex( ptr, 2 ); + + printf( + "S2%02X%06lX%s%02X\n", + count + S2_COUNT_OFFSET, address, data_str, ~sum & 0xff + ); +} + + +Rec_vitals motorola_s2_rec = +{ + is_moto_s2_data_rec, + get_moto_s2_rec_address, + get_moto_s2_rec_data_count, + MAX_LEN_S2_RECS, /* Maximum data bytes in a record. */ + get_moto_s2_rec_data_start, + put_moto_s2_data_rec +}; + + +/*------------------------- Motorola S3-record format ------------------------*/ + +#define S3_COUNT_OFFSET 5 + +Boolean is_moto_s3_data_rec( char * rec_str ) +{ + return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '3' ) ); +} + +Uint get_moto_s3_rec_data_count( char * rec_str ) +{ + return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S3_COUNT_OFFSET ); +} + +Ulong get_moto_s3_rec_address( char * rec_str ) +{ + return( get_ndigit_hex( rec_str + 4, 8 ) ); +} + +char * get_moto_s3_rec_data_start( char * rec_str ) +{ + return( rec_str + 12 ); +} + +void put_moto_s3_data_rec( Uint count, Ulong address, char * data_str ) +{ + char *ptr; + Uint sum = S3_COUNT_OFFSET + count + ( address >> 24 & 0xff ) + + ( address >> 16 & 0xff ) + + ( address >> 8 & 0xff ) + + ( address & 0xff ); + + for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) + sum += ( Uint ) get_ndigit_hex( ptr, 2 ); + + printf( + "S3%02X%08lX%s%02X\n", + count + S3_COUNT_OFFSET, address, data_str, ~sum & 0xff + ); +} + + +Rec_vitals motorola_s3_rec = +{ + is_moto_s3_data_rec, + get_moto_s3_rec_address, + get_moto_s3_rec_data_count, + MAX_LEN_S3_RECS, /* Maximum data bytes in a record. */ + get_moto_s3_rec_data_start, + put_moto_s3_data_rec +}; + + +/*-------------------- Put your favorite hex format here ---------------------*/ + +/* + * * * * The following is a template for an additional hex format: * * * + * + * + * Boolean is_X_data_rec( char * rec_str ) {} + * + * Uint get_X_rec_data_count( char * rec_str ) {} + * + * Ulong get_X_rec_address( char * rec_str ) {} + * + * char * get_X_rec_data_start( char * rec_str ) {} + * + * void put_X_data_rec( Uint count, Ulong address, char * data_str ) {} + * + * Rec_vitals X_rec = + * { + * is_X_data_rec, + * get_X_rec_address, + * get_X_rec_data_count, + * MAXIMUM DATA BYTES IN A RECORD, + * get_X_rec_data_start, + * put_X_data_rec + * }; + * + */ + +/*----------------------------------------------------------------------------*/ + + +/* + * Put address of additional Rec_vitals structures + * in this array, before the NULL entry. + */ + +Rec_vitals *formats[] = +{ + &intel_hex, + &motorola_s1_rec, + &motorola_s2_rec, + &motorola_s3_rec, + ( Rec_vitals * ) NULL +}; + + +/**** main ***************************************************************** +* +* +* Expects: Nothing (no command-line parameters). +* +* Returns: Exit status (EXIT_SUCCESS or EXIT_FAILURE). +* +* Reads hex records on the standard input and attempts to +* splice adjacent data fields together. Results appear on +* the standard output. +* +*******************************************************************************/ + +int main( + int argc, + char **argv +) +{ + + char inbuff[ MAX_LINE_SIZE ], outbuff[ MAX_LINE_SIZE ]; + char *in_dptr, *out_dptr; + int d_total, d_count, d_excess, n; + int length; + Ulong in_rec_addr, out_rec_addr = 0; + Rec_vitals *rptr; + + + /* Sift through file until first hex record is identified. */ + + rptr = identify_first_data_record( inbuff, MAX_LINE_SIZE ); + if ( rptr == NULL ) + { + fputs( "No hex records found.\n", stderr ); + exit( EXIT_FAILURE ); + } + + + /* Attempt data-record splicing until end-of-file is reached. */ + d_total = 0; + for (;;) { + if ( rptr->is_data_record( inbuff ) == YES ) + { /* Input record is a data record. */ + d_count = rptr->get_data_count( inbuff ); + in_rec_addr = rptr->get_address( inbuff ); + in_dptr = rptr->get_data_start( inbuff ); + + if ( d_total == 0 || in_rec_addr != out_rec_addr + d_total ) + { /* Begin a new output record. */ + if ( d_total != 0 ) + rptr->put_data_record( d_total, out_rec_addr, outbuff ); + out_dptr = outbuff; + n = d_total = d_count; + out_rec_addr = in_rec_addr; + } + else if + ( ( d_excess = d_total + d_count - rptr->max_data_count ) > 0 ) + { /* Output a maximum-length record, then start a new record. */ + strncat( outbuff, in_dptr, 2 * ( d_count - d_excess ) ); + rptr->put_data_record( + rptr->max_data_count, out_rec_addr, outbuff + ); + in_dptr += 2 * ( d_count - d_excess ); + out_dptr = outbuff; + n = d_total = d_excess; + out_rec_addr += rptr->max_data_count; + } + else + { /* Append input record's data field with accumulated data. */ + out_dptr = outbuff + ( 2 * d_total ); + d_total += n = d_count; + } + strncpy( out_dptr, in_dptr, 2 * n ); + out_dptr[ 2 * n ] = EOS; + } + else + { /* Not a data record; + * flush accumulated data then echo non-data record. + */ + if ( d_total != 0 ) + { + rptr->put_data_record( d_total, out_rec_addr, outbuff ); + d_total = 0; + } + puts( inbuff ); + } + + inbuff[ MAX_LINE_SIZE - 1 ] = '\0'; + if ( !fgets( inbuff, MAX_LINE_SIZE, stdin ) ) + break; + if ( inbuff[ MAX_LINE_SIZE - 1 ] ) { + fprintf( stderr, "Input line too long" ); + exit( 1 ); + } + length = strlen(inbuff); + inbuff[length - 1] = '\0'; + + } + + + return ( EXIT_SUCCESS ); + +} + + +/**** identify_first_data_record ******************************************* +* +* Expects: Pointer to hex-record line buffer. +* +* Returns: Pointer to hex-record structure (NULL if no match found). +* +* Reads the standard input, line by line, searching for a valid +* record header character. If a valid header is found, a pointer +* to the hex-record's type structure is returned, otherwise NULL. +* +* The input-stream pointer is left pointing to the first valid hex record. +* +*******************************************************************************/ + +Rec_vitals * identify_first_data_record( char * buff_ptr, int max_length ) +{ + Rec_vitals ** ptr; + int length; + + + + for ( ;; ) { + + buff_ptr[ max_length - 1 ] = '\0'; + if ( !fgets( buff_ptr, max_length, stdin ) ) + break; + if ( buff_ptr[ max_length - 1 ] ) { + fprintf( stderr, "Input line too long" ); + exit( 1 ); + } + length = strlen(buff_ptr); + buff_ptr[length - 1] = '\0'; + + for ( ptr = formats ; *ptr != ( Rec_vitals * ) NULL ; ptr++ ) + if ( ( *ptr )->is_data_record( buff_ptr ) == YES ) + return( *ptr ); /* Successful return. */ + + puts( buff_ptr ); /* Echo non-hex-record line. */ + } + + return( ( Rec_vitals * ) NULL ); /* Unsuccessful return. */ +} + + +/**** get_ndigit_hex ******************************************************* +* +* Expects: Pointer to first ASCII hexadecimal digit, number of digits. +* +* Returns: Value of hexadecimal string as an unsigned long. +* +*******************************************************************************/ + +Ulong get_ndigit_hex( char * cptr, int digits ) +{ + Ulong value; + + for ( value = 0 ; --digits >= 0 ; cptr++ ) + value = ( value * 16L ) + HEX_DIGIT( *cptr ); + + return( value ); +} + diff --git a/tools/mips/src/ramgen.c b/tools/mips/src/ramgen.c new file mode 100644 index 0000000..a1ecb69 --- /dev/null +++ b/tools/mips/src/ramgen.c @@ -0,0 +1,387 @@ +#include +#include +#include +#include + +#define ARCH_NAME "data" +#define ENT_NAME "ram" +#define JTAG_ADDR_WIDTH 16 + +// -------------------------------------------------------------- +void basename(char *pSrc, char *pDst) +{ + int i, size; + + size = strlen(pSrc); + + while(pSrc[size] != '.') + size--; + + for (i=0; i < size; i++) + pDst[i] = pSrc[i]; + + pDst[i] = 0; + +} + +int SaveRAM(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, filesize, romsize; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fprintf(pFileOut, "LIBRARY IEEE;\n"); + fprintf(pFileOut, "USE IEEE.STD_LOGIC_1164.ALL;\n"); + fprintf(pFileOut, "USE IEEE.NUMERIC_STD.ALL;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "ENTITY %s IS\n", ENT_NAME); + fprintf(pFileOut, "\tPort\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\t\tclk\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\tce\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\twe\t\t: in unsigned(%d downto 0);\n", nbits_data/8-1); + fprintf(pFileOut, "\t\taddr\t\t: in unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\t\tdin\t\t: in unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\t\tdout\t\t: out unsigned(%d downto 0)\n", nbits_data-1); + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "END %s;\n", ENT_NAME); + fprintf(pFileOut, "\n"); + + + fprintf(pFileOut, "ARCHITECTURE %s OF %s IS\n", ARCH_NAME, ENT_NAME); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tconstant depth : natural := %d;\n", romsize/4); + fprintf(pFileOut, "\tsubtype word_t is unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\ttype word_array_t is array (0 to %d) of word_t;\n", romsize/4-1); + + fprintf(pFileOut, "\tsignal sram : word_array_t :=\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + word = 0; + for (; i < romsize; i += sizeof(int)) + { + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "begin\n", ARCH_NAME); + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "RAM_RW:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(clk)\n"); + fprintf(pFileOut, "\tvariable index : natural range 0 to depth-1;\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(clk) and ce = '1' then\n"); + fprintf(pFileOut, "\t\t\tindex := to_integer(addr(%d downto 2));\n", nbits_addr+1); + fprintf(pFileOut, "\t\t\tif we(0) = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tsram(index)(7 downto 0)\t\t<= din(7 downto 0);\n"); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\t\tif we(1) = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tsram(index)(15 downto 8)\t<= din(15 downto 8);\n"); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\t\tif we(2) = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tsram(index)(23 downto 16)\t<= din(23 downto 16);\n"); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\t\tif we(3) = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tsram(index)(31 downto 24)\t\t<= din(31 downto 24);\n"); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\t\tdout <= sram(index);\n"); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "end %s;\n", ARCH_NAME); + + return 0; +} + +int SaveRAM_V4LD(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, filesize, romsize; + + char tpl[] = {"--------------------------------------------------------------------------\n-- Virtex-4: JTAG Loader\n--------------------------------------------------------------------------\n\ti00_BUFG : BUFG\n\tport map\n\t(\n\t\tO => bs_clk1,\n I => bs_clk0\n\t);\n\t\n\ti01_BUFG : BUFG\n\tport map\n\t(\n\t\tO => bs_update1,\n I => bs_update0\n\t);\n\n\tBSCAN_VIRTEX4_inst1 : BSCAN_VIRTEX4 \n\tgeneric map\n\t(\n\t\tJTAG_CHAIN => 1 -- Value to set BSCAN site of device. Possible values: (1,2,3 or 4)\n\t)\n\tport map \n\t(\n\t\tCAPTURE => bs_capture, -- CAPTURE output from TAP controller\n\t\tDRCK => bs_clk0, -- Data register output for USER functions\n\t\tRESET => bs_rst, -- Reset output from TAP controller\n\t\tSEL => bs_sel, -- USER active output\n\t\tSHIFT => bs_shift, -- SHIFT output from TAP controller\n\t\tTDI => bs_tdi, -- TDI output from TAP controller\n\t\tUPDATE => bs_update0, -- UPDATE output from TAP controller\n\t\tTDO => bs_tdo -- Data input for USER function\n\t);\n\n\tjtag_ld_addr <= user_regi(user_regi'left downto jtag_ld_dout'length);\n\tjtag_ld_din <= user_regi(jtag_ld_dout'length-1 downto 0);\n\tjtag_ld_clk <= bs_update1;\n\tjtag_ld_we <= bs_sel;\n\t\nsipo:\n\tprocess (bs_rst, bs_clk1, bs_tdi, bs_shift)\n\tbegin\n\t\tif bs_rst = '1' then\n\t\t\tuser_regi <= (others => '0');\n\t\telsif rising_edge(bs_clk1) then\n\t\t\tif bs_shift = '1' then\n\t\t\t\tuser_regi <= bs_tdi & user_regi(user_regi'left downto 1);\n\t\t\tend if;\n\t\tend if;\n\tend process;\t\n\npiso:\n\tprocess (bs_rst, bs_clk1, bs_shift, user_rego)\n\tbegin\n\t\tbs_tdo <= user_rego(0);\n\t\tif bs_rst = '1' then\n\t\t\tuser_rego <= (others => '0');\n\t\telsif rising_edge(bs_clk1) then\n\t\t\tif bs_shift = '1' then\n\t\t\t\tuser_rego <= user_rego(0) & user_rego(user_rego'left downto 1);\n\t\t\telse\n\t\t\t\tuser_rego <= (user_rego'left downto jtag_ld_dout'length => '0') & jtag_ld_dout;\t\n\t\n\t\t\tend if;\n\t\tend if;\n\tend process;\n\n"}; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fprintf(pFileOut, "LIBRARY IEEE;\n"); + fprintf(pFileOut, "USE IEEE.STD_LOGIC_1164.ALL;\n"); + fprintf(pFileOut, "USE IEEE.NUMERIC_STD.ALL;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "library UNISIM;\n"); + fprintf(pFileOut, "use UNISIM.VComponents.all;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "ENTITY %s IS\n", ENT_NAME); + fprintf(pFileOut, "\tPort\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\t\tclk\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\tce\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\taddr\t\t: in unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\t\tdout\t\t: out unsigned(%d downto 0)\n", nbits_data-1); + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "END %s;\n", ENT_NAME); + fprintf(pFileOut, "\n"); + + + fprintf(pFileOut, "ARCHITECTURE %s OF %s IS\n", ARCH_NAME, ENT_NAME); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tsubtype word_t is unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\ttype word_array_t is array (0 to %d) of word_t;\n", romsize/4-1); + + fprintf(pFileOut, "\tsignal jtag_ld_clk\t\t: STD_LOGIC;\n"); + fprintf(pFileOut, "\tsignal jtag_ld_we\t\t: STD_LOGIC;\n"); + fprintf(pFileOut, "\tsignal jtag_ld_addr\t\t: unsigned (%d downto 0);\n", JTAG_ADDR_WIDTH-1); + fprintf(pFileOut, "\tsignal jtag_ld_dout\t\t: unsigned (%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\tsignal jtag_ld_din\t\t: unsigned (%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\tsignal bs_rst, bs_sel, bs_shift, bs_tdi, bs_tdo : std_logic;\n"); + fprintf(pFileOut, "\tsignal bs_capture, bs_clk0, bs_clk1, bs_update0, bs_update1 : std_logic;\n"); + fprintf(pFileOut, "\tsignal user_regi, user_rego : unsigned (%d downto 0);\n", 31 + JTAG_ADDR_WIDTH); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tsignal word_array : word_array_t :=\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + word = 0; + for (; i < romsize; i += sizeof(int)) + { + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "begin\n", ARCH_NAME); + + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "PROM_READ:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(clk)\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(clk) then\n"); + fprintf(pFileOut, "\t\t\tif ce = '1' then\n"); + fprintf(pFileOut, "\t\t\tdout <= word_array(to_integer(addr(%d downto 2)));\n", nbits_addr+1); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "\n"); + fputs(tpl, pFileOut); + + fprintf(pFileOut, "PROM_WRITE:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(jtag_ld_clk, jtag_ld_we)\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(jtag_ld_clk) then\n"); + fprintf(pFileOut, "\t\t\tif jtag_ld_we = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tword_array(to_integer(jtag_ld_addr(%d downto 0))) <= jtag_ld_din;\n", nbits_addr-1); + fprintf(pFileOut, "\t\t\telse\n"); + fprintf(pFileOut, "\t\t\t\tjtag_ld_dout <= word_array(to_integer(jtag_ld_addr(%d downto 0)));\n", nbits_addr-1); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "end %s;\n", ARCH_NAME); + return 0; +} + +int SaveRAM_TCL(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, word_addr, filesize, romsize; + char binstr_addr[33]; + char binstr_data[33]; + + char tpl[] = {"# ---------------------------------------------------------------------\n# For Chipscope 9.1\n# ---------------------------------------------------------------------\n# Source JTAG/TCL frame work\ncd $env(CHIPSCOPE)\\\\bin\\\\nt\nsource csejtag.tcl\n\nnamespace import ::chipscope::*\n\n# Platform USB Cable\nset PLATFORM_USB_CABLE_ARGS [list \"port=USB2\" \"frequency=6000000\"]\n# frequency=\"24000000 | 12000000 | 6000000 | 3000000 | 1500000 | 750000\"\n\n# Create session\nset handle [::chipscope::csejtag_session create 0]\n\n# Open JTAG and lock\nset open_result [::chipscope::csejtag_target open $handle $CSEJTAG_TARGET_PLATFORMUSB 0 $PLATFORM_USB_CABLE_ARGS]\nset lock_result [::chipscope::csejtag_target lock $handle 1000]\n\nset devlist [::chipscope::csejtag_tap autodetect_chain $handle $CSEJTAG_SCAN_DEFAULT]\n\n# Get Device ID\nset devtype \"Virtex-4SX\"\nset devid 2\nset irlength [::chipscope::csejtag_tap get_irlength $handle $devid]\nset idcode [::chipscope::csejtag_tap get_device_idcode $handle $devid]\n\nset CSE_OP $CSEJTAG_SHIFT_READWRITE\nset CSE_ES $CSEJTAG_RUN_TEST_IDLE\n\n# Write Program\n"}; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fputs(tpl, pFileOut); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + fprintf(pFileOut, "# Assembled from %s\n", pFilenameIn); + fprintf(pFileOut, "# ---------------------------------------------------------------\n"); + fprintf(pFileOut, "# Shift the USER2 Instruction (b1111000011) into the Instruction Register of FPGA\n"); + fprintf(pFileOut, "# User 2\n"); + fprintf(pFileOut, "set result [::chipscope::csejtag_tap shift_device_ir $handle $devid $CSE_OP $CSE_ES 0 $irlength \"3C3\"]\n\n"); + + word_addr = 0; + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + fprintf(pFileOut, "::chipscope::csejtag_tap shift_device_dr $handle $devid $CSE_OP $CSE_ES 0 %d \"%4.4X%8.8X\"\n", nbits_data + JTAG_ADDR_WIDTH, word_addr, word); + word_addr++; + } + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "::chipscope::csejtag_target unlock $handle\n"); + fprintf(pFileOut, "::chipscope::csejtag_target close $handle\n"); + fprintf(pFileOut, "::chipscope::csejtag_session destroy $handle\n"); + fprintf(pFileOut, "exit\n"); + + return 0; +} + +int main(int argc, char *argv[]) +{ + char *pFilenameIn; + char name_prj[1024]; + char name_rom_tcl[1024]; + char name_rom[1024]; + char name_rom_v4ld[1024]; + + FILE *pFileIn; + int filesize, romsize, nbits_addr, nbits_data; + long start, end; + int word, i; + + if (argc < 2) + { + fprintf(stderr, "Usage: ramgen \n"); + return 1; + } + + pFilenameIn = argv[1]; + if (argc == 3) + nbits_addr = atoi(argv[2]); + + + basename(pFilenameIn, name_prj); + sprintf(name_rom, "%s.vhd", name_prj); + sprintf(name_rom_v4ld, "%s_ld.vhd", name_prj); + sprintf(name_rom_tcl, "%s.tcl", name_prj); + + SaveRAM(pFilenameIn, name_rom, ARCH_NAME, ENT_NAME, nbits_addr, 32); +// SaveROM_V4LD(pFilenameIn, name_rom_v4ld, ARCH_NAME, ENT_NAME, nbits_addr, 32); + SaveRAM_TCL(pFilenameIn, name_rom_tcl, ARCH_NAME, ENT_NAME, nbits_addr, 32); + + + return 0; +} + diff --git a/tools/mips/src/romgen.c b/tools/mips/src/romgen.c new file mode 100644 index 0000000..b988562 --- /dev/null +++ b/tools/mips/src/romgen.c @@ -0,0 +1,430 @@ +#include +#include +#include +#include + +#define INT8 char +#define INT16 short +#define INT32 long +#define UINT8 unsigned char +#define UINT16 unsigned short +#define UINT32 unsigned long +#define INT int +#define UINT unsigned int +#define FLOAT32 float +#define FLOAT64 double + +#define ARCH_NAME "data" +#define ENT_NAME "rom" +#define JTAG_ADDR_WIDTH 16 + +UINT32 g_endianess_EB; + +// -------------------------------------------------------------- +UINT32 conv_endian32(UINT32 src) +{ + UINT32 dst; + + dst = (0xFF000000 & (src << 24)) + | (0x00FF0000 & (src << 8)) + | (0x0000FF00 & (src >> 8)) + | (0x000000FF & (src >> 24)); + + return dst; +}; + +UINT32 to_big_endian32(UINT32 src) +{ +#ifndef NATIVE_IS_BIG_ENDIAN + return conv_endian32(src); +#else + return src; +#endif +} + +UINT32 from_big_endian32(UINT32 src) +{ +#ifndef NATIVE_IS_BIG_ENDIAN + return conv_endian32(src); +#else + return src; +#endif +} + +void basename(char *pSrc, char *pDst) +{ + int i, size; + + size = strlen(pSrc); + + while(pSrc[size] != '.') + size--; + + for (i=0; i < size; i++) + pDst[i] = pSrc[i]; + + pDst[i] = 0; + +} + +int SaveROM(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, filesize, romsize; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fprintf(pFileOut, "LIBRARY IEEE;\n"); + fprintf(pFileOut, "USE IEEE.STD_LOGIC_1164.ALL;\n"); + fprintf(pFileOut, "USE IEEE.NUMERIC_STD.ALL;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "ENTITY %s IS\n", ENT_NAME); + fprintf(pFileOut, "\tPort\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\t\tclk\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\tce\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\taddr\t\t: in unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\t\tdout\t\t: out unsigned(%d downto 0)\n", nbits_data-1); + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "END %s;\n", ENT_NAME); + fprintf(pFileOut, "\n"); + + + fprintf(pFileOut, "ARCHITECTURE %s OF %s IS\n", ARCH_NAME, ENT_NAME); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tsubtype word_t is unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\ttype word_array_t is array (0 to %d) of word_t;\n", romsize/4-1); + + fprintf(pFileOut, "\tconstant word_array : word_array_t :=\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + if (g_endianess_EB) + fprintf(pFileOut, "\t\tX\"%8.8X\"", to_big_endian32(word)); + else + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + word = 0; + for (; i < romsize; i += sizeof(int)) + { + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "begin\n", ARCH_NAME); + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "PROM_READ:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(clk)\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(clk) then\n"); + fprintf(pFileOut, "\t\t\tif ce = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tdout <= word_array(to_integer(addr(%d downto 2)));\n", nbits_addr+1); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "end %s;\n", ARCH_NAME); + + return 0; +} + +int SaveROM_V4LD(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, filesize, romsize; + + char tpl[] = {"--------------------------------------------------------------------------\n-- Virtex-4: JTAG Loader\n--------------------------------------------------------------------------\n\ti00_BUFG : BUFG\n\tport map\n\t(\n\t\tO => bs_clk1,\n I => bs_clk0\n\t);\n\t\n\ti01_BUFG : BUFG\n\tport map\n\t(\n\t\tO => bs_update1,\n I => bs_update0\n\t);\n\n\tBSCAN_VIRTEX4_inst1 : BSCAN_VIRTEX4 \n\tgeneric map\n\t(\n\t\tJTAG_CHAIN => 1 -- Value to set BSCAN site of device. Possible values: (1,2,3 or 4)\n\t)\n\tport map \n\t(\n\t\tCAPTURE => bs_capture, -- CAPTURE output from TAP controller\n\t\tDRCK => bs_clk0, -- Data register output for USER functions\n\t\tRESET => bs_rst, -- Reset output from TAP controller\n\t\tSEL => bs_sel, -- USER active output\n\t\tSHIFT => bs_shift, -- SHIFT output from TAP controller\n\t\tTDI => bs_tdi, -- TDI output from TAP controller\n\t\tUPDATE => bs_update0, -- UPDATE output from TAP controller\n\t\tTDO => bs_tdo -- Data input for USER function\n\t);\n\n\tjtag_ld_addr <= user_regi(user_regi'left downto jtag_ld_dout'length);\n\tjtag_ld_din <= user_regi(jtag_ld_dout'length-1 downto 0);\n\tjtag_ld_clk <= bs_update1;\n\tjtag_ld_we <= bs_sel;\n\t\nsipo:\n\tprocess (bs_rst, bs_clk1, bs_tdi, bs_shift)\n\tbegin\n\t\tif bs_rst = '1' then\n\t\t\tuser_regi <= (others => '0');\n\t\telsif rising_edge(bs_clk1) then\n\t\t\tif bs_shift = '1' then\n\t\t\t\tuser_regi <= bs_tdi & user_regi(user_regi'left downto 1);\n\t\t\tend if;\n\t\tend if;\n\tend process;\t\n\npiso:\n\tprocess (bs_rst, bs_clk1, bs_shift, user_rego)\n\tbegin\n\t\tbs_tdo <= user_rego(0);\n\t\tif bs_rst = '1' then\n\t\t\tuser_rego <= (others => '0');\n\t\telsif rising_edge(bs_clk1) then\n\t\t\tif bs_shift = '1' then\n\t\t\t\tuser_rego <= user_rego(0) & user_rego(user_rego'left downto 1);\n\t\t\telse\n\t\t\t\tuser_rego <= (user_rego'left downto jtag_ld_dout'length => '0') & jtag_ld_dout;\t\n\t\n\t\t\tend if;\n\t\tend if;\n\tend process;\n\n"}; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fprintf(pFileOut, "LIBRARY IEEE;\n"); + fprintf(pFileOut, "USE IEEE.STD_LOGIC_1164.ALL;\n"); + fprintf(pFileOut, "USE IEEE.NUMERIC_STD.ALL;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "library UNISIM;\n"); + fprintf(pFileOut, "use UNISIM.VComponents.all;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "ENTITY %s IS\n", ENT_NAME); + fprintf(pFileOut, "\tPort\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\t\tclk\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\tce\t\t: in STD_LOGIC;\n"); + fprintf(pFileOut, "\t\taddr\t\t: in unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\t\tdout\t\t: out unsigned(%d downto 0)\n", nbits_data-1); + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "END %s;\n", ENT_NAME); + fprintf(pFileOut, "\n"); + + + fprintf(pFileOut, "ARCHITECTURE %s OF %s IS\n", ARCH_NAME, ENT_NAME); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tsubtype word_t is unsigned(%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\ttype word_array_t is array (0 to %d) of word_t;\n", romsize/4-1); + + fprintf(pFileOut, "\tsignal jtag_ld_clk\t\t: STD_LOGIC;\n"); + fprintf(pFileOut, "\tsignal jtag_ld_we\t\t: STD_LOGIC;\n"); + fprintf(pFileOut, "\tsignal jtag_ld_addr\t\t: unsigned (%d downto 0);\n", JTAG_ADDR_WIDTH-1); + fprintf(pFileOut, "\tsignal jtag_ld_dout\t\t: unsigned (%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\tsignal jtag_ld_din\t\t: unsigned (%d downto 0);\n", nbits_data-1); + fprintf(pFileOut, "\tsignal bs_rst, bs_sel, bs_shift, bs_tdi, bs_tdo : std_logic;\n"); + fprintf(pFileOut, "\tsignal bs_capture, bs_clk0, bs_clk1, bs_update0, bs_update1 : std_logic;\n"); + fprintf(pFileOut, "\tsignal user_regi, user_rego : unsigned (%d downto 0);\n", 31 + JTAG_ADDR_WIDTH); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "\tsignal word_array : word_array_t :=\n"); + fprintf(pFileOut, "\t(\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + if (g_endianess_EB) + fprintf(pFileOut, "\t\tX\"%8.8X\"", to_big_endian32(word)); + else + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + word = 0; + for (; i < romsize; i += sizeof(int)) + { + fprintf(pFileOut, "\t\tX\"%8.8X\"", word); + if (i < (romsize-sizeof(int))) + fprintf(pFileOut, ", -- %8.8X\n", i); + else + fprintf(pFileOut, " -- %8.8X\n", i); + } + fprintf(pFileOut, "\t);\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "begin\n", ARCH_NAME); + + fprintf(pFileOut, "\n"); + fprintf(pFileOut, "PROM_READ:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(clk)\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(clk) and ce = '1' then\n"); + fprintf(pFileOut, "\t\t\tdout <= word_array(to_integer(addr(%d downto 2)));\n", nbits_addr+1); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "\n"); + fputs(tpl, pFileOut); + + fprintf(pFileOut, "PROM_WRITE:\n", ARCH_NAME); + fprintf(pFileOut, "\tprocess(jtag_ld_clk, jtag_ld_we)\n"); + fprintf(pFileOut, "\tbegin\n"); + fprintf(pFileOut, "\t\tif rising_edge(jtag_ld_clk) then\n"); + fprintf(pFileOut, "\t\t\tif jtag_ld_we = '1' then\n"); + fprintf(pFileOut, "\t\t\t\tword_array(to_integer(jtag_ld_addr(%d downto 0))) <= jtag_ld_din;\n", nbits_addr-1); + fprintf(pFileOut, "\t\t\telse\n"); + fprintf(pFileOut, "\t\t\t\tjtag_ld_dout <= word_array(to_integer(jtag_ld_addr(%d downto 0)));\n", nbits_addr-1); + fprintf(pFileOut, "\t\t\tend if;\n"); + fprintf(pFileOut, "\t\tend if;\n"); + fprintf(pFileOut, "\tend process;\n"); + fprintf(pFileOut, "\n"); + + fprintf(pFileOut, "end %s;\n", ARCH_NAME); + return 0; +} + +int SaveROM_TCL(char *pFilenameIn, char *pFilenameOut, char *pArchName, char *pEntName, int nbits_addr, int nbits_data) +{ + FILE *pFileIn, *pFileOut; + long start, end; + int i, word, word_addr, filesize, romsize; + char binstr_addr[33]; + char binstr_data[33]; + + char tpl[] = + {"# ---------------------------------------------------------------------\n# For Chipscope 10.1\n# ---------------------------------------------------------------------\n# Source JTAG/TCL frame work\nsource $env(CS_PATH)\\\\csejtag.tcl\n\nnamespace import ::chipscope::*\n\n# Platform USB Cable\n# Create session\nset handle [::chipscope::csejtag_session create 0]\n\n# Open JTAG and lock\nset open_result [::chipscope::csejtag_target open $handle $CSEJTAG_TARGET_PLATFORMUSB 0 \"port=USB2\" \"frequency=6000000\"]\n# frequency=\"24000000 | 12000000 | 6000000 | 3000000 | 1500000 | 750000\"\n\nset lock_result [::chipscope::csejtag_target lock $handle 1000]\n\nset devlist [::chipscope::csejtag_tap autodetect_chain $handle $CSEJTAG_SCAN_DEFAULT]\n\n# Get Device ID\nset devtype \"Virtex-4SX\"\nset devid 2\nset irlength [::chipscope::csejtag_tap get_irlength $handle $devid]\nset idcode [::chipscope::csejtag_tap get_device_idcode $handle $devid]\n\nset CSE_OP $CSEJTAG_SHIFT_READWRITE\nset CSE_ES $CSEJTAG_RUN_TEST_IDLE\n\n# Write Program\n"}; + + pFileIn = fopen(pFilenameIn, "rb"); + if (!pFileIn) + { + fprintf(stderr, "Error opening file %s\n", pFilenameIn); + return 1; + } + + pFileOut = fopen(pFilenameOut, "wb"); + if (!pFileOut) + { + fprintf(stderr, "Error opening file %s\n", pFilenameOut); + return 1; + } + + fseek(pFileIn, 0, SEEK_SET); + start = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_END); + end = ftell(pFileIn); + fseek(pFileIn, 0, SEEK_SET); + + filesize = (end-start); + romsize = (int)pow(2, nbits_addr+2); + + // ------------------------------------------------------------------------- + // Header + + // ------------------------------------------------------------------------- + fputs(tpl, pFileOut); + + // ------------------------------------------------------------------------- + // ROM part + // ------------------------------------------------------------------------- + fprintf(pFileOut, "# Assembled from %s\n", pFilenameIn); + fprintf(pFileOut, "# ---------------------------------------------------------------\n"); + fprintf(pFileOut, "# Shift the USER1 Instruction (b1111000010) into the Instruction Register of FPGA\n"); + fprintf(pFileOut, "# User 1\n"); + fprintf(pFileOut, "set result [::chipscope::csejtag_tap shift_device_ir $handle $devid $CSE_OP $CSE_ES 0 $irlength \"3C2\"]\n\n"); + + word_addr = 0; + for (i=0; i < filesize; i += sizeof(int)) + { + fread(&word, 1, sizeof(int), pFileIn); + if (g_endianess_EB) + fprintf(pFileOut, "::chipscope::csejtag_tap shift_device_dr $handle $devid $CSE_OP $CSE_ES 0 %d \"%4.4X%8.8X\"\n", nbits_data + JTAG_ADDR_WIDTH, word_addr, to_big_endian32(word)); + else + fprintf(pFileOut, "::chipscope::csejtag_tap shift_device_dr $handle $devid $CSE_OP $CSE_ES 0 %d \"%4.4X%8.8X\"\n", nbits_data + JTAG_ADDR_WIDTH, word_addr, word); + word_addr++; + } + fprintf(pFileOut, "\n"); + + // ------------------------------------------------------------------------- + // Trailer + // ------------------------------------------------------------------------- + fprintf(pFileOut, "::chipscope::csejtag_target unlock $handle\n"); + fprintf(pFileOut, "::chipscope::csejtag_target close $handle\n"); + fprintf(pFileOut, "::chipscope::csejtag_session destroy $handle\n"); + fprintf(pFileOut, "exit\n"); + + return 0; +} + +int main(int argc, char *argv[]) +{ + char *pFilenameIn; + char name_prj[1024]; + char name_rom[1024]; + char name_rom_v4ld[1024]; + char name_rom_tcl[1024]; + + FILE *pFileIn; + int filesize, romsize, nbits_addr, nbits_data; + long start, end; + int word, i; + + if (argc < 2) + { + fprintf(stderr, "Usage: romgen [-{EL|EB}]\n"); + return 1; + } + + pFilenameIn = argv[1]; + nbits_addr = atoi(argv[2]); + + g_endianess_EB = 0; + if (argc == 4) + { + if ((argv[3][0] == '-') && (toupper(argv[3][1]) == 'E') && (toupper(argv[3][2]) == 'B')) + g_endianess_EB = 1; + } + + basename(pFilenameIn, name_prj); + sprintf(name_rom, "%s.vhd", name_prj); + sprintf(name_rom_v4ld, "%s_ld.vhd", name_prj); + sprintf(name_rom_tcl, "%s.tcl", name_prj); + + SaveROM(pFilenameIn, name_rom, ARCH_NAME, ENT_NAME, nbits_addr, 32); + SaveROM_V4LD(pFilenameIn, name_rom_v4ld, ARCH_NAME, ENT_NAME, nbits_addr, 32); + SaveROM_TCL(pFilenameIn, name_rom_tcl, ARCH_NAME, ENT_NAME, nbits_addr, 32); + + + return 0; +} +