Last modified on 17 July 2006, at 05:47

Programming:CRC16

Revision as of 05:47, 17 July 2006 by Octoate (Talk | contribs) (CRC16 calculation)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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