;
; CC1.0 Loader application v0.04 (c) 2004 Crystal Chips http://www.crystal-chips.com/
;
; ! DO NOT ! exceed or change MAX_PATCH_SIZE!  This is automatically checked at the end of this file,
;   watch for it.
;
; This patch gets applied to rom0:OSDSYS when it's loaded from the BIOS.
;
; Function:
;   This patch will read OSDHOOK.BIN from EEPROM to EE RAM, unpack it using the UCL N2E algorithm and
;       execute it.
;   See OSDHOOK.S for more details on the function of OSDHOOK.BIN.
;

TARGET BIN
MAX_PATCH_SIZE          EQU 0x62C
INCLUDE "COMMON.S"

_FNO_CC_DisableIF       EQU 1
_FNO_CC_WriteEEP        EQU 1
_FNO_CC_WriteSRAM       EQU 1
_FNO_CC_SetAuthCtrl     EQU 1
_FNO_CC_SetBIOSCtrl     EQU 1

code                    SEGMENT 0x001001B4

main                    PROC

                        addiu       sp, sp, -0x20
                        sw          a0, 0x00(sp)
                        sw          a1, 0x04(sp)
                        sw          a2, 0x08(sp)
                        sw          a3, 0x0C(sp)

                        li          a0, 0x0002
                        addiu       a1, sp, 0x18
                        jal         CC_ReadEEPROM                       ; Read 16-bit length - 1 of loader
                        li          a2, 2

                        lbu         t0, 0x0018(sp)                      ; swap MSByte and LSByte
                        sll         t0, t0, 8
                        lbu         at, 0x0019(sp)
                        or          t0, t0, at
                        sw          t0, 0x0010(sp)

                        addiu       a0, t0, 6
                        addiu       a1, sp, 0x18
                        jal         CC_ReadEEPROM                       ; Read the 16-bit length of the OSDHOOK.
                        li          a2, 2

                        lw          a0, 0x0010(sp)
                        addiu       a0, a0, 8
                        lui         a1, 0x1e0
                        jal         CC_ReadEEPROM
                        lhu         a2, 0x0018(sp)

                        li          a1, ADDR_OSDHOOK
                        jal         n2e_decompress
                        lui         a0, 0x1e0

                        li          a0, 0
                        li          v1, 100
                        syscall                                         ; FlushCache(0);

                        li          a0, 2
                        li          v1, 100
                        syscall                                         ; FlushCache(2);

                        lw          a0, 0x00(sp)
                        lw          a1, 0x04(sp)
                        lw          a2, 0x08(sp)
                        lw          a3, 0x0C(sp)
                        li          at, ADDR_OSDHOOK
                        jr          at
                        addiu       sp, sp, 0x20

main                    ENDP

INCLUDE "CCHW.S"
INCLUDE "N2E.S"

code                    ENDS

IF code._size > MAX_PATCH_SIZE
ERROR "!!! Patch size(", code._size, ") exceeds maximum patch size(", MAX_PATCH_SIZE, ")!"
ELSE
MESSAGE "Patch size: ", code._size
ENDIF
