修訂 | afa134b89419f362494e8fefd6652f8f14ff05fe (tree) |
---|---|
時間 | 2014-07-08 21:26:24 |
作者 | hikarupsp <hikarupsp@user...> |
Commiter | hikarupsp |
DATAの内部処理方式を変更した。
@@ -12,6 +12,7 @@ | ||
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 | 14 | 1786B0941956C18800FD4F7B /* label.c in Sources */ = {isa = PBXBuildFile; fileRef = 1786B0931956C18800FD4F7B /* label.c */; }; |
15 | + 17958C6F196C0E8000D7DBB4 /* data.c in Sources */ = {isa = PBXBuildFile; fileRef = 17958C6E196C0E8000D7DBB4 /* data.c */; }; | |
15 | 16 | 17DD55BB1967992200DA7D82 /* args.c in Sources */ = {isa = PBXBuildFile; fileRef = 17DD55BA1967992200DA7D82 /* args.c */; }; |
16 | 17 | 17E5092A1967F7B00091A22A /* src.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17E509291967F7770091A22A /* src.txt */; }; |
17 | 18 | 17E50932196AD91D0091A22A /* debug.c in Sources */ = {isa = PBXBuildFile; fileRef = 17E50931196AD91D0091A22A /* debug.c */; }; |
@@ -41,6 +42,7 @@ | ||
41 | 42 | 17478D02193F586100293791 /* ch4.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ch4.h; sourceTree = "<group>"; usesTabs = 1; }; |
42 | 43 | 17478D031940B66700293791 /* opcode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = opcode.c; sourceTree = "<group>"; usesTabs = 1; }; |
43 | 44 | 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>"; }; | |
44 | 46 | 17DD55BA1967992200DA7D82 /* args.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = args.c; sourceTree = "<group>"; }; |
45 | 47 | 17E509291967F7770091A22A /* src.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = src.txt; sourceTree = "<group>"; }; |
46 | 48 | 17E50930196AD6BF0091A22A /* opcache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = opcache.h; sourceTree = "<group>"; }; |
@@ -87,6 +89,7 @@ | ||
87 | 89 | 17478D02193F586100293791 /* ch4.h */, |
88 | 90 | 17E50930196AD6BF0091A22A /* opcache.h */, |
89 | 91 | 17478D031940B66700293791 /* opcode.c */, |
92 | + 17958C6E196C0E8000D7DBB4 /* data.c */, | |
90 | 93 | 17F95C00194C8DA70064E1B6 /* bios.c */, |
91 | 94 | 1786B0931956C18800FD4F7B /* label.c */, |
92 | 95 | 17478CFD193E2ADA00293791 /* test.hex */, |
@@ -147,6 +150,7 @@ | ||
147 | 150 | buildActionMask = 2147483647; |
148 | 151 | files = ( |
149 | 152 | 1786B0941956C18800FD4F7B /* label.c in Sources */, |
153 | + 17958C6F196C0E8000D7DBB4 /* data.c in Sources */, | |
150 | 154 | 17E50932196AD91D0091A22A /* debug.c in Sources */, |
151 | 155 | 17F95C01194C8DA70064E1B6 /* bios.c in Sources */, |
152 | 156 | 17DD55BB1967992200DA7D82 /* args.c in Sources */, |
@@ -46,8 +46,8 @@ CHNCPU_VMArgs *bindVMArgs(int argc, const char *argv[]) | ||
46 | 46 | bdTag->pType = CHNCPU_VMArgs_GetNextAsSignedHexValue(argc, argv, &nextIndex); |
47 | 47 | bdTag->flags = CHNCPU_VMArgs_GetNextAsUnsignedHexValue(argc, argv, &nextIndex); |
48 | 48 | 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)){ | |
51 | 51 | puts("Invalid data option (-bd) args."); |
52 | 52 | argc = 0; |
53 | 53 | continue; |
@@ -84,10 +84,10 @@ int CHNCPU_VMArgs_BindDataToRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_VM | ||
84 | 84 | continue; |
85 | 85 | } |
86 | 86 | // データサイズを指定されたIRegに格納 |
87 | - env->iReg[bdTag->iReg] = CHNCPU_Op_DATA_GetDataCount(bdTag->dataOPTag); | |
87 | + env->iReg[bdTag->iReg] = bdTag->data->count; | |
88 | 88 | env->iRegBits[bdTag->iReg] = CHNCPU_BITS_MAX; |
89 | 89 | // データタグを指定されたPRegに格納 |
90 | - CHNCPU_Op_DATA_AssignDataTagToPReg(env, bdTag->pReg, bdTag->dataOPTag); | |
90 | + CHNCPU_DATA_AssignDataTagToPReg(env, bdTag->pReg, bdTag->data); | |
91 | 91 | } |
92 | 92 | |
93 | 93 | return 0; |
@@ -103,7 +103,7 @@ int CHNCPU_VMArgs_BindDataFromRuntimeEnv(CHNCPU_RuntimeEnvironment *env, CHNCPU_ | ||
103 | 103 | if(bdTag->flags != 1){ |
104 | 104 | continue; |
105 | 105 | } |
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); | |
107 | 107 | } |
108 | 108 | |
109 | 109 | return 0; |
@@ -146,7 +146,7 @@ CHNCPU_RuntimeEnvironment *CHNCPU_CreateRuntimeEnvironment(CHNCPU_OpTableSet *op | ||
146 | 146 | env->pReg[i].type = 0; |
147 | 147 | env->pReg[i].mindex = 0; |
148 | 148 | env->pReg[i].pindex = 0; |
149 | - env->pReg[i].opTag = NULL; | |
149 | + env->pReg[i].data = NULL; | |
150 | 150 | } |
151 | 151 | |
152 | 152 | // ラベル管理初期化 |
@@ -16,9 +16,9 @@ | ||
16 | 16 | |
17 | 17 | // DEBUG Flag |
18 | 18 | #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 | |
22 | 22 | #define DEBUG_PRINT_OP_EXECUTING 0 |
23 | 23 | |
24 | 24 | // VM flags |
@@ -82,6 +82,7 @@ | ||
82 | 82 | // Opcode |
83 | 83 | #define CHNCPU_OP_DATA 0x2E |
84 | 84 | |
85 | +typedef struct _CHNCPU_DATA CHNCPU_Data; | |
85 | 86 | typedef struct _CHNCPU_OP_TAG CHNCPU_OpTag; |
86 | 87 | typedef struct _CHNCPU_BIOS CHNCPU_BIOS; |
87 | 88 | typedef struct _CHNCPU_OP_TABLE_SET CHNCPU_OpTableSet; |
@@ -92,6 +93,14 @@ typedef struct _CHNCPU_RUN_TIME_ENVIRONMENT CHNCPU_RuntimeEnvironment; | ||
92 | 93 | typedef struct _CHNCPU_VM_ARGS CHNCPU_VMArgs; |
93 | 94 | typedef struct _CHNCPU_VM_ARGS_BIND_DATA_TAG CHNCPU_VMArgs_BindDataTag; |
94 | 95 | |
96 | +struct _CHNCPU_DATA { | |
97 | + ch4_sint type; | |
98 | + ch4_uint count; | |
99 | + void *rawData; | |
100 | + int rawDataByteSize; | |
101 | + const char *filePath; | |
102 | +}; | |
103 | + | |
95 | 104 | struct _CHNCPU_OP_TAG { |
96 | 105 | int opCode; |
97 | 106 | void *opCache; |
@@ -125,7 +134,7 @@ struct _CHNCPU_POINTER_TAG { | ||
125 | 134 | ch4_sint type; // CHNCPU_PType_xx |
126 | 135 | int mindex; // for label |
127 | 136 | int pindex; |
128 | - CHNCPU_OpTag *opTag; | |
137 | + CHNCPU_Data *data; | |
129 | 138 | }; |
130 | 139 | |
131 | 140 | struct _CHNCPU_RUN_TIME_ENVIRONMENT { |
@@ -147,7 +156,7 @@ struct _CHNCPU_RUN_TIME_ENVIRONMENT { | ||
147 | 156 | struct _CHNCPU_VM_ARGS_BIND_DATA_TAG { |
148 | 157 | int pReg, iReg; |
149 | 158 | int flags; // 0:read only, 1:write only |
150 | - CHNCPU_OpTag *dataOPTag; | |
159 | + CHNCPU_Data *data; | |
151 | 160 | const char *filePath; |
152 | 161 | int pType; |
153 | 162 | }; |
@@ -220,16 +229,7 @@ int CHNCPU_Op_CompareIReg_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag | ||
220 | 229 | int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix); |
221 | 230 | int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op); |
222 | 231 | 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); | |
233 | 233 | // |
234 | 234 | int CHNCPU_Op_Prefix2F_PrintWikiDoc(int opCode, FILE *file); |
235 | 235 |
@@ -245,6 +245,17 @@ CHNCPU_LabelSet *CHNCPU_CreateLabelSet(void); | ||
245 | 245 | int CHNCPU_Label_Add(CHNCPU_LabelSet *lbSet, int mindex, ch4_uint labelID, ch4_uint opt, ch4_sint pType); |
246 | 246 | CHNCPU_LabelTag *CHNCPU_Label_GetTagFromLabelID(CHNCPU_LabelSet *lbSet, int labelID); |
247 | 247 | |
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 | + | |
248 | 259 | // @debug.c |
249 | 260 | void CHNCPU_DumpAppBin(CHNCPU_RuntimeEnvironment *env); |
250 | 261 | void CHNCPU_DumpIReg(CHNCPU_RuntimeEnvironment *env); |
@@ -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 | +} |
@@ -144,12 +144,7 @@ typedef struct _CHNCPU_OP_CACHE_DATA CHNCPU_OpCache_DATA; | ||
144 | 144 | struct _CHNCPU_OP_CACHE_DATA { |
145 | 145 | ch4_uint labelID; |
146 | 146 | 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; | |
153 | 148 | }; |
154 | 149 | |
155 | 150 | // |
@@ -241,7 +241,7 @@ int CHNCPU_Op_PLIMM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | ||
241 | 241 | env->pReg[opCache->r].pindex = 0; |
242 | 242 | |
243 | 243 | 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]); | |
245 | 245 | } |
246 | 246 | |
247 | 247 | if(opCache->r == 0x3F){ |
@@ -413,7 +413,7 @@ int CHNCPU_Op_LMEM_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | ||
413 | 413 | return -1; |
414 | 414 | } |
415 | 415 | |
416 | - value = CHNCPU_Op_DATA_ReadAtIndex(env, p->opTag, p->pindex); | |
416 | + value = CHNCPU_DATA_ReadAtIndex(p->data, p->pindex, &env->errFlags); | |
417 | 417 | if(env->errFlags){ |
418 | 418 | return -1; |
419 | 419 | } |
@@ -712,31 +712,31 @@ int CHNCPU_Op_DATA_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, | ||
712 | 712 | { |
713 | 713 | CHNCPU_OpCache_DATA *opCache; |
714 | 714 | int i; |
715 | + ch4_sint type; | |
716 | + ch4_uint count; | |
715 | 717 | |
716 | 718 | opCache = malloc(sizeof(CHNCPU_OpCache_DATA)); |
717 | 719 | op->opCache = opCache; |
718 | 720 | |
719 | 721 | opCache->labelID = CH4Reader_ReadNextAsUINT(env->appbinReader); |
720 | 722 | 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); | |
726 | 725 | |
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){ | |
729 | 729 | return -1; |
730 | 730 | } |
731 | 731 | |
732 | 732 | // 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); | |
736 | 736 | } |
737 | 737 | } |
738 | 738 | |
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)){ | |
740 | 740 | env->errFlags |= CHNCPU_ERR_DUPLICATED_LABEL_ID; |
741 | 741 | } |
742 | 742 |
@@ -753,254 +753,27 @@ int CHNCPU_Op_DATA_Execute(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op) | ||
753 | 753 | } |
754 | 754 | int CHNCPU_Op_DATA_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, FILE *file) |
755 | 755 | { |
756 | - int i = 0; | |
757 | 756 | CHNCPU_OpCache_DATA *opCache; |
758 | 757 | opCache = op->opCache; |
759 | 758 | |
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); | |
761 | 760 | |
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); | |
778 | 762 | |
779 | 763 | return 0; |
780 | 764 | } |
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) | |
810 | 766 | { |
811 | - // 生データのメモリを割り当てる。 | |
812 | - CHNCPU_OpCache_DATA *opCache; | |
813 | - | |
814 | 767 | 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); | |
886 | 768 | return NULL; |
887 | 769 | } |
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; | |
998 | 771 | } |
999 | 772 | |
1000 | 773 | // |
1001 | 774 | // 32 MALLOC |
1002 | 775 | // |
1003 | - | |
776 | +/* | |
1004 | 777 | int CHNCPU_Op_MALLOC_BindOperand(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, unsigned int prefix) |
1005 | 778 | { |
1006 | 779 | CHNCPU_OpCache_MALLOC *opCache; |
@@ -1058,4 +831,5 @@ int CHNCPU_Op_MALLOC_PrintCode(CHNCPU_RuntimeEnvironment *env, CHNCPU_OpTag *op, | ||
1058 | 831 | 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); |
1059 | 832 | |
1060 | 833 | return 0; |
1061 | -} | |
\ No newline at end of file | ||
834 | +} | |
835 | +*/ | |
\ No newline at end of file |
@@ -52,7 +52,7 @@ void CHNCPU_DumpPReg(CHNCPU_RuntimeEnvironment *env) | ||
52 | 52 | if(env->pReg[i].mindex != CHNCPU_MemoryIndex_INVALID){ |
53 | 53 | printf("P%02X: Type:%2X mem[%d][%d]", i, env->pReg[i].type, env->pReg[i].mindex, env->pReg[i].pindex); |
54 | 54 | } 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); | |
56 | 56 | } |
57 | 57 | putchar('\n'); |
58 | 58 | regs++; |