; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef AQH_AVR_ILI9341_FONT12X16_ASM
#define AQH_AVR_ILI9341_FONT12X16_ASM



; ***************************************************************************
; code

.cseg


; ---------------------------------------------------------------------------
; @routine ili9341Font12x16MonoHandlerFn
;
; Handler for 12x16 Mono Fonts
;

ili9341Font12x16MonoHandlerFn:
  cpi r23, FONT_FN_RENDER
  breq ili9341Font12x16MonoRenderChar
  bigjmp FONT_GenericHandler
; @end




; ---------------------------------------------------------------------------
; @routine ili9341Font12x16RenderCharacter

; @param R16    character to write
; @param R1:R0  background color
; @param R3:R2  foreground color
; @param Z      pointer to font
; @return r18   char width in pixel
; @return r19   char height in pixel
; @clobbers r17, r18, r19, r23, r24, r25, x

ili9341Font12x16MonoRenderChar:
  push zl
    push zh
      rcall ili9341Font12x16GetCharPosInFont         ; (r16, r17, r18, r24, r25)
      ldi r25, 16                                    ; 16 bytes height
ili9341Font12x16MonoRenderChar_loop1:
        ldi r24, 12                                  ; 12 bits
        ldi r23, 8                                   ; bit counter
        lpm r17, Z+                                  ; current font byte
ili9341Font12x16MonoRenderChar_loop2:
          dec r23
          brne ili9341Font12x16MonoRenderChar_haveByte
          lpm r17, Z+
          ldi r23, 8
ili9341Font12x16MonoRenderChar_haveByte:
          lsr r17
          brcs ili9341Font12x16MonoRenderChar_writeForeground
          mov r18, r0
          mov r19, r1
          rjmp ili9341Font12x16MonoRenderChar_sendToDisplay
ili9341Font12x16MonoRenderChar_writeForeground:
          mov r18, r2
          mov r19, r3
ili9341Font12x16MonoRenderChar_sendToDisplay:
;          mov r16, r19
;          rcall SPIHW_MasterTransfer          ; (R16)
          M_SPIHW_MASTER_SEND_BYTE r19
;          mov r16, r18
;          rcall SPIHW_MasterTransfer          ; (R16)
          M_SPIHW_MASTER_SEND_BYTE r18
ili9341Font12x16MonoRenderChar_loop2end:
          dec r24
        brne ili9341Font12x16MonoRenderChar_loop2
        dec r25
      brne ili9341Font12x16MonoRenderChar_loop1
      ldi r18, 12
      ldi r19, 16
    pop zh
  pop zl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ili9341Font12x16GetCharPosInFont

; @param  R16     character to write
; @param  Z pointer to font
; @return Z pointer to begin of char data
; @clobbers r16, r17, r18, r24, r25

ili9341Font12x16GetCharPosInFont:
  adiw zh:zl, FONT_OFFS_FIRSTCHAR
  lpm r24, Z+    ; first char num
  lpm r25, Z    ; num of chars
  sbiw zh:zl, FONT_OFFS_FIRSTCHAR+1
  sub r16, r24
  brcs ili9341Font12x16GetCharPosInFont_ret
  cp r16, r25
  brcc ili9341Font12x16GetCharPosInFont_ret

  mov r25, r16  ; x256
  clr r24

  lsr r25       ; x128
  ror r24

  lsr r25       ; x64
  ror r24

  lsr r25       ; x32
  ror r24

  adiw zh:zl, FONT_OFFS_RESSOURCEID_LO
  lpm r16, Z+
  lpm r17, Z
  ldi zl, LOW(RESSOURCE_ADDR*2)
  ldi zh, HIGH(RESSOURCE_ADDR*2)
  bigcall RES_GetRessource             ; (r16, r17, r18)
  ; ignore error here

  add zl, r24
  adc zh, r25
ili9341Font12x16GetCharPosInFont_ret:
  ret
; @end



#endif ; AQH_AVR_ILI9341_FONT12X16_ASM

