-
Notifications
You must be signed in to change notification settings - Fork 0
/
tms.c
517 lines (429 loc) · 13.1 KB
/
tms.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
/*
tms.c --
TMS9918 and legacy video mode support.
*/
#include "shared.h"
int text_counter; /* Text offset counter */
uint8 tms_lookup[16][256][2]; /* Expand BD, PG data into 8-bit pixels (G1,G2) */
uint8 mc_lookup[16][256][8]; /* Expand BD, PG data into 8-bit pixels (MC) */
uint8 txt_lookup[256][2]; /* Expand BD, PG data into 8-bit pixels (TX) */
uint8 bp_expand[256][8]; /* Expand PG data into 8-bit pixels */
uint8 tms_obj_lut[16*256]; /* Look up priority between SG and display pixels */
static const uint8 diff_mask[] = {0x07, 0x07, 0x0F, 0x0F};
static const uint8 name_mask[] = {0xFF, 0xFF, 0xFC, 0xFC};
static const uint8 diff_shift[] = {0, 1, 0, 1};
static const uint8 size_tab[] = {8, 16, 16, 32};
/* Internally latched sprite data in the VDP */
typedef struct {
int xpos;
uint8 attr;
uint8 sg[2];
} tms_sprite;
tms_sprite sprites[4];
int sprites_found;
void parse_line(int line)
{
int yp, i;
int mode = vdp.reg[1] & 3;
int size = size_tab[mode];
int diff, name;
uint8 *sa, *sg;
tms_sprite *p;
/* Reset # of sprites found */
sprites_found = 0;
/* Parse sprites */
for(i = 0; i < 32; i++)
{
/* Point to current sprite in SA and our current sprite record */
p = &sprites[sprites_found];
sa = &vdp.vram[vdp.sa + (i << 2)];
/* Fetch Y coordinate */
yp = sa[0];
/* Check for end marker */
if(yp == 0xD0)
goto parse_end;
/* Wrap Y position */
if(yp > 0xE0)
yp -= 256;
/* Check if sprite falls on following line */
if(line >= yp && line < (yp + size))
{
/* Sprite overflow on this line */
if(sprites_found == 4)
{
/* Set 5S and abort parsing */
vdp.status |= 0x40;
goto parse_end;
}
/* Fetch X position */
p->xpos = sa[1];
/* Fetch name */
name = sa[2] & name_mask[mode];
/* Load attribute into attribute storage */
p->attr = sa[3];
/* Apply early clock bit */
if(p->attr & 0x80)
p->xpos -= 32;
/* Calculate offset in pattern */
diff = ((line - yp) >> diff_shift[mode]) & diff_mask[mode];
/* Insert additional name bit for 16-pixel tall sprites */
if(diff & 8)
name |= 1;
/* Fetch SG data */
sg = &vdp.vram[vdp.sg | (name << 3) | (diff & 7)];
p->sg[0] = sg[0x00];
p->sg[1] = sg[0x10];
/* Bump found sprite count */
++sprites_found;
}
}
parse_end:
/* Insert number of last sprite entry processed */
vdp.status = (vdp.status & 0xE0) | (i & 0x1F);
}
void render_obj_tms(int line)
{
int i, x = 0;
int size, start, end, mode;
uint8 *lb, *lut, *ex[2];
tms_sprite *p;
mode = vdp.reg[1] & 3;
size = size_tab[mode];
/* Render sprites */
for(i = 0; i < sprites_found; i++)
{
p = &sprites[i];
lb = &linebuf[p->xpos];
lut = &tms_obj_lut[(p->attr & 0x0F) << 8];
/* Point to expanded PG data */
ex[0] = bp_expand[p->sg[0]];
ex[1] = bp_expand[p->sg[1]];
/* Clip left edge */
if(p->xpos < 0)
start = 0 - p->xpos;
else
start = 0;
/* Clip right edge */
if(p->xpos > 256 - size)
end = 256 - p->xpos;
else
end = size;
/* Render sprite line */
switch(mode)
{
case 0: /* 8x8 */
for(x = start; x < end; x++) {
if(ex[0][x])
lb[x] = lut[lb[x]];
}
break;
case 1: /* 8x8 zoomed */
for(x = start; x < end; x++) {
if(ex[0][x >> 1])
lb[x] = lut[lb[x]];
}
break;
case 2: /* 16x16 */
for(x = start; x < end; x++) {
if(ex[(x >> 3) & 1][x & 7])
lb[x] = lut[lb[x]];
}
break;
case 3: /* 16x16 zoomed */
for(x = start; x < end; x++) {
if(ex[(x >> 4) & 1][(x >> 1) & 7])
lb[x] = lut[lb[x]];
}
break;
}
}
}
/****
1.) NOTE: xpos can be negative, but the 'start' value that is added
to xpos will ensure it is positive.
For an EC sprite that is offscreen, 'start' will be larger
than 'end' and the for-loop used for rendering will abort
on the first pass.
***/
#define RENDER_TX_LINE \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ];
#define RENDER_TX_BORDER \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0]; \
*lb++ = 0x10 | clut[0];
#define RENDER_GR_LINE \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ]; \
*lb++ = 0x10 | clut[ *bpex++ ];
#define RENDER_MC_LINE \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++; \
*lb++ = 0x10 | *mcex++;
void make_tms_tables(void)
{
int i, j, x;
int bd, pg, ct;
int sx, bx;
for(sx = 0; sx < 16; sx++)
{
for(bx = 0; bx < 256; bx++)
{
// uint8 bd = (bx & 0x0F);
uint8 bs = (bx & 0x40);
// uint8 bt = (bd == 0) ? 1 : 0;
uint8 sd = (sx & 0x0F);
// uint8 st = (sd == 0) ? 1 : 0;
// opaque sprite pixel, choose 2nd pal and set sprite marker
if(sd && !bs)
{
tms_obj_lut[(sx<<8)|(bx)] = sd | 0x10 | 0x40;
}
else
if(sd && bs)
{
// writing over a sprite
tms_obj_lut[(sx<<8)|(bx)] = bx;
}
else
{
tms_obj_lut[(sx<<8)|(bx)] = bx;
}
}
}
/* Text lookup table */
for(bd = 0; bd < 256; bd++)
{
uint8 bg = (bd >> 0) & 0x0F;
uint8 fg = (bd >> 4) & 0x0F;
/* If foreground is transparent, use background color */
if(fg == 0) fg = bg;
txt_lookup[bd][0] = bg;
txt_lookup[bd][1] = fg;
}
/* Multicolor lookup table */
for(bd = 0; bd < 16; bd++)
{
for(pg = 0; pg < 256; pg++)
{
int l = (pg >> 4) & 0x0F;
int r = (pg >> 0) & 0x0F;
/* If foreground is transparent, use background color */
if(l == 0) l = bd;
if(r == 0) r = bd;
/* Unpack 2 nibbles across eight pixels */
for(x = 0; x < 8; x++)
{
int c = (x & 4) ? r : l;
mc_lookup[bd][pg][x] = c;
}
}
}
/* Make bitmap data expansion table */
memset(bp_expand, 0, sizeof(bp_expand));
for(i = 0; i < 256; i++)
{
for(j = 0; j < 8; j++)
{
int c = (i >> (j ^ 7)) & 1;
bp_expand[i][j] = c;
}
}
/* Graphics I/II lookup table */
for(bd = 0; bd < 0x10; bd++)
{
for(ct = 0; ct < 0x100; ct++)
{
int backdrop = (bd & 0x0F);
int background = (ct >> 0) & 0x0F;
int foreground = (ct >> 4) & 0x0F;
/* If foreground is transparent, use background color */
if(background == 0) background = backdrop;
if(foreground == 0) foreground = backdrop;
tms_lookup[bd][ct][0] = background;
tms_lookup[bd][ct][1] = foreground;
}
}
}
void render_bg_tms(int line)
{
switch(vdp.mode & 7)
{
case 0: /* Graphics I */
render_bg_m0(line);
break;
case 1: /* Text */
render_bg_m1(line);
break;
case 2: /* Graphics II */
render_bg_m2(line);
break;
case 3: /* Text (Extended PG) */
render_bg_m1x(line);
break;
case 4: /* Multicolor */
render_bg_m3(line);
break;
case 5: /* Invalid (1+3) */
render_bg_inv(line);
break;
case 6: /* Multicolor (Extended PG) */
render_bg_m3x(line);
break;
case 7: /* Invalid (1+2+3) */
render_bg_inv(line);
break;
}
}
/* Graphics I */
void render_bg_m0(int line)
{
int v_row = (line & 7);
int column;
int name;
uint8 *clut;
uint8 *bpex;
uint8 *lb = &linebuf[0];
uint8 *pn = &vdp.vram[vdp.pn + ((line >> 3) << 5)];
uint8 *ct = &vdp.vram[vdp.ct];
uint8 *pg = &vdp.vram[vdp.pg | (v_row)];
for(column = 0; column < 32; column++)
{
name = pn[column];
clut = &tms_lookup[vdp.bd][ct[name >> 3]][0];
bpex = &bp_expand[pg[name << 3]][0];
RENDER_GR_LINE
}
}
/* Text */
void render_bg_m1(int line)
{
int v_row = (line & 7);
int column;
uint8 *clut;
uint8 *bpex;
uint8 *lb = &linebuf[0];
// uint8 *pn = &vdp.vram[vdp.pn + ((line >> 3) * 40)];
uint8 *pn = &vdp.vram[vdp.pn + text_counter];
uint8 *pg = &vdp.vram[vdp.pg | (v_row)];
uint8 bk = vdp.reg[7];
clut = &txt_lookup[bk][0];
for(column = 0; column < 40; column++)
{
bpex = &bp_expand[pg[pn[column] << 3]][0];
RENDER_TX_LINE
}
/* V3 */
if((vdp.line & 7) == 7)
text_counter += 40;
RENDER_TX_BORDER
}
/* Text + extended PG */
void render_bg_m1x(int line)
{
int v_row = (line & 7);
int column;
uint8 *clut;
uint8 *bpex;
uint8 *lb = &linebuf[0];
uint8 *pn = &vdp.vram[vdp.pn + ((line >> 3) * 40)];
uint8 *pg = &vdp.vram[vdp.pg + (v_row) + ((line & 0xC0) << 5)];
uint8 bk = vdp.reg[7];
clut = &tms_lookup[0][bk][0];
for(column = 0; column < 40; column++)
{
bpex = &bp_expand[pg[pn[column] << 3]][0];
RENDER_TX_LINE
}
RENDER_TX_BORDER
}
/* Invalid (2+3/1+2+3) */
void render_bg_inv(int line)
{
int column;
uint8 *clut;
uint8 *bpex;
uint8 *lb = &linebuf[0];
uint8 bk = vdp.reg[7];
clut = &txt_lookup[bk][0];
for(column = 0; column < 40; column++)
{
bpex = &bp_expand[0xF0][0];
RENDER_TX_LINE
}
}
/* Multicolor */
void render_bg_m3(int line)
{
int column;
uint8 *mcex;
uint8 *lb = &linebuf[0];
uint8 *pn = &vdp.vram[vdp.pn + ((line >> 3) << 5)];
uint8 *pg = &vdp.vram[vdp.pg + ((line >> 2) & 7)];
for(column = 0; column < 32; column++)
{
mcex = &mc_lookup[vdp.bd][pg[pn[column]<<3]][0];
RENDER_MC_LINE
}
}
/* Multicolor + extended PG */
void render_bg_m3x(int line)
{
int column;
uint8 *mcex;
uint8 *lb = &linebuf[0];
uint8 *pn = &vdp.vram[vdp.pn + ((line >> 3) << 5)];
uint8 *pg = &vdp.vram[vdp.pg + ((line >> 2) & 7) + ((line & 0xC0) << 5)];
for(column = 0; column < 32; column++)
{
mcex = &mc_lookup[vdp.bd][pg[pn[column]<<3]][0];
RENDER_MC_LINE
}
}
/* Graphics II */
void render_bg_m2(int line)
{
int v_row = (line & 7);
int column;
int name;
uint8 *clut;
uint8 *bpex;
uint8 *lb = &linebuf[0];
uint8 *pn = &vdp.vram[vdp.pn | ((line & 0xF8) << 2)];
uint8 *ct = &vdp.vram[(vdp.ct & 0x2000) | (v_row) | ((line & 0xC0) << 5)];
uint8 *pg = &vdp.vram[(vdp.pg & 0x2000) | (v_row) | ((line & 0xC0) << 5)];
for(column = 0; column < 32; column++)
{
name = pn[column] << 3;
clut = &tms_lookup[vdp.bd][ct[name]][0];
bpex = &bp_expand[pg[name]][0];
RENDER_GR_LINE
}
}