• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

修訂07971b929785a6a08e42d3b0e0b70bf8334384e1 (tree)
時間2014-06-10 17:21:10
作者hikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

BitwiseOperationと2F-0プレフィックスを実装

Change Summary

差異

--- a/chncpu/chncpu.c
+++ b/chncpu/chncpu.c
@@ -127,26 +127,27 @@ CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(void)
127127
128128 env->appbinReader = malloc(sizeof(CH4Reader));
129129
130- // bindOpFuncTable
130+ // FuncTables
131131 for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){
132132 env->bindOpFuncTable[i] = NULL;
133133 }
134- env->bindOpFuncTable[0x02] = CHNCPU_Op_LIMM_BindOperand;
135- env->bindOpFuncTable[0x10] = CHNCPU_Op_TernaryReg_BindOperand;
136-
137- // execOpFuncTable
138- for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){
134+ for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){
139135 env->execOpFuncTable[i] = NULL;
140136 }
141- env->execOpFuncTable[0x02] = CHNCPU_Op_LIMM_Execute;
142- env->execOpFuncTable[0x10] = CHNCPU_Op_TernaryReg_Execute;
143-
144- // printOpFuncTable
145- for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){
137+ for(i = 0; i <= CHNCPU_OPECODE_MAX; i++){
146138 env->printOpFuncTable[i] = NULL;
147139 }
148- env->printOpFuncTable[0x02] = CHNCPU_Op_LIMM_PrintCode;
149- env->printOpFuncTable[0x10] = CHNCPU_Op_TernaryReg_PrintCode;
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;
144+
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+ }
150151
151152 env->errFlags = 0;
152153
@@ -191,6 +192,7 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
191192 CHNCPU_OpTag *op;
192193 int breakFlag = 0;
193194 int retv;
195+ unsigned int prefix = 0;
194196
195197 // env->appbinReaderから読み込んで、実行可能な状態にする。
196198 // これはコンパイラ版におけるコンパイルに相当する。
@@ -211,12 +213,26 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
211213 case 0x00:
212214 // NOP
213215 puts("NOP();\n");
216+ // メモリには追加しない
214217 mindex--;
215218 break;
219+ case 0x2F:
220+ // Prefix
221+ opcode = CH4Reader_ReadNextAsUINT(env->appbinReader);
222+ printf("Prefix-%X\n", opcode);
223+ if(opcode > CHNCPU_PREFIX_MAX){
224+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
225+ breakFlag = 1;
226+ break;
227+ }
228+ prefix |= (0x01 << opcode);
229+ // メモリには追加しない
230+ mindex--;
231+ break;
216232 default:
217233 // ごく一部の特殊な命令以外は、命令テーブルを参照する
218234 if(opcode <= CHNCPU_OPECODE_MAX && env->bindOpFuncTable[opcode]){
219- retv = env->bindOpFuncTable[opcode](env, op);
235+ retv = env->bindOpFuncTable[opcode](env, op, prefix);
220236 if(retv){
221237 opcode = CHNCPU_OPCODE_INVALID;
222238 }
@@ -228,6 +244,7 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
228244 op->opCode = CHNCPU_OPCODE_INVALID;
229245 breakFlag = 1;
230246 }
247+ prefix = 0;
231248 break;
232249 }
233250 if(env->appbinReader->errorFlags){
@@ -258,6 +275,9 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
258275 if(env->errFlags & CHNCPU_ERR_C_INVALID_BIN){
259276 puts("INVALID-C: Invalid binary.");
260277 }
278+ if(env->errFlags & CHNCPU_ERR_C_PREFIX){
279+ puts("INVALID-C: Invalid prefix.");
280+ }
261281 }
262282 return 0;
263283 }
@@ -296,6 +316,9 @@ int CHNCPU_Execute(CHNCPU_RuntimeEnvironment *env)
296316 if(env->errFlags & CHNCPU_ERR_X_INTERNAL){
297317 puts("INVALID-X: Internal error.");
298318 }
319+ if(env->errFlags & CHNCPU_ERR_X_TRUNCATED_VALUE){
320+ puts("INVALID-X: Truncated value without prefix 2F-0.");
321+ }
299322 }
300323
301324 CHNCPU_DumpIReg(env);
@@ -348,4 +371,3 @@ int CHNCPU_CHK_IsAvailableBits(CHNCPU_RuntimeEnvironment *env, ch4_uint bits)
348371 }
349372 return 1;
350373 }
351-
--- a/chncpu/chncpu.h
+++ b/chncpu/chncpu.h
@@ -21,15 +21,25 @@
2121 #define CHNCPU_LENGTH_OF_MAIN_MEMORY (64 * 1024) // per Op
2222 #define CHNCPU_SIZE_APPBIN (1024 * 1024 * 1)
2323 #define CHNCPU_OPCODE_INVALID (-1)
24+#define CHNCPU_PREFIX_MAX 31
2425
25-#define CHNCPU_ERR_C_REGNUM 1
26-#define CHNCPU_ERR_C_BITS 2
27-#define CHNCPU_ERR_C_OPCODE 4
28-#define CHNCPU_ERR_C_EXPRESSION 8
29-#define CHNCPU_ERR_C_INTERNAL 16
30-#define CHNCPU_ERR_C_INVALID_BIN 32
26+// ErrorCode at compile
27+#define CHNCPU_ERR_C_REGNUM 0x01
28+#define CHNCPU_ERR_C_BITS 0x02
29+#define CHNCPU_ERR_C_OPCODE 0x04
30+#define CHNCPU_ERR_C_EXPRESSION 0x08
31+#define CHNCPU_ERR_C_INTERNAL 0x10
32+#define CHNCPU_ERR_C_INVALID_BIN 0x20
33+#define CHNCPU_ERR_C_PREFIX 0x40
3134
32-#define CHNCPU_ERR_X_INTERNAL 1
35+// ErrorCode in execution
36+#define CHNCPU_ERR_X_INTERNAL 0x01
37+#define CHNCPU_ERR_X_TRUNCATED_VALUE 0x02
38+
39+// (2F)Prefix
40+#define CHNCPU_PREFIX_ALLOW_TRUNCATE 0x01
41+
42+#define CHNCPU_PREFIX_MASK_Op_TernaryRegBitwise (CHNCPU_PREFIX_ALLOW_TRUNCATE)
3343
3444 typedef struct CHNCPU_OP_TAG CHNCPU_OpTag;
3545 struct CHNCPU_OP_TAG {
@@ -39,7 +49,7 @@ struct CHNCPU_OP_TAG {
3949
4050 typedef struct CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment;
4151 struct CHNCPU_RUN_TIME_ENVIRONMENT {
42- int (*bindOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
52+ int (*bindOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
4353 int (*execOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
4454 int (*printOpFuncTable[CHNCPU_OPECODE_MAX])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
4555 int iReg[CHNCPU_NUMBER_OF_IREG];
@@ -63,10 +73,10 @@ void CHNCPU_DumpIReg(CHNCPU_RuntimeEnvironment *env);
6373 int CHNCPU_CHK_IsAvailableBits(CHNCPU_RuntimeEnvironment *env, ch4_uint bits);
6474
6575 // @opcode.c
66-int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
76+int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
6777 int CHNCPU_Op_LIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
6878 int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
69-int CHNCPU_Op_TernaryReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
70-int CHNCPU_Op_TernaryReg_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
79+int CHNCPU_Op_TernaryRegBitwise_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
80+int CHNCPU_Op_TernaryRegBitwise_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
7181 int CHNCPU_Op_TernaryReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
7282 #endif
--- a/chncpu/opcode.c
+++ b/chncpu/opcode.c
@@ -18,7 +18,7 @@ struct CHNCPU_OP_CACHE_LIMM {
1818 ch4_uint r;
1919 ch4_uint bit;
2020 };
21-int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
21+int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
2222 {
2323 CHNCPU_OpCache_LIMM *opCache;
2424
@@ -30,7 +30,7 @@ int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
3030 opCache->bit = CH4Reader_ReadNextAsUINT(env->appbinReader);
3131
3232 CHNCPU_Op_LIMM_PrintCode(env, op, stdout);
33-
33+
3434 if(opCache->r >= CHNCPU_NUMBER_OF_IREG){
3535 env->errFlags |= CHNCPU_ERR_C_REGNUM;
3636 return -1;
@@ -42,6 +42,10 @@ int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
4242 env->errFlags |= CHNCPU_ERR_C_EXPRESSION;
4343 return -1;
4444 }
45+ if(prefix != 0){
46+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
47+ return -1;
48+ }
4549 return 0;
4650 }
4751 int CHNCPU_Op_LIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
@@ -72,7 +76,10 @@ int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, F
7276 // Ternary Register Operation
7377 //
7478
75-// 10 CP/OR
79+// Bitwise
80+// 10 CP/OR
81+// 11 XOR
82+// 12 AND
7683
7784 typedef struct CHNCPU_OP_CACHE_TERNARY_REG CHNCPU_OpCache_TernaryReg;
7885 struct CHNCPU_OP_CACHE_TERNARY_REG {
@@ -80,8 +87,9 @@ struct CHNCPU_OP_CACHE_TERNARY_REG {
8087 ch4_uint r1;
8188 ch4_uint r2;
8289 ch4_uint bit;
90+ unsigned int prefix;
8391 };
84-int CHNCPU_Op_TernaryReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
92+int CHNCPU_Op_TernaryRegBitwise_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
8593 {
8694 CHNCPU_OpCache_TernaryReg *opCache;
8795
@@ -92,6 +100,7 @@ int CHNCPU_Op_TernaryReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTa
92100 opCache->r2 = CH4Reader_ReadNextAsUINT(env->appbinReader);
93101 opCache->r0 = CH4Reader_ReadNextAsUINT(env->appbinReader);
94102 opCache->bit = CH4Reader_ReadNextAsUINT(env->appbinReader);
103+ opCache->prefix = prefix;
95104
96105 CHNCPU_Op_TernaryReg_PrintCode(env, op, stdout);
97106
@@ -104,36 +113,45 @@ int CHNCPU_Op_TernaryReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTa
104113 if(!CHNCPU_CHK_IsAvailableBits(env, opCache->bit)){
105114 return -1;
106115 }
116+ if((prefix | CHNCPU_PREFIX_MASK_Op_TernaryRegBitwise) != CHNCPU_PREFIX_MASK_Op_TernaryRegBitwise){
117+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
118+ return -1;
119+ }
107120 return 0;
108121 }
109-int CHNCPU_Op_TernaryReg_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
122+int CHNCPU_Op_TernaryRegBitwise_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
110123 {
111124 CHNCPU_OpCache_TernaryReg *opCache;
125+ int opCode;
126+ ch4_sint result;
112127
113128 opCache = op->opCache;
129+ opCode = op->opCode;
114130
115131 CHNCPU_Op_TernaryReg_PrintCode(env, op, stdout);
116132
117- switch (op->opCode) {
118- case 0x10: // CP/OR
119- if(opCache->r1 == opCache->r2){
120- // CP
121- env->iReg[opCache->r0] = env->iReg[opCache->r1] & (0xFFFFFFFF >> (32 - opCache->bit));
122- env->iRegBits[opCache->r0] = opCache->bit;
123- } else{
124- // OR
125- env->errFlags |= CHNCPU_ERR_X_INTERNAL;
126- return -1;
127- }
128- break;
129- default:
130- env->errFlags |= CHNCPU_ERR_X_INTERNAL;
131- return -1;
133+ if(opCode == 0x10){
134+ if(opCache->r1 == opCache->r2){
135+ // CP
136+ result = env->iReg[opCache->r1];
137+ } else{
138+ // OR
139+ result = env->iReg[opCache->r1] | env->iReg[opCache->r2];
140+ }
141+ } else if(opCode == 0x11){
142+ // XOR
143+ result = env->iReg[opCache->r1] ^ env->iReg[opCache->r2];
144+ } else{
145+ env->errFlags |= CHNCPU_ERR_X_INTERNAL;
146+ return -1;
132147 }
133148
134- if(!CHNCPU_CHK_IsAvailableBits(env, opCache->bit)){
135- return -1;
136- }
149+ env->iReg[opCache->r0] = result & (0xFFFFFFFF >> (32 - opCache->bit));
150+ if(!(opCache->prefix & CHNCPU_PREFIX_ALLOW_TRUNCATE) && env->iReg[opCache->r0] != result){
151+ env->errFlags |= CHNCPU_ERR_X_TRUNCATED_VALUE;
152+ return -1;
153+ }
154+ env->iRegBits[opCache->r0] = opCache->bit;
137155
138156 return 0;
139157 }
--- a/chncpu/test.hex
+++ b/chncpu/test.hex
@@ -1 +1 @@
1-2 E123 0 4 2 E365 1 88 90 1 1 2 4
\ No newline at end of file
1+2 E123 0 90 2 E365 1 90 91 0 1 2 90
\ No newline at end of file