Programming:CRC16

From CPCWiki - THE Amstrad CPC encyclopedia!
Jump to: navigation, search

This code is untested and may not work as advertised or at all.

CRC16:
;Borrowed from http://zilog.sh.cvut.cz/~base/misc/z80bits.html
; and moddified to be a loop
;The arrow comments show what lines I added or commented out from the original.
;Inputs:    de->data bc=number of bytes
;Outputs:   hl=CRC16
   push bc     ;<---<<<
   push de     ;<---<<<
   push af     ;<---<<<
   ld hl,$FFFF
   push bc     ;<---<<<
CRC16_Read:
   ld a,(de)
   inc de
   xor h
   ld h,a
   ld b,8
CRC16_CrcByte:
   add hl,hl
   jr nc,CRC16_Next
   ld a,h
   xor $10
   ld h,a
   ld a,l
   xor $21
   ld l,a
CRC16_Next:
   djnz CRC16_CrcByte
;   dec c      ;>>>--->
   pop bc      ;<---<<<
   dec bc      ;<---<<<
   push bc     ;<---<<<
   ld a,b      ;<---<<<
   or c        ;<---<<<
   jr nz,CRC16_Read
   pop bc      ;<---<<<
   pop af      ;<---<<<
   pop de      ;<---<<<
   pop bc      ;<---<<<
   ret