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

Subversion Repositories ncore

[/] [ncore/] [web_uploads/] [CASM.C] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 root
//STEFAN, Istvan
2
//Little endian???
3
//published under GPL
4
//Mukodik:
5
//      utasitasok, regiszterek, direktivak felismerese
6
//      kovetkezo direktivaknal szam felismerese:
7
//              .byte,.word,.dword,.qword,.ascii,.asciiz
8
//              nincs szohatarhoz igazitva az adatsor vege sem eleje
9
//      nincs kezelve, szukseges?:
10
//              .nibble,.bool,.space!!!Ez kellene
11
//              .stack:verem meretenek beallitasa
12
//      todo:
13
//              stack meretenek olvasasa
14
//              hexa szamok felismerese (boolean, octa?)
15
//              szohatarhoz igazitas
16
//              kimeneti fajlformatum:
17
//                      szekciok meretenek kiirasa
18
//                      !!eddig nem tud futni a sim
19
//              a sorszamok helyes szamolasa
20
 
21
#include 
22
#include //strcmp(),strcpy()
23
#include //atoll()
24
 
25
//Line number
26
int line=0;
27
//Version of the core
28
int version=1;;
29
//Instruction's names
30
#define MAXCMD 16
31
char *cmds[MAXCMD+1]={"coA","coB","add","sub","mvA","mvB","shl","shr"
32
                ,"and","orr","xor","jmp","Fmv","mvD","Dmv","mvP",""};
33
char *cmds2[MAXCMD+1]={"mvA","coA","mvB","coB","csB","shl","shr","and"
34
                ,"orr","xor","add","sub","cal","ret","Imv","jmp",""};
35
 
36
char *cmds3[MAXCMD+1]={"mvA","coA","mvB","coB","csB","shl","shr","and"
37
                ,"orr","xor","add","sub","int","ire","Imv","jmp",""};
38
//Register's names
39
#define MAXREG 16
40
char *regs[MAXREG+1]={"reg0","reg1","reg2","reg3","reg4","reg5","reg6","reg7"
41
                ,"reg8","reg9","reg10","reg11","reg12","reg13","reg14","reg15",""};
42
char *regs_d[MAXREG+1]={"$0","$1","$2","$3","$4","$5","$6","$7"
43
                ,"$8","$9","$10","$11","$12","$13","$14","$15",""};
44
char *regs_r[MAXREG+1]={"r0","r1","r2","r3","r4","r5","r6","r7"
45
                ,"r8","r9","r10","r11","r12","r13","r14","r15",""};
46
char *regs_n[MAXREG+1]={"","","","","","","",""
47
                ,"","","","ireA","FLAG","mem","DP","IP",""};//???
48
 
49
//Directive's names
50
#define MAXDIR 12
51
char *dire[MAXDIR+1]={".data",".stack",".text",".core3",".byte",".word",".dword",
52
                ".qword",".ascii",".asciiz",".space",".core2",""};
53
 
54
int dorg=0,torg=0;//Global vars, place actual in the section .data/.text
55
int stackl=0;
56
char datab=0,stackb=0,textb=0;//These directives can be declared only one time
57
    //0:not yet defined,1:defined,2:actual section
58
 
59
struct labstruct{
60
    char name[15];
61
    unsigned int address;
62
    unsigned char label_type;//0:address not defined,1:data label,2:text label
63
};
64
 
65
//Storing the labels
66
struct labstruct lab[100];
67
int maxlab=0;//Current number of labels
68
 
69
//Structure of the binary file:
70
//      address of main function (unsigned int)
71
//      length of stack section (unsigned int)
72
//              ???length of the next section (unsigned int)
73
//      .data section
74
//              //0x0000:the adress of the stack at runtime->deleted
75
//              label's pointers
76
//              initialised datas
77
//              ???length of the next section (unsigned int)
78
//      .text section
79
//              ???length of the next section (unsigned int){0}
80
//      ???chksum???
81
//Structure on execution:
82
//      .text segment
83
//      .data segment:
84
//              address of stack?->deleted
85
//      .stack
86
//Structure of the data section at the runtime
87
//      0x0 address of the stack
88
//      0x1 addresses of the labels (functions, variables, etc)
89
 
90
int wordsize=32;//This is the width of the registers and granulity of the .data
91
 
92
int htu(char *c){
93
    printf("Asm compiler for the nCore project at opencores.org\n");
94
    printf("Published under the GPL.\n");
95
    printf("\tUse:\n");
96
    printf("\t%s infile.asm [outfile]\n",c);
97
    return(0);
98
}
99
 
100
char fgetc_line(FILE *f){
101
    char c=fgetc(f);
102
    if(c=='\n')
103
        line++;//We count the line-number
104
    return c;
105
}
106
 
107
int whitespace(char c){
108
return ((c==' ')|(c=='\t')|(c=='\n')|(c=='\r')|(c==';')
109
        |(c==':'));//For labels
110
}
111
#undef debug
112
//#define debug
113
char readchar(FILE * f){
114
    char c;
115
#ifdef debug
116
    printf("\treadchar");
117
    c=fputc(fgetc_line(f),stdout);
118
#else
119
    c=fgetc_line(f);
120
#endif
121
    if(feof(f))return(-10);
122
    while(whitespace(c)){
123
        if(c==';'){
124
#ifdef debug
125
            while(fputc(fgetc_line(f),stdout)!='\n')
126
#else
127
            while(fgetc_line(f)!='\n')
128
#endif
129
                    if(feof(f))return(-10);
130
                    //line++;
131
                    }
132
#ifdef debug
133
        c=fputc(fgetc_line(f),stdout);
134
#else
135
        c=fgetc_line(f);
136
#endif
137
        if(feof(f))return(-10);
138
        }
139
#ifdef debug
140
    fputc('\n',stdout);
141
#endif
142
//    if(c=='\n')
143
        //line++;
144
    return(c);
145
}
146
 
147
int newlabel(char * s){//Add the label name s to the list of labels(w/o address)
148
    int i=0;
149
    while(((i
150
        i++;
151
    if(i
152
        return(-1);//The label already exist, we can't overwrite it
153
    strcpy(lab[maxlab].name,s);
154
    lab[maxlab].address=0;
155
    lab[maxlab].label_type=0;//'Not yet used' label
156
    maxlab++;
157
    return(i);//i=maxlab-1
158
}
159
 
160
int findlabel(char * s){//Search the label s in the label's list
161
    int i=0;
162
    while(((i
163
        i++;
164
    if(i==maxlab)
165
        return(-1);//The label not exist
166
    return(i);
167
}
168
 
169
//#define debug
170
int addlabel(char * s){//Add the address to the existing label s
171
    int i=0;
172
    while(((i
173
        i++;
174
    if(datab==2){
175
        lab[i].address=dorg;
176
        lab[i].label_type=1;//Label under use, pointing to data
177
        return(i);
178
        }
179
    else if(textb==2){
180
        lab[i].address=torg;
181
        lab[i].label_type=2;//Label under use, pointing to function
182
#ifdef debug
183
        printf("Marked label at %d:%d\n",torg,lab[i].address);
184
#endif
185
        return(i);
186
        }
187
    else return(-2);//We can't define a label out of the .data/.text sections
188
}
189
 
190
#undef debug
191
//#define debug
192
int readreg(FILE * f){//visszateres: 0< regiszter, <0 cimke, 
193
    char cmd[31];
194
    int i=0;
195
    if(feof(f))return(-10);
196
    cmd[0]=readchar(f);
197
    while(!whitespace(cmd[i])){
198
        i++;
199
        cmd[i]=fgetc_line(f);
200
    if(feof(f))return(-10);
201
    }
202
    cmd[i]=0;
203
#ifdef debug
204
    printf("\treg:%s\n",cmd);
205
#endif
206
    i=0;
207
    while((strcmp(cmd,regs[i])!=0)&(i
208
#ifdef debug
209
        printf("%s:%s\n",cmd,regs[i]);
210
#endif
211
        i++;}
212
    if(i
213
#ifdef debug
214
        printf("%s\n",regs[i]);
215
#endif
216
        return(i);
217
    }
218
//Not in the regs set
219
    i=0;
220
    while((strcmp(cmd,regs_d[i])!=0)&(i
221
        i++;}
222
    if(i
223
#ifdef debug
224
        printf("%s\n",regs_d[i]);
225
#endif
226
        return(i);
227
    }
228
//Not in the regs_d set
229
    i=0;
230
    while((strcmp(cmd,regs_r[i])!=0)&(i
231
        i++;}
232
    if(i
233
#ifdef debug
234
        printf("%s\n",regs_r[i]);
235
#endif
236
        return(i);
237
    }
238
//Not in the regs_r set
239
    i=0;
240
    while((strcmp(cmd,regs_n[i])!=0)&(i
241
        i++;}
242
    if(i
243
#ifdef debug
244
        printf("%s\n",regs_n[i]);
245
#endif
246
        return(i);
247
    }
248
if(version>1){//We can use a label as param only after version 2 of the core
249
#ifdef debug
250
        printf("Unknown register:%s, searching in labels...",cmd);
251
#endif
252
    i=findlabel(cmd);
253
    if(i==-1){
254
        i=newlabel(cmd);
255
#ifdef debug
256
        printf("created.\n");
257
#endif
258
        }
259
#ifdef debug
260
    else
261
        printf("found.\n");
262
#endif
263
    return (-i-1);
264
}
265
        printf("Unknown register:%s, at line %d\n",cmd,line);
266
        return(-maxlab-10);//Unknown register/label
267
}
268
 
269
#undef debug
270
int direrr(char * dire){
271
    printf("%s was declared more than once.\nLine:%d",dire,line);
272
    return(-1);
273
}
274
#undef debug
275
//#define debug
276
int getvalue(FILE *f,FILE * d,int w){//Recognizing dec,hex,oct,bin numbers!!!,
277
                        //and writing to .data
278
    char s[15];//TODO!!!!
279
    int i=0;//No of readed numbers
280
    long long j=0;
281
    int l;//writed bytes
282
    int ch;
283
    char c;//the next character in f
284
    int k;//altalanos ciklusvaltozo
285
    c=fgetc_line(f);
286
#ifdef debug
287
    printf("Readed0:%c",c);
288
#endif
289
    if(feof(f))
290
        return(-1);//Early file-end
291
    while(!feof(f)){
292
        while(((c==' ')|(c=='\t'))&(!feof(f))){
293
            c=fgetc_line(f);
294
#ifdef debug
295
            printf("Readed1:%c",c);
296
#endif
297
            }
298
#ifdef debug
299
        printf("\t First readed char:'%c',%d\n",c,c);
300
#endif
301
        if(feof(f))
302
            return(-1);//Early file-end
303
        if(((c>'9')|(c<'0'))&((c!='\'')&(c!='\"')&(c!=',')&(c!=' ')&(c!='\t'))){
304
            printf("Need number after this directive\nLine:%d",line);
305
            return(-1);//Early end-of-line or not well-formatted
306
            }
307
        switch(c){
308
            case '\''://This will be a character
309
                c=fgetc_line(f);
310
#ifdef debug
311
                printf("Readed2:%c",c);
312
#endif
313
                if(feof(f))
314
                    return(-1);//Early file-end
315
                if((ch=fgetc_line(f))!='\'')//Not well-coded
316
                    return(-1);
317
#ifdef debug
318
                printf("readed3:%c",ch);
319
#endif
320
                fputc(c,d);//Writing out the data
321
#ifdef debug
322
                printf("writed:'%c'",c);
323
#endif
324
                i++;//We have one more data
325
                c=fgetc_line(f);
326
                break;
327
            case '\"'://This will be a string
328
                c=fgetc_line(f);
329
#ifdef debug
330
                printf("readed4:%c",c);
331
#endif
332
                if(feof(f))
333
                    return(-1);//Early file-end
334
                while((c!='\"')&(c!='\n')&(c!='\r')&(!feof(f))){//Until the end
335
                    if((c=='\n')|(c=='\r'))
336
                        return(-1);//Not well-coded
337
                    if((c!='\"')){
338
                        fputc(c,d);
339
#ifdef debug
340
                        printf("writed:'%c'",c);
341
#endif
342
                        i++;
343
                        c=fgetc_line(f);
344
#ifdef debug
345
                        printf("readed5:%c",c);
346
#endif
347
                        if(feof(f))
348
                            return(-1);//Early file-end
349
                        }
350
                    }
351
                c=fgetc_line(f);
352
                break;
353
            case ';'://This is a comment? Not well-coded?
354
            case '\n'://This is the end of the line
355
            case '\r'://This is the end of the line
356
                goto end_of_function;//:-S
357
//              return(i);
358
                break;
359
            default://This is possibly a number
360
                j=0;
361
                s[0]=c;
362
                while(((!whitespace(c))&(c!=','))&(!feof(f))){
363
                    j++;
364
                    c=fgetc_line(f);
365
                    s[j]=c;
366
#ifdef debug
367
                    printf("readed6:'%c'",c);
368
#endif
369
                    }
370
                if(feof(f))
371
                    return(-1);//Early end of file
372
                s[j]='\0';
373
                j=atoll(s);
374
#ifdef debug
375
                    printf("\nreaded6 number:\"%lld\",'%s', till now:%d\n",j,s,i);
376
#endif
377
 
378
                do{//little endian
379
                    ch=j%256;
380
                    fputc(ch,d);
381
#ifdef debug
382
                    printf("writed:'%d',bytes:%d",ch,i);
383
#endif
384
                    i++;
385
                    j=j/256;
386
                }while(j!=0);
387
        }
388
        while((whitespace(c))&((c!='\n')&(c!='\r'))&(!feof(f)))
389
            c=fgetc_line(f);
390
#ifdef debug
391
            printf("Readed7:'%c'",c);
392
#endif
393
        switch(c){
394
            case ';'://This is comment? Not well-coded?
395
            case '\n':
396
            case '\r'://End of line..
397
//              line++;
398
                goto end_of_function;//:-S
399
//              return(i);
400
                break;
401
            case ' '://Next item
402
            case '\t'://Next item
403
            case ','://Next item
404
                c=fgetc_line(f);
405
#ifdef debug
406
                printf("%c",c);
407
#endif
408
                break;
409
            default:
410
                return(-1);//Something not well...
411
        }
412
    }
413
 
414
    return(-1);
415
    end_of_function:
416
//    while(i%(wordsize/8)!=0){
417
//    while(i%w!=0){
418
    while(i%4!=0){
419
        fputc(0,d);
420
        i++;
421
        }
422
 
423
    return i;
424
}
425
 
426
#undef debug
427
//#define debug
428
long readconst(FILE * f){//Recognizing dec,hex,oct,bin numbers!!!,
429
                        //and writing to .data
430
    int i;//Var for reading the string
431
    long j=-1;//The readed consant
432
    char s[15];//TODO!!!!
433
    int ch;
434
    char c;//the next character in f
435
    int k;//altalanos ciklusvaltozo
436
    c=fgetc_line(f);
437
#ifdef debug
438
    printf("Readed0:%c",c);
439
#endif
440
    if(feof(f))
441
        return(-1);//Early file-end
442
//    while(!feof(f)){
443
        while(((c==' ')|(c=='\t'))&(!feof(f))){
444
            c=fgetc_line(f);
445
#ifdef debug
446
            printf("Readed1:%c",c);
447
#endif
448
            }
449
#ifdef debug
450
        printf("\t First readed char:'%c',%d\n",c,c);
451
#endif
452
        if(feof(f))
453
            return(-1);//Early file-end
454
        if(((c>'9')|(c<'0'))&((c!='\'')&(c!='\"')&(c!=','))){
455
            printf("Need number after this directive\nLine:%d",line);
456
            return(-1);//Early end-of-line or not well-formatted
457
            }
458
        switch(c){
459
            case ';'://This is a comment? Not well-coded?
460
            case '\n'://This is the end of the line
461
            case '\r'://This is the end of the line
462
                goto end_of_function;//:-S
463
//              return(i);
464
                break;
465
            default://This is possibly a number
466
                i=0;
467
                s[0]=c;
468
                while(((!whitespace(c))&(c!=','))&(!feof(f))){
469
                    i++;
470
                    c=fgetc_line(f);
471
                    s[i]=c;
472
#ifdef debug
473
                    printf("readed6:'%c'",c);
474
#endif
475
                    }
476
                if(feof(f))
477
                    return(-1);//Early end of file
478
                s[i]='\0';
479
                j=atoll(s);
480
#ifdef debug
481
                    printf("\nreaded6 number:\"%ld\",'%s'\n",j,s);
482
#endif
483
 
484
        }
485
//    }
486
    end_of_function:
487
    return j;
488
}
489
#undef debug
490
//#define debug
491
int directive(char * s,FILE * f,FILE * d){
492
    int i=0,j=0,k,l;
493
    while((strcmp(s,dire[i])!=0)&(i
494
#ifdef debug
495
//      printf("%s:%s\n",s,dire[i]);
496
#endif
497
        i++;}
498
    if(i==MAXDIR){
499
        printf("Unknown directive:%s\nLine:%d",s,line);
500
        return(-5);//Unknown instruction
501
        }
502
#ifdef debug
503
    printf("Directive recognized:\n\t%s,%d\nLine:%d",dire[i],i,line);
504
#endif
505
    switch(i){
506
        case 0://.data
507
                if(stackb==2)
508
                    stackb=1;
509
                if(textb==2)
510
                    textb=1;
511
                datab=2;
512
            break;
513
        case 1://.stack
514
            if(stackb){
515
                return(direrr(s));
516
            }
517
            else{
518
                if(datab==2)
519
                    datab=1;
520
                if(textb==2)
521
                    textb=1;
522
                stackb=2;
523
                if(!(stackl=readconst(f))){
524
                    printf("Not found size after directive .stack\n");
525
                    return(-1);
526
                    }
527
            }
528
            break;
529
        case 2://.text
530
                if(stackb==2)
531
                    stackb=1;
532
                if(datab==2)
533
                    datab=1;
534
                textb=2;
535
            break;
536
        case 3://.core3 we are working with the 3d version
537
            version=3;
538
            break;
539
        case 4://.byte
540
            if(datab==2){
541
                j=getvalue(f,d,1);
542
                dorg+=j*8/(wordsize);
543
//              dorg+=(j*8)/wordsize;
544
//              if((j*8)%wordsize!=0)
545
//                  dorg++;
546
                }
547
            else j=-5;
548
            break;
549
        case 5://.word
550
            if(datab==2){
551
                j=getvalue(f,d,2);
552
                dorg+=j*16/(wordsize);
553
//              dorg+=(j*16)/wordsize;
554
//              if((j*16)%wordsize!=0)
555
//                  dorg++;
556
                }
557
            else j=-5;
558
            break;
559
        case 6://.dword
560
            if(datab==2){
561
                j=getvalue(f,d,4);
562
printf("\t%d\n",j);
563
                dorg+=j*32/(wordsize);
564
//              dorg+=(j*32)/wordsize;
565
//              if((j*32)%wordsize!=0)
566
//                  dorg++;
567
                }
568
            else j=-5;
569
            break;
570
        case 7://.qword
571
            if(datab==2){
572
                j=getvalue(f,d,8);
573
printf("\t%d\n",j);
574
                dorg+=j*64/(wordsize);
575
//              dorg+=(j*64)/wordsize;
576
                }
577
            else j=-5;
578
            break;
579
        case 8://.ascii we will using one character per word
580
            if(datab==2){
581
                dorg+=getvalue(f,d,1)*8/(wordsize);
582
                }
583
            else j=-5;
584
            break;
585
        case 9://.asciiz we will using one character per word
586
            if(datab==2){//!!bug, end of the string&dorg
587
                dorg+=(getvalue(f,d,1)+1)*8/(wordsize);
588
                for(k=0;k<(wordsize/8.0);k++)
589
                    fputc('0',d);
590
                //dorg++;
591
                }
592
            else j=-5;
593
            break;
594
        case 10://.space in byte
595
            if(datab==2){
596
                j=readconst(f);
597
                for(k=0;k
598
                    fputc('0',d);
599
                dorg+=(j*8)/wordsize;
600
                if((j*8)%wordsize!=0)
601
                    dorg++;
602
                while((j*8)%wordsize!=0){
603
                    fputc(0,d);
604
                    j++;
605
                    }
606
                }
607
            else j=-5;//Not in the data section
608
            break;
609
        case 11://.core2 we are working with the 2nd version
610
            version=2;
611
            break;
612
        default:
613
            return(-5);//Something error
614
    }
615
    if(j<0){
616
#ifdef debug
617
        printf("Wrong place of data.\nLine:%d",line);
618
#endif
619
        return(j);
620
    }
621
    return(i);
622
}
623
 
624
#undef debug
625
 
626
//#define debug
627
int readcmd(FILE *f,FILE *d,FILE *t){
628
    char cmd[31];//TODO!!
629
    int j;//The register part
630
    int i=0;//The instruction part
631
#ifdef debugg
632
printf("\t\ttorg:%d,dorg:%d\n",torg,dorg);
633
#endif
634
 
635
    if(feof(f))return(-10);
636
    cmd[0]=readchar(f);
637
    while(!whitespace(cmd[i])){
638
        i++;
639
        cmd[i]=fgetc_line(f);
640
    if(feof(f))return(-10);
641
    }
642
    if(cmd[i]!=':')
643
        cmd[i]='\0';
644
    else
645
        cmd[i+1]='\0';
646
    if(cmd[0]=='.'){//This is compiler-directive
647
        if(directive(cmd,f,d)<0){
648
#ifdef debug
649
            printf("Directive:'%s'\n",cmd);
650
#endif
651
            return(-2);
652
            }
653
        return(-1);
654
        }
655
    if(cmd[i]==':'){
656
    cmd[i]=0;
657
#ifdef debug
658
    printf("Label:'%s',at %d\n",cmd,torg);
659
#endif
660
        //Searching for a pre-defined label
661
        i=findlabel(cmd);
662
        if(i<0)
663
            newlabel(cmd);
664
        addlabel(cmd);//Setting the address of the label
665
        return(-3);}//This is label
666
#ifdef debug
667
    printf("\tcmd:'%s'\n",cmd);
668
#endif
669
    i=0;
670
switch(version){
671
case 1:{//version==1
672
    while((strcmp(cmd,cmds[i])!=0)&(i
673
#ifdef debug
674
    printf("%s:%s\n",cmd,cmds[i]);
675
#endif
676
        i++;}
677
    }
678
case 2:{//version==2
679
    while((strcmp(cmd,cmds2[i])!=0)&(i
680
#ifdef debug
681
    printf("%s:%s\n",cmd,cmds2[i]);
682
#endif
683
        i++;}
684
    }
685
case 3:{//version==3
686
    while((strcmp(cmd,cmds3[i])!=0)&(i
687
#ifdef debug
688
    printf("%s:%s\n",cmd,cmds3[i]);
689
#endif
690
        i++;}
691
    }
692
}
693
    if(i==MAXCMD){
694
        printf("Unknown instruction:%s\nLine:%d\n",cmd,line);
695
        return(-5);//Unknown instruction
696
        }
697
#ifdef debug
698
    printf("Instruction recognized:\n\t%s ",cmds[i]);
699
#endif
700
    if(textb!=2){
701
//Only for ver1!!!
702
#ifdef debug
703
        printf("Found instruction not in the text section:%s!\nLine:%d\n",cmds[i],line);
704
#endif
705
        return(-5);
706
    }
707
//      instruction in 'i', register or label in 'j'
708
    //If the instruction is cxx, we need to read the constant
709
if(((version==1)&((i==0) | (i==1)))|((version>2)&((i==1) | (i==3) | (i==4)))){
710
    torg++;
711
    j=readconst(f);
712
    fprintf(t,"%c",(i<<4)+j);
713
#ifdef debug
714
    printf("Writed instruction:%s\n",cmd);
715
#endif
716
    }
717
else {
718
j=readreg(f);;
719
if (j==-1)return -5;//There was an error evaluating the parameter
720
if(version>2){
721
    if(j<0){
722
//#ifdef debug
723
    printf("Found label as parameter at line %d: turning the instruction to macro\n",line);
724
//#endif
725
    }
726
    torg++;
727
//Writing the coded instruction to the .text section
728
    if(j>=0){
729
        fprintf(t,"%c",(i<<4)+j);
730
#ifdef debug
731
    printf("Writed instruction:%s\n",cmd);
732
#endif
733
        }
734
    else{
735
        fprintf(t,"%c",i<<4);//TODO!!! The macros
736
#ifdef debug
737
    printf("Writed instruction:%s\n",cmd);
738
#endif
739
        }
740
}
741
else{
742
    if(j<0)
743
        return(-5);
744
    else
745
        fprintf(t,"%c",(i<<4)+j);
746
}
747
}
748
#ifdef debug
749
    printf("torg:%d,dorg:%d\n",torg,dorg);
750
#endif
751
    return(i);
752
};
753
//Egyszeres forditas:
754
//      a cimkek tablazatat a .data elejere kiirjuk, igy nem valtozik
755
//      a kod a cimkektol fuggoen
756
//      cimkere valo hivatkozaskor nem a cimke cimet olvassuk be, hanem
757
//      a .data elejenek cimehez adjuk hozza a cimke sorszamat, onnan
758
//      olvassuk be a cimke cimet->athelyezhetoek akar futas kozben is!!!
759
//      (feltetel: minden olvasas es iras elott be kell olvasni a cimet,
760
//      valamint/illetve csak kooperativ taszk eseten, mert preemptiv
761
//      eseten a betoltes utan is athelyezodhet a kod-vagy csak bizonyos
762
//      rendszerhivas eseten helyezodjon at)
763
//      ha nem athelyezheto, gyorsabb, mert kevesebb a konstans
764
//Jo lenne az elf formatum:
765
//      kovetelmeny a 32 bit
766
#undef debug
767
#define debug
768
int main(int arg,char **argv){
769
    FILE * f;
770
    FILE * d;//Data section,temporary
771
    FILE * t;//Text section,temporary
772
    FILE * o;//Output file
773
    int cer;
774
    int i;
775
    int m=0;//Is the __main defined?
776
    char chartmp;
777
    if(arg==1){
778
        htu(argv[0]);
779
        return(0);
780
    }
781
 
782
    if(arg>=2){//There's an input file
783
        if(!(f=fopen(argv[1],"ro"))){
784
            printf("Error opening the input file\n");
785
            return(-1);
786
        }
787
    }
788
 
789
    if(arg==3){//There's an output file
790
        if(!(o=fopen(argv[2],"w+"))){
791
            printf("Error opening the output file '%s\n'\n",argv[2]);
792
            return(-1);
793
        }
794
    }else
795
        if(!(o=fopen("out","w+"))){
796
            printf("Error opening the output file 'out'\n");
797
            return(-1);
798
        }
799
 
800
    d=tmpfile();//Temp file to store the informations about the .data section
801
    t=tmpfile();//Temp file to store the informations about the .text section
802
//Init de SP->do at init of the task
803
//newlabel("SP");
804
//datab=2;
805
//addlabel("SP");
806
    while(!feof(f)){
807
#ifdef debug
808
//      printf("readcmd\n");
809
#endif
810
        cer=readcmd(f,d,t);
811
        if(cer==-5){
812
            printf("Unknown instruction or register at line %d\n",line);
813
 
814
            fclose(d);
815
            fclose(t);
816
            fclose(f);
817
            fclose(o);
818
            return(-1);
819
        }
820
        if(cer==-2){
821
            printf("Wrong utilisation of directive at line %d\n",line);
822
 
823
            fclose(t);
824
            fclose(f);
825
            fclose(o);
826
            return(-1);
827
        }
828
 
829
    }
830
//Creating the output file
831
//Writing out the version of the used instruction set
832
        fprintf(o,"%c%c%c%c",(version & 0xFF),
833
                        (version>>8)& 0xFF,
834
                        (version>>16)& 0xFF,
835
                        (version>>24));//Little endian
836
//Address of the __main function, verifing the labels
837
for(i=0;iwhile
838
    if(strcmp(lab[i].name,"__main")==0){
839
#ifdef debug
840
        printf("__main found:%d::%d,%d\n",lab[i].address,(lab[i].address & 0xFF),lab[i].address>>8);
841
#endif
842
//Big or little endian?
843
//      fprintf(o,"%c%c",(lab[i].address & 0xFF),lab[i].address>>8);//Little
844
//      fprintf(o,"%c%c",lab[i].address>>8,(lab[i].address & 255));//Big
845
//      //Big or little endian?32 bit
846
        fprintf(o,"%c%c%c%c",(lab[i].address & 0xFF),
847
                        (lab[i].address>>8)& 0xFF,
848
                        (lab[i].address>>16)& 0xFF,
849
                        (lab[i].address>>24));//Little
850
////    fprintf(o,"%c%c%c%c",lab[i].address>>24,
851
//                      (lab[i].address>>16)& 0xFF,
852
//                      (lab[i].address>>8)& 0xFF,
853
//                      (lab[i].address & 255));//Big
854
        m=1;
855
        }
856
 
857
    if(lab[i].label_type==0){
858
        printf("Error:\nThe following label is not defined but used:%s\n"
859
            ,lab[i].name);
860
        fclose(d);
861
        fclose(t);
862
        fclose(f);
863
        fclose(o);
864
        remove("out");
865
        return(-1);
866
        }
867
#ifdef debug
868
    else{
869
        printf("Label:%s,position at %d\n",lab[i].name,lab[i].address);
870
        }
871
#endif
872
    }
873
 
874
if(m==0)//If there's no main function, we begin at the start of the code
875
//    fprintf(o,"%c%c",0,0);//16 bit address
876
    fprintf(o,"%c%c%c%c",0,0,0,0);//32 bit address
877
 
878
#ifdef debug
879
    printf("Length of the .stack section:%d\n",stackl);
880
#endif
881
//Writing the length of the stack section
882
        fprintf(o,"%c%c%c%c",(stackl & 0xFF),
883
                        (stackl>>8)& 0xFF,
884
                        (stackl>>16)& 0xFF,
885
                        (stackl>>24));//Little endian
886
 
887
//Writing the length of the next (.data) section
888
//That's equ:dorg+no of labels!
889
dorg+=maxlab;
890
#ifdef debug
891
    printf("Length of the .data section:%d\n",dorg);
892
#endif
893
        fprintf(o,"%c%c%c%c",(dorg & 0xFF),
894
                        (dorg>>8)& 0xFF,
895
                        (dorg>>16)& 0xFF,
896
                        (dorg>>24));//Little endian
897
 
898
//Writing out the labels
899
for(i=0;i
900
    if(lab[i].label_type==1)
901
        lab[i].address+=maxlab;
902
//    lab[i].address+=1;//+address of the stack at runtime->deleted, done at ini
903
        //Big or little endian?
904
//      fprintf(o,"%c%c",(lab[i].address & 0xFF),lab[i].address>>8);//Little
905
//      fprintf(o,"%c%c",lab[i].address>>8,(lab[i].address & 255));//Big
906
//      //Big or little endian?32 bit
907
        fprintf(o,"%c%c%c%c",(lab[i].address & 0xFF),
908
                        (lab[i].address>>8)& 0xFF,
909
                        (lab[i].address>>16)& 0xFF,
910
                        (lab[i].address>>24));//Little
911
        printf("%d:%d,%d,%d,%d;%d\n",i,(lab[i].address & 0xFF),
912
                        (lab[i].address>>8)& 0xFF,
913
                        (lab[i].address>>16)& 0xFF,
914
                        (lab[i].address>>24),lab[i].address);//Little
915
////    fprintf(o,"%c%c%c%c",lab[i].address>>24,
916
//                      (lab[i].address>>16)& 0xFF,
917
//                      (lab[i].address>>8)& 0xFF,
918
//                      (lab[i].address & 255));//Big
919
    }
920
 
921
rewind(d);//Writing the rest of the data section
922
while(!feof(d)){
923
    chartmp=fgetc(d);
924
    if(!feof(d))
925
        fputc(chartmp,o);
926
        }
927
#ifdef debug
928
    printf("Length of the .text section:%d\n",torg);
929
#endif
930
//Writing the length of the next (.text) section
931
        fprintf(o,"%c%c%c%c",(torg & 0xFF),
932
                        (torg>>8)& 0xFF,
933
                        (torg>>16)& 0xFF,
934
                        (torg>>24));//Little endian
935
 
936
        printf("%d,%d,%d,%d\n",(torg & 0xFF),
937
                        (torg>>8)& 0xFF,
938
                        (torg>>16)& 0xFF,
939
                        (torg>>24));//Little endian
940
 
941
rewind(t);//Writing the text section
942
while(!feof(t)){
943
    chartmp=fgetc(t);
944
    if(!feof(t))
945
        fputc(chartmp,o);
946
        }
947
//while(!feof(t))->to del
948
//    fputc(fgetc(t),o);
949
 
950
    fclose(d);
951
    fclose(t);
952
    fclose(f);
953
    fclose(o);
954
    return 0;
955
}

powered by: WebSVN 2.1.0

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