-
Notifications
You must be signed in to change notification settings - Fork 31
/
testrommain.asm
2081 lines (1437 loc) · 33.1 KB
/
testrommain.asm
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
;
; ZX Diagnostics - fixing ZX Spectrums in the 21st Century
; https://github.com/brendanalford/zx-diagnostics
;
; Original code by Dylan Smith
; Modifications and 128K support by Brendan Alford
;
; This code is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation;
; version 2.1 of the License.
;
; This code 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
; Lesser General Public License for more details.
;
; testrom.asm
;
;
; Spectrum Diagnostics ROM
;
; v0.1 by Dylan 'Winston' Smith
; v0.2+ modifications and 128K testing by Brendan Alford.
;
define TESTROM
include "defines.asm"
include "version.asm"
; Define the system variable locations in upper RAM
include "vars.asm"
;
; Define a build timestamp
; Also insert defines for Git branch and commit hash
;
LUA ALLPASS
file = io.open("branch.txt", "r")
if (file==nil) then
branch = "(none)"
else
io.input(file)
branch = io.read()
io.close(file)
if (branch==nil) then
branch = "(none)"
end
end
file = io.open("commit.txt", "r")
if (file==nil) then
commit = "(none)"
else
io.input(file)
commit = io.read()
io.close(file)
if (commit==nil) then
commit = "(none)"
end
end
hostname = os.getenv("USERDOMAIN")
if (hostname==nil) then
file = io.open("hostname.txt",r)
if (file==nil) then
hostname = "(unknown)"
else
io.input(file)
hostname = io.read()
io.close(file)
if (hostname==nil) then
hostname = "(unknown)"
end
end
end
sj.insert_define("GIT_BRANCH", '"' .. branch .. '"');
sj.insert_define("GIT_COMMIT", '"' .. commit .. '"');
sj.insert_define("HOSTNAME", '"' .. hostname:lower() .. '"');
sj.insert_define("BUILD_TIMESTAMP", '"' .. os.date("%d/%m/%Y %H:%M:%S") .. '"');
ENDLUA
; First 56 bytes are reserved for the bootstrap/checksum routine
org 0x38
; Define a version string in the dead space between the ROM start and the
; IM1 0x38 restart/NMI/start vector
isr
jp isr_main
; Be careful to change this only in conjunction with changes in vars.asm
BLOCK v_rom_magic_loc-$, 0xFF
str_rommagicstring
defb "TROM"
BLOCK 0x0066-$, 0xff
nmi
; Start the keyboard test routine.
; Handle this via NMI as the user may not be able
; to hold down the K key if the keyboard membrane
; is suspect.
; If D key is held down, jump to the memory browser.
; This facilitates debugging the diagnostics themselves.
ld bc, 0xfdfe
in a, (c)
bit 2, a
jp z, mem_browser
ld hl, 48878
jp keyboard_test
str_build
defb "ZX Diagnostics ", VERSION, "\nBuilt: ", BUILD_TIMESTAMP , 0
str_gitbranch
defb "Branch: ", GIT_BRANCH, 0
str_gitcommit
defb "Commit: ", GIT_COMMIT, 0
str_buildmachine
defb "Host: ", HOSTNAME, 0
; Modify this value (0x00e0) only in tandem with the value of
; start_diags define in testrom.asm
BLOCK 0x0100-$, 0xff
start
; I = 0 to signify basic ISR functionality
ld a, 0
ld i, a
im 1
; Workaround for Unrainer GAL's on 128 machines - force screen
; page to 0 on startup
xor a
ld bc, 0x7ffd
out (c), a
; Blank the screen, (and all lower RAM)
BLANKMEM 16384, 16384, 0
ld a, 0xff
out (LED_PORT), a ; Light all LED's on startup
ld a, 1
out (ULA_PORT), a
; Display splash screen (i.e. line)
ld hl, splash_screen
ld iy, 0x4840
splash_loop
; Copy each line in double-height format
ld de, iy
ld bc, 0x20
ldir
inc iyh
ld de, 0x20
or a
sbc hl, de
ld de, iy
ld bc, 0x20
ldir
inc iyh
; Are we doing the first 8-line block or the second?
ld a, iyl
cp 0x40
jr nz, splash_check_upper
; First block, check if we've got off into the second
ld a, iyh
cp 0x50
jr nz, splash_loop
; Alter offset to second line if so
ld iy, 0x4860
jr splash_loop
splash_check_upper
; On second line, loop if we need to
ld a, iyh
cp 0x4a
jr nz, splash_loop
BLANKMEM 0x5940, 63, 7
; Insert delay here
ld d, 127
ld b, 8
start_loop_outer
ld hl, 0x8000
exx
ld l, 1
BEEP 0x10, 0x0150
exx
start_loop_inner
; Terminate the 5 sec wait if a key is pressed
in a, (0xfe)
and 0x1f
cp 0x1f
jr nz, start_loop_end
dec hl
ld a, h
or l
jr nz, start_loop_inner
ld a, d
out (LED_PORT), a
and a
rr d
djnz start_loop_outer
start_loop_end
; Extinguish the LED's if any are still lit
xor a
out (LED_PORT), a
; Sound a brief tone to indicate tests are starting.
; This also verifies that the CPU and ULA are working.
ld l, 1 ; Border colour to preserve
COLORBEEP 0x48, 0x0450
COLORBEEP 0x23, 0x0150
xor a
out (LED_PORT), a ; Extinguish the LED's
; Tone done, check if space is being pressed
ld bc, 0x7ffe
in a, (c)
; Only interested in SPACE key, start testcard if pressed
bit 0, a
jp z, testcard
; Launch about screen if Symbol shift is pressed
bit 1, a
jp z, about
; Jump to memory browser if M is pressed
bit 2, a
jp z, mem_browser
; Jump to ULA test routine if U key is pressed
ld bc, 0xdffe
in a, (c)
bit 3, a
jp z, ulatest
; Jump to keyboard test if K is pressed
ld bc, 0xbffe
in a, (c)
bit 2, a
jp z, keyboard_test
; Set up for tests
xor a
ld b, a ; using ixh to store a flag to tell us whether upper
; ram is good (if it is we continue testing)
ld c, a
ld ix, bc
ld iy, bc ; iy is our soak test status register
; 0 - no soak test being performed; else
; holds the current iteration of the test
; Test if the S key is being pressed, if true then go into soaktest mode
ld bc, 0xfdfe
in a, (c)
bit 1, a
jr z, enable_soak_test
; IF2 inputs
; These are actioned on port 2 (67890)
; FIRE - activate soak test
; LEFT - activate test card
; RIGHT - activate ULA test
; Test if FIRE on Sinclair IF2 Port 1 is being pressed (or 0), if true launch soaktest mode
ld bc, 0xeffe
in a, (c)
bit 0, a
jr z, enable_soak_test
bit 3, a
jp z, ulatest
bit 4, a
jp z, testcard
; Andrew Bunker special :)
ld bc, 0xff00
kemp_interface_test
; Check to see if the upper three bits of the Kempston port are
; at any time non-zero - this would indicate a faulty interface or
; one that's not present.
in a, (0x1f)
ld c, a
and 0xe0
; Are amy of the top bits set?
cp 0
jr nz, start_testing
; Loop a bit to make sure
djnz kemp_interface_test
; Interface is present.
; Perform actions based on the same controls as the IF2:
; FIRE - activate soak test
; LEFT - activate test card
; RIGHT - activate ULA test
bit 0, c
jp nz, ulatest
bit 1, c
jp nz, testcard
bit 4, c
jr nz, enable_soak_test
; No options selected, start normal testing
jr start_testing
enable_soak_test
; User is holding Fire on a Kempston stick, enable soak tests.
ld iy, 1 ; Soak testing - start at iteration 1
BEEP 0x10, 0x300
start_testing
; Blue border - signal no errors (yet)
ld a, BORDERGRN
out (ULA_PORT), a
; Same for LED's - all off signifies no errors
xor a
out (LED_PORT), a
; Set all RAM to zero.
BLANKMEM 16384, 49152, 0
; Remove comment to bypass lower RAM test
;jp use_uppermem
; Start lower RAM 'walking bit' test
lowerram_walk
WALKLOOP 16384,16384
; Then the inversion test
lowerram_inversion
ALTPATA 16384, 16384, 0
ALTPATA 16384, 16384, 255
ALTPATB 16384, 16384, 0
ALTPATB 16384, 16384, 255
lowerram_march
MARCHTEST 16384, 16384
; Lastly the Random fill test
lowerram_random
RANDFILLUP 16384, 8192, 0
RANDFILLDOWN 32766, 8191, 255
; This gives the opportunity to visually see what's happening in
; lower memory in case there is a problem with it.
; Conveniently, if there's some lower RAM, then this'll give us
; a pattern to lock onto with the floating bus sync test.
BLANKMEM 16384, 6144, 0
; Attributes - white screen, blank ink.
BLANKMEM 22528, 768, 56
; Check if lower ram tests passed
ld a, ixh
cp 0
jp z, use_uppermem
; Lower memory is no good, give up now.
; We won't be able to test anything else effectively.
; Finish with painting border with bad bits: black border
; with red stripes for failed IC's, green for good ones.
; Topmost stripe is bit 0, lowermost is bit 7.
; Set diag board LED's to outline failed IC's
ld de, ix
ld a, d
out (LED_PORT), a
lower_ram_fail
;
; Paint the RAM FAIL message on screen
; Set a black border here too
;
ld hl, 0x5880
ld de, 0x5881
ld bc, 0x1ff
ld (hl), 0
ldir
ld hl, fail_ram_bitmap
ld de, 0x5880
ld b, 0x40
fail_msg_loop
ld c, 8
ld a, (hl)
fail_msg_byte
bit 7, a
jr z, fail_msg_next
ex de, hl
ld (hl), 0x7f
ex de, hl
fail_msg_next
inc de
rla
dec c
jr nz, fail_msg_byte
inc hl
dec b
jr nz, fail_msg_loop
; Blank out the working RAM digits
ld hl, 0x5980
ld c, ixh
ld d, 8
fail_bits_outer_loop
ld b, 8
fail_bits_loop
bit 0, c
jr nz, fail_bits_ok
ld a, 0
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
inc hl
jr fail_bits_next
fail_bits_ok
inc hl
inc hl
inc hl
inc hl
fail_bits_next
rrc c
djnz fail_bits_loop
dec d
ld a, d
cp 0
jr nz, fail_bits_outer_loop
; Use L as a frame counter
ld l, 0
;
; Lower RAM failure detected, default ISR with I=0
; jumps to routine that paints the bits in the border.
; This'll work on a machine with no RAM and no floating bus.
;
ei
halt
; Called from the ISR we just pointed at.
; Start painting border, start with black
fail_border
; Lose the return address, YAGNI
pop bc
; Failed bitmap is in IXH, transfer to DE
ld de, ix
ld a, d
cp 0
jr nz, fail_border_init
; Don't paint stripes if all RAM has passed
; (We've ended up here from one of the other
; test routines that's done a cursory RAM check)
ei
halt
fail_border_init
; Starting border black until we need stripes
ld a, 0
out (ULA_PORT), a
; Add a small delay so that the stripes begin when
; paper begins
ld a, 0x24
ld c, a
ld a, 0x2
ld b, a
fail_border_wait
dec bc
ld a, b
or c
jr nz, fail_border_wait
fail_border_1
xor a
ld c, a
; Change border to green or red depending on whether the current
; bit has been determined bad or not
fail_border_2
ld a, 4
bit 0, d
jr z, fail_border_3
ld a, 2
; Output the status colour for this bit
fail_border_3
out (ULA_PORT), a
ld a, 0xff
ld b, a
fail_border_4
djnz fail_border_4
; Change back to black for gap between stripes
ld a, l
and 0xf8
out (ULA_PORT), a
ld a, 0xa8
ld b, a
fail_border_5
djnz fail_border_5
rr d
inc c
ld a, c
cp 8
jr nz, fail_border_2
; Done, now delay a little
ld bc, 0x40
fail_border_6
dec bc
ld a, c
or b
jr nz, fail_border_6
fail_border_7
;
; Check if we're doing a soak test
;
ld a, iyh
or iyl
jr z, fail_border_end_no_soak
;
; Yes, output an additional yellow stripe to signify this
;
ld a, BORDERYEL
out (ULA_PORT), a
ld a, 0x8a
ld b, a
fail_border_8
djnz fail_border_8
ld a, 0
out (ULA_PORT), a
inc l
; And repeat for next frame - enable ints and wait for and interrupt
; to carry us back
fail_border_end_no_soak
ld de, ix
ei
halt
;
; Upper / 128K RAM Testing
;
; We can initialise the screen/system vars now that we have verified
; lower RAM and can create a stack.
use_uppermem
; Clear the rest of lower memory we just trashed
BLANKMEM 0x5b00, 0x2500, 0
; Init stack
ld sp, sys_stack
; Initialize system variables without performing additional RAM checks
call initialize_no_ram_check
; Clear the screen and print the top and bottom banners
ld a, BORDERWHT
out (ULA_PORT), a
call cls
ld hl, str_banner
call print_header
ld hl, str_lowerrampass
call print
; Are we in a soak test?
ld a, iyh
or iyl
jr z, print_testrom_footer
; Yes, print the current iteration
ld hl, str_soaktest
call print
ld hl, iy
ld de, v_decstr
call Num2Dec
ld hl, v_decstr
call print
jr rom_test
print_testrom_footer
call print_footer
rom_test
; Perform some ROM checksum testing to determine what
; model we're running on
; Assume 128K toastrack (so far)
xor a
ld (v_128type), a
ld a, (v_testhwtype)
cp 0
jr nz, rom_test_1
; No diagnostic hardware, skip the check
ld (v_column), a
ld hl, str_romdiagboard
call print
jp rom_unknown_2
rom_test_1
; Call CRC generator in RAM, CRC ends up in HL
ld hl, str_romcrc
call print
call sys_romcrc
; Save it in DE temporarily
ld de, hl
ld hl, rom_signature_table
; Check for a matching ROM
rom_check_loop
; Check for 0000 end marker
ld bc, (hl)
ld a, b
or c
jp z, rom_unknown
; Check saved ROM CRC in DE against value in table
ld a, d
xor b
jr nz, rom_check_next
ld a, e
xor c
jr z, rom_check_found
rom_check_next
ld bc, 8
add hl, bc
jr rom_check_loop
rom_check_found
; Print the appropriate ROM type to screen
push hl
inc hl
inc hl
ld de, (hl)
ld hl, de
xor a
ld (v_column), a
call print
pop hl
; Store the address of the testing routine
ld de, 4
add hl, de
ld bc, (hl)
ld (v_test_rtn), bc
; Check if additional ROM tests need to be run (128 machines)
inc hl
inc hl
ld de, (hl)
ld a, d
or e
jr z, rom_test_pass
; Extra ROM tests here
push hl
ld hl, str_testpending
call print
pop hl
; HL points to the ROM check table address
; Pop it into IX for handiness sake
ld de, (hl)
ld ix, de
; D tracks page number
ld d, 1
additional_rom_check_loop
; Page in ROM number (d)
ld a, d
call pagein_rom
push de
call sys_romcrc
pop de
; Check against the value at IX
ld a, (ix)
cp l
jr nz, additional_rom_fail
ld a, (ix + 1)
cp h
jr nz, additional_rom_fail
; This ROM passed, skip ROM fail string pointer
; and increment ROM page
inc d
inc ix
inc ix
inc ix
inc ix
; Any more ROM checksums?
ld b, (ix)
ld a, (ix + 1)
or b
jr nz, additional_rom_check_loop
; All passed, say so and continue with a call to the routine at v_test_rtn
jr rom_test_pass
additional_rom_fail
;
; When adding new ROM signatures, uncomment di/halt below. ROM checksum
; to add will be in HL.
;
; di
; halt
;
ld hl, str_testfail
call print
call newline
ld hl, str_check_rom
call print
ld de, (ix + 2)
ld hl, de
call print
ld hl, str_check_rom_2
call print
; Signify a ROM checksum failure
ld a, 0xff
ld (v_fail_rom), a
jp run_upper_ram_tests
rom_test_pass
; Page in start ROM 0 again
xor a
call pagein_rom
push hl
ld hl, str_testpass
call print
call newline
pop hl
run_upper_ram_tests
; Call the appropriate testing routine
; Set up return address to be the testinterrupts label
ld de, testinterrupts
push de
ld de, (v_test_rtn)
ld hl, de
jp hl
; Unknown ROM, say so and prompt the user for manual selection
rom_unknown
push de
ld hl, str_romunknown
xor a
ld (v_column), a
call print
pop hl
ld de, v_hexstr
call Num2Hex
xor a
ld (v_hexstr+4), a
ld hl, v_hexstr
call print
rom_unknown_2
; Check if we're in soak test mode, if so assume 48K mode
ld a, iyh
or iyl
jr z, rom_unknown_3
; ROM unknown and in soak test, assume 48K
ld hl, str_assume48k
call print
call newline
call test_48kgeneric
jr testinterrupts
rom_unknown_3
; Uncomment to disable user selection
;call test_48k
;jr testinterrupts
; end disable user selection
; Allow user to choose model if ROM version can't be determined
ld hl, str_testselect
call print
; Load HL with 15 secs * 50 frames = 750
; Enable interrupts so we can count accurately.
ld hl, 0x401
ei
select_test
; If more than 30 seconds elapses without input, assume
; 48K mode.
halt