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

Subversion Repositories marca

[/] [marca/] [tags/] [INITIAL/] [spar/] [spar.l] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jeunes2
/* This file is part of the assembler "spar" for marca.
2
   Copyright (C) 2007 Wolfgang Puffitsch
3
 
4
   This program is free software; you can redistribute it and/or modify it
5
   under the terms of the GNU Library General Public License as published
6
   by the Free Software Foundation; either version 2, or (at your option)
7
   any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Library General Public License for more details.
13
 
14
   You should have received a copy of the GNU Library General Public
15
   License along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
17
 
18
%option nounput
19
 
20
%{
21
#include 
22
#include 
23
#include 
24
#include 
25
#include 
26
#include 
27
 
28
#include "spar.h"
29
#include "ui.h"
30
#include "checks.h"
31
#include "code.h"
32
#include "emit.h"
33
#include "expand.h"
34
#include "exprs.h"
35
#include "fixes.h"
36
#include "optab.h"
37
#include "output.h"
38
#include "segtab.h"
39
#include "symtab.h"
40
 
41
char *command = NULL;
42
 
43
int8_t  output_mode = DEFAULT_MODE;
44
FILE   *output_file = NULL;
45
FILE   *rom0_file = NULL;
46
FILE   *rom1_file = NULL;
47
 
48
uint32_t codesize = DEFAULT_CODESIZE;
49
uint32_t datasize = DEFAULT_DATASIZE;
50
uint32_t romsize  = DEFAULT_ROMSIZE;
51
uint16_t filler = DEFAULT_FILLER;
52
 
53
int nostart = 0;
54
int noend = 0;
55
 
56
uint32_t line_count = 1;
57
uint32_t file_count = 0;
58
uint32_t error_count = 0;
59
 
60
int      seen_op = 0;
61
int8_t   args_to_match = -1;
62
 
63
/***************** WHAT TO DO ****************/
64
int initialize(void);
65
int merge(void);
66
int postprocess(void);
67
 
68
/* this does not really fit anywhere */
69
void expand_mod(void);
70
 
71
/***************** SHORTCUT ****************/
72
#define MATCH_OP(X) \
73
do { \
74
  struct seg *seg = get_current_seg(); \
75
  adjust_segsize(seg, seg->pos+1); \
76
  set_op(seg, seg->pos, (X)); \
77
  trace_listing(seg, seg->pos, yytext); \
78
  trace_listing(seg, seg->pos, "\t"); \
79
  seen_op = 1; \
80
  args_to_match = get_arg_count(seg, seg->pos)*2 - 1; \
81
} while(0)
82
 
83
%}
84
 
85
REG  (r[0-9]+)
86
NUM  ((-?0[0-7]*)|(-?[1-9][0-9]*)|(-?0x[0-9a-fA-F]+))
87
BNUM (0b[0-1]+)
88
SYM  (\.?[_\.a-zA-Z0-9]+)
89
EXPR (([^ \t\r\n,;])|([^ \t\r\n,;][^,;\n]*[^ \t\r\n,;]))
90
 
91
%%
92
 
93
\.file[ \t]*\r?\n   file_count++;
94
 
95
\.text[ \t]*\r?\n   set_current_segnum(SEG_TEXT);
96
\.data[ \t]*\r?\n   set_current_segnum(SEG_DATA);
97
\.bss[ \t]*\r?\n    set_current_segnum(SEG_BSS);
98
 
99
\.align[ \t]{EXPR}\r?\n {
100
         struct seg *seg = get_current_seg();
101
         char *s = xstrdup(yytext+7);
102
         int32_t a = 1 << expr_nevaluate(s);
103
 
104
         while ((seg->pos % a) != 0)
105
           {
106
             if (get_current_segnum() == SEG_TEXT)
107
               {
108
                 emit_nop(seg);
109
               }
110
             else
111
               {
112
                 emit_zero(seg);
113
               }
114
           }
115
       }
116
 
117
\.comm[ \t]{SYM}[ \t]*,[ \t]*{EXPR}\r?\n {
118
         int32_t i;
119
         char *s = xstrdup(yytext+6);
120
         char *t = s;
121
 
122
         t = strpbrk(s, ",");
123
         *t++ = '\0';
124
 
125
         s = localize_string(s);
126
         check_sym_dup(s);
127
         push_sym(s, SEG_BSS, get_seg(SEG_BSS)->pos);
128
         trace_listing(get_seg(SEG_BSS), get_seg(SEG_BSS)->pos, s);
129
         trace_listing(get_seg(SEG_BSS), get_seg(SEG_BSS)->pos, ":\t");
130
 
131
         for (i=0; i < expr_nevaluate(t); i++)
132
           {
133
             emit_zero(get_seg(SEG_BSS));
134
           }
135
       }
136
 
137
\.lcomm[ \t]{SYM}[ \t]*,[ \t]*{EXPR}\r?\n {
138
         int32_t i;
139
         char *s = xstrdup(yytext+7);
140
         char *t = s;
141
 
142
         t = strpbrk(s, ",");
143
         *t++ = '\0';
144
 
145
         s = localize_string(s);
146
         check_sym_dup(s);
147
         push_sym(s, SEG_BSS, get_seg(SEG_BSS)->pos);
148
         trace_listing(get_seg(SEG_BSS), get_seg(SEG_BSS)->pos, s);
149
         trace_listing(get_seg(SEG_BSS), get_seg(SEG_BSS)->pos, ":\t");
150
 
151
         for (i=0; i < expr_nevaluate(t); i++)
152
           {
153
             emit_zero(get_seg(SEG_BSS));
154
           }
155
       }
156
 
157
\.org[ \t]{EXPR}\r?\n {
158
         struct seg *seg = get_current_seg();
159
         char *s = xstrdup(yytext+6);
160
         int32_t o = expr_nevaluate(s);
161
 
162
         check_org_advances(seg, o);
163
 
164
         while (seg->pos < o)
165
           {
166
             if (get_current_segnum() == SEG_TEXT)
167
               {
168
                 emit_nop(seg);
169
               }
170
             else
171
               {
172
                 emit_zero(seg);
173
               }
174
           }
175
       }
176
 
177
\.skip[ \t]{EXPR}\r?\n {
178
         struct seg *seg = get_current_seg();
179
         char *s = xstrdup(yytext+6);
180
         int32_t i;
181
 
182
         for (i = 0; i < expr_nevaluate(s); i++)
183
           {
184
             if (get_current_segnum() == SEG_TEXT)
185
               {
186
                 emit_nop(seg);
187
               }
188
             else
189
               {
190
                 emit_zero(seg);
191
               }
192
           }
193
       }
194
 
195
{SYM}: {
196
         char *s;
197
         struct seg *seg = get_current_seg();
198
         s = xstrdup(yytext);
199
         s[yyleng-1] = '\0';
200
         s = localize_string(s);
201
         check_sym_dup(s);
202
         push_sym(s, get_current_segnum(), seg->pos);
203
         trace_listing(seg, seg->pos, s);
204
         trace_listing(seg, seg->pos, ":\t");
205
       }
206
 
207
data     MATCH_OP(DATA);
208
add      MATCH_OP(ADD);
209
addc     MATCH_OP(ADDC);
210
sub      MATCH_OP(SUB);
211
subc     MATCH_OP(SUBC);
212
and      MATCH_OP(AND);
213
or       MATCH_OP(OR);
214
xor      MATCH_OP(XOR);
215
mul      MATCH_OP(MUL);
216
div      MATCH_OP(DIV);
217
udiv     MATCH_OP(UDIV);
218
ldil     MATCH_OP(LDIL);
219
ldih     MATCH_OP(LDIH);
220
ldib     MATCH_OP(LDIB);
221
mov      MATCH_OP(MOV);
222
not      MATCH_OP(NOT);
223
neg      MATCH_OP(NEG);
224
cmp      MATCH_OP(CMP);
225
addi     MATCH_OP(ADDI);
226
cmpi     MATCH_OP(CMPI);
227
shl      MATCH_OP(SHL);
228
shr      MATCH_OP(SHR);
229
sar      MATCH_OP(SAR);
230
mod      MATCH_OP(MOD);
231
umod     MATCH_OP(UMOD);
232
rolc     MATCH_OP(ROLC);
233
rorc     MATCH_OP(RORC);
234
bset     MATCH_OP(BSET);
235
bclr     MATCH_OP(BCLR);
236
btest    MATCH_OP(BTEST);
237
load     MATCH_OP(LOAD);
238
store    MATCH_OP(STORE);
239
loadl    MATCH_OP(LOADL);
240
loadh    MATCH_OP(LOADH);
241
loadb    MATCH_OP(LOADB);
242
storel   MATCH_OP(STOREL);
243
storeh   MATCH_OP(STOREH);
244
call     MATCH_OP(CALL);
245
 
246
br       MATCH_OP(BR);
247
brz      MATCH_OP(BRZ);
248
brnz     MATCH_OP(BRNZ);
249
brle     MATCH_OP(BRLE);
250
brlt     MATCH_OP(BRLT);
251
brge     MATCH_OP(BRGE);
252
brgt     MATCH_OP(BRGT);
253
brule    MATCH_OP(BRULE);
254
brult    MATCH_OP(BRULT);
255
bruge    MATCH_OP(BRUGE);
256
brugt    MATCH_OP(BRUGT);
257
sext     MATCH_OP(SEXT);
258
ldvec    MATCH_OP(LDVEC);
259
stvec    MATCH_OP(STVEC);
260
 
261
jmp      MATCH_OP(JMP);
262
jmpz     MATCH_OP(JMPZ);
263
jmpnz    MATCH_OP(JMPNZ);
264
jmple    MATCH_OP(JMPLE);
265
jmplt    MATCH_OP(JMPLT);
266
jmpge    MATCH_OP(JMPGE);
267
jmpgt    MATCH_OP(JMPGT);
268
jmpule   MATCH_OP(JMPULE);
269
jmpult   MATCH_OP(JMPULT);
270
jmpuge   MATCH_OP(JMPUGE);
271
jmpugt   MATCH_OP(JMPUGT);
272
intr     MATCH_OP(INTR);
273
getira   MATCH_OP(GETIRA);
274
setira   MATCH_OP(SETIRA);
275
getfl    MATCH_OP(GETFL);
276
setfl    MATCH_OP(SETFL);
277
getshfl  MATCH_OP(GETSHFL);
278
setshfl  MATCH_OP(SETSHFL);
279
 
280
reti     MATCH_OP(RETI);
281
nop      MATCH_OP(NOP);
282
sei      MATCH_OP(SEI);
283
cli      MATCH_OP(CLI);
284
error    MATCH_OP(ERROR);
285
 
286
{REG} {
287
        struct seg *seg = get_current_seg();
288
        int arg_index = get_arg_count(seg, seg->pos) - (args_to_match+1)/2;
289
 
290
        if (check_arg_count() || check_arg_type(seg, seg->pos, arg_index, 'r'))
291
          YY_BREAK;
292
 
293
        set_regnum(seg, seg->pos, arg_index, strtol(yytext+1, NULL, 10));
294
        set_mode(seg, seg->pos, arg_index, 'r');
295
        trace_listing(seg, seg->pos, yytext);
296
        args_to_match--;
297
      }
298
 
299
{EXPR} {
300
        struct seg *seg = get_current_seg();
301
        int arg_index;
302
 
303
        if (!seen_op)
304
          {
305
            REJECT;
306
          }
307
 
308
        arg_index = get_arg_count(seg, seg->pos) - (args_to_match+1)/2;
309
 
310
        if (check_arg_count() || check_arg_type(seg, seg->pos, arg_index, 'n'))
311
          YY_BREAK;
312
 
313
        set_expr(seg, seg->pos, arg_index, expr_localize(yytext));
314
        set_mode(seg, seg->pos, arg_index, 'n');
315
        trace_listing(seg, seg->pos, yytext);
316
        args_to_match--;
317
       }
318
 
319
,          {
320
             check_colon_ok();
321
             trace_listing(get_current_seg(), get_current_seg()->pos, ", ");
322
             args_to_match--;
323
           }
324
 
325
;.*        { /* ignore comments */ }
326
 
327
[ \t\r]+   { /* ignore whitespace */ }
328
 
329
\n+ {
330
      check_premature();
331
      expand_mod();
332
      if (seen_op)
333
        {
334
          get_current_seg()->pos++;
335
          seen_op = 0;
336
          args_to_match = -1;
337
        }
338
      line_count += yyleng;
339
    }
340
 
341
<> {
342
      check_premature();
343
      expand_mod();
344
      if (seen_op)
345
        {
346
          get_current_seg()->pos++;
347
          seen_op = 0;
348
          args_to_match = -1;
349
        }
350
      yyterminate();
351
    }
352
 
353
. {
354
    fprintf(stderr, "invalid input `%c' in line %lu\n", yytext[0], line_count);
355
    error_count++;
356
  }
357
 
358
%%
359
 
360
int initialize(void)
361
{
362
  init_symtab();
363
 
364
  init_seg(SEG_DATA, SEG_INITSIZE/2);
365
  init_seg(SEG_BSS, SEG_INITSIZE/2);
366
  init_seg(SEG_TEXT, SEG_INITSIZE);
367
 
368
  set_current_segnum(SEG_TEXT);
369
 
370
  return 0;
371
}
372
 
373
int merge(void)
374
{
375
  uint32_t pos;
376
  int retval = 0;
377
 
378
  init_seg(SEG_PILE, SEG_INITSIZE);
379
 
380
  /* align .data to word boundary */
381
  if ((get_seg(SEG_DATA)->pos % 2) != 0)
382
    {
383
      emit_zero(get_seg(SEG_DATA));
384
    }
385
 
386
  /* align .bss to word boundary */
387
  if ((get_seg(SEG_BSS)->pos % 2) != 0)
388
    {
389
      emit_zero(get_seg(SEG_BSS));
390
    }
391
 
392
  /* check .data */
393
  for (pos = 0; pos < get_seg(SEG_DATA)->pos; pos += 2)
394
    {
395
      if ((get_op(get_seg(SEG_DATA), pos) != DATA)
396
          || (get_op(get_seg(SEG_DATA), pos+1) != DATA))
397
        {
398
          fprintf(stderr, "cannot initialize anything but `data' in .data\n");
399
          error_count++;
400
          retval = 1;
401
        }
402
    }
403
 
404
  /* check .bss */
405
  for (pos = 0; pos < get_seg(SEG_BSS)->pos; pos++)
406
    {
407
      if ((get_op(get_seg(SEG_BSS), pos) != DATA)
408
          || (expr_symcount(get_expr(get_seg(SEG_BSS), pos, 0)) > 0)
409
          || (expr_evaluate(get_expr(get_seg(SEG_BSS), pos, 0)) != 0))
410
        {
411
          fprintf(stderr, "cannot put anything but `data 0' in .bss\n");
412
          error_count++;
413
          retval = 1;
414
        }
415
    }
416
 
417
  /* copy data from ROM */
418
  if (get_seg(SEG_DATA)->pos != 0)
419
    {
420
      emit_ldil(get_seg(SEG_PILE), 0,
421
                xsprintf("lo(%d)", get_seg(SEG_DATA)->pos));
422
      emit_ldih(get_seg(SEG_PILE), 0,
423
                xsprintf("hi(%d)", get_seg(SEG_DATA)->pos));
424
 
425
      emit_ldil(get_seg(SEG_PILE), 1,
426
                xsprintf("lo(%d)", datasize));
427
      emit_ldih(get_seg(SEG_PILE), 1,
428
                xsprintf("hi(%d)", datasize));
429
 
430
      emit_ldib(get_seg(SEG_PILE), 2, "0");
431
      push_sym("__init_data_loop__", SEG_PILE, get_seg(SEG_PILE)->pos);
432
      trace_listing(get_seg(SEG_PILE), get_seg(SEG_PILE)->pos,
433
                    "__init_data_loop__:\t");
434
      emit_load(get_seg(SEG_PILE), 3, 1);
435
      emit_store(get_seg(SEG_PILE), 3, 2);
436
      emit_addi(get_seg(SEG_PILE), 1, "2");
437
      emit_addi(get_seg(SEG_PILE), 2, "2");
438
      emit_cmp(get_seg(SEG_PILE), 0, 2);
439
      emit_brnz(get_seg(SEG_PILE), "__init_data_loop__");
440
    }
441
 
442
  /* clear .bss */
443
  if (get_seg(SEG_BSS)->pos != 0)
444
    {
445
      emit_ldil(get_seg(SEG_PILE), 0,
446
                xsprintf("lo(%d+%d)", get_seg(SEG_DATA)->pos,
447
                                      get_seg(SEG_BSS)->pos));
448
      emit_ldih(get_seg(SEG_PILE), 0,
449
                xsprintf("hi(%d+%d)", get_seg(SEG_DATA)->pos,
450
                                      get_seg(SEG_BSS)->pos));
451
      emit_ldib(get_seg(SEG_PILE), 3, "0");
452
      push_sym("__init_bss_loop__", SEG_PILE, get_seg(SEG_PILE)->pos);
453
      trace_listing(get_seg(SEG_PILE), get_seg(SEG_PILE)->pos,
454
                    "__init_bss_loop__:\t");
455
      emit_store(get_seg(SEG_PILE), 3, 2);
456
      emit_addi(get_seg(SEG_PILE), 2, "2");
457
      emit_cmp(get_seg(SEG_PILE), 0, 2);
458
      emit_brnz(get_seg(SEG_PILE), "__init_bss_loop__");
459
    }
460
 
461
  reloc_syms(SEG_BSS, get_seg(SEG_DATA)->pos);
462
  reloc_syms(SEG_TEXT, get_seg(SEG_PILE)->pos);
463
 
464
  for(pos = 0; pos < get_seg(SEG_TEXT)->pos; pos++)
465
    {
466
      emit_op(get_seg(SEG_PILE), get_seg(SEG_TEXT)->code[pos]);
467
    }
468
 
469
  set_current_segnum(SEG_PILE);
470
 
471
  return retval;
472
}
473
 
474
int postprocess(void)
475
{
476
  struct seg *seg;
477
  uint32_t pos;
478
  int retval = 0;
479
 
480
  if ((output_mode != MODE_DRYRUN)
481
      && (check_code_size()
482
          || check_data_size()))
483
    {
484
      retval = 1;
485
    }
486
 
487
  seg = get_seg(SEG_PILE);
488
  for (pos = 0; pos < seg->pos; pos++)
489
    {
490
      if (check_mnemonic(seg, pos))
491
        {
492
          retval = 1;
493
        }
494
      if ((output_mode != MODE_DRYRUN)
495
          && (fix_operands(seg, pos)
496
              || check_ranges(seg, pos)
497
              || fix_code(seg, pos)))
498
        {
499
          retval = 1;
500
        }
501
    }
502
 
503
  seg = get_seg(SEG_DATA);
504
  for (pos = 0; pos < seg->pos; pos++)
505
    {
506
      if (check_mnemonic(seg, pos))
507
        {
508
          retval = 1;
509
        }
510
      if ((output_mode != MODE_DRYRUN)
511
          && (fix_operands(seg, pos)
512
              || check_ranges(seg, pos)
513
              || fix_code(seg, pos)))
514
        {
515
          retval = 1;
516
        }
517
    }
518
 
519
  return retval;
520
}
521
 
522
/***************** LIB *****************/
523
 
524
void *xmalloc(size_t size)
525
{
526
  void *retval = malloc(size);
527
  if (retval == NULL)
528
    {
529
      fprintf(stderr, "run out of memory\n");
530
      exit(EXIT_FAILURE);
531
    }
532
  return retval;
533
}
534
 
535
void *xrealloc(void *ptr, size_t size)
536
{
537
  void *retval = realloc(ptr, size);
538
  if (retval == NULL)
539
    {
540
      fprintf(stderr, "run out of memory\n");
541
      exit(EXIT_FAILURE);
542
    }
543
  return retval;
544
}
545
 
546
char *xstrdup(const char *str)
547
{
548
  size_t len = strlen(str);
549
  void *retval = xmalloc(len+1);
550
  memcpy(retval, str, len+1);
551
  return retval;
552
}
553
 
554
char *xsprintf(const char *fmt, ...)
555
{
556
  va_list ap;
557
  char *retval;
558
 
559
  va_start(ap, fmt);
560
  vasprintf(&retval, fmt, ap);
561
  va_end(ap);
562
 
563
  if (retval == NULL)
564
    {
565
      fprintf(stderr, "run out of memory\n");
566
      exit(EXIT_FAILURE);
567
    }
568
 
569
  return retval;
570
}
571
 
572
/***************** MAIN *****************/
573
 
574
int yywrap()
575
{
576
  return 1;
577
}
578
 
579
int main(int argc, char **argv)
580
{
581
  int opt;
582
 
583
  int opterr = 0;
584
 
585
  int hopt = 0;
586
  int vopt = 0;
587
 
588
  command = argv[0];
589
 
590
  while (optind < argc)
591
    {
592
      if (strcmp(argv[optind], "-") == 0 || (argv[optind][0] != '-'))
593
        {
594
          opt = -1;
595
        }
596
      else
597
        {
598
          opt = getopt_long(argc, argv, options, long_options, 0);
599
        }
600
 
601
      switch(opt)
602
        {
603
        case 'h': hopt++; break;
604
        case 'v': vopt++; break;
605
        case 'o': case '0': case '1':
606
        case 'c': case 'd': case 'r':
607
        case 'f': case 'i': case 'n': case 'l':
608
        case 'm': case 's': case 'e':
609
          break;
610
        case '?': opterr = 1; break;
611
        case -1: optind++; break;
612
        }
613
    }
614
 
615
  if (opterr)
616
    {
617
      printf("Usage:\n"
618
             "    %s [--intel|--nointel|--download|--dryrun]\n"
619
             "    [--codesize=*n*] [--datasize=*n*] [--romsize=*n*]\n"
620
             "    [--filler=*n*] [--output=file] [--rom0file=file] [--rom1file=file]\n"
621
             "    [--nostart] [--noend] file ...\n", command);
622
      return EXIT_FAILURE;
623
    }
624
 
625
  if (vopt)
626
    {
627
      printf("spar 0.2\n");
628
      return EXIT_SUCCESS;
629
    }
630
 
631
  if (hopt)
632
    {
633
      printf("Usage:\n"
634
             "    %s [--intel|--nointel|--download|--dryrun]\n"
635
             "    [--codesize=*n*] [--datasize=*n*] [--romsize=*n*]\n"
636
             "    [--filler=*n*] [--output=file] [--rom0file=file] [--rom1file=file]\n"
637
             "    [--nostart] [--noend] file ...\n", command);
638
      return EXIT_SUCCESS;
639
    }
640
 
641
  optind = 1;
642
 
643
  initialize();
644
 
645
  while (optind < argc)
646
    {
647
      if (strcmp(argv[optind], "-") == 0 || (argv[optind][0] != '-'))
648
        {
649
          opt = -1;
650
        }
651
      else
652
        {
653
          opt = getopt_long(argc, argv, options, long_options, 0);
654
        }
655
 
656
      switch (opt)
657
        {
658
        case 'h': case 'v':
659
          break;
660
        case 'o':
661
          if (strcmp(optarg, "-") == 0)
662
            {
663
              output_file = stdout;
664
            }
665
          else
666
            {
667
              output_file = fopen(optarg, "w");
668
              if (output_file == NULL)
669
                {
670
                  fprintf(stderr, "cannot open file %s\n", optarg);
671
                  return EXIT_FAILURE;
672
                }
673
            }
674
          break;
675
        case '0':
676
          if (strcmp(optarg, "-") == 0)
677
            {
678
              rom0_file = stdout;
679
            }
680
          else
681
            {
682
              rom0_file = fopen(optarg, "w");
683
              if (rom0_file == NULL)
684
                {
685
                  fprintf(stderr, "cannot open file %s\n", optarg);
686
                  return EXIT_FAILURE;
687
                }
688
            }
689
          break;
690
        case '1':
691
          if (strcmp(optarg, "-") == 0)
692
            {
693
              rom1_file = stdout;
694
            }
695
          else
696
            {
697
              rom1_file = fopen(optarg, "w");
698
              if (rom1_file == NULL)
699
                {
700
                  fprintf(stderr, "cannot open file %s\n", optarg);
701
                  return EXIT_FAILURE;
702
                }
703
            }
704
          break;
705
        case 'c':
706
          {
707
            char * errptr = NULL;
708
            romsize = strtol(optarg, &errptr, 0);
709
            if (*errptr != '\0')
710
              {
711
                fprintf(stderr, "invalid codesize %s\n", optarg);
712
                return EXIT_FAILURE;
713
              }
714
          }
715
          break;
716
        case 'd':
717
          {
718
            char * errptr = NULL;
719
            romsize = strtol(optarg, &errptr, 0);
720
            if (*errptr != '\0')
721
              {
722
                fprintf(stderr, "invalid datasize %s\n", optarg);
723
                return EXIT_FAILURE;
724
              }
725
          }
726
          break;
727
        case 'r':
728
          {
729
            char * errptr = NULL;
730
            romsize = strtol(optarg, &errptr, 0);
731
            if (*errptr != '\0')
732
              {
733
                fprintf(stderr, "invalid romsize %s\n", optarg);
734
                return EXIT_FAILURE;
735
              }
736
          }
737
          break;
738
        case 'f':
739
          {
740
            char * errptr = NULL;
741
            filler = strtol(optarg, &errptr, 0);
742
            if (*errptr != '\0')
743
              {
744
                fprintf(stderr, "invalid filler %s\n", optarg);
745
                return EXIT_FAILURE;
746
              }
747
          }
748
          break;
749
        case 'i': output_mode = MODE_INTEL; break;
750
        case 'n': output_mode = MODE_DRYRUN; break;
751
        case 'l': output_mode = MODE_DOWNLOAD; break;
752
        case 'm': output_mode = MODE_MIF; break;
753
        case 's': nostart = 1; break;
754
        case 'e': noend = 1; break;
755
        case '?': break;
756
        case -1:
757
          if (argv[optind] == NULL)
758
            break;
759
          if (strcmp(argv[optind], "-") == 0)
760
            {
761
              yyin = stdin;
762
            }
763
          else
764
            {
765
              yyin = fopen(argv[optind], "r");
766
              if (yyin == NULL)
767
                {
768
                  fprintf(stderr, "cannot open file %s\n", argv[optind]);
769
                  return EXIT_FAILURE;
770
                }
771
            }
772
          yylex();
773
          file_count++;
774
 
775
          optind++;
776
          break;
777
        }
778
    }
779
 
780
  if (output_file == NULL)
781
    {
782
      output_file = stdout;
783
    }
784
  if (rom0_file == NULL)
785
    {
786
      rom0_file = stdout;
787
    }
788
  if (rom1_file == NULL)
789
    {
790
      rom1_file = stdout;
791
    }
792
 
793
  if (file_count == 0)
794
    {
795
      yyin = stdin;
796
      yylex();
797
    }
798
 
799
  merge();
800
 
801
  postprocess();
802
 
803
  if (error_count != 0)
804
    {
805
      fprintf(stderr, "found %lu error(s)\n", error_count);
806
      return EXIT_FAILURE;
807
    }
808
 
809
  if (output_file == NULL)
810
    {
811
      output_file = stdout;
812
    }
813
  if (rom0_file == NULL)
814
    {
815
      rom0_file = stdout;
816
    }
817
  if (rom1_file == NULL)
818
    {
819
      rom1_file = stdout;
820
    }
821
 
822
  switch (output_mode)
823
    {
824
    case MODE_DRYRUN: break;
825
    case MODE_DOWNLOAD: print_download(output_file, rom0_file, rom1_file); break;
826
    case MODE_INTEL: print_intel(output_file, rom0_file, rom1_file); break;
827
    case MODE_MIF: print_mif(output_file, rom0_file, rom1_file); break;
828
    }
829
 
830
  if (output_file != stdout)
831
    {
832
      fclose(output_file);
833
    }
834
  if (rom0_file != stdout)
835
    {
836
      fclose(rom0_file);
837
    }
838
  if (rom1_file != stdout)
839
    {
840
      fclose(rom1_file);
841
    }
842
 
843
  return EXIT_SUCCESS;
844
}

powered by: WebSVN 2.1.0

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