; ***************************************************************************
; 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_LIST_T_H
#define AQH_AVR_COMMON_LIST_T_H



.equ LIST_TEST_OBJECT_OFFS_LIST   = 0
.equ LIST_TEST_OBJECT_OFFS_VALUE1 = LIST_SIZE
.equ LIST_TEST_OBJECT_OFFS_VALUE2 = LIST_SIZE+1
.equ LIST_TEST_OBJECT_SIZE        = LIST_SIZE+2




.dseg

listTest_list:    .byte 2
listTest_object1: .byte LIST_TEST_OBJECT_SIZE
listTest_object2: .byte LIST_TEST_OBJECT_SIZE
listTest_object3: .byte LIST_TEST_OBJECT_SIZE



.cseg


; ---------------------------------------------------------------------------
; @routine listTest_Object_Init
; @param Y pointer to object to init
; @param r18 value 1
; @param r19 value 2

listTest_Object_Init:
  bigcall List_InitObject                  ; (R16)
  std Y+LIST_TEST_OBJECT_OFFS_VALUE1, r18
  std Y+LIST_TEST_OBJECT_OFFS_VALUE2, r19
  ret
; @end



listTest1:
  ldi yl, LOW(listTest_object1)
  ldi yh, HIGH(listTest_object1)
  ldi r18, 1
  ldi r19, 2
  rcall listTest_Object_Init
  sts listTest_list, yl
  sts listTest_list+1, yh
  mov xl, yl                            ; X=listTest_object1
  mov xh, yh

  ldi yl, LOW(listTest_object2)
  ldi yh, HIGH(listTest_object2)
  ldi r18, 3
  ldi r19, 4
  rcall listTest_Object_Init
  
  ; X=object 1, Y=object 2
  bigcall List_AddObject  ; (r16, r17, x)

  lds yl, listTest_list
  lds yh, listTest_list+1

  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 1
  ldi r16, 1
  brne listTest1_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 2
  ldi r16, 2
  brne listTest1_error

  bigcall List_GetNextObject
  ldi r16, 3
  cpi xl, LOW(listTest_object2)
  brne listTest1_error
  cpi xh, HIGH(listTest_object2)
  brne listTest1_error
  
  mov yl, xl
  mov yh, xh
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 3
  ldi r16, 4
  brne listTest1_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 4
  ldi r16, 5
  brne listTest1_error

  bigcall List_GetNextObject
  mov r16, xh
  or r16, xl
  ldi r16, 6
  brne listTest1_error
  sec
  ret
listTest1_error:
  clc
  ret
; @end



listTest2:
  lds xl, listTest_list
  lds xh, listTest_list+1

  ldi yl, LOW(listTest_object3)
  ldi yh, HIGH(listTest_object3)
  ldi r18, 5
  ldi r19, 6
  rcall listTest_Object_Init
  
  ; X=object 1, Y=object 2
  bigcall List_AddObject  ; (r16, r17, x)

  mov xl, yl
  mov xh, yh
  bigcall List_GetLastObject
  ldi r16, 1
  cpi xl, LOW(listTest_object3)
  brne listTest2_error
  cpi xh, HIGH(listTest_object3)
  brne listTest2_error
  
  mov yl, xl
  mov yh, xh
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 5
  ldi r16, 2
  brne listTest2_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 6
  ldi r16, 3
  brne listTest2_error
  
  sec
  ret
listTest2_error:
  clc
  ret
; @end



listTest3:
  lds xl, listTest_list
  lds xh, listTest_list+1
  ldi yl, LOW(listTest_object2)
  ldi yh, HIGH(listTest_object2)
  bigcall List_UnlinkObject

  lds yl, listTest_list
  lds yh, listTest_list+1

  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 1
  ldi r16, 1
  brne listTest3_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 2
  ldi r16, 2
  brne listTest3_error

  bigcall List_GetNextObject
  ldi r16, 3
  cpi xl, LOW(listTest_object3)
  brne listTest3_error
  cpi xh, HIGH(listTest_object3)
  brne listTest3_error
  
  mov yl, xl
  mov yh, xh
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 5
  ldi r16, 4
  brne listTest3_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 6
  ldi r16, 5
  brne listTest3_error

  bigcall List_GetNextObject
  mov r16, xh
  or r16, xl
  ldi r16, 6
  brne listTest3_error
  sec
  ret
listTest3_error:
  clc
  ret
; @end



listTest4:
  lds xl, listTest_list
  lds xh, listTest_list+1
  ldi yl, LOW(listTest_object3)
  ldi yh, HIGH(listTest_object3)
  bigcall List_UnlinkObject

  lds yl, listTest_list
  lds yh, listTest_list+1

  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 1
  ldi r16, 1
  brne listTest4_error
  ldd r16, Y+LIST_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 2
  ldi r16, 2
  brne listTest4_error

  bigcall List_GetNextObject
  mov r16, xh
  or r16, xl
  ldi r16, 6
  brne listTest4_error
  sec
  ret
listTest4_error:
  clc
  ret
; @end



#endif
