;PSET

func_pset:

PUSH AF
LD A,($FCAF)
CP 5
JP C,func_pset_2 ;$7378
CALL func_pset_3 ;$7320
LD A,(6)
LD C,A
INC C
CALL func_pset_4 ;$6FC3
INC C
INC C
OUT (C),E
OUT (C),D
OUT (C),L
LD A,($FAF6)
OR H
OUT (C),A
OUT (C),A
OUT (C),A
OUT (C),A
OUT (C),A
POP AF
OUT (C),A
XOR A
OUT (C),A
LD A,B
OR $50
OUT (C),A
RET

func_pset_2:
POP AF
LD ($F3F2),A
LD C,E
LD B,D
EX DE,HL
LD IX,$57F5
JP func_pset_5 ;$73CC

func_pset_3:
LD A,($F7C0)
AND A
RET Z
LD A,D
ADD A,A
JR NC,func_pset_3b
LD DE,0
JR func_pset_3c ;0733AH

func_pset_3b:
LD A,($FCAF)
RRCA
AND 1
CP D
JR NC,func_pset_3c
LD E,$FF
LD D,A
func_pset_3c:
LD A,H
ADD A,A
JR NC,func_pset_3d
LD HL,0
RET
func_pset_3d:
JR NZ,func_pset_3e
LD A,L
CP $D4
RET C
func_pset_3e:
LD HL,$D3
RET

func_pset_4:
LD A,$24
DI
OUT (C),A
LD A,$91
EI
OUT (C),A
func_pset_4b:
LD A,2
DI
CALL func_pset_4c
IN A,(C)
PUSH AF
XOR A
CALL func_pset_4c
EI
POP AF
RRA
JR C,func_pset_4b
RET

func_pset_4c:
OUT (C),A
LD A,$8F
OUT (C),A
RET

func_pset_5:
CALL $159
EI
RET
