-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20131582.c
2254 lines (2148 loc) · 63.2 KB
/
20131582.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "20131582.h"
/*
* Function: initList
*
* allocate and initialize new list.
*
* return: the address of linked list
*/
LinkedList* initList(void){
LinkedList *L = (LinkedList *)malloc(sizeof(LinkedList));
L -> head = NULL;
L -> size = 0;
return L;
}
/*
* Function: appendList
*
* append new element to linked list.
*
* L: the address of linked list
* new_data: the data of node
*/
void appendList(LinkedList* L, void* new_data) {
Node *ptr = NULL;
if (L -> head == NULL) {
L -> head = (Node*)malloc(sizeof(Node));
L -> head -> data = new_data;
L -> head -> link = NULL;
} else {
for (ptr = L -> head; ptr -> link != NULL; ptr = ptr -> link);
ptr -> link = (Node*)malloc(sizeof(Node));
ptr -> link -> data = new_data;
ptr -> link -> link = NULL;
}
(L -> size)++;
}
/*
* Function: printHistory
*
* print each nodes on history linked list.
*
* history: history linked list
*/
void printHistory(LinkedList* history)
{
Node* ptr = NULL;
int cnt = 0;
for (ptr = history -> head; ptr != NULL; ptr = ptr -> link) {
printf("%d\t%s\n", ++cnt, (char*)ptr -> data);
}
}
/*
* Function: printExternalSymbolTable
*
* print each nodes on history linked list.
*
* history: history linked list
*/
void printExternalSymbolTable(LinkedList* estab)
{
Node* ptr = NULL;
int total_length = 0;
printf("control\t\tsymbol\t\taddress\t\tlength\n");
printf("section\t\tname\n");
printf("--------------------------------------------------------------\n");
for (ptr = estab -> head; ptr != NULL; ptr = ptr -> link) {
ExternalSymbol* es_data = ((ExternalSymbol*)(ptr->data));
if(es_data->is_symbol == 1){
// symbol
printf("\t\t%s\t\t%04X\n", es_data->name, es_data->address);
} else {
// control section
printf("%s\t\t\t\t%04X\t\t%04X\n", es_data->name, es_data->address, es_data->length);
total_length += es_data->length;
}
}
printf("--------------------------------------------------------------\n");
printf("\t\t\t\ttotal length\t%04X\n", total_length);
}
/*
* Function: processCmd
*
* process command.
*
* input: the pointer of input string
* return: result of processing
* (0: right command, 1: wrong command, -1: quit)
*/
int processCmd(char* input){
char *cmd = NULL, *tmp_input = NULL, *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *tmp_arg = NULL;
int len = 0, flag = 0, wrong_input_flag = 0;
LinkedList *object_lst = NULL;
if(history == NULL) history = initList();
len = strlen(input);
tmp_input = (char*)malloc(sizeof(char) * len);
strcpy(tmp_input, input);
// trim tmp input
tmp_input[strcspn(tmp_input, "\n")] = '\0';
// flag for input state
cmd = input;
for (int i = 0; i < len; i++) {
if (flag == 0) {
// input cmd
if (input[i] > 96 && input[i] < 123) {
continue;
} else if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
flag++;
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
}
else {
wrong_input_flag = 1;
break;
}
}
if(strcmp(cmd, "loader") == 0){
if (flag == 1){
// empty space
if (input[i] == ' ' || input[i] == '\t') {
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
} else {
arg1 = &input[i];
flag++;
continue;
}
}
else if (flag == 2){
// input arg1
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
flag++;
continue;
} else if (input[i] == '\n'){
input[i] = '\0';
break;
}
}
else if (flag == 3) {
// empty space
if (input[i] == ' ' || input[i] == '\t') {
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
} else {
arg2 = &input[i];
flag++;
continue;
}
}
else if (flag == 4) {
// input arg2
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
flag++;
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
}
}
else if (flag == 5) {
// empty space
if (input[i] == ' ' || input[i] == '\t') {
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
} else {
arg3 = &input[i];
flag++;
continue;
}
}
else if (flag == 6) {
// input arg3
if (input[i] == ' ' || input[i] == '\t' || input[i] == '\n') {
input[i] = '\0';
flag++;
continue;
}
}
} else {
if (flag == 1){
// empty space
if (input[i] == ' ' || input[i] == '\t') {
continue;
}
else {
arg1 = &input[i];
flag++;
continue;
}
}
else if (flag == 2){
// input arg1
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
flag++;
continue;
} else if (input[i] == '\n'){
input[i] = '\0';
break;
} else if (input[i] == ',') {
input[i] = '\0';
flag += 2;
}
}
else if (flag == 3) {
// empty space before comma
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
}
else if (input[i] == ',') {
input[i] = '\0';
flag ++;
continue;
}
else {
wrong_input_flag = 1;
break;
}
}
else if (flag == 4) {
// empty space after comma
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
}
else {
arg2 = &input[i];
flag++;
continue;
}
}
else if (flag == 5) {
// input arg2
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
flag++;
continue;
} else if (input[i] == ',') {
input[i] = '\0';
flag += 2;
continue;
} else if(input[i] == '\n'){
input[i] = '\0';
break;
}
}
else if (flag == 6) {
// empty space before comma
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
}
else if (input[i] == ',') {
input[i] = '\0';
flag++;
continue;
}
else {
wrong_input_flag = 1;
break;
}
}
else if (flag == 7) {
// empty space after comma
if (input[i] == ' ' || input[i] == '\t') {
input[i] = '\0';
}
else {
arg3 = &input[i];
flag++;
continue;
}
}
else if (flag == 8) {
// input arg3
if (input[i] == ' ' || input[i] == '\t' || input[i] == '\n') {
input[i] = '\0';
flag++;
continue;
}
else if (input[i] == ',') {
input[i] = '\0';
flag += 2;
continue;
}
}
}
}
// printf("cmd: %s arg1: %s arg2: %s arg3: %s\n", cmd, arg1, arg2, arg3);
if(wrong_input_flag == 1){
// wrong command
printf("wrong command\n");
printf("if you want to know the commands h[elp]\n");
return 0;
}
else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0) {
// h[elp]
display_help();
}
else if (strcmp(cmd, "d") == 0 || strcmp(cmd, "dir") == 0) {
// d[ir]
display_dir();
}
else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0) {
// q[uit]
printf("quit shell\n");
return -1;
}
else if (strcmp(cmd, "hi") == 0 || strcmp(cmd, "history") == 0) {
// hi[story]
appendList(history, (void*)tmp_input);
printHistory(history);
return 0;
}
else if (strcmp(cmd, "du") == 0 || strcmp(cmd, "dump") == 0) {
if(arg1==NULL){
if(last_addr<=0xFFF60){
// du[mp]
last_addr = hexDump(last_addr);
} else {
// segmentation fault
printf("segmentation fault\n");
last_addr = 0;
}
} else {
int hex_arg1 = (int)strtol(arg1, NULL, 16);
if(arg2==NULL){
// du[mp] start
last_addr = hexDumpWithStart(hex_arg1);
}else{
int hex_arg2 = (int)strtol(arg2, NULL, 16);
if((hex_arg1 <= hex_arg2) && arg3==NULL){
// du[mp] start, end
if(hex_arg2 <= 0xFFFFF){
last_addr = hexDumpWithStartEnd(hex_arg1, hex_arg2);
} else {
printf("segmentation fault\n");
}
}else{
printf("end address is lower than start address\n");
return 1;
}
}
}
} else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0) {
// e[dit] address, data
if(arg1!=NULL && arg2!=NULL){
edit((int)strtol(arg1, NULL, 16), (int)strtol(arg2, NULL, 16));
}
} else if(strcmp(cmd, "f") == 0 || strcmp(cmd, "fill") == 0){
// f[ill] start, end, data
if((arg1!=NULL && arg2!=NULL) && arg3!=NULL){
int hex_arg1 = (int)strtol(arg1, NULL, 16);
int hex_arg2 = (int)strtol(arg2, NULL, 16);
int hex_arg3 = (int)strtol(arg3, NULL, 16);
fill(hex_arg1, hex_arg2, hex_arg3);
}
} else if (strcmp(cmd, "reset") == 0){
// reset
memset(addr, 0, sizeof(addr));
} else if (strcmp(cmd, "opcode") == 0){
if(arg1!=NULL){
// opcode mnemonic
int opcode = get_opcode_by_key((unsigned char*)arg1);
if(opcode != -1){
printf("opcode is %02X\n", get_opcode_by_key((unsigned char*)arg1));
appendList(history, (void*)tmp_input);
return 0;
} else {
printf("Can't find %s in opcodelist\n", arg1);
return 1;
}
}
} else if (strcmp(cmd, "opcodelist") == 0) {
// opcodelist
if(opcodelist() != 0){
printf("Something wrong");
}
} else if (strcmp(cmd, "type") == 0) {
// type filename
if(arg1!=NULL){
type_file(arg1);
}
} else if (strcmp(cmd, "assemble") == 0){
// assemble asm file
if(arg1!=NULL){
if(assemble_file(arg1) == 0){
char* filename = strtok(arg1, ".");
printf("output file : [%s.lst], [%s.obj]", filename, filename);
};
}
} else if (strcmp(cmd, "symbol") == 0){
// assemble asm file
print_symbol_table();
} else if (strcmp(cmd, "progaddr") == 0){
// progaddr [address]
if(arg1 != NULL){
progaddr = (int)strtol(arg1, NULL, 16);
if(safe_progaddr(progaddr) == -1){
printf("the address should be between 0 ~ 2^20\n");
return 1;
} else {
appendList(history, (void*)tmp_input);
return 0;
}
} else {
printf("address is missing\n");
return 1;
}
} else if (strcmp(cmd, "loader") == 0){
// loader [object filename1] [object filename2] [...]
if(safe_progaddr(progaddr) == -1){
printf("the address is between 0 ~ 2^20\n");
return 1;
}
object_lst = initList();
if(arg1 != NULL){
tmp_arg = (char*)calloc(sizeof(char), MAXLEN);
strcpy(tmp_arg, arg1);
appendList(object_lst, (void*)tmp_arg);
}
if(arg2 != NULL){
tmp_arg = (char*)calloc(sizeof(char), MAXLEN);
strcpy(tmp_arg, arg2);
appendList(object_lst, (void*)tmp_arg);
}
if(arg3 != NULL){
tmp_arg = (char*)calloc(sizeof(char), MAXLEN);
strcpy(tmp_arg, arg3);
appendList(object_lst, (void*)tmp_arg);
}
linkingLoader(object_lst);
// type_file(arg1);
// type_file(arg2);
// type_file(arg3);
return 0;
} else if (strcmp(cmd, "run") == 0){
int tmp = run();
if(tmp != -1){
progaddr = tmp;
} else {
return 1;
}
} else if (strcmp(cmd, "bp") == 0){
if(arg1 != NULL){
if(strcmp(arg1, "clear") == 0){
clearBreakPoint();
} else {
addressBreakPoint((int)strtol(arg1, NULL, 16));
}
} else {
printBreakPoint();
}
} else {
// wrong command
printf("wrong command\n");
printf("if you want to know the commands h[elp]\n");
return 1;
}
// create node on history linked list
appendList(history, (void*)tmp_input);
return 0;
}
int check_opcode(int opcode) {
int right_opcode_ary[20] = {0xB4,0x28,0xA0,0x3C,0x30,0x38,0x48,0x00,0x68,0x50,
0x74,0xD8,0x4C,0x0C,0x54,0x14,0x10,0xE0,0xB8,0xDC};
for(int i = 0; i < sizeof(right_opcode_ary) / sizeof(int) ; i++){
if(right_opcode_ary[i] == opcode){
return 1;
}
}
return 0;
}
int compareRegisters(int reg1, int reg2) {
return regi_ary[reg1] < regi_ary[reg2] ? -1 : regi_ary[reg1] == regi_ary[reg2] ? 0 : 1;
}
/*
* Function: safe_progaddr
*
* detect unsafe address reference
*
* addr: address of proaddr
* return: address or error
* (-1: error, address(0 ~ 2^20))
*/
int safe_progaddr(int addr){
if (0 <= addr && addr < (MEMSIZE)) return addr;
return -1;
}
/*
* Function: loadOpcodelist
*
* read opcode.txt load on the hash table.
*
* return: Success
* (0: success, -1: failure)
*/
int loadOpcodelist(void){
// open opcode.txt with readonly.
FILE *fp_opcode = fopen("opcode.txt", "r");
if (!fp_opcode){
printf("Can't read opcode.txt file");
return -1;
}
while (!feof(fp_opcode)) {
// fill out hash table with opcode.txt.
fscanf(fp_opcode, "%x %s %s", &inst_record.opcode, inst_record.mnemonic, inst_record.format);
insert_hash_table(inst_record);
}
// close file stream.
fclose(fp_opcode);
return 0;
}
/*
* Function: checkRegiNum
*
* check if regi num is right
*
* return: Success
* (1: success, 0: failure)
*/
int checkRegiNum(int reg) {
return reg < 10 && 0 <= reg && reg != 7;
}
/*
* Function: getRegiNum
*
* get number of register
*
* return: regi data
*/
int getRegiNum(char *register_name) {
char *reg[] = {"A", "X", "L", "B", "S", "T", "F", "PC", "SW"};
int reg_num[] = {0, 1, 2, 3, 4, 5, 6, 8, 9}, i;
for (i = 0; i < 9; i++)
if (!strcmp(reg[i], register_name)) return reg_num[i];
printf("run: undefined register %s is found.\n", register_name);
return -1;
}
/*
* Function: printRegisters
*
* print registers
*
*/
void printRegisters(int *registers) {
char *reg[] = {"A", "X", "L", "PC", "B", "S", "T"};
for (int i = 0; i < 7; i++){
if(i%2==0 && i != 6){
printf(" %-2s: %06X\t", reg[i], registers[getRegiNum(reg[i])] & 0xFFFFFF);
} else {
printf(" %-2s: %06X\n", reg[i], registers[getRegiNum(reg[i])] & 0xFFFFFF);
}
}
}
/*
* Function: printBreakPoint
*
* print breakpoint
*
* return: Success
* (0: success)
*/
int printBreakPoint(void){
printf("\tbreakpoints\n");
printf("\t-----------\n");
for (int i = 0; i < MEMSIZE; i++) {
if (bp[i])
printf("\t%04X\n", i);
}
return 0;
}
/*
* Function: addressBreakPoint
*
* bp [address]
*
* return: Success
* (0: success)
*/
int addressBreakPoint(int addr){
if (0 > addr || addr >= MEMSIZE){
printf("bp: please input valid hexadecimal number less than 0x100000\n");
}
bp[addr] = 1;
printf("\t[ok] create breakpoint %04X\n", addr);
return 0;
}
/*
* Function: clearBreakPoint
*
* clear breakpoint
*
* return: Success
* (0: success)
*/
int clearBreakPoint(void){
for (int i = 0; i < MEMSIZE; i++){
bp[i] = 0;
}
printf("\t[ok] clear all breakpoints\n");
return 0;
}
/*
* Function: run
*
* run the program that allocated on addresses
*
* return: Success
* (0: success, -1: failure)
*/
int run(void)
{
int is_break_point = 0, opcode = 0, is_extended = 0, reg1 = 0, reg2 = 0, pc_delta = 0, cc = 0, target_value = 0, target_address = 0;
Inst *inst = NULL;
if (last_bp == -1) regi_ary[getRegiNum("L")] = 0xFFFFFF;
PC = progaddr;
while (PC < MEMSIZE && PC >= 0) {
is_break_point = bp[PC];
pc_delta = 0;
opcode = addr[PC] & (~3);
if ((inst = get_inst_by_opcode(opcode)) != NULL && check_opcode(inst -> opcode)) {
if (strcmp(inst->format, "3/4") == 0) {
is_extended = ((addr[PC + 1] & 0xF0) >> 4) & 1;
if ((addr[PC] & 3) == 0) {
pc_delta = 1;
PC += 1;
continue;
} else if (is_extended) {
is_break_point = bp[PC] | bp[PC + 1] | bp[PC + 2] | bp[PC + 3];
BP_FUNC(PC);
target_address =(((unsigned int)addr[PC + 1] & 0x0F) << 16) | (((unsigned int)addr[PC + 2]) << 8) | (((unsigned int)addr[PC + 3]) << 0);
target_address &= 0xFFFFFF;
target_value = (((unsigned int)addr[target_address + 0]) << 16) | (((unsigned int)addr[target_address + 1]) << 8) | (((unsigned int)addr[target_address + 2]) << 0);
if ((addr[PC + 0] & 3) == 1) target_value = target_address;
if ((addr[PC + 0] & 3) == 2) {
target_address = target_value;
if (0 <= target_value && target_value < MEMSIZE)
target_value = (((unsigned int)addr[target_value + 0]) << 16) | (((unsigned int)addr[target_value + 1]) << 8) | (((unsigned int)addr[target_value + 2]) << 0);
}
pc_delta = 4;
} else {
pc_delta = 3;
is_break_point = bp[PC] | bp[PC + 1] | bp[PC + 2];
BP_FUNC(PC);
target_address = (((unsigned int)addr[PC + 1] & 0x0F) << 8) | (((unsigned int)addr[PC + 2]) << 0);
if (((addr[PC + 1] & 0xF0) >> 4) & (1 << 3)) target_address += regi_ary[getRegiNum("X")];
if (((addr[PC + 1] & 0xF0) >> 4) & (1 << 2)) target_address += regi_ary[getRegiNum("B")];
if (((addr[PC + 1] & 0xF0) >> 4) & (1 << 1)) target_address = ((target_address > 0x7FF ? (0xFFF000 | target_address) : target_address) + regi_ary[getRegiNum("PC")] + pc_delta);
target_address &= 0xFFFFFF;
target_value = (((unsigned int)addr[target_address + 0]) << 16) | (((unsigned int)addr[target_address + 1]) << 8) | (((unsigned int)addr[target_address + 2]) << 0);
if ((addr[PC + 0] & 3) == 1) target_value = target_address;
if ((addr[PC + 0] & 3) == 2) {
target_address = target_value;
if (0 <= target_value && target_value < MEMSIZE)
target_value = (((unsigned int)addr[target_value + 0]) << 16) | (((unsigned int)addr[target_value + 1]) << 8) | (((unsigned int)addr[target_value + 2]) << 0);
}
}
} else if (strcmp(inst->format,"2") == 0) {
is_break_point = bp[PC] | bp[PC + 1];
BP_FUNC(PC);
reg1 = (addr[PC + 1] & 0xF0) >> 4;
reg2 = (addr[PC + 1] & 0x0F) >> 0;
if (!strcmp(inst->mnemonic, "CLEAR")) {
if (checkRegiNum(reg1)) {
pc_delta = 2;
} else {
pc_delta = 1;
continue;
}
} else {
if (checkRegiNum(reg1) && checkRegiNum(reg2)) {
pc_delta = 2; // this is valid format 2 instruction.
} else { // invalid operands.
pc_delta = 1;
continue;
}
}
} else {
is_break_point = bp[PC];
BP_FUNC(PC);
pc_delta = 1;
}
BP_FUNC(PC);
switch (opcode) {
case 0xB4://clear(2)
regi_ary[reg1] = 0;
break;
case 0x28://COMP;
reg2 = regi_ary[getRegiNum("X")];
regi_ary[getRegiNum("X")] = target_value;
cc = compareRegisters(getRegiNum("A"), getRegiNum("X"));
regi_ary[getRegiNum("X")] = reg2;
break;
case 0xA0://COMPR(2)
cc = compareRegisters(reg1, reg2);
break;
case 0x3C://J
pc_delta = target_address - PC;
break;
case 0x30://JEQ
if (cc != 0) break;
pc_delta = target_address - PC;
break;
case 0x38://JLT
if (cc != -1) break;
pc_delta = target_address - PC;
break;
case 0x48://JSUB
regi_ary[getRegiNum("L")] = PC + pc_delta;
pc_delta = target_address - PC;
break;
case 0x00://LDA
regi_ary[getRegiNum("A")] = target_value;
break;
case 0x68://LDB
regi_ary[getRegiNum("B")] = target_value;
break;
case 0x50://LDCH
regi_ary[getRegiNum("A")] = (regi_ary[getRegiNum("A")] & 0xFFFF00) | (((target_value & 0xFF0000) >> 16) & 0xFF);
break;
case 0x74://LDT
regi_ary[getRegiNum("T")] = target_value;
break;
case 0xD8://RD
cc = 1;
break;
case 0x4C://RSUB
pc_delta = regi_ary[getRegiNum("L")] - PC;
break;
case 0x0C://STA
addr[target_address + 0] = (regi_ary[getRegiNum("A")] >> 16) & 0xFF;
addr[target_address + 1] = (regi_ary[getRegiNum("A")] >> 8) & 0xFF;
addr[target_address + 2] = (regi_ary[getRegiNum("A")] >> 0) & 0xFF;
break;
case 0x54://STCH
addr[target_address + 0] = (regi_ary[getRegiNum("A")] >> 0) & 0xFF;
break;
case 0x14://STL
addr[target_address + 0] = (regi_ary[getRegiNum("L")] >> 16) & 0xFF;
addr[target_address + 1] = (regi_ary[getRegiNum("L")] >> 8) & 0xFF;
addr[target_address + 2] = (regi_ary[getRegiNum("L")] >> 0) & 0xFF;
break;
case 0x10://STX
addr[target_address + 0] = (regi_ary[getRegiNum("X")] >> 16) & 0xFF;
addr[target_address + 1] = (regi_ary[getRegiNum("X")] >> 8) & 0xFF;
addr[target_address + 2] = (regi_ary[getRegiNum("X")] >> 0) & 0xFF;
break;
case 0xE0://TD
cc = 1;
break;
case 0xB8://TIXR
regi_ary[getRegiNum("X")] += 1;
cc = compareRegisters(getRegiNum("X"), reg1);
break;
case 0xDC://WD
break;
}
} else { // not an instruction.
PC += 1;
continue;
}
PC += pc_delta;
}
last_bp = -1;
printRegisters(regi_ary);
printf("End program.\n");
return PC;
}
/*
* Function: linkingLoader
*
* Link multiple object files and load it
*
* return: Success
* (0: success, -1: failure)
*/
int linkingLoader(LinkedList* object_files){
int sym_addr = 0;
int cur_size = 0;
int cur_addr = progaddr;
int cur_scan_pos = 0;
int line_len = 0;
int cur_obj = 0;
int prog_length[1 << 8] = {};
int start_addr_array[1 << 8] = {};
int sym_addr_array[1 << 8] = {};
int cur_byte = 0;
int rec_start_addr_array = 0;
int rec_length = 0;
FILE *fp = NULL;
char *pstr = NULL, line_buffer[MAXLEN << 1] = "", prog_name[MAXLEN] = "", sym_name[MAXLEN] = "";
Node *ptr = NULL;
LinkedList *symbol_history = initList();
struct ExternalSymbol *ES = NULL;
for (ptr = object_files -> head; ptr != NULL; ptr = ptr -> link){
pstr = (char*)ptr -> data;
start_addr_array[cur_obj] = cur_addr;
if (strncmp(pstr + strlen(pstr) - 3, "obj", 3)){
printf("loader: please open .obj file.\n");
return -1;
}
fp = fopen(pstr, "r");
cur_size = -1;
if (fp == NULL){
printf("loader: cannot open %s.\n", pstr);
return -1;
}
while (fgets(line_buffer, sizeof(line_buffer), fp)){
line_len = strlen(line_buffer);
line_buffer[line_len - 1] = ' ';
if (*line_buffer == 'H') {
ES = (ExternalSymbol *)malloc(sizeof(ExternalSymbol));
sscanf(line_buffer, "H%6s%6x%6x", prog_name, start_addr_array + cur_obj, &cur_size);
start_addr_array[cur_obj] += cur_addr;
prog_length[cur_obj] = cur_size;
strcpy(ES->name, prog_name);
ES->address = cur_addr;
ES->length = cur_size;
ES->is_symbol = 0;
if(insert_external_symbol_table(*ES)){
printf("external symbol already exist");
return -1;
}
appendList(symbol_history, ES);
} else if (*line_buffer == 'D') {
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
return -1;
}
for (cur_scan_pos = 1; cur_scan_pos < line_len; cur_scan_pos += 12) {
ES = (ExternalSymbol *)malloc(sizeof(ExternalSymbol));
sym_name[0] = 0;
if (sscanf(line_buffer + cur_scan_pos, "%6s%6x", sym_name, &sym_addr) != 2) continue;
if (search_element_external_symbol_table((unsigned char*)sym_name) != -1){
printf("loader: repeated symbol %s is found.\n", sym_name);
return -1;
}
strcpy(ES->name, sym_name);
ES->address = sym_addr + cur_addr;
ES->length = 0;
ES->is_symbol = 1;
if(insert_external_symbol_table(*ES)){
printf("external symbol already exist");
return -1;
}
appendList(symbol_history, ES);
}
} else if (*line_buffer == 'R') {
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
} else if (*line_buffer == 'T') {
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
} else if (*line_buffer == 'E') {
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
break;
}
line_buffer[0] = 0;
}
cur_addr += cur_size;
cur_obj++;
fclose(fp);
}
cur_addr = progaddr;
cur_obj = 0;
for (ptr = object_files -> head; ptr != NULL; ptr = ptr -> link) {
pstr = (char*)ptr -> data;
if (strncmp(pstr + strlen(pstr) - 3, "obj", 3)){
printf("loader: please open .obj file.\n");
}
fp = fopen(pstr, "r");
cur_size = -1;
if (fp == NULL){
printf("loader: cannot open %s.\n", pstr);
}
while (fgets(line_buffer, sizeof(line_buffer), fp)) {
line_len = strlen(line_buffer);
if (*line_buffer == 'H') {
// H Record
sscanf(line_buffer + 1, "%6s", prog_name);
sym_addr_array[1] = start_addr_array[cur_obj];
cur_addr = start_addr_array[cur_obj];
cur_size = prog_length[cur_obj];
} else if (*line_buffer == 'R') {
// R Record
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
for (cur_scan_pos = 1; cur_scan_pos < line_len; cur_scan_pos += 8) {
if (sscanf(line_buffer + cur_scan_pos, "%2x%6s", &sym_addr, sym_name) != 2) continue;
if (search_element_external_symbol_table((unsigned char*)sym_name) == -1){
printf("loader: symbol %s is referenced but not found.\n", sym_name);
return -1;
}
sym_addr_array[sym_addr] = search_element_external_symbol_table((unsigned char*)sym_name);
}
} else if (*line_buffer == 'T') {
// T Record
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
sscanf(line_buffer + 1, "%6x%2x", &rec_start_addr_array, &rec_length);
for (cur_scan_pos = 9; cur_scan_pos < line_len; cur_scan_pos+=2) {
cur_byte = 0;
sscanf(line_buffer + cur_scan_pos, "%2x", &cur_byte);
addr[cur_addr + rec_start_addr_array + ((cur_scan_pos - 9) / 2)] = (unsigned char)(cur_byte & 0xFF);
}
} else if (*line_buffer == 'M') {
// M Record
sscanf(line_buffer + 1, "%6x%2x%1s%2x", &rec_start_addr_array, &rec_length, sym_name, &sym_addr);
rec_start_addr_array += cur_addr;
cur_byte = 0;
if (rec_length == 5) {
cur_byte = (((unsigned int)(addr[rec_start_addr_array + 0] & 0x0F)) << 16) | (((unsigned int)(addr[rec_start_addr_array + 1] & 0xFF)) << 8) | (((unsigned int)(addr[rec_start_addr_array + 2] & 0xFF)) << 0);
cur_byte += (*sym_name == '+' ? sym_addr_array[sym_addr] : -sym_addr_array[sym_addr]);
cur_byte &= 0x0FFFFF;
addr[rec_start_addr_array + 0] = (unsigned char)((unsigned int)addr[rec_start_addr_array + 0] & 0xF0) | ((unsigned int)(cur_byte >> 16) & 0x0F);
addr[rec_start_addr_array + 1] = (cur_byte >> 8) & 0xFF;
addr[rec_start_addr_array + 2] = (cur_byte >> 0) & 0xFF;
} else {
cur_byte = (((unsigned int)(addr[rec_start_addr_array + 0] & 0xFF)) << 16) | (((unsigned int)(addr[rec_start_addr_array + 1] & 0xFF)) << 8) | (((unsigned int)(addr[rec_start_addr_array + 2] & 0xFF)) << 0);
cur_byte += (*sym_name == '+' ? sym_addr_array[sym_addr] : -sym_addr_array[sym_addr]);
cur_byte &= 0xFFFFFF;
addr[rec_start_addr_array + 0] = (cur_byte >> 16) & 0xFF;
addr[rec_start_addr_array + 1] = (cur_byte >> 8) & 0xFF;
addr[rec_start_addr_array + 2] = (cur_byte >> 0) & 0xFF;
}
} else if (*line_buffer == 'E') {
// E Record
if (cur_size < 0){
printf("loader: the file is not a valid object file.\n");
}
break;
}
line_buffer[0] = 0;
}
cur_addr += cur_size;
cur_obj++;
fclose(fp);
}
cur_addr = progaddr;
cur_object_file = 0;
for (ptr = object_files -> head; ptr != NULL; ptr = ptr -> link) {
pstr = (char*)ptr -> data;
if (strncmp(pstr + strlen(pstr) - 3, "obj", 3)){
printf("loader: please open .obj file.\n");