Advertisement
PikalaxALT

Context switch func for CGB

Sep 20th, 2021
2,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ...
  2.     call Function_01d1
  3.     db $e0
  4. ...
  5.  
  6. ; Switch to specified context?
  7. Function_01d1:
  8.     ; Store the registers
  9.     push af
  10.     push bc
  11.     push de
  12.     push hl
  13.  
  14.     ; Read the return address
  15.     ld hl, sp+$08
  16.     ld a, [hli]
  17.     ld e, a
  18.     ld d, [hl]
  19.     ; Read the 1-byte arg to b
  20.     ld a, [de]
  21.     ld b, a
  22.     ; Set the return address to 1 byte after the arg
  23.     inc de
  24.     ld a, d
  25.     ld [hld], a
  26.     ld [hl], e
  27.  
  28.     ; Byte at HRAM address DF contains an offset into hram
  29.     ldh a, [$ffdf]
  30.     ld c, a
  31.     ; Store sp
  32.     ld hl, sp+0
  33.     ld a, l
  34.     ldh [c], a
  35.     inc c
  36.     ld a, h
  37.     ldh [c], a
  38.     inc c
  39.     ; Store the ROM bank number, which is a word at $4000
  40.     ld a, [$4000]
  41.     ldh [c], a
  42.     inc c
  43.     ld a, [$4001]
  44.     ldh [c], a
  45.     inc c
  46.     ; Store the WRAM bank number
  47.     ldh a, [rSVBK]
  48.     ldh [c], a
  49.     ; Store the new offset
  50.     ld a, b
  51.     ldh [$ffdf], a
  52.     ; Load the stack pointer corresponding to the new offset
  53.     ld c, a
  54.     ldh a, [c]
  55.     ld l, a
  56.     inc c
  57.     ldh a, [c]
  58.     ld h, a
  59.     ld sp, hl
  60.     inc c
  61.     ; Switch to the context banks
  62.     ldh a, [c]
  63.     inc c
  64.     inc c
  65.     call Bankswitch
  66.     ldh a, [c]
  67.     ldh [rSVBK], a
  68.     ; Pop the registers
  69.     pop hl
  70.     pop de
  71.     pop bc
  72.     pop af
  73.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement