-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.c
614 lines (479 loc) · 15.3 KB
/
render.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include "shared.h"
/* Background drawing function */
void (*render_bg)(int line);
/* Pointer to output buffer */
uint8 *linebuf;
/* Internal buffer for drawing non 8-bit displays */
uint8 internal_buffer[0x100];
/* Precalculated pixel table */
uint32 pixel[PALETTE_SIZE];
/* Pattern cache */
uint8 cache[0x20000];
/* Dirty pattern info */
uint8 vram_dirty[0x200];
uint8 is_vram_dirty;
/* Pixel look-up table */
uint8 lut[0x10000];
/* Attribute expansion table */
uint32 atex[4] =
{
0x00000000,
0x10101010,
0x20202020,
0x30303030,
};
/* Display sizes */
int vp_vstart;
int vp_vend;
int vp_hstart;
int vp_hend;
/* Macros to access memory 32-bits at a time (from MAME's drawgfx.c) */
#ifdef ALIGN_DWORD
static __inline__ uint32 read_dword(void *address)
{
if ((uint32)address & 3)
{
#ifdef LSB_FIRST /* little endian version */
return ( *((uint8 *)address) +
(*((uint8 *)address+1) << 8) +
(*((uint8 *)address+2) << 16) +
(*((uint8 *)address+3) << 24) );
#else /* big endian version */
return ( *((uint8 *)address+3) +
(*((uint8 *)address+2) << 8) +
(*((uint8 *)address+1) << 16) +
(*((uint8 *)address) << 24) );
#endif
}
else
return *(uint32 *)address;
}
static __inline__ void write_dword(void *address, uint32 data)
{
if ((uint32)address & 3)
{
#ifdef LSB_FIRST
*((uint8 *)address) = data;
*((uint8 *)address+1) = (data >> 8);
*((uint8 *)address+2) = (data >> 16);
*((uint8 *)address+3) = (data >> 24);
#else
*((uint8 *)address+3) = data;
*((uint8 *)address+2) = (data >> 8);
*((uint8 *)address+1) = (data >> 16);
*((uint8 *)address) = (data >> 24);
#endif
return;
}
else
*(uint32 *)address = data;
}
#else
#define read_dword(address) *(uint32 *)address
#define write_dword(address,data) *(uint32 *)address=data
#endif
/****************************************************************************/
/* Initialize the rendering data */
void render_init(void)
{
int bx, sx, b, s, bp, bf, sf, c;
/* Generate 64k of data for the look up table */
for(bx = 0; bx < 0x100; bx += 1)
{
for(sx = 0; sx < 0x100; sx += 1)
{
/* Background pixel */
b = (bx & 0x0F);
/* Background priority */
bp = (bx & 0x20) ? 1 : 0;
/* Full background pixel + priority + sprite marker */
bf = (bx & 0x7F);
/* Sprite pixel */
s = (sx & 0x0F);
/* Full sprite pixel, w/ palette and marker bits added */
sf = (sx & 0x0F) | 0x10 | 0x40;
/* Overwriting a sprite pixel ? */
if(bx & 0x40)
{
/* Return the input */
c = bf;
}
else
{
/* Work out priority and transparency for both pixels */
c = bp ? b ? bf : s ? sf : bf : s ? sf : bf;
}
/* Store result */
lut[(bx << 8) | (sx)] = c;
}
}
render_reset();
}
/* Reset the rendering data */
void render_reset(void)
{
int i;
/* Clear display bitmap */
memset(bitmap.data, 0, bitmap.pitch * bitmap.height);
/* Clear palette */
for(i = 0; i < PALETTE_SIZE; i += 1)
{
palette_sync(i);
}
/* Invalidate pattern cache */
is_vram_dirty = 1;
memset(vram_dirty, 1, 0x200);
memset(cache, 0, sizeof(cache));
/* Set up viewport size */
if(IS_GG)
{
vp_vstart = 24;
vp_vend = 168;
vp_hstart = 6;
vp_hend = 26;
}
else
{
vp_vstart = 0;
vp_vend = 192;
vp_hstart = 0;
vp_hend = 32;
}
/* Pick render routine */
render_bg = IS_GG ? render_bg_gg : render_bg_sms;
}
/* Draw a line of the display */
void render_line(int line)
{
/* Ensure we're within the viewport range */
if((line < vp_vstart) || (line >= vp_vend)) return;
/* Point to current line in output buffer */
linebuf = (bitmap.depth == 8) ? &bitmap.data[(line * bitmap.pitch)] : &internal_buffer[0];
/* Update pattern cache */
update_cache();
/* Blank line */
if( (!(vdp.reg[1] & 0x40)) || (((vdp.reg[2] & 1) == 0) && (IS_SMS)))
{
memset(linebuf + (vp_hstart << 3), BACKDROP_COLOR, BMP_WIDTH);
}
else
{
/* Draw background */
render_bg(line);
/* Draw sprites */
render_obj(line);
/* Blank leftmost column of display */
if(vdp.reg[0] & 0x20)
{
memset(linebuf, BACKDROP_COLOR, 8);
}
}
if(bitmap.depth != 8) remap_8_to_16(line);
}
/* Draw the Master System background */
void render_bg_sms(int line)
{
int locked = 0;
int v_line = (line + vdp.reg[9]) % 224;
int v_row = (v_line & 7) << 3;
int hscroll = ((vdp.reg[0] & 0x40) && (line < 0x10)) ? 0 : (0x100 - vdp.reg[8]);
int column = vp_hstart;
uint16 attr;
uint16 *nt = (uint16 *)&vdp.vram[vdp.ntab + ((v_line >> 3) << 6)];
int nt_scroll = (hscroll >> 3);
int shift = (hscroll & 7);
//int shift = 0;
uint32 atex_mask;
uint32 *cache_ptr;
uint32 *linebuf_ptr = (uint32 *)&linebuf[0];
/* Draw first column (clipped) */
if(shift)
{
int x, c, a;
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
a = (attr >> 7) & 0x30;
for(x = shift; x < 8; x += 1)
{
c = cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
linebuf[(0 - shift) + (x) ] = ((c) | (a));
}
column += 1;
}
/* Draw a line of the background */
for(; column < vp_hend; column += 1)
{
// Stop vertical scrolling for leftmost eight columns
/*if((vdp.reg[0] & 0x80) && (!locked) && (column >= 24))
{
locked = 1;
v_row = (line & 7) << 3;
nt = (uint16 *)&vdp.vram[((vdp.reg[2] << 10) & 0x3800) + ((line >> 3) << 6)];
}
// Get name table attribute word
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
// Expand priority and palette bits
atex_mask = atex[(attr >> 11) & 3];
// Point to a line of pattern data in cache
cache_ptr = (uint32 *)&cache[((attr & 0x7FF) << 6) | (v_row)];
// Copy the left half, adding the attribute bits in
write_dword( &linebuf_ptr[(column << 1)] , read_dword( &cache_ptr[0] ) | (atex_mask));
// Copy the right half, adding the attribute bits in
write_dword( &linebuf_ptr[(column << 1) | (1)], read_dword( &cache_ptr[1] ) | (atex_mask));*/
// FIXME: optimize this
int x, c, a;
char *p = &linebuf[(0 - shift)+(column << 3)];
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
a = (attr >> 7) & 0x30;
for(x = 0; x < 8; x += 1)
{
c = cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
p[x] = ((c) | (a));
}
}
/* Draw last column (clipped) */
if(shift)
{
int x, c, a;
char *p = &linebuf[(0 - shift)+(column << 3)];
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
a = (attr >> 7) & 0x30;
for(x = 0; x < shift; x += 1)
{
c = cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
p[x] = ((c) | (a));
}
}
}
/* Draw the Game Gear background */
void render_bg_gg(int line)
{
int v_line = (line + vdp.reg[9]) % 224;
int v_row = (v_line & 7) << 3;
int hscroll = (0x100 - vdp.reg[8]);
int column;
uint16 attr;
uint16 *nt = (uint16 *)&vdp.vram[vdp.ntab + ((v_line >> 3) << 6)];
int nt_scroll = (hscroll >> 3);
int shift = (hscroll & 7);
//int shift = 0;
uint32 atex_mask;
uint32 *cache_ptr;
//uint32 *linebuf_ptr = (uint32 *)&linebuf[0];
/* Draw a line of the background */
for(column = vp_hstart; column <= vp_hend; column += 1)
{
// Get name table attribute word
/*attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
// Expand priority and palette bits
atex_mask = atex[(attr >> 11) & 3];
// Point to a line of pattern data in cache
cache_ptr = (uint32 *)&cache[((attr & 0x7FF) << 6) | (v_row)];
// Copy the left half, adding the attribute bits in
write_dword( &linebuf_ptr[(column << 1)] , read_dword( &cache_ptr[0] ) | (atex_mask));
// Copy the right half, adding the attribute bits in
write_dword( &linebuf_ptr[(column << 1) | (1)], read_dword( &cache_ptr[1] ) | (atex_mask));*/
// FIXME: optimize this
int x, c, a;
char *p = &linebuf[(0 - shift)+(column << 3)];
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
a = (attr >> 7) & 0x30;
for(x = 0; x < 8; x += 1)
{
c = cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
p[x] = ((c) | (a));
}
}
}
/* Draw sprites */
void render_obj(int line)
{
int i;
/* Sprite count for current line (8 max.) */
int count = 0;
/* Sprite dimensions */
int width = 8;
int height = (vdp.reg[1] & 0x02) ? 16 : 8;
/* Pointer to sprite attribute table */
uint8 *st = (uint8 *)&vdp.vram[vdp.satb];
/* Adjust dimensions for double size sprites */
if(vdp.reg[1] & 0x01)
{
width *= 2;
height *= 2;
}
/* Draw sprites in front-to-back order */
for(i = 0; i < 64; i += 1)
{
/* Sprite Y position */
int yp = st[i];
/* End of sprite list marker? */
if(yp == 208) return;
/* Actual Y position is +1 */
yp += 1;
/* Wrap Y coordinate for sprites > 240 */
if(yp > 240) yp -= 256;
/* Check if sprite falls on current line */
if((line >= yp) && (line < (yp + height)))
{
uint8 *linebuf_ptr;
/* Width of sprite */
int start = 0;
int end = width;
/* Sprite X position */
int xp = st[0x80 + (i << 1)];
/* Pattern name */
int n = st[0x81 + (i << 1)];
/* Bump sprite count */
count += 1;
/* Too many sprites on this line ? */
if((vdp.limit) && (count == 9)) return;
/* X position shift */
if(vdp.reg[0] & 0x08) xp -= 8;
/* Add MSB of pattern name */
if(vdp.reg[6] & 0x04) n |= 0x0100;
/* Mask LSB for 8x16 sprites */
if(vdp.reg[1] & 0x02) n &= 0x01FE;
/* Point to offset in line buffer */
linebuf_ptr = (uint8 *)&linebuf[xp];
/* Clip sprites on left edge */
if(xp < 0)
{
start = (0 - xp);
}
/* Clip sprites on right edge */
if((xp + width) > 256)
{
end = (256 - xp);
}
/* Draw double size sprite */
if(vdp.reg[1] & 0x01)
{
int x;
uint8 *cache_ptr = (uint8 *)&cache[(n << 6) | (((line - yp) >> 1) << 3)];
/* Draw sprite line */
for(x = start; x < end; x += 1)
{
/* Source pixel from cache */
uint8 sp = cache_ptr[(x >> 1)];
/* Only draw opaque sprite pixels */
if(sp)
{
/* Background pixel from line buffer */
uint8 bg = linebuf_ptr[x];
/* Look up result */
linebuf_ptr[x] = lut[(bg << 8) | (sp)];
/* Set sprite collision flag */
if(bg & 0x40) vdp.status |= 0x20;
}
}
}
else /* Regular size sprite (8x8 / 8x16) */
{
int x;
uint8 *cache_ptr = (uint8 *)&cache[(n << 6) | ((line - yp) << 3)];
/* Draw sprite line */
for(x = start; x < end; x += 1)
{
/* Source pixel from cache */
uint8 sp = cache_ptr[x];
/* Only draw opaque sprite pixels */
if(sp)
{
/* Background pixel from line buffer */
uint8 bg = linebuf_ptr[x];
/* Look up result */
linebuf_ptr[x] = lut[(bg << 8) | (sp)];
/* Set sprite collision flag */
if(bg & 0x40) vdp.status |= 0x20;
}
}
}
}
}
}
/* Update pattern cache with modified tiles */
void update_cache(void)
{
int i, x, y, c;
int b0, b1, b2, b3;
int i0, i1, i2, i3;
if(!is_vram_dirty) return;
is_vram_dirty = 0;
for(i = 0; i < 0x200; i += 1)
{
if(vram_dirty[i])
{
vram_dirty[i] = 0;
for(y = 0; y < 8; y += 1)
{
b0 = vdp.vram[(i << 5) | (y << 2) | (0)];
b1 = vdp.vram[(i << 5) | (y << 2) | (1)];
b2 = vdp.vram[(i << 5) | (y << 2) | (2)];
b3 = vdp.vram[(i << 5) | (y << 2) | (3)];
for(x = 0; x < 8; x += 1)
{
i0 = (b0 >> (x ^ 7)) & 1;
i1 = (b1 >> (x ^ 7)) & 1;
i2 = (b2 >> (x ^ 7)) & 1;
i3 = (b3 >> (x ^ 7)) & 1;
c = (i3 << 3 | i2 << 2 | i1 << 1 | i0);
cache[0x00000 | (i << 6) | ((y ) << 3) | (x)] = c;
cache[0x08000 | (i << 6) | ((y ) << 3) | (x ^ 7)] = c;
cache[0x10000 | (i << 6) | ((y ^ 7) << 3) | (x)] = c;
cache[0x18000 | (i << 6) | ((y ^ 7) << 3) | (x ^ 7)] = c;
}
}
}
}
}
/* Update a palette entry */
void palette_sync(int index)
{
int r, g, b;
if(IS_GG)
{
r = ((vdp.cram[(index << 1) | 0] >> 1) & 7) << 5;
g = ((vdp.cram[(index << 1) | 0] >> 5) & 7) << 5;
b = ((vdp.cram[(index << 1) | 1] >> 1) & 7) << 5;
}
else
{
r = ((vdp.cram[index] >> 0) & 3) << 6;
g = ((vdp.cram[index] >> 2) & 3) << 6;
b = ((vdp.cram[index] >> 4) & 3) << 6;
}
bitmap.pal.color[index][0] = r;
bitmap.pal.color[index][1] = g;
bitmap.pal.color[index][2] = b;
pixel[index] = MAKE_PIXEL(r, g, b);
bitmap.pal.dirty[index] = bitmap.pal.update = 1;
}
void remap_8_to_16(int line)
{
int i;
int length = BMP_WIDTH;
int ofs = BMP_X_OFFSET;
uint32 *p = (uint32 *)&bitmap.data[(line * bitmap.pitch) + (ofs << 1)];
for(i = 0; i < length; i += 1)
{
p[i] = pixel[(internal_buffer[(ofs + i)] & PIXEL_MASK)];
}
}