Debian Codegraph @

protolib/examples/lfsrExample.cpp

raw
1 // This program validates and illustrates the use of the ProtoLFSR class
2
3 #include "protoTime.h"
4 #include "protoLFSR.h"
5 #include <stdio.h>
6 #include <stdlib.h> // for rand(), srand()
7
8 int main(int argc, char* argv[])
9 {
10 ProtoTime t;
11 t.GetCurrentTime();
12 srand(t.usec());
13
14 ProtoLFSR::Polynomial poly = ProtoLFSR::PN31;
15
16 // This section validates get next/prev _bit_
17 // (for both ProtoLFSR and ProtoLFSRX)
18 ProtoLFSR pn(poly);
19 for (int i = 0; i < 22; i++) pn.GetNextByte();
20
21 const int TEST_BYTES = 10;
22 printf("Generating seq: ");
23 UINT8 testBuffer[TEST_BYTES];
24 for (int i = 0; i< TEST_BYTES; i++)
25 {
26 UINT8 byte = pn.GetNextByte();
27 testBuffer[i] = byte;
28 printf("%d, ", byte);
29 }
30 printf("\n");
31
32 pn.Reset();
33 pn.Sync((char*)testBuffer, TEST_BYTES);
34 printf("poly: 0x%08x bits:%u state: 0x%08x\n",
35 pn.GetPolynomial(), pn.GetNumBits(), pn.GetState());
36 printf(" Syncing seq: ");
37 for (int i = 0; i< TEST_BYTES; i++)
38 {
39 UINT8 byte = pn.GetNextByte();
40 printf("%d, ", byte);
41 }
42 printf("\n");
43
44 // This validates our Berlekamp-Massey "ComputePolynomial()" method
45 printf(" Random seq: ");
46 for (int i = 0; i < TEST_BYTES; i++)
47 {
48 UINT8 byte = rand() % 256;
49 //UINT8 byte = i*5 + 22;
50 //UINT8 byte = pn.GetNextByte(); // this should gives us a short register
51 int j = 0;
52 while (j < i)
53 {
54 if (byte == testBuffer[j])
55 {
56 // Non-unique, so try again
57 byte = rand() % 256;
58 j = 0;
59 }
60 else
61 {
62 j++;
63 }
64 }
65 testBuffer[i] = byte;
66 printf("%d, ", testBuffer[i]);
67 }
68 printf("\n");
69 ProtoLFSRX pnx;
70 pnx.ComputePolynomial((char*)testBuffer, 8*TEST_BYTES);
71 UINT32 xpoly = *pnx.GetPolynomial();
72 UINT32 xstate = *pnx.GetState();
73 printf("xpoly: 0x%08x bits:%u state:0x%08x\n", xpoly, pnx.GetNumBits(), xstate);
74 printf(" Matching seq: ");
75 for (int i = 0; i< TEST_BYTES; i++)
76 {
77 UINT8 byte = pnx.GetNextByte();
78 printf("%d, ", byte);
79 }
80 printf("\n");
81
82 // Directly set the polynomial and see what we get
83 UINT32 p = (UINT32)poly;
84 pnx.SetPolynomial(&p, 5);
85 printf(" Direct set seq: ");
86 for (int i = 0; i< TEST_BYTES; i++)
87 {
88 UINT8 byte = pnx.GetNextByte();
89 printf("%d, ", byte);
90 }
91 printf("\n");
92
93
94 //return 0;
95
96 // Here we just directly set the lfsrs and compare
97 pn.Reset();
98 pnx.Reset();
99
100 int k;
101 for (k = 0; k < 16; k++)
102 {
103 UINT32 state = pn.GetState();
104 xstate = *pnx.GetState();
105 printf("%d) bit:%d (state = 0x%08x) xbit:%d (xstate = 0x%08x) \n",
106 k, pn.GetNextBit(), state, pnx.GetNextBit(), xstate);
107 }
108 printf("\n");
109 k -= 2;
110 for (; k >= 0; k--)
111 {
112 UINT32 state = pn.GetState();
113 xstate = *pnx.GetState();
114 printf("%d) bit:%d (state = 0x%08x) xbit:%d (xstate = 0x%08x) \n",
115 k, pn.GetPrevBit(), state, pnx.GetPrevBit(), xstate);
116 }
117
118
119 //return 0;
120
121 // This validates Seek()
122 for (k =0 ; k < (int)pn.GetNumBytes(); k++)
123 printf("pn[%d] = 0x%02x\n", k, pn.GetNextByte());
124 pn.Seek(-3*8);
125 k -= 3;
126 printf("seek pn[%d] = = 0x%02x\n", k, pn.GetNextByte());
127 k += 2;
128 pn.Seek(1*8);
129 printf("seek pn[%d] = = 0x%02x\n", k, pn.GetNextByte());
130
131
132 //return 0;
133 // This validates get next/prev _byte_
134
135 // Build a sequence starting with state from first "numBits"
136 ProtoLFSR txReg(poly, 0x08);
137
138 //ProtoLFSRX txReg; // alternative ProtoLFSRX validation
139 //UINT32 init = 0x08;
140 //txReg.SetPolynomial(&p, 11, &init);
141
142 char syncBuf[32]; // overkill buffer size
143
144 printf("txReg[-1] = 0x%02x\n", txReg.GetPrevByte());
145
146 txReg.FillBuffer(syncBuf, 32);
147
148 ProtoLFSR rxReg(poly, 0x12);
149 //ProtoLFSRX rxReg; // alternative ProtoLFSRX validation
150 //init = 0x12;
151 //rxReg.SetPolynomial(&p, 11, &init);
152
153 int offset = 3;
154 printf("syncing to 0x%02x...\n", (UINT8)syncBuf[offset]);
155 if (!rxReg.Sync(syncBuf+offset, txReg.GetNumBytes() - offset))
156 printf("SYNC ERROR\n");
157
158
159 printf("synced state = 0x%08x\n", rxReg.GetState());
160 for (unsigned int i = 0; i < txReg.GetNumBytes(); i++)
161 printf("txReg[%d] = 0x%02x\n", i, (UINT8)syncBuf[i]);
162
163 for (int i = (int)txReg.GetNumBytes()-2; i >= -offset; i--)
164 printf("txReg[%d] = 0x%02x\n", i, txReg.GetPrevByte());
165
166 printf("\n");
167
168 printf("rxReg[-1] = 0x%02x\n", rxReg.GetPrevByte());
169 printf("rxReg[-2] = 0x%02x\n", rxReg.GetPrevByte());
170 rxReg.GetNextByte();
171 for (unsigned int i = 0; i < txReg.GetNumBytes(); i++)
172 printf("rxReg[%d] = 0x%02x\n", i, rxReg.GetNextByte());
173
174 //return 0;
175
176 // This validates the m-sequence auto-correlation properties
177 // Create reference buffer with all possible shifts of sequence
178 unsigned int numBits = ProtoLFSR::GetPolySize(poly);
179 const UINT32 LEN = 0x00000001 << numBits;
180 char** refSeq = new char*[LEN];
181 if (NULL == refSeq)
182 {
183 perror("lfsrExample: new refSeq error");
184 return -1;
185 }
186 memset(refSeq, 0, LEN*sizeof(char*));
187 for (unsigned int i = 0; i < LEN; i++)
188 {
189 if (NULL == (refSeq[i] = new char[LEN >> 3]))
190 {
191 perror("lfserExample: new refSeq[] error");
192 for (unsigned int j = 0; j < i; j++) delete[] refSeq[j];
193 delete[] refSeq;
194 return -1;
195 }
196 ProtoLFSR lfsr(poly);
197 lfsr.Seek(i);
198 lfsr.FillBuffer(refSeq[i], LEN >> 3);
199 }
200
201 unsigned int minMask = 0;
202 unsigned int mask = 0x0500;
203 unsigned int wtMin = 0xffffffff;
204
205 for (unsigned int i = 1; i < LEN - 1; i++)
206 {
207 unsigned int wt = 0;
208 for (unsigned int j = 0; j < LEN>>3; j++)
209 {
210 unsigned char delta = (unsigned char)(refSeq[0][j] ^ refSeq[i][j]);
211 wt += ProtoLFSR::GetWeight(delta);
212 }
213 printf("shift:%d weight:%u\n", i, wt);
214 if (wt < wtMin)
215 {
216 wtMin = wt;
217 minMask = mask;
218 }
219 }
220
221 printf("wtMin = %d (minMask = 0x%08x)\n", wtMin, minMask);
222
223 for (unsigned int i = 0; i < LEN; i++)
224 if (NULL != refSeq[i]) delete[] refSeq[i];
225 delete[] refSeq;
226
227 return 0;
228 } // end main()