PikalaxALT icon

Context switch func for CGB

PikalaxALT | PRO | 09/20/21 07:12:50 PM UTC | 0 ⭐ | 8167 👁️ | Never ⏰ | []
Z80 Assembler |

0 B

|

None

|

0 👍

/

0 👎

...
    call Function_01d1
    db $e0
...
 
; Switch to specified context?
Function_01d1:
    ; Store the registers
    push af
    push bc
    push de
    push hl
 
    ; Read the return address
    ld hl, sp+$08
    ld a, [hli]
    ld e, a
    ld d, [hl]
    ; Read the 1-byte arg to b
    ld a, [de]
    ld b, a
    ; Set the return address to 1 byte after the arg
    inc de
    ld a, d
    ld [hld], a
    ld [hl], e
 
    ; Byte at HRAM address DF contains an offset into hram
    ldh a, [$ffdf]
    ld c, a
    ; Store sp
    ld hl, sp+0
    ld a, l
    ldh [c], a
    inc c
    ld a, h
    ldh [c], a
    inc c
    ; Store the ROM bank number, which is a word at $4000
    ld a, [$4000]
    ldh [c], a
    inc c
    ld a, [$4001]
    ldh [c], a
    inc c
    ; Store the WRAM bank number
    ldh a, [rSVBK]
    ldh [c], a
    ; Store the new offset
    ld a, b
    ldh [$ffdf], a
    ; Load the stack pointer corresponding to the new offset
    ld c, a
    ldh a, [c]
    ld l, a
    inc c
    ldh a, [c]
    ld h, a
    ld sp, hl
    inc c
    ; Switch to the context banks
    ldh a, [c]
    inc c
    inc c
    call Bankswitch
    ldh a, [c]
    ldh [rSVBK], a
    ; Pop the registers
    pop hl
    pop de
    pop bc
    pop af
    ret

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎