• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

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

Commit MetaInfo

修订版43e8a7ba3c37c430d812263859883f2d1f4f5caf (tree)
时间2014-06-22 19:52:53
作者hikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

LB,PLIMMを整備。
ちなみにテストコードは無限ループなので注意。

更改概述

差异

--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
1+Breakpoints_v2.xcbkptlist
--- a/chncpu.xcodeproj/project.pbxproj
+++ b/chncpu.xcodeproj/project.pbxproj
@@ -11,6 +11,7 @@
1111 17478CFE193E2DEB00293791 /* test.hex in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17478CFD193E2ADA00293791 /* test.hex */; };
1212 17478D01193F3D0000293791 /* ch4.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D00193F3D0000293791 /* ch4.c */; };
1313 17478D041940B66700293791 /* opcode.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D031940B66700293791 /* opcode.c */; };
14+ 1786B0941956C18800FD4F7B /* label.c in Sources */ = {isa = PBXBuildFile; fileRef = 1786B0931956C18800FD4F7B /* label.c */; };
1415 17F95C01194C8DA70064E1B6 /* bios.c in Sources */ = {isa = PBXBuildFile; fileRef = 17F95C00194C8DA70064E1B6 /* bios.c */; };
1516 /* End PBXBuildFile section */
1617
@@ -35,6 +36,7 @@
3536 17478D00193F3D0000293791 /* ch4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ch4.c; sourceTree = "<group>"; usesTabs = 1; };
3637 17478D02193F586100293791 /* ch4.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ch4.h; sourceTree = "<group>"; usesTabs = 1; };
3738 17478D031940B66700293791 /* opcode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = opcode.c; sourceTree = "<group>"; usesTabs = 1; };
39+ 1786B0931956C18800FD4F7B /* label.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = label.c; sourceTree = "<group>"; };
3840 17F95C00194C8DA70064E1B6 /* bios.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bios.c; sourceTree = "<group>"; };
3941 /* End PBXFileReference section */
4042
@@ -76,6 +78,7 @@
7678 17478CFD193E2ADA00293791 /* test.hex */,
7779 17478D031940B66700293791 /* opcode.c */,
7880 17F95C00194C8DA70064E1B6 /* bios.c */,
81+ 1786B0931956C18800FD4F7B /* label.c */,
7982 );
8083 path = chncpu;
8184 sourceTree = "<group>";
@@ -131,6 +134,7 @@
131134 isa = PBXSourcesBuildPhase;
132135 buildActionMask = 2147483647;
133136 files = (
137+ 1786B0941956C18800FD4F7B /* label.c in Sources */,
134138 17F95C01194C8DA70064E1B6 /* bios.c in Sources */,
135139 17478D041940B66700293791 /* opcode.c in Sources */,
136140 17478CF4193E2A4900293791 /* chncpu.c in Sources */,
--- a/chncpu/bios.c
+++ b/chncpu/bios.c
@@ -9,6 +9,21 @@
99 #include "chncpu.h"
1010 #include <stdio.h>
1111
12+CHNCPU_BIOS *CHNCPU_CreateBIOS(void)
13+{
14+ CHNCPU_BIOS *bios;
15+
16+ bios = malloc(sizeof(CHNCPU_BIOS));
17+ if(!bios){
18+ puts("CHNCPU_CreateBIOS: malloc error, abort.\n");
19+ exit(EXIT_FAILURE);
20+ }
21+
22+ CHNCPU_BIOS_Init(bios);
23+
24+ return bios;
25+}
26+
1227 int CHNCPU_BIOS_Init(CHNCPU_BIOS *bios)
1328 {
1429 int i;
@@ -33,8 +48,8 @@ int CHNCPU_BIOS_Init(CHNCPU_BIOS *bios)
3348 // putcReg
3449 //
3550
36-typedef struct CHNCPU_BIOS_OP_CACHE_LIMM CHNCPU_BIOS_OpCache_putcReg;
37-struct CHNCPU_BIOS_OP_CACHE_LIMM {
51+typedef struct _CHNCPU_BIOS_OP_CACHE_LIMM CHNCPU_BIOS_OpCache_putcReg;
52+struct _CHNCPU_BIOS_OP_CACHE_LIMM {
3853 ch4_uint r;
3954 };
4055 int CHNCPU_BIOS_Op_putcReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
--- a/chncpu/chncpu.c
+++ b/chncpu/chncpu.c
@@ -102,36 +102,6 @@ int decodeHexString(char *src0, char *src1, unsigned char *dst0, unsigned char *
102102 return wlen;
103103 }
104104
105-CHNCPU_OpTableSet *CHNCPU_CreateOpTableSet(void)
106-{
107- CHNCPU_OpTableSet *opSet;
108-
109- opSet = malloc(sizeof(CHNCPU_OpTableSet));
110- if(!opSet){
111- puts("CHNCPU_CreateOpTableSet: malloc error, abort.\n");
112- exit(EXIT_FAILURE);
113- }
114-
115- CHNCPU_Op_Init(opSet);
116-
117- return opSet;
118-}
119-
120-CHNCPU_BIOS *CHNCPU_CreateBIOS(void)
121-{
122- CHNCPU_BIOS *bios;
123-
124- bios = malloc(sizeof(CHNCPU_BIOS));
125- if(!bios){
126- puts("CHNCPU_CreateBIOS: malloc error, abort.\n");
127- exit(EXIT_FAILURE);
128- }
129-
130- CHNCPU_BIOS_Init(bios);
131-
132- return bios;
133-}
134-
135105 CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(CHNCPU_OpTableSet *opSet, CHNCPU_BIOS *bios)
136106 {
137107 CHNCPU_RuntimeEnvironment *env;
@@ -154,6 +124,13 @@ CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(CHNCPU_OpTableSet *op
154124 for(i = 0; i < CHNCPU_LENGTH_OF_MAIN_MEMORY; i++){
155125 env->mainmemory[i].opCode = CHNCPU_OPCODE_INVALID;
156126 }
127+ for(i = 0; i < CHNCPU_NUMBER_OF_PREG; i++){
128+ env->pReg[i].type = 0;
129+ env->pReg[i].mindex = 0;
130+ }
131+
132+ // ラベル管理初期化
133+ env->labelSet = CHNCPU_CreateLabelSet();
157134
158135 // 実行バイナリ関連初期化
159136 env->appbin0 = malloc(CHNCPU_SIZE_APPBIN);
@@ -199,47 +176,58 @@ int CHNCPU_LoadBinaryFromHexStringFilePath(CHNCPU_RuntimeEnvironment *env, const
199176 int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
200177 {
201178 ch4_uint opcode = 0;
202- int mindex;
203179 CHNCPU_OpTag *op;
204180 unsigned int prefix = 0;
205181 CHNCPU_OpTableSet *opSet = env->opSet;
206182
207183 // env->appbinReaderから読み込んで、実行可能な状態にする。
208184 // これはコンパイラ版におけるコンパイルに相当する。
209- //printf("< Beginning of binary > \n");
210- mindex = 0;
211- for(mindex = 0; mindex < CHNCPU_LENGTH_OF_MAIN_MEMORY; mindex++){
185+#if DEBUG_PRINT_OP_EXECUTING
186+ puts("< Start preparing for execution >");
187+#endif
188+ env->currentIndex = 0;
189+ for(env->currentIndex = 0; env->currentIndex < CHNCPU_LENGTH_OF_MAIN_MEMORY; env->currentIndex++){
212190 opcode = CH4Reader_ReadNextAsUINT(env->appbinReader);
213191 if(CH4Reader_IsEndOfBinary(env->appbinReader)){
214192 break;
215193 }
216- //printf("(%02X) ", opcode);
217-
218- op = &env->mainmemory[mindex];
219- op->opCode = opcode;
220- switch(opcode){
194+ switch(opcode){
221195 case 0x00:
222196 // NOP
223- mindex--; // メモリには追加しない
197+#if DEBUG_PRINT_OP_BINDING
198+ puts("NOP()");
199+#endif
200+ env->currentIndex--; // メモリには追加しない
224201 break;
225202 case 0x2F:
226203 // Prefix
227204 opcode = CH4Reader_ReadNextAsUINT(env->appbinReader);
228- //printf("Prefix-%X\n", opcode);
205+#if DEBUG_PRINT_OP_BINDING
206+ printf("Prefix-%X\n", opcode);
207+#endif
229208 if(opcode > CHNCPU_PREFIX_MAX){
230209 env->errFlags |= CHNCPU_ERR_C_PREFIX;
231210 break;
232211 }
233212 prefix |= (0x01 << opcode);
234- mindex--; // メモリには追加しない
213+ env->currentIndex--; // メモリには追加しない
235214 break;
236215 default:
237216 // ごく一部の特殊な命令以外は、命令テーブルを参照する
217+ op = &env->mainmemory[env->currentIndex];
218+ op->opCode = opcode;
238219 if(opcode <= CHNCPU_OPECODE_MAX && opSet->bindFuncTable[opcode]){
239220 opSet->bindFuncTable[opcode](env, op, prefix);
240221 } else{
241222 env->errFlags |= CHNCPU_ERR_C_OPCODE;
242223 }
224+#if DEBUG_PRINT_OP_EXECUTING
225+ if(op->opCode <= CHNCPU_OPECODE_MAX && opSet->printFuncTable[op->opCode]){
226+ opSet->printFuncTable[op->opCode](env, op, stdout);
227+ } else{
228+ printf("Unknown op: 0x%X", op->opCode);
229+ }
230+#endif
243231 prefix = 0;
244232 break;
245233 }
@@ -250,11 +238,12 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
250238 break;
251239 }
252240 }
253- if(mindex >= CHNCPU_LENGTH_OF_MAIN_MEMORY && !CH4Reader_IsEndOfBinary(env->appbinReader)){
241+ if(env->currentIndex >= CHNCPU_LENGTH_OF_MAIN_MEMORY && !CH4Reader_IsEndOfBinary(env->appbinReader)){
254242 env->errFlags |= CHNCPU_ERR_C_INTERNAL;
255243 puts("INVALID-C: Internal error (low on memory).");
256244 }
257245 if(env->errFlags){
246+ printf(">>> Compile error in label:%d\n", env->currentLabel);
258247 if(env->errFlags & CHNCPU_ERR_C_REGNUM){
259248 puts("INVALID-C: Invalid register number.");
260249 }
@@ -273,7 +262,12 @@ int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env)
273262 if(env->errFlags & CHNCPU_ERR_C_PREFIX){
274263 puts("INVALID-C: Invalid prefix.");
275264 }
265+ if(env->errFlags & CHNCPU_ERR_C_DUPLICATED_LB){
266+ puts("INVALID-C: Duplicated LabelID.");
267+ }
276268 }
269+ env->currentIndex = 0;
270+ env->currentLabel = 0;
277271 return 0;
278272 }
279273
@@ -286,8 +280,9 @@ int CHNCPU_Execute(CHNCPU_RuntimeEnvironment *env)
286280 if(env->errFlags){
287281 puts(">>> Can't execute binary because of there is some error.");
288282 }
289-
290- //puts("< Beginning of execution >");
283+#if DEBUG_PRINT_OP_EXECUTING
284+ puts("< Start execution >");
285+#endif
291286 for(; env->currentIndex < CHNCPU_LENGTH_OF_MAIN_MEMORY; env->currentIndex++){
292287 op = &env->mainmemory[env->currentIndex];
293288 if(op->opCode == CHNCPU_OPCODE_INVALID){
@@ -320,10 +315,13 @@ int CHNCPU_Execute(CHNCPU_RuntimeEnvironment *env)
320315 if(env->errFlags & CHNCPU_ERR_X_TRUNCATED_VALUE){
321316 puts("INVALID-X: Truncated value without prefix 2F-0.");
322317 }
318+ if(env->errFlags & CHNCPU_ERR_X_LABEL_NOT_FOUND){
319+ puts("INVALID-X: Label not found.");
320+ }
323321 }
324322
325323 #if DEBUG_PRINT_IREG_AFTER_EXECUTION
326- puts("\n>>> End of execution");
324+ puts("\n< End of execution >");
327325 CHNCPU_DumpIReg(env);
328326 #endif
329327
--- a/chncpu/chncpu.h
+++ b/chncpu/chncpu.h
@@ -25,6 +25,7 @@
2525 #define CHNCPU_BIOS_OP_MAX 0x00
2626 #define CHNCPU_BITS_MAX 32
2727 #define CHNCPU_NUMBER_OF_IREG 64
28+#define CHNCPU_NUMBER_OF_PREG 64
2829 #define CHNCPU_LENGTH_OF_MAIN_MEMORY (64 * 1024) // per Op
2930 #define CHNCPU_SIZE_APPBIN (1024 * 1024 * 1)
3031 #define CHNCPU_OPCODE_INVALID (-1)
@@ -38,20 +39,53 @@
3839 #define CHNCPU_ERR_C_INTERNAL 0x10
3940 #define CHNCPU_ERR_C_INVALID_BIN 0x20
4041 #define CHNCPU_ERR_C_PREFIX 0x40
42+#define CHNCPU_ERR_C_DUPLICATED_LB 0x80
4143
4244 // ErrorCode in execution
4345 #define CHNCPU_ERR_X_INTERNAL 0x01
4446 #define CHNCPU_ERR_X_TRUNCATED_VALUE 0x02
47+#define CHNCPU_ERR_X_LABEL_NOT_FOUND 0x04
4548
4649 // (2F)Prefix
4750 #define CHNCPU_PREFIX_ALLOW_TRUNCATE 0x01
4851
4952 #define CHNCPU_PREFIX_MASK_Op_TernaryRegBitwise (CHNCPU_PREFIX_ALLOW_TRUNCATE)
5053
54+// Type of Pointer
55+#define CHNCPU_PType_Undefined 0
56+#define CHNCPU_PType_Code (-1)
57+// type 型名 説明
58+// -1 Code
59+// 0x00 Undefined
60+// 0x01 VPtr
61+// 0x02 SINT8 signed char.
62+// 0x03 UINT8
63+// 0x04 SINT16 short.
64+// 0x05 UINT16
65+// 0x06 SINT32 sint
66+// 0x07 UINT32 uint
67+// 0x08 SINT4
68+// 0x09 UINT4
69+// 0x0A SINT2
70+// 0x0B UINT2
71+// 0x0C SINT1 bool.代入できるのは0か-1のみ.
72+// 0x0D UINT1
73+// 0x0E SINT12
74+// 0x0F UINT12
75+// 0x10 SINT20
76+// 0x11 UINT20
77+// 0x12 SINT24
78+// 0x13 UINT24
79+// 0x14 SINT28
80+// 0x15 UINT28
81+
5182 typedef struct _CHNCPU_OP_TAG CHNCPU_OpTag;
5283 typedef struct _CHNCPU_BIOS CHNCPU_BIOS;
5384 typedef struct _CHNCPU_OP_TABLE_SET CHNCPU_OpTableSet;
5485 typedef struct _CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment;
86+typedef struct _CHNCPU_LABEL_TAG CHNCPU_LabelTag;
87+typedef struct _CHNCPU_LABEL_SET CHNCPU_LabelSet;
88+typedef struct _CHNCPU_POINTER_TAG CHNCPU_PointerTag;
5589
5690
5791 struct _CHNCPU_OP_TAG {
@@ -71,24 +105,40 @@ struct _CHNCPU_OP_TABLE_SET {
71105 int (*printFuncTable[CHNCPU_OPECODE_MAX + 1])(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
72106 };
73107
108+struct _CHNCPU_LABEL_TAG {
109+ int labelID;
110+ int opt; // 0:local(R3F Only) 1:public(can be saved to memory, pRegs)
111+ int mindex;
112+ CHNCPU_LabelTag *next;
113+};
114+
115+struct _CHNCPU_LABEL_SET {
116+ CHNCPU_LabelTag top;
117+};
118+
119+struct _CHNCPU_POINTER_TAG {
120+ int type; // CHNCPU_PType_xx
121+ int mindex;
122+};
123+
74124 struct _CHNCPU_RUN_TIME_ENVIRONMENT {
75125 CHNCPU_BIOS *bios;
76126 CHNCPU_OpTableSet *opSet;
127+ CHNCPU_LabelSet *labelSet;
77128 int iReg[CHNCPU_NUMBER_OF_IREG];
78129 int iRegBits[CHNCPU_NUMBER_OF_IREG];
130+ CHNCPU_PointerTag pReg[CHNCPU_NUMBER_OF_PREG];
79131 CHNCPU_OpTag mainmemory[CHNCPU_LENGTH_OF_MAIN_MEMORY];
80132 unsigned char *appbin0;
81133 int appbinsize;
82134 CH4Reader *appbinReader;
83135 unsigned int errFlags;
84136 int currentIndex;
85-
137+ int currentLabel;
86138 };
87139
88140 // @chncpu.c
89141 int decodeHexString(char *src0, char *src1, unsigned char *dst0, unsigned char *dst1);
90-CHNCPU_OpTableSet *CHNCPU_CreateOpTableSet(void);
91-CHNCPU_BIOS *CHNCPU_CreateBIOS(void);
92142 CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(CHNCPU_OpTableSet *opSet, CHNCPU_BIOS *bios);
93143 int CHNCPU_LoadBinaryFromHexStringFilePath(CHNCPU_RuntimeEnvironment *env, const char *path);
94144 int CHNCPU_PrepareBinaryForExecution(CHNCPU_RuntimeEnvironment *env);
@@ -97,11 +147,19 @@ void CHNCPU_DumpAppBin(CHNCPU_RuntimeEnvironment *env);
97147 void CHNCPU_DumpIReg(CHNCPU_RuntimeEnvironment *env);
98148 int CHNCPU_CHK_IsAvailableBits(CHNCPU_RuntimeEnvironment *env, ch4_uint bits);
99149 int CHNCPU_AdjustValueForBit(CHNCPU_RuntimeEnvironment *env, int *value, ch4_uint bit, int prefix);
150+
100151 // @opcode.c
152+CHNCPU_OpTableSet *CHNCPU_CreateOpTableSet(void);
101153 int CHNCPU_Op_Init(CHNCPU_OpTableSet *env);
154+int CHNCPU_Op_LB_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
155+int CHNCPU_Op_LB_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
156+int CHNCPU_Op_LB_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
102157 int CHNCPU_Op_LIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
103158 int CHNCPU_Op_LIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
104159 int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
160+int CHNCPU_Op_PLIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
161+int CHNCPU_Op_PLIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
162+int CHNCPU_Op_PLIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
105163 int CHNCPU_Op_CALLBIOS_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
106164 int CHNCPU_Op_CALLBIOS_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
107165 int CHNCPU_Op_CALLBIOS_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
@@ -111,8 +169,15 @@ int CHNCPU_Op_TernaryRegArithmetic_Execute(CHNCPU_RuntimeEnvironment *env, CHNCP
111169 int CHNCPU_Op_TernaryReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
112170
113171 // @bios.c
172+CHNCPU_BIOS *CHNCPU_CreateBIOS(void);
114173 int CHNCPU_BIOS_Init(CHNCPU_BIOS *bios);
115174 int CHNCPU_BIOS_Op_putcReg_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
116175 int CHNCPU_BIOS_Op_putcReg_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
117176 int CHNCPU_BIOS_Op_putcReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
177+
178+// @label.c
179+CHNCPU_LabelSet *CHNCPU_CreateLabelSet(void);
180+int CHNCPU_Label_Add(CHNCPU_LabelSet *lbSet, int mindex, int labelID, int opt);
181+CHNCPU_LabelTag *CHNCPU_Label_GetTagFromLabelID(CHNCPU_LabelSet *lbSet, int labelID);
182+
118183 #endif
--- /dev/null
+++ b/chncpu/label.c
@@ -0,0 +1,74 @@
1+//
2+// label.c
3+// chncpu
4+//
5+// Created by 西田 耀 on 2014/06/22.
6+// Copyright (c) 2014年 CHNOSProject. All rights reserved.
7+//
8+
9+#include "chncpu.h"
10+
11+CHNCPU_LabelSet *CHNCPU_CreateLabelSet(void)
12+{
13+ CHNCPU_LabelSet *lbSet;
14+
15+ lbSet = malloc(sizeof(CHNCPU_LabelSet));
16+ if(!lbSet){
17+ puts("CHNCPU_CreateLabelSet: malloc error, abort.\n");
18+ exit(EXIT_FAILURE);
19+ }
20+
21+ lbSet->top.labelID = 0;
22+ lbSet->top.opt = 0;
23+ lbSet->top.mindex = 0;
24+ lbSet->top.next = NULL;
25+
26+ return lbSet;
27+}
28+
29+int CHNCPU_Label_Add(CHNCPU_LabelSet *lbSet, int mindex, int labelID, int opt)
30+{
31+ // retv: false:succeeded true:failed
32+ CHNCPU_LabelTag *t = &lbSet->top;
33+ for(;;){
34+ if(t->labelID == labelID){
35+ // 既に存在するラベルID
36+ return -1;
37+ }
38+ if(t->next == NULL){
39+ // 最後のタグ
40+ break;
41+ }
42+ t = t->next;
43+ }
44+
45+ t->next = malloc(sizeof(CHNCPU_LabelTag));
46+ if(t->next){
47+ t = t->next;
48+ t->mindex = mindex;
49+ t->labelID = labelID;
50+ t->opt = opt;
51+ t->next = NULL;
52+ } else{
53+ return -1;
54+ }
55+
56+ return 0;
57+}
58+
59+CHNCPU_LabelTag *CHNCPU_Label_GetTagFromLabelID(CHNCPU_LabelSet *lbSet, int labelID)
60+{
61+ CHNCPU_LabelTag *t = &lbSet->top;
62+ for(;;){
63+ if(t->labelID == labelID){
64+ return t;
65+ }
66+ if(t->next == NULL){
67+ // 最後のタグ
68+ break;
69+ }
70+ t = t->next;
71+ }
72+ return NULL;
73+}
74+
--- a/chncpu/opcode.c
+++ b/chncpu/opcode.c
@@ -8,6 +8,21 @@
88
99 #include "chncpu.h"
1010
11+CHNCPU_OpTableSet *CHNCPU_CreateOpTableSet(void)
12+{
13+ CHNCPU_OpTableSet *opSet;
14+
15+ opSet = malloc(sizeof(CHNCPU_OpTableSet));
16+ if(!opSet){
17+ puts("CHNCPU_CreateOpTableSet: malloc error, abort.\n");
18+ exit(EXIT_FAILURE);
19+ }
20+
21+ CHNCPU_Op_Init(opSet);
22+
23+ return opSet;
24+}
25+
1126 int CHNCPU_Op_Init(CHNCPU_OpTableSet *opSet)
1227 {
1328 int i;
@@ -19,11 +34,21 @@ int CHNCPU_Op_Init(CHNCPU_OpTableSet *opSet)
1934 opSet->printFuncTable[i] = NULL;
2035 }
2136
37+ // LB
38+ opSet->bindFuncTable[0x01] = CHNCPU_Op_LB_BindOperand;
39+ opSet->execFuncTable[0x01] = CHNCPU_Op_LB_Execute;
40+ opSet->printFuncTable[0x01] = CHNCPU_Op_LB_PrintCode;
41+
2242 // LIMM
2343 opSet->bindFuncTable[0x02] = CHNCPU_Op_LIMM_BindOperand;
2444 opSet->execFuncTable[0x02] = CHNCPU_Op_LIMM_Execute;
2545 opSet->printFuncTable[0x02] = CHNCPU_Op_LIMM_PrintCode;
2646
47+ // LIMM
48+ opSet->bindFuncTable[0x03] = CHNCPU_Op_PLIMM_BindOperand;
49+ opSet->execFuncTable[0x03] = CHNCPU_Op_PLIMM_Execute;
50+ opSet->printFuncTable[0x03] = CHNCPU_Op_PLIMM_PrintCode;
51+
2752 // CALLBIOS
2853 opSet->bindFuncTable[0x05] = CHNCPU_Op_CALLBIOS_BindOperand;
2954 opSet->execFuncTable[0x05] = CHNCPU_Op_CALLBIOS_Execute;
@@ -51,11 +76,61 @@ int CHNCPU_Op_Init(CHNCPU_OpTableSet *opSet)
5176 }
5277
5378 //
79+// 01 LB
80+//
81+typedef struct _CHNCPU_OP_CACHE_LB CHNCPU_OpCache_LB;
82+struct _CHNCPU_OP_CACHE_LB {
83+ ch4_uint labelID;
84+ ch4_uint opt;
85+};
86+int CHNCPU_Op_LB_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
87+{
88+ CHNCPU_OpCache_LB *opCache;
89+
90+ opCache = malloc(sizeof(CHNCPU_OpCache_LB));
91+ op->opCache = opCache;
92+
93+ opCache->labelID = CH4Reader_ReadNextAsUINT(env->appbinReader);
94+ opCache->opt = CH4Reader_ReadNextAsUINT(env->appbinReader);
95+
96+ env->currentLabel = opCache->labelID;
97+
98+ if(CHNCPU_Label_Add(env->labelSet, env->currentIndex, opCache->labelID, opCache->opt)){
99+ env->errFlags |= CHNCPU_ERR_C_DUPLICATED_LB;
100+ }
101+
102+ if(prefix != 0){
103+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
104+ return -1;
105+ }
106+ return 0;
107+}
108+int CHNCPU_Op_LB_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
109+{
110+ CHNCPU_OpCache_LB *opCache;
111+ opCache = op->opCache;
112+
113+ env->currentLabel = opCache->labelID;
114+
115+ return 0;
116+}
117+
118+int CHNCPU_Op_LB_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file)
119+{
120+ CHNCPU_OpCache_LB *opCache;
121+ opCache = op->opCache;
122+
123+ fprintf(file, "LB(id:0x%X, opt:%d);\n", opCache->labelID, opCache->opt);
124+
125+ return 0;
126+}
127+
128+//
54129 // 02 LIMM
55130 //
56131
57-typedef struct CHNCPU_OP_CACHE_LIMM CHNCPU_OpCache_LIMM;
58-struct CHNCPU_OP_CACHE_LIMM {
132+typedef struct _CHNCPU_OP_CACHE_LIMM CHNCPU_OpCache_LIMM;
133+struct _CHNCPU_OP_CACHE_LIMM {
59134 ch4_sint imm;
60135 ch4_uint r;
61136 ch4_uint bit;
@@ -105,12 +180,76 @@ int CHNCPU_Op_LIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, F
105180 CHNCPU_OpCache_LIMM *opCache;
106181
107182 opCache = op->opCache;
108- fprintf(file, "LIMM(imm:0x%X, r:%d, bit:%d);\n", opCache->imm, opCache->r, opCache->bit);
183+ fprintf(file, "LIMM(imm:0x%X, r:%02X, bit:%d);\n", opCache->imm, opCache->r, opCache->bit);
109184
110185 return 0;
111186 }
112187
113188 //
189+// 02 PLIMM
190+//
191+
192+typedef struct _CHNCPU_OP_CACHE_PLIMM CHNCPU_OpCache_PLIMM;
193+struct _CHNCPU_OP_CACHE_PLIMM {
194+ ch4_sint labelID;
195+ ch4_uint r;
196+};
197+int CHNCPU_Op_PLIMM_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
198+{
199+ CHNCPU_OpCache_PLIMM *opCache;
200+
201+ opCache = malloc(sizeof(CHNCPU_OpCache_PLIMM));
202+ op->opCache = opCache;
203+
204+ opCache->labelID = CH4Reader_ReadNextAsUINT(env->appbinReader);
205+ opCache->r = CH4Reader_ReadNextAsUINT(env->appbinReader);
206+
207+ if(opCache->r >= CHNCPU_NUMBER_OF_PREG){
208+ env->errFlags |= CHNCPU_ERR_C_REGNUM;
209+ return -1;
210+ }
211+ if(prefix != 0){
212+ env->errFlags |= CHNCPU_ERR_C_PREFIX;
213+ return -1;
214+ }
215+ return 0;
216+}
217+int CHNCPU_Op_PLIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
218+{
219+ CHNCPU_OpCache_PLIMM *opCache;
220+ CHNCPU_LabelTag *label;
221+
222+ opCache = op->opCache;
223+
224+ label = CHNCPU_Label_GetTagFromLabelID(env->labelSet, opCache->labelID);
225+ if(!label){
226+ env->errFlags |= CHNCPU_ERR_X_LABEL_NOT_FOUND;
227+ return -1;
228+ }
229+
230+ env->pReg[opCache->r].mindex = label->mindex;
231+ env->pReg[opCache->r].type = CHNCPU_PType_Code;
232+
233+ if(opCache->r == 0x3F){
234+ if(env->pReg[0x3F].type == CHNCPU_PType_Code){
235+ env->currentIndex = env->pReg[0x3F].mindex - 1;
236+ }
237+ }
238+
239+ return 0;
240+}
241+
242+int CHNCPU_Op_PLIMM_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file)
243+{
244+ CHNCPU_OpCache_PLIMM *opCache;
245+
246+ opCache = op->opCache;
247+ fprintf(file, "PLIMM(labelID:0x%X, r:%02X);\n", opCache->labelID, opCache->r);
248+
249+ return 0;
250+}
251+
252+//
114253 // 05 CALLBIOS
115254 //
116255 int CHNCPU_Op_CALLBIOS_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
@@ -191,8 +330,8 @@ int CHNCPU_Op_CALLBIOS_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *o
191330 // 1A DIV
192331 // 1B MOD
193332
194-typedef struct CHNCPU_OP_CACHE_TERNARY_REG CHNCPU_OpCache_TernaryReg;
195-struct CHNCPU_OP_CACHE_TERNARY_REG {
333+typedef struct _CHNCPU_OP_CACHE_TERNARY_REG CHNCPU_OpCache_TernaryReg;
334+struct _CHNCPU_OP_CACHE_TERNARY_REG {
196335 ch4_uint r0;
197336 ch4_uint r1;
198337 ch4_uint r2;
@@ -345,7 +484,7 @@ int CHNCPU_Op_TernaryReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag
345484 fprintf(file, "(%02X)", op->opCode);
346485 }
347486
348- fprintf(file, "(r0:%d, r1:%d, r2:%d, bit:%d);\n", opCache->r0, opCache->r1, opCache->r2, opCache->bit);
487+ fprintf(file, "(r0:%d, r1:%02X, r2:%02X, bit:%02X);\n", opCache->r0, opCache->r1, opCache->r2, opCache->bit);
349488
350489 return 0;
351490 }
--- a/chncpu/test.hex
+++ b/chncpu/test.hex
@@ -1,5 +1,6 @@
11 2 a0 0 90
22 2 1 1 90
3-
3+1 1 0
44 5 0 0
5-94 0 1 0 90
\ No newline at end of file
5+94 0 1 0 90
6+3 1 bf