;           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
;                    Version 2, December 2004
;  
; Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
; 
; Everyone is permitted to copy and distribute verbatim or modified
; copies of this license document, and changing it is allowed as long
; as the name is changed.
;  
;            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
;   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
;  
;  0. You just DO WHAT THE FUCK YOU WANT TO.

;
; Text display routine for Obocchama Kun.
; author: MooZ
;
MAX_CHAR  = $1c
MAX_LINES = $08
;pin-son descendre de 1 et decaler de 1 sur la gauche
; ? decaler de 1 sur la gauche

	.code
	.bank 2
    .org $b64f
    ; end credit display
b64f:   lda $27d4
        and #$07
        bne _l1
        clc
        lda <$24
        adc #$40
        sta <$24
        cla
        adc <$25
        and #$0f
        sta <$25
        lda <$22
        bne _l1
        jsr $b694
        jsr b687
b66e:   jsr bb1a
        cmp #$ff
        beq .l0
            jsr b6a1
            bra b66e
.l0:    lda [$20]
        sta <$22
b687:   inc <$20
        bne _l1
            inc <$21
_l1:    rts

	.org $b6a1
    
b6a1:   pha
        
        inc <$6e        ; increment BAT pointer
        bne .l0
            lda <$6f
            inc A
            and #$0f
            sta <$6f
.l0:
        pla
        ldx #$f0         ; add current symbol to display list
        jsr $e8f4    
b6d7:   rts

fetch_char:
        lda [$20]       ; fetch byte from text bloc
        inc <$20
        bne .l2
            inc <$21
.l2:
        cmp	#ENCODED_CHAR_MIN
        bcc	.raw
        cmp	#ENCODED_CHAR_MAX
        bcs	.raw
.encoded:
            sec
            sbc	#ENCODED_CHAR_MIN
            tay

            lda	 _bpe_second, Y
            sta  <$e1
            
            smb0 <$e0
            
            lda	 _bpe_first, Y
            rts
.raw:
            stz  <$e0
            rts

; max: b730     
     
    ; brutal timing setup.
    .org $b813
    .db $06 ; original value = 8
    
    ; text display
    .org $bab4
      
bab4:   jsr bafc
        jsr bb1a
        cmp #$ff
        bne bac0        ; #$ff = end of text bloc
            sec
            rts
bac0:   cmp #$fc
        beq bad3        ; #$fc = new line
        
        cmp #$fd
        beq badf        ; #$fd = reset textbox
        
        jsr b6a1
        inc <$22
        lda <$22
        cmp #MAX_CHAR   ; check if we reached the end of the line
        bcc bae2
        
bad3:   stz <$22
        inc <$23
        lda <$23
        cmp #MAX_LINES  ; check if we output the maximum number of lines
        bcc bae2
        stz <$23
badf:   jsr bb2d
bae2:   clc
        rts

bae4:   lda #MAX_CHAR
        jsr $e69f
        lda <$2f        ; Fetch next text bloc
        asl A
        tay
        lda $7522,Y
        sta <$20
        lda $7523,Y
        sta <$21
        stz <$22
        stz <$23
        rts
        
bafc:   clc             ; Compute BAT pointer
        lda <$23        ; The BAT width is 64. $22 contains the X coordinate
        adc #$10        ; and $23 the Y coordinates of the line.
        sta <$6f        ; The formula for computing the BAT pointer is:
        cla             ; ptr = bat_x + ((bat_y + 16) * bat_width)
        lsr <$6f        ; 16 is added to $23 in order to bring the text in the
        ror A           ; middle of the screen.
        lsr <$6f
        ror A
        ora <$22
        sta <$6e
        rts
        
bb1a:   bbr0 <$e0, .fetch_new
            stz    <$e0
            lda    <$e1
            rts
.fetch_new:
        jmp fetch_char
        
        .org $bb2d
bb2d: lda #$00          ; Clean text area.
      sta <$6e
      lda #$04
      sta <$6f
      lda #$0c
      sta <$02
bb39:   lda #$20
        sta <$03
bb3d:       lda #$00
            ldx #$f0
            jsr $e8f4 
            inc <$6e
            dec <$03
            bne bb3d
        clc
        lda <$6e
        adc #$20
        sta <$6e
        cla
        adc <$6f
        sta <$6f
        dec <$02
        bne bb39
bb5a: rts

    .bank 3
    .org $c70c
c70c:   lda #$8a
        sta <$6e
        lda #$01
        sta <$6f
        
    .bank 3
    .org $c8ba
c8ba:   jsr bb1a
        cmp #$ff
        beq .end
        jsr b6a1
        bra c8ba
.end:   rts

    .data
	.bank 0 
	.org $fe80
	.include "text.asm"        ; generated by encodeScript
    
    .data
    .bank $37
    .org $637e

monster_00:  .db $4F, $C8, $B2, $B5, $40, $01, $F0, $00, $00, $FD, $00, $00, $01, $46, $05, $06, $12, $00
monster_01:  .db $4F, $C8, $9E, $AA, $40, $01, $C0, $00, $00, $FD, $00, $00, $41, $2A, $0F, $06, $13, $00
monster_02:  .db $4F, $C8, $B3, $BF, $40, $01, $F0, $00, $80, $FE, $00, $00, $01, $8C, $04, $06, $14, $00
monster_03:  .db $4F, $C8, $18, $C0, $40, $01, $C0, $00, $00, $FF, $00, $01, $41, $7E, $0F, $06, $15, $01
monster_04:  .db $4F, $C8, $AE, $C0, $40, $01, $F0, $00, $00, $FE, $00, $00, $01, $69, $04, $06, $16, $00
monster_05:  .db $4F, $C8, $0F, $C4, $40, $01, $F0, $00, $00, $FF, $00, $00, $41, $7E, $0F, $06, $17, $00
monster_06:  .db $4F, $C8, $D1, $C4, $40, $01, $F0, $00, $00, $FF, $00, $FC, $01, $D2, $03, $06, $18, $02
monster_07:  .db $4F, $C8, $FF, $C4, $40, $01, $F0, $00, $00, $FF, $00, $00, $61, $7E, $0f, $06, $19, $00
monster_08:  .db $4F, $C8, $27, $C5, $70, $00, $F0, $00, $00, $00, $00, $00, $01, $40, $05, $06, $1A, $00
monster_09:  .db $4F, $C8, $09, $C6, $40, $01, $F0, $00, $00, $FF, $00, $00, $41, $7E, $10, $06, $1B, $00
monster_0a:  .db $4F, $C8, $38, $C6, $40, $01, $F0, $00, $00, $FF, $00, $00, $01, $D2, $03, $06, $1C, $00
monster_0b:  .db $4F, $C8, $5F, $C6, $C0, $00, $F0, $00, $00, $00, $00, $00, $E1, $10, $0F, $06, $1D, $00
monster_0c:  .db $4F, $C8, $E1, $CA, $40, $01, $F0, $00, $00, $FD, $00, $00, $01, $46, $04, $06, $1E, $00
monster_0d:  .db $4F, $C8, $4C, $CB, $C0, $00, $E0, $00, $00, $00, $00, $00, $C1, $16, $10, $06, $1F, $00
monster_0e:  .db $4F, $C8, $94, $CC, $40, $01, $F0, $00, $00, $FF, $00, $00, $01, $D2, $04, $06, $20, $00
monster_0f:  .db $4F, $C8, $AE, $CD, $40, $01, $F0, $00, $00, $FF, $00, $00, $41, $7E, $0F, $06, $21, $02
monster_10:  .db $4F, $C8, $35, $D0, $00, $00, $C0, $00, $00, $01, $00, $00, $01, $78, $05, $06, $23, $00
monster_11:  .db $4F, $C8, $F8, $D0, $40, $01, $F0, $00, $00, $FF, $00, $00, $41, $7E, $0F, $06, $22, $02
monster_12:  .db $4F, $C8, $08, $C2, $40, $01, $C0, $00, $00, $FE, $00, $00, $01, $50, $08, $06, $24, $00
monster_13:  .db $4F, $C8, $11, $D8, $40, $01, $C0, $00, $00, $FE, $00, $00, $01, $50, $08, $06, $25, $00
monster_14:  .db $4F, $C8, $3C, $D8, $20, $01, $F0, $00, $00, $FF, $00, $00, $01, $7E, $07, $06, $26, $00
monster_15:  .db $4F, $C8, $27, $AF, $40, $01, $C0, $00, $00, $FF, $00, $00, $01, $D2, $02, $06, $27, $00
monster_16:  .db $4F, $C8, $91, $D9, $40, $01, $F0, $00, $00, $FF, $00, $00, $41, $7E, $10, $06, $28, $02
monster_17:  .db $4F, $C8, $B9, $DA, $70, $00, $F0, $00, $00, $00, $00, $00, $01, $40, $05, $06, $29, $00
monster_18:  .db $4F, $C8, $75, $DB, $D0, $00, $F0, $00, $00, $00, $00, $00, $41, $40, $0f, $06, $2A, $00
monster_19:  .db $4F, $C8, $77, $DD, $40, $01, $F0, $00, $00, $FF, $00, $00, $01, $D2, $03, $06, $2B, $00
monster_20:  .db $4F, $C8, $BC, $DD, $40, $01, $C0, $00, $00, $FF, $00, $00, $61, $7E, $10, $06, $2C, $00
monster_21:  .db $4F, $C8, $A4, $DE, $40, $01, $C0, $00, $00, $FE, $00, $00, $01, $50, $0D, $06, $2D, $00
monster_22:  .db $4F, $C8, $87, $7D, $A0, $00, $F0, $00, $00, $00, $00, $00, $01, $40, $0f, $06, $2E, $00

    .data
    .bank $1b
    .org $66ab
font_start:
    .incbin "compressedFont.bin"
font_end:
font_size = font_end - font_start
    .incbin "compressedFont.bin.hed"
    .org $66a9
    .dw (font_size+6)

    .bank $3b
    .org $6cf6
title_start:
    .incbin "title.compressed.bin"
title_end:
title_size = title_end - title_start
    .incbin "title.compressed.bin.hed"
title_header_end:
    .org $6cf4
    .dw (title_size+6)
    
    .bank $36
    .org $4bca
title_bat:
    .incbin "title.bat"