-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmem.c
More file actions
234 lines (184 loc) · 4.5 KB
/
Copy pathmem.c
File metadata and controls
234 lines (184 loc) · 4.5 KB
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
230
231
#include "mem.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
uint8_t mem_read_byte(mem_t *mem, uint32_t address)
{
return mem->ram[address & 0xFFFFFF];
}
uint16_t mem_read_word(mem_t *mem, uint32_t address, bool *error)
{
address &= 0xFFFFFF;
if (address % 2 != 0) {
*error = true;
return 0;
} else {
return mem->ram[address+1] |
(mem->ram[address] << 8);
}
}
uint32_t mem_read_long(mem_t *mem, uint32_t address, bool *error)
{
address &= 0xFFFFFF;
if (address % 2 != 0) {
*error = true;
return 0;
} else {
if (address == 0xFFFFFE) {
return mem->ram[0x000001] |
(mem->ram[0x000000] << 8) |
(mem->ram[address+1] << 16) |
(mem->ram[address] << 24);
} else {
return mem->ram[address+3] |
(mem->ram[address+2] << 8) |
(mem->ram[address+1] << 16) |
(mem->ram[address] << 24);
}
}
}
void mem_write_byte(mem_t *mem, uint32_t address, uint8_t value)
{
mem->ram[address & 0xFFFFFF] = value;
}
void mem_write_word(mem_t *mem, uint32_t address, uint16_t value, bool *error)
{
address &= 0xFFFFFF;
if (address % 2 != 0) {
*error = true;
} else {
mem->ram[address] = (value >> 8) & 0xFF;
mem->ram[address+1] = value & 0xFF;
}
}
void mem_write_long(mem_t *mem, uint32_t address, uint32_t value, bool *error)
{
address &= 0xFFFFFF;
if (address % 2 != 0) {
*error = true;
} else {
if (address == 0xFFFFFE) {
mem->ram[address ] = (value >> 24) & 0xFF;
mem->ram[address+1] = (value >> 16) & 0xFF;
mem->ram[0x000000] = (value >> 8) & 0xFF;
mem->ram[0x000001] = value & 0xFF;
} else {
mem->ram[address ] = (value >> 24) & 0xFF;
mem->ram[address+1] = (value >> 16) & 0xFF;
mem->ram[address+2] = (value >> 8) & 0xFF;
mem->ram[address+3] = value & 0xFF;
}
}
}
void mem_init(mem_t *mem)
{
int i;
for (i = 0; i < MEM_MAX; i++) {
mem->ram[i] = 0x0;
}
}
int mem_load_binary(mem_t *mem, const char *filename, uint32_t address)
{
FILE *fh;
int c;
fh = fopen(filename, "rb");
if (fh == NULL) {
return -1;
}
while ((c = fgetc(fh)) != EOF) {
mem_write_byte(mem, address, c);
address++;
}
fclose(fh);
return 0;
}
int mem_load_srec(mem_t *mem, const char *filename)
{
FILE *fh;
int i;
int n;
char line[128];
uint8_t count;
uint8_t type;
uint8_t byte;
uint32_t address;
fh = fopen(filename, "r");
if (fh == NULL) {
return -1;
}
while (fgets(line, sizeof(line), fh) != NULL) {
if (line[0] != 'S') {
continue; /* Line must start with 'S'. */
}
type = line[1];
if (! (type == '1' || type == '2')) {
continue; /* Only 16-bit or 24-bit load address supported. */
}
if (sscanf(&line[2], "%02hhX", &count) != 1) {
continue; /* Unable to read byte count. */
}
if (type == '1') {
if (sscanf(&line[4], "%04X", &address) != 1) {
continue; /* Unable to read address. */
}
i = 8;
} else if (type == '2') {
if (sscanf(&line[4], "%06X", &address) != 1) {
continue; /* Unable to read address. */
}
i = 10;
}
/* Read data bytes: */
for (n = 0; i < (count * 2) + 2; i += 2, n++) {
if (sscanf(&line[i], "%02hhX", &byte) != 1) {
continue; /* Unable to read byte. */
}
mem_write_byte(mem, address + n, byte);
}
}
fclose(fh);
return 0;
}
static void mem_dump_16(FILE *fh, mem_t *mem, uint32_t start, uint32_t end)
{
int i;
uint32_t address;
uint8_t value;
fprintf(fh, "%06x ", start & 0xFFFFF0);
/* Hex */
for (i = 0; i < 16; i++) {
address = (start & 0xFFFFF0) + i;
value = mem_read_byte(mem, address);
if ((address >= start) && (address <= end)) {
fprintf(fh, "%02x ", value);
} else {
fprintf(fh, " ");
}
if (i % 4 == 3) {
fprintf(fh, " ");
}
}
/* Character */
for (i = 0; i < 16; i++) {
address = (start & 0xFFFFF0) + i;
value = mem_read_byte(mem, address);
if ((address >= start) && (address <= end)) {
if (isprint(value)) {
fprintf(fh, "%c", value);
} else {
fprintf(fh, ".");
}
} else {
fprintf(fh, " ");
}
}
fprintf(fh, "\n");
}
void mem_dump(FILE *fh, mem_t *mem, uint32_t start, uint32_t end)
{
uint32_t i;
mem_dump_16(fh, mem, start, end);
for (i = (start & 0xFFFFF0) + 16; i <= end; i += 16) {
mem_dump_16(fh, mem, i, end);
}
}