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

Subversion Repositories copyblaze

[/] [copyblaze/] [trunk/] [copyblaze/] [sw/] [tools/] [asm/] [xapp387/] [c/] [asm.cpp] - Blame information for rev 3

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

Line No. Rev Author Line
1 2 ameziti
/*******************************************************
2
assembler for picoblaze microcontroller
3
 
4
  v1.0 - developement started 8/5/2002
5
       - predefined instructions are identical with picoblaze VHDL code
6
           - this program parse the assembly code and generates
7
 
8
           - .bin file, program word in hex format
9
           - .fmt file, formated assembly file
10
           - .mcs file, intel mcs-86 format file for programming
11
           - .vhd file, rom vhdl module for simulation
12
           - .log file, program report
13
 
14
*******************************************************/
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
#include <ctype.h>
19
#include <math.h>
20
#include <time.h>
21
 
22
/* declare all instructions */
23
/* same as picoblaze VHDL code */
24
 
25
/* program control group */
26
char *jump_id = "11010";
27
char *call_id = "11011";
28
char *return_id = "10010";
29
 
30
/* logical group */
31
char *load_k_to_x_id = "00000";
32
char *load_y_to_x_id = "01000";
33
char *and_k_to_x_id = "00001";
34
char *and_y_to_x_id = "01001";
35
char *or_k_to_x_id = "00010";
36
char *or_y_to_x_id = "01010";
37
char *xor_k_to_x_id = "00011";
38
char *xor_y_to_x_id = "01011";
39
 
40
/* arithmetic group */
41
char *add_k_to_x_id = "00100";
42
char *add_y_to_x_id = "01100";
43
char *addcy_k_to_x_id = "00101";
44
char *addcy_y_to_x_id = "01101";
45
char *sub_k_to_x_id = "00110";
46
char *sub_y_to_x_id = "01110";
47
char *subcy_k_to_x_id = "00111";
48
char *subcy_y_to_x_id = "01111";
49
 
50
/* shift and rotate */
51
char *shift_rotate_id = "10100";
52
/* shift decoding
53
instruction(3) - shift left/right
54
bit2 bit1 bit0
55
1    1    0    - SR0/SL0; 6
56
1    1    1    - SR1/Sl1; 7
57
 
58
 
59
1    0    0    - RR /RL ; 4
60
*/
61
#define SR0_SL0 6
62
#define SR1_SL1 7
63
#define SRX_SLX 2
64
#define SRA_SLA 0
65
#define RR_RL 4
66
#define SHIFT_RIGHT 8
67
#define SHIFT_LEFT 0
68
 
69
/* flip */ /* added new instruction */
70
char *flip_id = "11111";
71
 
72
 
73
/* input/output group */
74
char *input_p_to_x_id = "10000";
75
char *input_y_to_x_id = "11000";
76
char *output_p_to_x_id = "10001";
77
char *output_y_to_x_id = "11001";
78
 
79
/* interrupt group */
80
 
81
char *interrupt_id = "11110";
82
char *returni_id = "10110";
83
 
84
/* flag */
85
char *zero_id = "00";
86
char *not_zero_id = "01";
87
char *carry_id = "10";
88
char *not_carry_id = "11";
89
 
90
#define MAX_LINE_COUNT 1000 /* max 1000 lines allowed */
91
#define PROGRAM_COUNT 256       /* total program word */
92
 
93
/* increase instruction_count for added new instruction */
94
#define instruction_count 30/* total instruction set */
95
 
96
#define CONSTANT_COUNT 100      /* max 100 constant can be declared */
97
#define REG_COUNT 8                     /* max 8 namereg can be declared */
98
 
99
 
100
char filename[200];
101
FILE *ifp;
102
FILE *ofp;
103
FILE *ffp;
104
 
105
char linebuf[200];
106
int line_count = 0;
107
int constant_count = 0;
108
int reg_count = 0;
109
unsigned program_word[PROGRAM_COUNT]; /* program word array */
110
 
111
typedef struct reg {
112
        char *name;
113
        int value;
114
}reg_t;
115
 
116
reg_t reg_set[REG_COUNT]; /* namereg array */
117
 
118
typedef struct constant {
119
        char *name;
120
        int value;
121
}constant_t;
122
 
123
constant_t constant_set[CONSTANT_COUNT]; /* constant array */
124
 
125
typedef struct opcode {
126
        unsigned int address;
127
        char *label;
128
        char *instruction;
129
        char *op1;
130
        char *op2;
131
        char *comment;
132
}opcode_t;
133
 
134
opcode op[MAX_LINE_COUNT]; /* operaton array to save info for each line */
135
 
136
char *instruction_set[] = {
137
        "JUMP",         /* 0 */
138
        "CALL",         /* 1 */
139
        "RETURN",       /* 2 */
140
        "LOAD",         /* 3 */
141
        "AND",          /* 4 */
142
        "OR",           /* 5 */
143
        "XOR",          /* 6 */
144
        "ADD",          /* 7 */
145
        "ADDCY",        /* 8 */
146
        "SUB",          /* 9 */
147
        "SUBCY",        /* 10 */
148
        "SR0",          /* 11 */
149
        "SR1",          /* 12 */
150
        "SRX",          /* 13 */
151
        "SRA",          /* 14 */
152
        "RR",           /* 15 */
153
        "SL0",          /* 16 */
154
        "SL1",          /* 17 */
155
        "SLX",          /* 18 */
156
        "SLA",          /* 19 */
157
        "RL",           /* 20 */
158
        "INPUT",        /* 21 */
159
        "OUTPUT",       /* 22 */
160
        "RETURNI",      /* 23 */
161
        "ENABLE",       /* 24 */
162
        "DISABLE",  /* 25 */
163
        "CONSTANT",     /* 26 */
164
        "NAMEREG",      /* 27 */
165
        "ADDRESS",      /* 28 */
166
        "FLIP"};        /* 29 */ /* added new instruction */
167
 
168
int error = 0;
169
/*====================================== */
170
void free_mem(void)
171
{
172
        int i;
173
 
174
        for(i = 0; i < line_count; i++){
175
                if(op[i].comment != NULL) free(op[i].comment);
176
                if(op[i].label != NULL) free(op[i].label);
177
                if(op[i].instruction != NULL) free(op[i].instruction);
178
                if(op[i].op1 != NULL) free(op[i].op1);
179
                if(op[i].op2 != NULL) free(op[i].op2);
180
        }
181
}
182
 
183
/*====================================== */
184
void init_program_word(void)
185
{
186
        int i;
187
 
188
        for(i = 0; i < PROGRAM_COUNT; i++)
189
                program_word[i] = 0;
190
}
191
 
192
/*====================================== */
193
void error_out(void)
194
{
195
        free_mem();
196
        exit(1);
197
}
198
 
199
/*====================================== */
200
/* convert hex string to int, return -1 if not valid */
201
int htoi(char *s)
202
{
203
        int i, l, n = 0;
204
        char *p;
205
 
206
        l = strlen(s);
207
        for(i = 0; i < l; i++){
208
                p = s+l-1-i;
209
                if(isdigit(*p) || (*p >= 'A' && *p <= 'F')){
210
                        if(isdigit(*p)) n += (*p - '0') * (int) pow(16 , i);
211
                        else n += (*p - 'A' + 10) * (int) pow(16 , i);
212
                } else return (-1);
213
        }
214
        return(n);
215
}
216
 
217
/*====================================== */
218
/* Only S0 - S7 are valid */
219
int register_number(char *s)
220
{
221
        if(*s != 'S') return( -1 );
222
        if(strlen(s) != 2) return( -1 );
223
        if((*(s+1) >= '0') && (*(s+1) <= '7'))
224
                return (*(s+1) - '0');
225
        else return( -1 );
226
}
227
 
228
/*====================================== */
229
void insert_instruction(char *s, int p)
230
{
231
        int i, l;
232
        unsigned n = 0;
233
 
234
        l = strlen(s);
235
        for(i = 0; i < l; i++)
236
                if(*(s+i) == '1')
237
                        n = n + (unsigned) pow(2, (l-i-1));
238
 
239
        program_word[p] = program_word[p] | (n << 11);
240
}
241
 
242
/*====================================== */
243
void insert_sXX(int c, int p)
244
{
245
        program_word[p] = program_word[p] | (unsigned) (c << 8);
246
}
247
 
248
/*====================================== */
249
void insert_sYY(int c, int p)
250
{
251
        program_word[p] = program_word[p] | (unsigned) (c << 5);
252
}
253
 
254
/*====================================== */
255
void insert_constant(int c, int p)
256
{
257
        program_word[p] = program_word[p] | (unsigned) (c);
258
}
259
 
260
/*====================================== */
261
void insert_flag(int c, int p)
262
{
263
        program_word[p] = program_word[p] | (unsigned) (c << 8);
264
}
265
 
266
/*====================================== */
267
int decode_flag(char *s)
268
{
269
        if(!strcmp(s, "Z")) return (4);
270
        else if (!strcmp(s, "NZ")) return (5);
271
        else if (!strcmp(s, "C")) return (6);
272
        else if (!strcmp(s, "NC")) return (7);
273
        else return (-1);
274
}
275
 
276
/*====================================== */
277
int find_constant(char *s)
278
{
279
        int i;
280
 
281
        for(i = 0; i < constant_count; i++)
282
                if(!strcmp(s, constant_set[i].name))
283
                        return(constant_set[i].value);
284
        return(-1);
285
}
286
 
287
/*====================================== */
288
int find_label(char *s)
289
{
290
        int i;
291
 
292
        for(i = 0; i < line_count; i++)
293
                if(op[i].label != NULL)
294
                        if(!strcmp(s, op[i].label))
295
                                return(op[i].address);
296
        return(-1);
297
}
298
/*====================================== */
299
int find_namereg(char *s)
300
{
301
        int i;
302
 
303
        for(i = 0; i < reg_count; i++)
304
                if(!strcmp(s, reg_set[i].name))
305
                        return(reg_set[i].value);
306
        return(-1);
307
}
308
 
309
/*====================================== */
310
int parse_linebuf(void)
311
{
312
        char *ptr;
313
        char seps[]   = " :;,\t\n";
314
    char *token;
315
 
316
        /* get comment */
317
        if( (ptr = strchr(linebuf, ';')) != NULL ){
318
                op[line_count].comment = strdup(ptr);
319
                *ptr = '\0';
320
                op[line_count].comment[strlen(op[line_count].comment)-1] = '\0';
321
        }
322
 
323
        /* get label */
324
        if( (ptr = strchr(linebuf, ':')) != NULL ){
325
                token = strtok( linebuf, seps );
326
                op[line_count].label = strdup(token);
327
                strupr(op[line_count].label);
328
        }
329
 
330
        /* get instruction */
331
        if (ptr == NULL)
332
                token = strtok( linebuf, seps );
333
        else token = strtok( NULL, seps);
334
        if (token != NULL){
335
                op[line_count].instruction = strdup(token);
336
                strupr(op[line_count].instruction);
337
        } else return (0);
338
 
339
        /* get op1 */
340
        token = strtok( NULL, seps);
341
        if (token != NULL){
342
                op[line_count].op1 = strdup(token);
343
                strupr(op[line_count].op1);
344
        } else return (0);
345
 
346
        /* get op2 */
347
        token = strtok( NULL, seps);
348
        if (token != NULL){
349
                op[line_count].op2 = strdup(token);
350
                strupr(op[line_count].op2);
351
        } else return (0);
352
 
353
        /* make sure nothing left */
354
        token = strtok( NULL, seps);
355
        if (token != NULL){
356
                printf("\nToo many operands in line %d\n", line_count+1);
357
                fprintf(ofp,"\nToo many operands in line %d\n", line_count+1);
358
                error++;
359
        }
360
        return (0);
361
}
362
 
363
/*====================================== */
364
/* syntax test and assign addresses */
365
void test_instructions(void)
366
{
367
        int i, j, k;
368
        int address = 0;
369
 
370
        for(i = 0; i < line_count; i++){
371
                if(op[i].instruction != NULL){
372
                        for(j = 0; j < instruction_count; j++)
373
                                if(!stricmp(op[i].instruction, instruction_set[j]))
374
                                        break;
375
                        if(j >= instruction_count){
376
                                printf("Unknown instruction - %s found on line %d\n",op[i].instruction, i+1);
377
                                fprintf(ofp,"Unknown instruction - %s found on line %d\n",op[i].instruction, i+1);
378
                                error++;
379
                        }
380
                        switch (j)
381
                        {
382
                                case 0: /* JUMP */
383
                                case 1: /* CALL */
384
                                        if(op[i].op2 != NULL){
385
                                                if(decode_flag(op[i].op1) == -1){
386
                                                        printf("ERROR - Invalid operand %s on line %d\n", op[i].op1, i+1);
387
                                                        fprintf(ofp,"ERROR - Invalid operand %s on line %d\n", op[i].op1, i+1);
388
                                                        error++;
389
                                                }
390
                                        } else if(op[i].op1 == NULL){
391
                                                printf("ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
392
                                                fprintf(ofp,"ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
393
                                                error++;
394
                                        }
395
                                        break;
396
                                case 2: /* RETURN */
397
                                        if(op[i].op2 != NULL){
398
                                                printf("ERROR - Too many Operands for %s\n on line %d", op[i].instruction, i+1);
399
                                                fprintf(ofp,"ERROR - Too many Operands for %s\n on line %d", op[i].instruction, i+1);
400
                                                error++;
401
                                        } else if (op[i].op1 != NULL){
402
                                                if(decode_flag(op[i].op1) == -1){
403
                                                        printf("ERROR - Invalid operand %s on line %d\n", op[i].op1, i+1);
404
                                                        fprintf(ofp,"ERROR - Invalid operand %s on line %d\n", op[i].op1, i+1);
405
                                                        error++;
406
                                                }
407
                                        }
408
                                        break;
409
                                case 3: /* LOAD */
410
                                case 4: /* AND */
411
                                case 5: /* OR */
412
                                case 6: /* XOR */
413
                                case 7: /* ADD */
414
                                case 8: /* ADDCY */
415
                                case 9: /* SUB */
416
                                case 10: /* SUBCY */
417
                                        if((op[i].op1 == NULL) || (op[i].op2 == NULL)){
418
                                                printf("ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
419
                                                fprintf(ofp,"ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
420
                                                error++;
421
                                        }
422
                                        break;
423
                                case 11: /* SR0 */
424
                                case 12: /* SR1 */
425
                                case 13: /* SRX */
426
                                case 14: /* SRA */
427
                                case 15: /* RR */
428
                                case 16: /* SL0 */
429
                                case 17: /* SL1 */
430
                                case 18: /* SLX */
431
                                case 19: /* SLA */
432
                                case 20: /* RL */
433
                                case 29: /* FLIP */ /* added new instruction, same syntax with shift/rotate */
434
                                        if(op[i].op2 != NULL){
435
                                                printf("ERROR - Too many Operands for %s on line %d\n", op[i].instruction, i+1);
436
                                                fprintf(ofp,"ERROR - Too many Operands for %s on line %d\n", op[i].instruction, i+1);
437
                                                error++;
438
                                        } else if(op[i].op1 == NULL){
439
                                                printf("ERROR - Missing operand for %s on line %d\n", op[i].instruction, i+1);
440
                                                fprintf(ofp,"ERROR - Missing operand for %s on line %d\n", op[i].instruction, i+1);
441
                                                error++;
442
                                        }
443
                                        break;
444
                                case 21: /* INPUT */
445
                                case 22: /* OUTPUT */
446
                                        if((op[i].op1 == NULL) || (op[i].op2 == NULL)){
447
                                                printf("ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
448
                                                fprintf(ofp,"ERROR - Missing operand for %s on line %d\n",op[i].instruction, i+1);
449
                                                error++;
450
                                        }                                       break;
451
                                case 23: /* RETURNI */
452
                                        if(op[i].op2 != NULL){
453
                                                printf("ERROR - Too many Operands for RETURNI on line %d\n", i+1);
454
                                                fprintf(ofp,"ERROR - Too many Operands for RETURNI on line %d\n", i+1);
455
                                                error++;
456
                                        } else if(op[i].op1 == NULL){
457
                                                printf("ERROR - Missing operand for RETURNI on line %d\n", i+1);
458
                                                fprintf(ofp,"ERROR - Missing operand for RETURNI on line %d\n", i+1);
459
                                                error++;
460
                                        } else if(strcmp(op[i].op1,"ENABLE") && strcmp(op[i].op1,"DISABLE")){
461
                                                printf("ERROR - Invalid operand on line %d, only ENABLE/DISABLE allowed\n", i+1);
462
                                                fprintf(ofp,"ERROR - Invalid operand on line %d, only ENABLE/DISABLE allowed\n", i+1);
463
                                                error++;
464
                                        }
465
                                        break;
466
                                case 24: /* ENABLE */
467
                                case 25: /* DISABLE */
468
                                        if(op[i].op2 != NULL){
469
                                                printf("ERROR - Too many Operands for ENABLE/DISABLE on line %d\n", i+1);
470
                                                fprintf(ofp,"ERROR - Too many Operands for ENABLE/DISABLE on line %d\n", i+1);
471
                                                error++;
472
                                        } else if(op[i].op1 == NULL){
473
                                                printf("ERROR - Missing operand for %s on line %d\n", op[i].instruction, i+1);
474
                                                fprintf(ofp,"ERROR - Missing operand for %s on line %d\n", op[i].instruction, i+1);
475
                                                error++;
476
                                        } else if(strcmp(op[i].op1,"INTERRUPT")){
477
                                                printf("ERROR - Invalid operand on line %d, only INTERRUPT allowed\n", i+1);
478
                                                fprintf(ofp,"ERROR - Invalid operand on line %d, only INTERRUPT allowed\n", i+1);
479
                                                error++;
480
                                        }
481
                                        break;
482
                                case 26: /* CONSTANT */
483
                                        if((op[i].op1 == NULL) || (op[i].op2 == NULL)){
484
                                                printf("ERROR - Missing operand for CONSTANT on line %d\n", i+1);
485
                                                fprintf(ofp,"ERROR - Missing operand for CONSTANT on line %d\n", i+1);
486
                                                error++;
487
                                        } else if(htoi(op[i].op1) != -1) {
488
                                                printf("ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op1, i+1);
489
                                                fprintf(ofp,"ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op1, i+1);
490
                                                error++;
491
                                        } else if(htoi(op[i].op2) == -1){
492
                                                printf("ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op2, i+1);
493
                                                fprintf(ofp,"ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op2, i+1);
494
                                                error++;
495
                                        } else {
496
                                                if(constant_count >= CONSTANT_COUNT){
497
                                                        printf("ERROR - Too many CONSTANT declared\n");
498
                                                        fprintf(ofp,"ERROR - Too many CONSTANT declared\n");
499
                                                        error++;
500
                                                }
501
                                                for(k = 0; k < constant_count; k++)
502
                                                        if(!strcmp(constant_set[k].name, op[i].op1)){
503
                                                                printf("ERROR - Duplicate CONSTANT name %s found\n", op[i].op1);
504
                                                                fprintf(ofp,"ERROR - Duplicate CONSTANT name %s found\n", op[i].op1);
505
                                                                error++;
506
                                                        }
507
                                                constant_set[constant_count].name = op[i].op1;
508
                                                constant_set[constant_count].value = htoi(op[i].op2);
509
                                                if(constant_set[constant_count].value >= PROGRAM_COUNT){
510
                                                        printf("ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op2, i+1);
511
                                                        fprintf(ofp,"ERROR - Invalid operand %s for CONSTANT on line %d\n", op[i].op2, i+1);
512
                                                        error++;
513
                                                }
514
                                                constant_count++;
515
                                        }
516
                                        break;
517
                                case 27: /* NAMEREG */
518
                                        if((op[i].op1 == NULL) || (op[i].op2 == NULL)){
519
                                                printf("ERROR - Missing operand for NAMEREG on line %d\n", i+1);
520
                                                fprintf(ofp,"ERROR - Missing operand for NAMEREG on line %d\n", i+1);
521
                                                error++;
522
                                        } else if(htoi(op[i].op2) != -1){
523
                                                printf("ERROR - Invalid operand %s for NAMEREG on line %d\n", op[i].op2, i+1);
524
                                                fprintf(ofp,"ERROR - Invalid operand %s for NAMEREG on line %d\n", op[i].op2, i+1);
525
                                                error++;
526
                                        } else if(register_number(op[i].op1) == -1){
527
                                                printf("ERROR - Invalid operand %s for NAMEREG on line %d\n", op[i].op1, i+1);
528
                                                fprintf(ofp,"ERROR - Invalid operand %s for NAMEREG on line %d\n", op[i].op1, i+1);
529
                                                error++;
530
                                        } else {
531
                                                if(reg_count >= REG_COUNT){
532
                                                        printf("ERROR - Too many NAMEREG declared\n");
533
                                                        fprintf(ofp,"ERROR - Too many NAMEREG declared\n");
534
                                                        error++;
535
                                                }
536
                                                for(k = 0; k < reg_count; k++)
537
                                                        if(!strcmp(reg_set[k].name, op[i].op2)){
538
                                                                printf("ERROR - Duplicate NAMEREG name %s found\n", op[i].op2);
539
                                                                fprintf(ofp,"ERROR - Duplicate NAMEREG name %s found\n", op[i].op2);
540
                                                                error++;
541
                                                        }
542
                                                reg_set[reg_count].name = op[i].op2;
543
                                                reg_set[reg_count].value = register_number(op[i].op1);
544
                                                reg_count++;
545
                                        }
546
                                        break;
547
                                case 28: /* ADDRESS */
548
                                        //assign op1 to address
549
                                        if(op[i].op2 != NULL){
550
                                                printf("ERROR - Too many Operands for ADDRESS directive on line %d\n", i+1);
551
                                                fprintf(ofp,"ERROR - Too many Operands for ADDRESS directive on line %d\n", i+1);
552
                                                error++;
553
                                        } else if(op[i].op1 == NULL){
554
                                                printf("ERROR - Missing operand for ADDRESS directive on line %d\n", i+1);
555
                                                fprintf(ofp,"ERROR - Missing operand for ADDRESS directive on line %d\n", i+1);
556
                                                error++;
557
                                        } else {
558
                                                address = htoi(op[i].op1);
559
                                                if((address == -1) || (address >= PROGRAM_COUNT)){
560
                                                        printf("ERROR - Invalid ADDRESS directive on line %d\n", i+1);
561
                                                        fprintf(ofp,"ERROR - Invalid ADDRESS directive on line %d\n", i+1);
562
                                                        error++;
563
                                                }
564
                                        }
565
                                        break;
566
                        }
567
                        op[i].address = address;
568
                        /* add (j > 28) for FLIP instruction, - added new instruction */
569
                        if((j < 26) ||(j > 28)) address ++;
570
                } else op[i].address = address; /* This is a comment line*/
571
        }
572
}
573
 
574
/*====================================== */
575
/* parse instructions and write program word */
576
void write_program_word(void)
577
{
578
        int i, j, reg_n;
579
        char *kptr, *sptr;
580
 
581
        for(i = 0; i < line_count; i++){
582
                if(op[i].instruction != NULL){
583
                        for(j = 0; j < instruction_count; j++)
584
                                if(!stricmp(op[i].instruction, instruction_set[j]))
585
                                        break;
586
                        switch (j)
587
                        {
588
                                case 0: /* JUMP */
589
                                case 1: /* CALL */
590
                                        if(j == 0)
591
                                                kptr = jump_id;
592
                                        else
593
                                                kptr = call_id;
594
                                        insert_instruction(kptr, op[i].address);
595
                                        if(op[i].op2 == NULL){
596
                                                if((reg_n = find_label(op[i].op1)) != -1){
597
                                                        insert_constant(reg_n, op[i].address);
598
                                                } else if((reg_n = find_constant(op[i].op1)) != -1){
599
                                                        insert_constant(reg_n, op[i].address);
600
                                                } else if(((reg_n = htoi(op[i].op1)) != -1) && (reg_n < PROGRAM_COUNT)){
601
                                                        insert_constant(reg_n, op[i].address);
602
                                                } else {
603
                                                        printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
604
                                                        fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
605
                                                        error++;
606
                                                }
607
                                        } else {
608
                                                if(op[i].op1 != NULL){
609
                                                        reg_n = decode_flag(op[i].op1);
610
                                                        insert_flag(reg_n, op[i].address);
611
                                                }
612
                                                if((reg_n = find_label(op[i].op2)) != -1){
613
                                                        insert_constant(reg_n, op[i].address);
614
                                                } else if((reg_n = find_constant(op[i].op2)) != -1){
615
                                                        insert_constant(reg_n, op[i].address);
616
                                                } else if(((reg_n = htoi(op[i].op2)) != -1) && (reg_n < PROGRAM_COUNT)){
617
                                                        insert_constant(reg_n, op[i].address);
618
                                                } else {
619
                                                        printf("ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
620
                                                        fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
621
                                                        error++;
622
                                                }
623
                                        }
624
                                        break;
625
                                case 2: /* RETURN */
626
                                        insert_instruction(return_id,op[i].address);
627
                                        if(op[i].op1 != NULL){
628
                                                reg_n = decode_flag(op[i].op1);
629
                                                insert_flag(reg_n, op[i].address);
630
                                        }
631
                                        break;
632
                                case 3: /* LOAD */
633
                                case 4: /* AND */
634
                                case 5: /* OR */
635
                                case 6: /* XOR */
636
                                case 7: /* ADD */
637
                                case 8: /* ADDCY */
638
                                case 9: /* SUB */
639
                                case 10: /* SUBCY */
640
                                        if(j == 3){ kptr = load_k_to_x_id; sptr = load_y_to_x_id;}
641
                                        if(j == 4){ kptr = and_k_to_x_id; sptr = and_y_to_x_id;}
642
                                        if(j == 5){ kptr = or_k_to_x_id; sptr = or_y_to_x_id;}
643
                                        if(j == 6){ kptr = xor_k_to_x_id; sptr = xor_y_to_x_id;}
644
                                        if(j == 7){ kptr = add_k_to_x_id; sptr = add_y_to_x_id;}
645
                                        if(j == 8){ kptr = addcy_k_to_x_id; sptr = addcy_y_to_x_id;}
646
                                        if(j == 9){ kptr = sub_k_to_x_id; sptr = sub_y_to_x_id;}
647
                                        if(j == 10){ kptr = subcy_k_to_x_id; sptr = subcy_y_to_x_id;}
648
                                        if((reg_n = find_namereg(op[i].op1)) != -1)
649
                                                insert_sXX(reg_n, op[i].address);
650
                                        else if((reg_n = register_number(op[i].op1)) != -1)
651
                                                insert_sXX(reg_n, op[i].address);
652
                                        else {
653
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
654
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
655
                                                error++;
656
                                        }
657
                                        if((reg_n = find_constant(op[i].op2)) != -1){
658
                                                insert_constant(reg_n, op[i].address);
659
                                                insert_instruction(kptr,op[i].address);
660
                                        } else if((reg_n = find_namereg(op[i].op2)) != -1){
661
                                                insert_sYY(reg_n, op[i].address);
662
                                                insert_instruction(sptr,op[i].address);
663
                                        } else if((reg_n = register_number(op[i].op2)) != -1){
664
                                                insert_sYY(reg_n, op[i].address);
665
                                                insert_instruction(sptr,op[i].address);
666
                                        } else if(((reg_n = htoi(op[i].op2)) != -1) && (reg_n < PROGRAM_COUNT)){
667
                                                insert_constant(reg_n, op[i].address);
668
                                                insert_instruction(kptr,op[i].address);
669
                                        } else {
670
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
671
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
672
                                                error++;
673
                                        }
674
                                        break;
675
                                case 11: /* SR0 */
676
                                case 12: /* SR1 */
677
                                case 13: /* SRX */
678
                                case 14: /* SRA */
679
                                case 15: /* RR */
680
                                case 16: /* SL0 */
681
                                case 17: /* SL1 */
682
                                case 18: /* SLX */
683
                                case 19: /* SLA */
684
                                case 20: /* RL */
685
                                        insert_instruction(shift_rotate_id,op[i].address);
686
                                        if((reg_n = find_namereg(op[i].op1)) != -1)
687
                                                insert_sXX(reg_n, op[i].address);
688
                                        else if((reg_n = register_number(op[i].op1)) != -1)
689
                                                insert_sXX(reg_n, op[i].address);
690
                                        else {
691
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
692
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
693
                                                error++;
694
                                        }
695
                                        if((j >= 11) && (j <= 15)) reg_n = SHIFT_RIGHT;
696
                                        else reg_n = SHIFT_LEFT;
697
                                        if((j == 11) || (j == 16)) reg_n = reg_n + SR0_SL0;
698
                                        else if((j == 12) || (j == 17)) reg_n = reg_n + SR1_SL1;
699
                                        else if((j == 13) || (j == 18)) reg_n = reg_n + SRX_SLX;
700
                                        else if((j == 14) || (j == 19)) reg_n = reg_n + SRA_SLA;
701
                                        else if((j == 15) || (j == 20)) reg_n = reg_n + RR_RL;
702
                                        insert_constant(reg_n, op[i].address);
703
                                        break;
704
                                case 21: /* INPUT */
705
                                        if((reg_n = find_namereg(op[i].op1)) != -1)
706
                                                insert_sXX(reg_n, op[i].address);
707
                                        else if((reg_n = register_number(op[i].op1)) != -1)
708
                                                insert_sXX(reg_n, op[i].address);
709
                                        else {
710
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
711
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
712
                                                error++;
713
                                        }
714
                                        if((reg_n = find_constant(op[i].op2)) != -1){
715
                                                insert_constant(reg_n, op[i].address);
716
                                                insert_instruction(input_p_to_x_id,op[i].address);
717
                                        } else if((reg_n = find_namereg(op[i].op2)) != -1){
718
                                                insert_sYY(reg_n, op[i].address);
719
                                                insert_instruction(input_y_to_x_id,op[i].address);
720
                                        } else if((reg_n = register_number(op[i].op2)) != -1){
721
                                                insert_sYY(reg_n, op[i].address);
722
                                                insert_instruction(input_y_to_x_id,op[i].address);
723
                                        } else if(((reg_n = htoi(op[i].op2)) != -1) && (reg_n < PROGRAM_COUNT)){
724
                                                insert_constant(reg_n, op[i].address);
725
                                                insert_instruction(input_p_to_x_id,op[i].address);
726
                                        } else {
727
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
728
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
729
                                                error++;
730
                                        }
731
                                        break;
732
                                case 22: /* OUTPUT */
733
                                        if((reg_n = find_namereg(op[i].op1)) != -1)
734
                                                insert_sXX(reg_n, op[i].address);
735
                                        else if((reg_n = register_number(op[i].op1)) != -1)
736
                                                insert_sXX(reg_n, op[i].address);
737
                                        else {
738
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
739
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
740
                                                error++;
741
                                        }
742
                                        if((reg_n = find_constant(op[i].op2)) != -1){
743
                                                insert_constant(reg_n, op[i].address);
744
                                                insert_instruction(output_p_to_x_id,op[i].address);
745
                                        } else if((reg_n = find_namereg(op[i].op2)) != -1){
746
                                                insert_sYY(reg_n, op[i].address);
747
                                                insert_instruction(output_y_to_x_id,op[i].address);
748
                                        } else if((reg_n = register_number(op[i].op2)) != -1){
749
                                                insert_sYY(reg_n, op[i].address);
750
                                                insert_instruction(output_y_to_x_id,op[i].address);
751
                                        } else if(((reg_n = htoi(op[i].op2)) != -1) && (reg_n < PROGRAM_COUNT)){
752
                                                insert_constant(reg_n, op[i].address);
753
                                                insert_instruction(output_p_to_x_id,op[i].address);
754
                                        } else {
755
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
756
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op2, i+1);
757
                                                error++;
758
                                        }
759
                                        break;
760
                                case 23: /* RETURNI */
761
                                        insert_instruction(returni_id, op[i].address);
762
                                        if(!strcmp(op[i].op1, "ENABLE"))
763
                                                program_word[op[i].address]++;
764
                                        break;
765
                                case 24: /* ENABLE */
766
                                case 25: /* DISABLE */
767
                                        insert_instruction(interrupt_id, op[i].address);
768
                                        if(j == 24) /* ENABLE */
769
                                                program_word[op[i].address]++;
770
                                        break;
771
                                case 26: /* CONSTANT */
772
                                case 27: /* NAMEREG */
773
                                case 28: /* ADDRESS */
774
                                        break;
775
                                case 29: /* FLIP */ /* added new instruction */
776
                                        insert_instruction(flip_id, op[i].address);
777
                                        if((reg_n = find_namereg(op[i].op1)) != -1)
778
                                                insert_sXX(reg_n, op[i].address);
779
                                        else if((reg_n = register_number(op[i].op1)) != -1)
780
                                                insert_sXX(reg_n, op[i].address);
781
                                        else {
782
                                                printf("ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
783
                                                fprintf(ofp,"ERROR - Invalid operand %s on line %d\n",op[i].op1, i+1);
784
                                                error++;
785
                                        }
786
                                        break;
787
                        }
788
                }
789
        }
790
}
791
/*====================================== */
792
void write_fmt(void)
793
{
794
        char *ptr;
795
        int i;
796
 
797
        ptr = strstr(filename, ".log");
798
        *ptr = '\0';
799
        strcat(filename,".fmt");
800
 
801
        ffp = fopen(filename, "w");
802
        if (ffp == NULL){
803
        printf("\nCan not open fmt file\n");
804
                fprintf(ofp,"\nCan not open fmt file\n");
805
                error_out();
806
        }
807
 
808
        for(i = 0; i < line_count; i++){
809
                fprintf(ffp, "%3d %3X ", i+1,op[i].address);
810
                if(op[i].label != NULL)
811
                        fprintf(ffp, "%-12s ", op[i].label);
812
                else fprintf(ffp, "%-12s ","");
813
                if(op[i].instruction != NULL)
814
                        fprintf(ffp, ":%-10s ", op[i].instruction);
815
                if(op[i].op1 != NULL)
816
                        fprintf(ffp, "%-12s ", op[i].op1);
817
                if(op[i].op2 != NULL)
818
                        fprintf(ffp, "%-12s ", op[i].op2);
819
                if(op[i].comment != NULL)
820
                        fprintf(ffp, "%s", op[i].comment);
821
                fprintf(ffp,"\n");
822
        }
823
        fclose(ffp);
824
}
825
 
826
/*====================================== */
827
void write_log(void)
828
{
829
        int i;
830
 
831
        for(i = 0; i < line_count; i++){
832
                fprintf(ofp, "ADDRESS : %d\n", op[i].address);
833
                fprintf(ofp, "LABEL : %s\n", op[i].label);
834
                fprintf(ofp, "INSTRUCTION : %s\n", op[i].instruction);
835
                fprintf(ofp, "OP1 : %s\n", op[i].op1);
836
                fprintf(ofp, "OP2 : %s\n", op[i].op2);
837
                fprintf(ofp, "COMMENT : %s\n", op[i].comment);
838
        }
839
}
840
 
841
/*====================================== */
842
/* write program word in hex format */
843
void write_bin(void)
844
{
845
        int i;
846
        char *ptr;
847
 
848
        ptr = strstr(filename, ".fmt");
849
        *ptr = '\0';
850
        strcat(filename,".bin");
851
 
852
        ffp = fopen(filename, "w");
853
        if (ffp == NULL){
854
        printf("\nCan not open bin file\n");
855
                exit(1);
856
        }
857
 
858
        for(i = 0; i < PROGRAM_COUNT; i++){
859
                fprintf(ffp, "%3d : %04X\n", i, program_word[i]);
860
        }
861
 
862
        fclose(ffp);
863
}
864
 
865
/*====================================== */
866
/* write intel mcs file for programming */
867
void write_mcs(void)
868
{
869
        unsigned int i,j,k;
870
        char *ptr;
871
        unsigned int checksum;
872
 
873
        ptr = strstr(filename, ".bin");
874
        *ptr = '\0';
875
        strcat(filename,".mcs");
876
 
877
        ffp = fopen(filename, "w");
878
        if (ffp == NULL){
879
        printf("\nCan not open mcs file\n");
880
                exit(1);
881
        }
882
 
883
        fprintf(ffp, ":020000020000FC\n");
884
        for(i = 0; i < PROGRAM_COUNT/8; i++){
885
                checksum = 0;
886
                fprintf(ffp, ":10%04X00", i*16);
887
                checksum = checksum + 16 + i*16%256 + i*16/256;
888
                for(j = 0; j < 8; j++){
889
                        k = i*8+j;
890
                        fprintf(ffp,"%02X", program_word[k]%256);
891
                        checksum = checksum + program_word[k]%256;
892
                        fprintf(ffp,"%02X", program_word[k]/256);
893
                        checksum = checksum + program_word[k]/256;
894
                }
895
                fprintf(ffp,"%02X\n", (256-checksum%256)%256);
896
        }
897
 
898
        fprintf(ffp, ":00000001FF\n");
899
        fclose(ffp);
900
}
901
 
902
/*====================================== */
903
/* write vhdl module for simulation */
904
void write_vhd(void)
905
{
906
        int i, j;
907
        char *ptr;
908
        char basename[200];
909
 
910
        ptr = strstr(filename, ".mcs");
911
        *ptr = '\0';
912
        strcpy(basename, filename);
913
        strcat(filename,".vhd");
914
 
915
        ffp = fopen(filename, "w");
916
        if (ffp == NULL){
917
        printf("\nCan not open vhd file\n");
918
                exit(1);
919
        }
920
 
921
        fprintf(ffp,"library ieee;\nuse ieee.std_logic_1164.all;\n");
922
        fprintf(ffp,"use ieee.std_logic_unsigned.all;\n\n");
923
 
924
        fprintf(ffp,"entity %s is\n",basename);
925
        fprintf(ffp,"\tport( address : in std_logic_vector(7 downto 0);\n");
926
        fprintf(ffp,"\t\tclk : in std_logic;\n\t\tdout : out std_logic_vector(15 downto 0));\n\tend;\n\n");
927
        fprintf(ffp,"architecture v1 of %s is\n\n", basename);
928
 
929
        fprintf(ffp,"\tconstant ROM_WIDTH: INTEGER:= 16;\n");
930
        fprintf(ffp,"\tconstant ROM_LENGTH: INTEGER:= 256;\n\n");
931
        fprintf(ffp,"\tsubtype rom_word is std_logic_vector(ROM_WIDTH-1 downto 0);\n");
932
        fprintf(ffp,"\ttype rom_table is array (0 to ROM_LENGTH-1) of rom_word;\n\n");
933
        fprintf(ffp,"constant rom: rom_table := rom_table'(\n");
934
        for(i = 0; i < PROGRAM_COUNT-1; i++){
935
                fprintf(ffp, "\t\"");
936
                for(j = 15; j >= 0; j--)
937
                        fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
938
                fprintf(ffp, "\",\n");
939
        }
940
        fprintf(ffp, "\t\"");
941
        for(j = 15; j >= 0; j--)
942
                fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
943
        fprintf(ffp, "\");\n\n");
944
 
945
        fprintf(ffp,"begin\n\nprocess (clk)\nbegin\n", basename);
946
        fprintf(ffp,"\tif clk'event and clk = '1' then\n\t\tdout <= rom(conv_integer(address));\n");
947
        fprintf(ffp,"\tend if;\nend process;\nend v1;\n");
948
 
949
        fclose(ffp);
950
}
951
 
952
/*====================================== */
953
int main(int argc, char **argv)
954
{
955
        char *ptr;
956
 
957
    if(argc != 2){
958
                printf("\nCommand line syntax:\n\nasm file.asm\n");
959
                exit(1);
960
        }
961
 
962
        strcpy(filename, argv[1]);
963
        ptr = strstr(filename, ".asm");
964
        if (ptr == NULL){
965
                printf("\nInvalid file type, use .asm extension\n");
966
                exit(1);
967
        }
968
        *ptr = '\0';
969
        strcat(filename,".log");
970
 
971
    ifp = fopen(argv[1], "r");
972
    if (ifp == NULL){
973
        printf("\nCan not open input file\n");
974
                exit(1);
975
        }
976
 
977
        ofp = fopen(filename, "w");
978
        if (ofp == NULL){
979
        printf("\nCan not open output file\n");
980
                exit(1);
981
        }
982
        fprintf(ofp, "Crypto asm logfile\n\n");
983
 
984
        printf("Reading input file...\n");
985
        fprintf(ofp,"Reading input file...\n");
986
        while ( fgets(linebuf, 128, ifp) != NULL ) {
987
                if(line_count >= MAX_LINE_COUNT){
988
                        printf("\nInput exceed maximum line number\n");
989
                        error_out();
990
                }
991
                parse_linebuf();
992
                line_count++;
993
        }
994
 
995
        init_program_word();
996
        printf("Testing instructions...\n");
997
        fprintf(ofp,"Testing instructions...\n");
998
        test_instructions();
999
        if(error > 0){
1000
                printf("Program aborted...error count %d\n", error);
1001
                fprintf(ofp,"Program aborted...error count %d\n", error);
1002
                error_out();
1003
        }
1004
        write_program_word();
1005
        if(error > 0){
1006
                printf("Program aborted...error count %d\n", error);
1007
                fprintf(ofp,"Program aborted...error count %d\n", error);
1008
                error_out();
1009
        }
1010
 
1011
        printf("Write output files...\n");
1012
        fprintf(ofp,"Write output files...\n");
1013
        write_fmt();
1014
        write_bin();
1015
        write_mcs();
1016
        write_vhd();
1017
 
1018
        free_mem();
1019
        printf("Program completed...\n");
1020
        fprintf(ofp,"Program completed...\n");
1021
        fclose(ifp);
1022
        fclose(ofp);
1023
        return(0);
1024
}
1025
 

powered by: WebSVN 2.1.0

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