forked from audetto/AppleWin
/
NTSC.cpp
2640 lines (2263 loc) · 87.4 KB
/
NTSC.cpp
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
/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 2010-2011, William S Simms
Copyright (C) 2014-2016, Michael Pohoreski, Tom Charlesworth
AppleWin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
AppleWin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Includes
#include "StdAfx.h"
#include "NTSC.h"
#include "Core.h"
#include "CPU.h" // CpuGetCyclesThisVideoFrame()
#include "Memory.h" // MemGetMainPtr(), MemGetAuxPtr(), MemGetAnnunciator()
#include "Interface.h" // GetFrameBuffer()
#include "RGBMonitor.h"
#include "VidHD.h"
#include "NTSC_CharSet.h"
// Some reference material here from 2000:
// http://www.kreativekorp.com/miscpages/a2info/munafo.shtml
//
#define NTSC_REMOVE_WHITE_RINGING 1 // 0 = theoritical dimmed white has chroma, 1 = pure white without chroma tinting
#define NTSC_REMOVE_BLACK_GHOSTING 1 // 1 = remove black smear/smudges carrying over
#define NTSC_REMOVE_GRAY_CHROMA 1 // 1 = remove all chroma in gray1 and gray2
#define DEBUG_PHASE_ZERO 0
#define ALT_TABLE 0
#if ALT_TABLE
#include "ntsc_rgb.h"
#endif
// Defines
#define HGR_TEST_PATTERN 0
#ifdef _MSC_VER
#define INLINE __forceinline
#else
#define INLINE inline
#endif
#define PI 3.1415926535898f
#define DEG_TO_RAD(x) (PI*(x)/180.f) // 2PI=360, PI=180,PI/2=90,PI/4=45
#define RAD_45 PI*0.25f
#define RAD_90 PI*0.5f
#define RAD_360 PI*2.f
// sadly float64 precision is needed
#define real double
//#define CYCLESTART (PI/4.f) // PI/4 = 45 degrees
#define CYCLESTART (DEG_TO_RAD(45))
// Globals (Public) ___________________________________________________
uint16_t g_nVideoClockVert = 0; // 9-bit: VC VB VA V5 V4 V3 V2 V1 V0 = 0 .. 262
uint16_t g_nVideoClockHorz = 0; // 6-bit: H5 H4 H3 H2 H1 H0 = 0 .. 64, 25 >= visible (NB. final hpos is 2 cycles long, so a line is 65 cycles)
// Globals (Private) __________________________________________________
static int g_nVideoCharSet = 0;
static int g_nVideoMixed = 0;
static int g_nHiresPage = 1;
static int g_nTextPage = 1;
static bool g_bDelayVideoMode = false; // NB. No need to save to save-state, as it will be done immediately after opcode completes in NTSC_VideoUpdateCycles()
static uint32_t g_uNewVideoModeFlags = 0;
// Understanding the Apple II, Timing Generation and the Video Scanner, Pg 3-11
// Vertical Scanning
// Horizontal Scanning
// "There are exactly 17030 (65 x 262) 6502 cycles in every television scan of an American Apple."
#define VIDEO_SCANNER_MAX_HORZ 65 // TODO: use Video.cpp: kHClocks
#define VIDEO_SCANNER_MAX_VERT 262 // TODO: use Video.cpp: kNTSCScanLines
static const UINT VIDEO_SCANNER_6502_CYCLES = VIDEO_SCANNER_MAX_HORZ * VIDEO_SCANNER_MAX_VERT;
#define VIDEO_SCANNER_MAX_VERT_PAL 312
static const UINT VIDEO_SCANNER_6502_CYCLES_PAL = VIDEO_SCANNER_MAX_HORZ * VIDEO_SCANNER_MAX_VERT_PAL;
static UINT g_videoScannerMaxVert = VIDEO_SCANNER_MAX_VERT; // default to NTSC
static UINT g_videoScanner6502Cycles = VIDEO_SCANNER_6502_CYCLES; // default to NTSC
#define VIDEO_SCANNER_HORZ_COLORBURST_BEG 12
#define VIDEO_SCANNER_HORZ_COLORBURST_END 16
#define VIDEO_SCANNER_HORZ_START 25 // first displayable horz scanner index
#define VIDEO_SCANNER_Y_MIXED 160 // num scanlins for mixed graphics + text
#define VIDEO_SCANNER_Y_DISPLAY 192 // max displayable scanlines
#define VIDEO_SCANNER_Y_DISPLAY_IIGS 200
// These 3 vars are initialized in NTSC_VideoInit()
static bgra_t* g_pVideoAddress = 0;
// To maintain the 280x192 aspect ratio for 560px width, we double every scan line -> 560x384
// NB. For IIgs SHR, the 320x200 is again doubled (to 640x400), but this gives a ~16:9 ratio, when 4:3 is probably required (ie. stretch height from 200 to 240)
static bgra_t* g_pScanLines[VIDEO_SCANNER_Y_DISPLAY_IIGS * 2];
static UINT g_kFrameBufferWidth = 0;
static unsigned short (*g_pHorzClockOffset)[VIDEO_SCANNER_MAX_HORZ] = 0;
typedef void (*UpdateScreenFunc_t)(long);
static UpdateScreenFunc_t g_apFuncVideoUpdateScanline[VIDEO_SCANNER_Y_DISPLAY];
static UpdateScreenFunc_t g_pFuncUpdateTextScreen = 0; // updateScreenText40;
static UpdateScreenFunc_t g_pFuncUpdateGraphicsScreen = 0; // updateScreenText40;
static UpdateScreenFunc_t g_pFuncModeSwitchDelayed = 0;
typedef void (*UpdatePixelFunc_t)(uint16_t);
static UpdatePixelFunc_t g_pFuncUpdateBnWPixel = 0; //updatePixelBnWMonitorSingleScanline;
static UpdatePixelFunc_t g_pFuncUpdateHuePixel = 0; //updatePixelHueMonitorSingleScanline;
static uint8_t g_nTextFlashCounter = 0;
static uint16_t g_nTextFlashMask = 0;
static unsigned g_aPixelMaskGR [ 16];
static uint16_t g_aPixelDoubleMaskHGR[128]; // hgrbits -> g_aPixelDoubleMaskHGR: 7-bit mono 280 pixels to 560 pixel doubling
static int g_nLastColumnPixelNTSC;
static int g_nColorBurstPixels;
#define INITIAL_COLOR_PHASE 0
static int g_nColorPhaseNTSC = INITIAL_COLOR_PHASE;
static int g_nSignalBitsNTSC = 0;
#define NTSC_NUM_PHASES 4
#define NTSC_NUM_SEQUENCES 4096
/*extern*/ uint32_t g_nChromaSize = 0; // for NTSC_VideoGetChromaTable()
static bgra_t g_aBnWMonitor [NTSC_NUM_SEQUENCES];
static bgra_t g_aHueMonitor[NTSC_NUM_PHASES][NTSC_NUM_SEQUENCES];
static bgra_t g_aBnwColorTV [NTSC_NUM_SEQUENCES];
static bgra_t g_aHueColorTV[NTSC_NUM_PHASES][NTSC_NUM_SEQUENCES];
// g_aBnWMonitor * g_nMonochromeRGB -> g_aBnWMonitorCustom
// g_aBnwColorTV * g_nMonochromeRGB -> g_aBnWColorTVCustom
static bgra_t g_aBnWMonitorCustom [NTSC_NUM_SEQUENCES];
static bgra_t g_aBnWColorTVCustom [NTSC_NUM_SEQUENCES];
#define CHROMA_ZEROS 2
#define CHROMA_POLES 2
#define CHROMA_GAIN 7.438011255f // Should this be 7.15909 MHz ?
#define CHROMA_0 -0.7318893645f
#define CHROMA_1 1.2336442711f
//#define LUMGAIN 1.062635655e+01
//#define LUMCOEF1 -0.3412038399
//#define LUMCOEF2 0.9647813115
#define LUMA_ZEROS 2
#define LUMA_POLES 2
#define LUMA_GAIN 13.71331570f // Should this be 14.318180 MHz ?
#define LUMA_0 -0.3961075449f
#define LUMA_1 1.1044202472f
#define SIGNAL_ZEROS 2
#define SIGNAL_POLES 2
#define SIGNAL_GAIN 7.614490548f // Should this be 7.15909 MHz ?
#define SIGNAL_0 -0.2718798058f
#define SIGNAL_1 0.7465656072f
// Tables
// Video scanner tables are now runtime-generated using UTAIIe logic
static unsigned short g_aClockVertOffsetsHGR[VIDEO_SCANNER_MAX_VERT_PAL];
static unsigned short g_aClockVertOffsetsTXT[VIDEO_SCANNER_MAX_VERT_PAL/8];
static unsigned short APPLE_IIP_HORZ_CLOCK_OFFSET[5][VIDEO_SCANNER_MAX_HORZ]; // 5 = CEILING(312/64) = CEILING(262/64)
static unsigned short APPLE_IIE_HORZ_CLOCK_OFFSET[5][VIDEO_SCANNER_MAX_HORZ];
#ifdef _DEBUG
static unsigned short g_kClockVertOffsetsHGR[ VIDEO_SCANNER_MAX_VERT ] =
{
0x0000,0x0400,0x0800,0x0C00,0x1000,0x1400,0x1800,0x1C00,0x0080,0x0480,0x0880,0x0C80,0x1080,0x1480,0x1880,0x1C80,
0x0100,0x0500,0x0900,0x0D00,0x1100,0x1500,0x1900,0x1D00,0x0180,0x0580,0x0980,0x0D80,0x1180,0x1580,0x1980,0x1D80,
0x0200,0x0600,0x0A00,0x0E00,0x1200,0x1600,0x1A00,0x1E00,0x0280,0x0680,0x0A80,0x0E80,0x1280,0x1680,0x1A80,0x1E80,
0x0300,0x0700,0x0B00,0x0F00,0x1300,0x1700,0x1B00,0x1F00,0x0380,0x0780,0x0B80,0x0F80,0x1380,0x1780,0x1B80,0x1F80,
0x0000,0x0400,0x0800,0x0C00,0x1000,0x1400,0x1800,0x1C00,0x0080,0x0480,0x0880,0x0C80,0x1080,0x1480,0x1880,0x1C80,
0x0100,0x0500,0x0900,0x0D00,0x1100,0x1500,0x1900,0x1D00,0x0180,0x0580,0x0980,0x0D80,0x1180,0x1580,0x1980,0x1D80,
0x0200,0x0600,0x0A00,0x0E00,0x1200,0x1600,0x1A00,0x1E00,0x0280,0x0680,0x0A80,0x0E80,0x1280,0x1680,0x1A80,0x1E80,
0x0300,0x0700,0x0B00,0x0F00,0x1300,0x1700,0x1B00,0x1F00,0x0380,0x0780,0x0B80,0x0F80,0x1380,0x1780,0x1B80,0x1F80,
0x0000,0x0400,0x0800,0x0C00,0x1000,0x1400,0x1800,0x1C00,0x0080,0x0480,0x0880,0x0C80,0x1080,0x1480,0x1880,0x1C80,
0x0100,0x0500,0x0900,0x0D00,0x1100,0x1500,0x1900,0x1D00,0x0180,0x0580,0x0980,0x0D80,0x1180,0x1580,0x1980,0x1D80,
0x0200,0x0600,0x0A00,0x0E00,0x1200,0x1600,0x1A00,0x1E00,0x0280,0x0680,0x0A80,0x0E80,0x1280,0x1680,0x1A80,0x1E80,
0x0300,0x0700,0x0B00,0x0F00,0x1300,0x1700,0x1B00,0x1F00,0x0380,0x0780,0x0B80,0x0F80,0x1380,0x1780,0x1B80,0x1F80,
0x0000,0x0400,0x0800,0x0C00,0x1000,0x1400,0x1800,0x1C00,0x0080,0x0480,0x0880,0x0C80,0x1080,0x1480,0x1880,0x1C80,
0x0100,0x0500,0x0900,0x0D00,0x1100,0x1500,0x1900,0x1D00,0x0180,0x0580,0x0980,0x0D80,0x1180,0x1580,0x1980,0x1D80,
0x0200,0x0600,0x0A00,0x0E00,0x1200,0x1600,0x1A00,0x1E00,0x0280,0x0680,0x0A80,0x0E80,0x1280,0x1680,0x1A80,0x1E80,
0x0300,0x0700,0x0B00,0x0F00,0x1300,0x1700,0x1B00,0x1F00,0x0380,0x0780,0x0B80,0x0F80,0x1380,0x1780,0x1B80,0x1F80,
0x0B80,0x0F80,0x1380,0x1780,0x1B80,0x1F80
};
static unsigned short g_kClockVertOffsetsTXT[33] = // 33 = CEILING(262/8)
{
0x0000,0x0080,0x0100,0x0180,0x0200,0x0280,0x0300,0x0380,
0x0000,0x0080,0x0100,0x0180,0x0200,0x0280,0x0300,0x0380,
0x0000,0x0080,0x0100,0x0180,0x0200,0x0280,0x0300,0x0380,
0x0000,0x0080,0x0100,0x0180,0x0200,0x0280,0x0300,0x0380,
0x380
};
static unsigned short kAPPLE_IIP_HORZ_CLOCK_OFFSET[5][VIDEO_SCANNER_MAX_HORZ] =
{
{0x1068,0x1068,0x1069,0x106A,0x106B,0x106C,0x106D,0x106E,0x106F,
0x1070,0x1071,0x1072,0x1073,0x1074,0x1075,0x1076,0x1077,
0x1078,0x1079,0x107A,0x107B,0x107C,0x107D,0x107E,0x107F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F,
0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027},
{0x1010,0x1010,0x1011,0x1012,0x1013,0x1014,0x1015,0x1016,0x1017,
0x1018,0x1019,0x101A,0x101B,0x101C,0x101D,0x101E,0x101F,
0x1020,0x1021,0x1022,0x1023,0x1024,0x1025,0x1026,0x1027,
0x0028,0x0029,0x002A,0x002B,0x002C,0x002D,0x002E,0x002F,
0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,
0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F,
0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,
0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F},
{0x1038,0x1038,0x1039,0x103A,0x103B,0x103C,0x103D,0x103E,0x103F,
0x1040,0x1041,0x1042,0x1043,0x1044,0x1045,0x1046,0x1047,
0x1048,0x1049,0x104A,0x104B,0x104C,0x104D,0x104E,0x104F,
0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,
0x0058,0x0059,0x005A,0x005B,0x005C,0x005D,0x005E,0x005F,
0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077},
{0x1060,0x1060,0x1061,0x1062,0x1063,0x1064,0x1065,0x1066,0x1067,
0x1068,0x1069,0x106A,0x106B,0x106C,0x106D,0x106E,0x106F,
0x1070,0x1071,0x1072,0x1073,0x1074,0x1075,0x1076,0x1077,
0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F},
{0x1060,0x1060,0x1061,0x1062,0x1063,0x1064,0x1065,0x1066,0x1067,
0x1068,0x1069,0x106A,0x106B,0x106C,0x106D,0x106E,0x106F,
0x1070,0x1071,0x1072,0x1073,0x1074,0x1075,0x1076,0x1077,
0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F}
};
static unsigned short kAPPLE_IIE_HORZ_CLOCK_OFFSET[5][VIDEO_SCANNER_MAX_HORZ] =
{
{0x0068,0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,
0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F,
0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027},
{0x0010,0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F,
0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,
0x0028,0x0029,0x002A,0x002B,0x002C,0x002D,0x002E,0x002F,
0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,
0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F,
0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,
0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F},
{0x0038,0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F,
0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,
0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F,
0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,
0x0058,0x0059,0x005A,0x005B,0x005C,0x005D,0x005E,0x005F,
0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077},
{0x0060,0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,
0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F},
{0x0060,0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,
0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F,
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F}
};
#endif
static csbits_t csbits; // charset, optionally followed by alt charset
// Prototypes
INLINE void updateFramebufferTVSingleScanline( uint16_t signal, bgra_t *pTable );
INLINE void updateFramebufferTVDoubleScanline( uint16_t signal, bgra_t *pTable );
INLINE void updateFramebufferMonitorSingleScanline( uint16_t signal, bgra_t *pTable );
INLINE void updateFramebufferMonitorDoubleScanline( uint16_t signal, bgra_t *pTable );
INLINE void updatePixels( uint16_t bits );
INLINE void updateVideoScannerHorzEOL();
INLINE void updateVideoScannerAddress();
INLINE uint16_t getVideoScannerAddressTXT();
INLINE uint16_t getVideoScannerAddressHGR();
static void initChromaPhaseTables();
static real initFilterChroma (real z);
static real initFilterLuma0 (real z);
static real initFilterLuma1 (real z);
static real initFilterSignal(real z);
static void initPixelDoubleMasks(void);
static void updateMonochromeTables( uint16_t r, uint16_t g, uint16_t b );
static void updatePixelBnWColorTVSingleScanline( uint16_t compositeSignal );
static void updatePixelBnWColorTVDoubleScanline( uint16_t compositeSignal );
static void updatePixelBnWMonitorSingleScanline( uint16_t compositeSignal );
static void updatePixelBnWMonitorDoubleScanline( uint16_t compositeSignal );
static void updatePixelHueColorTVSingleScanline( uint16_t compositeSignal );
static void updatePixelHueColorTVDoubleScanline( uint16_t compositeSignal );
static void updatePixelHueMonitorSingleScanline( uint16_t compositeSignal );
static void updatePixelHueMonitorDoubleScanline( uint16_t compositeSignal );
static void updateScreenDoubleHires40( long cycles6502 );
static void updateScreenDoubleHires80( long cycles6502 );
static void updateScreenDoubleLores40( long cycles6502 );
static void updateScreenDoubleLores80( long cycles6502 );
static void updateScreenSingleHires40( long cycles6502 );
static void updateScreenSingleLores40( long cycles6502 );
static void updateScreenText40 ( long cycles6502 );
static void updateScreenText80 ( long cycles6502 );
static void updateScreenText40RGB ( long cycles6502 );
static void updateScreenText80RGB ( long cycles6502 );
static void updateScreenDoubleHires80Simplified(long cycles6502);
static void updateScreenDoubleHires80RGB(long cycles6502);
static void updateScreenSHR(long cycles6502);
//===========================================================================
static void set_csbits()
{
// NB. For models that don't have an alt charset then set /g_nVideoCharSet/ to zero
switch ( GetApple2Type() )
{
case A2TYPE_APPLE2: csbits = &csbits_a2[0]; g_nVideoCharSet = 0; break;
case A2TYPE_APPLE2PLUS: csbits = &csbits_a2[0]; g_nVideoCharSet = 0; break;
case A2TYPE_APPLE2JPLUS: csbits = &csbits_a2j[MemGetAnnunciator(2) ? 1 : 0]; g_nVideoCharSet = 0; break;
case A2TYPE_APPLE2E: csbits = Get2e_csbits(); break;
case A2TYPE_APPLE2EENHANCED:csbits = Get2e_csbits(); break;
case A2TYPE_PRAVETS82: csbits = &csbits_pravets82[0]; g_nVideoCharSet = 0; break; // Apple ][ clone
case A2TYPE_PRAVETS8M: csbits = &csbits_pravets8M[0]; g_nVideoCharSet = 0; break; // Apple ][ clone
case A2TYPE_PRAVETS8A: csbits = &csbits_pravets8C[0]; break; // Apple //e clone
case A2TYPE_TK30002E: csbits = &csbits_enhanced2e[0]; break; // Enhanced Apple //e clone
case A2TYPE_BASE64A: csbits = &csbits_base64a[GetVideo().GetVideoRomRockerSwitch() ? 0 : 1]; g_nVideoCharSet = 0; break; // Apple ][ clone
default: _ASSERT(0); csbits = &csbits_enhanced2e[0]; break;
}
}
//===========================================================================
inline float clampZeroOne( const float & x )
{
if (x < 0.f) return 0.f;
if (x > 1.f) return 1.f;
/* ...... */ return x;
}
//===========================================================================
inline uint8_t getCharSetBits(int iChar)
{
return csbits[g_nVideoCharSet][iChar][g_nVideoClockVert & 7];
}
//===========================================================================
inline uint16_t getLoResBits( uint8_t iByte )
{
return g_aPixelMaskGR[ (iByte >> (g_nVideoClockVert & 4)) & 0xF ];
}
//===========================================================================
inline uint32_t getScanlineColor( const uint16_t signal, const bgra_t *pTable )
{
g_nSignalBitsNTSC = ((g_nSignalBitsNTSC << 1) | signal) & 0xFFF; // 12-bit
return *(uint32_t*) &pTable[ g_nSignalBitsNTSC ];
}
//===========================================================================
inline uint32_t* getScanlineNextInbetween()
{
return (uint32_t*) (g_pVideoAddress - 1*g_kFrameBufferWidth);
}
#if 0 // don't use this pixel, as it's from the previous video-frame!
inline uint32_t* getScanlineNext()
{
return (uint32_t*) (g_pVideoAddress - 2*g_kFrameBufferWidth);
}
#endif
//===========================================================================
inline uint32_t* getScanlinePreviousInbetween()
{
return (uint32_t*) (g_pVideoAddress + 1*g_kFrameBufferWidth);
}
inline uint32_t* getScanlinePrevious()
{
return (uint32_t*) (g_pVideoAddress + 2*g_kFrameBufferWidth);
}
//===========================================================================
inline uint32_t* getScanlineCurrent()
{
return (uint32_t*) g_pVideoAddress;
}
//===========================================================================
inline void updateColorPhase()
{
g_nColorPhaseNTSC++;
g_nColorPhaseNTSC &= 3;
}
//===========================================================================
inline void updateFlashRate() // TODO: Flash rate should be constant (regardless of CPU speed)
{
// BUG: In unthrottled CPU mode, flash rate should not be affected
// Flash rate:
// . NTSC : 60/16 ~= 4Hz
// . PAL : 50/16 ~= 3Hz
if ((++g_nTextFlashCounter & 0xF) == 0)
g_nTextFlashMask ^= -1; // 16-bits
// The old way to handle flashing was
// if ((SW_TEXT || SW_MIXED) ) // && !SW_80COL) // FIX: FLASH 80-Column
// g_nTextFlashMask = true;
// The new way is to check the active char set, inlined:
// if (0 == g_nVideoCharSet && 0x40 == (m & 0xC0)) // Flash only if mousetext not active
}
#if 0
#define updateFramebufferMonitorSingleScanline(signal,table) \
do { \
uint32_t *cp, *mp; \
g_nSignalBitsNTSC = ((g_nSignalBitsNTSC << 1) | signal) & 0xFFF; \
cp = (uint32_t*) &table[g_nSignalBitsNTSC]; \
*(uint32_t*)g_pVideoAddress = *cp; \
mp = (uint32_t*)(g_pVideoAddress - FRAMEBUFFER_W); \
*mp = ((*cp & 0x00fcfcfc) >> 2) | ALPHA32_MASK; \
g_pVideoAddress++; \
} while(0)
// prevp is never used nor blended with!
#define updateFramebufferTVSingleScanline(signal,table) \
do { \
uint32_t ntscp, /*prevp,*/ betwp; \
uint32_t *prevlin, *between; \
g_nSignalBitsNTSC = ((g_nSignalBitsNTSC << 1) | signal) & 0xFFF; \
/*prevlin = (uint32_t*)(g_pVideoAddress + 2*FRAMEBUFFER_W);*/ \
between = (uint32_t*)(g_pVideoAddress + 1*FRAMEBUFFER_W); \
ntscp = *(uint32_t*) &table[g_nSignalBitsNTSC]; /* raw current NTSC color */ \
/*prevp = *prevlin;*/ \
betwp = ntscp - ((ntscp & 0x00fcfcfc) >> 2); \
*between = betwp | ALPHA32_MASK; \
*(uint32_t*)g_pVideoAddress = ntscp; \
g_pVideoAddress++; \
} while(0)
#define updateFramebufferMonitorDoubleScanline(signal,table) \
do { \
uint32_t *cp, *mp; \
g_nSignalBitsNTSC = ((g_nSignalBitsNTSC << 1) | signal) & 0xFFF; \
cp = (uint32_t*) &table[g_nSignalBitsNTSC]; \
mp = (uint32_t*)(g_pVideoAddress - FRAMEBUFFER_W); \
*(uint32_t*)g_pVideoAddress = *mp = *cp; \
g_pVideoAddress++; \
} while(0)
#define updateFramebufferTVDoubleScanline(signal,table) \
do { \
uint32_t ntscp, prevp, betwp; \
uint32_t *prevlin, *between; \
g_nSignalBitsNTSC = ((g_nSignalBitsNTSC << 1) | signal) & 0xFFF; \
prevlin = (uint32_t*)(g_pVideoAddress + 2*FRAMEBUFFER_W); \
between = (uint32_t*)(g_pVideoAddress + 1*FRAMEBUFFER_W); \
ntscp = *(uint32_t*) &table[g_nSignalBitsNTSC]; /* raw current NTSC color */ \
prevp = *prevlin; \
betwp = ((ntscp & 0x00fefefe) >> 1) + ((prevp & 0x00fefefe) >> 1); \
*between = betwp | ALPHA32_MASK; \
*(uint32_t*)g_pVideoAddress = ntscp; \
g_pVideoAddress++; \
} while(0)
#else
//===========================================================================
// Original: Prev1(inbetween) = current - 25% of previous AppleII scanline
// GH#650: Prev1(inbetween) = 50% of (50% current + 50% of previous AppleII scanline)
inline void updateFramebufferTVSingleScanline( uint16_t signal, bgra_t *pTable )
{
uint32_t *pLine0Curr = getScanlineCurrent();
uint32_t *pLine1Prev = getScanlinePreviousInbetween();
uint32_t *pLine2Prev = getScanlinePrevious();
const uint32_t color0 = getScanlineColor( signal, pTable );
const uint32_t color2 = *pLine2Prev;
uint32_t color1 = ((color0 & 0x00fefefe) >> 1) + ((color2 & 0x00fefefe) >> 1); // 50% Blend
color1 = (color1 & 0x00fefefe) >> 1; // ... then 50% brightness for inbetween line
*pLine1Prev = color1 | ALPHA32_MASK;
*pLine0Curr = color0;
// GH#650: Draw to final inbetween scanline to avoid residue from other video modes (eg. Amber->TV B&W)
if (g_nVideoClockVert == (VIDEO_SCANNER_Y_DISPLAY-1))
*getScanlineNextInbetween() = ((color0 & 0x00fcfcfc) >> 2) | ALPHA32_MASK; // 50% of (50% current + black)) = 25% of current
g_pVideoAddress++;
}
//===========================================================================
// Original: Prev1(inbetween) = 50% current + 50% of previous AppleII scanline
inline void updateFramebufferTVDoubleScanline( uint16_t signal, bgra_t *pTable )
{
uint32_t *pLine0Curr = getScanlineCurrent();
uint32_t *pLine1Prev = getScanlinePreviousInbetween();
uint32_t *pLine2Prev = getScanlinePrevious();
const uint32_t color0 = getScanlineColor( signal, pTable );
const uint32_t color2 = *pLine2Prev;
const uint32_t color1 = ((color0 & 0x00fefefe) >> 1) + ((color2 & 0x00fefefe) >> 1); // 50% Blend
*pLine1Prev = color1 | ALPHA32_MASK;
*pLine0Curr = color0;
// GH#650: Draw to final inbetween scanline to avoid residue from other video modes (eg. Amber->TV B&W)
if (g_nVideoClockVert == (VIDEO_SCANNER_Y_DISPLAY-1))
*getScanlineNextInbetween() = ((color0 & 0x00fefefe) >> 1) | ALPHA32_MASK; // (50% current + black)) = 50% of current
g_pVideoAddress++;
}
//===========================================================================
inline void updateFramebufferMonitorSingleScanline( uint16_t signal, bgra_t *pTable )
{
uint32_t *pLine0Curr = getScanlineCurrent();
uint32_t *pLine1Next = getScanlineNextInbetween();
const uint32_t color0 = getScanlineColor( signal, pTable );
const uint32_t color1 = 0; // Remove blending for consistent DHGR MIX mode (GH#631)
// const uint32_t color1 = ((color0 & 0x00fcfcfc) >> 2); // 25% Blend (original)
*pLine1Next = color1 | ALPHA32_MASK;
*pLine0Curr = color0;
g_pVideoAddress++;
}
//===========================================================================
inline void updateFramebufferMonitorDoubleScanline( uint16_t signal, bgra_t *pTable )
{
uint32_t *pLine0Curr = getScanlineCurrent();
uint32_t *pLine1Next = getScanlineNextInbetween();
const uint32_t color0 = getScanlineColor( signal, pTable );
*pLine1Next = color0;
*pLine0Curr = color0;
g_pVideoAddress++;
}
#endif
//===========================================================================
inline bool GetColorBurst( void )
{
return g_nColorBurstPixels >= 2;
}
//===========================================================================
void update7MonoPixels( uint16_t bits )
{
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1);
}
//===========================================================================
// NB. g_nLastColumnPixelNTSC = bits.b13 will be superseded by these parent funcs which use bits.b14:
// . updateScreenDoubleHires80(), updateScreenDoubleLores80(), updateScreenText80()
inline void updatePixels(uint16_t bits)
{
if (!GetColorBurst())
{
/* #1 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #2 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #3 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #4 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #5 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #6 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
/* #7 of 7 */
g_pFuncUpdateBnWPixel(bits & 1); bits >>= 1;
g_pFuncUpdateBnWPixel(bits & 1);
g_nLastColumnPixelNTSC = bits & 1;
}
else
{
/* #1 of 7 */ // abcd efgh ijkl mnop
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0abc defg hijk lmno
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 00ab cdef ghi jklmn
/* #2 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 000a bcde fghi jklm
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 abcd efgh ijkl
/* #3 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0abc defg hijk
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 00ab cdef ghij
/* #4 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 000a bcde fghi
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 abcd efgh
/* #5 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 0abc defg
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 00ab cdef
/* #6 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 000a bcde
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 0000 abcd
/* #7 of 7 */
g_pFuncUpdateHuePixel(bits & 1); bits >>= 1; // 0000 0000 0000 0abc
g_pFuncUpdateHuePixel(bits & 1);
g_nLastColumnPixelNTSC = bits & 1;
}
}
//===========================================================================
inline void updateVideoScannerHorzEOLSimple()
{
if (VIDEO_SCANNER_MAX_HORZ == ++g_nVideoClockHorz)
{
g_nVideoClockHorz = 0;
if (++g_nVideoClockVert == g_videoScannerMaxVert)
{
g_nVideoClockVert = 0;
updateFlashRate();
}
if (g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY)
{
updateVideoScannerAddress();
}
}
}
// NOTE: This writes out-of-bounds for a 560x384 framebuffer
inline void updateVideoScannerHorzEOL()
{
if (VIDEO_SCANNER_MAX_HORZ == ++g_nVideoClockHorz)
{
if (g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY)
{
if (!GetColorBurst())
{
// Only for: VF_TEXT && !VF_MIXED (ie. full 24-row TEXT40 or TEXT80)
g_pFuncUpdateBnWPixel(g_nLastColumnPixelNTSC);
g_pFuncUpdateBnWPixel(0);
g_pFuncUpdateBnWPixel(0);
}
else
{
g_pFuncUpdateHuePixel(g_nLastColumnPixelNTSC);
g_pFuncUpdateHuePixel(0);
g_pFuncUpdateHuePixel(0);
}
}
g_nVideoClockHorz = 0;
if (++g_nVideoClockVert == g_videoScannerMaxVert)
{
g_nVideoClockVert = 0;
updateFlashRate();
}
if (g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY)
{
updateVideoScannerAddress();
}
}
}
inline void updateVideoScannerHorzEOL_SHR()
{
if (VIDEO_SCANNER_MAX_HORZ == ++g_nVideoClockHorz)
{
g_nVideoClockHorz = 0;
if (++g_nVideoClockVert == g_videoScannerMaxVert)
{
g_nVideoClockVert = 0;
}
if (g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY_IIGS)
{
updateVideoScannerAddress();
}
}
}
//===========================================================================
inline void updateVideoScannerAddress()
{
if (g_nVideoMixed && g_nVideoClockVert >= VIDEO_SCANNER_Y_MIXED && GetVideo().GetVideoRefreshRate() == VR_50HZ) // GH#763
g_nColorBurstPixels = 0; // instantaneously kill color-burst!
if (g_pFuncUpdateGraphicsScreen == updateScreenSHR)
{
g_pVideoAddress = g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY_IIGS ? g_pScanLines[2 * g_nVideoClockVert] : g_pScanLines[0];
return;
}
g_pVideoAddress = g_nVideoClockVert < VIDEO_SCANNER_Y_DISPLAY ? g_pScanLines[2 * g_nVideoClockVert] : g_pScanLines[0];
// Adjust, as these video styles have 2x 14M pixels of pre-render
// NB. For VT_COLOR_MONITOR_NTSC, also check color-burst so that TEXT and MIXED(HGR+TEXT) render the TEXT at the same offset (GH#341)
if (GetVideo().GetVideoType() == VT_MONO_TV || GetVideo().GetVideoType() == VT_COLOR_TV || (GetVideo().GetVideoType() == VT_COLOR_MONITOR_NTSC && GetColorBurst()))
g_pVideoAddress -= 2;
// GH#555: For the 14M video modes (DHGR,DGR,80COL), start rendering 1x 14M pixel early to account for these video modes being shifted right by 1 pixel
// NB. This 1 pixel shift right is a workaround for the 14M video modes that actually start 7x 14M pixels to the left on *real h/w*.
// . 7x 14M pixels early + 1x 14M pixel shifted right = 2 complete color phase rotations.
// . ie. the 14M colors are correct, but being 1 pixel out is the closest we can get the 7M and 14M video modes to overlap.
// . The alternative is to render the 14M correctly 7 pixels early, but have 7-pixel borders left (for 7M modes) or right (for 14M modes).
if (((g_pFuncUpdateGraphicsScreen == updateScreenDoubleHires80) ||
(g_pFuncUpdateGraphicsScreen == updateScreenDoubleLores80) ||
(g_pFuncUpdateGraphicsScreen == updateScreenText80) ||
(g_pFuncUpdateGraphicsScreen == updateScreenText80RGB) ||
(g_nVideoMixed && g_nVideoClockVert >= VIDEO_SCANNER_Y_MIXED && (g_pFuncUpdateTextScreen == updateScreenText80 || g_pFuncUpdateGraphicsScreen == updateScreenText80RGB)))
&& (GetVideo().GetVideoType() != VT_COLOR_IDEALIZED) && (GetVideo().GetVideoType() != VT_COLOR_VIDEOCARD_RGB)) // Fix for "Ansi Story" (Turn the disk over) - Top row of TEXT80 is shifted by 1 pixel
{
g_pVideoAddress -= 1;
}
// Centre the older //e video modes when running with a VidHD
g_pVideoAddress += GetVideo().GetFrameBufferCentringValue();
g_nColorPhaseNTSC = INITIAL_COLOR_PHASE;
g_nLastColumnPixelNTSC = 0;
g_nSignalBitsNTSC = 0;
}
//===========================================================================
INLINE uint16_t getVideoScannerAddressTXT()
{
uint16_t nAddress = (g_aClockVertOffsetsTXT[g_nVideoClockVert/8]
+ g_pHorzClockOffset [g_nVideoClockVert/64][g_nVideoClockHorz]
+ (g_nTextPage * 0x400));
return nAddress;
}
//===========================================================================
INLINE uint16_t getVideoScannerAddressHGR()
{
// NB. For both A2 and //e use APPLE_IIE_HORZ_CLOCK_OFFSET - see VideoGetScannerAddress() where only TEXT mode adds $1000
uint16_t nAddress = (g_aClockVertOffsetsHGR[g_nVideoClockVert ]
+ APPLE_IIE_HORZ_CLOCK_OFFSET[g_nVideoClockVert/64][g_nVideoClockHorz]
+ (g_nHiresPage * 0x2000));
return nAddress;
}
// Non-Inline _________________________________________________________
// Build the 4 phase chroma lookup table
// The YI'Q' colors are hard-coded
//===========================================================================
static void initChromaPhaseTables (void)
{
int phase,s,t,n;
real z,y0,y1,c,i,q;
real phi,zz;
float brightness;
double r64,g64,b64;
float r32,g32,b32;
for (phase = 0; phase < 4; ++phase)
{
phi = (phase * RAD_90) + CYCLESTART;
for (s = 0; s < NTSC_NUM_SEQUENCES; ++s)
{
t = s;
y0 = y1 = c = i = q = 0.0;
for (n = 0; n < 12; ++n)
{
z = (real)(0 != (t & 0x800));
t = t << 1;
for(int k = 0; k < 2; k++ )
{
//z = z * 1.25;
zz = initFilterSignal(z);
c = initFilterChroma(zz); // "Mostly" correct _if_ CYCLESTART = PI/4 = 45 degrees
y0 = initFilterLuma0 (zz);
y1 = initFilterLuma1 (zz - c);
c = c * 2.f;
i = i + (c * cos(phi) - i) / 8.f;
q = q + (c * sin(phi) - q) / 8.f;
phi += RAD_45;
} // k
} // samples
brightness = clampZeroOne( (float)z );
g_aBnWMonitor[s].b = (uint8_t)(brightness * 255);
g_aBnWMonitor[s].g = (uint8_t)(brightness * 255);
g_aBnWMonitor[s].r = (uint8_t)(brightness * 255);
g_aBnWMonitor[s].a = 255;
brightness = clampZeroOne( (float)y1);
g_aBnwColorTV[s].b = (uint8_t)(brightness * 255);
g_aBnwColorTV[s].g = (uint8_t)(brightness * 255);
g_aBnwColorTV[s].r = (uint8_t)(brightness * 255);
g_aBnwColorTV[s].a = 255;
/*
YI'V' to RGB
[r g b] = [y i v][ 1 1 1 ]
[0.956 -0.272 -1.105]
[0.621 -0.647 1.702]
[r] [1 0.956 0.621][y]
[g] = [1 -0.272 -0.647][i]
[b] [1 -1.105 1.702][v]
*/
#define I_TO_R 0.956f
#define I_TO_G -0.272f
#define I_TO_B -1.105f
#define Q_TO_R 0.621f
#define Q_TO_G -0.647f
#define Q_TO_B 1.702f
r64 = y0 + (I_TO_R * i) + (Q_TO_R * q);
g64 = y0 + (I_TO_G * i) + (Q_TO_G * q);
b64 = y0 + (I_TO_B * i) + (Q_TO_B * q);
b32 = clampZeroOne( (float)b64);
g32 = clampZeroOne( (float)g64);
r32 = clampZeroOne( (float)r64);
int color = s & 15;
#if NTSC_REMOVE_WHITE_RINGING
if( color == 15 ) // white
{
r32 = 1;
g32 = 1;
b32 = 1;
}
#endif
#if NTSC_REMOVE_BLACK_GHOSTING
if( color == 0 ) // Black
{
r32 = 0;
g32 = 0;
b32 = 0;
}
#endif
#if NTSC_REMOVE_GRAY_CHROMA
if( color == 5 ) // Gray1 & Gray2
{
const float g = (float) 0x83 / (float) 0xFF;
r32 = g;
g32 = g;
b32 = g;
}
if( color == 10 ) // Gray2 & Gray1
{
const float g = (float) 0x78 / (float) 0xFF;
r32 = g;
g32 = g;
b32 = g;
}
#endif
g_aHueMonitor[phase][s].b = (uint8_t)(b32 * 255);
g_aHueMonitor[phase][s].g = (uint8_t)(g32 * 255);
g_aHueMonitor[phase][s].r = (uint8_t)(r32 * 255);
g_aHueMonitor[phase][s].a = 255;
r64 = y1 + (I_TO_R * i) + (Q_TO_R * q);
g64 = y1 + (I_TO_G * i) + (Q_TO_G * q);
b64 = y1 + (I_TO_B * i) + (Q_TO_B * q);
b32 = clampZeroOne( (float)b64 );
g32 = clampZeroOne( (float)g64 );
r32 = clampZeroOne( (float)r64 );
#if NTSC_REMOVE_WHITE_RINGING
if( color == 15 ) // white
{
r32 = 1;
g32 = 1;
b32 = 1;
}
#endif
#if NTSC_REMOVE_BLACK_GHOSTING
if( color == 0 ) // Black
{
r32 = 0;
g32 = 0;
b32 = 0;
}
#endif
g_aHueColorTV[phase][s].b = (uint8_t)(b32 * 255);
g_aHueColorTV[phase][s].g = (uint8_t)(g32 * 255);
g_aHueColorTV[phase][s].r = (uint8_t)(r32 * 255);
g_aHueColorTV[phase][s].a = 255;
}
}
#if DEBUG_PHASE_ZERO
uint8_t *p = (uint8_t*)g_aHueMonitor;
*p++ = 0xFF;
*p++ = 0x00;
*p++ = 0x00;
*p++ = 0xFF;
#endif
}
/*
http://www-users.cs.york.ac.uk/~fisher/mkfilter/trad.html
Sample Rate: ???
Corner Freq 1: ?
Corner Freq 2: ?
double ButterworthLowPass2( double a, double b, double g, double z )
{
const int POLES=2;
static double x[POLES+1];
static double y[POLES+1];
for( int iPole = 0; iPole < POLES; iPole++ )
{
x[iPole] = x[iPole+1];
y[iPole] = y[iPole+1];
}
x[POLES] = z / g;
y[POLES] = x[0] + x[2] + (2.f*x[1]) + (a*y[0]) + (b*y[1]);
return y[2];
}
*/
// What filter is this ??
// Filter Order: 2 -> poles for low pass
//===========================================================================
static real initFilterChroma (real z)
{
static real x[CHROMA_ZEROS + 1] = {0,0,0};
static real y[CHROMA_POLES + 1] = {0,0,0};