-
Notifications
You must be signed in to change notification settings - Fork 31
/
membrowser.asm
1126 lines (845 loc) · 13.7 KB
/
membrowser.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.
;
; membrowser.asm
;
define KEY_DELAY 10
mem_browser
ld sp, sys_stack
call initialize
ld hl, ld_a_hl
ld de, sys_ld_a_hl
ld bc, ld_a_hl_end-ld_a_hl
ldir
ld hl, ld_buffer_8_bytes_hl
ld de, sys_ld_buffer_8_bytes_hl
ld bc, ld_buffer_8_bytes_hl_end-ld_buffer_8_bytes_hl
ldir
; Set the correct interrupt service routine
ld hl, membrowser_isr
ld (v_userint), hl
ld a, BORDERWHT
out (ULA_PORT), a
call cls
ld hl, str_mem_browser_header
call print_header
ld hl, str_mem_browser_footer
call print
ld hl, str_break_to_exit
call print
; Paint initial memory locations
; HL is our starting address.
ld hl, 0x4000
; IX is our cursor location.
; IXh = row, IXl = column * 2, e.g. IXl=3 is low nibble of
; byte 1.
ld ix, 0x0000
call refresh_mem_display
; Set up the keyboard delay
ex af, af'
ld a, 0
ex af, af'
ld c, a
call print_cursor
ei
mem_loop
ex af, af'
ld b, a
ex af, af'
; Delay repeat key presses
ld a, b
cp 0
jr nz, key_delay
ld a, 1
ld b, a
key_delay
halt
djnz key_delay
; Scan the keyboard
xor a
call scan_keys
jr c, mem_key_pressed
; No key pressed, reset the delay timer
ex af, af'
ld a, 0
ex af, af'
jr mem_loop
mem_key_pressed
; A key was pressed, set the timer to the initial delay,
; or reduce the delay by one if not already = 1.
ex af, af'
cp 0
jr z, delay_timer_start
cp 1
jr z, delay_timer_end
dec a
jr delay_timer_end
delay_timer_start
ld a, KEY_DELAY
delay_timer_end
ex af, af'
check_keys
; Movement keys.
cp 'Z'
jp z, page_up
cp 'X'
jp z, page_down
cp KEY_UP
jp z, cur_up
cp KEY_DOWN
jp z, cur_down
cp KEY_LEFT
jp z, cur_left
cp KEY_RIGHT
jp z, cur_right
; Option keys
cp 'P'
jp z, ram_page
cp 'R'
jp z, rom_page
cp 'G'
jp z, goto_addr
cp BREAK
jr z, exit
cp 'H'
jr z, hard_copy
; Test for Hex characters
cp 'F' + 1
jr nc, mem_loop
cp '0'
jr c, mem_loop
cp '9' + 1
jr c, hex_digit
cp 'A'
jr nc, hex_digit
hex_digit
; Normalise it
sub '0'
cp 10
jr c, hex_digit_2
sub 0x7
hex_digit_2
; Work out the target byte
push hl
push af
call get_hl_cursor_addr
write_check_zxc3
; If on ZXC3 hardware, check if
; we're about to write to 3Fc0-3FFF.
; Do not perform the write if so.
ld a, (v_testhwtype)
cp HW_TYPE_ZXC3
jr nz, write_check_dandanator
ld a, h
cp 0x3f
jr nz, write_calc_nibble
ld a, l
cp 0xc0
jr c, write_calc_nibble
pop af
pop hl
jr write_end
write_check_dandanator
ld a, (v_testhwtype)
cp HW_TYPE_DANDANATOR
jr nz, write_calc_nibble
ld a, h
cp 0x40
jr nc, write_calc_nibble
pop af
pop hl
jr write_end
write_calc_nibble
ld a, ixl
bit 0, a
jr z, write_high_nibble
write_low_nibble
call sys_ld_a_hl
and 0xf0
ld b, a
pop af
and 0x0f
jr write_byte
write_high_nibble
call sys_ld_a_hl
and 0x0f
ld b, a
pop af
rla
rla
rla
rla
and 0xf0
write_byte
; Write the byte in question.
; Temporarily lock paging on diagnostic
; hardware that supports it, to avoid a
; write to ROM space triggering a paging
; command.
or b
push hl
push af
ld a, 3
call sys_rompaging
pop af
pop hl
ld (hl), a
ld a, 4
call sys_rompaging
pop hl
call refresh_mem_display
write_end
call cur_right
jp mem_loop
exit
jp diagrom_exit
hard_copy
push hl
call lprint_screen
pop hl
jp mem_loop
; Routine to allow the user to enter a 4 digit
; hexadecimal memory address to go to
goto_addr
; We're replacing the value of HL (memory pointer)
; so it's okay to trash it
ld hl, str_goto_addr_prompt
call print
ld hl, str_cursor
call print
ld bc, 0x0000
call get_hex_digit
sla a
sla a
sla a
sla a
or b
ld b, a
call get_hex_digit
or b
ld b, a
call get_hex_digit
sla a
sla a
sla a
sla a
or c
ld c, a
call get_hex_digit
or c
ld ixl, a
and 0xf8
ld c, a
ld hl, str_goto_addr_default
call print
push bc
pop hl
ld ixh, 0
ld a, ixl
and 0x07
sla a
ld ixl, a
set 0, c
call refresh_mem_display
call print_cursor
jp mem_loop
get_hex_digit
; Get a keypress and see if it's a valid hex digit
xor a
call scan_keys
jr nc, get_hex_digit
cp 'F' + 1
jr nc, get_hex_digit
cp '0'
jr c, get_hex_digit
cp '9' + 1
jr c, got_hex_digit
cp 'A'
jr c, get_hex_digit
got_hex_digit
push af
; Debounce the keypress
get_hex_digit_2
xor a
call scan_keys
jr c, get_hex_digit_2
pop af
; Print the digit
push af
call putchar
ld d, 6
ld a, (v_column)
add a, d
ld (v_column), a
ld hl, str_cursor
call print
pop af
; Normalise the ASCII keypress to a hex digit 0-F
sub '0'
cp 10
ret c
sub 0x7
ret
; Page in whatever ROM the user wants
rom_page
push hl
ld hl, str_selrompage
call print_header
rom_page_sel
xor a
call scan_keys
jr nc, rom_page_sel
cp ' '
jp nz, rom_page_sel_chk
ld hl, str_mem_browser_header
call print_header
pop hl
jp mem_loop
rom_page_sel_chk
cp '0'
jr c, rom_page_sel
cp '4'
jr nc, rom_page_sel
; Normalise to 0-3
sub '0'
push af
; Write 0x1ffd part
and 0x2
rla
ld b, a
ld a, (v_paging_2)
and 0xfb
or b
ld bc, 0x1ffd
out (c), a
ld (v_paging_2), a
; Write 0x7ffd part
pop af
and 0x1
rla
rla
rla
rla
ld b, a
ld a, (v_paging)
and 0xef
or b
ld bc, 0x7ffd
out (c), a
ld (v_paging), a
; Restore header and refresh memory display
ld hl, str_mem_browser_header
call print_header
pop hl
call refresh_mem_display
jp mem_loop
ram_page
push hl
ld hl, str_selrampage
call print_header
ram_page_sel
xor a
call scan_keys
jr nc, ram_page_sel
cp ' '
jp nz, ram_page_sel_chk
ld hl, str_mem_browser_header
call print_header
pop hl
jp mem_loop
ram_page_sel_chk
cp '0'
jr c, ram_page_sel
cp '8'
jr nc, ram_page_sel
; Page in required RAM bank
sub '0'
and 0x7
ld b, a
ld a, (v_paging)
and 0xf8
or b
ld bc, 0x7ffd
out (c), a
ld (v_paging), a
ld hl, str_mem_browser_header
call print_header
pop hl
call refresh_mem_display
jp mem_loop
page_up
and a
ld de, 0x90
sbc hl, de
call refresh_mem_display
call print_cursor
jp mem_loop
page_down
ld de, 0x90
add hl, de
call refresh_mem_display
call print_cursor
jp mem_loop
cur_left
ld c, 0
call print_cursor
ld c, 1
ld a, ixl
cp 0
jr nz, cur_left_normal
ld a, 15
ld ixl, a
ld a, ixh
cp 0
jr nz, cur_left_nopage
call scroll_mem_down
inc ixh
cur_left_nopage
dec ixh
call print_cursor
jp mem_loop
cur_left_normal
dec ixl
call print_cursor
jp mem_loop
cur_right
ld c, 0
call print_cursor
ld c, 1
ld a, ixl
cp 15
jr nz, cur_right_normal
ld a, 0
ld ixl, a
ld a, ixh
cp 0x11
jr nz, cur_right_nopage
call scroll_mem_up
dec ixh
cur_right_nopage
inc ixh
call print_cursor
jp mem_loop
cur_right_normal
inc ixl
call print_cursor
jp mem_loop
cur_up
ld c, 0
call print_cursor
ld c, 1
ld a, ixh
cp 0
jr nz, cur_up_normal
call scroll_mem_down
call print_cursor
jp mem_loop
cur_up_normal
dec ixh
call print_cursor
jp mem_loop
cur_down
ld c, 0
call print_cursor
ld c, 1
ld a, ixh
cp 0x11
jr nz, cur_down_normal
call scroll_mem_up
call print_cursor
jp mem_loop
cur_down_normal
inc ixh
call print_cursor
jp mem_loop
scroll_mem_up
ld de, 8
add hl, de
push hl
ld de, 0x88
add hl, de
push hl
ld hl, 0x1300
call set_print_pos
ld hl, 0x0212
call scroll_up
pop hl
call print_mem_line
pop hl
ret
scroll_mem_down
ld de, 8
and a
sbc hl, de
push hl
push hl
ld hl, 0x1312
call scroll_down
ld hl, 0x0200
call set_print_pos
pop hl
call print_mem_line
pop hl
ret
;
; Refreshes the entire memory display.
;
refresh_mem_display
push af
push bc
ld a, 2
ld (v_row), a
xor a
ld (v_column), a
ld b, 0x12
push hl
init_mem_loop
call print_mem_line
call newline
djnz init_mem_loop
pop hl
pop bc
pop af
ret
;
; Sets ink colour appropriately for the
; memory region being displayed. Red=
; read only, black=read/write.
;
set_mem_line_colour
ld a, 57
ld (v_attr), a
ld a, h
cp 0x40
ret nc
ld a, 58
ld (v_attr),a
ret
;
; Prints a single memory line
; Start address is in HL
; Print position is presumed to be set
;
print_mem_line
; Set red ink if we're in ROM address space
call set_mem_line_colour
ld de, v_hexstr
call Num2Hex
push hl
ld hl, v_hexstr
call print
ld hl, str_colon
call print
pop hl
push bc
ld b, 8
call sys_ld_buffer_8_bytes_hl
push hl
ld hl, v_membrowser_buffer
mem_loop_hex
ld a, (hl)
push hl
push bc
ld l, a
ld h, 0
ld de, v_hexstr + 2
call Byte2Hex
ld hl, v_hexstr + 2
call print
ld a, (v_column)
ld d, 6
add d
ld (v_column), a
pop bc
pop hl
inc hl
djnz mem_loop_hex
pop hl
; Time to output the same 8 bytes in ASCII
ld a, (v_column)
ld d, 12
add d
ld (v_column), a
ld b, 8
push hl
ld hl, v_membrowser_buffer
mem_loop_ascii
ld a, (hl)
cp 32
jr c, control_char
cp 127
jr nc, control_char
call putchar
jr mem_loop_ascii_next
control_char
ld a, '?'
call putchar
mem_loop_ascii_next
ld d, 6
ld a, (v_column)
add d
ld (v_column), a
inc hl
djnz mem_loop_ascii
pop hl
pop bc
; Add 8 bytes to the original HL value to get the next line
ld de, 8
and a
adc hl, de
; Restore attributes for next line
ld a, 56
ld (v_attr), a
ret
;
; Prints cursor and memory value associated with it.
; HL = memory location of top line/leftmost byte
; IX = cursor position (IXH = row, IXL & 0xfe = column,
; IXL bit 0 = nibble within byte)
; C = nonzero, inverse. Zero = normal video
;
print_cursor
push hl
call get_hl_cursor_addr
call set_mem_line_colour
; HL now contains the memory address. Grab it
; and store it in B register
call sys_ld_a_hl
ld b, a
; Now calculate the actual screen address to print
; Offset from top left is 2,7
ld a, 2
ld e, ixh
add e
ld h, a
; IXL = cursor position (8 bytes * 2 nibbles)
ld a, ixl
; Divide by 2 to take account of nibbles
srl a
; Every byte takes 3 print positions ( * 6 )
ld l, a
add l
add l
add l
add l
add l
; (byte * 6) * 3
ld l, a
xor a
add l
add l
add l
ld l, a
; 42 + (byte * 6) * 3
ld a, 7 * 6
add l
ld l, a
; Cursor is now in position to print byte.
call set_print_pos
; Do high nibble first
ld a, b
ld hl, v_pr_ops
res 1, (hl)
rra
rra
rra
rra
call num_2_hex
ld d, ixl
bit 0, d
jr nz, no_inverse_1
bit 0, c
jr z, no_inverse_1
set 1, (hl)
no_inverse_1
call putchar
; Now lower nibble.
res 1, (hl)
ld a, (v_column)
inc a
inc a
inc a
inc a
inc a
inc a
ld (v_column), a
ld a, b
and 0x0f
call num_2_hex
ld d, ixl
bit 0, d
jr z, no_inverse_2
bit 0, c
jr z, no_inverse_2
set 1, (hl)
no_inverse_2
call putchar
res 1, (hl)
pop hl
ld a, 56
ld (v_attr), a
ret
;
; Gets the address of the byte in memory pointed
; to by the cursor represented by IX.
; HL = start screen address, IX=cursor
; Output: HL is cursor byte.
;
get_hl_cursor_addr
; First get memory byte in question in HL
; Work out row position * 8
ld a, ixh
add a
add a
add a
ld e, a
ld d, 0
add hl, de
; Now get offset within row
ld a, ixl
srl a
ld e, a
add hl, de
ret
;
; Converts number in A to its ASCII hex equivalent.
;
num_2_hex
or 0xF0
daa
add a,#A0
adc a,#40
ret
;
; Waits until no keys are being pressed.
; Designed to be jumped to from a subrouting
; so it returns to the caller's caller.
;
release_key
push af
rel_key_loop
xor a
in a, (0xfe)
and 0x1f
cp 0x1f
jr nz, rel_key_loop
pop af
ret
membrowser_isr
ret
;
; Routine for relocation to RAM. Loads A with memory location in HL.
; Pages out ROM to do it.
; Checks to see if ZXC4 is active, and if the location points to the
; area between 3FC0-3FFF. It'll return FF's in this case.
;
ld_a_hl
di