-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcdc.c
247 lines (220 loc) · 5.42 KB
/
lcdc.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
#include <stdlib.h>
#include "gnuboy.h"
#include "defs.h"
#include "hw.h"
#include "cpu.h"
#include "regs.h"
#include "lcd.h"
#define C (cpu.lcdc)
/*
* stat_trigger updates the STAT interrupt line to reflect whether any
* of the conditions set to be tested (by bits 3-6 of R_STAT) are met.
* This function should be called whenever any of the following occur:
* 1) LY or LYC changes.
* 2) A state transition affects the low 2 bits of R_STAT (see below).
* 3) The program writes to the upper bits of R_STAT.
* stat_trigger also updates bit 2 of R_STAT to reflect whether LY=LYC.
*/
void stat_trigger()
{
static const int condbits[4] = { 0x08, 0x10, 0x20, 0x00 };
int flag = 0;
if (R_LY == R_LYC)
{
R_STAT |= 0x04;
if (R_STAT & 0x40) flag = IF_STAT;
}
else R_STAT &= ~0x04;
if (R_STAT & condbits[R_STAT&3]) flag = IF_STAT;
if (!(R_LCDC & 0x80)) flag = 0;
hw_interrupt(flag, IF_STAT);
}
void stat_write(byte b)
{
R_STAT = (R_STAT & 0x07) | (b & 0x78);
if (!hw.cgb && !(R_STAT & 2)) /* DMG STAT write bug => interrupt */
hw_interrupt(IF_STAT, IF_STAT);
stat_trigger();
}
/*
* stat_change is called when a transition results in a change to the
* LCD STAT condition (the low 2 bits of R_STAT). It raises or lowers
* the VBLANK interrupt line appropriately and calls stat_trigger to
* update the STAT interrupt line.
*/
/* FIXME: function now will only lower vblank interrupt, description
does not match anymore */
static void stat_change(int stat)
{
stat &= 3;
R_STAT = (R_STAT & 0x7C) | stat;
if (stat != 1) hw_interrupt(0, IF_VBLANK);
/* hw_interrupt((stat == 1) ? IF_VBLANK : 0, IF_VBLANK); */
stat_trigger();
}
void lcdc_change(byte b)
{
byte old = R_LCDC;
R_LCDC = b;
if ((R_LCDC ^ old) & 0x80) /* lcd on/off change */
{
R_LY = 0;
stat_change(2);
C = 40;
lcd_begin();
}
}
/*
LCD controller operates with 154 lines per frame, of which lines
#0..#143 are visible and lines #144..#153 are processed in vblank
state.
lcdc_trans() performs cyclic switching between lcdc states (OAM
search/data transfer/hblank/vblank), updates system state and time
counters accordingly. Control is returned to the caller immediately
after a step that sets LCDC ahead of CPU, so that LCDC is always
ahead of CPU by one state change. Once CPU reaches same point in
time, LCDC is advanced through the next step.
For each visible line LCDC goes through states 2 (search), 3
(transfer) and then 0 (hblank). At the first line of vblank LCDC
is switched to state 1 (vblank) and remains there till line #0 is
reached (execution is still interrupted after each line so that
function could return if it ran out of time).
Irregardless of state switches per line, time spent in each line
adds up to exactly 228 double-speed cycles (109us).
LCDC emulation begins with R_LCDC set to "operation enabled", R_LY
set to line #0 and R_STAT set to state-hblank. cpu.lcdc is also
set to zero, to begin emulation we call lcdc_trans() once to
force-advance LCD through the first iteration.
Docs aren't entirely accurate about time intervals within single
line; besides that, intervals will vary depending on number of
sprites on the line and probably other factors. States 1, 2 and 3
do not require precise sub-line CPU-LCDC sync, but state 0 might do.
*/
/* lcdc_trans()
Main LCDC emulation routine
*/
void lcdc_trans()
{
/* FIXME: lacks clarity;
try and break into two switch() blocks
switch(state) {
case 0:
if(vblank) state = 1;
else state = 2;
case 1:
state = 2;
case 2:
state = 3;
case 3:
state = 0;
}
switch(state) {
case 0:
handle hblank
case 1:
handle vblank
case 2:
handle search
case 3:
handle transfer
}
*/
if (!(R_LCDC & 0x80))
{
/* LCDC operation disabled (short route) */
while (C <= 0)
{
switch ((byte)(R_STAT & 3))
{
case 0: /* hblank */
case 1: /* vblank */
stat_change(2);
C += 40;
break;
case 2: /* search */
stat_change(3);
C += 86;
break;
case 3: /* transfer */
stat_change(0);
/* FIXME: check docs; HDMA might require operating LCDC */
if (hw.hdma & 0x80)
hw_hdma();
else
C += 102;
break;
}
return;
}
}
while (C <= 0)
{
switch ((byte)(R_STAT & 3))
{
case 1:
/* vblank -> */
if (!(hw.ilines & IF_VBLANK))
{
C += 218;
hw_interrupt(IF_VBLANK, IF_VBLANK);
break;
}
if (R_LY == 0)
{
lcd_begin();
stat_change(2); /* -> search */
C += 40;
break;
}
else if (R_LY < 152)
C += 228;
else if (R_LY == 152)
/* Handling special case on the last line; see
docs/HACKING */
C += 28;
else
{
R_LY = -1;
C += 200;
}
R_LY++;
stat_trigger();
break;
case 2:
/* search -> */
lcd_refreshline();
stat_change(3); /* -> transfer */
C += 86;
break;
case 3:
/* transfer -> */
stat_change(0); /* -> hblank */
if (hw.hdma & 0x80)
hw_hdma();
/* FIXME -- how much of the hblank does hdma use?? */
/* else */
C += 102;
break;
case 0:
/* hblank -> */
if (++R_LY >= 144)
{
/* FIXME: pick _one_ place to trigger vblank interrupt
this better be done here or within stat_change(),
otherwise CPU will have a chance to run for some time
before interrupt is triggered */
if (cpu.halt)
{
hw_interrupt(IF_VBLANK, IF_VBLANK);
C += 228;
}
else C += 10;
stat_change(1); /* -> vblank */
break;
}
stat_change(2); /* -> search */
C += 40;
break;
}
}
}