]> git.sur5r.net Git - rgb2r-2017/blob - crc16.s
Do not optimize
[rgb2r-2017] / crc16.s
1
2         .export _crc16_ccitt
3         .importzp tmp1, tmp2, tmp3
4         .import popax
5
6 .code
7
8 CRCLO   := tmp1
9 CRCHI   := tmp2
10
11 ;; Ad[ao]pted from http://6502.org/source/integers/crc-more.html (Greg Cook)
12
13 ;; uint16t __fastcall__ crc16_ccitt(uint16_t crc, uint8_t input)
14 .proc   _crc16_ccitt
15         STA tmp3
16         JSR popax
17         STA CRCLO
18         STX CRCHI
19         LDA tmp3
20
21         EOR CRCHI       ; A contained the data
22         STA CRCHI       ; XOR it into high byte
23         LSR             ; right shift A 4 bits
24         LSR             ; to make top of x^12 term
25         LSR             ; ($1...)
26         LSR
27         TAX             ; save it
28         ASL             ; then make top of x^5 term
29         EOR CRCLO       ; and XOR that with low byte
30         STA CRCLO       ; and save
31         TXA             ; restore partial term
32         EOR CRCHI       ; and update high byte
33         STA CRCHI       ; and save
34         ASL             ; left shift three
35         ASL             ; the rest of the terms
36         ASL             ; have feedback from x^12
37         TAX             ; save bottom of x^12
38         ASL             ; left shift two more
39         ASL             ; watch the carry flag
40         EOR CRCHI       ; bottom of x^5 ($..2.)
41         TAY             ; save high byte
42         TXA             ; fetch temp value
43         ROL             ; bottom of x^12, middle of x^5!
44         EOR CRCLO       ; finally update low byte
45         ;STA CRCHI      ; then swap high and low bytes
46         ;STY CRCLO
47
48         TAX             ; Store result in registers
49         TYA
50
51         RTS
52 .endproc