-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
602 lines (485 loc) · 15.1 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
/*
render.c --
Display rendering.
*/
#include "shared.h"
uint8 sms_cram_expand_table[4];
uint8 gg_cram_expand_table[16];
/* Background drawing function */
void (*render_bg)(int line) = NULL;
void (*render_obj)(int line) = NULL;
/* Pointer to output buffer */
uint8 *linebuf;
/* Internal buffer for drawing non 8-bit displays */
uint8 internal_buffer[0x100];
/* Precalculated pixel table */
uint16 pixel[PALETTE_SIZE];
/* Dirty pattern info */
uint8 bg_name_dirty[0x200]; /* 1= This pattern is dirty */
uint16 bg_name_list[0x200]; /* List of modified pattern indices */
uint16 bg_list_index; /* # of modified patterns in list */
uint8 bg_pattern_cache[0x20000];/* Cached and flipped patterns */
/* Pixel look-up table */
uint8 lut[0x10000];
/* Attribute expansion table */
static const uint32 atex[4] =
{
0x00000000,
0x10101010,
0x20202020,
0x30303030,
};
/* Bitplane to packed pixel LUT */
uint32 bp_lut[0x10000];
/* 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
/****************************************************************************/
void render_shutdown(void)
{
}
/* Initialize the rendering data */
void render_init(void)
{
int i, j;
int bx, sx, b, s, bp, bf, sf, c;
make_tms_tables();
/* Generate 64k of data for the look up table */
for(bx = 0; bx < 0x100; bx++)
{
for(sx = 0; sx < 0x100; sx++)
{
/* 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 */
if(bp)
{
/* Underlying pixel is high priority */
if(b)
{
c = bf | 0x40;
}
else
{
if(s)
{
c = sf;
}
else
{
c = bf;
}
}
}
else
{
/* Underlying pixel is low priority */
if(s)
{
c = sf;
}
else
{
c = bf;
}
}
}
/* Store result */
lut[(bx << 8) | (sx)] = c;
}
}
/* Make bitplane to pixel lookup table */
for(i = 0; i < 0x100; i++)
for(j = 0; j < 0x100; j++)
{
int x;
uint32 out = 0;
for(x = 0; x < 8; x++)
{
out |= (j & (0x80 >> x)) ? (uint32)(8 << (x << 2)) : 0;
out |= (i & (0x80 >> x)) ? (uint32)(4 << (x << 2)) : 0;
}
#if LSB_FIRST
bp_lut[(j << 8) | (i)] = out;
#else
bp_lut[(i << 8) | (j)] = out;
#endif
}
for(i = 0; i < 4; i++)
{
uint8 c = i << 6 | i << 4 | i << 2 | i;
sms_cram_expand_table[i] = c;
}
for(i = 0; i < 16; i++)
{
uint8 c = i << 4 | i;
gg_cram_expand_table[i] = 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++)
{
palette_sync(i, 1);
}
/* Invalidate pattern cache */
memset(bg_name_dirty, 0, sizeof(bg_name_dirty));
memset(bg_name_list, 0, sizeof(bg_name_list));
bg_list_index = 0;
memset(bg_pattern_cache, 0, sizeof(bg_pattern_cache));
/* Pick render routine */
render_bg = render_bg_sms;
render_obj = render_obj_sms;
}
/* Draw a line of the display */
void render_line(int line)
{
/* Ensure we're within the viewport range */
if(line >= vdp.height)
return;
/* Point to current line in output buffer */
linebuf = (bitmap.depth == 8) ? &bitmap.data[(line * bitmap.pitch)] : &internal_buffer[0];
/* Update pattern cache */
update_bg_pattern_cache();
/* Blank line (full width) */
if(!(vdp.reg[1] & 0x40))
{
memset(linebuf, BACKDROP_COLOR, bitmap.width);
}
else
{
/* Draw background */
if(render_bg != NULL)
render_bg(line);
/* Draw sprites */
if(render_obj != NULL)
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 yscroll_mask = (vdp.extended) ? 256 : 224;
int v_line = (line + vdp.reg[9]) % yscroll_mask;
int v_row = (v_line & 7) << 3;
int hscroll = ((vdp.reg[0] & 0x40) && (line < 0x10)) ? 0 : (0x100 - vdp.reg[8]);
int column = 0;
uint16 attr;
uint16 *nt = (uint16 *)&vdp.vram[vdp.ntab + ((v_line >> 3) << 6)];
int nt_scroll = (hscroll >> 3);
int shift = (hscroll & 7);
uint32 atex_mask;
uint32 *cache_ptr;
uint32 *linebuf_ptr = (uint32 *)&linebuf[0 - shift];
/* Draw first column (clipped) */
if(shift)
{
int x;
for(x = shift; x < 8; x++)
linebuf[(0 - shift) + (x)] = 0;
column++;
}
/* Draw a line of the background */
for(; column < 32; column++)
{
/* 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 *)&bg_pattern_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));
}
/* Draw last column (clipped) */
if(shift)
{
int x, c, a;
uint8 *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++)
{
c = bg_pattern_cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
p[x] = ((c) | (a));
}
}
}
/* Draw sprites */
void render_obj_sms(int line)
{
int i;
uint8 collision_buffer = 0;
/* 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++)
{
/* Sprite Y position */
int yp = st[i];
/* Found end of sprite list marker for non-extended modes? */
if(vdp.extended == 0 && yp == 208)
goto end;
/* Actual Y position is +1 */
yp++;
/* 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++;
/* Too many sprites on this line ? */
if(count == 9)
{
vdp.status |= 0x40;
goto end;
}
/* 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 *)&bg_pattern_cache[(n << 6) | (((line - yp) >> 1) << 3)];
/* Draw sprite line */
for(x = start; x < end; x++)
{
/* 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)];
/* Update collision buffer */
collision_buffer |= bg;
}
}
}
else /* Regular size sprite (8x8 / 8x16) */
{
int x;
uint8 *cache_ptr = (uint8 *)&bg_pattern_cache[(n << 6) | ((line - yp) << 3)];
/* Draw sprite line */
for(x = start; x < end; x++)
{
/* 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)];
/* Update collision buffer */
collision_buffer |= bg;
}
}
}
}
}
end:
/* Set sprite collision flag */
if(collision_buffer & 0x40)
vdp.status |= 0x20;
}
void update_bg_pattern_cache(void)
{
int i;
uint8 x, y;
uint16 name;
if(!bg_list_index) return;
for(i = 0; i < bg_list_index; i++)
{
name = bg_name_list[i];
bg_name_list[i] = 0;
for(y = 0; y < 8; y++)
{
if(bg_name_dirty[name] & (1 << y))
{
uint8 *dst = &bg_pattern_cache[name << 6];
uint16 bp01 = *(uint16 *)&vdp.vram[(name << 5) | (y << 2) | (0)];
uint16 bp23 = *(uint16 *)&vdp.vram[(name << 5) | (y << 2) | (2)];
uint32 temp = (bp_lut[bp01] >> 2) | (bp_lut[bp23]);
for(x = 0; x < 8; x++)
{
uint8 c = (temp >> (x << 2)) & 0x0F;
dst[0x00000 | (y << 3) | (x)] = (c);
dst[0x08000 | (y << 3) | (x ^ 7)] = (c);
dst[0x10000 | ((y ^ 7) << 3) | (x)] = (c);
dst[0x18000 | ((y ^ 7) << 3) | (x ^ 7)] = (c);
}
}
}
bg_name_dirty[name] = 0;
}
bg_list_index = 0;
}
/* Update a palette entry */
void palette_sync(int index, int force)
{
int r, g, b;
// unless we are forcing an update,
// if not in mode 4, exit
if(IS_SMS && !force && ((vdp.reg[0] & 4) == 0) )
return;
if(IS_GG)
{
/* ----BBBBGGGGRRRR */
r = (vdp.cram[(index << 1) | (0)] >> 0) & 0x0F;
g = (vdp.cram[(index << 1) | (0)] >> 4) & 0x0F;
b = (vdp.cram[(index << 1) | (1)] >> 0) & 0x0F;
r = gg_cram_expand_table[r];
g = gg_cram_expand_table[g];
b = gg_cram_expand_table[b];
}
else
{
/* --BBGGRR */
r = (vdp.cram[index] >> 0) & 3;
g = (vdp.cram[index] >> 2) & 3;
b = (vdp.cram[index] >> 4) & 3;
r = sms_cram_expand_table[r];
g = sms_cram_expand_table[g];
b = sms_cram_expand_table[b];
}
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;
uint16 *p = (uint16 *)&bitmap.data[(line * bitmap.pitch)];
for(i = bitmap.viewport.x; i < bitmap.viewport.w + bitmap.viewport.x; i++)
{
p[i] = pixel[ internal_buffer[i] & PIXEL_MASK ];
}
}