OpTable -- CPU0 的指令表

檔案:OpTable.h

#ifndef OPTABLE_H
#define OPTABLE_H

#include "HashTable.h"

#define OP_LD   0x00
#define OP_ST   0x01
#define OP_LDB  0x02
#define OP_STB  0x03
#define OP_LDR  0x04
#define OP_STR  0x05
#define OP_LBR  0x06
#define OP_SBR  0x07
#define OP_LDI  0x08
#define OP_CMP  0x10
#define OP_MOV  0x12
#define OP_ADD  0x13
#define OP_SUB  0x14
#define OP_MUL  0x15
#define OP_DIV  0x16
#define OP_AND  0x18
#define OP_OR   0x19
#define OP_XOR  0x1A
#define OP_ROL  0x1C
#define OP_ROR  0x1D
#define OP_SHL  0x1E
#define OP_SHR  0x1F
#define OP_JEQ  0x20
#define OP_JNE  0x21
#define OP_JLT  0x22
#define OP_JGT  0x23
#define OP_JLE  0x24
#define OP_JGE  0x25
#define OP_JMP  0x26
#define OP_SWI  0x2A
#define OP_JSUB 0x2B
#define OP_RET  0x2C
#define OP_PUSH 0x30
#define OP_POP  0x31
#define OP_PUSHB 0x32
#define OP_POPB 0x33

#define OP_RESW 0xF0
#define OP_RESB 0xF1
#define OP_WORD 0xF2
#define OP_BYTE 0xF3
#define OP_NULL 0xFF

typedef struct {
  char *name;
  int code;
  char type;
} Op;

void OpTableTest();

HashTable *OpTableNew();
void OpTableFree();

Op* OpNew(char *opLine);
void OpFree(Op *op);
int OpPrintln(Op *op);

extern char *opList[];
extern HashTable *opTable;

#endif

檔案:OpTable.c

#include "OpTable.h"

void OpTableTest() {
  OpTableNew();
  HashTableEach(opTable, (FuncPtr1) OpPrintln);  
  OpTableFree();
}

char *opList[] = {"LD 00 L", "ST 01 L", "LB 02 L", "SB 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", "JSUB 2B J", "RET 2C J", "PUSH 30 J", "POP 31 J", 
  "PUSHB 32 J", "POPB 33 J", "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 = newStr(opName);
  return op;
}

void OpFree(Op *op) {
  freeMemory(op->name);
  ObjFree(op);
}

int OpPrintln(Op *op) {
  printf("%s %2x %c\n", op->name, op->code, op->type);
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License