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

Subversion Repositories copyblaze

[/] [copyblaze/] [trunk/] [copyblaze/] [sw/] [tools/] [asm/] [pBlazASM/] [pBlazMRG/] [pBlazMRG.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ameziti
/*
2
 *  Copyright © 2003..2010 : Henk van Kampen <henk@mediatronix.com>
3
 *
4
 *      This file is part of pBlazMRG.
5
 *
6
 *  pBlazMRG is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  pBlazMRG is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with pBlazMRG.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
 
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <stdint.h>
24
#include <unistd.h>
25
#include <time.h>
26
 
27
#include "pbTypes.h"
28
#include "pbLibgen.h"
29
 
30
#ifdef TCC
31
#include "getopt.h"
32
#endif
33
 
34
#define MAXMEM  4096
35
 
36
uint32_t Code[ 4096 ] ;
37
 
38
static void usage( char * text ) {
39
        printf( "\n%s - %s\n", text, "Picoblaze Assembler merge utility V1.1" ) ;
40
        printf( "\nUSAGE:\n" ) ;
41
        printf( "   pBlazMRG [-v] [-s<MEM data inputfile] -e<entity_name> <MEM code inputfile> <TPL inputfile> <ROM outputfile>\n" ) ;
42
}
43
 
44
bool loadMEM( const char * strCodefile, const char * strDatafile ) {
45
        int i, addr ;
46
        uint32_t code ;
47
        char line[ 256 ], *p ;
48
        FILE * infile = NULL ;
49
 
50
        for ( i = 0 ; i < MAXMEM; i++ )
51
                Code[ i ] = 0 ;
52
 
53
        infile = fopen( strCodefile, "r" ) ;
54
        if ( infile == NULL ) {
55
                fprintf( stderr, "? Unable to open code MEM file '%s'", strCodefile ) ;
56
                return false ;
57
        }
58
 
59
        for ( addr = -1 ; addr < MAXMEM && fgets( line, sizeof( line ), infile ) != NULL; ) {
60
                if ( ( p = strchr( line, '@' ) ) != NULL ) {
61
                        if ( sscanf( ++p, "%X", &addr ) != 1 ) {
62
                                fprintf( stderr, "? Missing address in code MEM file '%s'", strCodefile ) ;
63
                                return false ;
64
                        }
65
                } else {
66
                        if ( addr == -1 ) {
67
                                fprintf( stderr, "? Missing address in code MEM file '%s'", strCodefile ) ;
68
                                return false ;
69
                        }
70
                        sscanf( line, "%X", &code ) ;
71
                        Code[ addr ] = code ;
72
 
73
                        addr += 1 ;
74
                }
75
        }
76
 
77
        fclose( infile ) ;
78
 
79
        if (strlen( strDatafile ) == 0 )
80
                return true ;
81
 
82
        infile = fopen( strDatafile, "r" ) ;
83
        if ( infile == NULL ) {
84
                fprintf( stderr, "? Unable to open data SCR file '%s'", strDatafile ) ;
85
                return false ;
86
        }
87
 
88
        for ( addr = -1 ; addr < MAXMEM && fgets( line, sizeof( line ), infile ) != NULL; ) {
89
                if ( ( p = strchr( line, '@' ) ) != NULL ) {
90
                        if ( sscanf( ++p, "%X", &addr ) != 1 ) {
91
                                fprintf( stderr, "? Missing address in data SCR file '%s'", strDatafile ) ;
92
                                return false ;
93
                        }
94
                } else {
95
                        if ( addr == -1 ) {
96
                                fprintf( stderr, "? Missing address in data SCR file '%s'", strDatafile ) ;
97
                                return false ;
98
                        }
99
                        sscanf( line, "%X", &code ) ;
100
                        if ( addr & 1 )
101
                                Code[ addr / 2 ] |= ( Code[ addr / 2 ] & 0x001FF ) | ( ( code & 0xFF ) << 8 ) ;
102
                        else
103
                                Code[ addr / 2 ] |= ( Code[ addr / 2 ] & 0x3FF00 ) | ( ( code & 0xFF ) << 0 ) ;
104
 
105
                        addr += 1 ;
106
                }
107
        }
108
 
109
        fclose( infile ) ;
110
 
111
// debug values
112
//for ( i = 0 ; i < MAXMEM; i++ )
113
//Code[ i ] = i | ( i << 6 ) | ( i << 12 ) ;
114
 
115
        return true ;
116
}
117
 
118
bool mergeTPL( const char * strTPLfile, const char * strROMfile, const char * strEntity ) {
119
        FILE * infile = NULL ;
120
        FILE * outfile = NULL ;
121
        enum {
122
                stIDLE, stCOPY, stMERGE
123
        } state = stIDLE ;
124
        char buffer[ 65 ] ;
125
        uint32_t code, line, bit ;
126
        int c ;
127
        int p = 0 ;
128
        int i ;
129
 
130
        infile = fopen( strTPLfile, "r" ) ;
131
        if ( infile == NULL ) {
132
                fprintf( stderr, "? Unable to open template file '%s'", strTPLfile ) ;
133
                return false ;
134
        }
135
 
136
        outfile = fopen( strROMfile, "w" ) ;
137
        if ( outfile == NULL ) {
138
                fprintf( stderr, "? Unable to open output file '%s'", strROMfile ) ;
139
                fclose( infile ) ;
140
                return false ;
141
        }
142
 
143
        while ( ( c = fgetc( infile ) ) != EOF ) {
144
                switch ( state ) {
145
                case stIDLE :
146
                        buffer[ 0 ] = '\0' ;
147
                        if ( c == '{' ) {
148
                                state = stMERGE ;
149
                                p = 0 ;
150
                        }
151
                        break ;
152
 
153
                case stCOPY :
154
                        if ( c == '{' ) {
155
                                state = stMERGE ;
156
                                p = 0 ;
157
                        } else {
158
                                fputc( c, outfile ) ;
159
                        }
160
                        break ;
161
 
162
                case stMERGE :
163
                        if ( c != '}' ) {
164
                                if ( p < 64 ) {
165
                                        buffer[ p++ ] = c ;
166
                                        buffer[ p ] = '\0' ;
167
                                }
168
                        } else if ( strlen( buffer ) > 0 ) {
169
                                // BYTE based INITs
170
                                if ( strncmp( "[8:0]_INIT_", buffer, 11 ) == 0 ) {
171
                                        sscanf( buffer, "[8:0]_INIT_%02X", &line ) ;
172
                                        if ( line < 128 )
173
                                                for ( i = 31 ; i >= 0; i-- ) {
174
                                                        fprintf( outfile, "%02X", ( Code[ line * 32 + i ] >> 0 ) & 0xFF ) ;
175
                                                }
176
                                        state = stCOPY ;
177
                                // parity bits
178
                                } else if ( strncmp( "[8:0]_INITP_", buffer, 12 ) == 0 ) {
179
                                        // accumulate all bits 8
180
                                        sscanf( buffer, "[8:0]_INITP_%02X", &line ) ;
181
                                        if ( line < 16 )
182
                                                for ( i = 31 ; i >= 0; i-- ) {
183
                                                        code  =  ( Code[ ( line * 32 + i ) * 8 + 0 ] >> 8 ) & 0x01 ;
184
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 1 ] >> 8 ) & 0x01 ) << 1 ;
185
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 2 ] >> 8 ) & 0x01 ) << 2 ;
186
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 3 ] >> 8 ) & 0x01 ) << 3 ;
187
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 4 ] >> 8 ) & 0x01 ) << 4 ;
188
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 5 ] >> 8 ) & 0x01 ) << 5 ;
189
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 6 ] >> 8 ) & 0x01 ) << 6 ;
190
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 7 ] >> 8 ) & 0x01 ) << 7 ;
191
                                                        fprintf( outfile, "%02X", code ) ;
192
                                                }
193
                                        state = stCOPY ;
194
 
195
                                } else if ( strncmp( "[17:9]_INIT_", buffer, 12 ) == 0 ) {
196
                                        sscanf( buffer, "[17:9]_INIT_%02X", &line ) ;
197
                                        if ( line < 128 )
198
                                                for ( i = 31 ; i >= 0; i-- ) {
199
                                                        fprintf( outfile, "%02X", ( Code[ line * 32 + i ] >> 9 ) & 0xFF ) ;
200
                                                }
201
                                        state = stCOPY ;
202
                                // parity bits
203
                                } else if ( strncmp( "[17:9]_INITP_", buffer, 13 ) == 0 ) {
204
                                        // accumulate all bits 17
205
                                        sscanf( buffer, "[17:9]_INITP_%02X", &line ) ;
206
                                        if ( line < 16 )
207
                                                for ( i = 31 ; i >= 0; i-- ) {
208
                                                        code  =  ( Code[ ( line * 32 + i ) * 8 + 0 ] >> 17 ) & 0x01 ;
209
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 1 ] >> 17 ) & 0x01 ) << 1 ;
210
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 2 ] >> 17 ) & 0x01 ) << 2 ;
211
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 3 ] >> 17 ) & 0x01 ) << 3 ;
212
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 4 ] >> 17 ) & 0x01 ) << 4 ;
213
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 5 ] >> 17 ) & 0x01 ) << 5 ;
214
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 6 ] >> 17 ) & 0x01 ) << 6 ;
215
                                                        code |= (( Code[ ( line * 32 + i ) * 8 + 7 ] >> 17 ) & 0x01 ) << 7 ;
216
                                                        fprintf( outfile, "%02X", code ) ;
217
                                                }
218
                                        state = stCOPY ;
219
 
220
                                // WORD based INITs
221
                                } else if ( strncmp( "INIT_", buffer, 5 ) == 0 ) {
222
                                        sscanf( buffer, "INIT_%02X", &line ) ;
223
                                        if ( line < 128 )
224
                                                for ( i = 15 ; i >= 0; i-- ) {
225
                                                        fprintf( outfile, "%04X", ( Code[ line * 16 + i ] >> 0 ) & 0xFFFF ) ;
226
                                                }
227
                                        state = stCOPY ;
228
                                // parity bits
229
                                } else if ( strncmp( "INITP_", buffer, 6 ) == 0 ) {
230
                                        sscanf( buffer, "INITP_%02X", &line ) ;
231
                                        if ( line < 16 )
232
                                                for ( i = 31 ; i >= 0; i-- ) {
233
                                                        code  =  ( Code[ ( line * 32 + i ) * 4 + 0 ] >> 16 ) & 0x03 ;
234
                                                        code |= (( Code[ ( line * 32 + i ) * 4 + 1 ] >> 16 ) & 0x03 ) << 2 ;
235
                                                        code |= (( Code[ ( line * 32 + i ) * 4 + 2 ] >> 16 ) & 0x03 ) << 4 ;
236
                                                        code |= (( Code[ ( line * 32 + i ) * 4 + 3 ] >> 16 ) & 0x03 ) << 6 ;
237
                                                        fprintf( outfile, "%02X", code ) ;
238
                                                }
239
                                        state = stCOPY ;
240
 
241
                                // bit based INITs
242
                                } else if ( strncmp( "INIT64_", buffer, 6 ) == 0 ) {
243
                                        sscanf( buffer, "INIT64_%d", &bit ) ;
244
                                        if ( bit < 18 ) {
245
                                                for ( i = 15 ; i >= 0 ; i -= 1 ) {
246
                                                        code  = ( ( ( Code[ i * 4 + 0 ] >> bit ) & 1 ) << 0 ) ;
247
                                                        code |= ( ( ( Code[ i * 4 + 1 ] >> bit ) & 1 ) << 1 ) ;
248
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 2 ) ;
249
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 3 ) ;
250
                                                        fprintf( outfile, "%1X", code ) ;
251
                                                }
252
                                        }
253
                                        state = stCOPY ;
254
                                } else if ( strncmp( "INIT128_", buffer, 6 ) == 0 ) {
255
                                        sscanf( buffer, "INIT128_%d", &bit ) ;
256
                                        if ( bit < 18 ) {
257
                                                for ( i = 31 ; i >= 0 ; i -= 1 ) {
258
                                                        code  = ( ( ( Code[ i * 4 + 0 ] >> bit ) & 1 ) << 0 ) ;
259
                                                        code |= ( ( ( Code[ i * 4 + 1 ] >> bit ) & 1 ) << 1 ) ;
260
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 2 ) ;
261
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 3 ) ;
262
                                                        fprintf( outfile, "%1X", code ) ;
263
                                                }
264
                                        }
265
                                        state = stCOPY ;
266
                                } else if ( strncmp( "INIT256_", buffer, 8 ) == 0 ) {
267
                                        sscanf( buffer, "INIT256_%d", &bit ) ;
268
                                        if ( bit < 18 ) {
269
                                                for ( i = 63 ; i >= 0 ; i -= 1 ) {
270
                                                        code  = ( ( ( Code[ i * 4 + 0 ] >> bit ) & 1 ) << 0 ) ;
271
                                                        code |= ( ( ( Code[ i * 4 + 1 ] >> bit ) & 1 ) << 1 ) ;
272
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 2 ) ;
273
                                                        code |= ( ( ( Code[ i * 4 + 2 ] >> bit ) & 1 ) << 3 ) ;
274
                                                        fprintf( outfile, "%1X", code ) ;
275
                                                }
276
                                        }
277
                                        state = stCOPY ;
278
 
279
                                } else if ( strcmp( "psmname", buffer ) == 0 ) {
280
                                        fprintf( outfile, "%s", strEntity ) ;
281
                                        state = stCOPY ;
282
                                } else if ( strcmp( "name", buffer ) == 0 ) {
283
                                        fprintf( outfile, "%s", strEntity ) ;
284
                                        state = stCOPY ;
285
                                } else if ( strcmp( "tool", buffer ) == 0 ) {
286
                                        fprintf( outfile, "pBlazMRG" ) ;
287
                                        state = stCOPY ;
288
                                } else if ( strcmp( "timestamp", buffer ) == 0 ) {
289
                                        char date_str[9], time_str[9] ;
290
 
291
                                        _strdate( date_str ) ;
292
                                        _strtime( time_str ) ;
293
                                        fprintf( outfile, "%s %s", date_str, time_str ) ;
294
                                        state = stCOPY ;
295
                                } else if ( strcmp( "begin template", buffer ) == 0 ) {
296
                                        state = stCOPY ;
297
                                } else
298
                                        state = stIDLE ;
299
                        } else
300
                                state = stIDLE ;
301
                        break ;
302
                }
303
        }
304
 
305
        fclose( outfile ) ;
306
        fclose( infile ) ;
307
 
308
        return true ;
309
}
310
 
311
int main( int argc, char *argv[] ) {
312
        char code_filename[ 256 ] = { '\0' } ;
313
        char data_filename[ 256 ] = { '\0' } ;
314
        char tpl_filename[ 256 ] = { '\0' } ;
315
        char rom_filename[ 256 ] = { '\0' } ;
316
        char entity_name[ 256 ] = { '\0' } ;
317
 
318
        bool bOptErr = false ;
319
        bool bVerbose = false ;
320
 
321
        extern char * optarg ;
322
        extern int optind, optopt ;
323
        int optch ;
324
 
325
        opterr = -1 ;
326
        while ( ( optch = getopt( argc, argv, ":e:hs:v" ) ) != -1 ) {
327
                switch ( optch ) {
328
                case 'e' :
329
                        if ( optarg != NULL )
330
                                strcpy( entity_name, optarg ) ;
331
                        else
332
                                bOptErr = true ;
333
                        break ;
334
                case 's' :
335
                        if ( optarg != NULL )
336
                                strcpy( data_filename, optarg ) ;
337
                        else
338
                                bOptErr = true ;
339
                        break ;
340
                case 'h' :
341
                        bOptErr = true ;
342
                        break ;
343
                case 'v' :
344
                        bVerbose = true ;
345
                        break ;
346
                case ':' :
347
                        fprintf( stderr, "? missing option: -%c\n", optopt ) ;
348
                        bOptErr = true ;
349
                        break ;
350
                default :
351
                        fprintf( stderr, "? unknown option: -%c\n", optopt ) ;
352
                        bOptErr = true ;
353
                        break ;
354
                }
355
        }
356
 
357
        if ( bOptErr ) {
358
                usage( basename( argv[ 0 ] ) ) ;
359
                exit( -1 ) ;
360
        }
361
 
362
        // source filename
363
        if ( argv[ optind ] == NULL ) {
364
                fprintf( stderr, "? source file missing\n" ) ;
365
                usage( basename( argv[ 0 ] ) ) ;
366
                exit( -1 ) ;
367
        }
368
        strcpy( code_filename, argv[ optind++ ] ) ;
369
        if ( strrchr( code_filename, '.' ) == NULL )
370
                strcat( code_filename, ".mem" ) ;
371
        if ( bVerbose )
372
                printf( "! code MEM file: %s\n", code_filename ) ;
373
 
374
        if ( strlen( entity_name ) == 0 ) {
375
                strcpy( entity_name, filename( code_filename ) ) ;
376
        }
377
        if ( bVerbose )
378
                printf( "! entity name: %s\n", entity_name ) ;
379
 
380
        if ( strlen( data_filename ) > 0 ) {
381
                if ( strrchr( data_filename, '.' ) == NULL )
382
                        strcat( data_filename, ".mem" ) ;
383
                if ( bVerbose )
384
                        printf( "! dataMEM file: %s\n", data_filename ) ;
385
        }
386
 
387
        // template filename
388
        if ( argv[ optind ] == NULL ) {
389
                strcpy( tpl_filename, "template.vhd" ) ;
390
        } else {
391
                strcpy( tpl_filename, argv[ optind++ ] ) ;
392
        }
393
        if ( strrchr( tpl_filename, '.' ) == NULL )
394
                strcat( tpl_filename, ".vhd" ) ;
395
        if ( bVerbose )
396
                printf( "! template file: %s\n", tpl_filename ) ;
397
 
398
        // output filename
399
        if ( argv[ optind ] == NULL ) {
400
                strcpy( rom_filename, filename( code_filename ) ) ;
401
        } else {
402
                strcpy( rom_filename, argv[ optind++ ] ) ;
403
        }
404
        if ( strrchr( rom_filename, '.' ) == NULL )
405
                strcat( rom_filename, ".vhd" ) ;
406
        if ( bVerbose )
407
                printf( "! output file: %s\n", rom_filename ) ;
408
 
409
        if ( loadMEM( code_filename, data_filename ) ) {
410
                mergeTPL( tpl_filename, rom_filename, entity_name ) ;
411
                exit( 0 ) ;
412
        } else
413
                exit( -2 ) ;
414
}

powered by: WebSVN 2.1.0

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