-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgbs-control-atmega.ino
2875 lines (2585 loc) · 88.2 KB
/
gbs-control-atmega.ino
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
#include <Wire.h>
#include <EEPROM.h>
#include "minimal_startup.h"
#include "rgbhv.h"
//#include "ypbpr_1080i.h"
#include "ofw_ypbpr.h"
//#include "ntsc_240p.h"
//#include "pal_240p.h"
#include "pal_widescreen.h"
#include "pal_fullscreen.h"
#include "pal_feedbackclock.h"
#include "ntsc_widescreen.h"
#include "ntsc_fullscreen.h"
#include "ntsc_feedbackclock.h"
#define LEDON digitalWrite(LED_BUILTIN, HIGH)
#define LEDOFF digitalWrite(LED_BUILTIN, LOW)
#define vsyncInPin 10
#define BUTTON1 2
#define BUTTON2 3
#define BUTTON3 4
#define SWITCH1 8
#define GBS_ADDR 0x17
//#define REGISTER_DUMP
// runTimeOptions holds system variables
struct runTimeOptions {
boolean inputIsYpBpR;
boolean syncWatcher;
uint8_t videoStandardInput : 4; // 0 - unknown, 1 - NTSC like, 2 - PAL like, 3 480p NTSC, 4 576p PAL
uint8_t phaseSP;
uint8_t phaseADC;
uint8_t samplingStart;
uint8_t currentLevelSOG;
boolean deinterlacerWasTurnedOff;
boolean modeDetectInReset;
boolean syncLockEnabled;
boolean syncLockFound;
boolean VSYNCconnected;
boolean IFdown; // push button support example using an interrupt
boolean printInfos;
boolean sourceDisconnected;
} rtos;
struct runTimeOptions *rto = &rtos;
// userOptions holds user preferences / customizations
struct userOptions {
uint8_t presetPreference; // 0 - normal, 1 - feedback clock, 2 - customized
} uopts;
struct userOptions *uopt = &uopts;
uint8_t imageFunctionToggle = 0;
bool widescreenSwitchEnabled = false;
char globalCommand;
void nopdelay(unsigned int times) {
while (times-- > 0)
__asm__("nop\n\t");
}
void writeOneByte(uint8_t slaveRegister, uint8_t value) {
writeBytes(slaveRegister, &value, 1);
}
void writeBytes(uint8_t slaveAddress, uint8_t slaveRegister, uint8_t* values, uint8_t numValues) {
Wire.beginTransmission(slaveAddress);
Wire.write(slaveRegister);
int sentBytes = Wire.write(values, numValues);
Wire.endTransmission();
if (sentBytes != numValues) {
Serial.print(F("i2c error\n"));
}
}
void writeBytes(uint8_t slaveRegister, uint8_t* values, int numValues) {
writeBytes(GBS_ADDR, slaveRegister, values, numValues);
}
void writeProgramArray(const uint8_t* programArray) {
for (int y = 0; y < 6; y++) {
writeOneByte(0xF0, (uint8_t)y );
for (int z = 0; z < 16; z++) {
uint8_t bank[16];
for (int w = 0; w < 16; w++) {
bank[w] = pgm_read_byte(programArray + (y * 256 + z * 16 + w));
}
writeBytes(z * 16, bank, 16);
}
}
}
void writeProgramArrayNew(const uint8_t* programArray) {
int index = 0;
uint8_t bank[16];
// programs all valid registers (the register map has holes in it, so it's not straight forward)
// 'index' keeps track of the current preset data location.
writeOneByte(0xF0, 0);
writeOneByte(0x46, 0x00); // reset controls 1
writeOneByte(0x47, 0x00); // reset controls 2
// 498 = s5_12, 499 = s5_13
writeOneByte(0xF0, 5);
writeOneByte(0x11, 0x11); // Initial VCO control voltage
writeOneByte(0x13, getSingleByteFromPreset(programArray, 499)); // load PLLAD divider high bits first (tvp7002 manual)
writeOneByte(0x12, getSingleByteFromPreset(programArray, 498));
writeOneByte(0x16, getSingleByteFromPreset(programArray, 502)); // might as well
writeOneByte(0x17, getSingleByteFromPreset(programArray, 503)); // charge pump current
writeOneByte(0x18, 0); writeOneByte(0x19, 0); // adc / sp phase reset
for (int y = 0; y < 6; y++) {
writeOneByte(0xF0, (uint8_t)y );
switch (y) {
case 0:
for (int j = 0; j <= 1; j++) { // 2 times
for (int x = 0; x <= 15; x++) {
// reset controls are at 0x46, 0x47
if (j == 0 && (x == 6 || x == 7)) {
// keep reset controls active
bank[x] = 0;
}
else {
// use preset values
bank[x] = pgm_read_byte(programArray + index);
}
index++;
}
writeBytes(0x40 + (j * 16), bank, 16);
}
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(0x90, bank, 16);
break;
case 1:
for (int j = 0; j <= 8; j++) { // 9 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
break;
case 2:
for (int j = 0; j <= 3; j++) { // 4 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
break;
case 3:
for (int j = 0; j <= 7; j++) { // 8 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
break;
case 4:
for (int j = 0; j <= 5; j++) { // 6 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
break;
case 5:
for (int j = 0; j <= 6; j++) { // 7 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
if (index == 482) { // s5_02 bit 6+7 = input selector (only bit 6 is relevant)
if (rto->inputIsYpBpR)bitClear(bank[x], 6);
else bitSet(bank[x], 6);
}
index++;
}
writeBytes(j * 16, bank, 16);
}
break;
}
}
setParametersSP();
writeOneByte(0xF0, 1);
writeOneByte(0x60, 0x81); // MD H unlock / lock
writeOneByte(0x61, 0x81); // MD V unlock / lock
writeOneByte(0x80, 0xa9); // MD V nonsensical custom mode
writeOneByte(0x81, 0x2e); // MD H nonsensical custom mode
writeOneByte(0x82, 0x35); // MD H / V timer detect enable, auto detect enable
writeOneByte(0x83, 0x10); // MD H / V unstable estimation lock value medium
//update rto phase variables
uint8_t readout = 0;
writeOneByte(0xF0, 5);
readFromRegister(0x18, 1, &readout);
rto->phaseADC = ((readout & 0x3e) >> 1);
readFromRegister(0x19, 1, &readout);
rto->phaseSP = ((readout & 0x3e) >> 1);
Serial.print(rto->phaseADC);
Serial.print("\n");
Serial.print(rto->phaseSP);
Serial.print("\n");
//reset rto sampling start variable
//writeOneByte(0xF0, 1);
//readFromRegister(0x26, 1, &readout);
//rto->samplingStart = readout;
//Serial.print(rto->samplingStart);
//Serial.print("\n");
writeOneByte(0xF0, 0);
writeOneByte(0x46, 0x3f); // reset controls 1 // everything on except VDS display output
writeOneByte(0x47, 0x17); // all on except HD bypass
}
void writeProgramArraySection(const uint8_t* programArray, byte section, byte subsection = 0) {
// section 1: index = 48
uint8_t bank[16];
int index = 0;
if (section == 0) {
index = 0;
writeOneByte(0xF0, 0);
for (int j = 0; j <= 1; j++) { // 2 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(0x40 + (j * 16), bank, 16);
}
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(0x90, bank, 16);
}
if (section == 1) {
index = 48;
writeOneByte(0xF0, 1);
for (int j = 0; j <= 8; j++) { // 9 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
}
if (section == 2) {
index = 192;
writeOneByte(0xF0, 2);
for (int j = 0; j <= 3; j++) { // 4 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
}
if (section == 3) {
index = 256;
writeOneByte(0xF0, 3);
for (int j = 0; j <= 7; j++) { // 8 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
}
if (section == 4) {
index = 384;
writeOneByte(0xF0, 4);
for (int j = 0; j <= 5; j++) { // 6 times
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
index++;
}
writeBytes(j * 16, bank, 16);
}
}
if (section == 5) {
index = 480;
int j = 0;
if (subsection == 1) {
index = 512;
j = 2;
}
writeOneByte(0xF0, 5);
for (; j <= 6; j++) {
for (int x = 0; x <= 15; x++) {
bank[x] = pgm_read_byte(programArray + index);
if (index == 482) { // s5_02 bit 6+7 = input selector (only 6 is relevant)
if (rto->inputIsYpBpR)bitClear(bank[x], 6);
else bitSet(bank[x], 6);
}
index++;
}
writeBytes(j * 16, bank, 16);
}
resetPLL(); resetPLLAD();// only for section 5
}
}
void fuzzySPWrite() {
writeOneByte(0xF0, 5);
for (uint8_t reg = 0x21; reg <= 0x34; reg++) {
writeOneByte(reg, random(0x00, 0xFF));
}
}
void setParametersSP() {
writeOneByte(0xF0, 5);
if (rto->videoStandardInput == 3) {
writeOneByte(0x00, 0xd0);
writeOneByte(0x16, 0x1f);
writeOneByte(0x37, 0x04); // need to work on this
writeOneByte(0x3b, 0x11);
writeOneByte(0x3f, 0x1b);
writeOneByte(0x40, 0x20);
writeOneByte(0x50, 0x00);
}
else if (rto->videoStandardInput == 4) {
writeOneByte(0x00, 0xd0);
writeOneByte(0x16, 0x1f);
writeOneByte(0x37, 0x04);
writeOneByte(0x3b, 0x11);
writeOneByte(0x3f, 0x58);
writeOneByte(0x40, 0x5b);
writeOneByte(0x50, 0x00);
}
else {
writeOneByte(0x37, 0x58); // need to work on this
}
writeOneByte(0x20, 0x12); // was 0xd2 // keep jitter sync on! (snes, check debug vsync)(auto correct sog polarity, sog source = ADC)
// H active detect control
writeOneByte(0x21, 0x1b); // SP_SYNC_TGL_THD H Sync toggle times threshold 0x20
writeOneByte(0x22, 0x0f); // SP_L_DLT_REG Sync pulse width different threshold (little than this as equal).
writeOneByte(0x24, 0x40); // SP_T_DLT_REG H total width different threshold rgbhv: b // try reducing to 0x0b again
writeOneByte(0x25, 0x00); // SP_T_DLT_REG
writeOneByte(0x26, 0x05); // SP_SYNC_PD_THD H sync pulse width threshold // try increasing to ~ 0x50
writeOneByte(0x27, 0x00); // SP_SYNC_PD_THD
writeOneByte(0x2a, 0x0f); // SP_PRD_EQ_THD How many continue legal line as valid
// V active detect control
writeOneByte(0x2d, 0x04); // SP_VSYNC_TGL_THD V sync toggle times threshold
writeOneByte(0x2e, 0x04); // SP_SYNC_WIDTH_DTHD V sync pulse width threshod // the 04 is a test
writeOneByte(0x2f, 0x04); // SP_V_PRD_EQ_THD How many continue legal v sync as valid 0x04
writeOneByte(0x31, 0x2f); // SP_VT_DLT_REG V total different threshold
// Timer value control
writeOneByte(0x33, 0x28); // SP_H_TIMER_VAL H timer value for h detect (hpw 148 typical, need a little slack > 160/4 = 40 (0x28)) (was 0x28)
writeOneByte(0x34, 0x03); // SP_V_TIMER_VAL V timer for V detect (?typical vtotal: 259. times 2 for 518. ntsc 525 - 518 = 7. so 0x08?)
// Sync separation control
writeOneByte(0x35, 0xb0); // SP_DLT_REG [7:0] Sync pulse width difference threshold (tweak point) (b0 seems best from experiments. above, no difference)
writeOneByte(0x36, 0x00); // SP_DLT_REG [11:8]
writeOneByte(0x38, 0x07); // h coast pre (psx starts eq pulses around 4 hsyncs before vs pulse) rgbhv: 7
writeOneByte(0x39, 0x03); // h coast post (psx stops eq pulses around 4 hsyncs after vs pulse) rgbhv: 12
// note: the pre / post lines number probably depends on the vsync pulse delay, ie: sync stripper vsync delay
writeOneByte(0x3a, 0x0a); // 0x0a rgbhv: 20
//writeOneByte(0x3f, 0x03); // 0x03
//writeOneByte(0x40, 0x0b); // 0x0b
//writeOneByte(0x3e, 0x00); // problems with snes 239 line mode, use 0x00 0xc0 rgbhv: f0
// clamp position
// in RGB mode, should use sync tip clamping: s5s41s80 s5s43s90 s5s42s06 s5s44s06
// in YUV mode, should use back porch clamping: s5s41s70 s5s43s98 s5s42s00 s5s44s00
// tip: see clamp pulse in RGB signal with clamp start > clamp end (scope trigger on sync in, show one of the RGB lines)
writeOneByte(0x41, 0x19); writeOneByte(0x43, 0x27); // 0x70, 0x98
writeOneByte(0x42, 0x00); writeOneByte(0x44, 0x00); // 0x00 0x05
// 0x45 to 0x48 set a HS position just for Mode Detect. it's fine at start = 0 and stop = 1 or above
// Update: This is the retiming module. It can be used for SP processing with t5t57t6
writeOneByte(0x45, 0x00); // 0x00 // retiming SOG HS start
writeOneByte(0x46, 0x00); // 0xc0 // retiming SOG HS start
writeOneByte(0x47, 0x02); // 0x05 // retiming SOG HS stop // align with 1_26 (same value) seems good for phase
writeOneByte(0x48, 0x00); // 0xc0 // retiming SOG HS stop
writeOneByte(0x49, 0x04); // 0x04 rgbhv: 20
writeOneByte(0x4a, 0x00); // 0xc0
writeOneByte(0x4b, 0x44); // 0x34 rgbhv: 50
writeOneByte(0x4c, 0x00); // 0xc0
// h coast start / stop positions
// try these values and t5t3et2 when using cvid sync / no sync stripper
// appears start should be around 0x70, stop should be htotal - 0x70
//writeOneByte(0x4e, 0x00); writeOneByte(0x4d, 0x70); // | rgbhv: 0 0
//writeOneByte(0x50, 0x06); // rgbhv: 0 // is 0x06 for comment below
writeOneByte(0x4f, 0x9a); // rgbhv: 0 // psx with 54mhz osc. > 0xa4 too much, 0xa0 barely ok, > 0x9a!
writeOneByte(0x51, 0x02); // 0x00 rgbhv: 2
writeOneByte(0x52, 0x00); // 0xc0
writeOneByte(0x53, 0x06); // 0x05 rgbhv: 6
writeOneByte(0x54, 0x00); // 0xc0
//writeOneByte(0x55, 0x50); // auto coast off (on = d0, was default) 0xc0 rgbhv: 0 but 50 is fine
//writeOneByte(0x56, 0x0d); // sog mode on, clamp source pixclk, no sync inversion (default was invert h sync?) 0x21 rgbhv: 36
if (rto->videoStandardInput == 3 || rto->videoStandardInput == 4) {
writeOneByte(0x56, 0x01); // 0x01 for 480p over component
}
else {
writeOneByte(0x56, 0x05); // update: one of the new bits causes clamp glitches, check with checkerboard pattern
}
//writeOneByte(0x57, 0xc0); // 0xc0 rgbhv: 44 // set to 0x80 for retiming
writeOneByte(0x58, 0x05); //rgbhv: 0
writeOneByte(0x59, 0x00); //rgbhv: c0
writeOneByte(0x5a, 0x01); //rgbhv: 0 // was 0x05 but 480p ps2 doesnt like it
writeOneByte(0x5b, 0x00); //rgbhv: c8
writeOneByte(0x5c, 0x06); //rgbhv: 0
writeOneByte(0x5d, 0x00); //rgbhv: 0
}
// Sync detect resolution: 5bits; comparator voltage range 10mv~320mv.
// -> 10mV per step; recommended 120mV = 0x59 / 0x19 (snes likes 100mV, fine.)
void setSOGLevel(uint8_t level) {
uint8_t reg_5_02 = 0;
writeOneByte(0xF0, 5);
readFromRegister(0x02, 1, ®_5_02);
reg_5_02 = (reg_5_02 & 0xc1) | (level << 1);
writeOneByte(0x02, reg_5_02);
rto->currentLevelSOG = level;
Serial.print(" SOG lvl "); Serial.print(rto->currentLevelSOG);
Serial.print("\n");
}
void inputAndSyncDetect() {
// GBS boards have 2 potential sync sources:
// - 3 plug RCA connector
// - VGA input / 5 pin RGBS header / 8 pin VGA header (all 3 are shared electrically)
// This routine finds the input that has a sync signal, then stores the result for global use.
// Note: It is assumed that the 5725 has a preset loaded!
uint8_t readout = 0;
uint8_t previous = 0;
byte timeout = 0;
boolean syncFound = false;
setParametersSP();
writeOneByte(0xF0, 5);
writeOneByte(0x02, 0x15); // SOG on, slicer level 100mV, input 00 > R0/G0/B0/SOG0 as input (YUV)
writeOneByte(0xF0, 0);
timeout = 6; // try this input a few times and look for a change
readFromRegister(0x19, 1, &readout); // in hor. pulse width
while (timeout-- > 0) {
previous = readout;
readFromRegister(0x19, 1, &readout);
if (previous != readout) {
rto->inputIsYpBpR = 1;
syncFound = true;
break;
}
delay(1);
}
if (syncFound == false) {
writeOneByte(0xF0, 5);
writeOneByte(0x02, 0x55); // SOG on, slicer level 100mV, input 01 > R1/G1/B1/SOG1 as input (RGBS)
writeOneByte(0xF0, 0);
timeout = 6; // try this input a few times and look for a change
readFromRegister(0x19, 1, &readout); // in hor. pulse width
while (timeout-- > 0) {
previous = readout;
readFromRegister(0x19, 1, &readout);
if (previous != readout) {
rto->inputIsYpBpR = 0;
syncFound = true;
break;
}
delay(1);
}
}
if (!syncFound) {
Serial.print(F("no input with sync found\n"));
writeOneByte(0xF0, 0);
byte a = 0;
for (byte b = 0; b < 100; b++) {
readFromRegister(0x17, 1, &readout); // input htotal
a += readout;
}
if (a == 0) {
rto->sourceDisconnected = true;
Serial.print(F("source is off\n"));
}
}
if (syncFound && rto->inputIsYpBpR == true) {
Serial.print(F("using RCA inputs\n"));
rto->sourceDisconnected = false;
applyYuvPatches();
}
else if (syncFound && rto->inputIsYpBpR == false) {
Serial.print(F("using RGBS inputs\n"));
rto->sourceDisconnected = false;
}
}
uint8_t getSingleByteFromPreset(const uint8_t* programArray, unsigned int offset) {
return pgm_read_byte(programArray + offset);
}
void zeroAll() {
// turn processing units off first
writeOneByte(0xF0, 0);
writeOneByte(0x46, 0x00); // reset controls 1
writeOneByte(0x47, 0x00); // reset controls 2
// zero out entire register space
for (int y = 0; y < 6; y++) {
writeOneByte(0xF0, (uint8_t)y );
for (int z = 0; z < 16; z++) {
uint8_t bank[16];
for (int w = 0; w < 16; w++) {
bank[w] = 0;
}
writeBytes(z * 16, bank, 16);
}
}
}
void readFromRegister(uint8_t segment, uint8_t reg, int bytesToRead, uint8_t* output) {
writeOneByte(0xF0, segment);
readFromRegister(reg, bytesToRead, output);
}
void readFromRegister(uint8_t reg, int bytesToRead, uint8_t* output) {
Wire.beginTransmission(GBS_ADDR);
if (!Wire.write(reg)) {
Serial.print(F("i2c error\n"));
}
Wire.endTransmission();
Wire.requestFrom(GBS_ADDR, bytesToRead, true);
int rcvBytes = 0;
while (Wire.available()) {
output[rcvBytes++] = Wire.read();
}
if (rcvBytes != bytesToRead) {
Serial.print(F("i2c error\n"));
}
}
// dumps the current chip configuration in a format that's ready to use as new preset :)
void dumpRegisters(byte segment) {
uint8_t readout = 0;
if (segment > 5) return;
writeOneByte(0xF0, segment);
switch (segment) {
case 0:
for (int x = 0x40; x <= 0x5F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
for (int x = 0x90; x <= 0x9F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
case 1:
for (int x = 0x0; x <= 0x8F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
case 2:
for (int x = 0x0; x <= 0x3F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
case 3:
for (int x = 0x0; x <= 0x7F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
case 4:
for (int x = 0x0; x <= 0x5F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
case 5:
for (int x = 0x0; x <= 0x6F; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
break;
}
}
// required sections for reduced sets:
// S0_40 - S0_59 "misc"
// S1_00 - S1_2a "IF"
// S3_00 - S3_74 "VDS"
void dumpRegistersReduced() {
uint8_t readout = 0;
writeOneByte(0xF0, 0);
for (int x = 0x40; x <= 0x59; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
writeOneByte(0xF0, 1);
for (int x = 0x0; x <= 0x2a; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
writeOneByte(0xF0, 3);
for (int x = 0x0; x <= 0x74; x++) {
readFromRegister(x, 1, &readout);
Serial.print(readout);
Serial.println(",");
}
}
void resetPLLAD() {
uint8_t readout = 0;
writeOneByte(0xF0, 5);
readFromRegister(0x11, 1, &readout);
readout &= ~(1 << 7); // latch off
readout |= (1 << 0); // init vco voltage on
readout &= ~(1 << 1); // lock off
writeOneByte(0x11, readout);
readFromRegister(0x11, 1, &readout);
readout |= (1 << 7); // latch on
readout &= 0xfe; // init vco voltage off
writeOneByte(0x11, readout);
readFromRegister(0x11, 1, &readout);
readout |= (1 << 1); // lock on
delay(2);
writeOneByte(0x11, readout);
}
void resetPLL() {
uint8_t readout = 0;
writeOneByte(0xF0, 0);
readFromRegister(0x43, 1, &readout);
readout |= (1 << 2); // low skew
readout &= ~(1 << 5); // initial vco voltage off
writeOneByte(0x43, (readout & ~(1 << 5)));
readFromRegister(0x43, 1, &readout);
readout |= (1 << 4); // lock on
delay(2);
writeOneByte(0x43, readout); // main pll lock on
}
// soft reset cycle
// This restarts all chip units, which is sometimes required when important config bits are changed.
// Note: This leaves the main PLL uninitialized so issue a resetPLL() after this!
void resetDigital() {
writeOneByte(0xF0, 0);
writeOneByte(0x46, 0); writeOneByte(0x47, 0);
writeOneByte(0x43, 0x20); delay(10); // initial VCO voltage
resetPLL(); delay(10);
writeOneByte(0x46, 0x3f); // all on except VDS (display enable)
writeOneByte(0x47, 0x17); // all on except HD bypass
Serial.print(F("resetDigital\n"));
}
// returns true when all SP parameters are reasonable
// This needs to be extended for supporting more video modes.
boolean getSyncProcessorSignalValid() {
uint8_t register_low, register_high = 0;
uint16_t register_combined = 0;
boolean returnValue = false;
boolean horizontalOkay = false;
boolean verticalOkay = false;
boolean hpwOkay = false;
writeOneByte(0xF0, 0);
readFromRegister(0x07, 1, ®ister_high); readFromRegister(0x06, 1, ®ister_low);
register_combined = (((uint16_t)register_high & 0x0001) << 8) | (uint16_t)register_low;
// pal: 432, ntsc: 428, hdtv: 214?
if (register_combined > 422 && register_combined < 438) {
horizontalOkay = true; // pal, ntsc 428-432
}
else if (register_combined > 205 && register_combined < 225) {
horizontalOkay = true; // hdtv 214
}
//else Serial.print("hor bad\n");
readFromRegister(0x08, 1, ®ister_high); readFromRegister(0x07, 1, ®ister_low);
register_combined = (((uint16_t(register_high) & 0x000f)) << 7) | (((uint16_t)register_low & 0x00fe) >> 1);
if ((register_combined > 518 && register_combined < 530) && (horizontalOkay == true) ) {
verticalOkay = true; // ntsc
}
else if ((register_combined > 620 && register_combined < 632) && (horizontalOkay == true) ) {
verticalOkay = true; // pal
}
//else Serial.print("ver bad\n");
readFromRegister(0x1a, 1, ®ister_high); readFromRegister(0x19, 1, ®ister_low);
register_combined = (((uint16_t(register_high) & 0x000f)) << 8) | (uint16_t)register_low;
if ( (register_combined < 180) && (register_combined > 5)) {
hpwOkay = true;
}
else {
//Serial.print("hpw bad: ");
Serial.print(register_combined);
Serial.print("\n");
}
if ((horizontalOkay == true) && (verticalOkay == true) && (hpwOkay == true)) {
returnValue = true;
}
return returnValue;
}
void switchInputs() {
uint8_t readout = 0;
writeOneByte(0xF0, 5); readFromRegister(0x02, 1, &readout);
writeOneByte(0x02, (readout & ~(1 << 6)));
}
void SyncProcessorOffOn() {
uint8_t readout = 0;
disableDeinterlacer(); delay(5); // hide the glitching
writeOneByte(0xF0, 0);
readFromRegister(0x47, 1, &readout);
writeOneByte(0x47, readout & ~(1 << 2));
readFromRegister(0x47, 1, &readout);
writeOneByte(0x47, readout | (1 << 2));
enableDeinterlacer();
}
void resetModeDetect() {
uint8_t readout = 0, backup = 0;
writeOneByte(0xF0, 1);
readFromRegister(0x63, 1, &readout);
backup = readout;
writeOneByte(0x63, readout & ~(1 << 6));
writeOneByte(0x63, readout | (1 << 6));
writeOneByte(0x63, readout & ~(1 << 7));
writeOneByte(0x63, readout | (1 << 7));
writeOneByte(0x63, backup);
}
void shiftHorizontal(uint16_t amountToAdd, bool subtracting) {
uint8_t hrstLow = 0x00;
uint8_t hrstHigh = 0x00;
uint16_t htotal = 0x0000;
uint8_t hbstLow = 0x00;
uint8_t hbstHigh = 0x00;
uint16_t Vds_hb_st = 0x0000;
uint8_t hbspLow = 0x00;
uint8_t hbspHigh = 0x00;
uint16_t Vds_hb_sp = 0x0000;
// get HRST
readFromRegister(0x03, 0x01, 1, &hrstLow);
readFromRegister(0x02, 1, &hrstHigh);
htotal = ( ( ((uint16_t)hrstHigh) & 0x000f) << 8) | (uint16_t)hrstLow;
// get HBST
readFromRegister(0x04, 1, &hbstLow);
readFromRegister(0x05, 1, &hbstHigh);
Vds_hb_st = ( ( ((uint16_t)hbstHigh) & 0x000f) << 8) | (uint16_t)hbstLow;
// get HBSP
hbspLow = hbstHigh;
readFromRegister(0x06, 1, &hbspHigh);
Vds_hb_sp = ( ( ((uint16_t)hbspHigh) & 0x00ff) << 4) | ( (((uint16_t)hbspLow) & 0x00f0) >> 4);
// Perform the addition/subtraction
if (subtracting) {
Vds_hb_st -= amountToAdd;
Vds_hb_sp -= amountToAdd;
} else {
Vds_hb_st += amountToAdd;
Vds_hb_sp += amountToAdd;
}
// handle the case where hbst or hbsp have been decremented below 0
if (Vds_hb_st & 0x8000) {
Vds_hb_st = htotal % 2 == 1 ? (htotal + Vds_hb_st) + 1 : (htotal + Vds_hb_st);
}
if (Vds_hb_sp & 0x8000) {
Vds_hb_sp = htotal % 2 == 1 ? (htotal + Vds_hb_sp) + 1 : (htotal + Vds_hb_sp);
}
// handle the case where hbst or hbsp have been incremented above htotal
if (Vds_hb_st > htotal) {
Vds_hb_st = htotal % 2 == 1 ? (Vds_hb_st - htotal) - 1 : (Vds_hb_st - htotal);
}
if (Vds_hb_sp > htotal) {
Vds_hb_sp = htotal % 2 == 1 ? (Vds_hb_sp - htotal) - 1 : (Vds_hb_sp - htotal);
}
writeOneByte(0x04, (uint8_t)(Vds_hb_st & 0x00ff));
writeOneByte(0x05, ((uint8_t)(Vds_hb_sp & 0x000f) << 4) | ((uint8_t)((Vds_hb_st & 0x0f00) >> 8)) );
writeOneByte(0x06, (uint8_t)((Vds_hb_sp & 0x0ff0) >> 4) );
}
void shiftHorizontalLeft() {
shiftHorizontal(4, true);
}
void shiftHorizontalRight() {
shiftHorizontal(4, false);
}
void scaleHorizontalAbsolute(uint16_t value) {
uint8_t high = 0x00;
uint8_t newHigh = 0x00;
uint8_t low = 0x00;
uint8_t newLow = 0x00;
readFromRegister(0x03, 0x16, 1, &low);
readFromRegister(0x17, 1, &high);
Serial.print(F("Scale Hor: "));
Serial.print(value);
Serial.print("\n");
newHigh = (high & 0xfc) | (uint8_t)( (value / 256) & 0x0003);
newLow = (uint8_t)(value & 0x00ff);
writeOneByte(0x16, newLow);
writeOneByte(0x17, newHigh);
}
void scaleHorizontal(uint16_t amountToAdd, bool subtracting) {
uint8_t high = 0x00;
uint8_t newHigh = 0x00;
uint8_t low = 0x00;
uint8_t newLow = 0x00;
uint16_t newValue = 0x0000;
readFromRegister(0x03, 0x16, 1, &low);
readFromRegister(0x17, 1, &high);
newValue = ( ( ((uint16_t)high) & 0x0003) * 256) + (uint16_t)low;
if (subtracting && ((newValue - amountToAdd) > 0)) {
newValue -= amountToAdd;
} else if ((newValue + amountToAdd) <= 1023) {
newValue += amountToAdd;
}
Serial.print(F("Scale Hor: "));
Serial.print(newValue);
Serial.print("\n");
newHigh = (high & 0xfc) | (uint8_t)( (newValue / 256) & 0x0003);
newLow = (uint8_t)(newValue & 0x00ff);
writeOneByte(0x16, newLow);
writeOneByte(0x17, newHigh);
}
void scaleHorizontalSmaller() {
scaleHorizontal(1, false);
}
void scaleHorizontalLarger() {
scaleHorizontal(1, true);
}
void moveHS(uint16_t amountToAdd, bool subtracting) {
uint8_t high, low;
uint16_t newST, newSP;
writeOneByte(0xf0, 3);
readFromRegister(0x0a, 1, &low);
readFromRegister(0x0b, 1, &high);
newST = ( ( ((uint16_t)high) & 0x000f) << 8) | (uint16_t)low;
readFromRegister(0x0b, 1, &low);
readFromRegister(0x0c, 1, &high);
newSP = ( (((uint16_t)high) & 0x00ff) << 4) | ( (((uint16_t)low) & 0x00f0) >> 4);
if (subtracting) {
newST -= amountToAdd;
newSP -= amountToAdd;
} else {
newST += amountToAdd;
newSP += amountToAdd;
}
Serial.print("HSST: ");
Serial.print(newST);
Serial.print("\n");
Serial.print(" HSSP: ");
Serial.print(newSP);
Serial.print("\n");
writeOneByte(0x0a, (uint8_t)(newST & 0x00ff));
writeOneByte(0x0b, ((uint8_t)(newSP & 0x000f) << 4) | ((uint8_t)((newST & 0x0f00) >> 8)) );
writeOneByte(0x0c, (uint8_t)((newSP & 0x0ff0) >> 4) );
}
void moveVS(uint16_t amountToAdd, bool subtracting) {
uint8_t regHigh, regLow;
uint16_t newST, newSP, VDS_DIS_VB_ST, VDS_DIS_VB_SP;
writeOneByte(0xf0, 3);
// get VBST
readFromRegister(3, 0x13, 1, ®Low);
readFromRegister(3, 0x14, 1, ®High);
VDS_DIS_VB_ST = (((uint16_t)regHigh & 0x0007) << 8) | ((uint16_t)regLow) ;
// get VBSP
readFromRegister(3, 0x14, 1, ®Low);
readFromRegister(3, 0x15, 1, ®High);
VDS_DIS_VB_SP = ((((uint16_t)regHigh & 0x007f) << 4) | ((uint16_t)regLow & 0x00f0) >> 4) ;
readFromRegister(0x0d, 1, ®Low);
readFromRegister(0x0e, 1, ®High);
newST = ( ( ((uint16_t)regHigh) & 0x000f) << 8) | (uint16_t)regLow;
readFromRegister(0x0e, 1, ®Low);
readFromRegister(0x0f, 1, ®High);
newSP = ( (((uint16_t)regHigh) & 0x00ff) << 4) | ( (((uint16_t)regLow) & 0x00f0) >> 4);
if (subtracting) {
if ((newST - amountToAdd) > VDS_DIS_VB_ST) {
newST -= amountToAdd;
newSP -= amountToAdd;
} else Serial.print(F("limit!\n"));
} else {
if ((newSP + amountToAdd) < VDS_DIS_VB_SP) {
newST += amountToAdd;
newSP += amountToAdd;
} else Serial.print(F("limit!\n"));
}
Serial.print("VSST: ");
Serial.print(newST);
Serial.print("\n");
Serial.print(" VSSP: ");
Serial.print(newSP);
Serial.print("\n");
writeOneByte(0x0d, (uint8_t)(newST & 0x00ff));
writeOneByte(0x0e, ((uint8_t)(newSP & 0x000f) << 4) | ((uint8_t)((newST & 0x0f00) >> 8)) );
writeOneByte(0x0f, (uint8_t)((newSP & 0x0ff0) >> 4) );
}
void invertHS() {
uint8_t high, low;
uint16_t newST, newSP;
writeOneByte(0xf0, 3);
readFromRegister(0x0a, 1, &low);
readFromRegister(0x0b, 1, &high);
newST = ( ( ((uint16_t)high) & 0x000f) << 8) | (uint16_t)low;
readFromRegister(0x0b, 1, &low);
readFromRegister(0x0c, 1, &high);
newSP = ( (((uint16_t)high) & 0x00ff) << 4) | ( (((uint16_t)low) & 0x00f0) >> 4);
uint16_t temp = newST;
newST = newSP;
newSP = temp;
writeOneByte(0x0a, (uint8_t)(newST & 0x00ff));
writeOneByte(0x0b, ((uint8_t)(newSP & 0x000f) << 4) | ((uint8_t)((newST & 0x0f00) >> 8)) );
writeOneByte(0x0c, (uint8_t)((newSP & 0x0ff0) >> 4) );
}
void invertVS() {
uint8_t high, low;
uint16_t newST, newSP;
writeOneByte(0xf0, 3);
readFromRegister(0x0d, 1, &low);
readFromRegister(0x0e, 1, &high);
newST = ( ( ((uint16_t)high) & 0x000f) << 8) | (uint16_t)low;
readFromRegister(0x0e, 1, &low);
readFromRegister(0x0f, 1, &high);
newSP = ( (((uint16_t)high) & 0x00ff) << 4) | ( (((uint16_t)low) & 0x00f0) >> 4);
uint16_t temp = newST;
newST = newSP;
newSP = temp;