Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / model / pcie / pl / data_scrambler.h
CommitLineData
86530b38
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: data_scrambler.h
5* Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
6* 4150 Network Circle, Santa Clara, California 95054, U.S.A.
7*
8* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9*
10* This program is free software; you can redistribute it and/or modify
11* it under the terms of the GNU General Public License as published by
12* the Free Software Foundation; version 2 of the License.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License
20* along with this program; if not, write to the Free Software
21* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*
23* For the avoidance of doubt, and except that if any non-GPL license
24* choice is available it will apply instead, Sun elects to use only
25* the General Public License version 2 (GPLv2) at this time for any
26* software where a choice of GPL license versions is made
27* available with the language indicating that GPLv2 or any later version
28* may be used, or where a choice of which version of the GPL is applied is
29* otherwise unspecified.
30*
31* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
32* CA 95054 USA or visit www.sun.com if you need additional information or
33* have any questions.
34*
35*
36* ========== Copyright Header End ============================================
37*/
38// ========== Copyright Header Begin ==========================================
39//
40// OpenSPARC T2 Processor File: data_scrambler.h
41// Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
42// 4150 Network Circle, Santa Clara, California 95054, U.S.A.
43//
44// * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45//
46// This program is free software; you can redistribute it and/or modify
47// it under the terms of the GNU General Public License as published by
48// the Free Software Foundation; version 2 of the License.
49//
50// This program is distributed in the hope that it will be useful,
51// but WITHOUT ANY WARRANTY; without even the implied warranty of
52// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53// GNU General Public License for more details.
54//
55// You should have received a copy of the GNU General Public License
56// along with this program; if not, write to the Free Software
57// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
58//
59// For the avoidance of doubt, and except that if any non-GPL license
60// choice is available it will apply instead, Sun elects to use only
61// the General Public License version 2 (GPLv2) at this time for any
62// software where a choice of GPL license versions is made
63// available with the language indicating that GPLv2 or any later version
64// may be used, or where a choice of which version of the GPL is applied is
65// otherwise unspecified.
66//
67// Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
68// CA 95054 USA or visit www.sun.com if you need additional information or
69// have any questions.
70//
71// ========== Copyright Header End ============================================
72#ifndef INC_data_scrambler_h__
73#define INC_data_scrambler_h__
74
75#ifndef __EDG__
76
77#include "systemc.h"
78#define LOG_SERVICE
79#include "pcie_common/logger.hpp"
80#include "pl/encode_map.h"
81
82#define LINK_WIDTH 8
83
84USING_NAMESPACE(Logger)
85
86/// Static scrambler class
87/// As the name suggests, it scrambles an 8 bit input
88/// according to the PCIex standard LFSR
89
90class data_scrambler
91{
92 public :
93 bool init_done;
94 bool scramble_enable;
95 bool start_scramble_reg[LINK_WIDTH];
96
97 static data_scrambler *scramble()
98 {
99 if(scrambler == NULL)
100 scrambler = new data_scrambler();
101 return scrambler;
102 }
103
104 static data_scrambler *descramble()
105 {
106 if(descrambler == NULL)
107 descrambler = new data_scrambler();
108 return descrambler;
109 }
110
111 protected : data_scrambler()
112 {
113 int i;
114 for(i=0;i<LINK_WIDTH;i++)
115 {
116 lfsr[i] = 0xffff;
117 start_scramble_reg[i] = 0;
118 }
119 scramble_enable = 0;
120 init_done = 0;
121 }
122
123 public :
124 int data_scrambler::scramble_descramble_all(int inbyte,int is_scrambler,int is_control)
125 {
126 int i,outbyte;
127 for(i=0;i<LINK_WIDTH;i++)
128 outbyte = scramble_descramble(inbyte,is_scrambler,is_control,i);
129
130 return outbyte;
131
132 }
133
134 public :
135 int data_scrambler::scramble_descramble(int inbyte,int is_scrambler,int is_control,int lane_num)
136 {
137 static int scrambit[16][LINK_WIDTH];
138 static int bit[16][LINK_WIDTH];
139 static int bit_out[16][LINK_WIDTH];
140 int i, outbyte;
141 if(inbyte == COM && (is_control == 1))
142 {
143 lfsr[lane_num] = 0xffff;
144 return(COM);
145 }
146
147 if(inbyte == SKP && (is_scrambler == 0) && (is_control == 1))
148 {
149 return(SKP);
150 }
151 for(i=0 ; i< 16; i++)
152 {
153 bit[i][lane_num] = (lfsr[lane_num] >> i) &1;
154 }
155
156 for(i=0 ; i<8 ; i++)
157 {
158 scrambit[i][lane_num] = (inbyte >> i) &1;
159 }
160
161 if(start_scramble_reg[lane_num])
162 {
163 scrambit[0][lane_num] ^= bit[15][lane_num];
164 scrambit[1][lane_num] ^= bit[14][lane_num];
165 scrambit[2][lane_num] ^= bit[13][lane_num];
166 scrambit[3][lane_num] ^= bit[12][lane_num];
167 scrambit[4][lane_num] ^= bit[11][lane_num];
168 scrambit[5][lane_num] ^= bit[10][lane_num];
169 scrambit[6][lane_num] ^= bit[9][lane_num];
170 scrambit[7][lane_num] ^= bit[8][lane_num];
171 }
172
173 // Now advance the LFSR 8 serial clocks
174 bit_out[ 0][lane_num] = bit[ 8][lane_num];
175 bit_out[ 1][lane_num] = bit[ 9][lane_num];
176 bit_out[ 2][lane_num] = bit[10][lane_num];
177 bit_out[ 3][lane_num] = bit[11][lane_num] ^ bit[ 8][lane_num];
178 bit_out[ 4][lane_num] = bit[12][lane_num] ^ bit[ 9][lane_num] ^ bit[ 8][lane_num];
179 bit_out[ 5][lane_num] = bit[13][lane_num] ^ bit[10][lane_num] ^ bit[ 9][lane_num] ^ bit[ 8][lane_num];
180 bit_out[ 6][lane_num] = bit[14][lane_num] ^ bit[11][lane_num] ^ bit[10][lane_num] ^ bit[ 9][lane_num];
181 bit_out[ 7][lane_num] = bit[15][lane_num] ^ bit[12][lane_num] ^ bit[11][lane_num] ^ bit[10][lane_num];
182 bit_out[ 8][lane_num] = bit[ 0][lane_num] ^ bit[13][lane_num] ^ bit[12][lane_num] ^ bit[11][lane_num];
183 bit_out[ 9][lane_num] = bit[ 1][lane_num] ^ bit[14][lane_num] ^ bit[13][lane_num] ^ bit[12][lane_num];
184 bit_out[10][lane_num] = bit[ 2][lane_num] ^ bit[15][lane_num] ^ bit[14][lane_num] ^ bit[13][lane_num];
185 bit_out[11][lane_num] = bit[ 3][lane_num] ^ bit[15][lane_num] ^ bit[14][lane_num];
186 bit_out[12][lane_num] = bit[ 4][lane_num] ^ bit[15][lane_num];
187 bit_out[13][lane_num] = bit[ 5][lane_num];
188 bit_out[14][lane_num] = bit[ 6][lane_num];
189 bit_out[15][lane_num] = bit[ 7][lane_num];
190
191 lfsr[lane_num] = 0;
192 for (i=0; i <16; i++) // convert the LFSR back to an integer
193 lfsr[lane_num] += (bit_out[i][lane_num] << i);
194 outbyte = 0;
195 for (i= 0; i<8; i++) // convert data back to an integer
196 outbyte += (scrambit[i][lane_num] << i);
197
198 wait(SC_ZERO_TIME);
199
200 return outbyte;
201 }
202
203 void reset()
204 {
205 int i;
206 for(i=0;i<LINK_WIDTH;i++)
207 {
208 lfsr[i] = 0xffff;
209 start_scramble_reg[i] = 0;
210 }
211 scramble_enable = 0;
212 }
213
214 unsigned short get_lfsr(int lane_num)
215 {
216 return lfsr[lane_num];
217 }
218
219 void set_lfsr(unsigned short val,int lane_num)
220 {
221 lfsr[lane_num] = val;
222 }
223
224 private :
225 static data_scrambler *scrambler;
226 static data_scrambler *descrambler;
227 unsigned short lfsr[LINK_WIDTH];
228};
229
230#endif // __EDG__
231
232#endif //INC_data_scrambler_h__