Skip to content

Instantly share code, notes, and snippets.

@S0urceror
Created August 14, 2021 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save S0urceror/63ffbd58b9e58c083c12fb9b071a24d7 to your computer and use it in GitHub Desktop.
Save S0urceror/63ffbd58b9e58c083c12fb9b071a24d7 to your computer and use it in GitHub Desktop.
org 08000h
ld hl, 08000h ; this memory area
call Memory_GetSlot
call GET_SLTWRK_FOR_SLOT
; retrieve pointer from SLTWRK area
ld e,(hl)
inc hl
ld d,(hl)
; do something with it
;
;
ret
GET_SLTWRK_FOR_SLOT:
; formula: SLTWRK address = FD09H + 32*primary slot + 8*expansion slot + 2*page
ld b, a
and 00000011b; select primary slot
ld hl,0
ld l,a
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl; multiply by 32
ld a, b
and 00001100b; select extended slot
sla a; times 2
ld de, 0
ld e, a
add hl, de
ld de, SLTWRK
add hl, de
ld a, (hl)
ld e, a
inc hl
ld a, (hl)
ld d, a
ld hl, de
or a ; clear Cy
ret
; h = memory address high byte (bits 6-7: page)
; a <- slot ID formatted FxxxSSPP
; Modifies: f, bc, de
Memory_GetSlot:
call BIOS_RSLREG
bit 7,h
jr z,PrimaryShiftContinue
rrca
rrca
rrca
rrca
PrimaryShiftContinue:
bit 6,h
jr z,PrimaryShiftDone
rrca
rrca
PrimaryShiftDone:
and #0b00000011
ld c,a
ld b,#0
ex de,hl
ld hl,#BIOS_EXPTBL
add hl,bc
ld a,(hl)
and #0x80
or c
ld c,a
inc hl ; move to SLTTBL
inc hl
inc hl
inc hl
ld a,(hl)
ex de,hl
bit 7,h
jr z,SecondaryShiftContinue
rrca
rrca
rrca
rrca
SecondaryShiftContinue:
bit 6,h
jr nz,SecondaryShiftDone
rlca
rlca
SecondaryShiftDone:
and #0b00001100
or c
ld l, a
ret
@D15C0DE
Copy link

D15C0DE commented Aug 14, 2021

This code is highly unoptimized. Instead of trying to find a non-standard approach, you could just optimize it like this:

GET_POINTER_FROM_SLTWRK:
    ; formula: SLTWRK address = FD09H + 32*primary slot + 8*expansion slot + 2*page
        ld      l,a             ; X000SSPP
        and     00000011b       ; select primary slot
        rrca                    ; multiply by 32
        rrca
        rrca
        add     a,l             ; add extended slot times 2
        add     a,l
        and     01111000b
        ld      hl,SLTWRK       ; add SLTWRK (8-bit add suffices)
        add     a,l
        ld      l,a

        ld      e,(hl)          ; retrieve pointer from SLTWRK area
        inc     l               ; (8-bit increment suffices)
        ld      d,(hl)
        ex      de,hl

        or      a               ; clear Cy (why is this needed? in any case, carry is already cleared by the last ADD A,L)
        ret

@D15C0DE
Copy link

D15C0DE commented Aug 14, 2021

Memory_GetSlot can also be optimized greatly. For starters, it's written as a universal routine, but you are supplying a fixed input.

@S0urceror
Copy link
Author

Thanks D15CODE, yes you can certainly optimize but the principle remains at every HTIMI I have to lookup my WRKAREA.

@D15C0DE
Copy link

D15C0DE commented Aug 15, 2021

Yes, but if the overhead is small(er), what's the problem? Compared to the overhead of the interslotcall you probably have on the H.TIMI hook, this is nothing.

@D15C0DE
Copy link

D15C0DE commented Aug 15, 2021

Allocating more memory in page 3 leads to all kinds of compatibility problems. It's too short-sighted.

@S0urceror
Copy link
Author

S0urceror commented Aug 15, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment