; ***************************************************************************
; 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_COMMON_RESSOURCE_H
#define AQH_AVR_COMMON_RESSOURCE_H


; ***************************************************************************
; defs

.equ RES_IMAGE_OFFS_TYPE_LO     = 0
.equ RES_IMAGE_OFFS_TYPE_HI     = 1
.equ RES_IMAGE_OFFS_WIDTH_LO    = 2
.equ RES_IMAGE_OFFS_WIDTH_HI    = 3
.equ RES_IMAGE_OFFS_HEIGHT_LO   = 4
.equ RES_IMAGE_OFFS_HEIGHT_HI   = 5
.equ RES_IMAGE_OFFS_COLORMAP_LO = 6
.equ RES_IMAGE_OFFS_COLORMAP_HI = 7
.equ RES_IMAGE_OFFS_PIXELS_LO   = 8
.equ RES_IMAGE_OFFS_PIXELS_HI   = 9

.equ RES_IMAGE_COLORMAP_OFFS_NUM_LO  = 0
.equ RES_IMAGE_COLORMAP_OFFS_NUM_HI  = 1
.equ RES_IMAGE_COLORMAP_OFFS_ENTRIES = 2



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

.cseg


; ---------------------------------------------------------------------------
; @routine RES_GetRessource
;
; The Ressource table has one entry for every ressource handled by this table.
; The first entry contains the number of ressource entries in the table.
; All following entries contain byte pointer addresses (for LPM) to ressources.
; Ressource ids start with zero.
;
; @param r17:r16 ressource id (starting with 0)
; @param Z pointer to start of ressources in FLASH (byte address)
; @return CF set if ressource found, cleared otherwise
; @return Z if CF set: pointer to ressource (byte address)
; @clobbers r16, r17, r18

RES_GetRessource:
  lpm r18, Z+   ; first entry: number of entries in table
  cp r16, r18   ; id must be < num entries
  lpm r18, Z+   ; Z points to first entry now
  cpc r17, r18
  brcc RES_GetRessource_ret
RES_GetRessource_getRessource:
  lsl r16       ; byte address, so we need to add ressource id twice (i.e. *2)
  rol r17
  add zl, r16
  add zh, r17
  lpm r16, Z+
  lpm r17, Z+
  mov zl, r16
  mov zh, r17
  sec
RES_GetRessource_ret:
  ret
; @end





#endif

