OpenCores
URL https://opencores.org/ocsvn/System09/System09/trunk

Subversion Repositories System09

[/] [System09/] [trunk/] [Tools/] [s19tovhd/] [S19toVHD.cpp] - Blame information for rev 159

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 25 davidgb
// S19toVHD.cpp : Defines the entry point for the console application.
2
//
3
 
4
/*
5
* epedit
6
*
7
* binary file editer program
8
*/
9
#include <stdio.h>
10
#include <string.h>
11
#include <ctype.h>
12 90 davidgb
#include <math.h>
13
 
14 25 davidgb
/*
15
* equates
16
*/
17
#define EPROM_MAX (1<<16)
18
#define CMD_LINE_MAX 80
19
#define FALSE 0
20
#define TRUE !FALSE
21
#define BINARY 0
22
#define MOTOROLA 1
23
#define INTEL 2
24
#define SMAL32 3
25
#define VHDL_BIN 4
26
#define VHDL_BYTE 5
27
#define VHDL_WORD 6
28
 
29
/*
30
* global variables
31
*/
32
FILE *cmdfp;                            /* command input pointer */
33
char cmdbuff[CMD_LINE_MAX];
34
unsigned char eprom_buff[EPROM_MAX];    /* eprom buffer */
35
int eprom_top;                          /* top of EPROM buffer */
36
int mod_flag;                           /* buffer has been modified */
37
int auxflag;                            /* Auxillary input file specified */
38
int count;
39
int checksum;
40
int offset;                             /* Eprom Buffer memory offset */
41
int format_type;                        /* load / save format type */
42
char *hex_str = "0123456789ABCDEF";
43
 
44
 
45
/*
46
* compare a string of specified length
47
* return TRUE if a match
48
* return FALSE if no match
49
* ignore case
50
*/
51
int str_equal( char *s1, char *s2, int len )
52
{
53 90 davidgb
        int i = 0;
54
        while( i<len ) {
55 25 davidgb
                if( toupper( s1[i] ) == toupper( s2[i] ) )
56
                        i++;
57 90 davidgb
                else
58
                        return FALSE;
59 25 davidgb
        }
60
        return TRUE;
61
}
62
 
63
int to_hexadecimal( char c )
64
{
65 90 davidgb
        for( int k=0; k<16; k++ ) {
66
                if( toupper(c) == hex_str[k] ) return k;
67 25 davidgb
        }
68
        return -1;
69
}
70
 
71
/*
72
* extract an address from the command line
73
* returns an offset to the end of the argument.
74
*/
75
int get_address( char *cb, int *addr )
76
{
77
        int i, j, k;
78
 
79 90 davidgb
        j = i = 0;
80 25 davidgb
 
81 90 davidgb
        while((k = to_hexadecimal(cb[i])) != -1) {
82 25 davidgb
                i++;
83
                j = j *16 + k;
84
        }
85
        *addr = j;
86 90 davidgb
        if( i == 0 ) return i;
87
        while( isspace( cb[i]) ) i++;
88 25 davidgb
        return i;
89
}
90
 
91
/*
92
* Motorola S1 format to Intel hex format
93
* Usage
94
* mot2hex <file_name>
95
*/
96
 
97
int gethex( FILE *fp_in )
98
{
99
        int hex;
100
 
101
        hex = fgetc( fp_in );
102
        return( to_hexadecimal( hex ) );
103
}
104
 
105
int get2hex( FILE *fp_in )
106
{
107
        int hexhi, hexlo, byte;
108
 
109
        hexhi = gethex( fp_in );
110
        if( hexhi != -1 )
111
        {
112
                hexlo = gethex( fp_in );
113
                if( hexlo != -1 )
114
                {
115
                        byte = hexhi * 16 + hexlo;
116
                        checksum = (checksum + byte) & 0xff;
117
                        return byte;
118
                }
119
        }
120
        return -1;
121
}
122
 
123
int get4hex( FILE *fp_in )
124
{
125
        int bytehi, bytelo, addr;
126
 
127
        bytehi = get2hex( fp_in );
128
        if( bytehi != -1 )
129
        {
130
                bytelo = get2hex( fp_in );
131
                if( bytelo != -1 )
132
                {
133
                        addr = (bytehi * 256) + bytelo;
134
                        return addr;
135
                }
136
        }
137
        return -1;
138
}
139
 
140
int get6hex( FILE *fp_in )
141
{
142
        int bytehi, bytemid, bytelow, addr;
143
 
144
        bytehi = get2hex( fp_in );
145
        if( bytehi != -1 )
146
        {
147
                bytemid = get2hex( fp_in );
148
                if( bytemid != -1 )
149
                {
150
                        bytelow = get2hex( fp_in );
151
                        if( bytelow != -1 )
152
                        {
153
                                addr = (bytehi << 16) + (bytemid << 8) + bytelow;
154
                                return addr;
155
                        }
156
                }
157
        }
158
        return -1;
159
}
160
 
161
long get8hex( FILE *fp_in )
162
{
163
        int wordhi, wordlow;
164
        long addr;
165
 
166
        wordhi = get4hex( fp_in );
167
        if( wordhi != -1 )
168
        {
169
                wordlow = get4hex( fp_in );
170
                if( wordlow != -1 )
171
                {
172
                        addr = ((long)wordhi << 16) + (long)wordlow;
173
                        return addr;
174
                }
175
        }
176
        return -1;
177 90 davidgb
}
178 25 davidgb
 
179
/*
180
* load motorola formatted file
181
*/
182
 
183 78 davidgb
bool load_mot( char *fname_in )
184 25 davidgb
{
185
        FILE *fp_in;
186
        int byte, addr, i;
187
 
188
        fp_in = fopen( fname_in, "r" );
189 90 davidgb
        if ( !fp_in ) {
190 25 davidgb
                printf( "\nCan't open %s", fname_in );
191 78 davidgb
                return false;
192 25 davidgb
        }
193
 
194
        byte = 0;
195
        addr = 0;
196
 
197 90 davidgb
        while( byte != -1 ) {
198 25 davidgb
                do {
199
                        byte = fgetc( fp_in);
200
                } while( (byte != 'S') && (byte != -1) );
201
 
202
                byte = fgetc( fp_in );
203
                checksum = 0;
204 90 davidgb
                if ( (byte == '1') || (byte == '2') ) {
205 25 davidgb
                        count = get2hex( fp_in );
206 90 davidgb
                        if ( byte == '1' ) {
207 25 davidgb
                                addr = get4hex( fp_in );
208
                                count -= 3;
209 90 davidgb
                        } else {
210 25 davidgb
                                addr = get6hex( fp_in );
211
                                count -= 4;
212
                        }
213 90 davidgb
                        for( i=0; i<count; i++ ) {
214 25 davidgb
                                byte = get2hex( fp_in );
215
                                eprom_buff[( addr - offset) % EPROM_MAX ] = (unsigned char)byte;
216
                                addr++;
217
                        }
218
                        byte = get2hex( fp_in);
219
                        checksum = (~checksum) & 0xff;
220 90 davidgb
                        if ( checksum != 0 )
221 25 davidgb
                                printf( "\nchecksum error - read check = %02x", byte );
222
                }
223
        }
224
        fclose( fp_in );
225 78 davidgb
        return true;
226 25 davidgb
}
227
 
228
int put2hex( FILE *fp, int h )
229
{
230 90 davidgb
        int hex = (h & 0xf0)>>4;
231
        int i = fputc( (int)hex_str[hex], fp );
232 25 davidgb
        hex = (h & 0xf);
233
        i = fputc( (int)hex_str[hex], fp );
234
        checksum = (checksum + h) & 0xff;
235
        return i;
236
}
237
 
238
int put4hex( FILE * fp, int h )
239
{
240 90 davidgb
        int i = put2hex( fp, (h & 0xff00 )>>8 );
241 25 davidgb
        i = put2hex( fp, (h & 0xff) );
242
        return i;
243
}
244
 
245 90 davidgb
char *bin_string( int num, int num_len )
246
{
247
        static char retbuf[33];
248
        char *p;
249
        int i;
250 25 davidgb
 
251 90 davidgb
        p = &retbuf[sizeof(retbuf)-1];
252
        *p = '\0';
253
        for (i=0; i<num_len; i++ ) {
254
                *--p = "01"[num % 2];
255
                num >>= 1;
256
        }
257
        return p;
258
}
259
 
260 25 davidgb
/*
261 90 davidgb
* save VHDL hexadecimal file 4KBit Block RAM (Spartan 2)
262 25 davidgb
*/
263
 
264 90 davidgb
void save_vhdl_b4( FILE *fp_out, char *entity_name, int start_addr, int end_addr )
265 25 davidgb
{
266
        int addr;
267
        int i,j;
268
        int byte;
269 90 davidgb
        int rom_num, rom_max, rom_len;
270
        int addr_len;
271 25 davidgb
 
272 90 davidgb
        rom_max = (end_addr+1-start_addr)/512;
273
        rom_len = 8;
274
        addr_len = (int)(log((double)(end_addr-start_addr))/log(2.0));
275
 
276 25 davidgb
        fprintf(fp_out, "library IEEE;\n");
277
        fprintf(fp_out, "   use IEEE.std_logic_1164.all;\n");
278
        fprintf(fp_out, "   use IEEE.std_logic_arith.all;\n");
279
        fprintf(fp_out, "library unisim;\n");
280
        fprintf(fp_out, "   use unisim.vcomponents.all;\n");
281
        fprintf(fp_out, "\n");
282
        fprintf(fp_out, "entity %s is\n", entity_name);
283
        fprintf(fp_out, "   port(\n");
284 98 davidgb
        fprintf(fp_out, "      clk       : in  std_logic;\n");
285
        fprintf(fp_out, "      rst       : in  std_logic;\n");
286
        fprintf(fp_out, "      cs        : in  std_logic;\n");
287
        fprintf(fp_out, "      rw        : in  std_logic;\n");
288
        fprintf(fp_out, "      addr      : in  std_logic_vector(%d downto 0);\n", addr_len);
289
        fprintf(fp_out, "      data_out  : out std_logic_vector(7 downto 0);\n");
290
        fprintf(fp_out, "      data_in   : in  std_logic_vector(7 downto 0)\n");
291 25 davidgb
        fprintf(fp_out, "   );\n");
292
        fprintf(fp_out, "end %s;\n", entity_name);
293
        fprintf(fp_out, "\n");
294
        fprintf(fp_out, "architecture rtl of %s is\n", entity_name);
295 90 davidgb
        fprintf(fp_out, "\n");
296
        fprintf(fp_out, "   type data_array is array(0 to %d) of std_logic_vector(7 downto 0);\n",rom_max-1);
297
        fprintf(fp_out, "   signal xdata : data_array;\n");
298
        fprintf(fp_out, "   signal en : std_logic_vector(%d downto 0);\n", rom_max-1);
299 25 davidgb
        fprintf(fp_out, "   signal we : std_logic;\n");
300 90 davidgb
        fprintf(fp_out, "\n");
301
        fprintf(fp_out,"   begin\n");
302
        fprintf(fp_out, "\n");
303 25 davidgb
 
304 90 davidgb
        addr=start_addr;
305
        for( rom_num=0; rom_num<rom_max; rom_num++) {
306
                fprintf(fp_out, "   ROM%02x: RAMB4_S8\n", rom_num );
307
                fprintf(fp_out, "      generic map (\n");
308
                for( j=0; j<16; j++ ) {
309
                        fprintf( fp_out, "         INIT_%02x => x\"", j );
310
                        for(i=31; i>=0; i-- ) {
311
                                byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
312
                                putc( hex_str[(byte >>4) & 0xf], fp_out );
313
                                putc( hex_str[byte & 0xf], fp_out );
314
                        }
315
                        if (j < 15) {
316
                                fprintf( fp_out, "\",\n" );
317
                        } else {
318
                                fprintf( fp_out, "\"\n" );
319
                        }
320
                        addr+=32;
321 25 davidgb
                }
322 90 davidgb
                fprintf(fp_out, "      )\n");
323
                fprintf(fp_out, "      port map (\n");
324
                fprintf(fp_out, "         clk     => clk,\n");
325
                fprintf(fp_out, "         en      => en(%d),\n", rom_num );
326
                fprintf(fp_out, "         we      => we,\n");
327
                fprintf(fp_out, "         rst     => rst,\n");
328
                fprintf(fp_out, "         addr    => addr(8 downto 0),\n");
329 98 davidgb
                fprintf(fp_out, "         di      => data_in,\n");
330 90 davidgb
                fprintf(fp_out, "         do      => xdata(%d)\n", rom_num );
331
                fprintf(fp_out, "      );\n");
332
                fprintf(fp_out, "\n");
333
        }
334
 
335
        fprintf(fp_out, "   rom_glue: process (cs, rw, addr, xdata)\n");
336
        fprintf(fp_out, "   begin\n");
337
        if( addr_len > rom_len ) {
338
                fprintf(fp_out, "      en <= (others=>'0');\n");
339
                fprintf(fp_out, "      case addr(%d downto %d) is\n", addr_len, rom_len+1 );
340
 
341
                for( rom_num=0; rom_num<rom_max; rom_num++ ) {
342
                        fprintf(fp_out, "      when \"%s\" =>\n", bin_string(rom_num, addr_len-rom_len) );
343
                        fprintf(fp_out, "         en(%d)  <= cs;\n", rom_num );
344 98 davidgb
                        fprintf(fp_out, "         data_out  <= xdata(%d);\n", rom_num);
345 25 davidgb
                }
346 90 davidgb
 
347
                fprintf(fp_out, "      when others =>\n");
348
                fprintf(fp_out, "         null;\n");
349
                fprintf(fp_out, "      end case;\n");
350
        } else {
351
                fprintf(fp_out, "      en(0)  <= cs;\n" );
352 98 davidgb
                fprintf(fp_out, "      data_out  <= xdata(0);\n" );
353 25 davidgb
        }
354
        fprintf(fp_out, "      we <= not rw;\n");
355
        fprintf(fp_out, "   end process;\n");
356
        fprintf(fp_out, "end architecture rtl;\n\n");
357
}
358
 
359
 
360 90 davidgb
/*
361
* save VHDL hexadecimal file 16 KBit Block RAM (Spartan 3)
362
*/
363 25 davidgb
 
364 90 davidgb
void save_vhdl_b16( FILE *fp_out, char *entity_name, int start_addr, int end_addr )
365
{
366
        int addr;
367
        int i,j;
368
        int byte;
369
        int rom_num, rom_max, rom_len;
370
        int addr_len;
371 25 davidgb
 
372 90 davidgb
        rom_max = (end_addr+1-start_addr)/2048;
373
        rom_len = 10;
374
        addr_len = (int)(log((double)(end_addr-start_addr))/log(2.0));
375
 
376
        fprintf(fp_out, "library IEEE;\n");
377
        fprintf(fp_out, "   use IEEE.std_logic_1164.all;\n");
378
        fprintf(fp_out, "   use IEEE.std_logic_arith.all;\n");
379
        fprintf(fp_out, "library unisim;\n");
380
        fprintf(fp_out, "   use unisim.vcomponents.all;\n");
381
        fprintf(fp_out, "\n");
382
        fprintf(fp_out, "entity %s is\n", entity_name);
383
        fprintf(fp_out, "   port(\n");
384 98 davidgb
        fprintf(fp_out, "      clk       : in  std_logic;\n");
385
        fprintf(fp_out, "      rst       : in  std_logic;\n");
386
        fprintf(fp_out, "      cs        : in  std_logic;\n");
387
        fprintf(fp_out, "      rw        : in  std_logic;\n");
388
        fprintf(fp_out, "      addr      : in  std_logic_vector(%d downto 0);\n", addr_len);
389
        fprintf(fp_out, "      data_out  : out std_logic_vector(7 downto 0);\n");
390
        fprintf(fp_out, "      data_in   : in  std_logic_vector(7 downto 0)\n");
391 90 davidgb
        fprintf(fp_out, "   );\n");
392
        fprintf(fp_out, "end %s;\n", entity_name);
393
        fprintf(fp_out, "\n");
394
        fprintf(fp_out, "architecture rtl of %s is\n", entity_name);
395
        fprintf(fp_out, "\n");
396
        fprintf(fp_out, "   type data_array is array(0 to %d) of std_logic_vector(7 downto 0);\n",rom_max-1);
397
        fprintf(fp_out, "   signal xdata : data_array;\n");
398
        fprintf(fp_out, "   signal en : std_logic_vector(%d downto 0);\n", rom_max-1);
399
        fprintf(fp_out, "   signal dp : std_logic_vector(%d downto 0);\n", rom_max-1);
400
        fprintf(fp_out, "   signal we : std_logic;\n");
401
        fprintf(fp_out, "\n");
402
        fprintf(fp_out, "   begin\n");
403
        fprintf(fp_out, "\n");
404
 
405
        addr=start_addr;
406
        for( rom_num=0; rom_num<rom_max; rom_num++) {
407
                fprintf(fp_out, "   ROM%02x: RAMB16_S9\n", rom_num );
408
                fprintf(fp_out, "      generic map (\n");
409
                for( j=0; j<64; j++ ) {
410
                        fprintf( fp_out, "         INIT_%02x => x\"", j );
411
                        for(i=31; i>=0; i-- ) {
412
                                byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
413
                                putc( hex_str[(byte >>4) & 0xf], fp_out );
414
                                putc( hex_str[byte & 0xf], fp_out );
415
                        }
416
                        if (j < 63)  {
417
                                fprintf( fp_out, "\",\n" );
418
                        } else {
419
                                fprintf( fp_out, "\"\n" );
420
                        }
421
                        addr+=32;
422
                }
423
                fprintf(fp_out, "      )\n");
424
                fprintf(fp_out, "      port map (\n");
425
                fprintf(fp_out, "         CLK     => clk,\n");
426
                fprintf(fp_out, "         SSR     => rst,\n");
427
                fprintf(fp_out, "         EN      => en(%d),\n", rom_num );
428
                fprintf(fp_out, "         WE      => we,\n");
429
                fprintf(fp_out, "         ADDR    => addr(10 downto 0),\n");
430 98 davidgb
                fprintf(fp_out, "         DI      => data_in,\n");
431 90 davidgb
                fprintf(fp_out, "         DIP(0)  => dp(%d),\n", rom_num );
432
                fprintf(fp_out, "         DO      => xdata(%d),\n", rom_num );
433
                fprintf(fp_out, "         DOP(0)  => dp(%d)\n", rom_num );
434
                fprintf(fp_out, "      );\n");
435
        }
436
 
437
        fprintf(fp_out, "   rom_glue: process (cs, rw, addr, xdata)\n");
438
        fprintf(fp_out, "   begin\n");
439
        if( addr_len > rom_len ) {
440
                fprintf(fp_out, "      en <= (others=>'0');\n");
441
                fprintf(fp_out, "      case addr(%d downto %d) is\n", addr_len, rom_len+1 );
442
 
443
                for( rom_num=0; rom_num<rom_max; rom_num++ ) {
444
                        fprintf(fp_out, "      when \"%s\" =>\n", bin_string(rom_num, addr_len-rom_len) );
445
                        fprintf(fp_out, "         en(%d)  <= cs;\n", rom_num );
446 98 davidgb
                        fprintf(fp_out, "         data_out  <= xdata(%d);\n", rom_num);
447 90 davidgb
                }
448
 
449
                fprintf(fp_out, "      when others =>\n");
450
                fprintf(fp_out, "         null;\n");
451
                fprintf(fp_out, "      end case;\n");
452
        } else {
453
                fprintf(fp_out, "      en(0)  <= cs;\n");
454 98 davidgb
                fprintf(fp_out, "      data_out  <= xdata(0);\n");
455 90 davidgb
        }
456
        fprintf(fp_out, "      we <= not rw;\n");
457
        fprintf(fp_out, "   end process;\n");
458
        fprintf(fp_out, "end architecture rtl;\n\n");
459
}
460
 
461 25 davidgb
/*
462
* epedit main program
463
*/
464
int main(int argc, char* argv[])
465
{
466
        int start_addr;
467
        int end_addr;
468
        int arglen;
469
        char entity_name_buf[512];
470
        char hdl_file_buf[1024];
471
        char buf[1024];
472
        char *curpos;
473
        FILE *fp_out;
474
 
475 90 davidgb
        if (argc < 5)
476
        {
477
                printf("Usage: s19tovhd <bram_type> <s19 file> <base vhd file> <vhdl base entity name> <addr> [<addr> ...]\n");
478 25 davidgb
                return(-1);
479
        }
480 90 davidgb
        int bram_type_arg_pos = 1;
481
        int s19_file_arg_pos = 2;
482
        int base_vhdl_file_arg_pos = 3;
483
        int entity_name_arg_pos = 4;
484
        int first_addr_arg_pos = 5;
485 25 davidgb
 
486 90 davidgb
        char *bram_type = argv[bram_type_arg_pos];
487
        char *s19_file = argv[s19_file_arg_pos];
488
        char *base_vhd_file = argv[base_vhdl_file_arg_pos];
489
        char *entity_name = argv[entity_name_arg_pos];
490
        printf("Block-RAM type '%s'\n", bram_type);
491
        printf("Reading Motorola S19 from file '%s'\n", s19_file);
492
        printf("VHDL file name '%s'\n", base_vhd_file);
493
        printf("Base RAM/ROM entity name is '%s'\n", entity_name);
494
        if (! load_mot( s19_file )) return (-1);
495
 
496
        if (strcmp(bram_type,"b16") == 0) {
497
                if( (fp_out = fopen( base_vhd_file, "w" )) == NULL ) {
498
                        printf( "\nCan't open '%s' for write ", base_vhd_file );
499
                        return(-1);
500 25 davidgb
                }
501
 
502 90 davidgb
                for (int cnt=first_addr_arg_pos; cnt<argc; cnt++) {
503
                        if( (arglen = get_address( argv[cnt], &start_addr )) == 0 ) {
504
                                printf("Expected hex start address, got %s\n", argv[cnt]);
505
                                continue;
506
                        }
507
                        end_addr = start_addr + 2047;
508
                        sprintf(entity_name_buf, "%s_%4X", entity_name, start_addr);
509
 
510
                        printf("Entity '%s' (address range '0x%4X'-'0x%4X') written to file '%s'\n",
511
                                entity_name_buf, start_addr, end_addr, base_vhd_file);
512
                        save_vhdl_b16( fp_out, entity_name_buf, start_addr, end_addr );
513
                }
514
                if (fp_out) fclose(fp_out);
515 25 davidgb
        }
516 90 davidgb
        if (strcmp(bram_type,"b4") == 0) {
517
                if( (fp_out = fopen( base_vhd_file, "w" )) == NULL ) {
518
                        printf( "\nCan't open '%s' for write ", base_vhd_file );
519
                        return(-1);
520
                }
521
 
522
                for (int cnt=first_addr_arg_pos; cnt<argc; cnt++) {
523
                        if( (arglen = get_address( argv[cnt], &start_addr )) == 0 ) {
524
                                printf("Expected hex start address, got %s\n", argv[cnt]);
525
                                continue;
526
                        }
527
                        end_addr = start_addr + 2047;
528
                        sprintf(entity_name_buf, "%s_%4X", entity_name, start_addr);
529
 
530
                        printf("Entity '%s' (address range '0x%4X'-'0x%4X') written to file '%s'\n",
531
                                entity_name_buf, start_addr, end_addr, base_vhd_file);
532
                        save_vhdl_b4( fp_out, entity_name_buf, start_addr, end_addr );
533
                }
534
                if (fp_out) fclose(fp_out);
535
        }
536 25 davidgb
        return(0);
537
}
538
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.