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

Subversion Repositories jtag_stapl_player

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sukhanov
/****************************************************************************/
2
/*                                                                                                                                                      */
3
/*      Module:                 jamcomp.c                                                                                               */
4
/*                                                                                                                                                      */
5
/*                                      Copyright (C) Altera Corporation 1997                                   */
6
/*                                                                                                                                                      */
7
/*      Description:    Contains the code for compressing and uncompressing             */
8
/*                                      Boolean array data.                                                                             */
9
/*                                                                                                                                                      */
10
/*                                      This algorithm works by searching previous bytes in the */
11
/*                                      data that match the current data. If a match is found,  */
12
/*                                      then the offset and length of the matching data can             */
13
/*                                      replace the actual data in the output.                                  */
14
/*                                                                                                                                                      */
15
/****************************************************************************/
16
 
17
/****************************************************************************/
18
/*                                                                                                                                                      */
19
/*      Actel version 1.1             May 2003                                                                  */
20
/*                                                                                                                                                      */
21
/****************************************************************************/
22
 
23
#include "jamexprt.h"
24
#include "jamdefs.h"
25
#include "jamcomp.h"
26
 
27
#define SHORT_BITS                      16
28
#define CHAR_BITS                       8
29
#define DATA_BLOB_LENGTH        3
30
#define MATCH_DATA_LENGTH       8192
31
 
32
/****************************************************************************/
33
/*                                                                                                                                                      */
34
 
35
short jam_bits_required(short n)
36
 
37
/*                                                                                                                                                      */
38
/*      Description:    Calculate the minimum number of bits required to                */
39
/*                                      represent n.                                                                                    */
40
/*                                                                                                                                                      */
41
/*      Returns:                Number of bits.                                                                                 */
42
/*                                                                                                                                                      */
43
/****************************************************************************/
44
{
45
        short   result = SHORT_BITS;
46
 
47
        if (n == 0) result = 1;
48
        else
49
        {
50
                /* Look for the highest non-zero bit position */
51
                while ((n & (1 << (SHORT_BITS - 1))) == 0)
52
                {
53
                        n = (short) (n << 1);
54
                        --result;
55
                }
56
        }
57
 
58
        return (result);
59
}
60
 
61
/****************************************************************************/
62
/*                                                                                                                                                      */
63
 
64
short jam_read_packed(char *buffer, long length, short bits)
65
 
66
/*                                                                                                                                                      */
67
/*      Description:    Read the next value from the input array "buffer".              */
68
/*                                      Read only "bits" bits from the array. The amount of             */
69
/*                                      bits that have already been read from "buffer" is               */
70
/*                                      stored internally to this function.                                             */
71
/*                                                                                                                                                      */
72
/*      Returns:                Up to 16 bit value. -1 if buffer overrun.                               */
73
/*                                                                                                                                                      */
74
/****************************************************************************/
75
{
76
        short                   result = -1;
77
        static long             index = 0L;
78
        static short    bits_avail = 0;
79
        short                   shift = 0;
80
 
81
        /* If buffer is NULL then initialize. */
82
        if (buffer == NULL)
83
        {
84
                index = 0;
85
                bits_avail = CHAR_BITS;
86
        }
87
        else
88
        {
89
                result = 0;
90
                while (result != -1 && bits > 0)
91
                {
92
                        result = (short) (result | (((buffer[index] >> (CHAR_BITS - bits_avail)) & (0xFF >> (CHAR_BITS - bits_avail))) << shift));
93
 
94
                        if (bits <= bits_avail)
95
                        {
96
                                result = (short) (result & (0xFFFF >> (SHORT_BITS - (bits + shift))));
97
                                bits_avail = (short) (bits_avail - bits);
98
                                bits = 0;
99
                        }
100
                        else
101
                        {
102
                                /* Check for buffer overflow. */
103
                                if (++index >= length) result = -1;
104
                                else
105
                                {
106
                                        shift = (short) (shift + bits_avail);
107
                                        bits = (short) (bits - bits_avail);
108
                                        bits_avail = CHAR_BITS;
109
                                }
110
                        }
111
                }
112
        }
113
 
114
        return (result);
115
}
116
 
117
/****************************************************************************/
118
/*                                                                                                                                                      */
119
 
120
long jam_uncompress
121
(
122
        char *in,
123
        long in_length,
124
        char *out,
125
        long out_length,
126
        int version
127
)
128
 
129
/*                                                                                                                                                      */
130
/*      Description:    Uncompress data in "in" and write result to     "out".          */
131
/*                                                                                                                                                      */
132
/*      Returns:                Length of uncompressed data. -1 if:                                             */
133
/*                                              1) out_length is too small                                                      */
134
/*                                              2) Internal error in the code                                           */
135
/*                                              3) in doesn't contain ACA compressed data.                      */
136
/*                                                                                                                                                      */
137
/****************************************************************************/
138
{
139
        long    i, j, data_length = 0L;
140
        short   offset, length;
141
        long    match_data_length = MATCH_DATA_LENGTH;
142
 
143
        if (version == 2) --match_data_length;
144
 
145
        jam_read_packed(NULL, 0, 0);
146
        for (i = 0; i < out_length; ++i) out[i] = 0;
147
 
148
        /* Read number of bytes in data. */
149
        for (i = 0; i < sizeof (in_length); ++i)
150
        {
151
                data_length = data_length | ((long) jam_read_packed(in, in_length, CHAR_BITS) << (long) (i * CHAR_BITS));
152
        }
153
 
154
        if (data_length > out_length) data_length = -1L;
155
        else
156
        {
157
                i = 0;
158
                while (i < data_length)
159
                {
160
                        /* A 0 bit indicates literal data. */
161
                        if (jam_read_packed(in, in_length, 1) == 0)
162
                        {
163
                                for (j = 0; j < DATA_BLOB_LENGTH; ++j)
164
                                {
165
                                        if (i < data_length)
166
                                        {
167
                                                out[i] = (char) jam_read_packed(in, in_length, CHAR_BITS);
168
                                                i++;
169
                                        }
170
                                }
171
                        }
172
                        else
173
                        {
174
                                /* A 1 bit indicates offset/length to follow. */
175
                                offset = jam_read_packed(in, in_length, jam_bits_required((short) (i > match_data_length ? match_data_length : i)));
176
                                length = jam_read_packed(in, in_length, CHAR_BITS);
177
 
178
                                for (j = 0; j < length; ++j)
179
                                {
180
                                        if (i < data_length)
181
                                        {
182
                                                out[i] = out[i - offset];
183
                                                i++;
184
                                        }
185
                                }
186
                        }
187
                }
188
        }
189
 
190
        return (data_length);
191
}

powered by: WebSVN 2.1.0

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