17 static const COMET2CMD comet2cmd[] = {
18 { "NOP", NONE, 0x0, nop },
19 { "LD", R_ADR_X, 0x1000, ld_r_adr_x },
20 { "ST", R_ADR_X, 0x1100, st },
21 { "LAD", R_ADR_X, 0x1200, lad },
22 { "LD", R1_R2, 0x1400, ld_r1_r2 },
23 { "ADDA", R_ADR_X, 0x2000, adda_r_adr_x },
24 { "SUBA", R_ADR_X, 0x2100, suba_r_adr_x },
25 { "ADDL", R_ADR_X, 0x2200, addl_r_adr_x },
26 { "SUBL", R_ADR_X, 0x2300, subl_r_adr_x },
27 { "ADDA", R1_R2, 0x2400, adda_r1_r2 },
28 { "SUBA", R1_R2, 0x2500, suba_r1_r2 },
29 { "ADDL", R1_R2, 0x2600, addl_r1_r2 },
30 { "SUBL", R1_R2, 0x2700, subl_r1_r2 },
31 { "AND", R_ADR_X, 0x3000, and_r_adr_x },
32 { "OR", R_ADR_X, 0x3100, or_r_adr_x },
33 { "XOR", R_ADR_X, 0x3200, xor_r_adr_x },
34 { "AND", R1_R2, 0x3400, and_r1_r2 },
35 { "OR", R1_R2, 0x3500, or_r1_r2 },
36 { "XOR", R1_R2, 0x3600, xor_r1_r2 },
37 { "CPA", R_ADR_X, 0x4000, cpa_r_adr_x },
38 { "CPL", R_ADR_X, 0x4100, cpl_r_adr_x },
39 { "CPA", R1_R2, 0x4400, cpa_r1_r2 },
40 { "CPL", R1_R2, 0x4500, cpl_r1_r2 },
41 { "SLA", R_ADR_X, 0x5000, sla },
42 { "SRA", R_ADR_X, 0x5100, sra },
43 { "SLL", R_ADR_X, 0x5200, sll },
44 { "SRL", R_ADR_X, 0x5300, srl },
45 { "JMI", ADR_X, 0x6100, jmi },
46 { "JNZ", ADR_X, 0x6200, jnz },
47 { "JZE", ADR_X, 0x6300, jze },
48 { "JUMP", ADR_X, 0x6400, jump },
49 { "JPL", ADR_X, 0x6500, jpl },
50 { "JOV", ADR_X, 0x6600, jov },
51 { "PUSH", ADR_X, 0x7000, push },
52 { "POP", R_, 0x7100, pop },
53 { "CALL", ADR_X, 0x8000, call },
54 { "SVC", ADR_X, 0xF000, svc },
55 { "RET", NONE, 0x8100, ret },
61 static int comet2cmdsize = ARRAYSIZE(comet2cmd);
73 static CMDTAB *cmdtab[HASH_MAX][CMDTABSIZE];
76 * 命令の名前とタイプからハッシュ値を生成する
78 unsigned hash_cmdtype(const char *cmd, CMDTYPE type);
83 unsigned hash_code(WORD code);
86 * 命令の名前とタイプからハッシュ値を生成する
88 unsigned hash_cmdtype(const char *cmd, CMDTYPE type)
94 keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]");
95 keys[0]->type = CHARS;
96 keys[0]->val.s = strdup_chk(cmd, "keys[0].val.s");
98 keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[1]");
100 keys[1]->val.i = (int)(type & 070);
102 hashval = hash(2, keys, CMDTABSIZE);
103 FREE(keys[0]->val.s);
113 bool create_cmdtable(CMDTAB_HASH hash)
119 for(i = 0; i < comet2cmdsize; i++) {
120 p = malloc_chk(sizeof(CMDTAB), "create_cmdtable.p");
121 p->cmd = &comet2cmd[i];
122 if(hash == HASH_CMDTYPE) {
123 hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type);
124 } else if(hash == HASH_CODE) {
125 hashval = hash_code(comet2cmd[i].code);
127 p->next = cmdtab[hash][hashval];
128 cmdtab[hash][hashval] = p;
136 void free_cmdtable(CMDTAB_HASH hash)
141 for(i = 0; i < CMDTABSIZE; i++) {
142 for(p = cmdtab[hash][i]; p != NULL; p = q) {
146 cmdtab[hash][i] = NULL;
151 * 命令の名前とタイプから、命令コードを返す\n
154 WORD getcmdcode(const char *cmd, CMDTYPE type)
160 for(p = cmdtab[HASH_CMDTYPE][hash_cmdtype(cmd, type)]; p != NULL; p = p->next) {
161 if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) {
172 unsigned hash_code(WORD code)
178 keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key");
180 keys[0]->val.i = (int)(code >> 8);
181 h = hash(1, keys, CMDTABSIZE);
187 * 命令コードから命令の関数ポインタを返す
189 const void (*getcmdptr(WORD code))
192 const void *ptr = NULL;
194 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
195 if(code == t->cmd->code) {
206 CMDTYPE getcmdtype(WORD code)
211 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
212 if(code == t->cmd->code) {
223 char *getcmdname(WORD code)
228 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
229 if(code == t->cmd->code) {
238 * 汎用レジスタの番号からレジスタを表す文字列を返す
241 char *grstr(WORD word)
244 char *str = malloc_chk(3 + 1, "grstr.str");
245 sprintf(str, "GR%d", word);
252 void reset(int memsize, int clocks)
256 sys = malloc_chk(sizeof(SYSTEM), "sys");
258 sys->memsize = memsize;
260 sys->clocks = clocks;
262 sys->memory = calloc_chk(sys->memsize, sizeof(WORD), "memory");
264 sys->cpu = malloc_chk(sizeof(CPU), "cpu");
265 for(i = 0; i < GRSIZE; i++) { /* 汎用レジスタ */
266 sys->cpu->gr[i] = 0x0;
268 sys->cpu->sp = sys->memsize; /* スタックポインタ */
269 sys->cpu->pr = 0x0; /* プログラムレジスタ */
270 sys->cpu->fr = 0x0; /* フラグレジスタ */
271 /* CASL2プログラムの開始と終了のアドレスを初期化 */
272 execptr = malloc_chk(sizeof(EXECPTR), "execptr");
273 execptr->stop = false;
277 * COMET II仮想マシンのシャットダウン