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

Subversion Repositories lwrisc

[/] [lwrisc/] [trunk/] [CTOOL/] [hexdasm.c] - Blame information for rev 20

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

Line No. Rev Author Line
1 11 mcupro
/******************************************************************
2
 *                                                                *
3
 *    Author: Liwei                                               *
4
 *                                                                *
5
 *    This file is part of the "ClaiRISC" project,                *
6
 *    The folder in CVS is named as "lwrisc"                      *
7
 *    Downloaded from:                                            *
8
 *    http://www.opencores.org/pdownloads.cgi/list/lwrisc         *
9
 *                                                                *
10
 *    If you encountered any problem, please contact me via       *
11
 *    Email:mcupro@opencores.org  or mcupro@163.com               *
12
 *                                                                *
13
 ******************************************************************/
14
 
15
 
16 7 mcupro
#include "stdlib.h"
17
#include "stdio.h"
18
#include "string.h"
19 15 mcupro
void add_ins(char *ins);
20 7 mcupro
int func1(int in)
21
{
22
const int table[]={1,2,4,8,16,32,64,128,256,512,1024,2048};
23
int i =0;
24
if (in>=2048)return -1;
25
for(;;i++)
26
if ((in>table[i])&&(in<table[i+1])){
27
return i+1;
28
}
29
}
30
 
31
 
32
int getbit(unsigned int data,unsigned int index)
33
{
34
    unsigned int temp=1<<index ;
35
    if(data&temp)
36
    return 1 ;
37
    else
38
    return 0 ;
39
}
40
 
41
char temp[100];
42
char*ins12tostr(unsigned int ins)
43
{
44
    unsigned int i ;
45
    for(i=0;i<100;++i)temp[i]=0 ;
46
    for(i=0;i<=11;++i)
47
    temp[i]=getbit(ins,11-i)+'0' ;
48
    return temp ;
49
}
50
/*static volatile       unsigned char   WB_ADDR         @ 0x01;
51
//static volatile       unsigned char   PCL             @ 0x02;
52
static volatile       unsigned char     STATUS          @ 0x03;
53
static                unsigned char     FSR             @ 0x04;
54
static volatile       unsigned char     WB_DATA         @ 0x05;
55
static volatile       unsigned char     WB_CTL          @ 0x06;*/
56
static char reg_name[30];
57
char*find_reg(unsigned int t)
58 16 mcupro
{/*
59 7 mcupro
    //puts(&ins[7]);
60
    if(t==0)
61
    {
62
        //  printf("t=%d",t);
63
        strcpy(reg_name,"WB_ADDR");
64
        return reg_name ;
65
    }
66
    //    if(reg_no==0)strcpy(reg_name,"WB_CTL");
67
    else if(t==1)
68
    {
69
        strcpy(reg_name,"TMR");
70
        return reg_name ;
71
    }
72
    //    else if(reg_no==1)strcpy(reg_name,"WB_ADDR");
73
    else if(t==2)
74
    {
75
        strcpy(reg_name,"PCL");
76
        return reg_name ;
77
    }
78 16 mcupro
 
79
    else
80
    */ if(t-3==0)
81 7 mcupro
    {
82
        strcpy(reg_name,"STATUS");
83
        return reg_name ;
84 16 mcupro
    }/*
85 7 mcupro
    else if(t==0+4)
86
    {
87
        strcpy(reg_name,"FSR");
88
        return reg_name ;
89
    }
90
    else if(t==5+0)
91
    {
92
        strcpy(reg_name,"WB_DATA");
93
        return reg_name ;
94
    }
95
    //else if(reg_no==5)strcpy(reg_name,"WB_WR_DATA");
96
    else if(t==6+0)
97
    {
98
        strcpy(reg_name,"WB_CTL");
99
        return reg_name ;
100
    }
101
    //else if(reg_no==6)strcpy(reg_name,"WB_RD_DATA");
102
    else if(t==0+1+6)
103
    {
104
        strcpy(reg_name,"PORTC");
105
        return reg_name ;
106
    }
107 16 mcupro
    */
108 7 mcupro
    return NULL ;
109
}
110
 
111
char hex[]=
112
{
113
    "0123456789abcdef"
114
}
115
;
116
char*p,__hex[20]=
117
{
118
 
119
}
120
;
121
unsigned char*bs2hs(char*bs)
122
{
123
    unsigned int t=0,i=0 ;
124
    for(i=0;;++i)
125
    {
126
        if(bs[i]==0)break ;
127
        t=t*2+bs[i]-'0' ;
128
    }
129
    //printf("now t==%d",t);
130
    p=find_reg(t);
131
    //if (t>=8) goto else
132
    if(p!=NULL)
133
    strcpy(__hex,p);
134
    else
135
    {
136
        __hex[0]='0' ;
137
        __hex[1]='x' ;
138
        __hex[2]=hex[t/16];
139
        __hex[3]=hex[t%16];
140
        __hex[4]=0 ;
141
    }
142
    return __hex ;
143
}
144
 
145
char _temp[100];
146
//get file reg adderss from instruction!
147
char*gen_ins_fa(char*ins)
148
{//gen_ins_in(
149
    strcpy(_temp,ins);
150
    return bs2hs(&_temp[7]);
151
}
152
 
153
//get bit data from instruction!
154
unsigned int gen_ins_bd(char*ins)
155
{
156
//1111_1111_1111
157
    unsigned int t=0 ;
158
    if(ins[5+1]=='1')t+=1 ;
159
    if(ins[4+1]=='1')t+=2 ;
160
    if(ins[3+1]=='1')t+=4 ;
161
    return t ;
162
}
163
 
164
//get goto(and call) address data from instruction!
165
unsigned int gen_ins_goto(char*ins)
166
{
167
//000_111111001
168
    unsigned int t=0 ;
169
    if(ins[11-0]=='1')t+=1;
170
    if(ins[11-1]=='1')t+=2 ;
171
    if(ins[11-2]=='1')t+=4;
172
    if(ins[11-3]=='1')t+=8 ;
173
    if(ins[11-4]=='1')t+=16;//1<<4 ;
174
    if(ins[11-5]=='1')t+=32;//1<<5 ;
175
    if(ins[11-6]=='1')t+=64;//1<<6 ;
176
    if(ins[11-7]=='1')t+=128;//1<<7 ;
177 16 mcupro
   // if(ins[11-8]=='1')t+=256;//1<<8 ;
178 7 mcupro
    return t ;
179
}
180
 
181
//get instant data from instruction!
182
unsigned int gen_ins_in(char*ins)
183
{
184
    unsigned int t=0 ;
185
    if(ins[11-0]=='1')t+=1;
186
    if(ins[11-1]=='1')t+=2 ;
187
    if(ins[11-2]=='1')t+=4;//1<<3 ;
188
    if(ins[11-3]=='1')t+=8;//1<<4 ;
189
    if(ins[11-4]=='1')t+=16;//1<<4 ;
190
    if(ins[11-5]=='1')t+=32;//1<<5 ;
191
    if(ins[11-6]=='1')t+=64;//1<<6 ;
192
    if(ins[11-7]=='1')t+=128;//1<<7 ;
193
    // if(ins[11-8]=='1')t+=1<<8 ;
194
    return t ;
195
}
196
 
197
 
198
unsigned int inscmp(char*src,char*dst)
199
{
200
    unsigned int i ;
201
    for(i=0;i<=9;i++)
202
    {
203
        if(src[i]=='X')continue ;
204
        else if(src[i]!=dst[i])return 0 ;
205
    }
206
    return 1 ;
207
}
208
 
209
 
210
unsigned int str2u12(char*str)
211
{
212
    unsigned int ret=0 ;
213
    unsigned int i ;
214
    for(i=0;i<=11;++i)
215
    {
216
        if(str[i]==0)return ret ;
217
        ret=ret*10+str[i]-'0' ;
218
    }
219
    return ret ;
220
}
221
/*
222
unsigned int getfno(char *ins,unsigned char start,
223
unsigned char len)
224
{
225
unsigned int data = str2u12(ins);
226
    unsigned int tmp1=0xffff<<len ;
227
    unsigned int tmp2=data>>start ;
228
    unsigned int ret ;
229
    tmp1=~tmp1 ;
230
    ret=tmp1&tmp2 ;
231
    ret=ret&0xffff ;
232
    return ret ;
233
 
234
unsigned int i,ret=0;
235
for(i=11-start;i<len;++i)
236
ret=ret+(ins[i]-'0')*2;
237
return ret;
238
}*/
239
/*
240
void test_main(void)
241
{
242
    printf("%d",getno(str2u12("0xff"),0,1));
243
    getchar();
244
}*/
245
 
246
 
247
 
248
char tmp_str[100]=
249
{
250
 
251
}
252
;
253
void init_temp_str(void)
254
{
255
    int i ;
256
    for(i=0;i<100;++i)
257
    tmp_str[i]=0 ;
258
}
259
 
260
char*op_ins_NOP(char*ins)
261
{
262
    init_temp_str();
263
    sprintf(tmp_str,"NOP");
264
    add_ins("NOP");
265
    //ok
266
    return tmp_str ;
267
 
268
}
269
char*op_ins_MOVWF(char*ins)
270
{
271
    init_temp_str();
272
    sprintf(tmp_str,"MOVWF %s",gen_ins_fa(ins));
273
    add_ins("MOVWF");
274
    //ok
275
    return tmp_str ;
276
}
277
char*op_ins_CLRW(char*ins)
278
{
279
    init_temp_str();
280
    sprintf(tmp_str,"CLRW");
281
    add_ins("CLRW");
282
    return tmp_str ;
283
}
284
char*op_ins_CLRF(char*ins)
285
{
286
    init_temp_str();
287
    sprintf(tmp_str,"CLRF %s",gen_ins_fa(ins));
288
    add_ins("CLRF");
289
    return tmp_str ;
290
}
291
char*op_ins_SUBWFW(char*ins)
292
{
293
    init_temp_str();
294
    sprintf(tmp_str,"SUBWFW %s",gen_ins_fa(ins));
295
    add_ins("SUBWFW");
296
    return tmp_str ;
297
}
298
char*op_ins_SUBWFF(char*ins)
299
{
300
    init_temp_str();
301
    sprintf(tmp_str,"SUBWFF %s",gen_ins_fa(ins));
302
    add_ins("WUBWFF");
303
    return tmp_str ;
304
}
305
char*op_ins_DECFW(char*ins)
306
{
307
    init_temp_str();
308
    sprintf(tmp_str,"DECFW %s",gen_ins_fa(ins));
309
    add_ins("DECFW");
310
    return tmp_str ;
311
}
312
char*op_ins_DECFF(char*ins)
313
{
314
    init_temp_str();
315
    sprintf(tmp_str,"DECFF %s",gen_ins_fa(ins));
316
    add_ins("DECFF");
317
    return tmp_str ;
318
}
319
char*op_ins_IORWFW(char*ins)
320
{
321
    init_temp_str();
322
    sprintf(tmp_str,"IORWFW %s",gen_ins_fa(ins));
323
    add_ins("IORWFW");
324
    return tmp_str ;
325
}
326
char*op_ins_IORWFF(char*ins)
327
{
328
    init_temp_str();
329
    sprintf(tmp_str,"IORWFF %s",gen_ins_fa(ins));
330
    add_ins("IORWFF");
331
    return tmp_str ;
332
}
333
char*op_ins_ANDWFW(char*ins)
334
{
335
    init_temp_str();
336
    sprintf(tmp_str,"ANDWFW %s",gen_ins_fa(ins));
337
    add_ins("ANDWFW");
338
    return tmp_str ;
339
}
340
char*op_ins_ANDWFF(char*ins)
341
{
342
    init_temp_str();
343
    add_ins("ANDWFF");
344
    sprintf(tmp_str,"ANDWFF %s",gen_ins_fa(ins));
345
    return tmp_str ;
346
}
347
char*op_ins_XORWFW(char*ins)
348
{
349
    init_temp_str();
350
    add_ins("XORWFW");
351
    sprintf(tmp_str,"XORWFW %s",gen_ins_fa(ins));
352
    return tmp_str ;
353
}
354
char*op_ins_XORWFF(char*ins)
355
{
356
    init_temp_str();
357
    add_ins("XORWFF");
358
    sprintf(tmp_str,"XORWFF %s",gen_ins_fa(ins));
359
    return tmp_str ;
360
}
361
char*op_ins_ADDWFW(char*ins)
362
{
363
    init_temp_str();
364
    add_ins("ADDWFW");
365
    sprintf(tmp_str,"ADDWFW %s",gen_ins_fa(ins));
366
    return tmp_str ;
367
}
368
char*op_ins_ADDWFF(char*ins)
369
{
370
    init_temp_str();
371
    add_ins("ADDWFF");
372
    sprintf(tmp_str,"ADDWFF %s",gen_ins_fa(ins));
373
    return tmp_str ;
374
}
375
char*op_ins_MOVFW(char*ins)
376
{
377
    init_temp_str();
378
    add_ins("MOVFW");
379
    sprintf(tmp_str,"MOVFW %s",gen_ins_fa(ins));
380
    return tmp_str ;
381
 
382
 
383
}
384
char*op_ins_MOVFF(char*ins)
385
{
386
    init_temp_str();
387
    add_ins("MOVFF");
388
    sprintf(tmp_str,"MOVFF %s",gen_ins_fa(ins));
389
    return tmp_str ;
390
}
391
char*op_ins_COMFW(char*ins)
392
{
393
    init_temp_str();
394
    add_ins("COMFW");
395
    sprintf(tmp_str,"COMFW %s",gen_ins_fa(ins));
396
    return tmp_str ;
397
}
398
char*op_ins_COMFF(char*ins)
399
{
400
    init_temp_str();
401
    add_ins("COMFF");
402
    sprintf(tmp_str,"COMFF %s",gen_ins_fa(ins));
403
    return tmp_str ;
404
}
405
char*op_ins_INCFW(char*ins)
406
{
407
    init_temp_str();
408
    add_ins("INCFW");
409
    sprintf(tmp_str,"INCFW %s",gen_ins_fa(ins));
410
    return tmp_str ;
411
}
412
char*op_ins_INCFF(char*ins)
413
{
414
    init_temp_str();
415
    add_ins("INCFF");
416
    sprintf(tmp_str,"INCFF %s",gen_ins_fa(ins));
417
    return tmp_str ;
418
}
419
char*op_ins_DECFSZW(char*ins)
420
{
421
    init_temp_str();
422
    add_ins("DECFSZW");
423
    sprintf(tmp_str,"DECFSZW %s",gen_ins_fa(ins));
424
    return tmp_str ;
425
}
426
char*op_ins_DECFSZF(char*ins)
427
{
428
    init_temp_str();
429
    add_ins("DECFSZF");
430
    sprintf(tmp_str,"DECFSZF %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
431
    return tmp_str ;
432
}
433
char*op_ins_RRFW(char*ins)
434
{
435
    init_temp_str();
436
    add_ins("RRFW");
437
    sprintf(tmp_str,"RRFW %s",gen_ins_fa(ins));
438
    return tmp_str ;
439
}
440
char*op_ins_RRFF(char*ins)
441
{
442
    init_temp_str();
443
    add_ins("RRFF");
444
    sprintf(tmp_str,"RRFF%s",gen_ins_fa(ins));
445
    return tmp_str ;
446
}
447
char*op_ins_RLFW(char*ins)
448
{
449
    init_temp_str();
450
    add_ins("RRFW");
451
    sprintf(tmp_str,"RLFW %s",gen_ins_fa(ins));
452
    return tmp_str ;
453
}
454
char*op_ins_RLFF(char*ins)
455
{
456
    init_temp_str();
457
    add_ins("RLFF");
458
    sprintf(tmp_str,"RLFF %s",gen_ins_fa(ins));
459
    return tmp_str ;
460
}
461
char*op_ins_SWAPFW(char*ins)
462
{
463
    init_temp_str();
464
    add_ins("SWAPFW");
465
    sprintf(tmp_str,"SWAPFW %s",gen_ins_fa(ins));
466
    return tmp_str ;
467
}
468
char*op_ins_SWAPFF(char*ins)
469
{
470
    init_temp_str();
471
    add_ins("SWAPFF");
472
    sprintf(tmp_str,"SWAPFF %s",gen_ins_fa(ins));
473
    return tmp_str ;
474
}
475
char*op_ins_INCFSZW(char*ins)
476
{
477
    init_temp_str();
478
    add_ins("INCFZW");
479
    sprintf(tmp_str,"INCFSZW %s",gen_ins_fa(ins));
480
    return tmp_str ;
481
}
482
char*op_ins_INCFSZF(char*ins)
483
{
484
    init_temp_str();
485
    add_ins("INCFSZF");
486
    sprintf(tmp_str,"INCFSZF %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
487
    return tmp_str ;
488
}
489
char*op_ins_BCF(char*ins)
490
{
491
    init_temp_str();
492
    add_ins("BCF");
493
    sprintf(tmp_str,"BCF %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
494
    return tmp_str ;
495
}
496
char*op_ins_BSF(char*ins)
497
{
498
    init_temp_str();
499
    add_ins("BSF");
500
    sprintf(tmp_str,"BSF %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
501
    return tmp_str ;
502
}
503
char*op_ins_BTFSC(char*ins)
504
{
505
    init_temp_str();
506
    add_ins("BTFSC");
507
    sprintf(tmp_str,"BTFSC %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
508
    return tmp_str ;
509
}
510
char*op_ins_BTFSS(char*ins)
511
{
512
    init_temp_str();
513
    add_ins("BTFSS");
514
    sprintf(tmp_str,"BTFSS %s  [%d]",gen_ins_fa(ins),gen_ins_bd(ins));
515
    return tmp_str ;
516
}
517
char*op_ins_RETLW(char*ins)
518
{
519
    init_temp_str();
520
    add_ins("RETLW");
521
    sprintf(tmp_str,"RETLW %d",gen_ins_in(ins));
522
    return tmp_str ;
523
}
524
char*op_ins_CALL(char*ins)
525
{
526
    init_temp_str();
527
    add_ins("CALL");
528
    sprintf(tmp_str,"CALL %d",gen_ins_goto(ins));
529
    return tmp_str ;
530
}
531
char*op_ins_GOTO(char*ins)
532
{
533
    init_temp_str();
534
    add_ins("GOTO");
535
    sprintf(tmp_str,"GOTO %d",gen_ins_goto(ins));
536
    return tmp_str ;
537
}
538
char*op_ins_MOVLW(char*ins)
539
{
540
    init_temp_str();
541
    add_ins("MOVLW");
542
    sprintf(tmp_str,"MOVLW %d",gen_ins_in(ins));
543
    return tmp_str ;
544
}
545
char*op_ins_IORLW(char*ins)
546
{
547
    init_temp_str();
548
    add_ins("IORLW");
549
    sprintf(tmp_str,"IORLW %d",gen_ins_in(ins));
550
    return tmp_str ;
551
}
552
char*op_ins_ANDLW(char*ins)
553
{
554
    init_temp_str();
555
    add_ins("ANDLW");
556
    sprintf(tmp_str,"ANDLW %d",gen_ins_in(ins));
557
    return tmp_str ;
558
}
559
char*op_ins_XORLW(char*ins)
560
{
561
    init_temp_str();
562
    add_ins("XORLW");
563
    sprintf(tmp_str,"XORLW %d",gen_ins_in(ins));
564
    return tmp_str ;
565
}
566
 
567
 
568
char*branch_ins(char*ins)
569
{
570
    if(inscmp("000000000000",ins)==1)
571
    return op_ins_NOP(ins);
572
    // char * op_ins_NOP (char *ins){}
573
    else if(inscmp("0000001XXXXX",ins)==1)
574
    return op_ins_MOVWF(ins);
575
    // char * op_ins_MOVWF (char *ins){}
576
    else if(inscmp("000001000000",ins)==1)
577
    return op_ins_CLRW(ins);
578
    // char * op_ins_CLRW (char *ins){}
579
    else if(inscmp("0000011XXXXX",ins)==1)
580
    return op_ins_CLRF(ins);
581
    // char * op_ins_CLRF (char *ins){}
582
    else if(inscmp("0000100XXXXX",ins)==1)
583
    return op_ins_SUBWFW(ins);
584
    // char * op_ins_SUBWFW (char *ins){}
585
    else if(inscmp("0000101XXXXX",ins)==1)
586
    return op_ins_SUBWFF(ins);
587
    // char * op_ins_SUBWFF (char *ins){}
588
    else if(inscmp("0000110XXXXX",ins)==1)
589
    return op_ins_DECFW(ins);
590
    // char * op_ins_DECFW (char *ins){}
591
    else if(inscmp("0000111XXXXX",ins)==1)
592
    return op_ins_DECFF(ins);
593
    // char *op_ins_DECFF  (char *ins){}
594
    else if(inscmp("0001000XXXXX",ins)==1)
595
    return op_ins_IORWFW(ins);
596
    // char * op_ins_IORWFW (char *ins){}
597
    else if(inscmp("0001001XXXXX",ins)==1)
598
    return op_ins_IORWFF(ins);
599
    // char * op_ins_IORWFF (char *ins){}
600
    else if(inscmp("0001010XXXXX",ins)==1)
601
    return op_ins_ANDWFW(ins);
602
    // char * op_ins_ANDWFW (char *ins){}
603
    else if(inscmp("0001011XXXXX",ins)==1)
604
    return op_ins_ANDWFF(ins);
605
    // char *  op_ins_ANDWFF(char *ins){}
606
    else if(inscmp("0001100XXXXX",ins)==1)
607
    return op_ins_XORWFW(ins);
608
    // char * op_ins_XORWFW (char *ins){}
609
    else if(inscmp("0001101XXXXX",ins)==1)
610
    return op_ins_XORWFF(ins);
611
    // char *  op_ins_XORWFF(char *ins){}
612
    else if(inscmp("0001110XXXXX",ins)==1)
613
    return op_ins_ADDWFW(ins);
614
    // char *   op_ins_ADDWFW(char *ins){}
615
    else if(inscmp("0001111XXXXX",ins)==1)
616
    return op_ins_ADDWFF(ins);
617
    // char *  op_ins_ADDWFF(char *ins){}
618
    else if(inscmp("0010000XXXXX",ins)==1)
619
    return op_ins_MOVFW(ins);
620
    // char * op_ins_MOVFW (char *ins){}
621
    else if(inscmp("0010001XXXXX",ins)==1)
622
    return op_ins_MOVFF(ins);
623
    // char *  op_ins_MOVFF(char *ins){}
624
    else if(inscmp("0010010XXXXX",ins)==1)
625
    return op_ins_COMFW(ins);
626
    // char * op_ins_COMFW (char *ins){}
627
    else if(inscmp("0010011XXXXX",ins)==1)
628
    return op_ins_COMFF(ins);
629
    // char *  op_ins_COMFF(char *ins){}
630
    else if(inscmp("0010100XXXXX",ins)==1)
631
    return op_ins_INCFW(ins);
632
    // char *  op_ins_INCFW(char *ins){}
633
    else if(inscmp("0010101XXXXX",ins)==1)
634
    return op_ins_INCFF(ins);
635
    // char *  op_ins_INCFF(char *ins){}
636
    else if(inscmp("0010110XXXXX",ins)==1)
637
    return op_ins_DECFSZW(ins);
638
    // char *op_ins_DECFSZW  (char *ins){}
639
    else if(inscmp("0010111XXXXX",ins)==1)
640
    return op_ins_DECFSZF(ins);
641
    // char *  op_ins_DECFSZF(char *ins){}
642
    else if(inscmp("0011000XXXXX",ins)==1)
643
    return op_ins_RRFW(ins);
644
    // char * op_ins_RRFW (char *ins){}
645
    else if(inscmp("0011001XXXXX",ins)==1)
646
    return op_ins_RRFF(ins);
647
    // char * op_ins_RRFF (char *ins){}
648
    else if(inscmp("0011010XXXXX",ins)==1)
649
    return op_ins_RLFW(ins);
650
    // char *  op_ins_RLFW(char *ins){}
651
    else if(inscmp("0011011XXXXX",ins)==1)
652
    return op_ins_RLFF(ins);
653
    // char *  op_ins_RLFF(char *ins){}
654
    else if(inscmp("0011100XXXXX",ins)==1)
655
    return op_ins_SWAPFW(ins);
656
    // char * op_ins_SWAPFW (char *ins){}
657
    else if(inscmp("0011101XXXXX",ins)==1)
658
    return op_ins_SWAPFF(ins);
659
    // char *  op_ins_SWAPFF(char *ins){}
660
    else if(inscmp("0011110XXXXX",ins)==1)
661
    return op_ins_INCFSZW(ins);
662
    // char *op_ins_INCFSZW  (char *ins){}
663
    else if(inscmp("0011111XXXXX",ins)==1)
664
    return op_ins_INCFSZF(ins);
665
    // char * op_ins_INCFSZF (char *ins){}
666
    else if(inscmp("0100XXXXXXXX",ins)==1)
667
    return op_ins_BCF(ins);
668
    // char * op_ins_BCF (char *ins){}
669
    else if(inscmp("0101XXXXXXXX",ins)==1)
670
    return op_ins_BSF(ins);
671
    // char *  op_ins_BSF(char *ins){}
672
    else if(inscmp("0110XXXXXXXX",ins)==1)
673
    return op_ins_BTFSC(ins);
674
    // char *  op_ins_BTFSC(char *ins){}
675
    else if(inscmp("0111XXXXXXXX",ins)==1)
676
    return op_ins_BTFSS(ins);
677
    // char * op_ins_BTFSS (char *ins){}
678
    else if(inscmp("1000XXXXXXXX",ins)==1)
679
    return op_ins_RETLW(ins);
680
    // char *  op_ins_RETLW(char *ins){}
681
    else if(inscmp("1001XXXXXXXX",ins)==1)
682
    return op_ins_CALL(ins);
683
    // char * op_ins_CALL (char *ins){}
684
    else if(inscmp("101XXXXXXXXX",ins)==1)
685
    return op_ins_GOTO(ins);
686
    // char *  op_ins_GOTO( (char *ins){}
687
    else if(inscmp("1100XXXXXXXX",ins)==1)
688
    return op_ins_MOVLW(ins);
689
    // char * op_ins_MOVLW (char *ins){}
690
    else if(inscmp("1101XXXXXXXX",ins)==1)
691
    return op_ins_IORLW(ins);
692
    // char *  op_ins_IORLW(char *ins){}
693
    else if(inscmp("1110XXXXXXXX",ins)==1)
694
    return op_ins_ANDLW(ins);
695
    // char * op_ins_ANDLW (char *ins){}
696
    else if(inscmp("1111XXXXXXXX",ins)==1)
697
    return op_ins_XORLW(ins);
698
    // char *  op_ins_XORLW(char *ins){}
699
    return NULL ;
700
}
701
 
702
/*  Input and Output file streams. */
703
FILE*fpi ;
704
 
705
/*  Well.. Let's read stuff in completely before outputting.. Programs */
706
/*  should be pretty small.. */
707
/*  */
708 13 mcupro
#define MAX_MEMORY_SIZE 4096
709 7 mcupro
struct
710
{
711
int nAddress ;
712
     int byData ;
713
    char ins[30];
714
    char dasm[30];
715
}
716
 
717
 
718
Memory[MAX_MEMORY_SIZE];
719
 
720
char szLine[80];
721
unsigned int start_address,address,ndata_bytes,ndata_words ;
722
unsigned int data ;
723
unsigned int nMemoryCount ;
724
char mif_fn[20]=
725
{
726
 
727
}
728
;
729
char fin[20]=
730
{
731
 
732
}
733
;
734
 
735
char ins_tsted[100][10];
736
int index=0;
737
void init1(void){
738
int i,j;
739
    for(i=0;i<100;++i)
740
    for(j=0;j<10;++j)
741
    ins_tsted[i][j]=0;
742
    index=0;
743
}
744
 
745
void add_ins(char *ins){
746
int i;
747
for(i=0;i<=index;++i)
748
if (strcmp(ins_tsted[i],ins)==0) return ;
749
++index;
750
//printf("add string >%s<\n",ins);getchar();
751
strcpy(ins_tsted[index],ins);
752
}
753
 
754
int main(int argc,char*argv[])
755
{
756
 
757
 
758
    int i ;
759
       int addr_wdt,wd_no;
760
    int max=0;
761
   init1();
762
 
763
    if(argc==2)strcpy(fin,argv[1]);
764
    else
765
    {
766
   //     printf("\nThe Synthetic PIC --- Intel HEX File to Altera memory file");
767
   //     printf("\nUsage: hex2mif <infile>");
768
  //      printf("\n");
769
        getchar();
770
        return 0 ;
771
        printf("Input Hex file name:");
772
        scanf("%s",fin);
773
        printf("Input Mif file name:");
774
        scanf("%s",mif_fn);
775
    }
776
 
777
 
778
 
779
    /*  Open input HEX file */
780
    fpi=fopen(argv[1],"r");
781
    if(!fpi)
782
    {
783
        printf("\nCan't open input file %s.\n",argv[1]);
784
        return 1 ;
785
    }
786
 
787
    /*  Read in the HEX file */
788
    /*  */
789
    /*  !! Note, that things are a little strange for us, because the PIC is */
790
    /*     a 12-bit instruction, addresses are 16-bit, and the hex format is */
791
    /*     8-bit oriented!! */
792
    /*  */
793
    nMemoryCount=0 ;
794
    while(!feof(fpi))
795
    {
796
        /*  Get one Intel HEX line */
797
        fgets(szLine,80,fpi);
798
        if(strlen(szLine)>=10)
799
        {
800
            /*  This is the PIC, with its 12-bit "words".  We're interested in these */
801
            /*  words and not the bytes.  Read 4 hex digits at a time for each */
802
            /*  address. */
803
            /*  */
804
            sscanf(&szLine[1],"%2x%4x",&ndata_bytes,&start_address);
805
            if(start_address>=0&&start_address<=20000&&ndata_bytes>0)
806
            {
807
                /*  Suck up data bytes starting at 9th byte. */
808
                i=9 ;
809
 
810
                /*  Words.. not bytes.. */
811
                ndata_words=ndata_bytes/2 ;
812
                start_address=start_address/2 ;
813
 
814
                /*  Spit out all the data that is supposed to be on this line. */
815
                for(address=start_address;address<start_address+ndata_words;address++)
816
                {
817
                    /*  Scan out 4 hex digits for a word.  This will be one address. */
818
                    sscanf(&szLine[i],"%04x",&data);
819
 
820
                    /*  Need to swap bytes... */
821
                    data=((data>>8)&0x00ff)|((data<<8)&0xff00);
822
                    i+=4 ;
823
 
824
                    /*  Store in our memory buffer */
825
                    Memory[nMemoryCount].nAddress=address ;
826
                    Memory[nMemoryCount].byData=data ;
827
                    nMemoryCount++;
828
                }
829
            }
830
        }
831
    }
832
    fclose(fpi);
833
 
834
    /*
835
            for(i=0;;++i)
836
            {
837
                mif_fn[i]=fin[i];
838
                if(mif_fn[i]=='.')
839
                {
840
                    mif_fn[i+1]='m' ;
841
                    mif_fn[i+2]='i' ;
842
                    mif_fn[3+i]='f' ;
843
                    mif_fn[4+i]=0 ;
844
                    break ;
845
                }
846
            }
847
                                  */
848
 
849
    strcpy(mif_fn,"init_file.mif");
850
 
851
    fpi=fopen(mif_fn,"w");
852
    if(NULL==fpi)return ;
853
    /*  Now output the Verilog $readmemh format! */
854
    /*  */
855
 
856
    /*  Now output the Verilog $readmemh format! */
857
    /*  */
858
    fprintf(fpi,"WIDTH=12;\nDEPTH=2048;\n\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\n\nCONTENT BEGIN\n");
859
    printf("WIDTH=8;\nDEPTH=256;\n\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\n\nCONTENT BEGIN\n");
860
    printf("Email:McuPro@163.com\n");
861
 
862
    for(i=0;i<nMemoryCount;i++)
863
    {
864
      //  printf("%X : %X;\n",Memory[i].nAddress,Memory[i].byData);
865
        fprintf(fpi,"%4X : %4X;\n",Memory[i].nAddress,Memory[i].byData);
866
    }
867
 
868 16 mcupro
    fprintf(fpi,"\nEND;\n");
869 7 mcupro
    printf("\END;\n");
870
    close(fpi);
871
    /*output simulate verilog code */
872
    /*
873
            for(i=0;;++i)
874
            {
875
                mif_fn[i]=fin[i];
876
                if(mif_fn[i]=='.')
877
                {
878
                    mif_fn[i+1]='v' ;
879
                    mif_fn[i+2]=0 ;
880
                    break ;
881
                }
882
            }
883
                            */
884
 
885
    strcpy(mif_fn,"sim_rom.v");
886
 
887
    fpi=fopen(mif_fn,"w");
888
    if(NULL==fpi)return ;
889
    fprintf(fpi,"//This file was created by a tool wrietten with C.\n");
890
    fprintf(fpi,"module sim_rom (\n");
891
    fprintf(fpi,"    address,\n");
892
    fprintf(fpi,"    clock,\n");
893
    fprintf(fpi,"    q);\n");
894
    fprintf(fpi,"    input    [10:0]  address;\n");
895
    fprintf(fpi,"    input      clock;\n");
896
    fprintf(fpi,"    output    [11:0]  q;\n");
897
    fprintf(fpi,"    \n");
898
    fprintf(fpi,"reg [10:0]    address_latched;\n");
899
    fprintf(fpi,"// Instantiate the memory array itself.\n");
900
    fprintf(fpi,"reg [11:0]    mem[0:2048-1];\n");
901
    fprintf(fpi,"initial begin \n");
902
    for(i=0;i<nMemoryCount;i++)
903
    {
904
    //    printf("mem[%05d] = s;\n",Memory[i].nAddress,Memory[i].ins);
905
        fprintf(fpi,"mem[%04d] = 12'b%s;\n",Memory[i].nAddress,ins12tostr(Memory[i].byData));
906
        }
907
    fprintf(fpi,"end\n");
908
    fprintf(fpi,"// Latch address\n");
909
    fprintf(fpi,"always @(posedge clock)\n");
910
    fprintf(fpi,"   address_latched <= address;\n");
911
    fprintf(fpi,"   \n");
912
    fprintf(fpi,"// READ\n");
913
    fprintf(fpi,"assign q = mem[address_latched];\n");
914
    fprintf(fpi,"\n");
915
    fprintf(fpi,"endmodule\n");
916
    fprintf(fpi,"\n");
917
    fprintf(fpi,"/*\n");
918
        for(i=0;i<nMemoryCount;i++)
919
    {
920
    //    printf("%04d:%s\n",Memory[i].nAddress,ins12tostr(Memory[i].byData));
921
        //     sprintf(Memory[i].ins,"%s",ins12tostr(Memory[i].byData));
922
        fprintf(fpi,"%04d: %s\n",Memory[i].nAddress,branch_ins(ins12tostr(Memory[i].byData)));
923
        // fprintf(fpi,"mem[%d] = %d;\n",Memory[i].nAddress,Memory[i].byData);
924
    }
925
    fprintf(fpi,"*/\n/*\ncovered instructions:\n");
926
 
927
    for(i=0;i<=index;++i)
928
    {
929
        fprintf(fpi,"%s\n",ins_tsted[i]);
930
    }
931
        fprintf(fpi,"*/\n");
932
    close(fpi);
933
 
934
 
935
    strcpy(mif_fn,"tested_instructions.txt");
936
     fpi=fopen(mif_fn,"w");
937
        for(i=0;i<=index;++i)
938
    {
939
        fprintf(fpi,"%s\n",ins_tsted[i]);
940
    }
941
            close(fpi);
942
 
943
    fpi=fopen(mif_fn,"w");
944
 
945 13 mcupro
    /*output deasm filr*/
946 7 mcupro
    strcpy(mif_fn,"Dasm.txt");
947
    fpi=fopen(mif_fn,"w");
948
    for(i=0;i<nMemoryCount;i++)
949
    {
950
    //   fprintf(fpi,"%04d:%s\n",Memory[i].nAddress,ins12tostr(Memory[i].byData));
951
        //     sprintf(Memory[i].ins,"%s",ins12tostr(Memory[i].byData));
952
        fprintf(fpi,"%04d : %s\n",Memory[i].nAddress,branch_ins(ins12tostr(Memory[i].byData)));
953
        // fprintf(fpi,"mem[%d] = %d;\n",Memory[i].nAddress,Memory[i].byData);
954
    }
955
    close(fpi);
956
    //   getchar();
957
 
958
 
959 16 mcupro
  //  strcpy(mif_fn,"rom_set.h");
960 7 mcupro
 
961 16 mcupro
   // fpi=fopen(mif_fn,"w");
962 7 mcupro
    for(i=0;i<nMemoryCount;i++)
963
    {
964
    if ((Memory[i].nAddress==2047)&&(Memory[i-1].nAddress!=2046))continue;
965
    if ((Memory[i].nAddress==1023)&&(Memory[i-1].nAddress!=1022))continue;
966
    if (Memory[i].nAddress>max) max=Memory[i].nAddress;
967
    //   Memory[i].nAddressfprintf(fpi,"%04d:%s\n",Memory[i].nAddress,ins12tostr(Memory[i].byData));
968
        //     sprintf(Memory[i].ins,"%s",ins12tostr(Memory[i].byData));
969
        //fprintf(fpi,"%04d : %s\n",Memory[i].nAddress,branch_ins(ins12tostr(Memory[i].byData)));
970
        // fprintf(fpi,"mem[%d] = %d;\n",Memory[i].nAddress,Memory[i].byData);
971
       // fprintf(fpi,"mem[%d] = %d;\n",
972
    }
973
    addr_wdt = func1(max);
974 13 mcupro
    //  fprintf(fpi,"`define     ALT_MEM_WIDTHAD   %d\n",addr_wdt);
975
   //   fprintf(fpi,"`define     ALT_MEM_NUMWORDS  %d\n",1<<addr_wdt); 
976
   //   `define      MIF_NAME  %s\n\n","init_file.mif");  
977 16 mcupro
//    fprintf(fpi,"`define    ROM_TYPE  rom%dx12\n\n",1<<func1(max));
978 13 mcupro
 
979 16 mcupro
   // close(fpi);
980 7 mcupro
    //   getchar();
981
}
982
 

powered by: WebSVN 2.1.0

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