Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CRC-16 xmodem, reusable source code

0 views
Skip to first unread message

The Right Reverend Colin James III

unread,
Mar 17, 1996, 3:00:00 AM3/17/96
to
! Program crc16.tru by Colin James III
!
! Copyright 1996, CEC Services, LLC All Rights Reserved
! 2080 Kipling St, Lakewood, CO 80215-1502
!
! This True BASIC program is based on logic from "CRC PickBasic"
! by Dave Meagher, posted on 1/10/96 to comp.databases.pick, which
! in turn was based on code by Jim Todhunter from 9/94.
!
! For an explanation see Press et al, "Numerical Recipes
! in FORTRAN", 2nd ed, pp 889 ff.
!
! This performs XMODEM type CRC-16 checksum.
!
! For "T", the CRC is 6769 or hex 1A71.
! For "CatMouse987654321", the CRC is 58710 or hex F56E.
!
! This uses the CCITT polynomial of x^16 + x^12 + x^5 + 1.
! To effect CRC-CCITT, the input bits must be the reverse image
! of what they are below, and the resulting CRC's bits reversed as
! well. The respective CRC is 14A1 and C28D, with implementaion
! left as a trivial exercise for the reader.
!
! This may be reused only if the captioned copyright
! notice is included in the source code.
!
! Disclaimer: There is no expressed or implied warranty.
!

PROGRAM crc16

LIBRARY "HexLib.trc"
DECLARE DEF XOR

DIM tbl( 8)

MAT READ tbl ! powers of 2 table
DATA 256, 512, 1024, 2048, 4096, 8192, 16384, 32768

LET inp$ = "T" ! "CatMouse987654321"
LET crc = 0
LET lgth = len( inp$)

FOR i = 1 TO lgth ! input byte indexed loop

LET cval = 256 * ORD( inp$[i:i]) ! ord is asc
LET temp = REMAINDER( CRC, 256)

FOR j = 1 TO 8 ! bit indexed loop
! remainder is mod
LET p = tbl( j) ! ip is integer part of
LET q = REMAINDER( IP( crc / p), 2)
LET r = REMAINDER( IP( cval / p), 2)
LET s = XOR( q, r)

IF s > 0 THEN
LET temp = temp + p
END IF

NEXT j

LET crc = temp

FOR j = 1 to 8

LET r = REMAINDER( IP( crc / 32768), 2)

IF r > 0 THEN ! r > 0 is true
! r = 0 is false
LET crc = crc - 32768
LET crc = crc + crc
LET r = REMAINDER( crc, 2)

IF r > 0 THEN
LET crc = crc - 1
ELSE
LET crc = crc + 1
END IF

LET r = REMAINDER( IP( crc / 32), 2)

IF r > 0 THEN
LET crc = crc - 32
ELSE
LET crc = crc + 32
END IF

LET r = REMAINDER( IP( crc / 4096), 2)

IF r > 0 THEN
LET crc = crc - 4096
ELSE
LET crc = crc + 4096
END IF

ELSE

LET crc = crc + crc

END IF

NEXT j

NEXT i

PRINT "crc = "; crc ! numeric value, not hex

END ! program crc16


~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Colin James III, Principal Scientist cja...@cec-services.com
CEC Services, 2080 Kipling St, Lakewood, CO 80215-1502 USA
Voice: 303.231.9437; Facsimile: .231.9438; Data: .231.9434
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

0 new messages