-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_reader.c
229 lines (210 loc) · 4.94 KB
/
file_reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*includes*/
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <dlfcn.h>
#include <stdio.h>
#include "API_header.h"
//get size
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
typedef bool (*init_data)();
typedef void (*read_data)(uint16_t, uint8_t*, uint32_t);
typedef void (*write_data)(uint16_t, uint8_t*, uint32_t);
typedef void (*close_data)();
FILE out_file;
FILE in_file;
init_data icurrent;
read_data rcurrent;
write_data wcurrent;
close_data ccurrent;
API_HIDE char Int2Char(uint8_t parse)
{
if(0 <=parse <= 9) return parse + '0';
else if(0x0A <= parse <= 0x0F) return parse + 'A' - 10;
else return 0;
}
API_HIDE uint8_t Char2Int(char hex)
{
if( '0' <= hex <= '9') return hex - '0';
else if('A' <= hex <= 'F') return hex - 'A' + 10;
else if( 'a' <= hex <= 'f') return hex - 'a' + 10;
else return 255;
}
API_HIDE void read_file(uint16_t address, uint8_t* data, uint32_t lenght){
}
API_HIDE void write_file(uint16_t address, uint8_t* data, uint32_t lenght){
}
API_HIDE void read_array(uint16_t address, uint8_t* data, uint32_t lenght){
}
API_HIDE void write_array(uint16_t address, uint8_t* data, uint32_t lenght){
}
API_SHOW bool set_infile(char* path){
if(in_file=path(file,"rb")) return true;
return false;
}
API_SHOW bool set_outfile(char* path){
if(out_file=path(file,"wb")) {return true;
wcurrent=}
return false;
}
API_SHOW bool read_IntelHEX(void* ROM,char* file){
FILE* in_file;
uint8_t parsed,lenght,checksum,mode = 0;
uint16_t address;
char buffer[BUFFER_SIZE];
char* buffer_ptr;
char* target_ptr;
struct stat file_disc;
if(!(in_file=fopen(file,"rb"))) return false;
fstat(in_file, &file_disc);
for(int size = file_disc.st_size;size;){
if(size>=1024){
fread(buffer_ptr = buffer,BUFFER_SIZE,1,in_file);
size -= BUFFER_SIZE;
}
else{
fread(buffer_ptr = &buffer[BUFFER_SIZE-size],size,1,in_file);
size = 0;
fclose(in_file);
}
while(buffer_ptr++ < &buffer[BUFFER_SIZE]){
if(*buffer_ptr == ':') mode=0;
else if((parsed=Char2Int(*buffer_ptr))!=255){
switch(mode++){
case 0:
lenght = parsed << 4;
break;
case 1:
lenght |= parsed;
checksum = lenght;
break;
case 2:
address = 0;
case 3:
case 4:
case 5:
address|=parsed<<(4*(6-mode));
checksum -= address + (address>>8);
target_ptr=&ROM[address];
break;
case 7:
if(parsed==1){
mode += (253-8);
target_ptr = NULL;
address = 0;
}
break;
case 8:
*target_ptr = parsed << 4;
break;
case 9:
*target_ptr |= parsed;
checksum-=*(target_ptr++);
if(--lenght > 0)mode = 7;
else mode += (253-10);
break;
case 253:
if(!(checksum&0x0F==parsed)) return false;
break;
case 254:
if(!(checksum&0xF0==parsed<<4)) return false;
else if((target_ptr==NULL)&&(address==0)) return true;
break;
case 6:
case 255:
default:
mode--;
break;
}
}
}
}
}
API_SHOW void write_IntelHEX(void* ROM,void* RAM,char* file){
}
API_SHOW void clean_IntelHEX(char* file){
}
API_SHOW bool load(char* file, char output){
uint8_t parsed,lenght,lenght_count,checksum,mode = 0;
uint16_t address;
char buffer[BUFFER_SIZE];
char* buffer_ptr;
char* target_ptr;
struct stat file_disc;
if( !icurrent() ) return false;
fstat(in_file, &file_disc);
for(int size = file_disc.st_size;size;){
if(size>=1024){
fread(buffer_ptr = buffer,BUFFER_SIZE,1,in_file);
size -= BUFFER_SIZE;
}
else{
fread(buffer_ptr = &buffer[BUFFER_SIZE-size],size,1,in_file);
size = 0;
fclose(in_file);
}
while(buffer_ptr++ < &buffer[BUFFER_SIZE]){
if(*buffer_ptr == ':') mode=0;
else if((parsed=Char2Int(*buffer_ptr))!=255){
switch(mode++){
case 0:
lenght_count = lenght = parsed << 4;
break;
case 1:
lenght_count = lenght |= parsed;
checksum = lenght;
free(target_ptr);
target_ptr = malloc(lenght * sizeof(uint8_t));
break;
case 2:
address = 0;
case 3:
case 4:
case 5:
address|=parsed<<(4*(6-mode));
checksum -= address + (address>>8);
break;
case 7:
if(parsed==1){
mode += (253-8);
target_ptr = NULL;
address = 0;
}
break;
case 8:
*target_ptr = parsed << 4;
break;
case 9:
*target_ptr |= parsed;
checksum-=*(target_ptr++);
if(--lenght_count > 0)mode = 7;
else mode += (253-10);
break;
case 253:
if(!(checksum&0x0F==parsed)) return false;
break;
case 254:
if(!(checksum&0xF0==parsed<<4)) return false;
else{
wcurrent(address,target_ptr,lenght);
}
if((target_ptr==NULL)&&(address==0)) {
ccurrent();
return true;
}
break;
case 6:
case 255:
default:
mode--;
break;
}
}
}
}
}
API_SHOW void Bin2IntelHEX(){}