-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmemory.c
257 lines (211 loc) · 6.1 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* TOSEMU - an emulated environment for TOS applications
* Copyright (C) 2014 Johan Thelin <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "memory.h"
#include <stdio.h>
#include <stdlib.h>
#include "tossystem.h"
#include "cpu.h"
#include "m68k.h"
/* Memory area linked list head */
static struct _memarea *head = 0;
/* Support functions */
static uint8_t ptr_read(struct _memarea *area, uint32_t address)
{
return ((uint8_t *)area->ptr)[address - area->base];
}
static void ptr_write(struct _memarea *area, uint32_t address, uint8_t value)
{
((uint8_t *)area->ptr)[address - area->base] = value;
}
int add_ptr_memory_area(char *name, uint8_t flags, uint32_t base, uint32_t len, void *ptr)
{
return add_fnct_memory_area(name, flags, base, len, ptr, ptr_read, ptr_write);
}
int add_fnct_memory_area(char *name, uint8_t flags, uint32_t base, uint32_t len, void *ptr, uint8_t (*read)(struct _memarea*, uint32_t), void (*write)(struct _memarea*, uint32_t, uint8_t))
{
struct _memarea *area;
/* TODO ensure that we do not have memory area collisions */
area = malloc(sizeof(struct _memarea));
if (!area) {
printf("Failed to allocate memory area for 0x%x\n", base);
return 1;
}
area->base = base;
area->len = len;
area->read = read;
area->write = write;
area->ptr = ptr;
area->flags = flags;
area->next = head;
head = area;
return 0;
}
int remove_memory_area(uint32_t base)
{
struct _memarea *ptr = head;
struct _memarea *prev = 0;
while (ptr)
{
if (ptr->base == base) {
if (prev)
prev->next = ptr->next;
else
head = ptr->next;
free(ptr);
return 0;
}
prev = ptr;
}
printf("Failed to remove memory area at 0x%x\n", base);
return 1;
}
void reset_memory()
{
while (head)
remove_memory_area(head->base);
}
struct _memarea *find_memarea(uint32_t address)
{
struct _memarea *area = head;
while(area)
{
if (address >= area->base && address < area->base + area->len)
break;
area = area->next;
}
return area;
}
void *tos_mem_to_host_mem(uint32_t address)
{
struct _memarea *area = find_memarea(address);
if (!area) {
halt_execution();
printf("Attempted to get direct access to non-existing memory at 0x%x\n", address);
return 0;
}
if (area->write != ptr_write || area->read != ptr_read)
{
halt_execution();
printf("Attempted to get direct access to non-mapped memory at 0x%x\n", address);
return 0;
}
return &(((uint8_t *)area->ptr)[address - area->base]);
}
/* These are the real read/write functions */
uint8_t tos_read(uint32_t address)
{
struct _memarea *area = find_memarea(address);
uint8_t mask;
if (!area) {
halt_execution();
printf("Attempted to read non-existing memory at 0x%x\n", address);
return 0;
}
/* Is the CPU in supervisor mode? */
if (is_supervisor_mode_enabled())
mask = MEMORY_READ | MEMORY_SUPERREAD;
else
mask = MEMORY_READ;
if ((area->flags & mask) != 0)
return area->read(area, address);
else {
halt_execution();
printf("Attempted to read non-readable memory at 0x%x\n", address);
return 0;
}
}
void tos_write(uint32_t address, uint8_t value)
{
struct _memarea *area = find_memarea(address);
uint8_t mask;
if (!area) {
halt_execution();
printf("Attempted to write to non-existing memory at 0x%x\n", address);
return;
}
/* Is the CPU in supervisor mode? */
if (is_supervisor_mode_enabled())
mask = MEMORY_WRITE | MEMORY_SUPERWRITE;
else
mask = MEMORY_WRITE;
if ((area->flags & mask) != 0)
area->write(area, address, value);
else {
halt_execution();
printf("Attempted to write to non-writeable memory at 0x%x\n", address);
}
}
/* These are the read/write functions used by Musashi */
unsigned int m68k_read_disassembler_8(unsigned int address)
{
return tos_read(address);
}
unsigned int m68k_read_disassembler_16(unsigned int address)
{
unsigned int res = 0;
int i;
for(i=0; i<2; ++i) {
res = res << 8;
res |= tos_read(address+i);
}
return res;
}
unsigned int m68k_read_disassembler_32(unsigned int address)
{
unsigned int res = 0;
int i;
for(i=0; i<4; ++i) {
res = res << 8;
res |= tos_read(address+i);
}
return res;
}
unsigned int m68k_read_memory_8(unsigned int address)
{
return m68k_read_disassembler_8(address);
}
unsigned int m68k_read_memory_16(unsigned int address)
{
return m68k_read_disassembler_16(address);
}
unsigned int m68k_read_memory_32(unsigned int address)
{
return m68k_read_disassembler_32(address);
}
void m68k_write_memory_8(unsigned int address, unsigned int value)
{
tos_write(address, value);
}
void m68k_write_memory_16(unsigned int address, unsigned int value)
{
int i;
for(i=0; i<2; ++i) {
tos_write(address+1-i, value&0xff);
value = value >> 8;
}
}
void m68k_write_memory_32(unsigned int address, unsigned int value)
{
int i;
for(i=0; i<4; ++i) {
tos_write(address+3-i, value&0xff);
value = value >> 8;
}
}