YACASL2
Loading...
Searching...
No Matches
struct.c
Go to the documentation of this file.
1#include <errno.h>
2#include "struct.h"
3#include "exec.h"
4
8SYSTEM *sys = NULL;
9
14
18static const COMET2CMD comet2cmd[] = {
19 { "NOP", NONE, 0x0, nop, 1 },
20 { "LD", R_ADR_X, 0x1000, ld_r_adr_x, 2 },
21 { "ST", R_ADR_X, 0x1100, st, 2 },
22 { "LAD", R_ADR_X, 0x1200, lad, 2 },
23 { "LD", R1_R2, 0x1400, ld_r1_r2, 1 },
24 { "ADDA", R_ADR_X, 0x2000, adda_r_adr_x, 2 },
25 { "SUBA", R_ADR_X, 0x2100, suba_r_adr_x, 2 },
26 { "ADDL", R_ADR_X, 0x2200, addl_r_adr_x, 2 },
27 { "SUBL", R_ADR_X, 0x2300, subl_r_adr_x, 2 },
28 { "ADDA", R1_R2, 0x2400, adda_r1_r2, 1 },
29 { "SUBA", R1_R2, 0x2500, suba_r1_r2, 1 },
30 { "ADDL", R1_R2, 0x2600, addl_r1_r2, 1 },
31 { "SUBL", R1_R2, 0x2700, subl_r1_r2, 1 },
32 { "AND", R_ADR_X, 0x3000, and_r_adr_x, 2 },
33 { "OR", R_ADR_X, 0x3100, or_r_adr_x, 2 },
34 { "XOR", R_ADR_X, 0x3200, xor_r_adr_x, 2 },
35 { "AND", R1_R2, 0x3400, and_r1_r2, 1 },
36 { "OR", R1_R2, 0x3500, or_r1_r2, 1 },
37 { "XOR", R1_R2, 0x3600, xor_r1_r2, 1 },
38 { "CPA", R_ADR_X, 0x4000, cpa_r_adr_x, 2 },
39 { "CPL", R_ADR_X, 0x4100, cpl_r_adr_x, 2 },
40 { "CPA", R1_R2, 0x4400, cpa_r1_r2, 1 },
41 { "CPL", R1_R2, 0x4500, cpl_r1_r2, 1 },
42 { "SLA", R_ADR_X, 0x5000, sla, 2 },
43 { "SRA", R_ADR_X, 0x5100, sra, 2 },
44 { "SLL", R_ADR_X, 0x5200, sll, 2 },
45 { "SRL", R_ADR_X, 0x5300, srl, 2 },
46 { "JMI", ADR_X, 0x6100, jmi, 2 },
47 { "JNZ", ADR_X, 0x6200, jnz, 2 },
48 { "JZE", ADR_X, 0x6300, jze, 2 },
49 { "JUMP", ADR_X, 0x6400, jump, 2 },
50 { "JPL", ADR_X, 0x6500, jpl, 2 },
51 { "JOV", ADR_X, 0x6600, jov, 2 },
52 { "PUSH", ADR_X, 0x7000, push, 2 },
53 { "POP", R_, 0x7100, pop, 1 },
54 { "CALL", ADR_X, 0x8000, call, 2 },
55 { "RET", NONE, 0x8100, ret, 1 },
56 { "SVC", ADR_X, 0xF000, svc, 2 },
57};
58
63
67enum {
69};
70
74static CMDTAB *cmdtab[HASH_MAX][CMDTABSIZE] = {{NULL}};
75
79unsigned hash_cmdtype(const char *cmd, CMDTYPE type);
80
84unsigned hash_code(WORD code);
85
89void cpu_reset();
90
94void memory_reset();
95
99unsigned hash_cmdtype(const char *cmd, CMDTYPE type)
100{
101 HKEY *keys[2] = {NULL};
102 unsigned hashval = 0;
103
104 /* 命令名を設定 */
105 keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]");
106 keys[0]->type = CHARS;
107 keys[0]->val.s = strdup_chk(cmd, "keys[0].val.s");
108 /* 命令タイプを設定 */
109 keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[1]");
110 keys[1]->type = INT;
111 keys[1]->val.i = (int)(type & 070);
112 /* ハッシュ値の計算 */
113 hashval = hash(2, keys, CMDTABSIZE);
114 FREE(keys[0]->val.s);
115 FREE(keys[0]);
116 FREE(keys[1]);
117 /* ハッシュ値を返す */
118 return hashval;
119}
120
125{
126 CMDTAB *p = NULL;
127 unsigned hashval;
128
129 for(int i = 0; i < comet2cmdsize; i++) {
130 p = malloc_chk(sizeof(CMDTAB), "create_cmdtable.p");
131 p->cmd = &comet2cmd[i];
132 if(hash == HASH_CMDTYPE) {
133 hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type);
134 } else if(hash == HASH_CODE) {
135 hashval = hash_code(comet2cmd[i].code);
136 }
137 p->next = cmdtab[hash][hashval];
138 cmdtab[hash][hashval] = p;
139 }
140 return true;
141}
142
147{
148 CMDTAB *p = NULL;
149 CMDTAB *q = NULL;
150
151 for(int i = 0; i < CMDTABSIZE; i++) {
152 for(p = cmdtab[hash][i]; p != NULL; p = q) {
153 q = p->next;
154 FREE(p);
155 }
156 cmdtab[hash][i] = NULL;
157 }
158}
159
164WORD getcmdcode(const char *cmd, CMDTYPE type)
165{
166 CMDTAB *p = NULL;
167 WORD w = 0xFFFF;
168
169 assert(cmd != NULL);
170 for(p = cmdtab[HASH_CMDTYPE][hash_cmdtype(cmd, type)]; p != NULL; p = p->next) {
171 if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) {
172 w = p->cmd->code;
173 break;
174 }
175 }
176 return w;
177}
178
183WORD getcmdwordlen(const char *cmd, CMDTYPE type)
184{
185 CMDTAB *p = NULL;
186 int wl = 0;
187
188 assert(cmd != NULL);
189 for(p = cmdtab[HASH_CMDTYPE][hash_cmdtype(cmd, type)]; p != NULL; p = p->next) {
190 if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) {
191 wl = p->cmd->wordlen;
192 break;
193 }
194 }
195 return wl;
196}
197
201unsigned hash_code(WORD code)
202{
203 HKEY *keys[1] = {NULL};
204 unsigned h = 0;
205
206 /* 命令コードを設定 */
207 keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key");
208 keys[0]->type = INT;
209 keys[0]->val.i = (int)(code >> 8);
210 h = hash(1, keys, CMDTABSIZE);
211 FREE(keys[0]);
212 return h;
213}
214
218void (*getcmdptr(WORD code))
219{
220 CMDTAB *t = NULL;
221 void *ptr = NULL;
222
223 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
224 if(code == t->cmd->code) {
225 ptr = t->cmd->ptr;
226 break;
227 }
228 }
229 return ptr;
230}
231
236{
237 CMDTAB *t = NULL;
238 CMDTYPE type = NONE;
239
240 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
241 if(code == t->cmd->code) {
242 type = t->cmd->type;
243 break;
244 }
245 }
246 return type;
247}
248
252char *getcmdname(WORD code)
253{
254 CMDTAB *t = NULL;
255 char *cmd = NULL;
256
257 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
258 if(code == t->cmd->code) {
259 cmd = t->cmd->name;
260 break;
261 }
262 }
263 return cmd;
264}
265
270{
271 bool res = true;
272 CMDTYPE cmdtype = getcmdtype(code & 0xFF00);
273 WORD gr = 0;
274 if(cmdtype == R_ADR_X || cmdtype == R1_R2 || cmdtype == R_) {
275 gr = (code & 0x00F0) >> 4;
276 if(gr < 0 || GRSIZE <= gr) {
277 res = false;
278 }
279 }
280 if(cmdtype == R_ADR_X || cmdtype == R1_R2 || cmdtype == ADR_X) {
281 gr = code & 0x000F;
282 if(GRSIZE <= gr) {
283 res = false;
284 }
285
286 }
287 return res;
288}
289
295{
296 CMDTAB *t = NULL;
297 int wl = 0;
298
299 for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) {
300 if(code == t->cmd->code) {
301 wl = t->cmd->wordlen;
302 break;
303 }
304 }
305 return wl;
306}
307
311
312char *grstr(WORD word)
313{
314 assert(word <= 7);
315 char *str = NULL;
316
317 str = malloc_chk(3 + 1, "grstr.str");
318 sprintf(str, "GR%d", word);
319 return str;
320}
321
322WORD memsize_str2word(const char *str) {
323 return (WORD)str2l_range(str, 1, MAX_MEMSIZE, "Memory Size");
324}
325
329void comet2_init(WORD memsize, CLOCK clocks)
330{
331 sys = malloc_chk(sizeof(SYSTEM), "sys");
332 /* メモリサイズを設定 */
333 sys->memsize = memsize;
334 /* クロック周波数を設定 */
335 assert(0 < clocks && clocks <= MAX_CLOCKS);
336 sys->clocks = clocks;
337 /* CPU領域の確保 */
338 sys->cpu = malloc_chk(sizeof(CPU), "comet2_init.cpu");
339 /* CPUをリセット */
340 cpu_reset();
341 /* メモリ領域の確保 */
342 sys->memory = calloc_chk(sys->memsize, sizeof(WORD), "comet2_init.memory");
343 /* メモリをリセット */
344 memory_reset();
345 /* CASL2プログラムの開始と終了のアドレスを初期化 */
346 execptr = malloc_chk(sizeof(EXECPTR), "execptr");
347 execptr->stop = false;
348}
349
350
351void cpu_reset() {
352 /* 汎用レジスタ */
353 for(int i = 0; i < GRSIZE; i++) {
354 sys->cpu->gr[i] = 0x0;
355 }
356 sys->cpu->sp = sys->memsize; /* スタックポインタ */
357 sys->cpu->pr = 0x0; /* プログラムレジスタ */
358 sys->cpu->fr = 0x0; /* フラグレジスタ */
359}
360
365 memset(sys->memory, 0, sys->memsize * sizeof(WORD));
366}
367
372{
373 /* CASL2プログラム終了のアドレスを初期化 */
374 execptr->stop = false;
375 /* CPUをリセット */
376 cpu_reset();
377 /* CPUとメモリを設定に応じて表示 */
379}
380
385{
386 /* CASL2プログラム終了のアドレスを初期化 */
387 execptr->stop = false;
388 /* CPUをリセット */
389 cpu_reset();
390 /* メモリをリセット */
391 memory_reset();
392 /* CPUとメモリを設定に応じて表示 */
394}
395
400{
401 FREE(execptr);
402 FREE(sys->memory);
403 FREE(sys->cpu);
404 FREE(sys);
405}
#define FREE(ptr)
メモリを解放するマクロ
Definition cmem.h:22
void * calloc_chk(size_t nmemb, size_t size, const char *tag)
領域の数とサイズを指定してメモリーを確保するcallocを実行する
Definition cmem.c:34
void * malloc_chk(size_t size, const char *tag)
mallocを実行し、0で初期化する
Definition cmem.c:23
#define ARRAYSIZE(array)
配列のサイズを返すマクロ
Definition cmem.h:15
long str2l_range(const char *str, long min, long max, const char *name)
数値文字列が特定の範囲の数値かチェックし、正の場合は変換した数値、不正の場合は0を返す
Definition cmem.c:3
char * strdup_chk(const char *s, const char *tag)
malloc_chkを実行してメモリを確保し、コピーした文字列を返す
Definition cmem.c:45
void and_r_adr_x()
AND命令 - オペランドr,adr,x。語長2.
Definition exec.c:366
void st()
ST命令。語長2.
Definition exec.c:245
void jmi()
JMI命令。語長2.
Definition exec.c:569
void svc()
SVC命令。語長2.
Definition exec.c:666
void suba_r1_r2()
SUBA命令 - オペランドr1,r2。語長1.
Definition exec.c:299
void cpl_r_adr_x()
CPL命令 - オペランドr,adr,x。語長2.
Definition exec.c:442
void sra()
SRA命令 - オペランドr,adr,x。語長2.
Definition exec.c:485
void pop()
POP命令。語長1.
Definition exec.c:633
void adda_r1_r2()
ADDA命令 - オペランドr1,r2。語長1.
Definition exec.c:285
void xor_r1_r2()
XOR命令 - オペランドr1,r2。語長1.
Definition exec.c:401
void subl_r_adr_x()
SUBL命令 - オペランドr,adr,x。語長2.
Definition exec.c:352
void sla()
SLA命令 - オペランドr,adr,x。語長2.
Definition exec.c:456
void srl()
SRL命令 - オペランドr,adr,x。語長2.
Definition exec.c:543
void ld_r_adr_x()
LD命令 - オペランドr,adr,x。語長2.
Definition exec.c:231
void jnz()
JNZ命令。語長2.
Definition exec.c:579
void adda_r_adr_x()
ADDA命令 - オペランドr,adr,x。語長2.
Definition exec.c:278
void addl_r1_r2()
ADDL命令 - オペランドr1,r2。語長1.
Definition exec.c:345
void cpl_r1_r2()
CPL命令 - オペランドr1,r2。語長1.
Definition exec.c:449
void jump()
JUMP命令。語長2.
Definition exec.c:599
void subl_r1_r2()
SUBL命令 - オペランドr1,r2。語長1.
Definition exec.c:359
void cpa_r1_r2()
CPA命令 - オペランドr1,r2。語長1.
Definition exec.c:425
void push()
PUSH命令。語長2.
Definition exec.c:625
void nop()
NOP命令。語長1(OPのみ)
Definition exec.c:226
void cpa_r_adr_x()
CPA命令 - オペランドr,adr,x。語長2.
Definition exec.c:418
void and_r1_r2()
AND命令 - オペランドr1,r2。語長1.
Definition exec.c:373
void ret()
RET命令。語長1(OPのみ)
Definition exec.c:656
void or_r_adr_x()
OR命令 - オペランドr,adr,x。語長2.
Definition exec.c:380
void suba_r_adr_x()
SUBA命令 - オペランドr,adr,x。語長2.
Definition exec.c:292
void addl_r_adr_x()
ADDL命令 - オペランドr,adr,x。語長2.
Definition exec.c:338
void sll()
SLL命令 - オペランドr,adr,x。語長2.
Definition exec.c:517
void call()
CALL命令。語長2.
Definition exec.c:648
void jpl()
JPL命令。語長2.
Definition exec.c:605
void lad()
LAD命令。語長2.
Definition exec.c:252
void dsp_trace_dump()
COMET IIのレジスタとメモリを実行状況と設定に応じて表示する
Definition dump.c:61
void xor_r_adr_x()
XOR命令 - オペランドr,adr,x。語長2.
Definition exec.c:394
void jze()
JZE命令。語長2.
Definition exec.c:589
void or_r1_r2()
OR命令 - オペランドr1,r2。語長1.
Definition exec.c:387
void jov()
JOV命令。語長2.
Definition exec.c:615
void ld_r1_r2()
LD命令 - オペランドr1,r2。語長1.
Definition exec.c:238
@ CHARS
Definition hash.h:8
@ INT
Definition hash.h:9
unsigned hash(int keyc, HKEY *keyv[], int tabsize)
ハッシュ値を取得する
Definition hash.c:3
WORD code2cmdwordlen(WORD code)
Definition struct.c:294
WORD memsize_str2word(const char *str)
メモリーサイズを表す数値文字列をWORD値に変換して返す。WORD値に変換できない場合は、エラーを表示して0を返す
Definition struct.c:322
void cpu_reset()
Definition struct.c:351
void comet2_shutdown()
COMET II仮想マシンのシャットダウン
Definition struct.c:399
void memory_reset()
Definition struct.c:364
CMDTYPE getcmdtype(WORD code)
Definition struct.c:235
bool create_cmdtable(CMDTAB_HASH hash)
命令ハッシュ表を作成する
Definition struct.c:124
static CMDTAB * cmdtab[HASH_MAX][CMDTABSIZE]
Definition struct.c:74
@ CMDTABSIZE
Definition struct.c:68
void * getcmdptr(WORD code)
Definition struct.c:218
unsigned hash_code(WORD code)
Definition struct.c:201
char * grstr(WORD word)
汎用レジスタの番号からレジスタを表す文字列を返す
Definition struct.c:312
void free_cmdtable(CMDTAB_HASH hash)
Definition struct.c:146
bool code_gr_valid(WORD code)
Definition struct.c:269
WORD getcmdwordlen(const char *cmd, CMDTYPE type)
Definition struct.c:183
void comet2_resetall()
Definition struct.c:384
void comet2_reset()
COMET II仮想マシンのCPUをリセット
Definition struct.c:371
static const COMET2CMD comet2cmd[]
Definition struct.c:18
char * getcmdname(WORD code)
Definition struct.c:252
static int comet2cmdsize
Definition struct.c:62
WORD getcmdcode(const char *cmd, CMDTYPE type)
Definition struct.c:164
unsigned hash_cmdtype(const char *cmd, CMDTYPE type)
Definition struct.c:99
void comet2_init(WORD memsize, CLOCK clocks)
COMET II仮想マシンの初期化
Definition struct.c:329
CMDTAB_HASH
Definition struct.h:63
@ HASH_CMDTYPE
Definition struct.h:64
@ HASH_MAX
Definition struct.h:66
@ HASH_CODE
Definition struct.h:65
@ MAX_MEMSIZE
Definition struct.h:20
@ GRSIZE
Definition struct.h:19
@ MAX_CLOCKS
Definition struct.h:22
EXECPTR * execptr
Definition struct.c:13
unsigned int CLOCK
Definition struct.h:12
CMDTYPE
Definition struct.h:73
@ R_
Definition struct.h:97
@ R_ADR_X
Definition struct.h:80
@ ADR_X
Definition struct.h:92
@ NONE
Definition struct.h:101
@ R1_R2
Definition struct.h:86
SYSTEM * sys
COMET IIの仮想実行マシンシステム
Definition struct.c:8
struct _CMDTAB CMDTAB
const COMET2CMD * cmd
Definition struct.h:128
struct _CMDTAB * next
Definition struct.h:127
CMDTYPE type
Definition struct.h:117
void * ptr
Definition struct.h:119
int wordlen
Definition struct.h:120
char * name
Definition struct.h:116
WORD code
Definition struct.h:118
COMET IIのCPUを表すデータ型
Definition struct.h:38
ハッシュ共用体のデータ型
Definition hash.h:15
char * s
Definition hash.h:18
UTYPE type
Definition hash.h:16
int i
Definition hash.h:19
union HKEY::@132303155052201023056030363273137132157157107260 val
COMET IIの仮想実行マシンシステムを表すデータ型
Definition struct.h:48
unsigned short WORD
16ビットの数値を表すデータ型
Definition word.h:9