-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtms9918a.hpp
712 lines (669 loc) · 26.2 KB
/
tms9918a.hpp
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/**
* SUZUKI PLAN - TinyMSX - TMS9918A Emulator
* -----------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2020 Yoji Suzuki.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* -----------------------------------------------------------------------------
*/
#ifndef INCLUDE_TMS9918A_HPP
#define INCLUDE_TMS9918A_HPP
#include <string.h>
#define TMS9918A_SCREEN_WIDTH 284
#define TMS9918A_SCREEN_HEIGHT 240
/**
* Note about the Screen Resolution: 284 x 240
* =================================================
* Pixel (horizontal) display timings:
* Left blanking: 2Hz (skip)
* Color burst: 14Hz (skip)
* Left blanking: 8Hz (skip)
* Left border: 13Hz (RENDER)
* Active display: 256Hz (RENDER)
* Right border: 15Hz (RENDER)
* Right blanking: 8Hz (skip)
* Horizontal sync: 26Hz (skip)
* Total: 342Hz (render: 284 pixels)
* =================================================
* Scanline (vertical) display timings:
* Top blanking: 13 lines (skip)
* Top border: 3 lines (skip)
* Top border: 24 lines (RENDER)
* Active display: 192 lines (RENDER)
* Bottom border: 24 lines (RENDER)
* Bottom blanking: 3 lines (skip)
* Vertical sync: 3 lines (skip)
* Total: 262 lines (render: 240 lines)
* =================================================
*/
class TMS9918A
{
public:
enum class ColorMode {
RGB555,
RGB565,
RGB565_Swap,
};
private:
void* arg;
void (*detectBlank)(void* arg);
void (*detectBreak)(void* arg);
void (*displayCallback)(void* arg, int frame, int line, unsigned short* display);
public:
unsigned short* display;
size_t displaySize;
bool displayNeedFree;
unsigned short palette[16];
const unsigned int rgb888[16] = {0x000000, 0x000000, 0x3EB849, 0x74D07D, 0x5955E0, 0x8076F1, 0xB95E51, 0x65DBEF, 0xDB6559, 0xFF897D, 0xCCC35E, 0xDED087, 0x3AA241, 0xB766B5, 0xCCCCCC, 0xFFFFFF};
typedef struct Context_ {
int bobo;
int countH;
int countV;
int frame;
int isRenderingLine;
int reverved32[3];
unsigned char ram[0x4000];
unsigned char reg[8];
unsigned char tmpAddr[2];
unsigned short addr;
unsigned short writeAddr;
unsigned char stat;
unsigned char latch;
unsigned char readBuffer;
unsigned char reserved8[1];
} Context;
Context* ctx;
bool ctxNeedFree;
unsigned short swap16(unsigned short src)
{
auto work = (src & 0xFF00) >> 8;
return ((src & 0x00FF) << 8) | work;
}
void initialize(ColorMode colorMode, void* arg, void (*detectBlank)(void*), void (*detectBreak)(void*), void (*displayCallback)(void*, int, int, unsigned short*) = nullptr, Context* vram = nullptr)
{
this->arg = arg;
this->detectBlank = detectBlank;
this->detectBreak = detectBreak;
this->displayCallback = displayCallback;
this->displaySize = (this->displayCallback ? 256 : 256 * 192) << 1;
this->display = (unsigned short*)malloc(this->displaySize);
this->displayNeedFree = true;
this->ctx = vram ? vram : (Context*)malloc(sizeof(Context));
this->ctxNeedFree = vram ? false : true;
memset(this->ctx, 0, sizeof(Context));
switch (colorMode) {
case ColorMode::RGB555:
for (int i = 0; i < 16; i++) {
this->palette[i] = 0;
this->palette[i] |= (this->rgb888[i] & 0b111110000000000000000000) >> 9;
this->palette[i] |= (this->rgb888[i] & 0b000000001111100000000000) >> 6;
this->palette[i] |= (this->rgb888[i] & 0b000000000000000011111000) >> 3;
}
break;
case ColorMode::RGB565:
for (int i = 0; i < 16; i++) {
this->palette[i] = 0;
this->palette[i] |= (this->rgb888[i] & 0b111110000000000000000000) >> 8;
this->palette[i] |= (this->rgb888[i] & 0b000000001111110000000000) >> 5;
this->palette[i] |= (this->rgb888[i] & 0b000000000000000011111000) >> 3;
}
break;
case ColorMode::RGB565_Swap:
for (int i = 0; i < 16; i++) {
this->palette[i] = 0;
this->palette[i] |= (this->rgb888[i] & 0b111110000000000000000000) >> 8;
this->palette[i] |= (this->rgb888[i] & 0b000000001111110000000000) >> 5;
this->palette[i] |= (this->rgb888[i] & 0b000000000000000011111000) >> 3;
this->palette[i] = swap16(this->palette[i]);
}
break;
default:
memset(this->palette, 0, sizeof(this->palette));
}
this->initRedneringLineTable();
this->reset();
}
~TMS9918A()
{
this->releaseDisplayBuffer();
this->releaseContext();
}
void useOwnDisplayBuffer(unsigned short* displayBuffer, size_t displayBufferSize)
{
this->releaseDisplayBuffer();
this->display = displayBuffer;
this->displaySize = displayBufferSize;
}
void reset()
{
memset(this->display, 0, this->displaySize);
memset(this->ctx, 0, sizeof(Context));
this->refresh();
}
void refresh() { this->acUpdate(); }
inline bool isEnabledScreen() { return ac.isEnabledScreen; }
inline bool isEnabledInterrupt() { return ac.isEnabledInterrupt; }
inline unsigned short getBackdropColor() { return ac.backdropColor; }
inline unsigned short getBackdropColor(bool swap) { return swap ? this->swap16(this->ac.backdropColor) : this->ac.backdropColor; }
inline void tick(int tickCount)
{
for (int i = 0; i < tickCount; i++) {
this->ctx->countH++;
// render backdrop border
if (this->ctx->isRenderingLine) {
if (24 + TMS9918A_SCREEN_WIDTH == this->ctx->countH) {
this->renderScanline(this->ctx->countV - 27);
}
}
// sync blank or end-of-frame
if (342 == this->ctx->countH) {
this->ctx->countH = 0;
this->ctx->countV++;
this->ctx->isRenderingLine = this->renderingLineTable[this->ctx->countV];
if (238 == this->ctx->countV) {
this->ctx->stat |= 0x80;
if (this->isEnabledInterrupt()) {
this->detectBlank(this->arg);
}
} else if (262 == this->ctx->countV) {
this->ctx->countV = 0;
this->detectBreak(this->arg);
this->ctx->frame++;
this->ctx->frame &= 0xFFFF;
}
}
}
}
inline unsigned char readData()
{
unsigned char result = this->ctx->readBuffer;
this->readVideoMemory();
this->ctx->latch = 0;
return result;
}
inline unsigned char readStatus()
{
unsigned char result = this->ctx->stat;
this->ctx->stat &= 0b01011111;
this->ctx->latch = 0;
return result;
}
inline void writeData(unsigned char value)
{
this->ctx->addr &= 0x3FFF;
this->ctx->readBuffer = value;
this->ctx->writeAddr = this->ctx->addr++;
this->ctx->ram[this->ctx->writeAddr] = this->ctx->readBuffer;
this->ctx->latch = 0;
}
inline void writeAddress(unsigned char value)
{
this->ctx->latch &= 1;
this->ctx->tmpAddr[this->ctx->latch++] = value;
if (2 == this->ctx->latch) {
if (this->ctx->tmpAddr[1] & 0b10000000) {
this->updateRegister();
} else if (this->ctx->tmpAddr[1] & 0b01000000) {
this->updateAddress();
} else {
this->updateAddress();
this->readVideoMemory();
}
} else {
this->ctx->addr &= 0xFF00;
this->ctx->addr |= this->ctx->tmpAddr[0];
}
}
private:
struct AddressCache {
int mode;
bool isEnabledScreen;
bool isEnabledInterrupt;
unsigned short backdropColor;
int si;
int mag;
int mag8;
int mag16;
int pn;
int ct0;
int ct2;
int pg0;
int pg2;
int pmask;
int cmask;
int sa;
int sg;
int bd;
} ac;
inline void acUpdateMode()
{
if (this->ctx->reg[1] & 0b00010000) {
this->ac.mode = 1;
} else if (this->ctx->reg[0] & 0b00000010) {
this->ac.mode = 2;
} else if (this->ctx->reg[1] & 0b00001000) {
this->ac.mode = 3;
} else {
this->ac.mode = 0;
}
}
static inline void acUpdate0(TMS9918A* tms)
{
tms->acUpdateMode();
}
static inline void acUpdate1(TMS9918A* tms)
{
tms->ac.si = tms->ctx->reg[1] & 0b00000010 ? 16 : 8;
tms->ac.mag = tms->ctx->reg[1] & 0b00000001 ? 2 : 1;
tms->ac.mag8 = tms->ac.mag * 8;
tms->ac.mag16 = tms->ac.mag * 16;
tms->ac.isEnabledScreen = tms->ctx->reg[1] & 0b01000000 ? true : false;
tms->ac.isEnabledInterrupt = tms->ctx->reg[1] & 0b00100000 ? true : false;
tms->acUpdateMode();
}
static inline void acUpdate2(TMS9918A* tms)
{
tms->ac.pn = (tms->ctx->reg[2] & 0b00001111) << 10;
}
static inline void acUpdate3(TMS9918A* tms)
{
tms->ac.ct0 = tms->ctx->reg[3] << 6;
tms->ac.ct2 = (tms->ctx->reg[3] & 0b10000000) << 6;
tms->ac.cmask = tms->ctx->reg[3] & 0b01111111;
tms->ac.cmask <<= 3;
tms->ac.cmask |= 0x07;
}
static inline void acUpdate4(TMS9918A* tms)
{
tms->ac.pg0 = (tms->ctx->reg[4] & 0b00000111) << 11;
tms->ac.pg2 = (tms->ctx->reg[4] & 0b00000100) << 11;
tms->ac.pmask = tms->ctx->reg[4] & 0b00000011;
tms->ac.pmask <<= 8;
tms->ac.pmask |= 0xFF;
}
static inline void acUpdate5(TMS9918A* tms)
{
tms->ac.sa = (tms->ctx->reg[5] & 0b01111111) << 7;
}
static inline void acUpdate6(TMS9918A* tms)
{
tms->ac.sg = (tms->ctx->reg[6] & 0b00000111) << 11;
}
static inline void acUpdate7(TMS9918A* tms)
{
tms->ac.bd = tms->ctx->reg[7] & 0b00001111;
tms->ac.backdropColor = tms->palette[tms->ac.bd];
}
void (*updateTable[8])(TMS9918A*) = {acUpdate0, acUpdate1, acUpdate2, acUpdate3, acUpdate4, acUpdate5, acUpdate6, acUpdate7};
inline void acUpdate(int n) { updateTable[n & 7](this); }
inline void acUpdate()
{
for (int i = 0; i < 8; i++) acUpdate(i);
}
int renderingLineTable[263];
void initRedneringLineTable()
{
for (int i = 0; i < 263; i++) {
if (27 <= i && i < 27 + 192) {
this->renderingLineTable[i] = 1;
} else {
this->renderingLineTable[i] = 0;
}
}
}
void releaseDisplayBuffer()
{
if (this->displayNeedFree) {
free(this->display);
this->display = nullptr;
this->displaySize = 0;
this->displayNeedFree = false;
}
}
void releaseContext()
{
if (this->ctxNeedFree) {
free(this->ctx);
this->ctx = nullptr;
this->ctxNeedFree = false;
}
}
inline void renderScanline(int lineNumber)
{
#ifdef TMS9918A_SKIP_ODD_FRAME_RENDERING
bool isEvenFrame = 0 == (this->ctx->frame & 1);
#endif
// TODO: Several modes (1, 3, undocumented) are not implemented
if (this->isEnabledScreen()) {
switch (this->ac.mode) {
#ifdef TMS9918A_SKIP_ODD_FRAME_RENDERING
case 0: this->renderScanlineMode0(lineNumber, isEvenFrame); break;
case 2: this->renderScanlineMode2(lineNumber, isEvenFrame); break;
#else
case 0: this->renderScanlineMode0(lineNumber); break;
case 2: this->renderScanlineMode2(lineNumber); break;
#endif
}
} else {
#ifdef TMS9918A_SKIP_ODD_FRAME_RENDERING
if (isEvenFrame) {
int dcur = this->getDisplayPtr(lineNumber);
for (int i = 0; i < 256; i++) {
this->display[dcur++] = this->ac.backdropColor;
}
}
#else
int dcur = this->getDisplayPtr(lineNumber);
for (int i = 0; i < 256; i++) {
this->display[dcur++] = this->ac.backdropColor;
}
#endif
}
#ifdef TMS9918A_SKIP_ODD_FRAME_RENDERING
if (this->displayCallback && isEvenFrame) {
this->displayCallback(this->arg, this->ctx->frame, lineNumber, this->display);
}
#else
if (this->displayCallback) {
this->displayCallback(this->arg, this->ctx->frame, lineNumber, this->display);
}
#endif
}
inline void updateAddress()
{
this->ctx->addr = this->ctx->tmpAddr[1];
this->ctx->addr <<= 8;
this->ctx->addr |= this->ctx->tmpAddr[0];
this->ctx->addr &= 0x3FFF;
}
inline void readVideoMemory()
{
this->ctx->addr &= 0x3FFF;
this->ctx->readBuffer = this->ctx->ram[this->ctx->addr++];
}
inline void updateRegister()
{
bool previousInterrupt = this->isEnabledInterrupt();
int r = this->ctx->tmpAddr[1] & 0b00001111;
this->ctx->reg[r] = this->ctx->tmpAddr[0];
this->acUpdate(r);
if (!previousInterrupt && this->isEnabledInterrupt() && this->ctx->stat & 0x80) {
this->detectBlank(this->arg);
}
}
inline int getDisplayPtr(int lineNumber)
{
return this->displayCallback ? 0 : lineNumber * 256;
}
inline void renderScanlineMode0(int lineNumber, bool rendering = true)
{
int dcur = this->getDisplayPtr(lineNumber);
int dcur0 = dcur;
if (rendering) {
int pixelLine = lineNumber % 8;
unsigned char* nam = &this->ctx->ram[ac.pn + lineNumber / 8 * 32];
int ptn;
int c;
int cc[2];
for (int i = 0; i < 32; i++) {
ptn = this->ctx->ram[ac.pg0 + nam[i] * 8 + pixelLine];
c = this->ctx->ram[ac.ct0 + nam[i] / 8];
cc[1] = (c & 0xF0) >> 4;
cc[1] = this->palette[cc[1] ? cc[1] : ac.bd];
cc[0] = c & 0x0F;
cc[0] = this->palette[cc[0] ? cc[0] : ac.bd];
this->display[dcur++] = cc[(ptn & 0b10000000) >> 7];
this->display[dcur++] = cc[(ptn & 0b01000000) >> 6];
this->display[dcur++] = cc[(ptn & 0b00100000) >> 5];
this->display[dcur++] = cc[(ptn & 0b00010000) >> 4];
this->display[dcur++] = cc[(ptn & 0b00001000) >> 3];
this->display[dcur++] = cc[(ptn & 0b00000100) >> 2];
this->display[dcur++] = cc[(ptn & 0b00000010) >> 1];
this->display[dcur++] = cc[ptn & 0b00000001];
}
}
renderSprites(lineNumber, &display[dcur0], rendering);
}
inline void renderScanlineMode2(int lineNumber, bool rendering = true)
{
int dcur = this->getDisplayPtr(lineNumber);
int dcur0 = dcur;
if (rendering) {
int pixelLine = lineNumber % 8;
unsigned char* nam = &this->ctx->ram[ac.pn + lineNumber / 8 * 32];
int ci = (lineNumber / 64) * 256;
int ptn;
int c;
int cc[2];
for (int i = 0; i < 32; i++) {
ptn = this->ctx->ram[ac.pg2 + ((nam[i] + ci) & ac.pmask) * 8 + pixelLine];
c = this->ctx->ram[ac.ct2 + ((nam[i] + ci) & ac.cmask) * 8 + pixelLine];
cc[1] = (c & 0xF0) >> 4;
cc[1] = this->palette[cc[1] ? cc[1] : ac.bd];
cc[0] = c & 0x0F;
cc[0] = this->palette[cc[0] ? cc[0] : ac.bd];
this->display[dcur++] = cc[(ptn & 0b10000000) >> 7];
this->display[dcur++] = cc[(ptn & 0b01000000) >> 6];
this->display[dcur++] = cc[(ptn & 0b00100000) >> 5];
this->display[dcur++] = cc[(ptn & 0b00010000) >> 4];
this->display[dcur++] = cc[(ptn & 0b00001000) >> 3];
this->display[dcur++] = cc[(ptn & 0b00000100) >> 2];
this->display[dcur++] = cc[(ptn & 0b00000010) >> 1];
this->display[dcur++] = cc[ptn & 0b00000001];
}
}
renderSprites(lineNumber, &display[dcur0], rendering);
}
inline void renderSprites(int lineNumber, unsigned short* renderPosition, bool rendering)
{
static const unsigned char bit[8] = {
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001};
bool si = this->ctx->reg[1] & 0b00000010;
bool mag = this->ctx->reg[1] & 0b00000001;
int sn = 0;
int tsn = 0;
unsigned char dlog[256];
unsigned char wlog[256];
memset(dlog, 0, sizeof(dlog));
memset(wlog, 0, sizeof(wlog));
bool limitOver = false;
for (int i = 0; i < 32; i++) {
int cur = ac.sa + i * 4;
unsigned char y = this->ctx->ram[cur++];
if (208 == y) break;
int x = this->ctx->ram[cur++];
unsigned char ptn = this->ctx->ram[cur++];
unsigned char col = this->ctx->ram[cur++];
if (col & 0x80) x -= 32;
col &= 0b00001111;
y++;
if (mag) {
if (si) {
// 16x16 x 2
if (y <= lineNumber && lineNumber < y + 32) {
sn++;
if (!col) tsn++;
if (5 == sn) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= 0b01000000 | i;
if (4 <= tsn) break;
limitOver = true;
} else if (sn < 5) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= i;
}
int pixelLine = lineNumber - y;
cur = ac.sg + (ptn & 252) * 8 + pixelLine % 16 / 2 + (pixelLine < 16 ? 0 : 8);
bool overflow = false;
for (int j = 0; !overflow && j < 16; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j / 2]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
cur += 16;
for (int j = 0; !overflow && j < 16; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j / 2]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
}
} else {
// 8x8 x 2
if (y <= lineNumber && lineNumber < y + 16) {
sn++;
if (!col) tsn++;
if (5 == sn) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= 0b01000000 | i;
if (4 <= tsn) break;
limitOver = true;
} else if (sn < 5) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= i;
}
cur = ac.sg + ptn * 8 + lineNumber % 8;
bool overflow = false;
for (int j = 0; !overflow && j < 16; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j / 2]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
}
}
} else {
if (si) {
// 16x16 x 1
if (y <= lineNumber && lineNumber < y + 16) {
sn++;
if (!col) tsn++;
if (5 == sn) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= 0b01000000 | i;
if (4 <= tsn) break;
limitOver = true;
} else if (sn < 5) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= i;
}
int pixelLine = lineNumber - y;
cur = ac.sg + (ptn & 252) * 8 + pixelLine % 8 + (pixelLine < 8 ? 0 : 8);
bool overflow = false;
for (int j = 0; !overflow && j < 8; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
cur += 16;
for (int j = 0; !overflow && j < 8; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
}
} else {
// 8x8 x 1
if (y <= lineNumber && lineNumber < y + 8) {
sn++;
if (!col) tsn++;
if (5 == sn) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= 0b01000000 | i;
if (4 <= tsn) break;
limitOver = true;
} else if (sn < 5) {
this->ctx->stat &= 0b11100000;
this->ctx->stat |= i;
}
cur = ac.sg + ptn * 8 + lineNumber % 8;
bool overflow = false;
for (int j = 0; !overflow && j < 8; j++, x++) {
if (x < 0) continue;
if (wlog[x] && !limitOver) {
this->ctx->stat |= 0b00100000;
}
if (0 == dlog[x]) {
if (this->ctx->ram[cur] & bit[j]) {
if (col && rendering) renderPosition[x] = this->palette[col];
dlog[x] = col;
wlog[x] = 1;
}
}
overflow = x == 0xFF;
}
}
}
}
}
}
};
#endif // INCLUDE_TMS9918A_HPP