-
Notifications
You must be signed in to change notification settings - Fork 3
/
emu.c
336 lines (289 loc) · 7.73 KB
/
emu.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "emu.h"
#ifdef N64
#include "m64k/m64k.h"
#else
#include "m68k.h"
#endif
#include "hw.h"
#include "video.h"
#include "roms.h"
#include "platform.h"
static int cpu_trace_count = 0;
void cpu_trace(unsigned int pc) {
(void)cpu_trace_count;
#ifndef N64
if (cpu_trace_count == 0) {
m68k_set_instr_hook_callback(NULL);
return;
}
char inst[1024];
m68k_disassemble(inst, pc, M68K_CPU_TYPE_68000);
debugf("trace: %06x %-30s", pc, inst);
if (strstr(inst, "A0")) debugf("A0=%08x ", m68k_get_reg(NULL, M68K_REG_A0));
if (strstr(inst, "A1")) debugf("A1=%08x ", m68k_get_reg(NULL, M68K_REG_A1));
if (strstr(inst, "A2")) debugf("A2=%08x ", m68k_get_reg(NULL, M68K_REG_A2));
if (strstr(inst, "A3")) debugf("A3=%08x ", m68k_get_reg(NULL, M68K_REG_A3));
if (strstr(inst, "A4")) debugf("A4=%08x ", m68k_get_reg(NULL, M68K_REG_A4));
if (strstr(inst, "A5")) debugf("A5=%08x ", m68k_get_reg(NULL, M68K_REG_A5));
if (strstr(inst, "A6")) debugf("A6=%08x ", m68k_get_reg(NULL, M68K_REG_A6));
if (strstr(inst, "A7")) debugf("A7=%08x ", m68k_get_reg(NULL, M68K_REG_A7));
if (strstr(inst, "D0")) debugf("D0=%08x ", m68k_get_reg(NULL, M68K_REG_D0));
if (strstr(inst, "D1")) debugf("D1=%08x ", m68k_get_reg(NULL, M68K_REG_D1));
if (strstr(inst, "D2")) debugf("D2=%08x ", m68k_get_reg(NULL, M68K_REG_D2));
if (strstr(inst, "D3")) debugf("D3=%08x ", m68k_get_reg(NULL, M68K_REG_D3));
if (strstr(inst, "D4")) debugf("D4=%08x ", m68k_get_reg(NULL, M68K_REG_D4));
if (strstr(inst, "D5")) debugf("D5=%08x ", m68k_get_reg(NULL, M68K_REG_D5));
if (strstr(inst, "D6")) debugf("D6=%08x ", m68k_get_reg(NULL, M68K_REG_D6));
if (strstr(inst, "D7")) debugf("D7=%08x ", m68k_get_reg(NULL, M68K_REG_D7));
debugf("\n");
cpu_trace_count--;
#endif
}
void cpu_start_trace(int cnt) {
#ifndef N64
m68k_set_instr_hook_callback(cpu_trace);
#endif
cpu_trace_count = cnt;
}
static int g_frame;
#ifdef N64
m64k_t m64k;
#endif
static uint64_t g_clock, g_clock_framebegin;
static uint64_t m68k_clock;
static EmuEvent events[MAX_EVENTS];
uint32_t profile_hw_io;
uint32_t profile_dma_load;
static uint64_t m68k_exec(uint64_t clock) {
clock /= M68K_CLOCK_DIV;
if (clock > m68k_clock) {
#ifdef N64
debugf("m68k_exec: %d\n", (int)(clock - m68k_clock));
m68k_clock = m64k_run(&m64k, clock);
#else
m68k_clock += m68k_execute(clock - m68k_clock);
#endif
}
return m68k_clock * M68K_CLOCK_DIV;
}
// Return the next event that must be executed
static EmuEvent* next_event() {
EmuEvent *e = NULL;
for (int i=0;i<MAX_EVENTS;i++) {
if (!events[i].cb) continue;
if (!e || events[i].clock < e->clock) e=&events[i];
}
return e;
}
int emu_add_event(int64_t clock, EmuEventCb cb, void *cbarg) {
for (int i=0;i<MAX_EVENTS;i++) {
if (events[i].cb) continue;
events[i].clock = clock;
events[i].cb = cb;
events[i].cbarg = cbarg;
events[i].current = false;
return i;
}
assert(0);
}
void emu_change_event(int event_id, int64_t newclock) {
events[event_id].clock = newclock;
if (events[event_id].current) {
#ifdef N64
m64k_run_stop(&m64k);
#else
m68k_end_timeslice();
#endif
}
}
int64_t emu_clock(void) {
#ifdef N64
return m64k_get_clock(&m64k) * M68K_CLOCK_DIV;
#else
return g_clock + m68k_cycles_run() * M68K_CLOCK_DIV;
#endif
}
int64_t emu_clock_frame(void) {
return emu_clock() - g_clock_framebegin;
}
void emu_cpu_reset(void) {
#ifdef N64
m64k_pulse_reset(&m64k);
#else
m68k_pulse_reset();
#endif
}
uint32_t emu_pc(void) {
#ifdef N64
return m64k_get_pc(&m64k) & 0xFFFFFF;
#else
return m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF;
#endif
}
void emu_cpu_irq(int irq, bool on) {
#ifdef N64
m64k_set_virq(&m64k, irq, on);
#else
m68k_set_virq(irq, on);
#endif
}
#ifdef N64
int cpu_irqack(void *ctx, int level)
{
// On NeoGeo hardware, interrupts must be manually acknowledged via a write
// to register 0x3C000C. So we do nothing here.
// NOTE: we still must register this hook, otherwise the m64k core will
// by default auto-acnowledge the interrupts.
return 0;
}
#endif
uint32_t emu_vblank_start(void* arg) {
emu_cpu_irq(1, true);
hw_vblank();
debugf("[EMU] VBlank - clock:%lld clock_frame:%lld\n", emu_clock(), emu_clock_frame());
return FRAME_CLOCK;
}
uint32_t render_time;
uint32_t emu_render(void *arg) {
#ifdef N64
if (CONFIG_FRAMESKIP_MODE == 2) {
extern volatile int N64_FRAME;
const int MAX_SKIP = 4;
static int skip = 0;
if (N64_FRAME > g_frame) {
skip++;
if (skip < MAX_SKIP) {
debugf("[RENDER] skip frame\n");
return FRAME_CLOCK;
}
debugf("[RENDER] max skip\n");
skip = 0;
disable_interrupts();
N64_FRAME = g_frame;
enable_interrupts();
}
}
#endif
if (CONFIG_FRAMESKIP_MODE == 1) {
if (g_frame & 1) {
debugf("[RENDER] skip frame\n");
return FRAME_CLOCK;
}
}
debugf("[RENDER] render\n");
#ifdef N64
uint32_t t0 = TICKS_READ();
#endif
plat_beginframe();
video_render();
plat_endframe();
rom_next_frame();
#ifdef N64
render_time = TICKS_DISTANCE(t0, TICKS_READ());
#endif
return FRAME_CLOCK;
}
void emu_run_frame(void) {
uint64_t vsync = g_clock_framebegin + FRAME_CLOCK;
EmuEvent *e;
// Run all events that are scheduled before next vsync
while ((e = next_event()) && (e->clock < vsync)) {
e->current = true;
g_clock = m68k_exec(e->clock);
e->current = false;
// Call the event callback, and check if it must be repeated.
if (g_clock >= e->clock) {
uint32_t repeat = e->cb(e->cbarg);
if (repeat != 0) e->clock += repeat;
else e->cb = NULL;
}
}
while (g_clock < vsync)
g_clock = m68k_exec(vsync);
// Frame completed
debugf("[EMU] Frame completed: %d (vsync: %llu)\n", g_frame, vsync);
g_frame++;
g_clock_framebegin += FRAME_CLOCK;
}
int main(int argc, char *argv[]) {
#ifndef N64
if (argc < 2) {
fprintf(stderr, "Usage:\n mvs64 <romdir>\n");
return 1;
}
#else
argc = 0; argv = NULL;
#endif
plat_init(44100, FPS);
plat_enable_video(true);
#ifdef N64
rom_load("rom:/");
#else
rom_load(argv[1]);
#endif
#ifdef N64
m64k_init(&m64k);
m64k_set_hook_irqack(&m64k, cpu_irqack, NULL);
#else
m68k_init();
#endif
hw_init();
g_clock = 0;
#ifdef N64
m64k_pulse_reset(&m64k);
#else
m68k_set_cpu_type(M68K_CPU_TYPE_68000);
m68k_pulse_reset();
#endif
m68k_clock = 0;
emu_add_event(LINE_CLOCK*24, emu_render, NULL);
emu_add_event(LINE_CLOCK*248, emu_vblank_start, NULL);
#ifdef N64
uint32_t fps_frame = 0;
uint32_t fps_time = TICKS_READ();
#endif
while (1) {
render_time = 0;
profile_hw_io = 0;
profile_dma_load = 0;
#ifdef N64
uint32_t t0 = TICKS_READ();
#endif
emu_run_frame();
if (!plat_poll()) break;
#ifdef N64
uint32_t emu_time = TICKS_DISTANCE(t0, TICKS_READ());
debugf("[PROFILE] cpu:%.2f%% io:%.2f%% draw:%.2f%% dma:%.2f%% PC:%06lx\n",
(float)emu_time * 100.f / (float)(TICKS_PER_SECOND / 60),
(float)profile_hw_io * 100.f / (float)(TICKS_PER_SECOND / 60),
(float)render_time * 100.f / (float)(TICKS_PER_SECOND / 60),
(float)profile_dma_load * 100.f / (float)(TICKS_PER_SECOND / 60),
#ifdef N64
m64k_get_pc(&m64k));
#else
(uint32_t)m68k_get_reg(NULL, M68K_REG_PC));
#endif
#endif
rom_next_frame();
#ifdef N64
uint32_t curtime = TICKS_READ();
if (TICKS_DISTANCE(fps_time, curtime) > TICKS_FROM_MS(1000)) {
debugf("FPS: %.1f\n", (g_frame - fps_frame) * (float)TICKS_PER_SECOND / TICKS_DISTANCE(fps_time, curtime));
fps_frame = g_frame;
fps_time = curtime;
}
#endif
}
debugf("end\n");
cpu_start_trace(1000);
m68k_exec(g_clock+100);
#ifndef N64
FILE *f = fopen("vram.dump", "wb");
fwrite(VIDEO_RAM, 1, sizeof(VIDEO_RAM), f);
fclose(f);
#endif
plat_save_screenshot("screen.bmp");
}