-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmem.c
More file actions
161 lines (124 loc) · 2.94 KB
/
mem.c
File metadata and controls
161 lines (124 loc) · 2.94 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
#include "mem.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <ctype.h>
#include "console.h"
#include "panic.h"
#ifdef MEM_BREAKPOINT
#include "debugger.h"
#endif /* MEM_BREAKPOINT */
void mem_init(mem_t *mem)
{
int i;
for (i = 0; i < MEM_SIZE_MAX; i++) {
mem->m[i] = 0x00;
}
for (i = 0; i < MEM_SIZE_MAX / MEM_SECTION; i++) {
mem->readonly[i] = false;
}
}
uint8_t mem_read(mem_t *mem, uint32_t address)
{
if (address >= MEM_SIZE_MAX) {
panic("Memory read above 1MB: 0x%08x\n", address);
return 0xFF;
} else {
return mem->m[address];
}
}
uint8_t mem_read_by_segment(mem_t *mem, uint16_t segment, uint16_t offset)
{
return mem_read(mem, ((segment << 4) + offset) & 0xFFFFF);
}
void mem_write(mem_t *mem, uint32_t address, uint8_t value)
{
if (address >= MEM_SIZE_MAX) {
panic("Memory write above 1MB: 0x%08x\n", address);
} else {
if (mem->readonly[address / MEM_SECTION] == false) {
mem->m[address] = value;
#ifdef MEM_BREAKPOINT
if ((int32_t)address == debugger_breakpoint_mem) {
panic("Memory write breakpoint: 0x%05x < 0x%02x\n", address, value);
}
#endif /* MEM_BREAKPOINT */
}
}
}
void mem_write_by_segment(mem_t *mem, uint16_t segment, uint16_t offset,
uint8_t value)
{
mem_write(mem, ((segment << 4) + offset) & 0xFFFFF, value);
}
int mem_load_rom(mem_t *mem, const char *filename, uint32_t address)
{
FILE *fh;
int c;
if (address >= MEM_SIZE_MAX) {
return -2;
}
fh = fopen(filename, "rb");
if (fh == NULL) {
console_exit();
fprintf(stderr, "fopen() for '%s' failed with errno: %d\n",
filename, errno);
return -1;
}
while ((c = fgetc(fh)) != EOF) {
mem->m[address] = c;
mem->readonly[address / MEM_SECTION] = true;
address++;
if (address >= MEM_SIZE_MAX) {
fclose(fh);
return 0;
}
}
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, "%05x ", start & 0xFFFF0);
/* Hex */
for (i = 0; i < 16; i++) {
address = (start & 0xFFFF0) + i;
value = mem_read(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 & 0xFFFF0) + i;
value = mem_read(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 & 0xFFFF0) + 16; i <= end; i += 16) {
mem_dump_16(fh, mem, i, end);
}
}