Last modified on 9 August 2006, at 15:28

Programming:CRC32

Revision as of 15:28, 9 August 2006 by Kangaroo (Talk | contribs)

The author

Author of this code is 84plusfreak. He wrote this routine for the TI84 calculator, but it should work on the CPC without big modifications (just change the addresses which are used).

The Code

CRC32Init

CRC32Init:
 ;Inputs:
 ; A=0 -> Use 'default' Polynomial (the one used in the ZIP Format)
 ; A=1 -> Use custom Polynomial
 ;     -> DEHL=Polynomial
 ;Outputs:
 ; ($8292)=Polynomial
 ; ($8296)=Current CRC (~($8296)=CRC32)
 ;Destroys:
 ; AF,DE,HL
 or a
 jr nz,$+08
 ld de,$EDB8
 ld hl,$8320
 ld a,d
 ld d,e
 ld e,a
 ld a,h
 ld h,l
 ld l,a
 ld ($8292),de
 ld ($8294),hl
 ld hl,$FFFF
 ld ($8296),hl
 ld ($8298),hl
 ret


CRC32Update

CRC32Update:
 ;Inputs:
 ; HL -> Points to data
 ; BC -> Number of bytes
 ;Outputs:
 ; ($8296) = Updated
 ;Destroys:
 ; AF,BC,DE,HL
 ld a,b
 or c
 ret z
 push hl
 push bc
 ld b,(hl)
 call CRC32Update_Routine
 pop bc
 pop hl
 inc hl
 dec bc
 jr CRC32Update
CRC32Update_OneByte:
 ;Inputs:
 ; A = Byte to add to CRC
 ;Destroys:
 ; AF,BC,DE,HL
 ld b,a
CRC32Update_Routine:
 ld a,($8296)
 xor b
 call CRC32TableLookup
 ld a,($8297)
 xor l
 ld ($8296),a
 ld a,($8298)
 xor h
 ld ($8297),a
 ld a,($8299)
 xor e
 ld ($8298),a
 ld a,d
 ld ($8299),a
 ret
CRC32TableLookup:
 ld de,$0000
 ld h,d
 ld l,a
 ld b,8
CRC32TableLookupLoop:
 push bc
 or a
 rr d
 rr e
 rr h
 rr l
 jr nc,CRC32TableLookupLoopEnd
 push hl
 pop bc
 ld hl,$8292
 ld a,d
 xor (hl)
 inc hl
 ld d,a
 ld a,e
 xor (hl)
 inc hl
 ld e,a
 ld a,b
 xor (hl)
 inc hl
 ld b,a
 ld a,c
 xor (hl)
 ld c,a
 push bc
 pop hl
CRC32TableLookupLoopEnd:
 pop bc
 djnz CRC32TableLookupLoop
 ret


CRC32Finalize

CRC32Finalize:
 ;Inputs:
 ; None
 ;Outputs:
 ; DEHL = CRC32
 ;Destroys:
 ; AF,BC,DE,HL
 ld de,($8298)
 ld bc,($8296)
 ld hl,$FFFF
 push hl
 or a
 sbc hl,bc
 ex (sp),hl
 sbc hl,de
 ex de,hl
 pop hl
 ret