Revisão | 1faeff8105b106b39607550f0e0249e4f8f9525d (tree) |
---|---|
Hora | 2014-06-21 22:04:55 |
Autor | hikarupsp <hikarupsp@user...> |
Commiter | hikarupsp |
CALLBIOS命令を実装
デバッグ出力のon/off
@@ -11,6 +11,7 @@ | ||
11 | 11 | 17478CFE193E2DEB00293791 /* test.hex in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17478CFD193E2ADA00293791 /* test.hex */; }; |
12 | 12 | 17478D01193F3D0000293791 /* ch4.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D00193F3D0000293791 /* ch4.c */; }; |
13 | 13 | 17478D041940B66700293791 /* opcode.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D031940B66700293791 /* opcode.c */; }; |
14 | + 17F95C01194C8DA70064E1B6 /* bios.c in Sources */ = {isa = PBXBuildFile; fileRef = 17F95C00194C8DA70064E1B6 /* bios.c */; }; | |
14 | 15 | /* End PBXBuildFile section */ |
15 | 16 | |
16 | 17 | /* Begin PBXCopyFilesBuildPhase section */ |
@@ -34,6 +35,7 @@ | ||
34 | 35 | 17478D00193F3D0000293791 /* ch4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ch4.c; sourceTree = "<group>"; usesTabs = 1; }; |
35 | 36 | 17478D02193F586100293791 /* ch4.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ch4.h; sourceTree = "<group>"; usesTabs = 1; }; |
36 | 37 | 17478D031940B66700293791 /* opcode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = opcode.c; sourceTree = "<group>"; usesTabs = 1; }; |
38 | + 17F95C00194C8DA70064E1B6 /* bios.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bios.c; sourceTree = "<group>"; }; | |
37 | 39 | /* End PBXFileReference section */ |
38 | 40 | |
39 | 41 | /* Begin PBXFrameworksBuildPhase section */ |
@@ -73,6 +75,7 @@ | ||
73 | 75 | 17478D02193F586100293791 /* ch4.h */, |
74 | 76 | 17478CFD193E2ADA00293791 /* test.hex */, |
75 | 77 | 17478D031940B66700293791 /* opcode.c */, |
78 | + 17F95C00194C8DA70064E1B6 /* bios.c */, | |
76 | 79 | ); |
77 | 80 | path = chncpu; |
78 | 81 | sourceTree = "<group>"; |
@@ -128,6 +131,7 @@ | ||
128 | 131 | isa = PBXSourcesBuildPhase; |
129 | 132 | buildActionMask = 2147483647; |
130 | 133 | files = ( |
134 | + 17F95C01194C8DA70064E1B6 /* bios.c in Sources */, | |
131 | 135 | 17478D041940B66700293791 /* opcode.c in Sources */, |
132 | 136 | 17478CF4193E2A4900293791 /* chncpu.c in Sources */, |
133 | 137 | 17478D01193F3D0000293791 /* ch4.c in Sources */, |
@@ -0,0 +1,78 @@ | ||
1 | +// | |
2 | +// bios.c | |
3 | +// chncpu | |
4 | +// | |
5 | +// Created by 西田 耀 on 2014/06/14. | |
6 | +// Copyright (c) 2014年 CHNOSProject. All rights reserved. | |
7 | +// | |
8 | + | |
9 | +#include "chncpu.h" | |
10 | +#include <stdio.h> | |
11 | + | |
12 | +int CHNCPU_BIOS_Init(CHNCPU_RuntimeEnvironment *env) | |
13 | +{ | |
14 | + int i; | |
15 | + | |
16 | + // FuncTables | |
17 | + for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){ | |
18 | + env->bios->bindBIOSFuncTable[i] = NULL; | |
19 | + env->bios->execBIOSFuncTable[i] = NULL; | |
20 | + env->bios->printBIOSFuncTable[i] = NULL; | |
21 | + } | |
22 | + | |
23 | + // putcReg | |
24 | + env->bios->bindBIOSFuncTable[0x00] = CHNCPU_BIOS_Op_putcReg_BindOperand; | |
25 | + env->bios->execBIOSFuncTable[0x00] = CHNCPU_BIOS_Op_putcReg_Execute; | |
26 | + env->bios->printBIOSFuncTable[0x00] = CHNCPU_BIOS_Op_putcReg_PrintCode; | |
27 | + | |
28 | + return 0; | |
29 | + | |
30 | +} | |
31 | + | |
32 | +// | |
33 | +// putcReg | |
34 | +// | |
35 | + | |
36 | +typedef struct CHNCPU_BIOS_OP_CACHE_LIMM CHNCPU_BIOS_OpCache_putcReg; | |
37 | +struct CHNCPU_BIOS_OP_CACHE_LIMM { | |
38 | + ch4_uint r; | |
39 | +}; | |
40 | +int CHNCPU_BIOS_Op_putcReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix) | |
41 | +{ | |
42 | + CHNCPU_BIOS_OpCache_putcReg *opCache; | |
43 | + | |
44 | + opCache = malloc(sizeof(CHNCPU_BIOS_OpCache_putcReg)); | |
45 | + op->opCache = opCache; | |
46 | + | |
47 | + opCache->r = CH4Reader_ReadNextAsUINT(env->appbinReader); | |
48 | + | |
49 | + if(opCache->r >= CHNCPU_NUMBER_OF_IREG){ | |
50 | + env->errFlags |= CHNCPU_ERR_C_REGNUM; | |
51 | + return -1; | |
52 | + } | |
53 | + if(prefix != 0){ | |
54 | + env->errFlags |= CHNCPU_ERR_C_PREFIX; | |
55 | + return -1; | |
56 | + } | |
57 | + return 0; | |
58 | +} | |
59 | +int CHNCPU_BIOS_Op_putcReg_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | |
60 | +{ | |
61 | + CHNCPU_BIOS_OpCache_putcReg *opCache; | |
62 | + | |
63 | + opCache = op->opCache; | |
64 | + | |
65 | + putchar(env->iReg[opCache->r]); | |
66 | + | |
67 | + return 0; | |
68 | +} | |
69 | + | |
70 | +int CHNCPU_BIOS_Op_putcReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file) | |
71 | +{ | |
72 | + CHNCPU_BIOS_OpCache_putcReg *opCache; | |
73 | + | |
74 | + opCache = op->opCache; | |
75 | + fprintf(file, "BIOS::putcReg(r:%d);\n", opCache->r); | |
76 | + | |
77 | + return 0; | |
78 | +} |
@@ -62,7 +62,7 @@ int decodeHexString(char *src0, char *src1, unsigned char *dst0, unsigned char * | ||
62 | 62 | puts("decodeHexString: not implemented.\n"); |
63 | 63 | exit(EXIT_FAILURE); |
64 | 64 | } else{ |
65 | - if(c == ' ' || c == '\t'){ | |
65 | + if(c == ' ' || c == '\t' || c == '\n'){ | |
66 | 66 | continue; |
67 | 67 | } |
68 | 68 | puts("decodeHexString: unknown character :"); |
@@ -127,28 +127,11 @@ CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(void) | ||
127 | 127 | |
128 | 128 | env->appbinReader = malloc(sizeof(CH4Reader)); |
129 | 129 | |
130 | - // FuncTables | |
131 | - for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){ | |
132 | - env->bindOpFuncTable[i] = NULL; | |
133 | - } | |
134 | - for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){ | |
135 | - env->execOpFuncTable[i] = NULL; | |
136 | - } | |
137 | - for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){ | |
138 | - env->printOpFuncTable[i] = NULL; | |
139 | - } | |
140 | - // LIMM | |
141 | - env->bindOpFuncTable[0x02] = CHNCPU_Op_LIMM_BindOperand; | |
142 | - env->execOpFuncTable[0x02] = CHNCPU_Op_LIMM_Execute; | |
143 | - env->printOpFuncTable[0x02] = CHNCPU_Op_LIMM_PrintCode; | |
130 | + CHNCPU_Op_Init(env); | |
131 | + | |
132 | + env->bios = malloc(sizeof(CHNCPU_BIOS)); | |
133 | + CHNCPU_BIOS_Init(env); | |
144 | 134 | |
145 | - // TernaryRegBitwise | |
146 | - for(i = 0x10; i <= 0x11; i++){ | |
147 | - env->bindOpFuncTable[i] = CHNCPU_Op_TernaryRegBitwise_BindOperand; | |
148 | - env->execOpFuncTable[i] = CHNCPU_Op_TernaryRegBitwise_Execute; | |
149 | - env->printOpFuncTable[i] = CHNCPU_Op_TernaryReg_PrintCode; | |
150 | - } | |
151 | - | |
152 | 135 | env->errFlags = 0; |
153 | 136 | |
154 | 137 | return env; |
@@ -175,8 +158,8 @@ int CHNCPU_LoadBinaryFromHexStringFilePath(CHNCPU_RuntimeEnvironment *env, const | ||
175 | 158 | } |
176 | 159 | |
177 | 160 | env->appbinsize = decodeHexString(tmpdata0, tmpdata0 + len, env->appbin0, env->appbin0 + CHNCPU_SIZE_APPBIN); |
178 | - printf("appbin = %d Bytes\n", env->appbinsize); | |
179 | - CHNCPU_DumpAppBin(env); | |
161 | + //printf("appbin = %d Bytes\n", env->appbinsize); | |
162 | + //CHNCPU_DumpAppBin(env); | |
180 | 163 | |
181 | 164 | CH4Reader_Initialize(env->appbinReader, env->appbin0, env->appbinsize); |
182 | 165 |
@@ -187,71 +170,54 @@ int CHNCPU_LoadBinaryFromHexStringFilePath(CHNCPU_RuntimeEnvironment *env, const | ||
187 | 170 | |
188 | 171 | int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env) |
189 | 172 | { |
190 | - ch4_uint opcode; | |
173 | + ch4_uint opcode = 0; | |
191 | 174 | int mindex; |
192 | 175 | CHNCPU_OpTag *op; |
193 | - int breakFlag = 0; | |
194 | - int retv; | |
195 | 176 | unsigned int prefix = 0; |
196 | 177 | |
197 | 178 | // env->appbinReaderから読み込んで、実行可能な状態にする。 |
198 | 179 | // これはコンパイラ版におけるコンパイルに相当する。 |
199 | - printf("< Beginning of binary > \n"); | |
180 | + //printf("< Beginning of binary > \n"); | |
200 | 181 | mindex = 0; |
201 | 182 | for(mindex = 0; mindex < CHNCPU_LENGTH_OF_MAIN_MEMORY; mindex++){ |
202 | 183 | opcode = CH4Reader_ReadNextAsUINT(env->appbinReader); |
203 | 184 | if(CH4Reader_IsEndOfBinary(env->appbinReader)){ |
204 | - puts("< End of binary >"); | |
205 | - breakFlag = 1; | |
206 | 185 | break; |
207 | 186 | } |
208 | - printf("(%02X) ", opcode); | |
187 | + //printf("(%02X) ", opcode); | |
209 | 188 | |
210 | 189 | op = &env->mainmemory[mindex]; |
211 | 190 | op->opCode = opcode; |
212 | 191 | switch(opcode){ |
213 | 192 | case 0x00: |
214 | 193 | // NOP |
215 | - puts("NOP();\n"); | |
216 | - // メモリには追加しない | |
217 | - mindex--; | |
194 | + mindex--; // メモリには追加しない | |
218 | 195 | break; |
219 | 196 | case 0x2F: |
220 | 197 | // Prefix |
221 | 198 | opcode = CH4Reader_ReadNextAsUINT(env->appbinReader); |
222 | - printf("Prefix-%X\n", opcode); | |
199 | + //printf("Prefix-%X\n", opcode); | |
223 | 200 | if(opcode > CHNCPU_PREFIX_MAX){ |
224 | 201 | env->errFlags |= CHNCPU_ERR_C_PREFIX; |
225 | - breakFlag = 1; | |
226 | 202 | break; |
227 | 203 | } |
228 | 204 | prefix |= (0x01 << opcode); |
229 | - // メモリには追加しない | |
230 | - mindex--; | |
205 | + mindex--; // メモリには追加しない | |
231 | 206 | break; |
232 | 207 | default: |
233 | 208 | // ごく一部の特殊な命令以外は、命令テーブルを参照する |
234 | 209 | if(opcode <= CHNCPU_OPECODE_MAX && env->bindOpFuncTable[opcode]){ |
235 | - retv = env->bindOpFuncTable[opcode](env, op, prefix); | |
236 | - if(retv){ | |
237 | - opcode = CHNCPU_OPCODE_INVALID; | |
238 | - } | |
210 | + env->bindOpFuncTable[opcode](env, op, prefix); | |
239 | 211 | } else{ |
240 | - opcode = CHNCPU_OPCODE_INVALID; | |
241 | 212 | env->errFlags |= CHNCPU_ERR_C_OPCODE; |
242 | 213 | } |
243 | - if(opcode == CHNCPU_OPCODE_INVALID){ | |
244 | - op->opCode = CHNCPU_OPCODE_INVALID; | |
245 | - breakFlag = 1; | |
246 | - } | |
247 | 214 | prefix = 0; |
248 | 215 | break; |
249 | 216 | } |
250 | 217 | if(env->appbinReader->errorFlags){ |
251 | 218 | env->errFlags |= CHNCPU_ERR_C_INVALID_BIN; |
252 | - break; | |
253 | 219 | } |
254 | - if(breakFlag == 1){ | |
220 | + if(env->errFlags){ | |
255 | 221 | break; |
256 | 222 | } |
257 | 223 | } |
@@ -267,7 +233,7 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env) | ||
267 | 233 | puts("INVALID-C: Not supported bit width."); |
268 | 234 | } |
269 | 235 | if(env->errFlags & CHNCPU_ERR_C_OPCODE){ |
270 | - puts("INVALID-C: Unknown opCode."); | |
236 | + printf("INVALID-C: Unknown opCode 0x%X\n", opcode); | |
271 | 237 | } |
272 | 238 | if(env->errFlags & CHNCPU_ERR_C_EXPRESSION){ |
273 | 239 | puts("INVALID-C: Invalid expression."); |
@@ -285,32 +251,37 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env) | ||
285 | 251 | int CHNCPU_Execute(CHNCPU_RuntimeEnvironment *env) |
286 | 252 | { |
287 | 253 | int count = 0; |
288 | - int retv; | |
289 | 254 | CHNCPU_OpTag *op; |
290 | 255 | |
291 | 256 | if(env->errFlags){ |
292 | 257 | puts(">>> Can't execute binary because of there is some error."); |
293 | 258 | } |
294 | 259 | |
295 | - puts("< Beginning of execution >"); | |
260 | + //puts("< Beginning of execution >"); | |
296 | 261 | for(; env->currentIndex < CHNCPU_LENGTH_OF_MAIN_MEMORY; env->currentIndex++){ |
297 | 262 | op = &env->mainmemory[env->currentIndex]; |
298 | 263 | if(op->opCode == CHNCPU_OPCODE_INVALID){ |
299 | 264 | // End of Binary |
300 | 265 | break; |
301 | 266 | } |
302 | - if(op->opCode <= CHNCPU_OPECODE_MAX && env->bindOpFuncTable[op->opCode]){ | |
303 | - retv = env->execOpFuncTable[op->opCode](env, op); | |
267 | +#if DEBUG_PRINT_OP_EXECUTING | |
268 | + if(op->opCode <= CHNCPU_OPECODE_MAX && env->printOpFuncTable[op->opCode]){ | |
269 | + env->printOpFuncTable[op->opCode](env, op, stdout); | |
270 | + } else{ | |
271 | + printf("Unknown op: 0x%X", op->opCode); | |
272 | + } | |
273 | +#endif | |
274 | + | |
275 | + if(op->opCode <= CHNCPU_OPECODE_MAX && env->execOpFuncTable[op->opCode]){ | |
276 | + if(env->execOpFuncTable[op->opCode](env, op)){ | |
277 | + // 命令実行時にエラーが発生 | |
278 | + break; | |
279 | + } | |
304 | 280 | } else{ |
305 | 281 | // ロード時にチェックしたはずなのに不明な命令が来た |
306 | 282 | env->errFlags |= CHNCPU_ERR_X_INTERNAL; |
307 | 283 | break; |
308 | 284 | } |
309 | - if(retv){ | |
310 | - // 命令実行時にエラーが発生 | |
311 | - break; | |
312 | - } | |
313 | - | |
314 | 285 | } |
315 | 286 | if(env->errFlags){ |
316 | 287 | if(env->errFlags & CHNCPU_ERR_X_INTERNAL){ |
@@ -321,9 +292,11 @@ int CHNCPU_Execute(CHNCPU_RuntimeEnvironment *env) | ||
321 | 292 | } |
322 | 293 | } |
323 | 294 | |
295 | +#if DEBUG_PRINT_IREG_AFTER_EXECUTION | |
296 | + puts("\n>>> End of execution"); | |
324 | 297 | CHNCPU_DumpIReg(env); |
298 | +#endif | |
325 | 299 | |
326 | - puts("< End of execution >"); | |
327 | 300 | |
328 | 301 | return count; |
329 | 302 | } |
@@ -13,9 +13,16 @@ | ||
13 | 13 | #include <stdlib.h> |
14 | 14 | #include "ch4.h" |
15 | 15 | |
16 | +// DEBUG Flag | |
17 | +#define DEBUG_PRINT_IREG_AFTER_EXECUTION 1 | |
18 | +#define DEBUG_PRINT_OP_BINDING 1 | |
19 | +#define DEBUG_PRINT_OP_EXECUTING 1 | |
20 | + | |
21 | +// Settings | |
16 | 22 | #define SIZE_TMPDATA (1024 * 1024 * 1) |
17 | 23 | |
18 | 24 | #define CHNCPU_OPECODE_MAX 0xFF |
25 | +#define CHNCPU_BIOS_OP_MAX 0x00 | |
19 | 26 | #define CHNCPU_BITS_MAX 32 |
20 | 27 | #define CHNCPU_NUMBER_OF_IREG 64 |
21 | 28 | #define CHNCPU_LENGTH_OF_MAIN_MEMORY (64 * 1024) // per Op |
@@ -42,16 +49,24 @@ | ||
42 | 49 | #define CHNCPU_PREFIX_MASK_Op_TernaryRegBitwise (CHNCPU_PREFIX_ALLOW_TRUNCATE) |
43 | 50 | |
44 | 51 | typedef struct CHNCPU_OP_TAG CHNCPU_OpTag; |
52 | +typedef struct CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment; | |
53 | +typedef struct _CHNCPU_BIOS CHNCPU_BIOS; | |
54 | + | |
45 | 55 | struct CHNCPU_OP_TAG { |
46 | 56 | int opCode; |
47 | 57 | void *opCache; |
48 | 58 | }; |
49 | 59 | |
50 | -typedef struct CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment; | |
60 | +struct _CHNCPU_BIOS { | |
61 | + int (*bindBIOSFuncTable[CHNCPU_BIOS_OP_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); | |
62 | + int (*execBIOSFuncTable[CHNCPU_BIOS_OP_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); | |
63 | + int (*printBIOSFuncTable[CHNCPU_BIOS_OP_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); | |
64 | +}; | |
65 | + | |
51 | 66 | struct CHNCPU_RUN_TIME_ENVIRONMENT { |
52 | - int (*bindOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); | |
53 | - int (*execOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); | |
54 | - int (*printOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); | |
67 | + int (*bindOpFuncTable[CHNCPU_OPECODE_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); | |
68 | + int (*execOpFuncTable[CHNCPU_OPECODE_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); | |
69 | + int (*printOpFuncTable[CHNCPU_OPECODE_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); | |
55 | 70 | int iReg[CHNCPU_NUMBER_OF_IREG]; |
56 | 71 | int iRegBits[CHNCPU_NUMBER_OF_IREG]; |
57 | 72 | CHNCPU_OpTag mainmemory[CHNCPU_LENGTH_OF_MAIN_MEMORY]; |
@@ -60,6 +75,7 @@ struct CHNCPU_RUN_TIME_ENVIRONMENT { | ||
60 | 75 | CH4Reader *appbinReader; |
61 | 76 | unsigned int errFlags; |
62 | 77 | int currentIndex; |
78 | + CHNCPU_BIOS *bios; | |
63 | 79 | }; |
64 | 80 | |
65 | 81 | // @chncpu.c |
@@ -73,10 +89,20 @@ void CHNCPU_DumpIReg(CHNCPU_RuntimeEnvironment *env); | ||
73 | 89 | int CHNCPU_CHK_IsAvailableBits(CHNCPU_RuntimeEnvironment *env, ch4_uint bits); |
74 | 90 | |
75 | 91 | // @opcode.c |
92 | +int CHNCPU_Op_Init(CHNCPU_RuntimeEnvironment *env); | |
76 | 93 | int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); |
77 | 94 | int CHNCPU_Op_LIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); |
78 | 95 | int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); |
96 | +int CHNCPU_Op_CALLBIOS_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); | |
97 | +int CHNCPU_Op_CALLBIOS_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); | |
98 | +int CHNCPU_Op_CALLBIOS_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); | |
79 | 99 | int CHNCPU_Op_TernaryRegBitwise_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); |
80 | 100 | int CHNCPU_Op_TernaryRegBitwise_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); |
81 | 101 | int CHNCPU_Op_TernaryReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); |
102 | + | |
103 | +// @bios.c | |
104 | +int CHNCPU_BIOS_Init(CHNCPU_RuntimeEnvironment *env); | |
105 | +int CHNCPU_BIOS_Op_putcReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); | |
106 | +int CHNCPU_BIOS_Op_putcReg_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); | |
107 | +int CHNCPU_BIOS_Op_putcReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file); | |
82 | 108 | #endif |
@@ -8,6 +8,37 @@ | ||
8 | 8 | |
9 | 9 | #include "chncpu.h" |
10 | 10 | |
11 | +int CHNCPU_Op_Init(CHNCPU_RuntimeEnvironment *env) | |
12 | +{ | |
13 | + int i; | |
14 | + | |
15 | + // FuncTables | |
16 | + for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){ | |
17 | + env->bindOpFuncTable[i] = NULL; | |
18 | + env->execOpFuncTable[i] = NULL; | |
19 | + env->printOpFuncTable[i] = NULL; | |
20 | + } | |
21 | + | |
22 | + // LIMM | |
23 | + env->bindOpFuncTable[0x02] = CHNCPU_Op_LIMM_BindOperand; | |
24 | + env->execOpFuncTable[0x02] = CHNCPU_Op_LIMM_Execute; | |
25 | + env->printOpFuncTable[0x02] = CHNCPU_Op_LIMM_PrintCode; | |
26 | + | |
27 | + // CALLBIOS | |
28 | + env->bindOpFuncTable[0x05] = CHNCPU_Op_CALLBIOS_BindOperand; | |
29 | + env->execOpFuncTable[0x05] = CHNCPU_Op_CALLBIOS_Execute; | |
30 | + env->printOpFuncTable[0x05] = CHNCPU_Op_CALLBIOS_PrintCode; | |
31 | + | |
32 | + // TernaryRegBitwise | |
33 | + for(i = 0x10; i <= 0x11; i++){ | |
34 | + env->bindOpFuncTable[i] = CHNCPU_Op_TernaryRegBitwise_BindOperand; | |
35 | + env->execOpFuncTable[i] = CHNCPU_Op_TernaryRegBitwise_Execute; | |
36 | + env->printOpFuncTable[i] = CHNCPU_Op_TernaryReg_PrintCode; | |
37 | + } | |
38 | + | |
39 | + return 0; | |
40 | +} | |
41 | + | |
11 | 42 | // |
12 | 43 | // 02 LIMM |
13 | 44 | // |
@@ -29,8 +60,6 @@ int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, | ||
29 | 60 | opCache->r = CH4Reader_ReadNextAsUINT(env->appbinReader); |
30 | 61 | opCache->bit = CH4Reader_ReadNextAsUINT(env->appbinReader); |
31 | 62 | |
32 | - CHNCPU_Op_LIMM_PrintCode(env, op, stdout); | |
33 | - | |
34 | 63 | if(opCache->r >= CHNCPU_NUMBER_OF_IREG){ |
35 | 64 | env->errFlags |= CHNCPU_ERR_C_REGNUM; |
36 | 65 | return -1; |
@@ -54,8 +83,6 @@ int CHNCPU_Op_LIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | ||
54 | 83 | |
55 | 84 | opCache = op->opCache; |
56 | 85 | |
57 | - CHNCPU_Op_LIMM_PrintCode(env, op, stdout); | |
58 | - | |
59 | 86 | env->iReg[opCache->r] = opCache->imm & (0xFFFFFFFF >> (32 - opCache->bit)); |
60 | 87 | env->iRegBits[opCache->r] = opCache->bit; |
61 | 88 |
@@ -73,6 +100,69 @@ int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, F | ||
73 | 100 | } |
74 | 101 | |
75 | 102 | // |
103 | +// 05 CALLBIOS | |
104 | +// | |
105 | +int CHNCPU_Op_CALLBIOS_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix) | |
106 | +{ | |
107 | + ch4_uint fid; | |
108 | + | |
109 | + CHNCPU_OpTag *opCache; | |
110 | + | |
111 | + opCache = malloc(sizeof(CHNCPU_OpTag)); | |
112 | + op->opCache = opCache; | |
113 | + | |
114 | + opCache->opCode = CH4Reader_ReadNextAsUINT(env->appbinReader); | |
115 | + | |
116 | + fid = opCache->opCode; | |
117 | + if(fid <= CHNCPU_BIOS_OP_MAX && env->bios->bindBIOSFuncTable[fid]){ | |
118 | + env->bios->bindBIOSFuncTable[fid](env, opCache, prefix); | |
119 | + } else{ | |
120 | + env->errFlags |= CHNCPU_ERR_C_OPCODE; | |
121 | + } | |
122 | + | |
123 | + if(env->errFlags){ | |
124 | + return -1; | |
125 | + } | |
126 | + return 0; | |
127 | +} | |
128 | +int CHNCPU_Op_CALLBIOS_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | |
129 | +{ | |
130 | + CHNCPU_OpTag *opCache; | |
131 | + ch4_uint fid; | |
132 | + | |
133 | + opCache = op->opCache; | |
134 | + | |
135 | + fid = opCache->opCode; | |
136 | + if(fid <= CHNCPU_BIOS_OP_MAX && env->bios->execBIOSFuncTable[fid]){ | |
137 | + env->bios->execBIOSFuncTable[fid](env, opCache); | |
138 | + } else{ | |
139 | + // ロード時にチェックしたはずなのに不明な命令が来た | |
140 | + env->errFlags |= CHNCPU_ERR_X_INTERNAL; | |
141 | + } | |
142 | + | |
143 | + return 0; | |
144 | +} | |
145 | + | |
146 | +int CHNCPU_Op_CALLBIOS_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file) | |
147 | +{ | |
148 | + CHNCPU_OpTag *opCache; | |
149 | + ch4_uint fid; | |
150 | + | |
151 | + opCache = op->opCache; | |
152 | + | |
153 | + fid = opCache->opCode; | |
154 | + if(fid <= CHNCPU_BIOS_OP_MAX && env->bios->printBIOSFuncTable[fid]){ | |
155 | + env->bios->printBIOSFuncTable[fid](env, opCache, stdout); | |
156 | + } else{ | |
157 | + fprintf(file, "CALLBIOS(func:0x%X);\n", fid); | |
158 | + } | |
159 | + | |
160 | + return 0; | |
161 | +} | |
162 | + | |
163 | + | |
164 | + | |
165 | +// | |
76 | 166 | // Ternary Register Operation |
77 | 167 | // |
78 | 168 |
@@ -101,9 +191,7 @@ int CHNCPU_Op_TernaryRegBitwise_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNC | ||
101 | 191 | opCache->r0 = CH4Reader_ReadNextAsUINT(env->appbinReader); |
102 | 192 | opCache->bit = CH4Reader_ReadNextAsUINT(env->appbinReader); |
103 | 193 | opCache->prefix = prefix; |
104 | - | |
105 | - CHNCPU_Op_TernaryReg_PrintCode(env, op, stdout); | |
106 | - | |
194 | + | |
107 | 195 | if(opCache->r0 >= CHNCPU_NUMBER_OF_IREG || |
108 | 196 | opCache->r1 >= CHNCPU_NUMBER_OF_IREG || |
109 | 197 | opCache->r2 >= CHNCPU_NUMBER_OF_IREG){ |
@@ -127,9 +215,7 @@ int CHNCPU_Op_TernaryRegBitwise_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_O | ||
127 | 215 | |
128 | 216 | opCache = op->opCache; |
129 | 217 | opCode = op->opCode; |
130 | - | |
131 | - CHNCPU_Op_TernaryReg_PrintCode(env, op, stdout); | |
132 | - | |
218 | + | |
133 | 219 | if(opCode == 0x10){ |
134 | 220 | if(opCache->r1 == opCache->r2){ |
135 | 221 | // CP |
@@ -1 +1 @@ | ||
1 | -2 E123 0 90 2 E365 1 90 91 0 1 2 90 | |
\ No newline at end of file | ||
1 | +2 c41 0 90 5 0 0 |