; CRC-32 checksum calculation zp_res checksum,4 zp_byte checksum_temp zp_byte checksum_off_ ; Turns CRC updating on/off. Allows nesting. ; Preserved: X, Y .macro crc_off inc checksum_off_ .endmacro .macro crc_on jsr crc_on_ .endmacro crc_on_: dec checksum_off_ jmi internal_error ; catch unbalanced crc calls rts ; Initializes checksum module init_crc: ; FALL THROUGH ; Clears checksum and turns it on ; Preserved: X, Y reset_crc: lda #0 sta checksum_off_ lda #$FF sta checksum sta checksum + 1 sta checksum + 2 sta checksum + 3 rts ; If enabled, updates checksum with byte in A ; Preserved: X, Y ; Time: 350 clocks average update_crc_: stx checksum_temp jmp :+ update_crc: stx checksum_temp ldx checksum_off_ bne @off : eor checksum ldx #8 @bit: lsr checksum+3 ror checksum+2 ror checksum+1 ror a bcc :+ sta checksum lda checksum+3 eor #$ED sta checksum+3 lda checksum+2 eor #$B8 sta checksum+2 lda checksum+1 eor #$83 sta checksum+1 lda checksum eor #$20 : dex bne @bit sta checksum @off: ldx checksum_temp rts ; Prints CRC-32 checksum as 8-character hex value print_crc: crc_off ; Print complement ldx #3 : lda checksum,x eor #$FF jsr print_hex dex bpl :- crc_on rts