Skip to content

Commit 69f1c64

Browse files
committed
feat: add memorybank.bas library
1 parent d006f46 commit 69f1c64

3 files changed

Lines changed: 245 additions & 0 deletions

File tree

examples/memory_banking.bas

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
' ---------------------------------------------------------
2+
' - MemoryBank.bas library Test ---------------------------
3+
' ---------------------------------------------------------
4+
' Compile at 24576
5+
6+
7+
#include "memorybank.bas"
8+
9+
10+
Main()
11+
DO:LOOP
12+
13+
14+
SUB Main()
15+
CLS
16+
Test_SetBank()
17+
PAUSE 0
18+
CLS
19+
SetBankSample()
20+
END SUB
21+
22+
23+
SUB Test_SetBank()
24+
DIM n, b AS UByte
25+
26+
' SetBank and GetBank
27+
FOR n = 0 TO 7
28+
PRINT AT n+1,0;"Bank: ";n;
29+
SetBank(n)
30+
PRINT ">";GetBank();
31+
POKE $c000,n
32+
NEXT n
33+
34+
' Test banks
35+
FOR n = 0 TO 7
36+
PRINT AT n+1,9;">";n;
37+
SetBank(n)
38+
b = PEEK($c000)
39+
IF b = n THEN
40+
PRINT ">OK";
41+
ELSE
42+
PRINT ">ERROR";
43+
END IF
44+
NEXT n
45+
46+
' SetCodeBank
47+
FOR n = 0 TO 7
48+
PRINT AT n+1,16;">";n;
49+
SetCodeBank(n)
50+
IF n = 2 THEN
51+
PRINT ">SKIP";
52+
ELSE
53+
b = PEEK($8000)
54+
PRINT ">";b;
55+
IF b = n THEN
56+
PRINT ">OK";
57+
ELSE
58+
PRINT ">ERROR";
59+
END IF
60+
END IF
61+
NEXT n
62+
END SUB
63+
64+
65+
SUB SetBankSample()
66+
DIM n, b AS UByte
67+
68+
' Fill banks with data
69+
FOR n = 0 TO 7
70+
SetBank(n)
71+
PRINT AT n,0;"Bank: ";n;
72+
POKE $c000,n
73+
NEXT n
74+
75+
' Read banks
76+
FOR n = 0 TO 7
77+
SetBank(n)
78+
PRINT AT n,10;PEEK($c000);
79+
NEXT n
80+
81+
END SUB
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2023
5+
' by Juan Segura (a.k.a. Duefectu) <http://zx.duefectucorp.com>
6+
'
7+
' Memory bank switch tools
8+
' ----------------------------------------------------------------
9+
10+
#ifndef __LIBRARY_MEMORYBANK__
11+
12+
REM Avoid recursive / multiple inclusion
13+
14+
#define __LIBRARY_MEMORYBANK__
15+
16+
17+
' ----------------------------------------------------------------
18+
' Place the bank indicated by bankNumber in the memory slot
19+
' between $c000 and $ffff and updates the system variable BANKM.
20+
' Only works on 128K and compatible models.
21+
' Danger: If our program exceeds the address $c000 it may cause
22+
' problems, use this function at your own risk.
23+
' Parameters:
24+
' bankNumber (UByte): Bank number to place at $c000
25+
' ----------------------------------------------------------------
26+
SUB FASTCALL SetBank(bankNumber AS UByte)
27+
ASM
28+
; A = bankNumber to place at $c000
29+
ld d,a ; D = bankNumber
30+
ld a,($5b5c) ; Read BANKM system variable
31+
and %11111000 ; Reset bank bits
32+
or d ; Set bank bits to bankNumber
33+
ld bc,$7ffd ; Memory Bank control port
34+
di ; Disable interrupts
35+
ld ($5b5c),a ; Update BANKM system variable
36+
out (c),a ; Set the bank
37+
ei ; Enable interrupts
38+
END ASM
39+
END SUB
40+
41+
42+
' ----------------------------------------------------------------
43+
' Returns the memory bank located at $c000 based on the system
44+
' variable BANKM.
45+
' Only works on 128K and compatible models.
46+
' Returns:
47+
' UByte: Bank number placed at $c000
48+
' ----------------------------------------------------------------
49+
FUNCTION FASTCALL GetBank() AS UByte
50+
RETURN PEEK $5b5c bAND %111
51+
END FUNCTION
52+
53+
54+
' ----------------------------------------------------------------
55+
' Place the bank indicated by bankNumber in the memory slot
56+
' between $c000 and $ffff, copy the contents to $8000-$bfff and
57+
' restore the bank that was at $c000 before you started.
58+
' Only works on 128K and compatible models.
59+
' Danger: The contents of memory located between $8000 and $bfff
60+
' are lost, and if our program exceeds the address $8000 it may
61+
' cause problems, use this function at your own risk.
62+
' Parameters:
63+
' bankNumber (UByte): Bank number to place at $8000
64+
' ----------------------------------------------------------------
65+
SUB SetCodeBank(bankNumber AS UByte)
66+
DIM b AS UByte
67+
68+
b = GetBank()
69+
SetBank(bankNumber)
70+
71+
ASM
72+
; Copy from $c000-$ffff to $8000-$bfff
73+
ld hl,$c000
74+
ld de,$8000
75+
ld bc,$4000
76+
ldir
77+
END ASM
78+
79+
SetBank(b)
80+
END SUB
81+
82+
#endif
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2023
5+
' by Juan Segura (a.k.a. Duefectu) <http://zx.duefectucorp.com>
6+
'
7+
' Memory bank switch tools
8+
' ----------------------------------------------------------------
9+
10+
#ifndef __LIBRARY_MEMORYBANK__
11+
12+
REM Avoid recursive / multiple inclusion
13+
14+
#define __LIBRARY_MEMORYBANK__
15+
16+
17+
' ----------------------------------------------------------------
18+
' Place the bank indicated by bankNumber in the memory slot
19+
' between $c000 and $ffff and updates the system variable BANKM.
20+
' Only works on 128K and compatible models.
21+
' Danger: If our program exceeds the address $c000 it may cause
22+
' problems, use this function at your own risk.
23+
' Parameters:
24+
' bankNumber (UByte): Bank number to place at $c000
25+
' ----------------------------------------------------------------
26+
SUB FASTCALL SetBank(bankNumber AS UByte)
27+
ASM
28+
; A = bankNumber to place at $c000
29+
ld d,a ; D = bankNumber
30+
ld a,($5b5c) ; Read BANKM system variable
31+
and %11111000 ; Reset bank bits
32+
or d ; Set bank bits to bankNumber
33+
ld bc,$7ffd ; Memory Bank control port
34+
di ; Disable interrupts
35+
ld ($5b5c),a ; Update BANKM system variable
36+
out (c),a ; Set the bank
37+
ei ; Enable interrupts
38+
END ASM
39+
END SUB
40+
41+
42+
' ----------------------------------------------------------------
43+
' Returns the memory bank located at $c000 based on the system
44+
' variable BANKM.
45+
' Only works on 128K and compatible models.
46+
' Returns:
47+
' UByte: Bank number placed at $c000
48+
' ----------------------------------------------------------------
49+
FUNCTION FASTCALL GetBank() AS UByte
50+
RETURN PEEK $5b5c bAND %111
51+
END FUNCTION
52+
53+
54+
' ----------------------------------------------------------------
55+
' Place the bank indicated by bankNumber in the memory slot
56+
' between $c000 and $ffff, copy the contents to $8000-$bfff and
57+
' restore the bank that was at $c000 before you started.
58+
' Only works on 128K and compatible models.
59+
' Danger: The contents of memory located between $8000 and $bfff
60+
' are lost, and if our program exceeds the address $8000 it may
61+
' cause problems, use this function at your own risk.
62+
' Parameters:
63+
' bankNumber (UByte): Bank number to place at $8000
64+
' ----------------------------------------------------------------
65+
SUB SetCodeBank(bankNumber AS UByte)
66+
DIM b AS UByte
67+
68+
b = GetBank()
69+
SetBank(bankNumber)
70+
71+
ASM
72+
; Copy from $c000-$ffff to $8000-$bfff
73+
ld hl,$c000
74+
ld de,$8000
75+
ld bc,$4000
76+
ldir
77+
END ASM
78+
79+
SetBank(b)
80+
END SUB
81+
82+
#endif

0 commit comments

Comments
 (0)