%include "compat.asm"
fname "mc.bin"

; -----------------------------------------------------------------------------
; Create bold font
; -----------------------------------------------------------------------------

LEN:    EQU 300h
L3D00:  EQU 3D00h         ; Location of original Spectrum charset
CHARS:  EQU 5C36h         ; CHARS system variable: 256 less than address of character set (which starts with space and carries on to the copyright symbol). Normally in ROM, but you can set up your own in RAM and make CHARS point to it.

        ORG FC19h

; Journey font by DamienG https://damieng.com
; Characters from " ", ... "_" (no lower case letters)
; Compressed with ZX0
FONT_C:
        DB 96h, 00h, 2Ah, 04h, 0Ch, 18h, 82h, 00h
        DB 30h, 00h, 36h, 8Fh, 24h, 48h, E0h, 96h
        DB 16h, 16h, 7Fh, 2Ch, FEh, 58h, 58h, 96h
        DB 3Ch, 52h, 70h, 38h, 1Ch, 94h, 78h, 96h
        DB 62h, B4h, 68h, 10h, 2Ch, 5Ah, 8Ch, 96h
        DB 38h, 6Ch, 28h, 72h, DCh, CCh, 76h, E3h
        DB A7h, 10h, 20h, FBh, B0h, 95h, 30h, 9Fh
        DB FFh, 18h, 0Ch, 00h, F9h, EEh, 7Fh, E8h
        DB DFh, ABh, 24h, 7Ch, FBh, 7Ch, BFh, FEh
        DB F2h, CFh, 46h, EEh, 08h, CEh, EBh, 83h
        DB 7Ch, 9Ah, 20h, 18h, 4Fh, BFh, 86h, 41h
        DB 66h, 66h, C6h, CCh, CCh, EFh, E1h, 1Ch
        DB DEh, BBh, 3Ch, E0h, A3h, 74h, 64h, F8h
        DB AEh, F1h, 46h, 5Dh, F9h, 8Ch, D0h, AAh
        DB 3Ch, 6Ch, 7Eh, 10h, 0Fh, 3Eh, 32h, 70h
        DB 78h, E0h, EBh, C4h, 7Eh, E3h, 9Fh, F1h
        DB 7Eh, 06h, 98h, 81h, 30h, 20h, 40h, 29h
        DB 38h, FBh, F0h, 3Eh, B8h, DDh, 60h, 48h
        DB EFh, FFh, 30h, F0h, 6Ah, 10h, A7h, 04h
        DB 70h, 18h, 04h, FBh, 18h, 48h, E6h, 04h
        DB 20h, 18h, 0Eh, CFh, 8Dh, E9h, FCh, 80h
        DB C1h, 92h, 61h, 64h, DAh, EEh, D4h, 60h
        DB 38h, 9Fh, 0Eh, 36h, 66h, 51h, C9h, FFh
        DB B7h, 29h, 26h, 66h, 7Ch, 46h, C6h, FCh
        DB E1h, 1Eh, 30h, 60h, FFh, CEh, 32h, 1Ch
        DB 00h, 78h, 6Ch, C1h, 21h, F2h, F8h, D1h
        DB A9h, EFh, 78h, DFh, 7Ch, F0h, AFh, 7Ch
        DB FFh, 86h, 11h, 60h, C0h, CEh, C6h, CEh
        DB 2Eh, E6h, 01h, BCh, FCh, FFh, B7h, 22h
        DB EEh, F7h, 61h, F4h, CAh, 71h, 70h, 87h
        DB 66h, 6Ch, 78h, 70h, D8h, A3h, D0h, E0h
        DB 60h, E0h, AFh, FFh, A2h, FCh, 00h, 63h
        DB 77h, FEh, B6h, 36h, 71h, 4Fh, 72h, 7Eh
        DB DEh, CCh, 8Ch, 84h, 81h, EEh, 53h, 50h
        DB 81h, 9Eh, 7Ch, C1h, E2h, 80h, E0h, A2h
        DB D6h, 7Ch, 06h, 6Eh, F8h, 93h, DAh, 86h
        DB 11h, 62h, 6Ch, 8Ch, B8h, 30h, 50h, ABh
        DB 20h, 22h, A2h, 31h, CCh, A1h, 78h, 99h
        DB 44h, C6h, 6Ch, 6Ch, 68h, 68h, 41h, 63h
        DB 63h, D6h, FEh, ECh, CCh, 6Ah, 62h, 66h
        DB 3Ch, 38h, 78h, 8Ch, C8h, 03h, B3h, 24h
        DB 3Ch, 99h, 73h, B1h, 50h, 88h, 60h, FCh
        DB F2h, B9h, B2h, F0h, A1h, B0h, C6h, EDh
        DB CCh, 00h, FCh, 42h, FFh, 83h, 31h, 10h
        DB 38h, 68h, 6Ch, F7h, 95h, FEh, C0h, 43h
        DB 00h, 20h

SETFONT:
        LD HL, FONT_C
        LD DE, FONT
        PUSH DE
        CALL DZX0_STANDARD
        POP HL
        DEC H
        LD (CHARS),HL
        RET

; -----------------------------------------------------------------------------
; Clear top third of screen and blank out lower two thirds attributes
; -----------------------------------------------------------------------------

; layout of the attribute byte:
; 7 6 5 4 3 2 1 0 : Bits
; f b G R B G R B : f(lash) b(rightness) R(ed) G(reen) B(lue)
; l r       -ink- : Foreground color (pixels set to 1)
; a i paper       : Background color (pixels set to 0)
; s g
; h h
;   t
B_WHITE:  EQU 01111000b   ; color attribute BRIGHT 1: INK 0: PAPER 7
BLACK:    EQU 0           ; color attribute: INK 0: PAPER 0
SCREEN:   EQU 4000h
ATTR:     EQU SCREEN+1800h

CLS:
        ; Erase top third of screen
        LD BC, 800h
        LD HL, SCREEN
        LD D, H
        LD E, L
        INC DE
        LD (HL), 0
        LDIR
        
        ; Set upper third attributes to BRIGHT 1: INK 0: PAPER 7
        LD BC, 100h
        LD HL, ATTR 
        LD D, H
        LD E, L
        INC DE
        LD (HL), B_WHITE
        LDIR
        
        ; Set lower two thirds attributes to INK 0: PAPER 0
        LD BC, 1FFh
        LD (HL), BLACK
        LDIR
        
        RET

; -----------------------------------------------------------------------------
; ZX0 decoder by Einar Saukas
; "Standard" version (69 bytes only)
; See https://github.com/b0nefish/ZX0
; -----------------------------------------------------------------------------
; Parameters:
;   HL: source address (compressed data)
;   DE: destination address (decompressing)
; -----------------------------------------------------------------------------

SOURCE: EQU $5000
DEST:   EQU $4000                        ; Screen

DECOMPRESS:
        LD      HL, SOURCE
        LD      DE, DEST

DZX0_STANDARD:
        LD      BC, $FFFF               ; preserve default offset 1
        PUSH    BC
        INC     BC
        LD      A, $80

DZX0S_LITERALS:
        CALL    DZX0S_ELIAS             ; obtain length
        LDIR                            ; copy literals
        ADD     A, A                    ; copy from last offset or new offset?
        JR      C, DZX0S_NEW_OFFSET
        CALL    DZX0S_ELIAS             ; obtain length

DZX0S_COPY:
        EX      (SP), HL                ; preserve source, restore offset
        PUSH    HL                      ; preserve offset
        ADD     HL, DE                  ; calculate destination - offset
        LDIR                            ; copy from offset
        POP     HL                      ; restore offset
        EX      (SP), HL                ; preserve offset, restore source
        ADD     A, A                    ; copy from literals or new offset?
        JR      NC, DZX0S_LITERALS

DZX0S_NEW_OFFSET:
        CALL    DZX0S_ELIAS             ; obtain offset MSB
        EX      AF, AF'
        POP     AF                      ; discard last offset
        XOR     A                       ; adjust for negative offset
        SUB     C
        RET     Z                       ; check end marker
        LD      B, A
        EX      AF, AF'
        LD      C, (HL)                 ; obtain offset LSB
        INC     HL
        RR      B                       ; last offset bit becomes first length bit
        RR      C
        PUSH    BC                      ; preserve new offset
        LD      BC, 1                   ; obtain length
        CALL    NC, DZX0S_ELIAS_BACKTRACK
        INC     BC
        JR      DZX0S_COPY

DZX0S_ELIAS:
        INC     C                       ; interlaced Elias gamma coding

DZX0S_ELIAS_LOOP:
        ADD     A, A
        JR      NZ, DZX0S_ELIAS_SKIP
        LD      A, (HL)                 ; load another group of 8 bits
        INC     HL
        RLA

DZX0S_ELIAS_SKIP:
        RET     C

DZX0S_ELIAS_BACKTRACK:
        ADD     A, A
        RL      C
        RL      B
        JR      DZX0S_ELIAS_LOOP

FONT:
        ; %defb   200h ; To calculate size of M/C block
PRGEND:
