• 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

修訂afa134b89419f362494e8fefd6652f8f14ff05fe (tree)
時間2014-07-08 21:26:24
作者hikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

DATAの内部処理方式を変更した。

Change Summary

差異

--- a/chncpu.xcodeproj/project.pbxproj
+++ b/chncpu.xcodeproj/project.pbxproj
@@ -12,6 +12,7 @@
1212 17478D01193F3D0000293791 /* ch4.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D00193F3D0000293791 /* ch4.c */; };
1313 17478D041940B66700293791 /* opcode.c in Sources */ = {isa = PBXBuildFile; fileRef = 17478D031940B66700293791 /* opcode.c */; };
1414 1786B0941956C18800FD4F7B /* label.c in Sources */ = {isa = PBXBuildFile; fileRef = 1786B0931956C18800FD4F7B /* label.c */; };
15+ 17958C6F196C0E8000D7DBB4 /* data.c in Sources */ = {isa = PBXBuildFile; fileRef = 17958C6E196C0E8000D7DBB4 /* data.c */; };
1516 17DD55BB1967992200DA7D82 /* args.c in Sources */ = {isa = PBXBuildFile; fileRef = 17DD55BA1967992200DA7D82 /* args.c */; };
1617 17E5092A1967F7B00091A22A /* src.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17E509291967F7770091A22A /* src.txt */; };
1718 17E50932196AD91D0091A22A /* debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 17E50931196AD91D0091A22A /* debug.c */; };
@@ -41,6 +42,7 @@
4142 17478D02193F586100293791 /* ch4.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ch4.h; sourceTree = "<group>"; usesTabs = 1; };
4243 17478D031940B66700293791 /* opcode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = opcode.c; sourceTree = "<group>"; usesTabs = 1; };
4344 1786B0931956C18800FD4F7B /* label.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = label.c; sourceTree = "<group>"; };
45+ 17958C6E196C0E8000D7DBB4 /* data.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = data.c; sourceTree = "<group>"; };
4446 17DD55BA1967992200DA7D82 /* args.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = args.c; sourceTree = "<group>"; };
4547 17E509291967F7770091A22A /* src.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = src.txt; sourceTree = "<group>"; };
4648 17E50930196AD6BF0091A22A /* opcache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = opcache.h; sourceTree = "<group>"; };
@@ -87,6 +89,7 @@
8789 17478D02193F586100293791 /* ch4.h */,
8890 17E50930196AD6BF0091A22A /* opcache.h */,
8991 17478D031940B66700293791 /* opcode.c */,
92+ 17958C6E196C0E8000D7DBB4 /* data.c */,
9093 17F95C00194C8DA70064E1B6 /* bios.c */,
9194 1786B0931956C18800FD4F7B /* label.c */,
9295 17478CFD193E2ADA00293791 /* test.hex */,
@@ -147,6 +150,7 @@
147150 buildActionMask = 2147483647;
148151 files = (
149152 1786B0941956C18800FD4F7B /* label.c in Sources */,
153+ 17958C6F196C0E8000D7DBB4 /* data.c in Sources */,
150154 17E50932196AD91D0091A22A /* debug.c in Sources */,
151155 17F95C01194C8DA70064E1B6 /* bios.c in Sources */,
152156 17DD55BB1967992200DA7D82 /* args.c in Sources */,
--- a/chncpu/args.c
+++ b/chncpu/args.c
@@ -46,8 +46,8 @@ CHNCPU_VMArgs *bindVMArgs(int argc, const char *argv[])
4646 bdTag->pType = CHNCPU_VMArgs_GetNextAsSignedHexValue(argc, argv, &nextIndex);
4747 bdTag->flags = CHNCPU_VMArgs_GetNextAsUnsignedHexValue(argc, argv, &nextIndex);
4848 bdTag->filePath = CHNCPU_VMArgs_GetNextAsString(argc, argv, &nextIndex);
49- bdTag->dataOPTag = CHNCPU_Op_DATA_CreateDataFromFileAsPType(bdTag->filePath, bdTag->pType);
50- if(bdTag->pReg < 0 || bdTag->iReg < 0 || bdTag->flags < 0 || (bdTag->flags == 0 && !bdTag->dataOPTag)){
49+ bdTag->data = CHNCPU_DATA_CreateDataFromFileAsPType(bdTag->filePath, bdTag->pType);
50+ if(bdTag->pReg < 0 || bdTag->iReg < 0 || bdTag->flags < 0 || (bdTag->flags == 0 && !bdTag->data)){
5151 puts("Invalid data option (-bd) args.");
5252 argc = 0;
5353 continue;
@@ -84,10 +84,10 @@ int CHNCPU_VMArgs_BindDataToRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_VM
8484 continue;
8585 }
8686 // データサイズを指定されたIRegに格納
87- env->iReg[bdTag->iReg] = CHNCPU_Op_DATA_GetDataCount(bdTag->dataOPTag);
87+ env->iReg[bdTag->iReg] = bdTag->data->count;
8888 env->iRegBits[bdTag->iReg] = CHNCPU_BITS_MAX;
8989 // データタグを指定されたPRegに格納
90- CHNCPU_Op_DATA_AssignDataTagToPReg(env, bdTag->pReg, bdTag->dataOPTag);
90+ CHNCPU_DATA_AssignDataTagToPReg(env, bdTag->pReg, bdTag->data);
9191 }
9292
9393 return 0;
@@ -103,7 +103,7 @@ int CHNCPU_VMArgs_BindDataFromRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_
103103 if(bdTag->flags != 1){
104104 continue;
105105 }
106- CHNCPU_Op_DATA_WriteRawDataToFile(env->pReg[bdTag->pReg].opTag, bdTag->filePath, env->iReg[bdTag->iReg], bdTag->pType);
106+ CHNCPU_DATA_WriteRawDataToFile(env->pReg[bdTag->pReg].data, bdTag->filePath, env->iReg[bdTag->iReg], bdTag->pType);
107107 }
108108
109109 return 0;
--- a/chncpu/chncpu.c
+++ b/chncpu/chncpu.c
@@ -146,7 +146,7 @@ CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(CHNCPU_OpTableSet *op
146146 env->pReg[i].type = 0;
147147 env->pReg[i].mindex = 0;
148148 env->pReg[i].pindex = 0;
149- env->pReg[i].opTag = NULL;
149+ env->pReg[i].data = NULL;
150150 }
151151
152152 // ラベル管理初期化
--- a/chncpu/chncpu.h
+++ b/chncpu/chncpu.h
@@ -16,9 +16,9 @@
1616
1717 // DEBUG Flag
1818 #define DEBUG_PRINT_APPBIN_BEFORE_EXECUTION 0
19-#define DEBUG_PRINT_IREG_AFTER_EXECUTION 1
20-#define DEBUG_PRINT_PREG_AFTER_EXECUTION 1
21-#define DEBUG_PRINT_OP_BINDING 1
19+#define DEBUG_PRINT_IREG_AFTER_EXECUTION 0
20+#define DEBUG_PRINT_PREG_AFTER_EXECUTION 0
21+#define DEBUG_PRINT_OP_BINDING 0
2222 #define DEBUG_PRINT_OP_EXECUTING 0
2323
2424 // VM flags
@@ -82,6 +82,7 @@
8282 // Opcode
8383 #define CHNCPU_OP_DATA 0x2E
8484
85+typedef struct _CHNCPU_DATA CHNCPU_Data;
8586 typedef struct _CHNCPU_OP_TAG CHNCPU_OpTag;
8687 typedef struct _CHNCPU_BIOS CHNCPU_BIOS;
8788 typedef struct _CHNCPU_OP_TABLE_SET CHNCPU_OpTableSet;
@@ -92,6 +93,14 @@ typedef struct _CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment;
9293 typedef struct _CHNCPU_VM_ARGS CHNCPU_VMArgs;
9394 typedef struct _CHNCPU_VM_ARGS_BIND_DATA_TAG CHNCPU_VMArgs_BindDataTag;
9495
96+struct _CHNCPU_DATA {
97+ ch4_sint type;
98+ ch4_uint count;
99+ void *rawData;
100+ int rawDataByteSize;
101+ const char *filePath;
102+};
103+
95104 struct _CHNCPU_OP_TAG {
96105 int opCode;
97106 void *opCache;
@@ -125,7 +134,7 @@ struct _CHNCPU_POINTER_TAG {
125134 ch4_sint type; // CHNCPU_PType_xx
126135 int mindex; // for label
127136 int pindex;
128- CHNCPU_OpTag *opTag;
137+ CHNCPU_Data *data;
129138 };
130139
131140 struct _CHNCPU_RUN_TIME_ENVIRONMENT {
@@ -147,7 +156,7 @@ struct _CHNCPU_RUN_TIME_ENVIRONMENT {
147156 struct _CHNCPU_VM_ARGS_BIND_DATA_TAG {
148157 int pReg, iReg;
149158 int flags; // 0:read only, 1:write only
150- CHNCPU_OpTag *dataOPTag;
159+ CHNCPU_Data *data;
151160 const char *filePath;
152161 int pType;
153162 };
@@ -220,16 +229,7 @@ int CHNCPU_Op_CompareIReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag
220229 int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix);
221230 int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op);
222231 int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file);
223-CHNCPU_OpTag *CHNCPU_Op_DATA_CreateEmptyData(void);
224-int CHNCPU_Op_DATA_AllocateRawDataMemory(CHNCPU_OpTag *op, ch4_sint dataType, ch4_uint dataCount, unsigned int *errFlags);
225-CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType);
226-int CHNCPU_Op_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_OpTag *op);
227-int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType);
228-int CHNCPU_Op_DATA_ReadAtIndex(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, int index);
229-int CHNCPU_Op_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount);
230-int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op);
231-int CHNCPU_Op_DATA_GetDataType(CHNCPU_OpTag *op);
232-const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op);
232+CHNCPU_Data *CHNCPU_Op_DATA_GetData(CHNCPU_OpTag *op);
233233 //
234234 int CHNCPU_Op_Prefix2F_PrintWikiDoc(int opCode, FILE *file);
235235
@@ -245,6 +245,17 @@ CHNCPU_LabelSet *CHNCPU_CreateLabelSet(void);
245245 int CHNCPU_Label_Add(CHNCPU_LabelSet *lbSet, int mindex, ch4_uint labelID, ch4_uint opt, ch4_sint pType);
246246 CHNCPU_LabelTag *CHNCPU_Label_GetTagFromLabelID(CHNCPU_LabelSet *lbSet, int labelID);
247247
248+// @data.c
249+CHNCPU_Data *CHNCPU_DATA_CreateEmptyData(void);
250+void CHNCPU_DATA_FreeData(CHNCPU_Data *data);
251+int CHNCPU_DATA_AllocateRawDataMemory(CHNCPU_Data *data, ch4_sint dataType, ch4_uint dataCount, unsigned int *errFlags);
252+CHNCPU_Data *CHNCPU_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType);
253+int CHNCPU_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_Data *data);
254+int CHNCPU_DATA_WriteRawDataToFile(CHNCPU_Data *data, const char dataPath[], int dataCount, int dataType);
255+int CHNCPU_DATA_ReadAtIndex(CHNCPU_Data *data, int index, unsigned int *errFlags);
256+int CHNCPU_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount);
257+int CHNCPU_DATA_PrintData(CHNCPU_Data *data, FILE *file);
258+
248259 // @debug.c
249260 void CHNCPU_DumpAppBin(CHNCPU_RuntimeEnvironment *env);
250261 void CHNCPU_DumpIReg(CHNCPU_RuntimeEnvironment *env);
--- /dev/null
+++ b/chncpu/data.c
@@ -0,0 +1,218 @@
1+//
2+// data.c
3+// chncpu
4+//
5+// Created by 西田 耀 on 2014/07/08.
6+// Copyright (c) 2014年 CHNOSProject. All rights reserved.
7+//
8+
9+#include "chncpu.h"
10+
11+CHNCPU_Data *CHNCPU_DATA_CreateEmptyData(void)
12+{
13+ CHNCPU_Data *data;
14+
15+ data = malloc(sizeof(CHNCPU_Data));
16+ if(!data){
17+ return NULL;
18+ }
19+
20+ data->type = CHNCPU_PType_Undefined;
21+ data->count = 0;
22+ data->rawData = NULL;
23+ data->rawDataByteSize = 0;
24+ data->filePath = NULL;
25+
26+ return data;
27+}
28+
29+void CHNCPU_DATA_FreeData(CHNCPU_Data *data)
30+{
31+ if(data){
32+ if(data->rawData){
33+ free(data->rawData);
34+ }
35+ free(data);
36+ }
37+}
38+
39+int CHNCPU_DATA_AllocateRawDataMemory(CHNCPU_Data *data, ch4_sint dataType, ch4_uint dataCount, unsigned int *errFlags)
40+{
41+ // 生データのメモリを割り当てる。
42+ if(!data){
43+ if(errFlags){
44+ *errFlags |= CHNCPU_ERR_INTERNAL;
45+ }
46+ return -1;
47+ }
48+
49+ data->rawDataByteSize = CHNCPU_DATA_CalculateByteSizeOfRawData(dataType, dataCount);
50+ if(data->rawDataByteSize <= 0){
51+ if(errFlags){
52+ *errFlags |= CHNCPU_ERR_INVALID_PTYPE;
53+ }
54+ return -1;
55+ }
56+ if(data->rawData){
57+ puts("CHNCPU_Op_DATA_AllocateRawDataMemory: Double allocation. abort.");
58+ exit(EXIT_FAILURE);
59+ }
60+ data->rawData = malloc(data->rawDataByteSize);
61+ if(!data->rawData){
62+ data->rawDataByteSize = 0;
63+ return -1;
64+ }
65+
66+ data->type = dataType;
67+ data->count = dataCount;
68+
69+ return 0;
70+}
71+
72+CHNCPU_Data *CHNCPU_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType)
73+{
74+ // 現状ではUINT8のみ
75+ FILE *fp;
76+ CHNCPU_Data *data;
77+ int i;
78+
79+ data = CHNCPU_DATA_CreateEmptyData();
80+ if(!data){
81+ return NULL;
82+ }
83+
84+ fp = fopen(dataPath, "rb");
85+ if(!fp){
86+ CHNCPU_DATA_FreeData(data);
87+ return NULL;
88+ }
89+ fseek(fp, 0, SEEK_END);
90+ data->rawDataByteSize = (int)ftell(fp);
91+ fseek(fp, 0, SEEK_SET);
92+
93+ // 現状はUINT8前提なのでopCache->rawDataByteSize==dataCount
94+ if(CHNCPU_DATA_AllocateRawDataMemory(data, pType, data->rawDataByteSize, NULL)){
95+ CHNCPU_DATA_FreeData(data);
96+ return NULL;
97+ }
98+
99+ // データの中身をファイルからコピー
100+ if(data->type == CHNCPU_PType_UINT8){
101+ for(i = 0; i < data->count; i++){
102+ ((unsigned char *)data->rawData)[i] = (unsigned char)fgetc(fp);
103+ }
104+ } else{
105+ fclose(fp);
106+ CHNCPU_DATA_FreeData(data);
107+ return NULL;
108+ }
109+ fclose(fp);
110+
111+ data->filePath = dataPath;
112+
113+ return data;
114+}
115+
116+int CHNCPU_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_Data *data)
117+{
118+ if(!data || p >= CHNCPU_NUMBER_OF_PREG){
119+ return -1;
120+ }
121+
122+ // データタグを指定されたPRegに格納
123+ env->pReg[p].type = data->type;
124+ env->pReg[p].mindex = CHNCPU_MemoryIndex_INVALID;
125+ env->pReg[p].pindex = 0;
126+ env->pReg[p].data = data;
127+
128+ return 0;
129+}
130+
131+
132+int CHNCPU_DATA_WriteRawDataToFile(CHNCPU_Data *data, const char dataPath[], int dataCount, int dataType)
133+{
134+ // retv: written bytes
135+ FILE *fp;
136+ int i = 0;
137+
138+ if(!data || !dataPath){
139+ return -1;
140+ }
141+ if(dataType == CHNCPU_PType_Undefined || data->type != dataType){
142+ // タイプが一致しなければバインドしない
143+ return -1;
144+ }
145+ if(dataCount > data->count){
146+ // ポインタに格納されているデータの個数を超えてファイルを書き込むことはしない
147+ return -1;
148+ }
149+ fp = fopen(dataPath, "wb");
150+ if(!fp){
151+ return -1;
152+ }
153+ if(data->type == CHNCPU_PType_UINT8){
154+ for(i = 0; i < data->rawDataByteSize; i++){
155+ fputc(((unsigned char *)data->rawData)[i], fp);
156+ }
157+ } else{
158+ i = -1;
159+ }
160+ fclose(fp);
161+
162+ return i;
163+}
164+
165+int CHNCPU_DATA_ReadAtIndex(CHNCPU_Data *data, int index, unsigned int *errFlags)
166+{
167+ int retv;
168+
169+ if(!data || index < 0 || index >= data->count){
170+ if(errFlags){
171+ *errFlags |= CHNCPU_ERR_BAD_ACCESS;
172+ }
173+ return 0;
174+ }
175+
176+ if(data->type == CHNCPU_PType_UINT8){
177+ retv = ((unsigned char *)data->rawData)[index];
178+ } else{
179+ if(errFlags){
180+ *errFlags |= CHNCPU_ERR_INVALID_PTYPE;
181+ }
182+ return 0;
183+ }
184+
185+ return retv;
186+}
187+
188+int CHNCPU_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount)
189+{
190+ if(dataType == CHNCPU_PType_UINT8){
191+ return dataCount;
192+ }
193+ return 0;
194+}
195+
196+int CHNCPU_DATA_PrintData(CHNCPU_Data *data, FILE *file)
197+{
198+ int i = 0;
199+
200+ if(data){
201+ fputc('{', file);
202+ if(data->type == CHNCPU_PType_UINT8){
203+ for(i = 0; i < data->count; i++){
204+ if((i & 7) == 0){
205+ fprintf(file, "\n\t");
206+ }
207+ fprintf(file, "%d, ", ((unsigned char *)data->rawData)[i]);
208+ }
209+ }
210+ if(((i - 1) & 7) != 0){
211+ fputc('\n', file);
212+ }
213+ fputc('}', file);
214+ fputc('\n', file);
215+ }
216+
217+ return 0;
218+}
--- a/chncpu/opcache.h
+++ b/chncpu/opcache.h
@@ -144,12 +144,7 @@ typedef struct _CHNCPU_OP_CACHE_DATA CHNCPU_OpCache_DATA;
144144 struct _CHNCPU_OP_CACHE_DATA {
145145 ch4_uint labelID;
146146 ch4_uint labelOpt;
147- ch4_sint dataType;
148- ch4_uint dataCount;
149- void *rawData;
150- //
151- int rawDataByteSize;
152- const char *filePath;
147+ CHNCPU_Data *data;
153148 };
154149
155150 //
--- a/chncpu/opcode.c
+++ b/chncpu/opcode.c
@@ -241,7 +241,7 @@ int CHNCPU_Op_PLIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
241241 env->pReg[opCache->r].pindex = 0;
242242
243243 if(0 <= label->mindex && label->mindex < CHNCPU_LENGTH_OF_MAIN_MEMORY){
244- env->pReg[opCache->r].opTag = &env->mainmemory[label->mindex];
244+ env->pReg[opCache->r].data = CHNCPU_Op_DATA_GetData(&env->mainmemory[label->mindex]);
245245 }
246246
247247 if(opCache->r == 0x3F){
@@ -413,7 +413,7 @@ int CHNCPU_Op_LMEM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
413413 return -1;
414414 }
415415
416- value = CHNCPU_Op_DATA_ReadAtIndex(env, p->opTag, p->pindex);
416+ value = CHNCPU_DATA_ReadAtIndex(p->data, p->pindex, &env->errFlags);
417417 if(env->errFlags){
418418 return -1;
419419 }
@@ -712,31 +712,31 @@ int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op,
712712 {
713713 CHNCPU_OpCache_DATA *opCache;
714714 int i;
715+ ch4_sint type;
716+ ch4_uint count;
715717
716718 opCache = malloc(sizeof(CHNCPU_OpCache_DATA));
717719 op->opCache = opCache;
718720
719721 opCache->labelID = CH4Reader_ReadNextAsUINT(env->appbinReader);
720722 opCache->labelOpt = CH4Reader_ReadNextAsUINT(env->appbinReader);
721- opCache->dataType = CH4Reader_ReadNextAsSINT(env->appbinReader);
722- opCache->dataCount = CH4Reader_ReadNextAsUINT(env->appbinReader);
723- opCache->rawData = NULL;
724- opCache->rawDataByteSize = 0;
725- opCache->filePath = NULL;
723+ type = CH4Reader_ReadNextAsSINT(env->appbinReader);
724+ count = CH4Reader_ReadNextAsUINT(env->appbinReader);
726725
727- // Check ptype & Calculate rawDataByteSize
728- if(CHNCPU_Op_DATA_AllocateRawDataMemory(op, opCache->dataType, opCache->dataCount, &env->errFlags)){
726+ opCache->data = CHNCPU_DATA_CreateEmptyData();
727+ CHNCPU_DATA_AllocateRawDataMemory(opCache->data, type, count, &env->errFlags);
728+ if(env->errFlags){
729729 return -1;
730730 }
731731
732732 // Bind rawData
733- if(opCache->dataType == CHNCPU_PType_UINT8){
734- for(i = 0; i < opCache->dataCount; i++){
735- ((unsigned char *)opCache->rawData)[i] = CH4Reader_ReadNextUINTn(env->appbinReader, 8);
733+ if(type == CHNCPU_PType_UINT8){
734+ for(i = 0; i < count; i++){
735+ ((unsigned char *)opCache->data->rawData)[i] = CH4Reader_ReadNextUINTn(env->appbinReader, 8);
736736 }
737737 }
738738
739- if(CHNCPU_Label_Add(env->labelSet, env->currentIndex, opCache->labelID, opCache->labelOpt, opCache->dataType)){
739+ if(CHNCPU_Label_Add(env->labelSet, env->currentIndex, opCache->labelID, opCache->labelOpt, type)){
740740 env->errFlags |= CHNCPU_ERR_DUPLICATED_LABEL_ID;
741741 }
742742
@@ -753,254 +753,27 @@ int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op)
753753 }
754754 int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file)
755755 {
756- int i = 0;
757756 CHNCPU_OpCache_DATA *opCache;
758757 opCache = op->opCache;
759758
760- fprintf(file, "DATA(labelID:0x%X, labelOpt:%d, dataType:%d, dataCount:%d);\n", opCache->labelID, opCache->labelOpt, opCache->dataType, opCache->dataCount);
759+ fprintf(file, "DATA(labelID:0x%X, labelOpt:%d, dataType:%d, dataCount:%d);\n", opCache->labelID, opCache->labelOpt, opCache->data->type, opCache->data->count);
761760
762- if(opCache->rawData){
763- fputc('{', file);
764- if(opCache->dataType == CHNCPU_PType_UINT8){
765- for(i = 0; i < opCache->dataCount; i++){
766- if((i & 7) == 0){
767- fprintf(file, "\n\t");
768- }
769- fprintf(file, "%d, ", ((unsigned char *)opCache->rawData)[i]);
770- }
771- }
772- if(((i - 1) & 7) != 0){
773- fputc('\n', file);
774- }
775- fputc('}', file);
776- fputc('\n', file);
777- }
761+ CHNCPU_DATA_PrintData(opCache->data, file);
778762
779763 return 0;
780764 }
781-CHNCPU_OpTag *CHNCPU_Op_DATA_CreateEmptyData(void)
782-{
783- CHNCPU_OpTag *op;
784- CHNCPU_OpCache_DATA *opCache;
785-
786- op = malloc(sizeof(CHNCPU_OpTag));
787- if(!op){
788- return NULL;
789- }
790- opCache = malloc(sizeof(CHNCPU_OpCache_DATA));
791- if(!opCache){
792- free(op);
793- return NULL;
794- }
795-
796- op->opCode = CHNCPU_OP_DATA;
797- op->opCache = opCache;
798-
799- opCache->labelID = 0;
800- opCache->labelOpt = 0;
801- opCache->dataType = CHNCPU_PType_Undefined;
802- opCache->dataCount = 0;
803- opCache->rawData = NULL;
804- opCache->rawDataByteSize = 0;
805- opCache->filePath = NULL;
806-
807- return op;
808-}
809-int CHNCPU_Op_DATA_AllocateRawDataMemory(CHNCPU_OpTag *op, ch4_sint dataType, ch4_uint dataCount, unsigned int *errFlags)
765+CHNCPU_Data *CHNCPU_Op_DATA_GetData(CHNCPU_OpTag *op)
810766 {
811- // 生データのメモリを割り当てる。
812- CHNCPU_OpCache_DATA *opCache;
813-
814767 if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
815- if(errFlags){
816- *errFlags |= CHNCPU_ERR_INTERNAL;
817- }
818- return -1;
819- }
820- opCache = op->opCache;
821-
822- opCache->rawDataByteSize = CHNCPU_Op_DATA_CalculateByteSizeOfRawData(dataType, dataCount);
823- if(opCache->rawDataByteSize <= 0){
824- if(errFlags){
825- *errFlags |= CHNCPU_ERR_INVALID_PTYPE;
826- }
827- return -1;
828- }
829- if(opCache->rawData){
830- puts("CHNCPU_Op_DATA_AllocateRawDataMemory: Double allocation. abort.");
831- exit(EXIT_FAILURE);
832- }
833- opCache->rawData = malloc(opCache->rawDataByteSize);
834- if(!opCache->rawData){
835- opCache->rawDataByteSize = 0;
836- return -1;
837- }
838-
839- opCache->dataType = dataType;
840- opCache->dataCount = dataCount;
841-
842- return 0;
843-}
844-CHNCPU_OpTag *CHNCPU_Op_DATA_CreateDataFromFileAsPType(const char dataPath[], ch4_sint pType)
845-{
846- // 現状ではUINT8のみ
847- FILE *fp;
848- CHNCPU_OpTag *op;
849- CHNCPU_OpCache_DATA *opCache;
850- int i;
851-
852- op = CHNCPU_Op_DATA_CreateEmptyData();
853- if(!op){
854- return NULL;
855- }
856- opCache = op->opCache;
857-
858- fp = fopen(dataPath, "rb");
859- if(!fp){
860- free(opCache);
861- free(op);
862- return NULL;
863- }
864- fseek(fp, 0, SEEK_END);
865- opCache->rawDataByteSize = (int)ftell(fp);
866- fseek(fp, 0, SEEK_SET);
867-
868- // 現状はUINT8前提なのでopCache->rawDataByteSize==dataCount
869- if(CHNCPU_Op_DATA_AllocateRawDataMemory(op, pType, opCache->rawDataByteSize, NULL)){
870- fclose(fp);
871- free(opCache);
872- free(op);
873- return NULL;
874- }
875-
876- // データの中身をファイルからコピー
877- if(opCache->dataType == CHNCPU_PType_UINT8){
878- for(i = 0; i < opCache->dataCount; i++){
879- ((unsigned char *)opCache->rawData)[i] = (unsigned char)fgetc(fp);
880- }
881- } else{
882- free(opCache->rawData);
883- fclose(fp);
884- free(opCache);
885- free(op);
886768 return NULL;
887769 }
888- fclose(fp);
889-
890- return op;
891-}
892-
893-int CHNCPU_Op_DATA_AssignDataTagToPReg(CHNCPU_RuntimeEnvironment *env, ch4_uint p, CHNCPU_OpTag *op)
894-{
895- CHNCPU_OpCache_DATA *opCache;
896-
897- if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache || p >= CHNCPU_NUMBER_OF_PREG){
898- return -1;
899- }
900- opCache = op->opCache;
901-
902- // データタグを指定されたPRegに格納
903- env->pReg[p].mindex = CHNCPU_MemoryIndex_INVALID;
904- env->pReg[p].opTag = op;
905- env->pReg[p].pindex = 0;
906- env->pReg[p].type = opCache->dataType;
907-
908- return 0;
909-}
910-
911-int CHNCPU_Op_DATA_WriteRawDataToFile(CHNCPU_OpTag *op, const char dataPath[], int dataCount, int dataType)
912-{
913- // retv: written bytes
914- FILE *fp;
915- CHNCPU_OpCache_DATA *opCache;
916- int i = 0;
917-
918- if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache || !dataPath){
919- return -1;
920- }
921- opCache = op->opCache;
922- if(dataType == CHNCPU_PType_Undefined || opCache->dataType != dataType){
923- // タイプが一致しなければバインドしない
924- return -1;
925- }
926- if(dataCount > opCache->dataCount){
927- // ポインタに格納されているデータの個数を超えてファイルを書き込むことはしない
928- return -1;
929- }
930- fp = fopen(dataPath, "wb");
931- if(!fp){
932- return -1;
933- }
934- if(opCache->dataType == CHNCPU_PType_UINT8){
935- opCache->dataCount = opCache->rawDataByteSize;
936- for(i = 0; i < dataCount; i++){
937- fputc(((unsigned char *)opCache->rawData)[i], fp);
938- }
939- } else{
940- i = -1;
941- }
942- fclose(fp);
943-
944- return i;
945-}
946-
947-int CHNCPU_Op_DATA_ReadAtIndex(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, int index)
948-{
949- int retv;
950- CHNCPU_OpCache_DATA *opCache;
951-
952-
953- if(!op || op->opCode != CHNCPU_OP_DATA || index < 0 || index >= ((CHNCPU_OpCache_DATA *)op->opCache)->dataCount){
954- env->errFlags |= CHNCPU_ERR_BAD_ACCESS;
955- return 0;
956- }
957- opCache = op->opCache;
958-
959- if(opCache->dataType == CHNCPU_PType_UINT8){
960- retv = ((unsigned char *)opCache->rawData)[index];
961- } else{
962- env->errFlags |= CHNCPU_ERR_INVALID_PTYPE;
963- return 0;
964- }
965-
966- return retv;
967-}
968-int CHNCPU_Op_DATA_CalculateByteSizeOfRawData(ch4_sint dataType, ch4_uint dataCount)
969-{
970- if(dataType == CHNCPU_PType_UINT8){
971- return dataCount;
972- }
973- return 0;
974-}
975-int CHNCPU_Op_DATA_GetDataCount(CHNCPU_OpTag *op)
976-{
977- if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
978- return -1;
979- }
980- return ((CHNCPU_OpCache_DATA *)op->opCache)->dataCount;
981-}
982-int CHNCPU_Op_DATA_GetDataType(CHNCPU_OpTag *op)
983-{
984- if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
985- return 0;
986- }
987- return ((CHNCPU_OpCache_DATA *)op->opCache)->dataType;
988-}
989-const char *CHNCPU_Op_DATA_GetFilePath(CHNCPU_OpTag *op)
990-{
991- if(!op || op->opCode != CHNCPU_OP_DATA || !op->opCache){
992- return 0;
993- }
994- if(!((CHNCPU_OpCache_DATA *)op->opCache)->filePath){
995- return "";
996- }
997- return ((CHNCPU_OpCache_DATA *)op->opCache)->filePath;
770+ return ((CHNCPU_OpCache_DATA *)op->opCache)->data;
998771 }
999772
1000773 //
1001774 // 32 MALLOC
1002775 //
1003-
776+/*
1004777 int CHNCPU_Op_MALLOC_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix)
1005778 {
1006779 CHNCPU_OpCache_MALLOC *opCache;
@@ -1058,4 +831,5 @@ int CHNCPU_Op_MALLOC_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op,
1058831 fprintf(file, "LMEM(p:0x%02X, pType:0x%X, pDiff:%d, r:%02X, bitDst:%d);\n", opCache->p, opCache->pType, opCache->pDiff, opCache->r, opCache->bitDst);
1059832
1060833 return 0;
1061-}
\ No newline at end of file
834+}
835+*/
\ No newline at end of file
--- a/debug.c
+++ b/debug.c
@@ -52,7 +52,7 @@ void CHNCPU_DumpPReg(CHNCPU_RuntimeEnvironment *env)
5252 if(env->pReg[i].mindex != CHNCPU_MemoryIndex_INVALID){
5353 printf("P%02X: Type:%2X mem[%d][%d]", i, env->pReg[i].type, env->pReg[i].mindex, env->pReg[i].pindex);
5454 } else{
55- printf("P%02X: Type:%2X %s[%d]", i, env->pReg[i].type, CHNCPU_Op_DATA_GetFilePath(env->pReg[i].opTag), env->pReg[i].pindex);
55+ printf("P%02X: Type:%2X %s[%d]", i, env->pReg[i].type, env->pReg[i].data->filePath, env->pReg[i].pindex);
5656 }
5757 putchar('\n');
5858 regs++;