-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
196 lines (174 loc) · 4.88 KB
/
memory.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memory.h"
#include "R5900.h"
#define MEMSIZE 32 * 1024 * 1024
void dumpasm(u32);
u8 memory[MEMSIZE];
u8 tags[MEMSIZE];
void mem_init()
{
memset(memory, 0, MEMSIZE);
memset(tags, 0, MEMSIZE);
}
void load_section(u32 mem, u8 *bytes, u32 size)
{
memcpy(memory + mem, bytes, size);
}
void dump_section(u32 mem, u8 *bytes, u32 size)
{
memcpy(bytes, memory + mem, size);
}
int is_zero(u32 mem)
{
return memory[mem] ? 0 : 1;
}
int memRead8(u32 mem, u8 *out)
{
switch ((mem & 0xf8000000) >> 24) {
case 0: /* default mem mapping */
break;
case 8: /* first copy */
mem &= 0xf7ffffff;
break;
default:
printf("\nUnknown memory mapping: %08X (reading at %08X)\n", mem & 0xf8000000, mem);
dumpasm(cpuRegs.pc - 4);
exit(-1);
}
if (mem >= MEMSIZE) {
printf("\nOut of range memory read: %08X at %08X\n", mem, cpuRegs.pc - 4);
dumpasm(cpuRegs.pc - 4);
exit(-1);
}
*out = memory[mem];
tag_read(mem);
return 0;
}
void memWrite8(u32 mem, u8 value)
{
switch ((mem & 0xf8000000) >> 24) {
case 0: /* default mem mapping */
break;
case 8: /* first copy */
mem &= 0xf7ffffff;
break;
default:
printf("\nUnknown memory mapping: %08X (writing at %08X)\n", mem & 0xf8000000, mem);
dumpasm(cpuRegs.pc - 4);
exit(-1);
}
if (mem >= MEMSIZE) {
printf("\nOut of range memory write: %08X at %08X\n", mem, cpuRegs.pc - 4);
dumpasm(cpuRegs.pc - 4);
exit(-1);
}
memory[mem] = value;
tag_write(mem);
}
void tag_read(u32 mem)
{
tags[mem] |= 1;
}
void tag_write(u32 mem)
{
tags[mem] |= 2;
}
int has_read(u32 mem)
{
return tags[mem] & 1 ? 1 : 0;
}
int has_write(u32 mem)
{
return tags[mem] & 2 ? 1 : 0;
}
/* ------- */
int memRead16(u32 mem, u16 *out)
{
memRead8(mem + 0, ((u8 *)out) + 0);
memRead8(mem + 1, ((u8 *)out) + 1);
return 0;
}
int memRead32(u32 mem, u32 *out)
{
memRead8(mem + 0, ((u8 *)out) + 0);
memRead8(mem + 1, ((u8 *)out) + 1);
memRead8(mem + 2, ((u8 *)out) + 2);
memRead8(mem + 3, ((u8 *)out) + 3);
return 0;
}
int memRead64(u32 mem, u64 *out)
{
memRead8(mem + 0, ((u8 *)out) + 0);
memRead8(mem + 1, ((u8 *)out) + 1);
memRead8(mem + 2, ((u8 *)out) + 2);
memRead8(mem + 3, ((u8 *)out) + 3);
memRead8(mem + 4, ((u8 *)out) + 4);
memRead8(mem + 5, ((u8 *)out) + 5);
memRead8(mem + 6, ((u8 *)out) + 6);
memRead8(mem + 7, ((u8 *)out) + 7);
return 0;
}
int memRead128(u32 mem, u64 *out)
{
memRead8(mem + 0, ((u8 *)out) + 0);
memRead8(mem + 1, ((u8 *)out) + 1);
memRead8(mem + 2, ((u8 *)out) + 2);
memRead8(mem + 3, ((u8 *)out) + 3);
memRead8(mem + 4, ((u8 *)out) + 4);
memRead8(mem + 5, ((u8 *)out) + 5);
memRead8(mem + 6, ((u8 *)out) + 6);
memRead8(mem + 7, ((u8 *)out) + 7);
memRead8(mem + 8, ((u8 *)out) + 8);
memRead8(mem + 9, ((u8 *)out) + 9);
memRead8(mem + 10, ((u8 *)out) + 10);
memRead8(mem + 11, ((u8 *)out) + 11);
memRead8(mem + 12, ((u8 *)out) + 12);
memRead8(mem + 13, ((u8 *)out) + 13);
memRead8(mem + 14, ((u8 *)out) + 14);
memRead8(mem + 15, ((u8 *)out) + 15);
return 0;
}
void memWrite16(u32 mem, u16 value)
{
memWrite8(mem + 0, *(((u8 *)&value) + 0));
memWrite8(mem + 1, *(((u8 *)&value) + 1));
}
void memWrite32(u32 mem, u32 value)
{
memWrite8(mem + 0, *(((u8 *)&value) + 0));
memWrite8(mem + 1, *(((u8 *)&value) + 1));
memWrite8(mem + 2, *(((u8 *)&value) + 2));
memWrite8(mem + 3, *(((u8 *)&value) + 3));
}
void memWrite64(u32 mem, u64 value)
{
memWrite8(mem + 0, *(((u8 *)&value) + 0));
memWrite8(mem + 1, *(((u8 *)&value) + 1));
memWrite8(mem + 2, *(((u8 *)&value) + 2));
memWrite8(mem + 3, *(((u8 *)&value) + 3));
memWrite8(mem + 4, *(((u8 *)&value) + 4));
memWrite8(mem + 5, *(((u8 *)&value) + 5));
memWrite8(mem + 6, *(((u8 *)&value) + 6));
memWrite8(mem + 7, *(((u8 *)&value) + 7));
}
void memWrite128(u32 mem, u64 *value)
{
memWrite8(mem + 0, *(((u8 *)&value) + 0));
memWrite8(mem + 1, *(((u8 *)&value) + 1));
memWrite8(mem + 2, *(((u8 *)&value) + 2));
memWrite8(mem + 3, *(((u8 *)&value) + 3));
memWrite8(mem + 4, *(((u8 *)&value) + 4));
memWrite8(mem + 5, *(((u8 *)&value) + 5));
memWrite8(mem + 6, *(((u8 *)&value) + 6));
memWrite8(mem + 7, *(((u8 *)&value) + 7));
memWrite8(mem + 8, *(((u8 *)&value) + 8));
memWrite8(mem + 9, *(((u8 *)&value) + 9));
memWrite8(mem + 10, *(((u8 *)&value) + 10));
memWrite8(mem + 11, *(((u8 *)&value) + 11));
memWrite8(mem + 12, *(((u8 *)&value) + 12));
memWrite8(mem + 13, *(((u8 *)&value) + 13));
memWrite8(mem + 14, *(((u8 *)&value) + 14));
memWrite8(mem + 15, *(((u8 *)&value) + 15));
}