-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc16.asm
67 lines (64 loc) · 3.22 KB
/
crc16.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;-------------------------------------------------------------------------------
;
; Author: Ivo Filot <ivo@ivofilot.nl>
;
; P2000T-SDCARD is free software:
; you can redistribute it and/or modify it under the terms of the
; GNU General Public License as published by the Free Software
; Foundation, either version 3 of the License, or (at your option)
; any later version.
;
; P2000T-SDCARD is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty
; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
; See the GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see http://www.gnu.org/licenses/.
;
;-------------------------------------------------------------------------------
SECTION code_user
PUBLIC _crc16_intram
;-------------------------------------------------------------------------------
; Generate a 16 bit checksum of internal RAM
;
; input: bc - number of bytes
; hl - start of memory address
; output: hl - crc16 checksum
; uses: a, bc, de, hl
;
; source: https://mdfs.net/Info/Comp/Comms/CRC16.htm
;-------------------------------------------------------------------------------
_crc16_intram:
pop de ; return address
pop hl ; ramptr
pop bc ; number of bytes
push de ; put return address back on stack
ld de,$0000 ; set de to $0000
nextbyte:
push bc ; push counter onto stack
ld a, (hl) ; read byte from ram chip
xor d ; xor byte into CRC top byte
ld b,8 ; prepare to rotate 8 bits
rot:
sla e ; rotate crc
adc a,a
jp nc,clr ; bit 15 was zero
ld d,a ; put crc high byte back into d
ld a,e ; crc = crc ^ $1021 (xmodem polynomic)
xor $21
ld e,a
ld a,d ; get crc top byte back into a
xor $10
clr:
dec b ; decrement bit counter
jp nz,rot ; loop for 8 bits
ld d,a ; put crc top byte back into d
inc hl ; step to next byte
pop bc ; get counter back from stack
dec bc ; decrement counter
ld a,b ; check if counter is zero
or c
jp nz,nextbyte ; if not zero, go to next byte
ex de,hl ; swap de and hl such that hl contains crc
ret ; return value is stored in hl