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