]> git.sur5r.net Git - cc65/blob - libsrc/zlib/crc32.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / zlib / crc32.s
1 ;
2 ; Piotr Fusik, 14.11.2001
3 ;
4 ; unsigned long __fastcall__ crc32 (unsigned long crc, unsigned char* buf,
5 ;                                   unsigned len);
6 ;
7
8         .export _crc32
9
10         .import         compleax, incsp2, incsp4, popax, popeax
11         .importzp       sreg, ptr1, ptr2, tmp1, tmp2
12
13 POLYNOMIAL      =       $EDB88320
14
15 make_table:
16         ldx     #0
17 @L1:    lda     #0
18         sta     tmp2
19         sta     sreg
20         sta     sreg+1
21         ldy     #8
22         txa
23 @L2:    sta     tmp1
24         lsr     a
25         bcc     @L3
26         lda     sreg+1
27         lsr     a
28         eor     #(POLYNOMIAL>>24)&$FF
29         sta     sreg+1
30         lda     sreg
31         ror     a
32         eor     #(POLYNOMIAL>>16)&$FF
33         sta     sreg
34         lda     tmp2
35         ror     a
36         eor     #(POLYNOMIAL>>8)&$FF
37         sta     tmp2
38         lda     tmp1
39         ror     a
40         eor     #POLYNOMIAL&$FF
41         bcs     @L4     ; branch always
42 @L3:    rol     a
43         lsr     sreg+1
44         ror     sreg
45         ror     tmp2
46         ror     a
47 @L4:    dey
48         bne     @L2
49         sta     table_0,x
50         lda     tmp2
51         sta     table_1,x
52         lda     sreg
53         sta     table_2,x
54         lda     sreg+1
55         sta     table_3,x
56         inx
57         bne     @L1
58         inc     table_initialised
59 RET:
60         rts
61
62 _crc32:
63 ; ptr2 = (len & 0xff) == 0 ? len : len + 0x100;
64         tay
65         beq     @L1
66         inx
67 @L1:    sta     ptr2
68         stx     ptr2+1
69 ; ptr1 = buf
70         jsr     popax
71         sta     ptr1
72         stx     ptr1+1
73 ; if (buf == NULL) return 0;
74         ora     ptr1+1
75         beq     @L0
76 ; if (!tables_initialised) make_tables();
77         lda     table_initialised
78         bne     @dont_make
79         jsr     make_table
80 @dont_make:
81 ; eax = crc
82         jsr     popeax
83 ; if (len == 0) return crc;
84         ldy     ptr2
85         bne     @L2
86         ldy     ptr2+1
87         beq     RET
88 @L2:
89 ; eax = ~crc
90         jsr     compleax
91         stx     tmp2
92         ldy     #0
93 ; crc = (crc >> 8) ^ table[(crc & 0xff) ^ *p++];
94 @L3:    eor     (ptr1),y
95         tax
96         lda     table_0,x
97         eor     tmp2
98         sta     tmp1
99         lda     table_1,x
100         eor     sreg
101         sta     tmp2
102         lda     table_2,x
103         eor     sreg+1
104         sta     sreg
105         lda     table_3,x
106         sta     sreg+1
107         lda     tmp1
108         iny
109         bne     @L4
110         inc     ptr1+1
111 @L4:    dec     ptr2
112         bne     @L3
113         dec     ptr2+1
114         bne     @L3
115         ldx     tmp2
116         jmp     compleax
117
118 ; return 0L
119 @L0:    sta     sreg
120         sta     sreg+1
121 ; ignore crc
122         jmp     incsp4
123
124                 .data
125 table_initialised:
126                 .byte   0
127
128                 .bss
129 table_0:        .res    256
130 table_1:        .res    256
131 table_2:        .res    256
132 table_3:        .res    256
133
134