-
Notifications
You must be signed in to change notification settings - Fork 0
/
v68.c
266 lines (217 loc) · 6.47 KB
/
v68.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
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include "v68.h"
#include "v68ipl.h"
#include "v68io.h"
#include "v68periph.h"
#include "v68iocscall.h"
#include "musashi/m68kcpu.h"
struct v68 v68;
int v68_init(int clock, int ram_size, int sample_rate) {
memset(&v68, 0, sizeof(v68));
v68.log_dasm = 0;
v68.verbosity = 0;
v68.ram_size = ram_size;
v68.cpu_clock = clock;
v68.sound_touched = 0;
v68.sample_rate = sample_rate;
v68.ram = calloc(v68.ram_size, 1);
if(!v68.ram) return errno;
m68k_init();
m68k_set_cpu_type(M68K_CPU_TYPE_68000);
v68_io_init();
return 0;
}
void v68_boot() {
verbose1("v68_boot\n");
v68_ipl_init();
v68_human_init();
v68_periph_init();
v68_iocs_init();
m68k_pulse_reset();
}
void v68_cpu_reset_instr_cb(void) {
verbose2("RESET peripherals\n");
v68_periph_reset();
}
int v68_shutdown() {
v68_periph_deinit();
free(v68.ram);
return 0;
}
extern int m68ki_initial_cycles;
static void v68_run_sample() {
int64_t x = v68.cpu_clock + v68.cpu_cycle_remainder;
int cpu_tstates = x / v68.sample_rate;
verbose2("v68_run executing cpu_tstates=%d log_calls=%d running=%d sound_touched=%d\n", cpu_tstates, v68.log_calls, v68.running, v68.sound_touched);
int executed_cycles = m68k_execute(cpu_tstates, v68.log_dasm);
v68.cpu_cycle_remainder = x - executed_cycles * v68.sample_rate;
}
void v68_run() {
verbose1("v68_run cpu_clock=%d sound_touched=%d running=%d log_calls=%d\n", v68.cpu_clock, v68.sound_touched, v68.running, v68.log_calls);
v68.running = 1;
while(v68.running && !v68.sound_touched) {
v68_run_sample();
}
int extra_samples = 0;
for(int i = 0; i < extra_samples; i++) {
v68_run_sample();
}
}
int v68_fill_buffer(int samples, int16_t *bufL, int16_t *bufR) {
verbose1("v68_fill_buffer samples=%d running=%d\n", samples, v68.running);
memset(bufL, 0, samples * sizeof(*bufL));
memset(bufR, 0, samples * sizeof(*bufR));
for(int i = 0; i < samples; i++) {
// render one sample at a time
int64_t x = v68.cpu_clock + v68.cpu_cycle_remainder;
int cpu_tstates = x / v68.sample_rate;
int executed_cycles = m68k_execute(cpu_tstates, v68.log_dasm);
v68.cpu_cycle_remainder = x - executed_cycles * v68.sample_rate;
v68_periph_advance(executed_cycles);
v68_periph_render_sample(bufL++, bufR++);
}
return 0;
}
unsigned int m68k_read_memory_8(unsigned int addr) {
if(addr <= 0x0000ffff && !v68.reset_pulsed) {
uint8_t r = v68_ipl_read_8(addr + 0xff0000);
verbose3("READ8 BOOT 0x%08x = 0x%02x\n", addr, r);
return r;
}
if(addr >= 0x00fe0000 && addr <= 0x00ffffff) {
uint8_t r = v68_ipl_read_8(addr);
verbose3("READ8 IPL 0x%08x = 0x%02x\n", addr, r);
return r;
}
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
uint8_t r = v68_read_periph_8(addr);
verbose3("READ8 PERIPH 0x%08x = 0x%02x\n", addr, r);
return r;
}
if(addr >= v68.ram_size) {
fprintf(stderr, "READ8 ERROR 0x%08x\n", addr);
exit(1);
}
uint8_t r = v68.ram[addr];
verbose3("READ8 RAM 0x%08x = 0x%02x\n", addr, r);
return r;
}
unsigned int m68k_read_memory_16(unsigned int addr) {
if(addr <= 0x0000ffff && !v68.reset_pulsed) {
uint16_t r = v68_ipl_read_16(addr + 0xff0000);
verbose3("READ16 BOOT 0x%08x = 0x%04x\n", addr, r);
return r;
}
if(addr >= 0x00fe0000 && addr <= 0x00ffffff) {
uint16_t r = v68_ipl_read_16(addr);
verbose3("READ16 IPL 0x%08x = 0x%04x\n", addr, r);
return r;
}
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
uint16_t r = v68_read_periph_16(addr);
verbose3("READ16 PERIPH 0x%08x = 0x%04x\n", addr, r);
return r;
}
if(addr >= v68.ram_size - 1) {
fprintf(stderr, "READ16 ERROR 0x%08x\n", addr);
exit(1);
}
uint16_t r = (v68.ram[addr] << 8) | v68.ram[addr + 1];
verbose3("READ16 RAM 0x%08x = 0x%04x\n", addr, r);
return r;
}
unsigned int m68k_read_memory_32(unsigned int addr) {
if(addr <= 0x0000ffff && !v68.reset_pulsed) {
uint32_t r = v68_ipl_read_32(addr + 0xff0000);
verbose3("READ32 BOOT 0x%08x = 0x%08x\n", addr, r);
return r;
}
if(addr >= 0x00fe0000 && addr <= 0x00ffffff) {
uint32_t r = v68_ipl_read_32(addr);
verbose3("READ32 IPL 0x%08x = 0x%08x\n", addr, r);
return r;
}
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
uint32_t r = v68_read_periph_32(addr);
verbose3("READ32 PERIPH 0x%08x = 0x%08x\n", addr, r);
return r;
}
if(addr > v68.ram_size) {
fprintf(stderr, "READ32 ERROR 0x%08x\n", addr);
exit(1);
}
uint32_t r =
(v68.ram[addr] << 24) |
(v68.ram[addr+1] << 16) |
(v68.ram[addr+2] << 8) |
v68.ram[addr+3];
verbose3("READ32 RAM 0x%08x = 0x%08x\n", addr, r);
return r;
}
void m68k_write_memory_8(unsigned int addr, unsigned int data) {
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
verbose3("WRITE8 PERIPH 0x%08x = 0x%02x\n", addr, data);
v68_write_periph_8(addr, data);
return;
}
verbose3("WRITE8 RAM 0x%08x = 0x%02x\n", addr, data);
if(addr > v68.ram_size) {
fprintf(stderr, "Could not write RAM at 0x%08x = 0x%02x\n", addr, data);
exit(1);
}
v68.ram[addr] = data;
}
void m68k_write_memory_16(unsigned int addr, unsigned int data) {
verbose3("WRITE16 0x%08x = 0x%04x\n", addr, data);
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
verbose3("WRITE16 PERIPH 0x%08x = 0x%04x\n", addr, data);
v68_write_periph_16(addr, data);
return;
}
if(addr > v68.ram_size) {
fprintf(stderr, "Could not write RAM at 0x%08x = 0x%04x\n", addr, data);
exit(1);
}
v68.ram[addr++] = data >> 8;
v68.ram[addr] = data;
}
void m68k_write_memory_32(unsigned int addr, unsigned int data) {
if(addr >= 0x00e80000 && addr < 0x00eb0000) {
verbose3("WRITE32 PERIPH 0x%08x = 0x%08x\n", addr, data);
v68_write_periph_32(addr, data);
return;
}
verbose3("WRITE32 RAM 0x%08x = 0x%08x\n", addr, data);
if(addr > v68.ram_size) {
fprintf(stderr, "Could not write RAM at 0x%08x = 0x%08x\n", addr, data);
exit(1);
}
v68.ram[addr++] = data >> 24;
v68.ram[addr++] = data >> 16;
v68.ram[addr++] = data >> 8;
v68.ram[addr] = data;
}
unsigned int m68k_read_disassembler_8 (unsigned int addr) {
return m68k_read_memory_8(addr);
}
unsigned int m68k_read_disassembler_16 (unsigned int addr) {
return m68k_read_memory_16(addr);
}
unsigned int m68k_read_disassembler_32 (unsigned int addr) {
return m68k_read_memory_32(addr);
}
int v68_trap(int which) {
verbose1("v68_trap which=%d d0=0x%08x\n", which, m68k_get_reg(NULL, M68K_REG_D0));
switch(which) {
case 15:
/* IOCS */
return v68_iocs_call(m68k_get_reg(NULL, M68K_REG_D0));
default:
verbose2("v68_trap unknown #%d d0=0x%08x\n", which, m68k_get_reg(0, M68K_REG_D0));
}
return 1;
}