; ***************************************************************************
; 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_FONT6X8_ASM
#define AQH_AVR_ILI9341_FONT6X8_ASM




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

.cseg



; ---------------------------------------------------------------------------
; @routine ili9341Font6x8MonoHandlerFn
;
; Handler for 6x8 Mono Fonts
;

ili9341Font6x8MonoHandlerFn:
  cpi r23, FONT_FN_RENDER
  breq ili9341Font6x8WriteChar
  rjmp FONT_GenericHandler
; @end




; ---------------------------------------------------------------------------
; @routine ili9341Font6x8WriteChar

; @param R16    character to write
; @param R1:R0  background color
; @param R3:R2  foreground color
; @param Z      pointer to font
; @return R18   width of char in pixels
; @return R19   height of char in pixels
; @clobbers r16, r17, r18, r19, r22, r23, r24, r25

ili9341Font6x8WriteChar:
  push zl
    push zh
      rcall ili9341Font6x8GetCharPosInFont  ; (r16, r17, r18, r24, r25)
      ldi r25, 8                            ; 16 pixels height
ili9341Font6x8WriteChar_loop1:
        ldi r24, 6                          ; width in pixels
        lpm r17, Z+                         ; font data
ili9341Font6x8WriteChar_loop2:
          lsr r17
          brcs ili9341Font6x8WriteChar_writeForeground
          mov r22, r0
          mov r23, r1
          rjmp ili9341Font6x8WriteChar_sendToDisplay
ili9341Font6x8WriteChar_writeForeground:
          mov r22, r2
          mov r23, r3
ili9341Font6x8WriteChar_sendToDisplay:
;          mov r16, r23
;          rcall SPIHW_MasterTransfer          ; (R16)
          M_SPIHW_MASTER_SEND_BYTE r23
;          mov r16, r22
;          rcall SPIHW_MasterTransfer          ; (R16)
          M_SPIHW_MASTER_SEND_BYTE r22
ili9341Font6x8WriteChar_loop2end:
          dec r24
        brne ili9341Font6x8WriteChar_loop2
        dec r25
      brne ili9341Font6x8WriteChar_loop1
ili9341Font6x8WriteChar_end:
      ldi r18, 6
      ldi r19, 8
    pop zh
  pop zl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ili9341Font6x8GetCharPosInFont (same as font8x8GetCharPosInFont!)

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

ili9341Font6x8GetCharPosInFont:
  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 ili9341Font6x8GetCharPosInFont_ret
  cp r16, r25
  brcc ili9341Font6x8GetCharPosInFont_ret
  mov r24, r16
  clr r25
  lsl r24  ; x2
  rol r25
  lsl r24  ; x4
  rol r25
  lsl r24  ; x8
  rol r25

  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
ili9341Font6x8GetCharPosInFont_ret:
  ret
; @end



#endif ; AQH_AVR_ILI9341_FONT6X8_ASM

