; ***************************************************************************
; 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.   *
; ***************************************************************************


.cseg


; ---------------------------------------------------------------------------
; @routine crc8Calc @global
; calc crc8 checksum using given polynomial
;
; @return r16 crc8 checksum
; @return X   points directly after last checked byte
; @param X    pointer to data to calc crc8 for
; @param r18  number of bytes to calc crc8 for
; @param r19  polynomial to use
; @clobbers: R16, R17, R18, R20, R21, X

crc8Calc:
  clr r16                    ; start crc
  
crc8Calc_loop1:
    ld r17, X+               ; running var
    ldi r20, 8               ; counter for loop2
crc8Calc_loop2:       ; r16=crc so far, r17=current inbyte
      mov r21, r16
      lsr r16
      eor r21, r17
      lsr r17
      andi r21, 1
      breq crc8Calc_withoutPoly
      eor r16, r19
crc8Calc_withoutPoly:
      dec r20
    brne crc8Calc_loop2
    dec r18
  brne crc8Calc_loop1
  ret
; @end

