|
| 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