Repository navigation
Expand file tree
/
Copy pathramhelpr.asm
More file actions
2388 lines (1987 loc) · 57.2 KB
/
Copy pathramhelpr.asm
File metadata and controls
2388 lines (1987 loc) · 57.2 KB
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
;--- MSX-UNAPI standalone RAM helper/mapper support routines installer
; By Konamiman
;
; See USAGE_S for usage instructions.
;
; You can compile it with Nestor80 (https://github.com/Konamiman/Nestor80):
;
; N80 ramhelpr.asm ramhelpr.com --define-symbols INSTALL_MSR=0
; N80 ramhelpr.asm msr.com --define-symbols INSTALL_MSR=1
;
; Use INSTALL_MSR=1 to build an installer for the DOS 2 mapper support routines and the UNAPI RAM helper,
; In that case the installer will fail if these routines are already present.
;
; Use INSTALL_MSR=0 to build an installer for the UNAPI RAM helper only.
;
; The resulting file is a MSX-DOS .COM program that installs the routines or the helper.
;
; Optional improvements (up to you):
;
; - Add code for uninstallation.
;
; * Version 1.1 copies the contents of the SHELL environment item
; to the PROGRAM environment item.
; This is needed because the SHELL item value becomes RAMHELPR.COM,
; and this leads to "Wrong version of COMMAND" errors.
;
; * Version 1.2:
; - Doesn't read from the mapped RAM ports anymore.
; - The routine to read a byte from a slot+segment doesn't corrupt BC, DE and HL,
; being then compliant with the UNAPI specification.
; - Installs also the DOS 2 mapper support routines if compiled with INSTALL_MSR=1
;
; NOTE: MSR = Standard mapper support routines,
; as defined in MSX-DOS 2 Program Interface Specification
;*******************
;*** CONSTANTS ***
;*******************
ifndef INSTALL_MSR
.fatal Assemble with --define-symbols INSTALL_MSR=0 or --define-symbols INSTALL_MSR=1
endif
if INSTALL_MSR eq 0
.print1 INSTALL_MSR is 0, assembling UNAPI RAM helper only installer
else
.print1 INSTALL_MSR is {INSTALL_MSR}, assembling UNAPI RAM helper + MSR installer
endif
;--- System variables and routines
_TERM0: equ 00h
_STROUT: equ 09h
_GENV: equ 6Bh
_SENV: equ 6Ch
ENDTPA: equ 0006h
RDSLT: equ 000Ch
CALSLT: equ 001Ch
ENASLT: equ 0024h
RAMSLOT0: equ 0F341h
RAMSLOT1: equ 0F342h
RAMSLOT3: equ 0F344h
HIMSAV: equ 0F349h
EXPTBL: equ 0FCC1h
EXTBIO: equ 0FFCAh
;*****************************
;*** INITIALIZATION CODE ***
;*****************************
org 100h
;--- Show welcome message
ld de,WELCOME_S
ld c,_STROUT
call 5
;--- Copy SHELL environment item to PROGRAM
; (in DOS 2 only, nothing happens in DOS 1)
ld hl,SHELL_S
ld de,8000h
ld b,255
ld c,_GENV
call 5
ld hl,PROGRAM_S
ld de,8000h
ld c,_SENV
call 5
;--- Put a 0 at the end of the command line
; (needed when running DOS 1)
ld hl,(0080h)
ld h,0
ld bc,0081h
add hl,bc
ld (hl),0
;--- Search the parameter
ld hl,80h
SRCHPAR:
inc hl
ld a,(hl)
cp " "
jr z,SRCHPAR
or a
jr z,SHOWINFO
or 32
if INSTALL_MSR
cp "c"
jp z,DO_CLEANUP
endif
cp "f"
jr z,PARAM_OK
cp "i"
jr nz,SHOWINFO
;Parameter is "i": check if already installed
push hl
ld a,0FFh
ld de,2222h
ld hl,0
call EXTBIO
ld a,h
or l
pop hl
jr z,PARAM_OK
ld de,RH_ALINST_S
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
;--- No parameters or invalid parameter:
; show usage information and terminate
SHOWINFO:
ld de,USAGE_S
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
;--- Parameters OK: Do install
PARAM_OK:
inc hl
NEXT_PARAM:
ld a,(hl)
or a
jr z,DO_INSTALL
cp " "
jr nz,DO_INSTALL
inc hl
jr NEXT_PARAM
DO_INSTALL:
ld (CMDLINE_PNT),hl
if INSTALL_MSR
xor a ;Get mapper support routines
ld de,0402h
call EXTBIO
or a
jp z,INST_HELPER
ld de,MSR_ALINST_S
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
;************************************
;*** CLEANUP USER MODE SEGMENTS ***
;************************************
DO_CLEANUP:
xor a
ld de,0402h
call EXTBIO
or a
ld de,NOMSR_S
jp z,PRINT_END
inc hl
inc hl
inc hl ;Point HL to FREE_SEG
ld a,0FFh
ld b,00Fh
ld de,DO_CLEANUP_NEXT
push de
jp (hl)
DO_CLEANUP_NEXT:
ld de,NOMYMSR_S
jp c,PRINT_END
ld a,b
or c
ld de,IHAVENTFREED_S
jp z,PRINT_END
push bc
ld de,IHAVEFREED_S
ld c,_STROUT
call 5
pop hl
ld de,FREEDSEGCOUNT_S
push de
call NUMTOASC
pop de
ld c,_STROUT
call 5
ld de,USERSEGMENTS_S
PRINT_END:
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
NOMSR_S:
db "*** No mapper support routines are installed.",13,10,"$"
NOMYMSR_S:
db "*** I haven't installed the mapper support routines myself,",13,10
db " therefore I can't free user segments.",13,10,"$"
IHAVENTFREED_S:
db "I haven't found any segment allocated in user mode.",13,10,"$"
IHAVEFREED_S:
db "I have freed $"
USERSEGMENTS_S:
db " segments allocated in user mode.",13,10,"$"
FREEDSEGCOUNT_S:
ds 6
endif
;******************************************
;*** MSR+RAM HELPER INSTALLATION CODE ***
;******************************************
INST_HELPER:
;--- Check that TPA end address is at least 0C2000h
ld a,(ENDTPA+1)
cp 0C2h
jr nc,OK_TPA
ld de,NOTPA_S
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
OK_TPA:
if INSTALL_MSR
;--- If TPA is spread across multiple slots, unify them
ld a,(RAMSLOT0)
ld hl,RAMSLOT3
cp (hl)
jr z,OKTPASLOTS
di
ld a,(RAMSLOT3)
ld h,40h
call ENASLT
ld a,3
out (0FDh),a
ld hl,0
ld de,4000h
ld bc,4000h
ldir
ld a,2
out (0FDh),a
ld h,0
ld a,(RAMSLOT3)
call ENASLT
ld a,(RAMSLOT3)
ld (RAMSLOT0),a
ld (RAMSLOT1),a
ei
ld de,IHAVESETSLOTS_S
ld c,_STROUT
call 5
OKTPASLOTS:
endif
;--- Prepare the two copies of the page 3 code
;>>> Get a copy of old EXTBIO hook
ld hl,EXTBIO
ld de,HOLDEXT__1
ld bc,5
ldir
ld hl,EXTBIO
ld de,HOLDEXT__2
ld bc,5
ldir
;>>> Build the mappers table
if INSTALL_MSR = 0
xor a ;Get mappers table
ld de,0401h
call EXTBIO
or a
jp nz,BUILDP3_DOS2
endif
;>>> Build the mappers table when no MSR are present:
; - If we are going to install MSR, build a MSR compatible
; table where each entry is 8 bytes long.
; - If not, build a reduced table where each entry is 2 bytes long.
BUILDP3_DOS1:
ld ix,MAPTAB__1
ld iy,MAPTAB__2
;Setup mappers entry for the primary mapper
if INSTALL_MSR
ld a,8
else
ld a,2
endif
ld (MAPTAB_SIZE),a
ld a,(RAMSLOT3)
ld h,40h
call ENASLT
ld de,3000h
call MEMTEST1
cp 5
jr nc,OK_PRIMAP
ld a,(RAMSLOT1)
ld h,40h
call ENASLT
ld de,NOMAPPER_S
ld c,_STROUT
call 5
ld c,_TERM0
jp 5
OK_PRIMAP:
if INSTALL_MSR
ld ix,MAPTAB__1+8
ld iy,MAPTAB__2+8
ld (ix-7),a ;Total segments
ld (iy-7),a
sub 5
ld (ix-6),a ;Free segments
ld (iy-6),a
ld a,5
ld (ix-5),a ;Segments allocated for system: our own code + TPA
ld (iy-5),a
xor a
ld (ix-4),a ;Segments allocated for user
ld (iy-4),a
ld (ix-3),a ;Unused
ld (iy-3),a
ld (ix-2),a ;Unused
ld (iy-2),a
ld (ix-1),a ;Unused
ld (iy-1),a
ld a,(RAMSLOT3)
ld (ix-8),a
ld (iy-8),a
else
ld ix,MAPTAB__1+2
ld iy,MAPTAB__2+2
dec a
ld (ix-1),a
ld (iy-1),a
ld a,(RAMSLOT3)
ld (ix-2),a
ld (iy-2),a
endif
;Setup mappers entry for other mappers, if any
ld b,3
BUILDP3_DOS1_L:
push bc
call NEXTSLOT
pop bc
cp 0FFh
jp z,END_BUILD_MAPTAB ;No more mappers
push ix
push iy
ld hl,RAMSLOT3
cp (hl)
jr z,BUILDP3_DOS1_N
ld c,a
push bc
ld h,40h
call ENASLT
ld de,3000h
call MEMTEST1
pop bc
cp 2
jr c,BUILDP3_DOS1_N
if INSTALL_MSR = 0 ;For MSR it's "segments count", for RH it's "number of last segment"
dec a
endif
pop iy
pop ix
ld (ix),c
ld (iy),c
ld (ix+1),a
ld (iy+1),a
if INSTALL_MSR
ld (ix+2),a ;Free segments
ld (iy+2),a
xor a
ld (ix+3),a ;Segments allocated to system
ld (iy+3),a
ld (ix+4),a ;Segments allocated to system
ld (iy+4),a
ld (ix+5),a ;Unused
ld (iy+5),a
ld (ix+6),a
ld (iy+6),a
ld (ix+7),a
ld (iy+7),a
push bc
ld bc,8
add ix,bc
add iy,bc
ld a,(MAPTAB_SIZE)
ld b,8
add a,b
ld (MAPTAB_SIZE),a
pop bc
else
inc ix
inc ix
inc iy
inc iy
ld hl,MAPTAB_SIZE
inc (hl)
inc (hl)
endif
djnz BUILDP3_DOS1_L
END_BUILD_MAPTAB:
ld (ix),0 ;End of the table
ld (iy),0
ld hl,MAPTAB_SIZE
inc (hl)
jr END_BUILDP3
BUILDP3_DOS1_N:
pop iy
pop ix
jp BUILDP3_DOS1_L
if INSTALL_MSR = 0
;>>> Build the mappers table when MSR are already present
BUILDP3_DOS2:
ld (_CALLRAM_MAPTAB__1+1),hl
ld (_CALLRAM_MAPTAB__2+1),hl
ld a,7 ;Opcode for RLCA
ld (_CALLRAM_RLCAS__1),a
ld (_CALLRAM_RLCAS__1+1),a
ld (_CALLRAM_RLCAS__2),a
ld (_CALLRAM_RLCAS__2+1),a
xor a ;Get mapper support routines
ld de,0402h
call EXTBIO
ld bc,1Eh
add hl,bc
push hl
ld de,PUT_P1__1
ld bc,6
ldir
pop hl
ld de,PUT_P1__2
ld bc,6
ldir
ld hl,0
ld (LDBC__1+1),hl
ld (LDBC__2+1),hl
endif
END_BUILDP3:
if INSTALL_MSR
ld hl,0 ;We don't provide reduced mappers table,
ld (LDBC__1+1),hl ;we've generated a standard table
ld (LDBC__2+1),hl
endif
;--- Calculate final size of page 3 area
ld bc,(MAPTAB_SIZE)
ld b,0
ld hl,P3_CODE_END__1-P3_CODE__1
add hl,bc
ld (0B002h),hl
;--- Allocate space on system work area on page 3
ld hl,(HIMSAV)
ld bc,(0B002h)
or a
sbc hl,bc
ld (HIMSAV),hl
ld a,(RAMSLOT1)
ld h,40h
call ENASLT
;--- Generate the page 3 code for the appropriate address,
; but generate it at 0B004h temporarily
; (we cannot use the allocated page 3 space yet)
ld hl,P3_CODE__1
ld de,P3_CODE__2
ld ix,(HIMSAV)
ld iy,0B004h
ld bc,(0B002h)
call REALLOC
ld hl,(HIMSAV)
ld (0B000h),hl
;--- Install code in segment
if INSTALL_MSR
ld a,(MAPTAB__1)
ld h,40h
call ENASLT
ld a,(MAPTAB__1+1)
dec a
out (0FDh),a
ld hl,SEGMENT_CODE
ld de,4000h
ld bc,SEGMENT_CODE_END-SEGMENT_CODE_START
ldir
;>>> Initialize the segments usage table, marking non-existing segments
ld hl,SEG_USE_TABLE
ld de,SEG_USE_TABLE+1
ld bc,256*4-1
ld (hl),0
ldir
ld ix,MAPTAB__1
ld hl,SEG_USE_TABLE+255
INI_SEG_TABLE_LOOP:
ld a,(ix)
or a
jr z,INI_SEG_TABLE_END
push ix
push hl
ld c,(ix+1) ;Segments count
ld b,0
ld hl,256
or a
sbc hl,bc
ld b,l ;Number of non-existing segments
ld a,0FFh
pop hl
push hl
MARK_NON_EX_LOOP:
ld (hl),a
dec hl
djnz MARK_NON_EX_LOOP
pop hl
pop ix
inc h ;Next entry in SEG_USE_TABLE
ld bc,8
add ix,bc
jr INI_SEG_TABLE_LOOP
;>>> Also mark TPA segments and our own segment as system
INI_SEG_TABLE_END:
ld ix,SEG_USE_TABLE
ld a,2
ld (ix),a
ld (ix+1),a
ld (ix+2),a
ld (ix+3),a
ld bc,(MAPTAB__1+1)
dec c
ld b,0
add ix,bc
ld (ix),a
ld a,2
out (0FDh),a
ld a,(0F342h)
ld h,40h
call ENASLT
endif
;--- All done, now we need to jump to BASIC and do _SYSTEM
ld de,OK_S
ld c,_STROUT
call 5
ld ix,ComLine+2
ld hl,(CMDLINE_PNT) ;No commands to copy?
ld a,(hl)
or a
jr z,OKBSC
ld a,(0F313h) ;DOS 1 and there are commands to copy:
or a ;print warning and ignore them
jr nz,DO_GET_CMD
ld de,CMDWARNING_S
ld c,_STROUT
call 5
jr OKBSC
DO_GET_CMD:
ld (ix-2),"("
ld (ix-1),34
ld a,(hl)
BUCSYS2:
ld (ix),a ;Copy characters until finding 0
inc ix
inc hl
ld a,(hl)
cp "&"
jr nz,NOAMP
ld a,"^"
NOAMP: or a
jr nz,BUCSYS2
ld (ix),34
ld (ix+1),")"
ld (ix+2),0
OKBSC:
ld hl,SystemProg
ld de,08000h
ld bc,0400h
ldir
jp 08000h
SystemProg:
ld a,(0FCC1h)
push af
ld h,0
call 024h
pop af
ld h,040h
call 024h
xor a
ld hl,0F41Fh
ld (0F860h),hl
ld hl,0F423h
ld (0F41Fh),hl
ld (hl),a
ld hl,0F52Ch
ld (0F421h),hl
ld (hl),a
ld hl,0F42Ch
ld (0F862h),hl
ld hl,8030h
jp 04601h
;The following is copied to address 8030h
SysTxT2:
db 3Ah ;:
db 97h,0DDh,0EFh ;DEF USR =
db 0Ch
SigueDir:
dw UsrCode-SystemProg+8000h ;Address of "UsrCode"
db 3Ah,91h,0DDh,"(",34,34,")" ;:? USR("")
db 03Ah,0CAh ;:CALL
db "SYSTEM"
ComLine:
db 0
ds 128 ;Space for extra commands
;>>> This is the code that will be executed by the USR instruction
UsrCode:
;--- Copy page 3 code to its definitive address
ld hl,0B004h
ld de,(0B000h)
ld bc,(0B002h)
ldir
;--- Setup new EXTBIO hook
di
ld a,0C3h
ld (EXTBIO),a
ld hl,(0B000h)
ld bc,NEWEXT__1-P3_CODE__1
add hl,bc
ld (EXTBIO+1),hl
ei
ret
;--- This routine reallocates a piece of code
; based on two different copies assembled in different locations.
; Input: HL = Address of first copy
; DE = Address of second copy
; IX = Target reallocation address
; IY = Address where the patched code will be placed
; BC = Length of code
REALLOC:
push iy
push bc
push de
push hl ;First copy code "as is"
push iy ;(HL to IY, length BC)
pop de
ldir
pop hl
pop de
push de
pop iy ;IY = Second copy
ld b,h
ld c,l
push ix
pop hl
or a
sbc hl,bc
ld b,h
ld c,l ;BC = Distance to sum (IX - HL)
exx
pop bc
exx
pop ix ;Originally IY
;At this point: IX = Destination
; IY = Second copy
; BC = Distance to sum (new dir - 1st copy)
; BC'= Length
REALOOP:
ld a,(ix)
cp (iy)
jr z,NEXT ;If no differences, go to next byte
ld l,a
ld h,(ix+1) ;HL = Data to be changed
add hl,bc ;HL = Data changed
ld (ix),l ;IX = Address of the data to be changed
ld (ix+1),h
call CHKCOMP
jr z,ENDREALL
inc ix
inc iy
NEXT: inc ix ;Next byte to compare
inc iy ;(if we have done substitution, we need to increase twice)
call CHKCOMP
jr nz,REALOOP
ENDREALL:
ret
CHKCOMP:
exx
dec bc ;Decrease counter, if it reaches 0,
ld a,b ;return with Z=1
or c
exx
ret
;--- NEXTSLOT:
; Returns in A the next slot available on the system every time it is called.
; When no more slots are available it returns 0FFh.
; To initialize it, 0FFh must be written in NEXTSL.
; Modifies: AF, BC, HL
NEXTSLOT:
ld a,(NEXTSL)
cp 0FFh
jr nz,NXTSL1
ld a,(EXPTBL) ;First slot
and 10000000b
ld (NEXTSL),a
ret
NXTSL1:
ld a,(NEXTSL)
cp 10001111b
jr z,NOMORESLT ;No more slots?
cp 00000011b
jr z,NOMORESLT
bit 7,a
jr nz,SLTEXP
SLTSIMP:
and 00000011b ;Simple slot
inc a
ld c,a
ld b,0
ld hl,EXPTBL
add hl,bc
ld a,(hl)
and 10000000b
or c
ld (NEXTSL),a
ret
SLTEXP:
ld c,a ;Expanded slot
and 00001100b
cp 00001100b
ld a,c
jr z,SLTSIMP
add 00000100b
ld (NEXTSL),a
ret
NOMORESLT:
ld a,0FFh
ret
NEXTSL:
db 0FFh ;Last returned slot
;--- Direct memory test (for DOS 1)
; INPUT: DE = 256 byte buffer, it must NOT be in page 1
; The slot to be tested must be switched on page 1
; OUTPUT: A = Number of available segments, or:
; 0 -> Error: not a RAM slot
; 1 -> Error: not mapped RAM
; MODIFIES: F, HL, BC, DE
MEMTEST1:
ld a,(5001h) ;Check if it is actually RAM
ld h,a
cpl
ld (5001h),a
ld a,(5001h)
cpl
ld (5001h),a
cpl
cp h
ld a,0
ret z
ld hl,4001h ;Test address
in a,(0FDh)
push af ;A = Current page 2 segment
push de ;DE = Buffer
ld b,0
;* Loop for all segment numbers from 0 to 255
MT1BUC1:
ld a,b ;Switch segment on page 1
out (0FDh),a
ld a,(hl)
ld (de),a ;Save data on the test address...
ld a,b
ld (hl),a ;...then write the supposed segment number on test address
inc de
inc b
ld a,b
cp 0
jr nz,MT1BUC1
;* Negate the value of test address for segment 0:
; this is the number of segments found (0 for 256)
out (0FDh),a
ld a,(hl)
neg
ld (NUMSGS),a
ld b,0
ld c,a
pop de
;* Restore original value of test address for all segments
MT1BUC2:
ld a,b
out (0FDh),a
ld a,(de)
ld (hl),a
inc de
inc b
ld a,b
cp c
jr nz,MT1BUC2
;* Restore original segment on page 1 and return number of segments
pop af
out (0FDh),a
ld a,(NUMSGS)
cp 1 ;No mapped RAM?
jr z,NOMAP1
or a
ret nz
ld a,0FFh ;If 256 segments, return 255
ret
NOMAP1: xor a
ret
NUMSGS: db 0
;--- NUMTOASC: Converts a 16 bit unsigned value to an ASCII string,
; terminating it with a "$" character
; Input: HL = Number to convert
; DE = Destination address for the string
;
; This routine is a modification of one borrowed from:
; http://map.tni.nl/sources/external/z80bits.html
; (this one skips dummy zeros and adds "$" at the end)
NUMTOASC:
;* HL=0 is a special case
ld a,h
or l