-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitbox_emu.c
173 lines (140 loc) · 3.12 KB
/
bitbox_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
#include "gnuboy.h"
#include "defs.h"
#include "regs.h"
#include "hw.h"
#include "cpu.h"
#include "sound.h"
#include "mem.h"
#include "lcd.h"
#include "rtc.h"
#include "rc.h"
#include "bitbox.h"
/*
static int framelen = 16743;
static int framecount;
*/
void emu_frame();
void emu_line(); // in lcd
void emu_init(int);
void gamepad_poll();
void menu_frame();
void menu_line();
void menu_setup();
extern int menu_done, menu_choice;
extern int bitbox_rom_load(int rom_id);
void game_init()
{
menu_setup();
}
int emu_started=0;
void game_frame()
{
if (emu_started) {
emu_frame();
}
else
{
menu_frame();
if (menu_done) emu_init(menu_choice);
}
}
void graph_frame(void) {}
void graph_line()
{
if (emu_started) emu_line();
else menu_line();
}
/*
* emu_reset is called to initialize the state of the emulated
* system. It should set cpu registers, hardware registers, etc. to
* their appropriate values at powerup time.
*/
void emu_reset()
{
hw_reset();
lcd_reset();
cpu_reset();
mbc_reset();
sound_reset();
}
/* emu_step()
make CPU catch up with LCDC
*/
void emu_step()
{
cpu_emulate(cpu.lcdc);
}
/*
Time intervals throughout the code, unless otherwise noted, are
specified in double-speed machine cycles (2MHz), each unit
roughly corresponds to 0.477us.
For CPU each cycle takes 2dsc (0.954us) in single-speed mode
and 1dsc (0.477us) in double speed mode.
Although hardware gbc LCDC would operate at completely different
and fixed frequency, for emulation purposes timings for it are
also specified in double-speed cycles.
line = 228 dsc (109us)
frame (154 lines) = 35112 dsc (16.7ms)
of which
visible lines x144 = 32832 dsc (15.66ms)
vblank lines x10 = 2280 dsc (1.08ms)
*/
void emu_init(int game_id)
{
vid_init();
pcm_init();
vid_preinit();
// load ROM & load palette
bitbox_rom_load(game_id);
emu_reset();
vid_begin();
lcd_begin();
emu_started = 1; // at the end , start line blitting
}
// void *timer = sys_timer();
// int delay;
void emu_frame(void)
{
/* FRAME BEGIN */
/* FIXME: djudging by the time specified this was intended
to emulate through vblank phase which is handled at the
end of the loop. */
cpu_emulate(2280);
/* FIXME: R_LY >= 0; comparsion to zero can also be removed
altogether, R_LY is always 0 at this point */
while (R_LY > 0 && R_LY < 144)
{
/* Step through visible line scanning phase */
emu_step();
}
/* VBLANK BEGIN */
vid_end();
rtc_tick();
sound_mix();
pcm_submit();
/* pcm_submit() introduces delay, if it fails we use
sys_sleep() instead */
/* timebase is exclusively done with pcm submit. we shall sync with LCD !
if (!pcm_submit())
{
delay = framelen - sys_elapsed(timer);
sys_sleep(delay);
sys_elapsed(timer);
}*/
// doevents(); // does not handle extra events
gamepad_poll();
vid_begin();
if (!(R_LCDC & 0x80)) {
/* LCDC operation stopped */
/* FIXME: djudging by the time specified, this is
intended to emulate through visible line scanning
phase, even though we are already at vblank here */
cpu_emulate(32832);
}
while (R_LY > 0) {
/* Step through vblank phase */
emu_step();
}
/* VBLANK END */
/* FRAME END */
}