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

Subversion Repositories System09

[/] [System09/] [rev_86/] [Tools/] [s19tovhd/] [S19toVHD.cpp] - Blame information for rev 158

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
/*
13
* equates
14
*/
15
#define EPROM_MAX (1<<16)
16
#define CMD_LINE_MAX 80
17
#define FALSE 0
18
#define TRUE !FALSE
19
#define BINARY 0
20
#define MOTOROLA 1
21
#define INTEL 2
22
#define SMAL32 3
23
#define VHDL_BIN 4
24
#define VHDL_BYTE 5
25
#define VHDL_WORD 6
26
 
27
/*
28
* global variables
29
*/
30
FILE *cmdfp;                            /* command input pointer */
31
char cmdbuff[CMD_LINE_MAX];
32
unsigned char eprom_buff[EPROM_MAX];    /* eprom buffer */
33
int eprom_top;                          /* top of EPROM buffer */
34
int mod_flag;                           /* buffer has been modified */
35
int auxflag;                            /* Auxillary input file specified */
36
int count;
37
int checksum;
38
int offset;                             /* Eprom Buffer memory offset */
39
int format_type;                        /* load / save format type */
40
char *hex_str = "0123456789ABCDEF";
41
 
42
 
43
/*
44
* compare a string of specified length
45
* return TRUE if a match
46
* return FALSE if no match
47
* ignore case
48
*/
49
int str_equal( char *s1, char *s2, int len )
50
{
51
        int i;
52
 
53
        i = 0;
54
        while( i<len )
55
        {
56
                if( toupper( s1[i] ) == toupper( s2[i] ) )
57
                        i++;
58
                else return FALSE;
59
        }
60
        return TRUE;
61
}
62
 
63
 
64
int to_hexadecimal( char c )
65
{
66
        int k;
67
 
68
        for( k=0; k<16; k++ )
69
        {
70
                if( toupper(c) == hex_str[k] )
71
                        return k;
72
        }
73
        return -1;
74
}
75
 
76
/*
77
* extract an address from the command line
78
* returns an offset to the end of the argument.
79
*/
80
int get_address( char *cb, int *addr )
81
{
82
        int i, j, k;
83
 
84
        j = 0;
85
        i = 0;
86
 
87
        while((k = to_hexadecimal(cb[i])) != -1)
88
        {
89
                i++;
90
                j = j *16 + k;
91
        }
92
        *addr = j;
93
        if( i == 0 )
94
                return i;
95
        while( isspace( cb[i]) )
96
                i++;
97
        return i;
98
}
99
 
100
 
101
/*
102
* Motorola S1 format to Intel hex format
103
* Usage
104
* mot2hex <file_name>
105
*/
106
 
107
int gethex( FILE *fp_in )
108
{
109
        int hex;
110
 
111
        hex = fgetc( fp_in );
112
        return( to_hexadecimal( hex ) );
113
}
114
 
115
int get2hex( FILE *fp_in )
116
{
117
        int hexhi, hexlo, byte;
118
 
119
        hexhi = gethex( fp_in );
120
        if( hexhi != -1 )
121
        {
122
                hexlo = gethex( fp_in );
123
                if( hexlo != -1 )
124
                {
125
                        byte = hexhi * 16 + hexlo;
126
                        checksum = (checksum + byte) & 0xff;
127
                        return byte;
128
                }
129
        }
130
        return -1;
131
}
132
 
133
int get4hex( FILE *fp_in )
134
{
135
        int bytehi, bytelo, addr;
136
 
137
        bytehi = get2hex( fp_in );
138
        if( bytehi != -1 )
139
        {
140
                bytelo = get2hex( fp_in );
141
                if( bytelo != -1 )
142
                {
143
                        addr = (bytehi * 256) + bytelo;
144
                        return addr;
145
                }
146
        }
147
        return -1;
148
}
149
 
150
int get6hex( FILE *fp_in )
151
{
152
        int bytehi, bytemid, bytelow, addr;
153
 
154
        bytehi = get2hex( fp_in );
155
        if( bytehi != -1 )
156
        {
157
                bytemid = get2hex( fp_in );
158
                if( bytemid != -1 )
159
                {
160
                        bytelow = get2hex( fp_in );
161
                        if( bytelow != -1 )
162
                        {
163
                                addr = (bytehi << 16) + (bytemid << 8) + bytelow;
164
                                return addr;
165
                        }
166
                }
167
        }
168
        return -1;
169
}
170
 
171
long get8hex( FILE *fp_in )
172
{
173
        int wordhi, wordlow;
174
        long addr;
175
 
176
        wordhi = get4hex( fp_in );
177
        if( wordhi != -1 )
178
        {
179
                wordlow = get4hex( fp_in );
180
                if( wordlow != -1 )
181
                {
182
                        addr = ((long)wordhi << 16) + (long)wordlow;
183
                        return addr;
184
                }
185
        }
186
        return -1;
187
}
188
 
189
 
190
 
191
 
192
/*
193
* load motorola formatted file
194
*/
195
 
196 78 davidgb
bool load_mot( char *fname_in )
197 25 davidgb
{
198
        FILE *fp_in;
199
        int byte, addr, i;
200
 
201
        fp_in = fopen( fname_in, "r" );
202
        if( !fp_in )
203
        {
204
                printf( "\nCan't open %s", fname_in );
205 78 davidgb
                return false;
206 25 davidgb
        }
207
 
208
        byte = 0;
209
        addr = 0;
210
 
211
        while( byte != -1 )
212
        {
213
                do {
214
                        byte = fgetc( fp_in);
215
                } while( (byte != 'S') && (byte != -1) );
216
 
217
                byte = fgetc( fp_in );
218
                checksum = 0;
219
                if( (byte == '1') || (byte == '2') )
220
                {
221
                        count = get2hex( fp_in );
222
                        if( byte == '1' )
223
                        {
224
                                addr = get4hex( fp_in );
225
                                count -= 3;
226
                        }
227
                        else
228
                        {
229
                                addr = get6hex( fp_in );
230
                                count -= 4;
231
                        }
232
                        for( i=0; i<count; i++ )
233
                        {
234
                                byte = get2hex( fp_in );
235
                                eprom_buff[( addr - offset) % EPROM_MAX ] = (unsigned char)byte;
236
                                addr++;
237
                        }
238
                        byte = get2hex( fp_in);
239
                        checksum = (~checksum) & 0xff;
240
                        if( checksum != 0 )
241
                                printf( "\nchecksum error - read check = %02x", byte );
242
                }
243
        }
244
        fclose( fp_in );
245 78 davidgb
        return true;
246 25 davidgb
}
247
 
248
 
249
 
250
 
251
int put2hex( FILE *fp, int h )
252
{
253
        int i, hex;
254
 
255
        hex = (h & 0xf0)>>4;
256
        i = fputc( (int)hex_str[hex], fp );
257
        hex = (h & 0xf);
258
        i = fputc( (int)hex_str[hex], fp );
259
        checksum = (checksum + h) & 0xff;
260
        return i;
261
}
262
 
263
int put4hex( FILE * fp, int h )
264
{
265
        int i;
266
 
267
        i = put2hex( fp, (h & 0xff00 )>>8 );
268
        i = put2hex( fp, (h & 0xff) );
269
        return i;
270
}
271
 
272
 
273
/*
274
* save VHDL hexadecimal file
275
*/
276
 
277
void save_vhdl_byte( FILE *fp_out, char *entity_name, int start_addr, int end_addr )
278
{
279
        int addr;
280
        int i,j;
281
        int byte;
282
 
283
        j=0;
284
        fprintf(fp_out, "library IEEE;\n");
285
        fprintf(fp_out, "   use IEEE.std_logic_1164.all;\n");
286
        fprintf(fp_out, "   use IEEE.std_logic_arith.all;\n");
287
        fprintf(fp_out, "library unisim;\n");
288
        fprintf(fp_out, "   use unisim.vcomponents.all;\n");
289
        fprintf(fp_out, "\n");
290
        fprintf(fp_out, "entity %s is\n", entity_name);
291
        fprintf(fp_out, "   port(\n");
292
        fprintf(fp_out, "      clk    : in  std_logic;\n");
293
        fprintf(fp_out, "      rst    : in  std_logic;\n");
294
        fprintf(fp_out, "      cs     : in  std_logic;\n");
295
        fprintf(fp_out, "      rw     : in  std_logic;\n");
296
        fprintf(fp_out, "      addr   : in  std_logic_vector(10 downto 0);\n");
297
        fprintf(fp_out, "      rdata  : out std_logic_vector(7 downto 0);\n");
298
        fprintf(fp_out, "      wdata  : in  std_logic_vector(7 downto 0)\n");
299
        fprintf(fp_out, "   );\n");
300
        fprintf(fp_out, "end %s;\n", entity_name);
301
        fprintf(fp_out, "\n");
302
        fprintf(fp_out, "architecture rtl of %s is\n", entity_name);
303
        fprintf(fp_out, "   signal we : std_logic;\n");
304
        fprintf(fp_out, "   signal dp : std_logic;\n");
305
        fprintf(fp_out, "begin\n");
306
        fprintf(fp_out, "   ROM: RAMB16_S9\n");
307
        fprintf(fp_out, "      generic map (\n");
308
 
309
        for( addr=start_addr; addr<=end_addr; addr+=32 )
310
        {
311
                fprintf( fp_out, "         INIT_%02x => x\"", j );
312
                for(i=31; i>=0; i-- )
313
                {
314
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
315
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
316
                        putc( hex_str[byte & 0xf], fp_out );
317
                }
318
                if (addr+32 < end_addr) {
319
                        fprintf( fp_out, "\",\n" );
320
                } else {
321
                        fprintf( fp_out, "\"\n" );
322
                }
323
                j++;
324
        }
325
        fprintf(fp_out, "      )\n");
326
        fprintf(fp_out, "      port map (\n");
327
        fprintf(fp_out, "         do    => rdata,\n");
328
        fprintf(fp_out, "         dop(0)  => dp,\n");
329
        fprintf(fp_out, "         addr    => addr,\n");
330
        fprintf(fp_out, "         clk     => clk,\n");
331
        fprintf(fp_out, "         di      => wdata,\n");
332
        fprintf(fp_out, "         dip(0)  => dp,\n");
333
        fprintf(fp_out, "         en      => cs,\n");
334
        fprintf(fp_out, "         ssr     => rst,\n");
335
        fprintf(fp_out, "         we      => we\n");
336
        fprintf(fp_out, "      );\n");
337
        fprintf(fp_out, "   drive_we: process (rw)\n");
338
        fprintf(fp_out, "   begin\n");
339
        fprintf(fp_out, "      we <= not rw;\n");
340
        fprintf(fp_out, "   end process;\n");
341
        fprintf(fp_out, "end architecture rtl;\n\n");
342
 
343
}
344
 
345
 
346
 
347
 
348
/*
349
* epedit main program
350
*/
351
int main(int argc, char* argv[])
352
{
353
        int start_addr;
354
        int end_addr;
355
        int arglen;
356
        char entity_name_buf[512];
357
        char hdl_file_buf[1024];
358
        char buf[1024];
359
        char *curpos;
360
        FILE *fp_out;
361
 
362
        if (argc < 5) {
363
                printf("Usage: s19tovhd <s19 file> <base vhd file> <vhdl base entity name> <addr> [<addr> ...]\n");
364
                return(-1);
365
        }
366
        printf("Reading Motorola S19 from file '%s'\n", argv[1]);
367
        printf("VHDL file name '%s'\n", argv[2]);
368
        printf("Base RAM/ROM entity name is '%s'\n", argv[3]);
369 78 davidgb
        if (!load_mot( argv[1] )) {
370
                return(-1);
371
        }
372 25 davidgb
        if( (fp_out = fopen( argv[2], "w" )) == NULL ) {
373
                printf( "\nCan't open '%s' for write ", argv[2] );
374
                return(-1);
375
        }
376
 
377
        for (int cnt=4; cnt<argc; cnt++) {
378
                if( (arglen = get_address( argv[cnt], &start_addr )) == 0 ) {
379
                        printf("Expected hex start address, got %s\n", argv[cnt]);
380
                        continue;
381
                }
382
                end_addr = start_addr + 2047;
383
                sprintf(entity_name_buf, "%s_%4X", argv[3], start_addr);
384
 
385
                printf("Entity '%s' (address range '0x%4X'-'0x%4X') written to file '%s'\n",
386
                        entity_name_buf, start_addr, end_addr, argv[2]);
387
                save_vhdl_byte( fp_out, entity_name_buf, start_addr, end_addr );
388
        }
389
        if (fp_out) fclose(fp_out);
390
        return(0);
391
}
392
 

powered by: WebSVN 2.1.0

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