-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.c
335 lines (272 loc) · 8.68 KB
/
video.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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include "xgbc.h"
#define FRAME_DROP 400
static SDL_Surface *screen, *vscreen;
extern struct io io_state;
extern bool has_cgb, lcd_on;
extern bool is_random;
extern uint8_t *full_vidram, *oam;
// Background palette, object palette
uint16_t bpalette[32], opalette[32];
// Internal background palette, internal object palette
uint32_t ibpalette[32], iopalette[32];
uint8_t *btm[2], *bwtd[2], *wtm[2];
#define vpb ((uint32_t *)vscreen->pixels)
#define WIDTH 160
#define HEIGHT 144
extern unsigned keystates;
void init_video(unsigned mult)
{
(void)mult;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
{
fprintf(stderr, "Konnte SDL nicht initialisieren: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_ASYNCBLIT);
if (screen == NULL)
{
fprintf(stderr, "Konnte keine SDL-Ausgabe öffnen: %s\n", SDL_GetError());
exit(1);
}
SDL_PixelFormat vscr_format = {
.BitsPerPixel = 32,
.BytesPerPixel = 4,
.Rshift = 16,
.Gshift = 8,
.Bshift = 0,
.Rmask = 0xFF0000,
.Gmask = 0x00FF00,
.Bmask = 0x0000FF,
};
vscreen = SDL_ConvertSurface(screen, &vscr_format, SDL_HWSURFACE | SDL_ASYNCBLIT);
if (vscreen == NULL)
{
fprintf(stderr, "Konnte vscreen nicht erstellen: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption("xgbcdyna", NULL);
btm[0] = &full_vidram[0x1800];
btm[1] = &full_vidram[0x3800];
bwtd[0] = &full_vidram[0x0000];
bwtd[1] = &full_vidram[0x2000];
wtm[0] = &full_vidram[0x1800];
wtm[1] = &full_vidram[0x3800];
for(int i = 0; i < 32; i++)
{
bpalette[i] = 0x011F;
opalette[i] = 0x011F;
ibpalette[i] = 0xFF4000;
iopalette[i] = 0xFF4000;
}
}
static void draw_bg_line(unsigned line, unsigned bit7val, unsigned window)
{
unsigned ay = io_state.scy + line;
int by = ay & 0xF8, ry = ay & 0x07;
line *= 160;
int cb1 = 0, cb2 = 0, cflags = 0, vbank = 0;
uint32_t *cpal = ibpalette;
uint8_t ax = io_state.scx;
for (int sx = 0; sx < 160; sx++, ax++)
{
if (unlikely(!sx) || unlikely(!(ax & 7)))
{
if (window && (io_state.wx <= ax))
break;
int tile = (by << 2) + (ax >> 3);
if (has_cgb)
cflags = btm[1][tile];
if ((cflags & (1 << 7)) != bit7val)
{
ax = ((ax + 9) & ~7) - 1;
sx = ax - io_state.scx;
if (sx < 0)
sx += 256;
continue;
}
uint8_t *tdat;
if (has_cgb)
{
vbank = !!(cflags & (1 << 3));
cpal = &ibpalette[(cflags & 7) * 4];
}
if (bwtd[0] == &full_vidram[0x0000])
tdat = &bwtd[vbank][btm[0][tile] * 16];
else
tdat = &bwtd[vbank][(int8_t)btm[0][tile] * 16];
if (cflags & (1 << 6))
{
cb1 = tdat[(7 - ry) * 2];
cb2 = tdat[(7 - ry) * 2 + 1];
}
else
{
cb1 = tdat[ry * 2];
cb2 = tdat[ry * 2 + 1];
}
}
int val, mask;
if (cflags & (1 << 5))
mask = 1 << (ax & 7);
else
mask = 1 << (7 - (ax & 7));
val = !!(cb1 & mask) + !!(cb2 & mask) * 2;
vpb[line + sx] = cpal[val] | (val << 24);
}
}
void draw_line(unsigned line)
{
static int frame_drop_counter;
if (!lcd_on || (is_random && frame_drop_counter))
{
if (line == 143)
{
handle_input_events();
frame_drop_counter = (frame_drop_counter + 1) % FRAME_DROP;
}
return;
}
struct
{
uint8_t y, x;
uint8_t num;
uint8_t flags;
} __attribute__((packed)) *soam = (void *)oam;
int window_active = ((io_state.lcdc & (1 << 5)) && (io_state.wx >= 7) && (io_state.wx <= 166) && (io_state.wy <= line));
unsigned line_o = line * 160;
if (!(io_state.lcdc & (1 << 0)))
memset(vpb + line_o, 0, 160 * sizeof(uint32_t));
else
draw_bg_line(line, 0 << 7, window_active);
if (window_active)
{
int wx = io_state.wx - 7, wy = io_state.wy;
int yoff = line - wy;
int by = yoff & 0xF8, ry = yoff & 0x07;
int tile = by * 4;
for (int bx = 0; bx < 160; bx += 8)
{
int flags;
if (has_cgb)
flags = wtm[1][tile];
else
flags = 0;
uint8_t *tdat;
int vbank;
uint32_t *pal;
if (has_cgb)
{
vbank = !!(flags & (1 << 3));
pal = &ibpalette[(flags & 7) * 4];
}
else
{
vbank = 0;
pal = ibpalette;
}
if (bwtd[0] == &full_vidram[0x0000])
tdat = &bwtd[vbank][(unsigned)wtm[0][tile] * 16];
else
tdat = &bwtd[vbank][(int)(int8_t)wtm[0][tile] * 16];
for (int rx = 0; rx < 8; rx++)
{
int val;
switch (flags & (3 << 5))
{
case (0 << 5):
val = !!(tdat[ry * 2] & (1 << (7 - rx)));
val += !!(tdat[ry * 2 + 1] & (1 << (7 - rx))) << 1;
break;
case (1 << 5):
val = !!(tdat[ry * 2] & (1 << rx));
val += !!(tdat[ry * 2 + 1] & (1 << rx)) << 1;
break;
case (2 << 5):
val = !!(tdat[(7 - ry) * 2] & (1 << (7 - rx)));
val += !!(tdat[(7 - ry) * 2 + 1] & (1 << (7 - rx))) << 1;
break;
default:
val = !!(tdat[(7 - ry) * 2] & (1 << rx));
val += !!(tdat[(7 - ry) * 2 + 1] & (1 << rx)) << 1;
}
vpb[line_o + bx + rx + wx] = pal[val];
}
tile++;
}
}
if (io_state.lcdc & (1 << 1))
{
int obj_height = (io_state.lcdc & (1 << 2)) ? 16 : 8;
int count = 0;
for (int sprite = 40; sprite >= 0; sprite--)
{
uint8_t *tdat;
int bx = soam[sprite].x, by = soam[sprite].y, flags = soam[sprite].flags;
uint32_t *pal;
if (has_cgb)
pal = &iopalette[(flags & 7) * 4];
else
pal = &iopalette[(flags & (1 << 4)) >> 2];
if (!has_cgb)
flags &= 0xF0;
bx -= 8;
by -= 16;
if ((by > (int)line) || (by + obj_height <= (int)line))
continue;
if (count++ >= 10)
break;
if ((bx <= -8) || (bx >= 160))
continue;
int ry = line - by;
if (flags & (1 << 6))
ry = (obj_height - 1) - ry;
if (obj_height == 8)
{
if (!(flags & (1 << 3)))
tdat = &full_vidram[0x0000 + soam[sprite].num * 16];
else
tdat = &full_vidram[0x2000 + soam[sprite].num * 16];
}
else
{
if (!(flags & (1 << 3)))
tdat = &full_vidram[0x0000 + (soam[sprite].num & 0xFE) * 16];
else
tdat = &full_vidram[0x2000 + (soam[sprite].num & 0xFE) * 16];
}
for (int rx = 0; rx < 8; rx++)
{
int val, bmask;
if (flags & (1 << 5))
bmask = 1 << rx;
else
bmask = 1 << (7 - rx);
val = !!(tdat[ry * 2] & bmask);
val += !!(tdat[ry * 2 + 1] & bmask) << 1;
if (val && !(flags & (1 << 7)))
vpb[line_o + bx + rx] = pal[val];
else if (val)
{
if (!(vpb[line_o + bx + rx] & 0xFF000000))
vpb[line_o + bx + rx] = pal[val];
}
}
}
}
if ((io_state.lcdc & (1 << 0)) && has_cgb)
draw_bg_line(line, 1 << 7, window_active);
if (line == 143)
{
handle_input_events();
frame_drop_counter = (frame_drop_counter + 1) % FRAME_DROP;
SDL_BlitSurface(vscreen, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
}