-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv9958.hpp
2544 lines (2424 loc) · 95.5 KB
/
v9958.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* micro MSX2+ - V9958 (MSX-VIDEO)
* -----------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2023 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_V9958_HPP
#define INCLUDE_V9958_HPP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #define COMMAND_DEBUG
class V9958
{
private:
enum HorizontalEventType {
ActiveDisplayH,
RightBorder,
RightErase,
SyncRight,
LeftErase,
LeftBorder
};
enum VerticalEventType {
VerticalSync,
TopErase,
TopBorder,
ActiveDisplayV,
BottomBorder,
BottomErase
};
struct EventTables {
HorizontalEventType ht[1368];
VerticalEventType vt[262];
int hi[1368];
int vi[262];
} evt;
inline void updateEventTableH()
{
for (int i = 0; i < 1368; i++) {
int ii = i + this->getAdjustX();
if (ii < 0) {
ii += 1368;
} else if (1368 <= ii) {
ii -= 1368;
}
if (i < 1024) {
this->evt.ht[ii] = HorizontalEventType::ActiveDisplayH;
this->evt.hi[ii] = i;
} else if (i < 1024 + 56) {
// note: Actually RightBorder is 59Hz, but 56Hz for optimize the performance
this->evt.ht[ii] = HorizontalEventType::RightBorder;
this->evt.hi[ii] = i - 1024;
} else if (i < 1024 + 56 + 30) {
// note: Actually RightErase is 27Hz, but 30Hz for optimize the performance
this->evt.ht[ii] = HorizontalEventType::RightErase;
this->evt.hi[ii] = i - 1024 - 56;
} else if (i < 1024 + 56 + 30 + 100) {
this->evt.ht[ii] = HorizontalEventType::SyncRight;
this->evt.hi[ii] = i - 1024 - 56 - 30;
} else if (i < 1024 + 56 + 30 + 100 + 102) {
this->evt.ht[ii] = HorizontalEventType::LeftErase;
this->evt.hi[ii] = i - 1024 - 56 - 30 - 100;
} else {
this->evt.ht[ii] = HorizontalEventType::LeftBorder;
this->evt.hi[ii] = i - 1024 - 56 - 30 - 100 - 102;
}
}
}
inline void updateEventTableV()
{
for (int i = 0; i < 262; i++) {
int ii = i + this->getAdjustY();
if (ii < 0) {
ii += 262;
} else if (262 <= ii) {
ii -= 262;
}
if (i < 3) {
this->evt.vt[ii] = VerticalEventType::VerticalSync;
this->evt.vi[ii] = i;
} else if (i < 3 + 13) {
this->evt.vt[ii] = VerticalEventType::TopErase;
this->evt.vi[ii] = i - 3;
} else if (i < 3 + 13 + this->getTopBorder()) {
this->evt.vt[ii] = VerticalEventType::TopBorder;
this->evt.vi[ii] = i - 3 - 13;
} else if (i < 3 + 13 + this->getTopBorder() + this->getLineNumber()) {
this->evt.vt[ii] = VerticalEventType::ActiveDisplayV;
this->evt.vi[ii] = i - 3 - 13 - this->getTopBorder();
} else if (i < 3 + 13 + this->getTopBorder() + this->getLineNumber() + this->getBottomBorder()) {
this->evt.vt[ii] = VerticalEventType::BottomBorder;
this->evt.vi[ii] = i - 3 - 13 - this->getTopBorder() - this->getLineNumber();
} else {
this->evt.vt[ii] = VerticalEventType::BottomErase;
this->evt.vi[ii] = i - 3 - 13 - this->getTopBorder() - this->getLineNumber() - this->getBottomBorder();
}
}
}
unsigned short yjkColor[32][64][64];
const unsigned char regMask[64] = {
0x7e, 0x7b, 0x7f, 0xff, 0x3f, 0xff, 0x3f, 0xff,
0xfb, 0xbf, 0x07, 0x03, 0xff, 0xff, 0x07, 0x0f,
0x0f, 0xbf, 0xff, 0xff, 0x3f, 0x3f, 0x3f, 0xff,
0x00, 0x7f, 0x3f, 0x07, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
int colorMode;
void* arg;
void (*detectInterrupt)(void* arg, int ie);
void (*cancelInterrupt)(void* arg);
void (*detectBreak)(void* arg);
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
const int adjust[16] = {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1};
public:
bool renderLimitOverSprites = true;
#ifdef MSX2_DISPLAY_HALF_HORIZONTAL
unsigned short display[284 * 240];
#else
unsigned short display[568 * 240];
#endif
unsigned short palette[16];
unsigned char lastRenderScanline;
struct CommandContext {
int wait;
int sx;
int sy;
int dx;
int dy;
int nx;
int ny;
int dix;
int diy;
int maj;
int min;
double majF;
double minF;
};
struct Context {
int bobo;
int countH;
int countV;
unsigned int addr;
struct CommandContext cmd;
unsigned char ram[0x20000];
unsigned char reg[64];
unsigned char pal[16][2];
unsigned char tmpAddr[2];
unsigned char stat[16];
unsigned char latch1;
unsigned char latch2;
unsigned char readBuffer;
unsigned char command;
unsigned char commandL;
unsigned char reverseVdpR9Bit4;
unsigned char reverseVdpR9Bit5;
unsigned char hardwareResetFlag;
unsigned int counter;
unsigned char lineIE1;
unsigned char reserved[7];
} ctx;
V9958()
{
memset(palette, 0, sizeof(palette));
this->reset();
}
void initialize(int colorMode, void* arg, void (*detectInterrupt)(void*, int), void (*cancelInterrupt)(void*), void (*detectBreak)(void*))
{
this->colorMode = colorMode;
this->arg = arg;
this->detectInterrupt = detectInterrupt;
this->cancelInterrupt = cancelInterrupt;
this->detectBreak = detectBreak;
this->initYjkColorTable();
this->reset();
}
void initYjkColorTable()
{
for (int y = 0; y < 32; y++) {
for (int J = 0; J < 64; J++) {
for (int K = 0; K < 64; K++) {
int j = (J & 0x1f) - (J & 0x20);
int k = (K & 0x1f) - (K & 0x20);
int r = 255 * (y + j) / 31;
int g = 255 * (y + k) / 31;
int b = 255 * ((5 * y - 2 * j - k) / 4) / 31;
r = this->min(255, this->max(0, r));
g = this->min(255, this->max(0, g));
b = this->min(255, this->max(0, b));
r = (r & 0b11111000) << (7 + colorMode);
g = (g & 0b11111000) << (2 + colorMode);
b = (b & 0b11111000) >> 3;
this->yjkColor[y][J][K] = r | g | b;
}
}
}
}
void updateAllPalettes()
{
for (int i = 0; i < 16; i++) {
this->updatePaletteCacheFromRegister(i);
}
}
void reset()
{
static unsigned int rgb[16] = {
0x000000, 0x000000,
//*** *** ***
0b110000000010000000100000, // 6 1 1
0b111000000110000001100000, // 7 3 3
0b001000000010000011100000, // 1 1 7
0b011000000100000011100000, // 3 2 7
0b001000010100000000100000, // 1 5 1
0b110000001000000011100000, // 6 2 7
0b001000011100000000100000, // 1 7 1
0b011000011100000001100000, // 3 7 3
0b110000011000000000100000, // 6 6 1
0b110000011000000010000000, // 6 6 4
0b100000000100000000100000, // 4 1 1
0b010000011000000010100000, // 2 6 5
0b101000101000000010100000, // 5 5 5
0b111000111000000011100000 // 7 7 7
};
static unsigned char stat[16] = {
0x1f, 0x00, 0xcc, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static unsigned char reg[64] = {
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
memset(this->display, 0, sizeof(this->display));
memset(&this->ctx, 0, sizeof(this->ctx));
memcpy(&this->ctx.stat, stat, sizeof(stat));
memcpy(&this->ctx.reg, reg, sizeof(reg));
this->ctx.hardwareResetFlag = 0xFF;
for (int i = 0; i < 16; i++) {
this->ctx.pal[i][0] = 0;
this->ctx.pal[i][1] = 0;
this->ctx.pal[i][0] |= (rgb[i] & 0b111000000000000000000000) >> 17;
this->ctx.pal[i][1] |= (rgb[i] & 0b000000001110000000000000) >> 13;
this->ctx.pal[i][0] |= (rgb[i] & 0b000000000000000011100000) >> 5;
updatePaletteCacheFromRegister(i);
}
this->updateEventTables();
}
inline int getScreenMode()
{
int mode = this->ctx.reg[1] & 0b00011000;
mode |= (this->ctx.reg[0] & 0b00001110) >> 1;
return mode;
}
inline int getVideoMode()
{
int mode = 0;
if (ctx.reg[1] & 0b00010000) mode |= 0b00001;
if (ctx.reg[1] & 0b00001000) mode |= 0b00010;
if (ctx.reg[0] & 0b00000010) mode |= 0b00100;
if (ctx.reg[0] & 0b00000100) mode |= 0b01000;
if (ctx.reg[0] & 0b00001000) mode |= 0b10000;
return mode;
}
inline void updateEventTables()
{
this->updateEventTableH();
this->updateEventTableV();
}
inline bool isExpansionRAM() { return ctx.reg[45] & 0b01000000 ? true : false; }
inline int getSpriteSize() { return ctx.reg[1] & 0b00000010 ? 16 : 8; }
inline bool isSpriteMag() { return ctx.reg[1] & 0b00000001 ? true : false; }
inline bool isEnabledExternalVideoInput() { return ctx.reg[0] & 0b00000001 ? true : false; }
inline bool isEnabledScreen() { return ctx.reg[1] & 0b01000000 ? true : false; }
inline bool isIE0() { return ctx.reg[1] & 0b00100000 ? true : false; }
inline bool isIE1() { return ctx.reg[0] & 0b00010000 ? true : false; }
inline bool isIE2() { return ctx.reg[0] & 0b00100000 ? true : false; }
inline bool isEnabledMouse() { return ctx.reg[8] & 0b10000000 ? true : false; }
inline bool isEnabledLightPen() { return ctx.reg[8] & 0b01000000 ? true : false; }
inline bool isYAE() { return ctx.reg[25] & 0b00010000 ? true : false; }
inline bool isYJK() { return ctx.reg[25] & 0b00001000 ? true : false; }
inline bool isMaskLeft8px() { return ctx.reg[25] & 0b00000010 ? true : false; }
inline bool getSP2() { return ctx.reg[25] & 0b00000001; }
inline int getOnTime() { return (ctx.reg[13] & 0xF0) >> 4; }
inline int getOffTime() { return ctx.reg[13] & 0x0F; }
inline int getSyncMode() { return (ctx.reg[9] & 0b00110000) >> 4; }
inline int getTopBorder() { return 26 - ((this->ctx.reg[9] & 0b10000000) >> 7) * 10; }
inline int getBottomBorder() { return 25 - ((this->ctx.reg[9] & 0b10000000) >> 7) * 10; }
inline int getLineNumber() { return 192 + ((this->ctx.reg[9] & 0b10000000) >> 7) * 20; }
inline int isInterlaceMode() { return this->ctx.reg[9] & 0b00001000 ? true : false; }
inline int isEvenOrderMode() { return this->ctx.reg[9] & 0b00000100 ? true : false; }
inline bool isSprite16px() { return this->ctx.reg[1] & 0b00000010 ? true : false; }
inline bool isSprite2x() { return this->ctx.reg[1] & 0b00000001 ? true : false; }
inline bool isSpriteDisplay() { return this->ctx.reg[8] & 0b00000010 ? false : true; }
inline int getAdjustX() { return this->adjust[this->ctx.reg[18] & 0x0F]; }
inline int getAdjustY() { return this->adjust[(this->ctx.reg[18] & 0xF0) >> 4]; }
inline bool isF() { return this->ctx.stat[0] & 0x80; }
inline void setF() { this->ctx.stat[0] |= 0x80; }
inline void resetF() { this->ctx.stat[0] &= 0x7F; }
inline void reset5S() { this->ctx.stat[0] &= 0b10111111; }
inline void resetCollision() { this->ctx.stat[0] &= 0b11011111; }
inline void set5S(bool f, int n)
{
this->ctx.stat[0] &= 0b11100000;
this->ctx.stat[0] |= (f ? 0b01000000 : 0) | (n & 0b00011111);
}
inline void setCollision(int x, int y)
{
this->ctx.stat[0] |= 0b00100000;
x += 12;
y += 8;
this->ctx.stat[3] = x & 0xFF;
this->ctx.stat[4] = (x & 0x0100) >> 8;
this->ctx.stat[5] = y & 0xFF;
this->ctx.stat[6] = (y & 0x0300) >> 8;
}
inline bool isFH() { return this->ctx.stat[1] & 0x01; }
inline void setFH() { this->ctx.stat[1] |= 0b00000001; }
inline void resetFH() { this->ctx.stat[1] &= 0b11111110; }
inline void setTR() { this->ctx.stat[2] |= 0b10000000; }
inline void resetTR() { this->ctx.stat[2] &= 0b01111111; }
inline void setVR() { this->ctx.stat[2] |= 0b01000000; }
inline void resetVR() { this->ctx.stat[2] &= 0b10111111; }
inline void setHR() { this->ctx.stat[2] |= 0b00100000; }
inline void resetHR() { this->ctx.stat[2] &= 0b11011111; }
inline void setBD() { this->ctx.stat[2] |= 0b00010000; }
inline void resetBD() { this->ctx.stat[2] &= 0b11101111; }
inline void setEO() { this->ctx.stat[2] |= 0b00000010; }
inline void resetEO() { this->ctx.stat[2] &= 0b11111101; }
inline void setCE() { this->ctx.stat[2] |= 0b00000001; }
inline void resetCE() { this->ctx.stat[2] &= 0b11111110; }
inline int getSpriteAttributeTableM1()
{
int addr = this->ctx.reg[11] & 0b00000011;
addr <<= 15;
addr |= (this->ctx.reg[5] & 0b11111111) << 7;
return addr;
}
inline int getSpriteAttributeTableM2()
{
int addr = this->ctx.reg[11] & 0b00000011;
addr <<= 15;
addr |= (this->ctx.reg[5] & 0b11111100) << 7;
return addr;
}
inline int getSpriteColorTable()
{
int addr = this->ctx.reg[11] & 0b00000011;
addr <<= 15;
addr |= (this->ctx.reg[5] & 0b11111000) << 7;
return addr;
}
inline int getSpriteGeneratorTable()
{
int addr = this->ctx.reg[6] & 0b00111111;
addr <<= 11;
return addr;
}
inline int getAddressMask()
{
switch (this->getScreenMode()) {
case 0b00000: // GRAPHIC1
case 0b00001: // GRAPHIC2
case 0b01000: // MULTI COLOR
case 0b10000: // TEXT1
return 0x3FFF;
case 0b00010: // GRAPHIC3
case 0b00011: // GRAPHIC4
case 0b00100: // GRAPHIC5
case 0b00101: // GRAPHIC6
case 0b00111: // GRAPHIC7
case 0b10010: // TEXT2
return 0x1FFFF;
default:
return 0;
}
}
inline unsigned short getBackdropColor()
{
switch (this->getScreenMode()) {
case 0b00111: return this->convertColor_8bit_to_16bit(this->ctx.reg[7]);
case 0b00100: return this->palette[this->ctx.reg[7] & 0b00000011];
default: return this->palette[this->ctx.reg[7] & 0b00001111];
}
}
inline unsigned short getTextColor()
{
if (this->getScreenMode() == 0b00111) {
return 0;
} else {
return this->palette[(this->ctx.reg[7] & 0b11110000) >> 4];
}
}
inline int getNameTableAddress()
{
switch (this->getScreenMode()) {
case 0b00000: // GRAPHIC1
case 0b00001: // GRAPHIC2
case 0b00010: // GRAPHIC3
case 0b01000: // MULTI COLOR
case 0b10000: // TEXT1
return (this->ctx.reg[2] & 0b01111111) << 10;
case 0b00011: // GRAPHIC4
case 0b00100: // GRAPHIC5
return (this->ctx.reg[2] & 0b01100000) << 10;
case 0b00101: // GRAPHIC6
case 0b00111: // GRAPHIC7
return (this->ctx.reg[2] & 0b00100000) << 11;
case 0b10010: // TEXT2
return (this->ctx.reg[2] & 0b01111100) << 10;
default:
return 0;
}
}
inline int getNameTableSize()
{
switch (this->getScreenMode()) {
case 0b00000: return 768; // GRAPHIC1
case 0b00001: return 768; // GRAPHIC2
case 0b00010: return 768; // GRAPHIC3
case 0b00011: // GRAPHIC4
case 0b00100: // GRAPHIC5
return this->getLineNumber() == 192 ? 0x6000 : 0x6A00;
case 0b00101: // GRAPHIC6
case 0b00111: // GRAPHIC7
return this->getLineNumber() == 192 ? 0xC000 : 0xD400;
case 0b01000: return 768; // MULTI COLOR
case 0b10000: return 40 * 24; // TEXT1
case 0b10010: return 80 * 27; // TEXT2
default: return 0; // n/a
}
}
inline int getPatternGeneratorAddress()
{
switch (this->getScreenMode()) {
case 0b00000: // GRAPHIC1
case 0b01000: // MULTI COLOR
case 0b10000: // TEXT1
case 0b10010: // TEXT2
return (this->ctx.reg[4] & 0b00111111) << 11;
case 0b00001: // GRAPHIC2
case 0b00010: // GRAPHIC3
return (this->ctx.reg[4] & 0b00111100) << 11;
default:
return 0; // n/a
}
}
inline int getPatternGeneratorSize()
{
switch (this->getScreenMode()) {
case 0b00000: return 2048; // GRAPHIC1
case 0b00001: return 6144; // GRAPHIC2
case 0b00010: return 6144; // GRAPHIC3
case 0b01000: return 2048; // MULTI COLOR
case 0b10000: return 2048; // TEXT1
case 0b10010: return 2048; // TEXT2
default: return 0; // n/a
}
}
inline int getColorTableAddress()
{
switch (this->getScreenMode()) {
case 0b00000: // GRAPHIC1
{
int r3 = this->ctx.reg[3];
int r10 = this->ctx.reg[10] & 0b00000111;
return (r3 << 6) + (r10 << 14);
}
case 0b00001: // GRAPHIC2
case 0b00010: // GRAPHIC3
{
int r3 = this->ctx.reg[3] & 0b10000000;
int r10 = this->ctx.reg[10] & 0b00000111;
return (r3 << 6) + (r10 << 14);
}
case 0b10010: // TEXT2 (Blink Table)
{
int r3 = this->ctx.reg[3] & 0b11111000;
int r10 = this->ctx.reg[10] & 0b00000111;
return (r3 << 6) + (r10 << 14);
}
default:
return 0; // n/a
}
}
inline int getColorTableSize()
{
switch (this->getScreenMode()) {
case 0b00000: return 32; // GRAPHIC1
case 0b00001: return 6144; // GRAPHIC2
case 0b00010: return 6144; // GRAPHIC3
case 0b10010: return 240; // TEXT2 (Blink Table)
default: return 0; // n/a
}
}
inline void tick(int tickCount)
{
for (int i = 0; i < tickCount; i++) {
// execute command
if (this->ctx.cmd.wait) {
this->ctx.cmd.wait--;
}
if (this->ctx.command && 0 == this->ctx.cmd.wait) {
switch (this->ctx.command) {
case 0b1101: this->executeCommandHMMM(false); break;
case 0b1100: this->executeCommandHMMV(false); break;
case 0b1001: this->executeCommandLMMM(false); break;
case 0b1000: this->executeCommandLMMV(false); break;
case 0b0111: this->executeCommandLINE(false); break;
case 0b0110: this->executeCommandSRCH(false); break;
case 0b0101: this->executeCommandPSET(false); break;
case 0b0100: this->executeCommandPOINT(false); break;
}
}
// count up (H)
auto htPrev = this->evt.ht[this->ctx.countH];
this->ctx.countH++;
this->ctx.countH %= 1368;
if (htPrev != this->evt.ht[this->ctx.countH]) {
tick_checkHorizontalEvents();
}
}
}
inline void tick_checkHorizontalEvents()
{
switch (this->evt.ht[this->ctx.countH]) {
case HorizontalEventType::LeftErase:
break;
case HorizontalEventType::LeftBorder:
break;
case HorizontalEventType::ActiveDisplayH:
this->resetHR(); // horizontal active
if (this->isF() || this->isFH()) this->checkIRQ(); // fire IRQ if F|FH
break;
case HorizontalEventType::RightBorder:
this->setHR(); // horizontal blanking
if (this->isIE1()) {
this->tick_checkIntH(); // set F if match Reg.19
} else {
this->ctx.stat[1] &= 0xFE; // Reset FH if is not IE1
}
break;
case HorizontalEventType::RightErase:
this->tick_display();
break;
case HorizontalEventType::SyncRight: {
// count up (V)
auto vtPrev = this->evt.vt[this->ctx.countV];
this->ctx.countV++;
this->ctx.countV %= 262;
if (vtPrev != this->evt.vt[this->ctx.countV]) {
tick_checkVerticalEvents();
}
break;
}
}
}
inline void tick_checkVerticalEvents()
{
switch (this->evt.vt[this->ctx.countV]) {
case VerticalEventType::VerticalSync:
this->ctx.counter++;
this->detectBreak(this->arg);
break;
case VerticalEventType::TopErase:
break;
case VerticalEventType::TopBorder:
break;
case VerticalEventType::ActiveDisplayV:
this->resetVR();
break;
case VerticalEventType::BottomBorder:
this->setVR();
this->setF(); // NOTE: do not callback at this timing (callback after HR reset)
break;
case VerticalEventType::BottomErase:
break;
}
}
inline int displayWidth()
{
#ifdef MSX2_DISPLAY_HALF_HORIZONTAL
return 284;
#else
return 568;
#endif
}
inline void tick_display()
{
int scanline = this->evt.vi[this->ctx.countV] - this->getAdjustY();
switch (this->evt.vt[this->ctx.countV]) {
case VerticalEventType::TopBorder:
break;
case VerticalEventType::ActiveDisplayV:
scanline += this->getTopBorder();
break;
case VerticalEventType::BottomBorder:
scanline += this->getTopBorder() + this->getLineNumber();
break;
default: return;
}
if (scanline < 0) {
scanline += 240;
} else if (240 <= scanline) {
return;
}
// render backdrop
auto renderPosition = &this->display[scanline * this->displayWidth()];
if (0b00100 == this->getScreenMode()) {
unsigned short ec = this->palette[(this->ctx.reg[7] & 0b00001100) >> 2];
#ifdef MSX2_DISPLAY_HALF_HORIZONTAL
for (int x = 0; x < 284; x++) {
renderPosition[x] = ec;
}
#else
unsigned short oc = this->palette[this->ctx.reg[7] & 0b00000011];
for (int x = 0; x < 568; x += 2) {
renderPosition[x] = ec;
renderPosition[x + 1] = oc;
}
#endif
} else {
unsigned short bc = this->getBackdropColor();
for (int x = 0; x < this->displayWidth(); x++) {
renderPosition[x] = bc;
}
}
// render main display
if (this->getTopBorder() - this->getAdjustY() <= scanline) {
int renderLine = scanline - (this->getTopBorder() - this->getAdjustY());
if (renderLine < this->getLineNumber()) {
#ifdef MSX2_DISPLAY_HALF_HORIZONTAL
this->renderScanline(renderLine, &renderPosition[13 - this->getAdjustX()]);
#else
this->renderScanline(renderLine, &renderPosition[26 - (this->getAdjustX() << 1)]);
#endif
}
}
}
inline void tick_checkIntH()
{
if (!this->isFH()) {
int scanline;
switch (this->evt.vt[this->ctx.countV]) {
case VerticalEventType::ActiveDisplayV:
scanline = this->evt.vi[this->ctx.countV];
break;
case VerticalEventType::BottomBorder:
scanline = this->evt.vi[this->ctx.countV];
scanline += this->getLineNumber();
break;
case VerticalEventType::TopBorder:
scanline = this->evt.vi[this->ctx.countV];
scanline += this->getLineNumber();
scanline += this->getBottomBorder();
break;
default:
return;
}
if (scanline == this->ctx.lineIE1) {
this->setFH();
}
}
}
inline unsigned char inPort98()
{
unsigned char result = this->ctx.readBuffer;
this->readVideoMemory();
this->ctx.latch1 = 0;
return result;
}
inline unsigned char inPort99()
{
int sn = this->ctx.reg[15] & 0b00001111;
unsigned char result = this->ctx.stat[sn];
switch (sn) {
case 0: // |F|S5|C|5#|5#|5#|5#|5#|
this->reset5S();
this->resetCollision();
if (this->isF()) {
this->resetF();
this->checkIRQ();
}
break;
case 1: // |*|*|ID|ID|ID|ID|ID|FH|
if (this->isFH() && this->isIE1()) {
this->resetFH();
this->checkIRQ();
}
result &= 0b00000001;
result |= 0b00000100;
break;
case 2: // |TR|VR|HR|BD|1|1|EO|CE|
result |= 0b00001100;
break;
case 5:
this->ctx.stat[3] = 0;
this->ctx.stat[4] = 0;
this->ctx.stat[5] = 0;
this->ctx.stat[6] = 0;
break;
case 7:
if (this->ctx.command == 0b1010) {
result = this->ctx.stat[7];
executeCommandLMCM(false);
}
break;
}
this->ctx.latch1 = 0;
return result;
}
inline void outPort98(unsigned char value)
{
this->ctx.readBuffer = value;
this->ctx.ram[this->ctx.addr] = this->ctx.readBuffer;
this->incrementAddress();
this->ctx.latch1 = 0;
}
inline void outPort99(unsigned char value)
{
this->ctx.latch1 &= 1;
this->ctx.tmpAddr[this->ctx.latch1++] = value;
if (2 == this->ctx.latch1) {
if ((this->ctx.tmpAddr[1] & 0b11000000) == 0b10000000) {
// Direct access to VDP registers
this->updateRegister(this->ctx.tmpAddr[1] & 0b00111111, this->ctx.tmpAddr[0]);
} else if (this->ctx.tmpAddr[1] & 0b01000000) {
this->updateAddress();
} else {
this->updateAddress();
this->readVideoMemory();
}
}
}
inline void outPort9A(unsigned char value)
{
this->ctx.latch2 &= 1;
int pn = this->ctx.reg[16] & 0b00001111;
this->ctx.pal[pn][this->ctx.latch2++] = value;
if (2 == this->ctx.latch2) {
updatePaletteCacheFromRegister(pn);
this->ctx.reg[16]++;
this->ctx.reg[16] &= 0b00001111;
}
}
inline void outPort9B(unsigned char value)
{
unsigned char r17 = this->ctx.reg[17];
if (17 != (r17 & 0b00111111)) {
this->updateRegister(r17 & 0b00111111, value);
}
if (0 == (r17 & 0b10000000)) {
this->ctx.reg[17] = (r17 + 1) & 0b00111111;
}
}
#if 0 // このポートを実装すると一部ゲームで表示がバグるので実装しない
inline void outPortF3(unsigned char value) {
/*
b0 M3
b1 M4
b2 M5
b3 M2
b4 M1
b5 TP
b6 YUV ... not support yet
b7 YAE ... not support yet
*/
// update M3/M4/M5
unsigned char r0 = this->ctx.reg[0] & 0b11110001;
r0 |= (value & 0b00000111) << 1;
this->updateRegister(0, r0);
// update M2/M1
unsigned char r1 = this->ctx.reg[1] & 0b11100111;
r1 |= value & 0b00011000;
this->updateRegister(1, r1);
// update TP
unsigned char r8 = this->ctx.reg[8] & 0b11011111;
r8 |= value & 0b00100000;
this->updateRegister(8, r8);
}
#endif
inline void outPortF4(unsigned char value)
{
this->ctx.hardwareResetFlag = value;
}
inline unsigned char inPortF4()
{
return this->ctx.hardwareResetFlag;
}
inline unsigned short bit2to5(unsigned short n)
{
n <<= 3;
n |= n & 0b01000 ? 1 : 0;
n |= n & 0b10000 ? 2 : 0;
n |= n & 0b11000 ? 4 : 0;
return n;
}
inline unsigned short bit3to5(unsigned short n)
{
n <<= 2;
n |= n & 0b01000 ? 1 : 0;
n |= n & 0b10000 ? 2 : 0;
return n;
}
inline unsigned short bit3to6(unsigned short n)
{
n <<= 3;
n |= n & 0b001000 ? 1 : 0;
n |= n & 0b010000 ? 2 : 0;
n |= n & 0b100000 ? 4 : 0;
return n;
}
inline void updatePaletteCacheFromRegister(int pn)
{
unsigned short r = (this->ctx.pal[pn][0] & 0b01110000) >> 4;
unsigned short b = this->ctx.pal[pn][0] & 0b00000111;
unsigned short g = this->ctx.pal[pn][1] & 0b00000111;
switch (this->colorMode) {
case 0: // RGB555
r = this->bit3to5(r) << 10;
g = this->bit3to5(g) << 5;
b = this->bit3to5(b);
break;
case 1: // RGB565
r = this->bit3to5(r) << 11;
g = this->bit3to6(g) << 5;
b = this->bit3to5(b);
break;
default:
r = 0;
g = 0;
b = 0;
}
this->palette[pn] = r | g | b;
}
inline const char* where(int addr)
{
static const char* answer[] = {
"UNKNOWN",
"NameTable",
"PatternGenerator",
"ColorTable",
"SpriteGenerator",
"SpriteAttributeTable",
"SpriteColorTable",
};
int nt = this->getNameTableAddress();
int pg = this->getPatternGeneratorAddress();
int ct = this->getColorTableAddress();
int sg = this->getSpriteGeneratorTable();
int sa = this->getSpriteAttributeTableM2();
int sc = this->getSpriteColorTable();
if (nt <= addr && addr < nt + this->getNameTableSize()) {
return answer[1];
}
if (pg <= addr && addr < pg + this->getPatternGeneratorSize()) {
return answer[2];
}
if (ct <= addr && addr < ct + this->getColorTableSize()) {
return answer[3];
}
if (sg <= addr && addr < sg + 8 * 256) {
return answer[4];
}
if (sa <= addr && addr < sa + 4 * 32) {
return answer[5];
}
if (sc <= addr && addr < sc + 512) {
return answer[6];
}
return answer[0];
}
inline void checkIRQ()
{
if (this->isIE0() && this->isF()) {
this->detectInterrupt(this->arg, 0);
} else if (this->isIE1() && this->isFH()) {
this->detectInterrupt(this->arg, 1);
} else {
this->cancelInterrupt(this->arg);
}
}
inline void updateAddress()
{
this->ctx.addr = this->ctx.tmpAddr[1] & 0b00111111;
this->ctx.addr <<= 8;
this->ctx.addr |= this->ctx.tmpAddr[0];
this->ctx.addr |= ((int)(this->ctx.reg[14] & 0b00000111)) << 14;
}
inline void readVideoMemory()
{
this->ctx.readBuffer = this->ctx.ram[this->ctx.addr];
this->incrementAddress();
}
inline bool isTMS9918ACompatMode()
{
switch (this->getScreenMode()) {
case 0b00000: return true; // GRAPHIC1
case 0b00001: return true; // GRAPHIC2
case 0b01000: return true; // MULTI COLOR
case 0b10000: return true; // TEXT1
default: return false;
}
}
inline void incrementAddress()
{
this->ctx.addr++;
this->ctx.addr &= this->isTMS9918ACompatMode() ? 0x3FFF : 0x1FFFF;
unsigned char r14 = (this->ctx.addr >> 14) & 0b00000111;
if (r14 != this->ctx.reg[14]) {
this->ctx.reg[14] = r14;
}
}