006
23.10.2010, 18:36 Uhr
volkerp
Default Group and Edit
|
Im originalen Brenn-Programm von robotron wird CRC-CCITT / CRC-16 (x16 + x12 + x5 + 1) genutzt. Der Startwert ist 0xFFFF.
Quellcode: | ; CRC berechnen
crc: ld hl, 0FFFFh crc1: ld a, (de) xor h ld h, a rrca rrca rrca rrca and 0Fh xor h ld h, a rrca rrca rrca push af and 1Fh xor l ld l, a pop af push af rrca and 0F0h xor l ld l, a pop af and 0E0h xor h ld h, l ld l, a inc de dec bc ld a, b or c jr nz, crc1 ret
|
und als Perl-Programm
Quellcode: | #CRC-CCITT (CRC-16) x16 + x12 + x5 + 1 $POLY = 0x8408; # 16-12-5-1
#this is the CCITT CRC 16 polynomial X^16+X^12+X^5+1 #This works out to be 0x1021, but the way the algorithm works #lets us use 0x8408 (the reverse of the bit pattern). The high #bit is always assumed to be set, thus we only use 16 bits to #represent the 17 bit value. #1 0001 0000 0010 0001 16-12-5-1 #1000 0100 0000 1000 1 reverse -> 0x8408h
#Startwert $crc16 = 0xFFFF;
for ($i=0;$i<$len;$i++) { my $bits = ord(substr($buf,$i,1)); for (0..7) { if (($bits & 1) ^ ($crc16 & 1)) { $crc16 >>= 1; $crc16 ^= $POLY; } else { $crc16 >>= 1; } $bits >>= 1; } } printf "CRC = %.4X\n", $crc16;
|
-- VolkerP
http://hc-ddr.hucki.net (Z9001, Z1013, LC-80, ...) |