; ***************************************************************************
; copyright   : (C) 2023 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.   *
; ***************************************************************************





#define COM_USE_CRC8 1
#define COM_CRC_POLYNOMIAL 0x97



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

.cseg


COM_CRC_BEGIN:



; ---------------------------------------------------------------------------
; add checksum byte to buffer
;
; IN:
; - X  : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, R19, X

comCalcAndAddChecksumByte:
  adiw xh:xl, COM_MSG_OFFS_MSGLEN
  ld r18, X                           ; read msg len
  inc r18                             ; account for dest address
  inc r18                             ; account for msg len byte
  sbiw xh:xl, COM_MSG_OFFS_MSGLEN
#ifdef COM_USE_CRC8
  ldi r19, COM_CRC_POLYNOMIAL
  rcall cproCalcCrc8                  ; (R16, R17, R18, R20, X)
#else
  rcall cproCalcXor                   ; (R16, R17, R18, X)
#endif
  st X, r16                           ; add checksum byte
  sec
  ret



; ---------------------------------------------------------------------------
; check message in buffer
;
; IN:
; - X  : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, R19, R20, X

cproCheckMessageInBuffer:
  adiw xh:xl, COM_MSG_OFFS_MSGLEN
  ld r18, X                           ; read msg len
  inc r18                             ; account for dest address
  inc r18                             ; account for msg len byte
  sbiw xh:xl, COM_MSG_OFFS_MSGLEN
#ifdef COM_USE_CRC8
  ldi r19, COM_CRC_POLYNOMIAL
  rcall cproCalcCrc8                  ; (R16, R17, R18, R20, X)
#else
  rcall cproCalcXor                   ; (R16, R17, R18, X)
#endif
  ld r17, X
  cp r16,r17                          ; should be equal
  brne cproCheckMessageInBuffer_error
  sec
  ret
cproCheckMessageInBuffer_error:
  clc
  ret



; ---------------------------------------------------------------------------
; calc xor checksum
;
; IN:
; - X  : pointer to data to calc crc8 for
; - r18: number of bytes to calc crc8 for
; OUT:
; - r16: xor checksum
; - X  : point directly behind the checked area
; MODIFIED REGS: R16, R17, R18, X
cproCalcXor:
  clr r16
cproCalcXor_loop1:
    ld r17, X+
    eor r16, r17
    dec r18
  brne cproCalcXor_loop1
  ret



; ---------------------------------------------------------------------------
; calc crc8 checksum using given polynomial
;
; IN:
; - X  : pointer to data to calc crc8 for
; - r18: number of bytes to calc crc8 for
; - r19: polynomial to use (e.g. 0x97: HD=4 up to 119 bytes, e.g. detects all 1 to 3 bit errors)
; OUT:
; - r16: crc8 checksum
; - X  : point directly behind the checked area
; MODIFIED REGS: R16, R17, R18, R20, X

cproCalcCrc8:
  ldi r16, 0xff              ; crc
  
cproCalcCrc8_loop1:
    ld r17, X+               ; running var
    eor r16, r17
    ldi r20, 8               ; counter for loop2
cproCalcCrc8_loop2:
      lsl r16
      brcc cproCalcCrc8_l1
      eor r16, r19
cproCalcCrc8_l1:
      dec r20
    brne cproCalcCrc8_loop2
    dec r18
  brne cproCalcCrc8_loop1
  ret


COM_CRC_END:
.equ MODULE_SIZE_COM_CRC = COM_CRC_END-COM_CRC_BEGIN


