	.code
	.bank 2
	.org $89e2

l_89e2: LDY     $271b
        LDA     [$5c], Y		; Load char

        CMP     #$ff			; [END]
        BNE     l_89ee			
        JMP     l_8aaf			; end of string routine

l_89ee: CMP     #$fc			; [FC-CR]
        BNE     l_89f5
        JMP     l_8a7e			; jump line

l_89f5: CMP     #$fe			; [FE-CR] (accentuation #1)
        BEQ     l_8a14

        CMP     #$fb			; [FB-CR] (accentuation #2)
        BEQ     l_8a49

        STA     $0002			; put char
        LDA     #$87
        STA     $0003
        INC     $2719
        LDA     $2719
        CMP     #$10			; check if we have reach the end of line
        BEQ     l_8a7e
		INC     $271b
		BRA     l_89e2		; process next char

					; [FE-CR] (accentuation #1 handler)
l_8a14: INC     $271b
        LDY     $271b			; fetch next char
        LDA     [$5c], Y		; 
        CMP     #$fd			; [FD-CR] (code for accentuation end) 
        BEQ     l_8a7e			; jump to next line

        STA     $271c			; A contains the line char offset of the 
					; accent
 
l_8a23: LDA     $2719			; so we'll put spaces until we reach this
        CMP     $271c			; position.
        BEQ     l_8a3a			
		LDA     #$9f		; $079F is the tile offset for space
		STA     $0002
		LDA     #$87
		STA     $0003
		INC     $2719
		BRA     l_8a23

l_8a3a: LDA     #$6c			; put accent ($076c)
        STA     $0002
        LDA     #$87
        STA     $0003
        INC     $2719			; increment line char offset
        BRA     l_8a14			; back to [FE-CR] handler

					; [FB-CR] (accentuation #2 handler)
l_8a49: INC     $271b
        LDY     $271b
        LDA     [$5c], Y		; fetch next char
        CMP     #$fd			; [FD-CR] (code for accentuation end) 
        BEQ     l_8a14			; jump back to [FE-CR] handler

        STA     $271c
l_8a58: LDA     $2719			; like [FE-CR], we'll put spaces until
        CMP     $271c			; we reach the accent position
        BEQ     l_8a6f
		LDA     #$9f
		STA     $0002
		LDA     #$87
		STA     $0003
		INC     $2719
		BRA     l_8a58

l_8a6f: LDA     #$6d			; put accent ($076d)
        STA     $0002
        LDA     #$87
        STA     $0003
        INC     $2719
        BRA     l_8a49

					; [FC-CR] = jump to next line
l_8a7e: STZ     $2719			; reset line char offset
        INC     $271a			; increment line offset?
        INC     $271b			; increment string char offset

        CLC     
        LDA     <$5a			; set Memory Address Write Register
        ADC     #$80
        STA     <$5a
        LDA     <$5b
        ADC     #$00
        STA     <$5b
        LDA     #$00
        STA     <$4a
        STA     $0000
        LDA     <$5a
        STA     $0002
        LDA     <$5b
l_8aa2: STA     $0003
        LDA     #$02			; use vram data register
        STA     <$4a
        STA     $0000
        JMP     l_89e2

l_8aaf: INC     $2701	; [END]
        JMP     $8cab

