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

Subversion Repositories System09

[/] [System09/] [branches/] [mkfiles_rev1/] [Tools/] [s19tovhd/] [S19toVHD.cpp] - Blame information for rev 121

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
void load_mot( char *fname_in )
197
{
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
                return;
206
        }
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
}
246
 
247
 
248
 
249
 
250
int put2hex( FILE *fp, int h )
251
{
252
        int i, hex;
253
 
254
        hex = (h & 0xf0)>>4;
255
        i = fputc( (int)hex_str[hex], fp );
256
        hex = (h & 0xf);
257
        i = fputc( (int)hex_str[hex], fp );
258
        checksum = (checksum + h) & 0xff;
259
        return i;
260
}
261
 
262
int put4hex( FILE * fp, int h )
263
{
264
        int i;
265
 
266
        i = put2hex( fp, (h & 0xff00 )>>8 );
267
        i = put2hex( fp, (h & 0xff) );
268
        return i;
269
}
270
 
271
 
272
/*
273
* save VHDL hexadecimal file
274
*/
275
 
276
void save_vhdl_byte( FILE *fp_out, char *entity_name, int start_addr, int end_addr )
277
{
278
        int addr;
279
        int i,j;
280
        int byte;
281
 
282
        j=0;
283
        fprintf(fp_out, "library IEEE;\n");
284
        fprintf(fp_out, "   use IEEE.std_logic_1164.all;\n");
285
        fprintf(fp_out, "   use IEEE.std_logic_arith.all;\n");
286
        fprintf(fp_out, "library unisim;\n");
287
        fprintf(fp_out, "   use unisim.vcomponents.all;\n");
288
        fprintf(fp_out, "\n");
289
        fprintf(fp_out, "entity %s is\n", entity_name);
290
        fprintf(fp_out, "   port(\n");
291
        fprintf(fp_out, "      clk    : in  std_logic;\n");
292
        fprintf(fp_out, "      rst    : in  std_logic;\n");
293
        fprintf(fp_out, "      cs     : in  std_logic;\n");
294
        fprintf(fp_out, "      rw     : in  std_logic;\n");
295
        fprintf(fp_out, "      addr   : in  std_logic_vector(10 downto 0);\n");
296
        fprintf(fp_out, "      rdata  : out std_logic_vector(7 downto 0);\n");
297
        fprintf(fp_out, "      wdata  : in  std_logic_vector(7 downto 0)\n");
298
        fprintf(fp_out, "   );\n");
299
        fprintf(fp_out, "end %s;\n", entity_name);
300
        fprintf(fp_out, "\n");
301
        fprintf(fp_out, "architecture rtl of %s is\n", entity_name);
302
        fprintf(fp_out, "   signal we : std_logic;\n");
303
        fprintf(fp_out, "   signal dp : std_logic;\n");
304
        fprintf(fp_out, "begin\n");
305
        fprintf(fp_out, "   ROM: RAMB16_S9\n");
306
        fprintf(fp_out, "      generic map (\n");
307
 
308
        for( addr=start_addr; addr<=end_addr; addr+=32 )
309
        {
310
                fprintf( fp_out, "         INIT_%02x => x\"", j );
311
                for(i=31; i>=0; i-- )
312
                {
313
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
314
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
315
                        putc( hex_str[byte & 0xf], fp_out );
316
                }
317
                if (addr+32 < end_addr) {
318
                        fprintf( fp_out, "\",\n" );
319
                } else {
320
                        fprintf( fp_out, "\"\n" );
321
                }
322
                j++;
323
        }
324
        fprintf(fp_out, "      )\n");
325
        fprintf(fp_out, "      port map (\n");
326
        fprintf(fp_out, "         do    => rdata,\n");
327
        fprintf(fp_out, "         dop(0)  => dp,\n");
328
        fprintf(fp_out, "         addr    => addr,\n");
329
        fprintf(fp_out, "         clk     => clk,\n");
330
        fprintf(fp_out, "         di      => wdata,\n");
331
        fprintf(fp_out, "         dip(0)  => dp,\n");
332
        fprintf(fp_out, "         en      => cs,\n");
333
        fprintf(fp_out, "         ssr     => rst,\n");
334
        fprintf(fp_out, "         we      => we\n");
335
        fprintf(fp_out, "      );\n");
336
        fprintf(fp_out, "   drive_we: process (rw)\n");
337
        fprintf(fp_out, "   begin\n");
338
        fprintf(fp_out, "      we <= not rw;\n");
339
        fprintf(fp_out, "   end process;\n");
340
        fprintf(fp_out, "end architecture rtl;\n\n");
341
 
342
}
343
 
344
 
345
 
346
 
347
/*
348
* epedit main program
349
*/
350
int main(int argc, char* argv[])
351
{
352
        int start_addr;
353
        int end_addr;
354
        int arglen;
355
        char entity_name_buf[512];
356
        char hdl_file_buf[1024];
357
        char buf[1024];
358
        char *curpos;
359
        FILE *fp_out;
360
 
361
        if (argc < 5) {
362
                printf("Usage: s19tovhd <s19 file> <base vhd file> <vhdl base entity name> <addr> [<addr> ...]\n");
363
                return(-1);
364
        }
365
        printf("Reading Motorola S19 from file '%s'\n", argv[1]);
366
        printf("VHDL file name '%s'\n", argv[2]);
367
        printf("Base RAM/ROM entity name is '%s'\n", argv[3]);
368
        load_mot( argv[1] );
369
        if( (fp_out = fopen( argv[2], "w" )) == NULL ) {
370
                printf( "\nCan't open '%s' for write ", argv[2] );
371
                return(-1);
372
        }
373
 
374
        for (int cnt=4; cnt<argc; cnt++) {
375
                if( (arglen = get_address( argv[cnt], &start_addr )) == 0 ) {
376
                        printf("Expected hex start address, got %s\n", argv[cnt]);
377
                        continue;
378
                }
379
                end_addr = start_addr + 2047;
380
                sprintf(entity_name_buf, "%s_%4X", argv[3], start_addr);
381
 
382
                printf("Entity '%s' (address range '0x%4X'-'0x%4X') written to file '%s'\n",
383
                        entity_name_buf, start_addr, end_addr, argv[2]);
384
                save_vhdl_byte( fp_out, entity_name_buf, start_addr, end_addr );
385
        }
386
        if (fp_out) fclose(fp_out);
387
        return(0);
388
}
389
 

powered by: WebSVN 2.1.0

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