OpenCores
URL https://opencores.org/ocsvn/jtag_stapl_player/jtag_stapl_player/trunk

Subversion Repositories jtag_stapl_player

[/] [jtag_stapl_player/] [trunk/] [jamcrc.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sukhanov
/****************************************************************************/
2
/*                                                                                                                                                      */
3
/*      Module:                 jamcrc.c                                                                                                */
4
/*                                                                                                                                                      */
5
/*                                      Copyright (C) Altera Corporation 1997                                   */
6
/*                                                                                                                                                      */
7
/*      Description:    Functions to calculate Cyclic Redundancy Check codes    */
8
/*                                                                                                                                                      */
9
/****************************************************************************/
10
 
11
/****************************************************************************/
12
/*                                                                                                                                                      */
13
/*      Actel version 1.1             May 2003                                                                  */
14
/*                                                                                                                                                      */
15
/****************************************************************************/
16
 
17
#include "jamexprt.h"
18
#include "jamdefs.h"
19
#include "jamexec.h"
20
#include "jamutil.h"
21
 
22
/****************************************************************************/
23
/*                                                                                                                                                      */
24
 
25
void jam_crc_init(unsigned short *shift_register)
26
 
27
/*                                                                                                                                                      */
28
/*      Description:    This function initializes CRC shift register.  It must  */
29
/*                                      be called before jam_crc_update().                                              */
30
/*                                                                                                                                                      */
31
/*      Returns:                Nothing                                                                                                 */
32
/*                                                                                                                                                      */
33
/****************************************************************************/
34
{
35
        *shift_register = 0xffff;       /* start with all ones in shift reg */
36
}
37
 
38
/****************************************************************************/
39
/*                                                                                                                                                      */
40
 
41
void jam_crc_update
42
(
43
        unsigned short *shift_register,
44
        int data
45
)
46
 
47
/*                                                                                                                                                      */
48
/*      Description:    This function updates crc shift register by shifting    */
49
/*                                      in the new data bits.  Must be called for each bytes in */
50
/*                                      the order that they appear in the data stream.                  */
51
/*                                                                                                                                                      */
52
/*      Returns:                Nothing                                                                                                 */
53
/*                                                                                                                                                      */
54
/****************************************************************************/
55
{
56
        int bit, feedback;
57
        unsigned short shift_register_copy;
58
 
59
        shift_register_copy = *shift_register;  /* copy it to local variable */
60
 
61
        for (bit = 0; bit < 8; bit++)    /* compute for each bit */
62
        {
63
                feedback = (data ^ shift_register_copy) & 0x01;
64
                shift_register_copy >>= 1;      /* shift the shift register */
65
                if (feedback)
66
                {
67
                        shift_register_copy ^= 0x8408;  /* invert selected bits */
68
                }
69
                data >>= 1;             /* get the next bit of input_byte */
70
        }
71
 
72
        *shift_register = shift_register_copy;
73
}
74
 
75
/****************************************************************************/
76
/*                                                                                                                                                      */
77
 
78
unsigned short jam_get_crc_value(unsigned short *shift_register)
79
 
80
/*                                                                                                                                                      */
81
/*      Description:    The content of the shift_register is the CRC of all             */
82
/*                                      bytes passed to jam_crc_update() since the last call    */
83
/*                                      to jam_crc_init().                                                                              */
84
/*                                                                                                                                                      */
85
/*      Returns:                CRC value from shift register.                                                  */
86
/*                                                                                                                                                      */
87
/***************************************************************************/
88
{
89
        /* CRC is complement of shift register */
90
        return((unsigned short)~(*shift_register));
91
}
92
 
93
int jam_hexchar(int ch)
94
{
95
        int value;
96
 
97
        if (jam_isdigit((char) ch)) value = (ch - '0');
98
        else value = (jam_toupper((char) ch) - 'A') + 10;
99
 
100
        return (value);
101
}
102
 
103
/****************************************************************************/
104
/*                                                                                                                                                      */
105
 
106
JAM_RETURN_TYPE jam_check_crc
107
(
108
        char *program,
109
        long program_size,
110
        unsigned short *expected_crc,
111
        unsigned short *actual_crc
112
)
113
 
114
/*                                                                                                                                                      */
115
/*      Description:    This function reads the entire input stream and                 */
116
/*                                      computes the CRC of everything up to the CRC statement  */
117
/*                                      itself (and the preceding new-line, if applicable).             */
118
/*                                      Carriage return characters (0x0d) which are followed    */
119
/*                                      by new-line characters (0x0a) are ignored, so the CRC   */
120
/*                                      will not change when the file is converted from MS-DOS  */
121
/*                                      text format (with CR-LF) to UNIX text format (only LF)  */
122
/*                                      and visa-versa.                                                                                 */
123
/*                                                                                                                                                      */
124
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
125
/*                                                                                                                                                      */
126
/****************************************************************************/
127
{
128
        BOOL comment = FALSE;
129
        BOOL quoted_string = FALSE;
130
        BOOL in_statement = FALSE;
131
        BOOL in_instruction = FALSE;
132
        BOOL found_expected_crc = FALSE;
133
        int ch = 0;
134
        long position = 0L;
135
        long left_quote_position = -1L;
136
        unsigned short crc_shift_register = 0;
137
        unsigned short crc_shift_register_backup[4] = {0};
138
        int ch_queue[4] = {0};
139
        unsigned short tmp_expected_crc = 0;
140
        unsigned short tmp_actual_crc = 0;
141
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
142
 
143
        jam_program = program;
144
        jam_program_size = program_size;
145
 
146
        status = jam_seek(0);
147
 
148
        jam_crc_init(&crc_shift_register);
149
 
150
        while ((status == JAMC_SUCCESS) && (!found_expected_crc))
151
        {
152
                ch = jam_getc();
153
 
154
                if ((ch != EOF) && (ch != JAMC_RETURN_CHAR))
155
                {
156
                        jam_crc_update(&crc_shift_register, ch);
157
 
158
                        if ((!comment) && (!quoted_string))
159
                        {
160
                                if (ch == JAMC_COMMENT_CHAR)
161
                                {
162
                                        /* beginning of comment */
163
                                        comment = TRUE;
164
                                }
165
                                else if (ch == JAMC_QUOTE_CHAR)
166
                                {
167
                                        /* beginning of quoted string */
168
                                        quoted_string = TRUE;
169
                                        left_quote_position = position;
170
                                }
171
                        }
172
 
173
                        /*
174
                        *       Check if this is the CRC statement
175
                        */
176
                        if ((!comment) && (!quoted_string) &&
177
                                in_statement && in_instruction &&
178
                                (jam_isspace((char) ch_queue[3])) &&
179
                                (ch_queue[2] == 'C') &&
180
                                (ch_queue[1] == 'R') &&
181
                                (ch_queue[0] == 'C') &&
182
                                (jam_isspace((char) ch)))
183
                        {
184
                                status = JAMC_SYNTAX_ERROR;
185
                                crc_shift_register = crc_shift_register_backup[3];
186
 
187
                                /* skip over any additional white space */
188
                                do { ch = jam_getc(); } while
189
                                        ((ch != EOF) && (jam_isspace((char) ch)));
190
 
191
                                if (jam_is_hex_char((char) ch))
192
                                {
193
                                        /* get remaining three characters of CRC */
194
                                        ch_queue[2] = jam_getc();
195
                                        ch_queue[1] = jam_getc();
196
                                        ch_queue[0] = jam_getc();
197
 
198
                                        if ((jam_is_hex_char((char) ch_queue[2])) &&
199
                                                (jam_is_hex_char((char) ch_queue[1])) &&
200
                                                (jam_is_hex_char((char) ch_queue[0])))
201
                                        {
202
                                                tmp_expected_crc = (unsigned short)
203
                                                        ((jam_hexchar(ch) << 12) |
204
                                                        (jam_hexchar(ch_queue[2]) << 8) |
205
                                                        (jam_hexchar(ch_queue[1]) << 4) |
206
                                                        jam_hexchar(ch_queue[0]));
207
 
208
                                                /* skip over any additional white space */
209
                                                do { ch = jam_getc(); } while
210
                                                        ((ch != EOF) && (jam_isspace((char) ch)));
211
 
212
                                                if (ch == JAMC_SEMICOLON_CHAR)
213
                                                {
214
                                                        status = JAMC_SUCCESS;
215
                                                        found_expected_crc = TRUE;
216
                                                }
217
                                        }
218
                                }
219
                        }
220
 
221
                        /* check if we are reading the instruction name */
222
                        if ((!comment) && (!quoted_string) && (!in_statement) &&
223
                                (jam_is_name_char((char) ch)))
224
                        {
225
                                in_statement = TRUE;
226
                                in_instruction = TRUE;
227
                        }
228
 
229
                        /* check if we are finished reading the instruction name */
230
                        if ((!comment) && (!quoted_string) && in_statement &&
231
                                in_instruction && (!jam_is_name_char((char) ch)))
232
                        {
233
                                in_instruction = FALSE;
234
                        }
235
 
236
                        if ((!comment) && (!quoted_string) && in_statement &&
237
                                (ch == JAMC_SEMICOLON_CHAR))
238
                        {
239
                                /* end of statement */
240
                                in_statement = FALSE;
241
                        }
242
 
243
                        if (comment &&
244
                                ((ch == JAMC_NEWLINE_CHAR) || (ch == JAMC_RETURN_CHAR)))
245
                        {
246
                                /* end of comment */
247
                                comment = FALSE;
248
                        }
249
                        else if (quoted_string && (ch == JAMC_QUOTE_CHAR) &&
250
                                (position > left_quote_position))
251
                        {
252
                                /* end of quoted string */
253
                                quoted_string = FALSE;
254
                        }
255
                }
256
 
257
                if (ch == EOF)
258
                {
259
                        /* end of file */
260
                        status = JAMC_UNEXPECTED_END;
261
                }
262
 
263
                ++position;     /* position of next character to be read */
264
 
265
                if (ch != JAMC_RETURN_CHAR)
266
                {
267
                        ch_queue[3] = ch_queue[2];
268
                        ch_queue[2] = ch_queue[1];
269
                        ch_queue[1] = ch_queue[0];
270
                        ch_queue[0] = ch;
271
 
272
                        crc_shift_register_backup[3] = crc_shift_register_backup[2];
273
                        crc_shift_register_backup[2] = crc_shift_register_backup[1];
274
                        crc_shift_register_backup[1] = crc_shift_register_backup[0];
275
                        crc_shift_register_backup[0] = crc_shift_register;
276
                }
277
        }
278
 
279
        tmp_actual_crc = jam_get_crc_value(&crc_shift_register);
280
 
281
        if (found_expected_crc && (expected_crc != NULL))
282
        {
283
                *expected_crc = tmp_expected_crc;
284
        }
285
 
286
        if (actual_crc != NULL)
287
        {
288
                *actual_crc = tmp_actual_crc;
289
        }
290
 
291
        if (found_expected_crc && (status == JAMC_SUCCESS) &&
292
                (tmp_expected_crc != tmp_actual_crc))
293
        {
294
                status = JAMC_CRC_ERROR;
295
        }
296
 
297
        return (status);
298
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.