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