2003-09-23 20:11:39 +00:00
|
|
|
;
|
2018-07-27 13:07:40 +00:00
|
|
|
; 2001-11-14, Piotr Fusik
|
|
|
|
; 2018-05-20, Christian Kruger
|
2003-09-23 20:11:39 +00:00
|
|
|
;
|
2021-03-23 07:30:08 +00:00
|
|
|
; unsigned long __fastcall__ crc32 (unsigned long crc,
|
|
|
|
; const unsigned char* buf,
|
2003-09-23 20:11:39 +00:00
|
|
|
; unsigned len);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _crc32
|
2003-09-23 20:11:39 +00:00
|
|
|
|
2018-05-20 13:30:18 +00:00
|
|
|
.import compleax, incsp2, incsp4, popptr1, popeax
|
2013-05-09 11:56:54 +00:00
|
|
|
.importzp sreg, ptr1, ptr2, tmp1, tmp2
|
2003-09-23 20:11:39 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
POLYNOMIAL = $EDB88320
|
2003-09-23 20:11:39 +00:00
|
|
|
|
|
|
|
make_table:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #0
|
|
|
|
@L1: lda #0
|
|
|
|
sta tmp2
|
|
|
|
sta sreg
|
|
|
|
sta sreg+1
|
|
|
|
ldy #8
|
|
|
|
txa
|
|
|
|
@L2: sta tmp1
|
|
|
|
lsr a
|
|
|
|
bcc @L3
|
|
|
|
lda sreg+1
|
|
|
|
lsr a
|
|
|
|
eor #(POLYNOMIAL>>24)&$FF
|
|
|
|
sta sreg+1
|
|
|
|
lda sreg
|
|
|
|
ror a
|
|
|
|
eor #(POLYNOMIAL>>16)&$FF
|
|
|
|
sta sreg
|
|
|
|
lda tmp2
|
|
|
|
ror a
|
|
|
|
eor #(POLYNOMIAL>>8)&$FF
|
|
|
|
sta tmp2
|
|
|
|
lda tmp1
|
|
|
|
ror a
|
|
|
|
eor #POLYNOMIAL&$FF
|
|
|
|
bcs @L4 ; branch always
|
|
|
|
@L3: rol a
|
|
|
|
lsr sreg+1
|
|
|
|
ror sreg
|
|
|
|
ror tmp2
|
|
|
|
ror a
|
|
|
|
@L4: dey
|
|
|
|
bne @L2
|
|
|
|
sta table_0,x
|
|
|
|
lda tmp2
|
|
|
|
sta table_1,x
|
|
|
|
lda sreg
|
|
|
|
sta table_2,x
|
|
|
|
lda sreg+1
|
|
|
|
sta table_3,x
|
|
|
|
inx
|
|
|
|
bne @L1
|
|
|
|
inc table_initialised
|
2003-09-23 20:11:39 +00:00
|
|
|
RET:
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2003-09-23 20:11:39 +00:00
|
|
|
|
|
|
|
_crc32:
|
|
|
|
; ptr2 = (len & 0xff) == 0 ? len : len + 0x100;
|
2013-05-09 11:56:54 +00:00
|
|
|
tay
|
|
|
|
beq @L1
|
|
|
|
inx
|
|
|
|
@L1: sta ptr2
|
|
|
|
stx ptr2+1
|
2003-09-23 20:11:39 +00:00
|
|
|
; ptr1 = buf
|
2018-05-20 13:30:18 +00:00
|
|
|
jsr popptr1
|
2003-09-23 20:11:39 +00:00
|
|
|
; if (buf == NULL) return 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
ora ptr1+1
|
|
|
|
beq @L0
|
2003-09-23 20:11:39 +00:00
|
|
|
; if (!tables_initialised) make_tables();
|
2013-05-09 11:56:54 +00:00
|
|
|
lda table_initialised
|
|
|
|
bne @dont_make
|
|
|
|
jsr make_table
|
2003-09-23 20:11:39 +00:00
|
|
|
@dont_make:
|
|
|
|
; eax = crc
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popeax
|
2003-09-23 20:11:39 +00:00
|
|
|
; if (len == 0) return crc;
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy ptr2
|
|
|
|
bne @L2
|
|
|
|
ldy ptr2+1
|
|
|
|
beq RET
|
2003-09-23 20:11:39 +00:00
|
|
|
@L2:
|
|
|
|
; eax = ~crc
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr compleax
|
|
|
|
stx tmp2
|
|
|
|
ldy #0
|
2003-09-23 20:11:39 +00:00
|
|
|
; crc = (crc >> 8) ^ table[(crc & 0xff) ^ *p++];
|
2013-05-09 11:56:54 +00:00
|
|
|
@L3: eor (ptr1),y
|
|
|
|
tax
|
|
|
|
lda table_0,x
|
|
|
|
eor tmp2
|
|
|
|
sta tmp1
|
|
|
|
lda table_1,x
|
|
|
|
eor sreg
|
|
|
|
sta tmp2
|
|
|
|
lda table_2,x
|
|
|
|
eor sreg+1
|
|
|
|
sta sreg
|
|
|
|
lda table_3,x
|
|
|
|
sta sreg+1
|
|
|
|
lda tmp1
|
|
|
|
iny
|
|
|
|
bne @L4
|
|
|
|
inc ptr1+1
|
|
|
|
@L4: dec ptr2
|
|
|
|
bne @L3
|
|
|
|
dec ptr2+1
|
|
|
|
bne @L3
|
|
|
|
ldx tmp2
|
|
|
|
jmp compleax
|
2003-09-23 20:11:39 +00:00
|
|
|
|
|
|
|
; return 0L
|
2013-05-09 11:56:54 +00:00
|
|
|
@L0: sta sreg
|
|
|
|
sta sreg+1
|
2018-07-27 13:07:40 +00:00
|
|
|
tax ; (popptr1 doesn't set .X)
|
2003-09-23 20:11:39 +00:00
|
|
|
; ignore crc
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp incsp4
|
2003-09-23 20:11:39 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.data
|
2003-09-23 20:11:39 +00:00
|
|
|
table_initialised:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 0
|
2003-09-23 20:11:39 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.bss
|
|
|
|
table_0: .res 256
|
|
|
|
table_1: .res 256
|
|
|
|
table_2: .res 256
|
|
|
|
table_3: .res 256
|