annotate Discovery/Src/crcmodel.c @ 71:c66e66f6faa7

remove wiki files from repo
author heinrichsweikamp
date Wed, 05 Sep 2018 10:41:48 +0200
parents 5f11787b4f42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 /* Start of crcmodel.c */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 /* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 /* Date : 3 June 1993. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 /* Status : Public domain. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 /* Description : This is the implementation (.c) file for the reference */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 /* information on the Rocksoft^tm Model CRC Algorithm, see the document */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 /* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 /* "ftp.adelaide.edu.au/pub/rocksoft". */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 /* Implementation Notes */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 /* -------------------- */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 /* To avoid inconsistencies, the specification of each function is not echoed */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 /* here. See the header file for a description of these functions. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 /* This package is light on checking because I want to keep it short and */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 /* simple and portable (i.e. it would be too messy to distribute my entire */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 /* C culture (e.g. assertions package) with this package. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 /* */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 #include "crcmodel.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 /* The following definitions make the code more readable. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 #define BITMASK(X) (1L << (X))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 #define MASK32 0xFFFFFFFFL
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #define LOCAL static
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 LOCAL ulong reflect P_((ulong v,int b));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 LOCAL ulong reflect (ulong v,int b)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 /* Returns the value v with the bottom b [0,32] bits reflected. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 /* Example: reflect(0x3e23L,3) == 0x3e26 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 int i;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 ulong t = v;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 for (i=0; i<b; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 if (t & 1L)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 v|= BITMASK((b-1)-i);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 v&= ~BITMASK((b-1)-i);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 t>>=1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 return v;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 LOCAL ulong widmask P_((p_cm_t));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 LOCAL ulong widmask (p_cm_t p_cm)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 /* Returns a longword whose value is (2^p_cm->cm_width)-1. */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 /* The trick is to do this portably (e.g. without doing <<32). */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 return (((1L<<(p_cm->cm_width-1))-1L)<<1)|1L;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 void cm_ini (p_cm_t p_cm)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 p_cm->cm_reg = p_cm->cm_init;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 void cm_nxt (p_cm_t p_cm, int ch)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 int i;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 ulong uch = (ulong) ch;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 ulong topbit = BITMASK(p_cm->cm_width-1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 if (p_cm->cm_refin) uch = reflect(uch,8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 p_cm->cm_reg ^= (uch << (p_cm->cm_width-8));
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 for (i=0; i<8; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 if (p_cm->cm_reg & topbit)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 p_cm->cm_reg = (p_cm->cm_reg << 1) ^ p_cm->cm_poly;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 p_cm->cm_reg <<= 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 p_cm->cm_reg &= widmask(p_cm);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 void cm_blk (p_cm_t p_cm,p_ubyte_ blk_adr,ulong blk_len)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 while (blk_len--) cm_nxt(p_cm,*blk_adr++);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 ulong cm_crc (p_cm_t p_cm)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 if (p_cm->cm_refot)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 return p_cm->cm_xorot ^ reflect(p_cm->cm_reg,p_cm->cm_width);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 return p_cm->cm_xorot ^ p_cm->cm_reg;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 ulong cm_tab (p_cm_t p_cm, int index)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 int i;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 ulong r;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 ulong topbit = BITMASK(p_cm->cm_width-1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 ulong inbyte = (ulong) index;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 if (p_cm->cm_refin) inbyte = reflect(inbyte,8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 r = inbyte << (p_cm->cm_width-8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 for (i=0; i<8; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 if (r & topbit)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 r = (r << 1) ^ p_cm->cm_poly;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 r<<=1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 if (p_cm->cm_refin) r = reflect(r,p_cm->cm_width);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 return r & widmask(p_cm);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 /* End of crcmodel.c */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 /******************************************************************************/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137