fix crash when specifying --source on command line
[rofl0r-gnuboy.git] / emu.c
blobd77c77def576dfea4c355e7dffb8d14064387262
4 #include "defs.h"
5 #include "regs.h"
6 #include "hw.h"
7 #include "cpu.h"
8 #include "mem.h"
9 #include "lcd.h"
10 #include "rc.h"
11 #include "rtc.h"
12 #include "sys.h"
13 #include "sound.h"
14 #include "cpu.h"
17 static int framelen = 16743;
18 static int framecount;
19 static int paused;
21 rcvar_t emu_exports[] =
23 RCV_INT("framelen", &framelen, ""),
24 RCV_INT("framecount", &framecount, ""),
25 RCV_END
30 void emu_pause(int dopause) {
31 paused = dopause;
34 int emu_paused(void) {
35 return paused;
38 void emu_init()
45 * emu_reset is called to initialize the state of the emulated
46 * system. It should set cpu registers, hardware registers, etc. to
47 * their appropriate values at powerup time.
50 void emu_reset()
52 hw_reset();
53 lcd_reset();
54 cpu_reset();
55 mbc_reset();
56 sound_reset();
57 mem_mapbootrom();
64 void emu_step()
66 cpu_emulate(cpu.lcdc);
71 /* This mess needs to be moved to another module; it's just here to
72 * make things work in the mean time. */
74 void *sys_timer();
76 void emu_run()
78 void *timer = sys_timer();
79 int delay;
81 vid_begin();
82 lcd_begin();
83 for (;;)
85 cpu_emulate(2280);
86 while (R_LY > 0 && R_LY < 144)
87 emu_step();
89 vid_end();
90 rtc_tick();
91 sound_mix();
92 if (!pcm_submit())
94 delay = framelen - sys_elapsed(timer);
95 sys_sleep(delay);
96 sys_elapsed(timer);
98 doevents();
99 if (paused) return;
100 vid_begin();
101 if (framecount) { if (!--framecount) die("finished\n"); }
102 if (!(R_LCDC & 0x80))
103 cpu_emulate(32832);
105 while (R_LY > 0) /* wait for next frame */
106 emu_step();