;SET PAGE

func_setpage:

LD ($FAF5),A
RRCA
RRCA
RRCA
AND $E0
LD E,A
LD A,($F3E1)
AND $1F
LD C,2
CALL func_setpage_5
LD A,($FCAF)
CP 7
LD A,($FAF5)
LD D,$FC
JR C,func_setpage_4
ADD A,A
LD D,$FD

func_setpage_4:
LD E,A
LD A,($FFEA)
LD C,$B
CALL func_setpage_6
LD A,D
RLCA
RLCA
RLCA
RLCA
LD D,A
LD A,E
RLCA
RLCA
RLCA
RLCA
LD E,A
LD A,($F3E5)
LD C,6
AND D
OR E
LD B,A
JP $47
PUSH AF
PUSH BC
PUSH DE
LD IX,$149
CALL $15F
POP DE
LD L,C
LD A,B
RLCA
RLCA
RLCA
RLCA
AND $F
LD E,A
LD A,B
POP BC
AND $F
LD C,A
LD A,D
INC A
JR NZ,func_setpage_1
LD D,E

func_setpage_1:
LD A,H
INC A
JR NZ,func_setpage_2
LD H,L

func_setpage_2:
LD A,B
INC A
JR NZ,func_setpage_3
LD B,C

func_setpage_3:
LD A,D
ADD A,A
ADD A,A
ADD A,A
ADD A,A
OR B
POP DE
LD E,H
LD IX,$14D
JP $15F

func_setpage_5:
OR E
LD B,A
JP $47

func_setpage_6:
AND D
OR E
LD B,A
JP $47