forked from sanni/cartreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN64.ino
4975 lines (4317 loc) · 135 KB
/
N64.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
//******************************************
// NINTENDO 64 MODULE
//******************************************
#ifdef enable_N64
/******************************************
Defines
*****************************************/
// These two macros toggle the eepDataPin/ControllerDataPin between input and output
// External 1K pull-up resistor from eepDataPin to VCC required
// 0x10 = 00010000 -> Port H Pin 4
#define N64_HIGH DDRH &= ~0x10
#define N64_LOW DDRH |= 0x10
// Read the current state(0/1) of the eepDataPin
#define N64_QUERY (PINH & 0x10)
/******************************************
Variables
*****************************************/
// Received N64 Eeprom data bits, 1 page
bool tempBits[65];
int eepPages;
// N64 Controller
// 256 bits of received Controller data
char N64_raw_dump[257];
// Array that holds one Controller Pak block of 32 bytes
byte myBlock[33];
String rawStr = ""; // above char array read into a string
struct {
char stick_x;
char stick_y;
}
N64_status;
//stings that hold the buttons
String button = "N/A";
String lastbutton = "N/A";
// Rom base address
unsigned long romBase = 0x10000000;
// Flashram type
byte flashramType = 1;
boolean MN63F81MPN = false;
//ControllerTest
bool quit = 1;
#ifdef savesummarytotxt
String CRC1 = "";
String CRC2 = "";
#endif
/******************************************
Menu
*****************************************/
// N64 start menu
static const char n64MenuItem1[] PROGMEM = "Game Cartridge";
static const char n64MenuItem2[] PROGMEM = "Controller";
static const char n64MenuItem3[] PROGMEM = "Flash Repro";
static const char n64MenuItem4[] PROGMEM = "Flash Gameshark";
static const char n64MenuItem5[] PROGMEM = "Reset";
static const char* const menuOptionsN64[] PROGMEM = {n64MenuItem1, n64MenuItem2, n64MenuItem3, n64MenuItem4, n64MenuItem5};
// N64 controller menu items
static const char N64ContMenuItem1[] PROGMEM = "Test Controller";
static const char N64ContMenuItem2[] PROGMEM = "Read ControllerPak";
static const char N64ContMenuItem3[] PROGMEM = "Write ControllerPak";
static const char N64ContMenuItem4[] PROGMEM = "Reset";
static const char* const menuOptionsN64Controller[] PROGMEM = {N64ContMenuItem1, N64ContMenuItem2, N64ContMenuItem3, N64ContMenuItem4};
// N64 cart menu items
static const char N64CartMenuItem1[] PROGMEM = "Read Rom";
static const char N64CartMenuItem2[] PROGMEM = "Read Save";
static const char N64CartMenuItem3[] PROGMEM = "Write Save";
static const char N64CartMenuItem4[] PROGMEM = "Force Savetype";
static const char N64CartMenuItem5[] PROGMEM = "Reset";
static const char* const menuOptionsN64Cart[] PROGMEM = {N64CartMenuItem1, N64CartMenuItem2, N64CartMenuItem3, N64CartMenuItem4, N64CartMenuItem5};
// N64 CRC32 error menu items
static const char N64CRCMenuItem1[] PROGMEM = "No";
static const char N64CRCMenuItem2[] PROGMEM = "Yes and keep old";
static const char N64CRCMenuItem3[] PROGMEM = "Yes and delete old";
static const char N64CRCMenuItem4[] PROGMEM = "Reset";
static const char* const menuOptionsN64CRC[] PROGMEM = {N64CRCMenuItem1, N64CRCMenuItem2, N64CRCMenuItem3, N64CRCMenuItem4};
// Rom menu
static const char N64RomItem1[] PROGMEM = "4MB";
static const char N64RomItem2[] PROGMEM = "8MB";
static const char N64RomItem3[] PROGMEM = "12MB";
static const char N64RomItem4[] PROGMEM = "16MB";
static const char N64RomItem5[] PROGMEM = "32MB";
static const char N64RomItem6[] PROGMEM = "64MB";
static const char* const romOptionsN64[] PROGMEM = {N64RomItem1, N64RomItem2, N64RomItem3, N64RomItem4, N64RomItem5, N64RomItem6};
// Save menu
static const char N64SaveItem1[] PROGMEM = "None";
static const char N64SaveItem2[] PROGMEM = "4K EEPROM";
static const char N64SaveItem3[] PROGMEM = "16K EEPROM";
static const char N64SaveItem4[] PROGMEM = "SRAM";
static const char N64SaveItem5[] PROGMEM = "FLASHRAM";
static const char* const saveOptionsN64[] PROGMEM = {N64SaveItem1, N64SaveItem2, N64SaveItem3, N64SaveItem4, N64SaveItem5};
// Repro write buffer menu
static const char N64BufferItem1[] PROGMEM = "no buffer";
static const char N64BufferItem2[] PROGMEM = "32 Byte";
static const char N64BufferItem3[] PROGMEM = "64 Byte";
static const char N64BufferItem4[] PROGMEM = "128 Byte";
static const char* const bufferOptionsN64[] PROGMEM = {N64BufferItem1, N64BufferItem2, N64BufferItem3, N64BufferItem4};
// Repro sector size menu
static const char N64SectorItem1[] PROGMEM = "8 KByte";
static const char N64SectorItem2[] PROGMEM = "32 KByte";
static const char N64SectorItem3[] PROGMEM = "64 KByte";
static const char N64SectorItem4[] PROGMEM = "128 KByte";
static const char* const sectorOptionsN64[] PROGMEM = {N64SectorItem1, N64SectorItem2, N64SectorItem3, N64SectorItem4};
// N64 start menu
void n64Menu() {
// create menu with title and 5 options to choose from
unsigned char n64Dev;
// Copy menuOptions out of progmem
convertPgm(menuOptionsN64, 5);
n64Dev = question_box(F("Select N64 device"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (n64Dev)
{
case 0:
display_Clear();
display_Update();
setup_N64_Cart();
printCartInfo_N64();
mode = mode_N64_Cart;
break;
case 1:
display_Clear();
display_Update();
setup_N64_Controller();
mode = mode_N64_Controller;
break;
case 2:
display_Clear();
display_Update();
setup_N64_Cart();
flashRepro_N64();
printCartInfo_N64();
mode = mode_N64_Cart;
break;
case 3:
display_Clear();
display_Update();
setup_N64_Cart();
flashGameshark_N64();
printCartInfo_N64();
mode = mode_N64_Cart;
break;
case 4:
resetArduino();
break;
}
}
// N64 Controller Menu
void n64ControllerMenu() {
// create menu with title and 4 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsN64Controller, 4);
mainMenu = question_box(F("N64 Controller"), menuOptions, 4, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
{
case 0:
display_Clear();
display_Update();
#if defined(enable_OLED)
controllerTest_OLED();
#elif defined(enable_LCD)
controllerTest_LCD();
#elif defined(enable_serial)
controllerTest_Serial();
#endif
quit = 1;
break;
case 1:
display_Clear();
display_Update();
readMPK();
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 2:
display_Clear();
display_Update();
// Change to root
filePath[0] = '\0';
sd.chdir("/");
// Launch file browser
fileBrowser(F("Select mpk file"));
display_Clear();
display_Update();
writeMPK();
verifyMPK();
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 3:
resetArduino();
break;
}
}
// N64 Cartridge Menu
void n64CartMenu() {
// create menu with title and 4 options to choose from
unsigned char mainMenu;
// Copy menuOptions out of progmem
convertPgm(menuOptionsN64Cart, 5);
mainMenu = question_box(F("N64 Cart Reader"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
{
case 0:
sd.chdir("/");
readRom_N64();
break;
case 1:
sd.chdir("/");
display_Clear();
if (saveType == 1) {
println_Msg(F("Reading Sram..."));
display_Update();
readSram(32768, 1);
}
else if (saveType == 4) {
getFramType();
println_Msg(F("Reading Flashram..."));
display_Update();
readFram(flashramType);
}
else if ((saveType == 5) || (saveType == 6)) {
println_Msg(F("Reading Eep..."));
display_Update();
#ifdef clockgen_installed
readEeprom();
#else
readEeprom_CLK();
#endif
}
else {
print_Error(F("Savetype Error"), false);
}
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 2:
filePath[0] = '\0';
sd.chdir("/");
if (saveType == 1) {
// Launch file browser
fileBrowser(F("Select sra file"));
display_Clear();
writeSram(32768);
writeErrors = verifySram(32768, 1);
if (writeErrors == 0) {
println_Msg(F("Sram verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
}
else if (saveType == 4) {
// Launch file browser
fileBrowser(F("Select fla file"));
display_Clear();
getFramType();
writeFram(flashramType);
print_Msg(F("Verifying..."));
display_Update();
writeErrors = verifyFram(flashramType);
if (writeErrors == 0) {
println_Msg(F("OK"));
display_Update();
}
else {
println_Msg("");
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
}
else if ((saveType == 5) || (saveType == 6)) {
// Launch file browser
fileBrowser(F("Select eep file"));
display_Clear();
#ifdef clockgen_installed
writeEeprom();
writeErrors = verifyEeprom();
#else
writeEeprom_CLK();
writeErrors = verifyEeprom_CLK();
#endif
if (writeErrors == 0) {
println_Msg(F("Eeprom verified OK"));
display_Update();
}
else {
print_Msg(F("Error: "));
print_Msg(writeErrors);
println_Msg(F(" bytes "));
print_Error(F("did not verify."), false);
}
}
else {
display_Clear();
print_Error(F("Savetype Error"), false);
}
println_Msg(F("Press Button..."));
display_Update();
wait();
break;
case 3:
// create submenu with title and 6 options to choose from
unsigned char N64SaveMenu;
// Copy menuOptions out of progmem
convertPgm(saveOptionsN64, 5);
N64SaveMenu = question_box(F("Select save type"), menuOptions, 5, 0);
// wait for user choice to come back from the question box menu
switch (N64SaveMenu)
{
case 0:
// None
saveType = 0;
break;
case 1:
// 4K EEPROM
saveType = 5;
eepPages = 64;
break;
case 2:
// 16K EEPROM
saveType = 6;
eepPages = 256;
break;
case 3:
// SRAM
saveType = 1;
break;
case 4:
// FLASHRAM
saveType = 4;
break;
}
break;
case 4:
resetArduino();
break;
}
}
/******************************************
Setup
*****************************************/
void setup_N64_Controller() {
// Output a low signal
PORTH &= ~(1 << 4);
// Set Controller Data Pin(PH4) to Input
DDRH &= ~(1 << 4);
}
void setup_N64_Cart() {
// Set Address Pins to Output and set them low
//A0-A7
DDRF = 0xFF;
PORTF = 0x00;
//A8-A15
DDRK = 0xFF;
PORTK = 0x00;
// Set Control Pins to Output RESET(PH0) WR(PH5) RD(PH6) aleL(PC0) aleH(PC1)
DDRH |= (1 << 0) | (1 << 5) | (1 << 6);
DDRC |= (1 << 0) | (1 << 1);
// Pull RESET(PH0) low until we are ready
PORTH &= ~(1 << 0);
// Output a high signal on WR(PH5) RD(PH6), pins are active low therefore everything is disabled now
PORTH |= (1 << 5) | (1 << 6);
// Pull aleL(PC0) low and aleH(PC1) high
PORTC &= ~(1 << 0);
PORTC |= (1 << 1);
#ifdef clockgen_installed
// Adafruit Clock Generator
initializeClockOffset();
if (!i2c_found) {
display_Clear();
print_Error(F("Clock Generator not found"), true);
}
// Set Eeprom clock to 2Mhz
clockgen.set_freq(200000000ULL, SI5351_CLK1);
// Start outputting Eeprom clock
clockgen.output_enable(SI5351_CLK1, 1); // Eeprom clock
#else
// Set Eeprom Clock Pin(PH1) to Output
DDRH |= (1 << 1);
// Output a high signal
PORTH |= (1 << 1);
#endif
// Set Eeprom Data Pin(PH4) to Input
DDRH &= ~(1 << 4);
// Activate Internal Pullup Resistors
//PORTH |= (1 << 4);
// Set sram base address
sramBase = 0x08000000;
#ifdef clockgen_installed
// Wait for clock generator
clockgen.update_status();
#endif
// Wait until all is stable
delay(300);
// Pull RESET(PH0) high to start eeprom
PORTH |= (1 << 0);
}
/******************************************
Low level functions
*****************************************/
// Switch Cartridge address/data pins to write
void adOut_N64() {
//A0-A7
DDRF = 0xFF;
PORTF = 0x00;
//A8-A15
DDRK = 0xFF;
PORTK = 0x00;
}
// Switch Cartridge address/data pins to read
void adIn_N64() {
//A0-A7
DDRF = 0x00;
//A8-A15
DDRK = 0x00;
//Enable internal pull-up resistors
//PORTF = 0xFF;
//PORTK = 0xFF;
}
// Set Cartridge address
void setAddress_N64(unsigned long myAddress) {
// Set address pins to output
adOut_N64();
// Split address into two words
word myAdrLowOut = myAddress & 0xFFFF;
word myAdrHighOut = myAddress >> 16;
// Switch WR(PH5) RD(PH6) ale_L(PC0) ale_H(PC1) to high (since the pins are active low)
PORTH |= (1 << 5) | (1 << 6);
PORTC |= (1 << 1);
__asm__("nop\n\t");
PORTC |= (1 << 0);
// Output high part to address pins
PORTF = myAdrHighOut & 0xFF;
PORTK = (myAdrHighOut >> 8) & 0xFF;
// Leave ale_H high for additional 62.5ns
__asm__("nop\n\t");
// Pull ale_H(PC1) low
PORTC &= ~(1 << 1);
// Output low part to address pins
PORTF = myAdrLowOut & 0xFF;
PORTK = (myAdrLowOut >> 8) & 0xFF;
// Leave ale_L high for ~125ns
__asm__("nop\n\t""nop\n\t");
// Pull ale_L(PC0) low
PORTC &= ~(1 << 0);
// Wait ~600ns just to be sure address is set
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Set data pins to input
adIn_N64();
}
// Read one word out of the cartridge
word readWord_N64() {
// Pull read(PH6) low
PORTH &= ~(1 << 6);
// Wait ~310ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Join bytes from PINF and PINK into a word
word tempWord = ( ( PINK & 0xFF ) << 8 ) | ( PINF & 0xFF );
// Pull read(PH6) high
PORTH |= (1 << 6);
// Wait 62.5ns
__asm__("nop\n\t");
return tempWord;
}
// Write one word to data pins of the cartridge
void writeWord_N64(word myWord) {
// Set address pins to output
adOut_N64();
// Output word to AD0-AD15
PORTF = myWord & 0xFF;
PORTK = (myWord >> 8) & 0xFF;
// Wait ~62.5ns
__asm__("nop\n\t");
// Pull write(PH5) low
PORTH &= ~(1 << 5);
// Wait ~310ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Pull write(PH5) high
PORTH |= (1 << 5);
// Wait ~125ns
__asm__("nop\n\t""nop\n\t");
// Set data pins to input
adIn_N64();
}
/******************************************
N64 Controller CRC Functions
*****************************************/
static word addrCRC(word address) {
// CRC table
word xor_table[16] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x15, 0x1F, 0x0B, 0x16, 0x19, 0x07, 0x0E, 0x1C, 0x0D, 0x1A, 0x01 };
word crc = 0;
// Make sure we have a valid address
address &= ~0x1F;
// Go through each bit in the address, and if set, xor the right value into the output
for (int i = 15; i >= 5; i--) {
// Is this bit set?
if ( ((address >> i) & 0x1)) {
crc ^= xor_table[i];
}
}
// Just in case
crc &= 0x1F;
// Create a new address with the CRC appended
return address | crc;
}
// unused
//static byte dataCRC(byte * data) {
// byte ret = 0;
// for (byte i = 0; i <= 32; i++) {
// for (byte j = 7; j >= 0; j--) {
// int tmp = 0;
// if (ret & 0x80) {
// tmp = 0x85;
// }
// ret <<= 1;
// if ( i < 32 ) {
// if (data[i] & (0x01 << j)) {
// ret |= 0x1;
// }
// }
// ret ^= tmp;
// }
// }
// return ret;
//}
/******************************************
N64 Controller Protocol Functions
*****************************************/
void N64_send(unsigned char *buffer, char length) {
// Send these bytes
char bits;
// This routine is very carefully timed by examining the assembly output.
// Do not change any statements, it could throw the timings off
//
// We get 16 cycles per microsecond, which should be plenty, but we need to
// be conservative. Most assembly ops take 1 cycle, but a few take 2
//
// I use manually constructed for-loops out of gotos so I have more control
// over the outputted assembly. I can insert nops where it was impossible
// with a for loop
asm volatile (";Starting outer for loop");
outer_loop:
{
asm volatile (";Starting inner for loop");
bits = 8;
inner_loop:
{
// Starting a bit, set the line low
asm volatile (";Setting line to low");
N64_LOW; // 1 op, 2 cycles
asm volatile (";branching");
if (*buffer >> 7) {
asm volatile (";Bit is a 1");
// 1 bit
// remain low for 1us, then go high for 3us
// nop block 1
asm volatile ("nop\nnop\nnop\nnop\nnop\n");
asm volatile (";Setting line to high");
N64_HIGH;
// nop block 2
// we'll wait only 2us to sync up with both conditions
// at the bottom of the if statement
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
}
else {
asm volatile (";Bit is a 0");
// 0 bit
// remain low for 3us, then go high for 1us
// nop block 3
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
asm volatile (";Setting line to high");
N64_HIGH;
// wait for 1us
asm volatile ("; end of conditional branch, need to wait 1us more before next bit");
}
// end of the if, the line is high and needs to remain
// high for exactly 16 more cycles, regardless of the previous
// branch path
asm volatile (";finishing inner loop body");
--bits;
if (bits != 0) {
// nop block 4
// this block is why a for loop was impossible
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
// rotate bits
asm volatile (";rotating out bits");
*buffer <<= 1;
goto inner_loop;
} // fall out of inner loop
}
asm volatile (";continuing outer loop");
// In this case: the inner loop exits and the outer loop iterates,
// there are /exactly/ 16 cycles taken up by the necessary operations.
// So no nops are needed here (that was lucky!)
--length;
if (length != 0) {
++buffer;
goto outer_loop;
} // fall out of outer loop
}
}
void N64_stop() {
// send a single stop (1) bit
// nop block 5
asm volatile ("nop\nnop\nnop\nnop\n");
N64_LOW;
// wait 1 us, 16 cycles, then raise the line
// 16-2=14
// nop block 6
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
N64_HIGH;
}
void N64_get(word bitcount) {
// listen for the expected bitcount/8 bytes of data back from the controller and
// blast it out to the N64_raw_dump array, one bit per byte for extra speed.
asm volatile (";Starting to listen");
unsigned char timeout;
char *bitbin = N64_raw_dump;
// Again, using gotos here to make the assembly more predictable and
// optimization easier (please don't kill me)
read_loop:
timeout = 0x3f;
// wait for line to go low
while (N64_QUERY) {
if (!--timeout)
return;
}
// wait approx 2us and poll the line
asm volatile (
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
*bitbin = N64_QUERY;
++bitbin;
--bitcount;
if (bitcount == 0)
return;
// wait for line to go high again
// it may already be high, so this should just drop through
timeout = 0x3f;
while (!N64_QUERY) {
if (!--timeout)
return;
}
goto read_loop;
}
/******************************************
N64 Controller Functions
*****************************************/
void get_button()
{
// Command to send to the gamecube
// The last bit is rumble, flip it to rumble
// yes this does need to be inside the loop, the
// array gets mutilated when it goes through N64_send
unsigned char command[] = {
0x01
};
// don't want interrupts getting in the way
noInterrupts();
// send those 3 bytes
N64_send(command, 1);
N64_stop();
// read in 32bits of data and dump it to N64_raw_dump
N64_get(32);
// end of time sensitive code
interrupts();
// The get_N64_status function sloppily dumps its data 1 bit per byte
// into the get_status_extended char array. It's our job to go through
// that and put each piece neatly into the struct N64_status
int i;
memset(&N64_status, 0, sizeof(N64_status));
// bits: joystick x value
// These are 8 bit values centered at 0x80 (128)
for (i = 0; i < 8; i++) {
N64_status.stick_x |= N64_raw_dump[16 + i] ? (0x80 >> i) : 0;
}
for (i = 0; i < 8; i++) {
N64_status.stick_y |= N64_raw_dump[24 + i] ? (0x80 >> i) : 0;
}
// read char array N64_raw_dump into string rawStr
rawStr = "";
for (i = 0; i < 16; i++) {
rawStr = rawStr + String(N64_raw_dump[i], DEC);
}
// Buttons (A,B,Z,S,DU,DD,DL,DR,0,0,L,R,CU,CD,CL,CR)
if (rawStr.substring(0, 16) == "0000000000000000") {
lastbutton = button;
button = F("Press a button");
}
else
{
for (int i = 0; i < 16; i++)
{
// seems to be 16, 8 or 4 depending on what pin is used
if (N64_raw_dump[i] == 16)
{
switch (i)
{
case 7:
button = F("D-Right");
break;
case 6:
button = F("D-Left");
break;
case 5:
button = F("D-Down");
break;
case 4:
button = F("D-Up");
break;
case 3:
button = F("START");
break;
case 2:
button = F("Z");
break;
case 1:
button = F("B");
break;
case 0:
button = F("A");
break;
case 15:
button = F("C-Right");
break;
case 14:
button = F("C-Left");
break;
case 13:
button = F("C-Down");
break;
case 12:
button = F("C-Up");
break;
case 11:
button = F("R");
break;
case 10:
button = F("L");
break;
}
}
}
}
}
/******************************************
N64 Controller Test
*****************************************/
#ifdef enable_serial
void controllerTest_Serial() {
while (quit) {
// Get Button and analog stick
get_button();
// Print Button
String buttonc = String("Button: " + String(button) + " ");
Serial.print(buttonc);
// Print Stick X Value
String stickx = String("X: " + String(N64_status.stick_x, DEC) + " ");
Serial.print(stickx);
// Print Stick Y Value
String sticky = String(" Y: " + String(N64_status.stick_y, DEC) + " ");
Serial.println(sticky);
if (button == "Press a button" && lastbutton == "Z") {
// Quit
Serial.println("");
quit = 0;
}
}
}
#endif
#ifdef enable_LCD
#define CENTER 64
// on which screens do we start
int startscreen = 1;
int test = 1;
void printSTR(String st, int x, int y)
{
char buf[st.length() + 1];
if (x == CENTER) {
x = 64 - (((st.length() - 5) / 2) * 4);
}
st.toCharArray(buf, st.length() + 1);
display.drawStr(x, y, buf);
}
void nextscreen()
{
if (button == "Press a button" && lastbutton == "START")
{
// reset button
lastbutton = "N/A";
display.clearDisplay();
if (startscreen != 4)
startscreen = startscreen + 1;
else
{
startscreen = 1;
test = 1;
}
}
else if (button == "Press a button" && lastbutton == "Z" && startscreen == 4)
{
// Quit
quit = 0;
}
}
void controllerTest_LCD() {
int mode = 0;
//name of the current displayed result
String anastick = "";
// Graph
int xax = 24; // midpoint x
int yax = 24; // midpoint y
int zax = 24; // size
// variables to display test data of different sticks
int upx = 0;
int upy = 0;
int uprightx = 0;
int uprighty = 0;
int rightx = 0;
int righty = 0;
int downrightx = 0;
int downrighty = 0;
int downx = 0;
int downy = 0;
int downleftx = 0;
int downlefty = 0;
int leftx = 0;
int lefty = 0;
int upleftx = 0;
int uplefty = 0;