comparison Discovery/Inc/crcmodel.h @ 38:5f11787b4f42

include in ostc4 repository
author heinrichsweikamp
date Sat, 28 Apr 2018 11:52:34 +0200
parents
children 45b8f3c2acce
comparison
equal deleted inserted replaced
37:ccc45c0e1ea2 38:5f11787b4f42
1 ///////////////////////////////////////////////////////////////////////////////
2 /// -*- coding: UTF-8 -*-
3 ///
4 /// \file Discovery/Inc/crcmodel.h
5 /// \brief Rocksoft CRC Model Algorithm
6 /// \author Ross Williams (ross@guest.adelaide.edu.au.) and Heinrichs Weikamp
7 /// \date 3 June 1993
8 ///
9 /// \details
10 ///
11 /// This is the header (.h) file for the reference
12 /// implementation of the Rocksoft^tm Model CRC Algorithm. For more
13 /// information on the Rocksoft^tm Model CRC Algorithm, see the document
14 /// titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
15 /// Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
16 /// "ftp.adelaide.edu.au/pub/rocksoft".
17 ///
18 /// Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
19 ///
20 /// $Id$
21 ///////////////////////////////////////////////////////////////////////////////
22 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
23 ///
24 /// This program is free software: you can redistribute it and/or modify
25 /// it under the terms of the GNU General Public License as published by
26 /// the Free Software Foundation, either version 3 of the License, or
27 /// (at your option) any later version.
28 ///
29 /// This program is distributed in the hope that it will be useful,
30 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
31 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 /// GNU General Public License for more details.
33 ///
34 /// You should have received a copy of the GNU General Public License
35 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
36 //////////////////////////////////////////////////////////////////////////////
37
38 /******************************************************************************/
39 /* Start of crcmodel.h */
40 /******************************************************************************/
41 /* */
42 /* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
43 /* Date : 3 June 1993. */
44 /* Status : Public domain. */
45 /* */
46 /* Description : This is the header (.h) file for the reference */
47 /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
48 /* information on the Rocksoft^tm Model CRC Algorithm, see the document */
49 /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
50 /* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
51 /* "ftp.adelaide.edu.au/pub/rocksoft". */
52 /* */
53 /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
54 /* */
55 /******************************************************************************/
56 /* */
57 /* How to Use This Package */
58 /* ----------------------- */
59 /* Step 1: Declare a variable of type cm_t. Declare another variable */
60 /* (p_cm say) of type p_cm_t and initialize it to point to the first */
61 /* variable (e.g. p_cm_t p_cm = &cm_t). */
62 /* */
63 /* Step 2: Assign values to the parameter fields of the structure. */
64 /* If you don't know what to assign, see the document cited earlier. */
65 /* For example: */
66 /* p_cm->cm_width = 16; */
67 /* p_cm->cm_poly = 0x8005L; */
68 /* p_cm->cm_init = 0L; */
69 /* p_cm->cm_refin = TRUE; */
70 /* p_cm->cm_refot = TRUE; */
71 /* p_cm->cm_xorot = 0L; */
72 /* Note: Poly is specified without its top bit (18005 becomes 8005). */
73 /* Note: Width is one bit less than the raw poly width. */
74 /* */
75 /* Step 3: Initialize the instance with a call cm_ini(p_cm); */
76 /* */
77 /* Step 4: Process zero or more message bytes by placing zero or more */
78 /* successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); */
79 /* */
80 /* Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); */
81 /* If the CRC is a 16-bit value, it will be in the bottom 16 bits. */
82 /* */
83 /******************************************************************************/
84 /* */
85 /* Design Notes */
86 /* ------------ */
87 /* PORTABILITY: This package has been coded very conservatively so that */
88 /* it will run on as many machines as possible. For example, all external */
89 /* identifiers have been restricted to 6 characters and all internal ones to */
90 /* 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid */
91 /* namespace collisions. This package is endian independent. */
92 /* */
93 /* EFFICIENCY: This package (and its interface) is not designed for */
94 /* speed. The purpose of this package is to act as a well-defined reference */
95 /* model for the specification of CRC algorithms. If you want speed, cook up */
96 /* a specific table-driven implementation as described in the document cited */
97 /* above. This package is designed for validation only; if you have found or */
98 /* implemented a CRC algorithm and wish to describe it as a set of parameters */
99 /* to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation */
100 /* should behave identically to this package under those parameters. */
101 /* */
102 /******************************************************************************/
103
104 #ifndef CRC_MODEL_H
105 #define CRC_MODEL_H
106
107 /******************************************************************************/
108
109 /* The following definitions are extracted from my style header file which */
110 /* would be cumbersome to distribute with this package. The DONE_STYLE is the */
111 /* idempotence symbol used in my style header file. */
112
113 #ifndef DONE_STYLE
114
115 typedef unsigned long ulong;
116 typedef unsigned bool;
117 typedef unsigned char * p_ubyte_;
118
119 #ifndef TRUE
120 #define FALSE 0
121 #define TRUE 1
122 #endif
123
124 /* Change to the second definition if you don't have prototypes. */
125 #define P_(A) A
126 /* #define P_(A) () */
127
128 /* Uncomment this definition if you don't have void. */
129 /* typedef int void; */
130
131 #endif
132
133 /******************************************************************************/
134
135 /* CRC Model Abstract Type */
136 /* ----------------------- */
137 /* The following type stores the context of an executing instance of the */
138 /* model algorithm. Most of the fields are model parameters which must be */
139 /* set before the first initializing call to cm_ini. */
140 typedef struct
141 {
142 int cm_width; /* Parameter: Width in bits [8,32]. */
143 ulong cm_poly; /* Parameter: The algorithm's polynomial. */
144 ulong cm_init; /* Parameter: Initial register value. */
145 bool cm_refin; /* Parameter: Reflect input bytes? */
146 bool cm_refot; /* Parameter: Reflect output CRC? */
147 ulong cm_xorot; /* Parameter: XOR this to output CRC. */
148
149 ulong cm_reg; /* Context: Context during execution. */
150 } cm_t;
151 typedef cm_t *p_cm_t;
152
153 /******************************************************************************/
154
155 /* Functions That Implement The Model */
156 /* ---------------------------------- */
157 /* The following functions animate the cm_t abstraction. */
158
159 void cm_ini P_((p_cm_t p_cm));
160 /* Initializes the argument CRC model instance. */
161 /* All parameter fields must be set before calling this. */
162
163 void cm_nxt P_((p_cm_t p_cm,int ch));
164 /* Processes a single message byte [0,255]. */
165
166 void cm_blk P_((p_cm_t p_cm,p_ubyte_ blk_adr,ulong blk_len));
167 /* Processes a block of message bytes. */
168
169 ulong cm_crc P_((p_cm_t p_cm));
170 /* Returns the CRC value for the message bytes processed so far. */
171
172 /******************************************************************************/
173
174 /* Functions For Table Calculation */
175 /* ------------------------------- */
176 /* The following function can be used to calculate a CRC lookup table. */
177 /* It can also be used at run-time to create or check static tables. */
178
179 ulong cm_tab P_((p_cm_t p_cm,int index));
180 /* Returns the i'th entry for the lookup table for the specified algorithm. */
181 /* The function examines the fields cm_width, cm_poly, cm_refin, and the */
182 /* argument table index in the range [0,255] and returns the table entry in */
183 /* the bottom cm_width bytes of the return value. */
184
185 /******************************************************************************/
186
187 #endif // CRC_MODEL_H