forked from louisje/OpenComputer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpTable.c
57 lines (49 loc) · 1.56 KB
/
OpTable.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
#include "OpTable.h"
void OpTableTest() {
debug("=======OpTableTest()==========\n");
OpTableNew();
HashTableEach(opTable, (FuncPtr1) OpPrintln);
OpTableFree();
memCheck();
}
char *opList[] = {"LD 00 L", "ST 01 L", "LDB 02 L", "STB 03 L",
"LDR 04 L", "STR 05 L", "LBR 06 L", "SBR 07 L", "LDI 08 L",
"CMP 10 A", "MOV 12 A", "ADD 13 A", "SUB 14 A", "MUL 15 A",
"DIV 16 A", "AND 18 A", "OR 19 A", "XOR 1A A", "ROL 1C A",
"ROR 1D A", "SHL 1E A", "SHR 1F A", "JEQ 20 J", "JNE 21 J",
"JLT 22 J", "JGT 23 J", "JLE 24 J", "JGE 25 J", "JMP 26 J",
"SWI 2A J", "CALL 2B J", "RET 2C J", "PUSH 30 L", "POP 31 L", "PUSHB 32 L", "POPB 33 L",
"LAND 40 A", "LOR 41 A", "EQ 42 A", "NE 43 A", "GT 44 A", "LT 45 A", "GE 46 A", "LE 47 A",
"RESW F0 D", "RESB F1 D", "WORD F2 D", "BYTE F3 D"};
HashTable *opTable = NULL;
HashTable *OpTableNew() {
if (opTable != NULL) return opTable;
opTable = HashTableNew(127);
int i;
for (i=0; i<sizeof(opList)/sizeof(char*); i++) {
Op *op = OpNew(opList[i]);
HashTablePut(opTable, op->name, op);
}
return opTable;
}
void OpTableFree() {
if (opTable != NULL) {
HashTableEach(opTable, (FuncPtr1) OpFree);
HashTableFree(opTable);
opTable = NULL;
}
}
Op* OpNew(char *opLine) {
Op *op = ObjNew(Op, 1);
char opName[100];
sscanf(opLine, "%s %x %c", opName, &op->code, &op->type);
op->name = strNew(opName);
return op;
}
void OpFree(Op *op) {
memFree(op->name);
ObjFree(op);
}
int OpPrintln(Op *op) {
printf("%s %2x %c\n", op->name, op->code, op->type);
}