xref: /titanic_44/usr/src/lib/libfru/libfruraw/crcmodel.h (revision 6bbe05905a1c10a2703f95fb4912eb14b87f6670)
1*6bbe0590SSundeep Panicker /*
2*6bbe0590SSundeep Panicker  * CDDL HEADER START
3*6bbe0590SSundeep Panicker  *
4*6bbe0590SSundeep Panicker  * The contents of this file are subject to the terms of the
5*6bbe0590SSundeep Panicker  * Common Development and Distribution License (the "License").
6*6bbe0590SSundeep Panicker  * You may not use this file except in compliance with the License.
7*6bbe0590SSundeep Panicker  *
8*6bbe0590SSundeep Panicker  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6bbe0590SSundeep Panicker  * or http://www.opensolaris.org/os/licensing.
10*6bbe0590SSundeep Panicker  * See the License for the specific language governing permissions
11*6bbe0590SSundeep Panicker  * and limitations under the License.
12*6bbe0590SSundeep Panicker  *
13*6bbe0590SSundeep Panicker  * When distributing Covered Code, include this CDDL HEADER in each
14*6bbe0590SSundeep Panicker  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6bbe0590SSundeep Panicker  * If applicable, add the following below this CDDL HEADER, with the
16*6bbe0590SSundeep Panicker  * fields enclosed by brackets "[]" replaced with your own identifying
17*6bbe0590SSundeep Panicker  * information: Portions Copyright [yyyy] [name of copyright owner]
18*6bbe0590SSundeep Panicker  *
19*6bbe0590SSundeep Panicker  * CDDL HEADER END
20*6bbe0590SSundeep Panicker  */
21*6bbe0590SSundeep Panicker 
22*6bbe0590SSundeep Panicker /*
23*6bbe0590SSundeep Panicker  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*6bbe0590SSundeep Panicker  * Use is subject to license terms.
25*6bbe0590SSundeep Panicker  */
26*6bbe0590SSundeep Panicker 
27*6bbe0590SSundeep Panicker #ifndef	_CRCMODEL_H
28*6bbe0590SSundeep Panicker #define	_CRCMODEL_H
29*6bbe0590SSundeep Panicker 
30*6bbe0590SSundeep Panicker #ifdef SOLARIS_UNIX
31*6bbe0590SSundeep Panicker #include <sys/types.h>
32*6bbe0590SSundeep Panicker #else
33*6bbe0590SSundeep Panicker typedef long uint32_t;
34*6bbe0590SSundeep Panicker #endif
35*6bbe0590SSundeep Panicker 
36*6bbe0590SSundeep Panicker #ifdef __cplusplus
37*6bbe0590SSundeep Panicker extern "C" {
38*6bbe0590SSundeep Panicker #endif
39*6bbe0590SSundeep Panicker 
40*6bbe0590SSundeep Panicker /*
41*6bbe0590SSundeep Panicker  *
42*6bbe0590SSundeep Panicker  *                             Start of crcmodel.h
43*6bbe0590SSundeep Panicker  *
44*6bbe0590SSundeep Panicker  *
45*6bbe0590SSundeep Panicker  * Author : Ross Williams (ross@guest.adelaide.edu.au.).
46*6bbe0590SSundeep Panicker  * Date   : 3 June 1993.
47*6bbe0590SSundeep Panicker  * Status : Public domain.
48*6bbe0590SSundeep Panicker  *
49*6bbe0590SSundeep Panicker  * Description : This is the header (.h) file for the reference
50*6bbe0590SSundeep Panicker  * implementation of the Rocksoft^tm Model CRC Algorithm. For more
51*6bbe0590SSundeep Panicker  * information on the Rocksoft^tm Model CRC Algorithm, see the document
52*6bbe0590SSundeep Panicker  * titled "A Painless Guide to CRC Error Detection Algorithms" by Ross
53*6bbe0590SSundeep Panicker  * Williams (ross@guest.adelaide.edu.au.). This document is likely to be in
54*6bbe0590SSundeep Panicker  * "ftp.adelaide.edu.au/pub/rocksoft".
55*6bbe0590SSundeep Panicker  *
56*6bbe0590SSundeep Panicker  * Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia.
57*6bbe0590SSundeep Panicker  *
58*6bbe0590SSundeep Panicker  *
59*6bbe0590SSundeep Panicker  *
60*6bbe0590SSundeep Panicker  * How to Use This Package
61*6bbe0590SSundeep Panicker  * -----------------------
62*6bbe0590SSundeep Panicker  * Step 1: Declare a variable of type cm_t. Declare another variable
63*6bbe0590SSundeep Panicker  *         (p_cm say) of type p_cm_t and initialize it to point to the first
64*6bbe0590SSundeep Panicker  *         variable (e.g. p_cm_t p_cm = &cm_t).
65*6bbe0590SSundeep Panicker  *
66*6bbe0590SSundeep Panicker  * Step 2: Assign values to the parameter fields of the structure.
67*6bbe0590SSundeep Panicker  *         If you don't know what to assign, see the document cited earlier.
68*6bbe0590SSundeep Panicker  *         For example:
69*6bbe0590SSundeep Panicker  *            p_cm->cm_width = 16;
70*6bbe0590SSundeep Panicker  *            p_cm->cm_poly  = 0x8005L;
71*6bbe0590SSundeep Panicker  *            p_cm->cm_init  = 0L;
72*6bbe0590SSundeep Panicker  *            p_cm->cm_refin = TRUE;
73*6bbe0590SSundeep Panicker  *            p_cm->cm_refot = TRUE;
74*6bbe0590SSundeep Panicker  *            p_cm->cm_xorot = 0L;
75*6bbe0590SSundeep Panicker  *         Note: Poly is specified without its top bit (18005 becomes 8005).
76*6bbe0590SSundeep Panicker  *         Note: Width is one bit less than the raw poly width.
77*6bbe0590SSundeep Panicker  *
78*6bbe0590SSundeep Panicker  * Step 3: Initialize the instance with a call cm_ini(p_cm);
79*6bbe0590SSundeep Panicker  *
80*6bbe0590SSundeep Panicker  * Step 4: Process zero or more message bytes by placing zero or more
81*6bbe0590SSundeep Panicker  *         successive calls to cm_nxt. Example: cm_nxt(p_cm,ch);
82*6bbe0590SSundeep Panicker  *
83*6bbe0590SSundeep Panicker  * Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm);
84*6bbe0590SSundeep Panicker  *         If the CRC is a 16-bit value, it will be in the bottom 16 bits.
85*6bbe0590SSundeep Panicker  *
86*6bbe0590SSundeep Panicker  *
87*6bbe0590SSundeep Panicker  *
88*6bbe0590SSundeep Panicker  * Design Notes
89*6bbe0590SSundeep Panicker  * ------------
90*6bbe0590SSundeep Panicker  * PORTABILITY: This package has been coded very conservatively so that
91*6bbe0590SSundeep Panicker  * it will run on as many machines as possible. For example, all external
92*6bbe0590SSundeep Panicker  * identifiers have been restricted to 6 characters and all internal ones to
93*6bbe0590SSundeep Panicker  * 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid
94*6bbe0590SSundeep Panicker  * namespace collisions. This package is endian independent.
95*6bbe0590SSundeep Panicker  *
96*6bbe0590SSundeep Panicker  * EFFICIENCY: This package (and its interface) is not designed for
97*6bbe0590SSundeep Panicker  * speed. The purpose of this package is to act as a well-defined reference
98*6bbe0590SSundeep Panicker  * model for the specification of CRC algorithms. If you want speed, cook up
99*6bbe0590SSundeep Panicker  * a specific table-driven implementation as described in the document cited
100*6bbe0590SSundeep Panicker  * above. This package is designed for validation only; if you have found or
101*6bbe0590SSundeep Panicker  * implemented a CRC algorithm and wish to describe it as a set of parameters
102*6bbe0590SSundeep Panicker  * to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation
103*6bbe0590SSundeep Panicker  * should behave identically to this package under those parameters.
104*6bbe0590SSundeep Panicker  *
105*6bbe0590SSundeep Panicker  */
106*6bbe0590SSundeep Panicker 
107*6bbe0590SSundeep Panicker 
108*6bbe0590SSundeep Panicker /* The following definitions are extracted from my style header file which */
109*6bbe0590SSundeep Panicker /* would be cumbersome to distribute with this package. The DONE_STYLE is the */
110*6bbe0590SSundeep Panicker /* idempotence symbol used in my style header file. */
111*6bbe0590SSundeep Panicker 
112*6bbe0590SSundeep Panicker #ifndef DONE_STYLE
113*6bbe0590SSundeep Panicker 
114*6bbe0590SSundeep Panicker typedef unsigned bool;
115*6bbe0590SSundeep Panicker typedef unsigned char *p_ubyte_;
116*6bbe0590SSundeep Panicker 
117*6bbe0590SSundeep Panicker #ifndef TRUE
118*6bbe0590SSundeep Panicker #define	FALSE 0
119*6bbe0590SSundeep Panicker #define	TRUE  1
120*6bbe0590SSundeep Panicker #endif
121*6bbe0590SSundeep Panicker 
122*6bbe0590SSundeep Panicker /* Change to the second definition if you don't have prototypes. */
123*6bbe0590SSundeep Panicker #define	P_(A) A
124*6bbe0590SSundeep Panicker /* #define P_(A) () */
125*6bbe0590SSundeep Panicker 
126*6bbe0590SSundeep Panicker /* Uncomment this definition if you don't have void. */
127*6bbe0590SSundeep Panicker /* typedef int void; */
128*6bbe0590SSundeep Panicker 
129*6bbe0590SSundeep Panicker #endif
130*6bbe0590SSundeep Panicker 
131*6bbe0590SSundeep Panicker /* CRC Model Abstract Type */
132*6bbe0590SSundeep Panicker /* ----------------------- */
133*6bbe0590SSundeep Panicker /* The following type stores the context of an executing instance of the */
134*6bbe0590SSundeep Panicker /* model algorithm. Most of the fields are model parameters which must be */
135*6bbe0590SSundeep Panicker /* set before the first initializing call to cm_ini. */
136*6bbe0590SSundeep Panicker typedef struct
137*6bbe0590SSundeep Panicker {
138*6bbe0590SSundeep Panicker 	int cm_width; /* Parameter: Width in bits [8,32]. */
139*6bbe0590SSundeep Panicker 	uint32_t cm_poly; /* Parameter: The algorithm's polynomial. */
140*6bbe0590SSundeep Panicker 	uint32_t cm_init; /* Parameter: Initial register value. */
141*6bbe0590SSundeep Panicker 	bool cm_refin; /* Parameter: Reflect input bytes? */
142*6bbe0590SSundeep Panicker 	bool cm_refot; /* Parameter: Reflect output CRC? */
143*6bbe0590SSundeep Panicker 	uint32_t cm_xorot; /* Parameter: XOR this to output CRC. */
144*6bbe0590SSundeep Panicker 
145*6bbe0590SSundeep Panicker 	uint32_t cm_reg; /* Context: Context during execution. */
146*6bbe0590SSundeep Panicker } cm_t;
147*6bbe0590SSundeep Panicker typedef cm_t *p_cm_t;
148*6bbe0590SSundeep Panicker 
149*6bbe0590SSundeep Panicker /* Functions That Implement The Model */
150*6bbe0590SSundeep Panicker /* ---------------------------------- */
151*6bbe0590SSundeep Panicker /* The following functions animate the cm_t abstraction. */
152*6bbe0590SSundeep Panicker 
153*6bbe0590SSundeep Panicker void cm_ini P_((p_cm_t p_cm));
154*6bbe0590SSundeep Panicker /* Initializes the argument CRC model instance. */
155*6bbe0590SSundeep Panicker /* All parameter fields must be set before calling this. */
156*6bbe0590SSundeep Panicker 
157*6bbe0590SSundeep Panicker void cm_nxt P_((p_cm_t p_cm, int ch));
158*6bbe0590SSundeep Panicker /* Processes a single message byte [0,255]. */
159*6bbe0590SSundeep Panicker 
160*6bbe0590SSundeep Panicker void cm_blk P_((p_cm_t p_cm, p_ubyte_ blk_adr, uint32_t blk_len));
161*6bbe0590SSundeep Panicker /* Processes a block of message bytes. */
162*6bbe0590SSundeep Panicker 
163*6bbe0590SSundeep Panicker uint32_t cm_crc P_((p_cm_t p_cm));
164*6bbe0590SSundeep Panicker /* Returns the CRC value for the message bytes processed so far. */
165*6bbe0590SSundeep Panicker 
166*6bbe0590SSundeep Panicker /* Functions For Table Calculation */
167*6bbe0590SSundeep Panicker /* ------------------------------- */
168*6bbe0590SSundeep Panicker /* The following function can be used to calculate a CRC lookup table. */
169*6bbe0590SSundeep Panicker /* It can also be used at run-time to create or check static tables. */
170*6bbe0590SSundeep Panicker 
171*6bbe0590SSundeep Panicker uint32_t cm_tab P_((p_cm_t p_cm, int index));
172*6bbe0590SSundeep Panicker /* Returns the i'th entry for the lookup table for the specified algorithm. */
173*6bbe0590SSundeep Panicker /* The function examines the fields cm_width, cm_poly, cm_refin, and the */
174*6bbe0590SSundeep Panicker /* argument table index in the range [0,255] and returns the table entry in */
175*6bbe0590SSundeep Panicker /* the bottom cm_width bytes of the return value. */
176*6bbe0590SSundeep Panicker 
177*6bbe0590SSundeep Panicker #ifdef __cplusplus
178*6bbe0590SSundeep Panicker }
179*6bbe0590SSundeep Panicker #endif
180*6bbe0590SSundeep Panicker 
181*6bbe0590SSundeep Panicker #endif /* _CRCMODEL_H */
182