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

Subversion Repositories suslik

[/] [suslik/] [trunk/] [src/] [assembler/] [ascpu2.c] - Blame information for rev 3

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

Line No. Rev Author Line
1 2 gorand2
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
 
5
 
6
typedef enum {enc_none,enc_reg,enc_rir,enc_cjmp,enc_nir,enc_store,enc_rnn,enc_in,enc_out} ENCODING;
7
struct encentry { ENCODING enc;int opcode;char *name;}; //if name==NULL ==>end of list
8
struct instr_reg { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned _pad:11;};
9
struct instr_cjmp { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
10
struct instr_rir { unsigned opcode:6; unsigned rA:5,con_0_4:5,rC:5; unsigned con_5_15:11;};
11
struct instr_store { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
12
struct instr_io { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned auxcode:6; unsigned _pad:5;};
13
 
14
typedef union {struct instr_reg reg; struct instr_cjmp cjmp; struct instr_rir rir; struct instr_store store; struct instr_io io; unsigned int code; } instruction;
15
 
16
typedef enum {reloc_cjmp,reloc_li,reloc_lih,reloc_imm,reloc_store} RELOCTYPE;
17
struct reloc {struct reloc *next;int offset; int section; RELOCTYPE type;};
18
struct label {char *name; int offset; int section; int resolved; struct reloc *reloc;};
19
struct label_array {int size; int count; struct label *labels;} labels[256];
20
int labelcount=0,labelsize=1024;
21
 
22
int currentsection=0;
23
instruction *program;
24
int instrcount=0,programlength=1024;
25
 
26
struct section { int length;int size; char *data; unsigned int addr; } sectn[3];
27
 
28
char *reallocz(char * addr, int old_size, int new_size)
29
{
30
  char * rtn;
31
  rtn=realloc(addr,new_size);
32
  if (old_size<new_size) memset(rtn+old_size,0,new_size-old_size);
33
  return rtn;
34
}
35
 
36
int findhash(char *name)
37
{
38
int hash=0,i;
39
for (i=0; name[i]!=0; i++) hash=hash+name[i];
40
return hash & 0xff;
41
}
42
 
43
int findlabel(char *name)
44
{
45
int hash,i,found=0;
46
hash=findhash(name);
47
for(i=0;i<labels[hash].count;i++)
48
 {
49
  if(strcmp(name,labels[hash].labels[i].name)==0)
50
   {
51
    found=1;
52
    break;
53
   }
54
 }
55
if (found) return i;
56
 else return -1;
57
}
58
 
59
int addlabel(char *name, int offset, int section,int resolved)
60
{
61
char *szname;
62
int hash,labelcount,i;
63
hash=findhash(name);
64
i=findlabel(name);
65
if (i>=0)
66
 {
67
  if (resolved)
68
   {
69
    labels[hash].labels[i].offset=offset;
70
    labels[hash].labels[i].section=section;
71
    labels[hash].labels[i].resolved=1;
72
   }
73
  return i;
74
 }
75
else
76
 {
77
  if (labels[hash].count>=labels[hash].size)
78
   {
79
    labels[hash].labels=reallocz(labels[hash].labels,labels[hash].size,labels[hash].size*2);
80
    labels[hash].size=labels[hash].size*2;
81
   }
82
  szname=malloc(strlen(name)+1);
83
  strcpy(szname,name);
84
  labelcount=labels[hash].count;
85
  labels[hash].labels[labelcount].name=szname;
86
  labels[hash].labels[labelcount].offset=offset;
87
  labels[hash].labels[labelcount].section=section;
88
  labels[hash].labels[labelcount].resolved=resolved;
89
  labels[hash].labels[labelcount].reloc=NULL;
90
  labels[hash].count++;
91
  return labelcount;
92
 }
93
}
94
 
95
void addreloc(char *labelname, int offset, int section, RELOCTYPE type)
96
{
97
int i,hash;
98
struct reloc *myreloc;
99
hash=findhash(labelname);
100
i=addlabel(labelname,0,0,0);
101
myreloc=malloc(sizeof(struct reloc));
102
myreloc->next=labels[hash].labels[i].reloc;
103
myreloc->offset=offset;
104
myreloc->section=section;
105
myreloc->type=type;
106
labels[hash].labels[i].reloc=myreloc;
107
 
108
}
109
 
110
void apply_relocs()
111
{
112
int i,hash;
113
struct reloc *rel;
114
instruction instr;
115
unsigned short int sh_a;
116
 
117
for (hash=0;hash<=255;hash++)
118
{
119
 for (i=0; i<labels[hash].count; i++)
120
 {
121
  rel=labels[hash].labels[i].reloc;
122
  while (rel!=NULL)
123
   {
124
    switch(rel->type)
125
     {
126
      case reloc_cjmp:
127
      if (rel->section==0)
128
       {
129
         if (labels[hash].labels[i].section==0)
130
          {
131
           ((short *) sectn[0].data)[(rel->offset+2)/2]=(labels[hash].labels[i].offset-rel->offset)/4;
132
          }
133
       }
134
      break;
135
      case reloc_li:
136
      if (rel->section==0)
137
       {
138
        instr=((instruction *) sectn[0].data)[rel->offset/4];
139
        sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset);
140
        //  fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a); 
141
        instr.rir.con_0_4=sh_a & 0x1f;
142
        instr.rir.con_5_15=sh_a >> 5;
143
        ((instruction *) sectn[0].data)[rel->offset/4]=instr;
144
       }
145
      break;
146
      case reloc_lih:
147
      if (rel->section==0)
148
       {
149
        instr=((instruction *) sectn[0].data)[rel->offset/4];
150
        sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
151
        instr.rir.con_0_4=sh_a & 0x1f;
152
        instr.rir.con_5_15=sh_a >> 5;
153
        ((instruction *) sectn[0].data)[rel->offset/4]=instr;
154
       }
155
      break;
156
      case reloc_imm:
157
      if (rel->section==0)
158
       {
159
        instr=((instruction *) sectn[0].data)[rel->offset/4];
160
        sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
161
        instr.store.offset=sh_a;
162
        ((instruction *) sectn[0].data)[rel->offset/4]=instr;
163
       }
164
      break;
165
      case reloc_store:
166
      if (rel->section==0)
167
       {
168
        instr=((instruction *) sectn[0].data)[rel->offset/4];
169
        sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) & 0xffff;
170
        //fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a); 
171
        instr.store.offset=sh_a;
172
        ((instruction *) sectn[0].data)[rel->offset/4]=instr;
173
       }
174
      break;
175
 
176
     }
177
    rel=rel->next;
178
   }
179
 }
180
}
181
}
182
 
183
int main()
184
{
185
int chr;
186
int toplevel=1;
187
int tokenchars=0;
188
char token[256];
189
int tokencount=0;
190
char tokens[16][256];
191
int i,a,b,c,d;
192
signed short sh_a;
193
int comma=0;
194
int PC=0;
195
int nexttoken=0;
196
int error=0;
197
int inquote=0;
198
int incomment=0;
199
int lineno=0;
200
 
201
instruction instr;
202
instruction extraInstr;
203
int hasExtraInstr=0;
204
 
205
static struct encentry instrset[]={
206
{enc_rir,0,"lih"},
207
{enc_rir,0,"lch"},
208
{enc_nir,1,"li"},
209
{enc_nir,1,"lc"},
210
{enc_reg,2,"and"},
211
{enc_rir,3,"andi"},
212
{enc_reg,4,"or"},
213
{enc_rir,5,"ori"},
214
{enc_reg,6,"xor"},
215
{enc_rir,7,"xori"},
216
{enc_reg,8,"add"},
217
{enc_rir,9,"addi"},
218
{enc_reg,10,"sub"},
219
{enc_rir,11,"subi"},
220
{enc_none,12,"nop"},
221
{enc_rir,13,"andi1"},
222
{ enc_reg, 14, "shl" },
223
{ enc_rir, 15, "shli" },
224
{ enc_reg, 16, "shr" },
225
{ enc_rir, 17, "shri" },
226
{ enc_reg, 18, "sar" },
227
{ enc_rir, 19, "sari" },
228
{ enc_in, 0, "inb" },
229
{enc_in,1,"inw"},
230
{enc_in,2,"inl"},
231
{enc_out,4,"outb"},
232
{enc_out,5,"outw"},
233
{enc_out,6,"outl"},
234
{enc_cjmp,32,"cjule"},
235
{enc_cjmp,32,"cjc"},
236
{enc_cjmp,33,"cjugt"},
237
{enc_cjmp,33,"cjnc"},
238
{enc_cjmp,34,"cjeq"},
239
{enc_cjmp,35,"cjne"},
240
{enc_cjmp,36,"cjult"},
241
{enc_cjmp,37,"cjuge"},
242
{enc_cjmp,38,"cjn"},
243
{enc_cjmp,39,"cjnn"},
244
{enc_cjmp,40,"cjslt"},
245
{enc_cjmp,41,"cjsge"},
246
{enc_cjmp,42,"cjsle"},
247
{enc_cjmp,43,"cjsgt"},
248
{enc_cjmp,44,"cjo"},
249
{enc_cjmp,45,"cjno"},
250
{enc_rir,46,"call"},
251
{enc_rnn,47,"ret"},
252
{enc_rir,56,"ldl"},
253
{enc_rir,57,"ldw"},
254
{enc_rir,58,"ldb"},
255
{enc_store,60,"stl"},
256
{enc_store,61,"stw"},
257
{enc_store,62,"stb"},
258
{enc_none,-1,NULL}
259
};
260
 
261
//program=malloc(programlength*(sizeof(instruction)));
262
for (i=0; i<=255; i++)
263
 {
264
  labels[i].count=0;
265
  labels[i].size=16;
266
  labels[i].labels=malloc(16*(sizeof(struct label)));
267
  memset(labels[i].labels,0,16*(sizeof(struct label)));
268
 }
269
 
270
for (i=0; i<=2; i++)
271
{
272
sectn[i].length=0;
273
sectn[i].addr=0;
274
sectn[i].size=4096;
275
sectn[i].data=malloc(4096);
276
memset(sectn[i].data,0,4096);
277
}
278
 
279
while(1)
280
 {
281
  chr=fgetc(stdin);
282
  if (chr==';') {incomment=1; continue; }
283
  if (incomment) goto handle_newline_eof;
284
  if (chr=='"')
285
   {
286
    if (!inquote) {inquote=1; continue;}
287
    else { inquote=0; continue;}
288
   }
289
  if (inquote && (chr!='\n') && (chr!=EOF))
290
  {
291
   if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
292
   continue;
293
  }
294
  if ((chr>='a' && chr<='z')||(chr>='A' && chr<='Z')||(chr>='0' && chr<='9')||(chr=='_'))
295
   {
296
    comma=0;
297
    if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
298
   }
299
  if (chr==' ' || chr=='\t')
300
   {if (toplevel) {} else
301
    {for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
302
     tokens[tokencount][tokenchars]=0;
303
     tokencount++;
304
     toplevel=1;
305
    }
306
   }
307
  if (chr==',' || chr==':') //single character tokens
308
   {if (toplevel) {tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;} else
309
    {for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
310
     tokens[tokencount][tokenchars]=0;
311
     tokencount++;
312
     tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;
313
     toplevel=1;
314
    }
315
   }
316
 /* if (chr==':')
317
   {if (toplevel) {tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;} else
318
    {for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
319
     tokens[tokencount][tokenchars]=0;
320
     tokencount++;
321
     tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;
322
     toplevel=1;
323
    }
324
   }*/
325
  handle_newline_eof:
326
  if (chr=='\n' || chr==EOF)
327
   {
328
    incomment=0;
329
    lineno++;
330
    if (toplevel) {} else
331
    {for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
332
     tokens[tokencount][tokenchars]=0;
333
     tokencount++;
334
     toplevel=1;
335
    }
336
    //for (i=0;i<tokencount;i++) printf ("t%i:%s\n",i,&tokens[i][0]);
337
    nexttoken=0;
338
    if (tokencount>=2 && !strcmp(tokens[1],":"))
339
    {addlabel(tokens[0],sectn[currentsection].length,currentsection,1);
340
     nexttoken=2;
341
    }
342
    //i=0;
343
    error=1;
344
    if (tokencount<=nexttoken) error=0;
345
    //printf("eRror %i\n",error);
346
    if (tokencount>nexttoken+1)
347
    {
348
     if (!strcmp(tokens[nexttoken],"section"))
349
     {
350
      if (!strcmp(tokens[nexttoken+1],"code")) {error=0;currentsection=0;}
351
      if (!strcmp(tokens[nexttoken+1],"data")) {error=0;currentsection=1;}
352
      if (!strcmp(tokens[nexttoken+1],"bss" )) {error=0;currentsection=2;}
353
     }
354
 
355
     if (!strcmp(tokens[nexttoken],"align"))
356
     {
357
       if (sscanf(tokens[nexttoken+1],"0x%x",&a)==1)
358
         error=0;
359
       else if (sscanf(tokens[nexttoken+1],"%i",&a)==1)
360
         error=0;
361
       if (error==0)
362
       {
363
         error=1;
364
         b=0xffffffff;
365
         for (i=0; (i<7) && (a!=1<<i); i++)
366
         {
367
           b=b<<1;
368
         }
369
         if (i<7)
370
         {
371
            if (((sectn[currentsection].length+(1<<i)-1) & b) > sectn[currentsection].size)
372
            {
373
              sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
374
              sectn[currentsection].size+=sectn[currentsection].size;
375
            }
376
            sectn[currentsection].length=(sectn[currentsection].length+(1<<i)-1) & b;
377
            error=0;
378
         }
379
       }
380
 
381
     }
382
 
383
     if (!strcmp(tokens[nexttoken],"DS"))
384
     {
385
      for (i=0; i<strlen(tokens[nexttoken+1]); i++)
386
      {
387
       //if (sscanf(tokens[i],"%i",&a)!=1)
388
       //  sscanf(tokens[i],"0x%x",&a);
389
       a=tokens[nexttoken+1][i];
390
       if (sectn[currentsection].length>=sectn[currentsection].size)
391
       {
392
        sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
393
        sectn[currentsection].size+=sectn[currentsection].size;
394
       }
395
       sectn[currentsection].data[sectn[currentsection].length]=a;
396
       sectn[currentsection].length++;
397
      }
398
      error=0;
399
     } //DS
400
 
401
     if (!strcmp(tokens[nexttoken],"DB"))
402
     {
403
      for (i=nexttoken+1; i<tokencount; i++)
404
      {
405
       if (sscanf(tokens[i],"0x%x",&a)!=1)
406
         sscanf(tokens[i],"%i",&a);
407
       if (sectn[currentsection].length>=sectn[currentsection].size)
408
       {
409
        sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
410
        sectn[currentsection].size+=sectn[currentsection].size;
411
       }
412
       sectn[currentsection].data[sectn[currentsection].length]=a;
413
       sectn[currentsection].length++;
414
      }
415
      error=0;
416
     }
417
     if (!strcmp(tokens[nexttoken],"DW"))
418
     {
419
      for (i=nexttoken+1; i<tokencount; i++)
420
      {
421
       if (sscanf(tokens[i],"0x%x",&a)!=1)
422
         sscanf(tokens[i],"%i",&a);
423
       if (sectn[currentsection].length+2>sectn[currentsection].size)
424
       {
425
        sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
426
        sectn[currentsection].size+=sectn[currentsection].size;
427
       }
428
       *((short int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
429
       sectn[currentsection].length+=2;
430
      }
431
      error=0;
432
     }
433
     if (!strcmp(tokens[nexttoken],"DL"))
434
     {
435
      for (i=nexttoken+1; i<tokencount; i++)
436
      {
437
       if (sscanf(tokens[i],"0x%x",&a)!=1)
438
         sscanf(tokens[i],"%i",&a);
439
       if (sectn[currentsection].length+4>sectn[currentsection].size)
440
       {
441
        sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
442
        sectn[currentsection].size+=sectn[currentsection].size;
443
       }
444
       *((int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
445
       sectn[currentsection].length+=4;
446
 
447
      }
448
      error=0;
449
     }
450
    }
451
 
452
    if ((tokencount>nexttoken) && (error==1))
453
    {
454
     for (i=0;;i++)
455
     {
456
      if (instrset[i].name==NULL) break;
457
      if (strcmp(tokens[nexttoken],instrset[i].name)==0) break;
458
     }
459
     instr.code=0;
460
     error=0;
461
     hasExtraInstr=0;
462
     extraInstr.code=0;
463
     if (instrset[i].name==NULL) error=1;
464
     else
465
     switch (instrset[i].enc)
466
     {
467
      case enc_none: instr.reg.opcode=instrset[i].opcode; break;
468
      case enc_reg:
469
       if (tokencount-nexttoken==6)
470
       {
471
        instr.reg.opcode=instrset[i].opcode;
472
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
473
        if (strcmp(tokens[nexttoken+2],",")) error=1;
474
        if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
475
        if (strcmp(tokens[nexttoken+4],",")) error=1;
476
        if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
477
        instr.reg.rA=a;
478
        instr.reg.rB=b;
479
        instr.reg.rC=c;
480
 
481
       }
482
       else error=1;
483
       break;
484
      case enc_rir:
485
       if (tokencount-nexttoken==6)
486
       {
487
        instr.rir.opcode=instrset[i].opcode;
488
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
489
        if (strcmp(tokens[nexttoken+2],",")) error=1;
490
        if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
491
         if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
492
         {
493
          addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
494
          addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_li);
495
          d=0xbaadf00d;
496
         }
497
        if (strcmp(tokens[nexttoken+4],",")) error=1;
498
        if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
499
        sh_a=(signed short) d;
500
        instr.rir.rA=a;
501
        instr.rir.rC=c;
502
        instr.rir.con_0_4=sh_a & 0x1f;
503
        instr.rir.con_5_15=sh_a >> 5;
504
        if (sh_a!=d)
505
        {
506
         extraInstr.store.opcode=30;
507
         extraInstr.store.offset=d>>16;
508
         hasExtraInstr=1;
509
        }
510
 
511
       }
512
       else error=1;
513
       break;
514
      case enc_nir:
515
       if (tokencount-nexttoken==4)
516
       {
517
        instr.rir.opcode=instrset[i].opcode;
518
        if (sscanf(tokens[nexttoken+1],"0x%x",&a)!=1)
519
         if (sscanf(tokens[nexttoken+1],"%d",&a)!=1)
520
           {//li
521
            addreloc(tokens[nexttoken+1],sectn[0].length+4,0,reloc_li);
522
            addreloc(tokens[nexttoken+1],sectn[0].length,0,reloc_imm);
523
            a=0xbaadf00d;
524
            //printf("reloc\n");
525
           }
526
        if (strcmp(tokens[nexttoken+2],",")) error=1;
527
        if (sscanf(tokens[nexttoken+3],"r%d",&c)!=1) error=1;
528
        sh_a=(signed short) a;
529
        instr.rir.rA=0;
530
        instr.rir.rC=c;
531
        instr.rir.con_0_4=sh_a & 0x1f;
532
        instr.rir.con_5_15=sh_a >> 5;
533
        //printf("sh_a=%i,token=%s\n",sh_a,tokens[nexttoken+1]);
534
        if (sh_a!=a)
535
        {
536
         hasExtraInstr=1;
537
         extraInstr.store.opcode=30;
538
         extraInstr.store.offset=a>>16;
539
        }
540
       }
541
       else error=1;
542
       break;
543
 
544
      case enc_cjmp:
545
       if (tokencount-nexttoken==6)
546
       {
547
        instr.reg.opcode=instrset[i].opcode;
548
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
549
        if (strcmp(tokens[nexttoken+2],",")) error=1;
550
        if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
551
        if (strcmp(tokens[nexttoken+4],",")) error=1;
552
        //if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
553
        instr.cjmp.rA=a;
554
        instr.cjmp.rB=b;
555
        if (error==0) addreloc(tokens[nexttoken+5],sectn[0].length,0,reloc_cjmp);
556
 
557
       }
558
       else error=1;
559
       break;
560
 
561
      case enc_store:
562
       if (tokencount-nexttoken==6)
563
       {
564
        instr.rir.opcode=instrset[i].opcode;
565
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
566
        if (strcmp(tokens[nexttoken+2],",")) error=1;
567
        if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
568
         if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
569
         {
570
          addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_store);
571
          addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
572
          d=0xbaadf00d;
573
         }
574
        if (strcmp(tokens[nexttoken+4],",")) error=1;
575
        if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
576
        sh_a=(signed short) d;
577
        instr.cjmp.rA=a;
578
        instr.cjmp.rB=c;
579
        instr.cjmp.offset=sh_a;
580
        if (sh_a!=d)
581
        {
582
         hasExtraInstr=1;
583
         extraInstr.store.opcode=30;
584
         extraInstr.store.offset=d>>16;
585
        }
586
 
587
       }
588
       else error=1;
589
       break;
590
 
591
      case enc_rnn:
592
       if (tokencount-nexttoken==2)
593
       {
594
        instr.rir.opcode=instrset[i].opcode;
595
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
596
        instr.rir.rA=a;
597
 
598
       }
599
       else error=1;
600
      break;
601
 
602
      case enc_in:
603
       if (tokencount-nexttoken==4)
604
       {
605
        instr.io.opcode=31;
606
        instr.io.auxcode=instrset[i].opcode;
607
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
608
        if (strcmp(tokens[nexttoken+2],",")) error=1;
609
        if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
610
 
611
        instr.io.rA=a;
612
        instr.io.rB=0;
613
        instr.io.rC=b;
614
 
615
       }
616
       else error=1;
617
       break;
618
 
619
      case enc_out:
620
       if (tokencount-nexttoken==4)
621
       {
622
        instr.io.opcode=31;
623
        instr.io.auxcode=instrset[i].opcode;
624
        if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
625
        if (strcmp(tokens[nexttoken+2],",")) error=1;
626
        if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
627
 
628
        instr.io.rA=a;
629
        instr.io.rB=b;
630
        instr.io.rC=0;
631
 
632
       }
633
       else error=1;
634
       break;
635
 
636
      default: error=1;
637
     }
638
//     if (error==0) printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
639
     if (error==0)
640
     {
641
 //     printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
642
      if (hasExtraInstr)
643
       {
644
         if (sectn[0].length+4>sectn[0].size)
645
          {
646
           sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
647
           sectn[0].size+=sectn[0].size;
648
          }
649
        ((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=extraInstr;
650
        PC+=4;
651
        sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
652
       }
653
      if (sectn[0].length+4>sectn[0].size)
654
       {
655
        sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
656
        sectn[0].size+=sectn[0].size;
657
       }
658
      ((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=instr;
659
      PC+=4;
660
      sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
661
     }
662
 
663
    } //else error=1;
664
    if (error) printf("error in line %i\n",lineno);
665
    tokencount=0;
666
    if (chr==EOF)
667
     {
668
      //printf("EOF\n");
669
      sectn[0].addr=0;//todo:origin
670
      for (i=1; i<=2; i++)
671
       sectn[i].addr=(sectn[i-1].addr+sectn[i-1].length+63) & 0xffffffc0;
672
      apply_relocs();
673
      for (a=0; a<=1; a++)
674
      {
675
       //printf("a=%i\n",a);
676
       for (i=0;i<((sectn[a].length+63)& 0xffffffc0)/4;i++)
677
       {
678
        printf("%x\n",((unsigned int *) sectn[a].data)[i]);
679
       }
680
      }
681
      exit(0);
682
 
683
     }
684
   }
685
 
686
 }
687
 
688
}

powered by: WebSVN 2.1.0

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