summaryrefslogtreecommitdiff
path: root/src/systemc/tests/systemc/examples/trie/trie.cpp
blob: 7714ce657968c917396199910c9698ae30adf9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include "systemc.h"

SC_MODULE(lc)
{
	sc_out<bool> DIN_rdy;
	sc_in<bool>  DIN_vld;
#define LOAD_ADDR   0
#define LOAD_DATA   1
#define LOOKUP 2 // really op(1) == 1 and op(0) - don't care
	sc_in<sc_uint<2> >  DIN_op;
	sc_in<sc_uint<32> > DIN_data_in;
	sc_in<bool>  DOUT_rdy;
	sc_out<bool> DOUT_vld;
	sc_out<sc_uint<32> > DOUT_hop;
	sc_in<bool> RSTN;
	sc_in_clk CLK;

	void thread();
	sc_uint<20> extract(sc_uint<32>, sc_uint<7>, sc_uint<5>);
	sc_uint<10> lookup(sc_uint<32>);

	sc_uint<32> M[1024];
	sc_uint<32>  addr;	// latched addr for loading memory

	SC_CTOR(lc){
		SC_CTHREAD(thread, CLK.pos());
		reset_signal_is(RSTN,false);
	}
};

//
// this implements the level-compressed trie
// ip routing algorithm that is proposed in:
//
// "Fast Address Lookup for Internet Routers"
// Stefan Nilsson and Gunnar Karlsson
// Proc IFIP 4th International Conference on BroadBand Communication
// pp. 11-22, 1998
//
// 
void
lc::thread()
{
	if( !RSTN){
		DIN_rdy = 1;
		DOUT_vld = 0;
		wait(1);
	}

	for(;;){
		bool tmp_vld;
		bool tmp_rdy;
		sc_uint<2> op;
		sc_uint<32> data_in;
		sc_uint<32> hop;

                {
                do {
                	tmp_vld = DIN_vld.read();
                        op = DIN_op.read();
                        data_in = DIN_data_in.read();
                        wait(1);
                } while( !tmp_vld);
                }

		if( op == 0){
			// latch address
			addr = data_in;
			wait(1);
		} else if(op == 1){
			// load memory
			M[addr] = data_in;
			wait(1);
		} else if( op[1] == 1){
			// do the ip lookup to next hop
			hop = lookup(data_in);
			{
				DIN_rdy = 0;
				do {
					tmp_rdy = DOUT_rdy.read();
					wait(1);
				} while( !tmp_rdy);
				DOUT_vld = 1;
				DOUT_hop.write(hop);
				DIN_rdy = 1;
				wait(1);
				DOUT_vld = 0;
			}
		}
	}

}

sc_uint<20>
lc::extract(sc_uint<32> x, sc_uint<7> p, sc_uint<5> w)
{
	return( (x>>(p-w+1)) & ~(~0<<w) );
}

#define BRANCH(T)	T.range(31,27)
#define SKIP(T)		T.range(26,20)
#define ADDR(T)		T.range(19,0)
#define LEN(T)		T.range(31,25)
#define NEXT_HOP(T)	T.range(24,15)
#define NEXT_PREFIX(T)	T.range(14,0)

sc_uint<10>
lc::lookup(sc_uint<32> ip)
{
	sc_uint<7> pos;
	sc_uint<5> branch;
	sc_uint<20> addr;
	sc_uint<32> t;
	sc_uint<32> ip_prefix;
	sc_uint<10> next_hop;
	sc_uint<32> mask;

	t = M[0];
	pos = SKIP(t);
	branch = BRANCH(t);
	addr = ADDR(t);
	while(branch != 0){
		addr = addr + extract(ip, pos, branch);
		t = M[addr];
		pos = pos -  (branch + SKIP(t));
		branch = BRANCH(t);
		addr = ADDR(t);
	}


	next_hop = 0;
	for(;;) {
		addr <<= 1;

		ip_prefix = M[addr];
		t = M[addr|1];
		mask = ~0 << (32-LEN(t));
		if( (ip_prefix&mask) == (ip&mask)){
			next_hop = NEXT_HOP(t);
			break;
		} 
		addr = NEXT_PREFIX(t);
		if( addr == 0)
			break;
	}

	return(next_hop);
}


/*
 * hop	prefix(bits 31 .. 0)
 *  1   0000*
 *  2   0001*
 *  3   00101*
 *  4   010*
 *  5   0110*
 *  6   0111*
 *  7   100*
 *  8   101000*
 *  9   101001*
 * 10   10101*
 * 11   10110*
 * 12   10111* 
 * 13   110*
 * 14   11101000*
 * 15   11101001*
 *
 * "not in table" produces a hop of 0
 */

#define TRIE(LN, SK, AD) \
  ((((LN)&0x1f)<<27) | (((SK)&0x7f)<<20) | ((AD)&0xfffff))
#define E(LN, NHP, NPX) \
  ((((LN)&0x7f)<<25) | (((NHP)&0x3ff)<<15) | ((((NPX))&0x7fff)))


#define M_SIZE 52
sc_uint<32> M[M_SIZE] = {
/* TRIE */
/* 00 */ TRIE(3, 31, 1),
/* 01 */ TRIE(1, 0, 9),
/* 02 */ TRIE(0, 2, 2+11),
/* 03 */ TRIE(0, 0, 3+11),
/* 04 */ TRIE(1, 0, 11),
/* 05 */ TRIE(0, 0, 6+11),
/* 06 */ TRIE(2, 0, 13),
/* 07 */ TRIE(0, 0, 12+11), 
/* 08 */ TRIE(1, 4, 17), 
/* 09 */ TRIE(0, 0, 0+11),
/* 10 */ TRIE(0, 0, 1+11), 
/* 11 */ TRIE(0, 0, 4+11),
/* 12 */ TRIE(0, 0, 5+11),
/* 13 */ TRIE(1, 0, 19), 
/* 14 */ TRIE(0, 0, 9+11),
/* 15 */ TRIE(0, 0, 10+11),
/* 16 */ TRIE(0, 0, 11+11),
/* 17 */ TRIE(0, 0, 13+11),
/* 18 */ TRIE(0, 0, 14+11),
/* 19 */ TRIE(0, 0, 7+11),
/* 20 */ TRIE(0, 0, 8+11),
/* 21 */ 0, 			/* pad */
/* BASE + PREFIX */
/* 22 */ 0x00000000, E(4, 1, 0),
/* 24 */ 0x10000000, E(4, 2, 0),
/* 26 */ 0x28000000, E(5, 3, 0),
/* 28 */ 0x40000000, E(3, 4, 0),
/* 31 */ 0x60000000, E(4, 5, 0),
/* 32 */ 0x70000000, E(4, 6, 0),
/* 34 */ 0x80000000, E(3, 7, 0),
/* 36 */ 0xa0000000, E(6, 8, 0),
/* 38 */ 0xa4000000, E(6, 9, 0),
/* 40 */ 0xa8000000, E(5, 10, 0),
/* 42 */ 0xb0000000, E(5, 11, 0),
/* 44 */ 0xb8000000, E(5, 12, 0),
/* 46 */ 0xc0000000, E(3, 13, 0),
/* 48 */ 0xe8000000, E(8, 14, 0),
/* 50 */ 0xe9000000, E(8, 15, 0)
};


struct stimuli {
	unsigned int ip;
	int hop;
} S[] = {
	{ 0xf0000000, 0 },
	{ 0x10000000, 2 }, 
	{ 0x7c000000, 6 }, 
	{ 0x7c001000, 6 }, 
	{ 0x7c000070, 6 }, 
};

int
sc_main(int argc, char *argv[])
{	
	sc_clock clk;
	sc_signal<bool> reset;
 	sc_signal<bool> in_rdy;
        sc_signal<bool> in_vld;
        sc_signal<bool> out_vld;
        sc_signal<bool> out_rdy;
        sc_signal<sc_uint<2> > op;
        sc_signal<sc_uint<32> > data;
        sc_signal<sc_uint<32> > hop;
	int i;
	int m_addr;
	lc *lcp;

	lcp = new lc("lc0");
	(*lcp)(in_rdy, in_vld,
	      op, data,
	      out_rdy, out_vld,
	      hop,
	      reset, clk);

	reset = 0;
	sc_start(2, SC_NS);
	reset = 1;
	sc_start(2, SC_NS);
	out_rdy = 1;

	/*
	 * download the RAM containing
	 * the routing data
	 */
	for(i = 0, m_addr = 0; i < M_SIZE; i++, m_addr++){
		while(!in_rdy) sc_start(1, SC_NS);
		in_vld = 1;
		op.write(LOAD_ADDR);
		data.write(m_addr);
		do { sc_start(1, SC_NS); in_vld = 0; } while(!in_rdy);
		sc_start(1, SC_NS);
		in_vld = 1;
		op.write(LOAD_DATA);
		data.write(M[m_addr]);
		do { sc_start(1, SC_NS); in_vld = 0; } while(!in_rdy);
		sc_start(1, SC_NS);
	}

	/*
	 * apply some ip's and see what
 	 * comes back as next-hops
	 */
	for(i = 0; i < sizeof S/sizeof(struct stimuli); i++){
		while(!in_rdy) sc_start(1, SC_NS);
		in_vld = 1;
		op.write(LOOKUP);
		data.write(S[i].ip);
		do { sc_start(1, SC_NS); in_vld = 0; } while(!out_vld);
		unsigned int h = hop.read();
		if( h != S[i].hop){
			cout << S[i].ip << " should be hop " << S[i].hop
                             << " got hop " << h << endl;
		}
	}
	cout << "program complete" << endl;
        return 0;

}