/
ICT.ino
2580 lines (2442 loc) · 64.1 KB
/
ICT.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
// ICT
// © 2020-2021 Patrick Lafarguette
#include "ICT.h"
#include <MCUFRIEND_kbv.h>
#include <SdFat.h>
#include <TouchScreen.h>
#include "IC.h"
#include "Board.h"
#include "Keyboard.h"
#include "Memory.h"
#include "Stack.h"
// Set one of the language to 1.
// French UI, currently not viable as screen only supports
// unaccented US-ASCII character set.
#define ENGLISH 1
#define FRENCH 0
#include "Language.h"
// TFT calibration
// See TouchScreen_Calibr_native example in MCUFRIEND_kbv library
const int XP = 8, XM = A2, YP = A3, YM = 9;
const int TS_LEFT = 908, TS_RT = 122, TS_TOP = 85, TS_BOT = 905;
MCUFRIEND_kbv tft;
// XP (LCD_RS), XM (LCD_D0) resistance is 345 Ω
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 345);
// SD
#if SD_FAT_VERSION < 20000
// Modify SdFatConfig.h
//
//#define ENABLE_SOFTWARE_SPI_CLASS 1
SdFatSoftSpi<12, 11, 13> fat;
#else
// Modify SdFatConfig.h
//
// #define SPI_DRIVER_SELECT 2
const uint8_t SD_CS_PIN = 10;
const uint8_t SOFT_MISO_PIN = 12;
const uint8_t SOFT_MOSI_PIN = 11;
const uint8_t SOFT_SCK_PIN = 13;
SoftSpiDriver<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> spi;
#define SDCONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(0), &spi)
SdFat fat;
#endif
File file;
IC ic;
Stack<IC*> ics;
Stack<String*> lines;
Stack<Filename*> folders;
Stack<Filename*> files;
String path;
Tiles TILES_MENU = {
2,
1,
2,
{ { TILE_FLAG_CENTER, "logic"}, { TILE_FLAG_CENTER, "memory" } }
};
Tiles TILES_LOGIC = {
10,
4,
3,
{
{ TILE_FLAG_CENTER, "14"}, { TILE_FLAG_CENTER, "16" }, { TILE_FLAG_CENTER, "18" },
{ TILE_FLAG_CENTER, "20"}, { TILE_FLAG_CENTER, "22" }, { TILE_FLAG_CENTER, "24" },
{ TILE_FLAG_CENTER, "28"}, { TILE_FLAG_CENTER, "32" }, { TILE_FLAG_CENTER, "40" },
{ TILE_FLAG_CENTER, "Keyboard"}
}
};
uint8_t PIN_COUNT[] = { 14, 16, 18, 20, 22, 24, 28, 32, 40 };
Tiles TILES_ROM = {
3,
3,
1,
{ { TILE_FLAG_NONE, "Break on blank fail" }, { TILE_FLAG_NONE, "Read to serial" }, { TILE_FLAG_NONE, "Read to file" } }
};
Tiles TILES_NONE = {
4,
4,
1,
{}
};
Tiles TILES_FRAM = {
4,
4,
1,
{
{ TILE_FLAG_NONE, "Read to serial" },
{ TILE_FLAG_NONE, "Read to file" },
{ TILE_FLAG_NONE, "Erase" },
{ TILE_FLAG_NONE, "Write" }
}
};
Tiles TILES_BROWSE = {
4,
4,
1,
{
{ TILE_FLAG_NONE, NULL },
{ TILE_FLAG_NONE, NULL },
{ TILE_FLAG_NONE, NULL },
{ TILE_FLAG_NONE, NULL }
}
};
bool options[option_count];
#define TEST 0 // 1 to enable test cases, 0 to disable
#define TIME 1 // 1 to enable time, 0 to disable
#define DEBUG 1 // 1 to enable serial debug messages, 0 to disable
#define MISMATCH 1 // 1 to enable serial mismatch messages, 0 to disable
#define CAPTURE 1 // 1 to enable screen captures, 0 to disable
#if DEBUG
#define Debug(...) Serial.print(__VA_ARGS__)
#define Debugln(...) Serial.println(__VA_ARGS__)
#else
#define Debug(...)
#define Debugln(...)
#endif
#define HEAP_BEGIN int32_t memory = freemem();
#define HEAP_END if (memory != freemem()) { Debug("LEAK "); Debugln(__func__); }
#define HEAP_FREE { Debug("HEAP "); Debug(__func__); Debug(" "); Debugln(freemem()); }
typedef struct Worker {
uint8_t task;
uint8_t screen;
uint8_t action;
bool interrupted;
// Browse
uint16_t first;
uint16_t last;
// TFT
int x;
int y;
// File
uint32_t position;
// IC
Package package;
String code;
// Identify
uint8_t index;
// Test
bool found;
bool ok;
unsigned int success;
unsigned int failure;
// UI
uint16_t color;
unsigned int indicator;
uint8_t ram;
uint16_t content;
} Worker;
Worker worker;
Keyboard keyboard(&tft, 0, INPUT_HEIGHT + 1);
Board board(&tft, 0, AREA_CONTENT, TFT_WIDTH, AREA_FOOTER - AREA_CONTENT);
Adafruit_GFX_Button buttons[button_count];
// Filenames
#define INIFILE "ict.ini"
#define TMPFILE "ict.tmp"
#define LOGICFILE "logic.ict"
#define MEMORYFILE "memory.ict"
// Number format
void bin(uint32_t value, uint8_t count) {
for (uint8_t index = count; index > 0;) {
Debug(value >> (--index * 1) & 0x01, BIN);
}
}
void hexa(uint32_t value, uint8_t count) {
for (uint8_t index = count; index > 0;) {
Debug(value >> (--index * 4) & 0x0F, HEX);
}
}
////////////
// Memory //
////////////
extern char *__brkval;
int freemem() {
char top;
return &top - __brkval;
}
/////////
// Bus //
/////////
const uint8_t BYTES[] = {
(uint8_t)(1 << 0),
(uint8_t)(1 << 1),
(uint8_t)(1 << 2),
(uint8_t)(1 << 3),
(uint8_t)(1 << 4),
(uint8_t)(1 << 5),
(uint8_t)(1 << 6),
(uint8_t)(1 << 7)
};
const uint16_t WORDS[] = {
(uint16_t)(1 << 0),
(uint16_t)(1 << 1),
(uint16_t)(1 << 2),
(uint16_t)(1 << 3),
(uint16_t)(1 << 4),
(uint16_t)(1 << 5),
(uint16_t)(1 << 6),
(uint16_t)(1 << 7),
(uint16_t)(1 << 8),
(uint16_t)(1 << 9),
(uint16_t)(1 << 10),
(uint16_t)(1 << 11),
(uint16_t)(1 << 12),
(uint16_t)(1 << 13),
(uint16_t)(1 << 14),
(uint16_t)(1 << 15)
};
void ic_bus_write_address_word(Bus& bus) {
uint16_t lsb = bus.address;
for (uint8_t index = 0; index < bus.width; ++index) {
ic_pin_write(worker.package.pins[bus.pins[index]], lsb & WORDS[index] ? HIGH : LOW);
}
}
void ic_bus_write_address_dword(Bus& bus) {
uint16_t lsb = bus.address;
uint16_t msb = bus.address >> 16;
uint8_t index = 0;
for (; index < 16; ++index) {
ic_pin_write(worker.package.pins[bus.pins[index]], lsb & WORDS[index] ? HIGH : LOW);
}
for (; index < bus.width; ++index) {
ic_pin_write(worker.package.pins[bus.pins[index]], msb & WORDS[index & 0x0F] ? HIGH : LOW);
}
}
void ic_bus_write_data(Bus& bus) {
for (uint8_t index = 0; index < bus.width; ++index) {
ic_pin_write(worker.package.pins[bus.pins[index]], bus.data & BYTES[index]);
}
}
void ic_bus_read_data(Bus& bus) {
bus.data = 0;
for (uint8_t index = 0; index < bus.width; ++index) {
if (ic_pin_read(worker.package.pins[bus.pins[index]])) {
bus.data |= BYTES[index];
}
}
}
void ic_bus_output(Bus& bus) {
for (uint8_t index = 0; index < bus.width; ++index) {
ic_pin_mode(worker.package.pins[bus.pins[index]], OUTPUT);
}
}
void ic_bus_input(Bus& bus) {
for (uint8_t index = 0; index < bus.width; ++index) {
ic_pin_mode(worker.package.pins[bus.pins[index]], INPUT);
}
}
void ic_bus_data(Bus& bus, uint8_t bit, const bool alternate) {
uint8_t mask = alternate ? HIGH : LOW;
bus.data = 0;
for (uint8_t index = 0; index < bus.width; ++index) {
if (bit) {
bus.data |= BYTES[index];
}
bit ^= mask;
}
Debug(F("fill 0b"));
bin(bus.data, bus.width);
Debugln();
}
////////////
// Memory //
////////////
bool ic_memory_signal(uint8_t signal) {
return memory.signals[signal] > -1;
}
/////////
// RAM //
/////////
void ic_ram_loop() {
// UI
worker.ram = 0;
worker.color = TFT_WHITE;
for (uint8_t index = 0; index < 4; ++index) {
ui_draw_ram();
}
worker.ram = 0;
#if TIME
// Timer
unsigned long start = millis();
#endif
// Setup
for (uint8_t index = 0; index < memory.bus[BUS_Q].width; ++index) {
ic_pin_mode(worker.package.pins[memory.bus[BUS_Q].pins[index]], INPUT);
}
// VCC, GND
if ((memory.signals[SIGNAL_VBB] == 0) && (memory.signals[SIGNAL_VDD] == 7)) {
// Power rails to -5 and +12 V
Debugln("Enable -5 and +12 V");
digitalWrite(A7, HIGH);
}
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_GND]], LOW);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], HIGH);
// At package creation all pins are output low
// LOW bus is preset, so only set HIGH bus high
for (uint8_t index = 0; index < memory.bus[BUS_HIGH].width; ++index) {
ic_pin_write(worker.package.pins[memory.bus[BUS_HIGH].pins[index]], HIGH);
}
// Idle
memory.idle();
// Test
worker.indicator = 0;
for (uint8_t loop = 0; loop < 4; ++loop) {
// 0 00 0 false true
// 1 01 0 false true
// 2 10 1 true false
// 3 11 1 true false
bool alternate = !(loop >> 1);
// 0 00 0 LOW
// 1 01 1 HIGH
// 2 10 0 LOW
// 3 11 1 HIGH
ic_bus_data(memory.bus[BUS_D], loop & 0x01, alternate);
memory.fill(alternate);
}
// Shutdown
if ((memory.signals[SIGNAL_VBB] == 0) && (memory.signals[SIGNAL_VDD] == 7)) {
Debugln("Disable -5 and +12 V");
// Power rails to TTL
digitalWrite(A7, LOW);
}
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], LOW);
#if TIME
unsigned long stop = millis();
Serial.print(F(TIME_ELAPSED));
Serial.println((stop - start) / 1000.0);
#endif
}
//////////
// DRAM //
//////////
void ic_dram_idle() {
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], HIGH);
if (ic_memory_signal(SIGNAL_OE)) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CAS]], HIGH);
for (uint8_t index = 0; index < memory.bus[BUS_RAS].width; ++index) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], LOW);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], HIGH);
}
}
void ic_dram_write() {
// Row
memory.write_address(memory.bus[BUS_RAS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], LOW);
// WE
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], LOW);
// D
ic_bus_write_data(memory.bus[BUS_D]);
// Column
memory.write_address(memory.bus[BUS_CAS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CAS]], LOW);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CAS]], HIGH);
}
void ic_dram_read() {
// Row
memory.write_address(memory.bus[BUS_RAS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], LOW);
// Column
memory.write_address(memory.bus[BUS_CAS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CAS]], LOW);
// OE
bool oe = ic_memory_signal(SIGNAL_OE);
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], LOW);
}
// Q
ic_bus_read_data(memory.bus[BUS_Q]);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_RAS]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CAS]], HIGH);
// OE
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
}
void ic_dram_fill(const bool alternate = false) {
uint8_t mask = (alternate && (memory.bus[BUS_D].width > 1)) ? memory.bus[BUS_D].high : 0;
worker.color = COLOR_GOOD;
for (memory.bus[BUS_CAS].address = 0; memory.bus[BUS_CAS].address < memory.bus[BUS_CAS].high; ++memory.bus[BUS_CAS].address) {
worker.success = 0;
worker.failure = 0;
ic_bus_output(memory.bus[BUS_D]);
// Inner loop is RAS to keep refreshing rows while writing a full column
for (memory.bus[BUS_RAS].address = 0; memory.bus[BUS_RAS].address < memory.bus[BUS_RAS].high; ++memory.bus[BUS_RAS].address) {
ic_dram_write();
memory.bus[BUS_D].data ^= mask;
}
ic_bus_input(memory.bus[BUS_Q]);
// Inner loop is RAS to keep refreshing rows while reading back a full column
for (memory.bus[BUS_RAS].address = 0; memory.bus[BUS_RAS].address < memory.bus[BUS_RAS].high; ++memory.bus[BUS_RAS].address) {
ic_dram_read();
if (memory.bus[BUS_D].data != memory.bus[BUS_Q].data) {
#if MISMATCH
Debug("RAS ");
bin(memory.bus[BUS_RAS].address, memory.bus[BUS_RAS].width);
Debug(", CAS ");
bin(memory.bus[BUS_CAS].address, memory.bus[BUS_CAS].width);
Debug(", D ");
bin(memory.bus[BUS_D].data, memory.bus[BUS_D].width);
Debug(", Q ");
bin(memory.bus[BUS_Q].data, memory.bus[BUS_Q].width);
Debug(", delta ");
bin(memory.bus[BUS_D].data ^ memory.bus[BUS_Q].data, memory.bus[BUS_D].width);
Debugln();
#endif
worker.failure++;
worker.color = COLOR_BAD;
} else {
worker.success++;
}
memory.bus[BUS_D].data ^= mask;
}
ui_draw_indicator((worker.success ? (worker.failure ? COLOR_MIXED : COLOR_GOOD) : COLOR_BAD));
}
ui_draw_ram();
}
//////////
// SRAM //
//////////
void ic_sram_idle() {
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], HIGH);
if (ic_memory_signal(SIGNAL_OE)) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
}
void ic_sram_write() {
#if 1
// Address
memory.write_address(memory.bus[BUS_ADDRESS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], LOW);
// WE
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], LOW);
// D
ic_bus_write_data(memory.bus[BUS_D]);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
#else
// Address
memory.write_address(memory.bus[BUS_ADDRESS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], LOW);
// Positive pulse
bool pp = ic_memory_signal(SIGNAL_PP);
if (pp) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_PP]], HIGH);
}
// D
ic_bus_write_data(memory.bus[BUS_D]);
// WE
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], LOW);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_WE]], HIGH);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
// Positive pulse
if (pp) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_PP]], LOW);
}
#endif
}
void ic_sram_read() {
#if 1
// Address
memory.write_address(memory.bus[BUS_ADDRESS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], LOW);
// OE
bool oe = ic_memory_signal(SIGNAL_OE);
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], LOW);
}
// Q
ic_bus_read_data(memory.bus[BUS_Q]);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
// OE
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
#else
// Address
memory.write_address(memory.bus[BUS_ADDRESS]);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], LOW);
// OE
bool oe = ic_memory_signal(SIGNAL_OE);
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], LOW);
}
// Positive pulse
bool pp = ic_memory_signal(SIGNAL_PP);
if (pp) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_PP]], HIGH);
}
// Q
ic_bus_read_data(memory.bus[BUS_Q]);
// Idle
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
// OE
if (oe) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
// Positive pulse
if (pp) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_PP]], LOW);
}
#endif
}
void ic_sram_fill(const bool alternate = false) {
uint8_t mask = (alternate && (memory.bus[BUS_D].width > 1)) ? memory.bus[BUS_D].high : 0;
worker.color = COLOR_GOOD;
// Write full address space
ic_bus_output(memory.bus[BUS_D]);
for (memory.bus[BUS_ADDRESS].address = 0; memory.bus[BUS_ADDRESS].address < memory.bus[BUS_ADDRESS].high; ++memory.bus[BUS_ADDRESS].address) {
ic_sram_write();
memory.bus[BUS_D].data ^= mask;
}
// Read full address space
ic_bus_input(memory.bus[BUS_Q]);
for (memory.bus[BUS_ADDRESS].address = 0; memory.bus[BUS_ADDRESS].address < memory.bus[BUS_ADDRESS].high; ++memory.bus[BUS_ADDRESS].address) {
worker.success = 0;
worker.failure = 0;
ic_sram_read();
if (memory.bus[BUS_D].data != memory.bus[BUS_Q].data) {
#if MISMATCH
Debug("A ");
bin(memory.bus[BUS_ADDRESS].address, memory.bus[BUS_ADDRESS].width);
Debug(", D ");
bin(memory.bus[BUS_D].data, memory.bus[BUS_D].width);
Debug(", Q ");
bin(memory.bus[BUS_Q].data, memory.bus[BUS_Q].width);
Debug(", delta ");
bin(memory.bus[BUS_D].data ^ memory.bus[BUS_Q].data, memory.bus[BUS_D].width);
Debugln();
#endif
worker.failure++;
worker.color = COLOR_BAD;
} else {
worker.success++;
}
memory.bus[BUS_D].data ^= mask;
ui_draw_indicator((worker.success ? (worker.failure ? COLOR_MIXED : COLOR_GOOD) : COLOR_BAD));
}
ui_draw_ram();
}
//////////
// FRAM //
//////////
// FRAM
// - Erase.
// - Write from file.
void ic_fram_loop() {
if (options[option_erase]) {
memory.bus[BUS_D].data = 0xFF;
}
// File
if (options[option_write]) {
String filename(TILES_BROWSE.tile[board.tile()].label);
file = fat.open(filename.c_str(), O_RDWR);
ui_clear_subcontent();
tft.print(F("Write "));
tft.println(filename);
}
// UI
uint16_t color = COLOR_OK;
worker.color = COLOR_GOOD;
ui_draw_rom();
// Miscellaneous
uint8_t data[16];
#if TIME
// Timer
unsigned long start = millis();
#endif
// Setup
for (uint8_t index = 0; index < memory.bus[BUS_D].width; ++index) {
ic_pin_mode(worker.package.pins[memory.bus[BUS_D].pins[index]], INPUT);
}
// VCC, GND
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_GND]], LOW);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], HIGH);
// At package creation all pins are output low
// LOW bus is preset, so only set HIGH bus high
for (uint8_t index = 0; index < memory.bus[BUS_HIGH].width; ++index) {
ic_pin_write(worker.package.pins[memory.bus[BUS_HIGH].pins[index]], HIGH);
}
// Idle
memory.idle();
// Write
worker.indicator = 0;
uint8_t index = 0;
// Write full address space
ic_bus_output(memory.bus[BUS_D]);
for (memory.bus[BUS_ADDRESS].address = 0; memory.bus[BUS_ADDRESS].address < memory.bus[BUS_ADDRESS].high; ++memory.bus[BUS_ADDRESS].address) {
if (options[option_write]) {
if (index == 0) {
file.read(data, 16);
}
memory.bus[BUS_D].data = data[index];
}
ic_sram_write();
++index %= 16;
ui_draw_indicator(color);
}
// Shutdown
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], LOW);
#if TIME
unsigned long stop = millis();
Serial.print(F(TIME_ELAPSED));
Serial.println((stop - start) / 1000.0);
#endif
// File
if (options[option_write]) {
file.close();
}
}
///////////
// Flash //
///////////
void ic_flash_id() {
// Write
ic_bus_output(memory.bus[BUS_D]);
// 5555 AA
// 2AAA 55
// 5555 90
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xAA;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x2AAA;
memory.bus[BUS_D].data = 0x55;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0x90;
ic_sram_write();
// Read
ic_bus_input(memory.bus[BUS_Q]);
// Manufacturer ID
memory.bus[BUS_ADDRESS].address = 0x0000;
ic_sram_read();
uint8_t manufacturerID = memory.bus[BUS_Q].data;
// Chip ID
memory.bus[BUS_ADDRESS].address = 0x0001;
ic_sram_read();
uint8_t chipID = memory.bus[BUS_Q].data;
// Write
ic_bus_output(memory.bus[BUS_D]);
// 5555 AA
// 2AAA 55
// 5555 F0
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xAA;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x2AAA;
memory.bus[BUS_D].data = 0x55;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xF0;
ic_sram_write();
// Output
Debug("Manufacturer 0x");
hexa(manufacturerID, 2);
Debug(", chip 0x");
hexa(chipID, 2);
Debugln();
}
void ic_flash_erase() {
#if TIME
// Timer
unsigned long start = millis();
#endif
// Write
ic_bus_output(memory.bus[BUS_D]);
// 5555 AA
// 2AAA 55
// 5555 80
// 5555 AA
// 2AAA 55
// 5555 10
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xAA;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x2AAA;
memory.bus[BUS_D].data = 0x55;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0x80;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xAA;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x2AAA;
memory.bus[BUS_D].data = 0x55;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0x10;
ic_sram_write();
// Delay TSCE, 100 ms
delay(100);
ic_toggle_bit(); // Not in SST39SF040.txt
#if TIME
unsigned long stop = millis();
Serial.print("Erase ");
Serial.print((stop - start));
Serial.println(" ms");
#endif
}
void ic_flash_write() {
// Write
ic_bus_output(memory.bus[BUS_D]);
// 5555 AA
// 2AAA 55
// 5555 A0
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xAA;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x2AAA;
memory.bus[BUS_D].data = 0x55;
ic_sram_write();
memory.bus[BUS_ADDRESS].address = 0x5555;
memory.bus[BUS_D].data = 0xA0;
ic_sram_write();
}
void ic_toggle_bit() {
uint8_t byte, same;
// Read
ic_bus_input(memory.bus[BUS_Q]);
// DQ6 toggle
do {
ic_sram_read();
byte = memory.bus[BUS_Q].data;
ic_sram_read();
same = memory.bus[BUS_Q].data;
} while ((byte ^ same) & 0x40);
}
// Flash
// - Erase.
// - Write from file.
void ic_flash_loop() {
// File
if (options[option_write]) {
String filename(TILES_BROWSE.tile[board.tile()].label);
file = fat.open(filename.c_str(), O_RDWR);
ui_clear_subcontent();
tft.print(F("Write "));
tft.println(filename);
}
// UI
uint16_t color = COLOR_OK;
worker.color = COLOR_GOOD;
ui_draw_rom();
// Miscellaneous
uint8_t data[16];
#if TIME
// Timer
unsigned long start = millis();
#endif
// Setup
for (uint8_t index = 0; index < memory.bus[BUS_D].width; ++index) {
ic_pin_mode(worker.package.pins[memory.bus[BUS_D].pins[index]], INPUT);
}
// VCC, GND
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_GND]], LOW);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], HIGH);
// At package creation all pins are output low
// LOW bus is preset, so only set HIGH bus high
for (uint8_t index = 0; index < memory.bus[BUS_HIGH].width; ++index) {
ic_pin_write(worker.package.pins[memory.bus[BUS_HIGH].pins[index]], HIGH);
}
// Idle
memory.idle();
// Read ID
ic_flash_id();
// Erase
ic_flash_erase();
// Write
if (options[option_write]) {
worker.indicator = 0;
uint8_t index = 0;
for (uint32_t address = 0; address < memory.bus[BUS_ADDRESS].high; ++address) {
if (index == 0) {
file.read(data, 16);
ui_draw_indicator(color);
}
ic_flash_write();
memory.bus[BUS_ADDRESS].address = address;
memory.bus[BUS_D].data = data[index];
ic_sram_write();
ic_toggle_bit();
++index %= 16;
}
}
// Shutdown
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], LOW);
#if TIME
unsigned long stop = millis();
Serial.print(F(TIME_ELAPSED));
Serial.println((stop - start) / 1000.0);
#endif
// File
if (options[option_write]) {
file.close();
}
}
/////////
// ROM //
/////////
// ROM, FRAM, Flash
// - Read to serial.
// - Read to file.
void ic_rom_loop() {
if (options[option_file]) {
String filename;
sd_filename(filename, "output", ".bin");
file = fat.open(filename.c_str(), O_CREAT | O_RDWR);
ui_clear_subcontent();
tft.print(F("Read "));
tft.println(filename);
}
// UI
uint16_t color = COLOR_OK;
worker.color = COLOR_GOOD;
ui_draw_rom();
// Miscellaneous
uint8_t count = (memory.bus[BUS_ADDRESS].width) / 4 + (memory.bus[BUS_ADDRESS].width % 4 ? 1 : 0);
uint8_t data[16];
#if TIME
// Timer
unsigned long start = millis();
#endif
// Setup
for (uint8_t index = 0; index < memory.bus[BUS_Q].width; ++index) {
ic_pin_mode(worker.package.pins[memory.bus[BUS_Q].pins[index]], INPUT);
}
// VCC, GND
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_GND]], LOW);
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], HIGH);
// At package creation all pins are output low
// LOW bus is preset, so only set HIGH bus high
for (uint8_t index = 0; index < memory.bus[BUS_HIGH].width; ++index) {
ic_pin_write(worker.package.pins[memory.bus[BUS_HIGH].pins[index]], HIGH);
}
// Idle
memory.idle();
// Test
worker.indicator = 0;
uint8_t index = 0;
bool blank = true;
// Read full address space
ic_bus_input(memory.bus[BUS_Q]);
for (memory.bus[BUS_ADDRESS].address = 0; memory.bus[BUS_ADDRESS].address < memory.bus[BUS_ADDRESS].high; ++memory.bus[BUS_ADDRESS].address) {
ic_sram_read();
data[index] = memory.bus[BUS_Q].data;
// Serial
if (options[option_serial]) {
if (index == 0) {
hexa(memory.bus[BUS_ADDRESS].address, count);
}
if (index == 8) {
Serial.print(" ");
}
Serial.print(" ");
hexa(memory.bus[BUS_Q].data, 2);
}
// Blank
if (blank && (memory.bus[BUS_Q].data != memory.bus[BUS_Q].high)) {
blank = false;
color = COLOR_KO;
worker.color = COLOR_BAD;
ui_draw_rom();
if (options[option_blank]) {
break;
}
}
++index %= 16;
if (index == 0) {
// Serial
if (options[option_serial]) {
Serial.print(" ");
for (uint8_t index = 0; index < 16; ++index) {
if (index == 8) {
Serial.print(" ");
}
if (iscntrl(char(data[index]))) {
Serial.print('.');
} else {
Debug(char(data[index]));
}
}
Serial.println();
}
// File
if (options[option_file]) {
file.write(data, 16);
}
ui_draw_indicator(color);
}
}
// Shutdown
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_VCC]], LOW);
#if TIME
unsigned long stop = millis();
Serial.print(F(TIME_ELAPSED));
Serial.println((stop - start) / 1000.0);
#endif
// File
if (options[option_file]) {
file.close();
}
}
void ic_rom_idle() {
// Idle
if (ic_memory_signal(SIGNAL_OE)) {
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_OE]], HIGH);
}
ic_pin_write(worker.package.pins[memory.signals[SIGNAL_CS]], HIGH);
}
//////////////////
// Touch screen //
//////////////////
#define TS_MIN 10
#define TS_MAX 1000
#define TS_DELAY 10