-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbench.c
432 lines (370 loc) · 10.7 KB
/
testbench.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
//
// AHIR release utilities
//
#include <pthreadUtils.h>
#include <Pipes.h>
#include <pipeHandler.h>
// These will wait.
#ifndef AA2C
#include "vhdlCStubs.h"
#else
#include "aa_c_model.h"
#endif
// includes the header.
#define PACKET_LENGTH_IN_WORDS 64
typedef struct _TbConfig {
// if 1, input port 1 will be fed by data
// else input port 1 will be unused.
int input_port_1_active;
// if random dest is set, then
// input port 1 can send data to either
// output port 1 or output port 2.
int input_port_1_random_dest_flag;
// if random_dest_flag is 0, then
// input port 1 writes only to
// this destination port (provided
// it is either 1 or 2).
int input_port_1_destination_port;
int input_port_2_active;
// see comments above.
int input_port_2_random_dest_flag;
int input_port_2_destination_port;
////////////////////////////////////////
// inout port 3
int input_port_3_active;
int input_port_3_random_dest_flag;
int input_port_3_destination_port;
////////////////////////////////////////
// inout port 4
int input_port_4_active;
int input_port_4_random_dest_flag;
int input_port_4_destination_port;
} TbConfig;
TbConfig tb_config;
int __err_flag__ = 0;
void input_port_core(int port_id)
{
uint32_t send_buffer[PACKET_LENGTH_IN_WORDS];
int i;
for(i = 0; i < PACKET_LENGTH_IN_WORDS; i++)
{
send_buffer[i] = i;
}
// Sequence Id based on the port they come from..
uint8_t seq_id = 0;
if(port_id ==1 )
{
seq_id = 1;
}
else if(port_id == 2)
{
seq_id = 2;
}
else if (port_id == 3)
{
seq_id = 3;
}
else
{
seq_id = 4;
}
while(1)
{
int dest_port = -1;
if(port_id == 1 )
{
// printf("dest_port 1\n");
dest_port =
(tb_config.input_port_1_random_dest_flag ? ((rand() & 0x3)+1) :
tb_config.input_port_1_destination_port);
}
else if(port_id == 2 )
{
// printf("dest_port 2 \n");
dest_port =
(tb_config.input_port_2_random_dest_flag ? ((rand() & 0x3)+1) :
tb_config.input_port_2_destination_port);
}
else if (port_id == 3 )
{
// printf("dest_port 3 \n");
dest_port =
(tb_config.input_port_3_random_dest_flag ? ((rand() & 0x3)+1) :
tb_config.input_port_3_destination_port);
}
else //if (port_id == 4)
{
// printf("dest_port 4 \n");
dest_port =
(tb_config.input_port_4_random_dest_flag ? ((rand() & 0x3)+1) :
tb_config.input_port_4_destination_port);
}
if((dest_port == 1) || (dest_port == 2) || (dest_port == 3) || (dest_port == 4))
{
send_buffer[0] = (dest_port << 24) | (64 << 8) | seq_id;
if(port_id == 1)
{
// printf("Sending from 1\n");
write_uint32_n ("in_data_1", send_buffer, PACKET_LENGTH_IN_WORDS);
}
else if (port_id == 2)
{
// printf("Sending from 2\n");
write_uint32_n ("in_data_2", send_buffer, PACKET_LENGTH_IN_WORDS);
}
else if (port_id == 3)
{
// printf("Sending from 3\n");
write_uint32_n ("in_data_3", send_buffer, PACKET_LENGTH_IN_WORDS);
}
else
{
// printf("Sending from 4\n");
write_uint32_n ("in_data_4", send_buffer, PACKET_LENGTH_IN_WORDS);
}
// increment by 4
}
}
}
void input_port_1_sender ()
{
input_port_core(1);
}
DEFINE_THREAD(input_port_1_sender);
void input_port_2_sender ()
{
input_port_core(2);
}
DEFINE_THREAD(input_port_2_sender);
void input_port_3_sender ()
{
input_port_core(3);
}
DEFINE_THREAD(input_port_3_sender);
void input_port_4_sender ()
{
input_port_core(4);
}
DEFINE_THREAD(input_port_4_sender);
void output_port_core(int port_id)
{
int PCOUNT = 0;
int err = 0;
while(1)
{
uint32_t packet[PACKET_LENGTH_IN_WORDS];
if(port_id == 1)
{
read_uint32_n ("out_data_1", packet, PACKET_LENGTH_IN_WORDS);
// printf("data read from port 1 \n");
}
else if(port_id == 2)
{
read_uint32_n ("out_data_2", packet, PACKET_LENGTH_IN_WORDS);
// printf("data read from port 2 \n");
}
else if(port_id == 3)
{
read_uint32_n ("out_data_3", packet, PACKET_LENGTH_IN_WORDS);
// printf("data read from port 3 \n");
}
else
{
read_uint32_n ("out_data_4", packet, PACKET_LENGTH_IN_WORDS);
// printf("data read from port 4 \n");
}
//////////////////////////////// Worked till here...................
PCOUNT++;
int dest = (packet[0] >> 24);
///////////////////////////////////////////////////////Editted below....................
int input_port ;
uint8_t seq_id = packet[0] & 255;
// printf("seq_id = %d \n", seq_id);
if(seq_id==1)
{
input_port = 1;
}
else if (seq_id==1)
{
input_port = 2;
}
else if(seq_id==3)
{
input_port = 3;
}
else if(seq_id==4)
{
input_port = 4;
}
else
input_port=2;
///////////////////////////////////////////////Till here.....................
// int input_port = ((packet[0] & 0x7)); ///////////////// This part is to understand...........
////////////////////////////////////////////////////////////////////////////////////////
//
// check the destination?
//
if(dest != port_id)
{
fprintf(stderr,"Error: at port %d, packet number %d from input port %d,"
" destination mismatch!\n", port_id, PCOUNT, input_port);
err = 1;
}
else
{
fprintf(stderr,"\nRx[%d] at output port %d from input port %d\n",
PCOUNT, port_id, input_port);
}
// check integrity of the packet.
int I ;
for(I=1; I < PACKET_LENGTH_IN_WORDS; I++)
{
if (packet[I] != I)
{
fprintf(stderr,"\nError: packet[%d]=%d, expected %d.\n",
I, packet[I], I);
int q ;
for(q=0; q < PACKET_LENGTH_IN_WORDS; q++)
{
printf("%d & %d & %d & %d & %d\n", q,packet[q],input_port,dest,port_id);
}
err = 1;
break;
}
//else if(packet[I])
}
if(err)
{
__err_flag__ = 1;
break;
}
}
}
void output_port_1_receiver ()
{
output_port_core(1);
}
DEFINE_THREAD(output_port_1_receiver);
void output_port_2_receiver ()
{
output_port_core(2);
}
DEFINE_THREAD(output_port_2_receiver);
void output_port_3_receiver ()
{
output_port_core(3);
}
DEFINE_THREAD(output_port_3_receiver);
void output_port_4_receiver ()
{
output_port_core(4);
}
DEFINE_THREAD(output_port_4_receiver);
int main(int argc, char* argv[])
{
if(argc < 3)
{
/////////////// Asking for which type of packet transfer
fprintf(stderr,"Usage: %s [trace-file] [test_type] \n trace-file=null for no trace, stdout for stdout\n" "test_type = 1to1/1to2/1to3/1to4/1toall/2to1/2to2/2to3//2to4/2toall/3to1/3to2/3to3/3to4/3toall/4to1/4to2/4to3/4to4/4toall/alltoall\n",
argv[0]);
return(1);
}
FILE* fp = NULL;
if(strcmp(argv[1],"stdout") == 0)
{
fp = stdout;
}
else if(strcmp(argv[1], "null") != 0)
{
fp = fopen(argv[1],"w");
if(fp == NULL)
{
fprintf(stderr,"Error: could not open trace file %s\n", argv[1]);
return(1);
}
}
int __1to1 = (strcmp(argv[2],"1to1") == 0);
int __1to2 = (strcmp(argv[2],"1to2") == 0);
int __1to3 = (strcmp(argv[2],"1to3") == 0);
int __1to4 = (strcmp(argv[2],"1to4") == 0);
int __1toall = (strcmp(argv[2],"1toall") == 0);
int __2to1 = (strcmp(argv[2],"2to1") == 0);
int __2to2 = (strcmp(argv[2],"2to2") == 0);
int __2to3 = (strcmp(argv[2],"2to3") == 0);
int __2to4 = (strcmp(argv[2],"2to4") == 0);
int __2toall = (strcmp(argv[2],"2toall") == 0);
int __3to1 = (strcmp(argv[2],"3to1") == 0);
int __3to2 = (strcmp(argv[2],"3to2") == 0);
int __3to3 = (strcmp(argv[2],"3to3") == 0);
int __3to4 = (strcmp(argv[2],"3to4") == 0);
int __3toall = (strcmp(argv[2],"3toall") == 0);
int __4to1 = (strcmp(argv[2],"4to1") == 0);
int __4to2 = (strcmp(argv[2],"4to2") == 0);
int __4to3 = (strcmp(argv[2],"4to3") == 0);
int __4to4 = (strcmp(argv[2],"4to4") == 0);
int __4toall = (strcmp(argv[2],"4toall") == 0);
int __alltoall = (strcmp(argv[2],"alltoall") == 0);
#ifdef AA2C
init_pipe_handler();
start_daemons (fp,0);
#endif
// test configuration setup.
// both input ports active, send
// randomly to output ports.
tb_config.input_port_1_active = (__1to1 || __1to2 || __1to3 || __1to4 || __1toall || __alltoall);
tb_config.input_port_1_random_dest_flag = (__1toall || __alltoall);
/////////////////////////////////////////////////Have to work on destination port here below.............
tb_config.input_port_1_destination_port = (__1to1 ? 1 : (__1to2 ? 2 : (__1to3 ? 3 : (__1to4 ? 4 : -1))));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
tb_config.input_port_2_active = (__2to1 || __2to2 ||__2to3 ||__2to4|| __2toall || __alltoall);
tb_config.input_port_2_random_dest_flag = (__2toall || __alltoall);
/////////////////////////////////////////////////Have to work on destination port here below.............
tb_config.input_port_2_destination_port = (__2to1 ? 1 : (__2to2 ? 2 : (__2to3 ? 3 : (__2to4 ? 4 : -1))));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
tb_config.input_port_3_active = (__3to1 || __3to2 ||__3to3 ||__3to4|| __3toall || __alltoall);
tb_config.input_port_3_random_dest_flag = (__3toall || __alltoall);
/////////////////////////////////////////////////Have to work on destination port here below.............
tb_config.input_port_3_destination_port = (__3to1 ? 1 : (__3to2 ? 2 : (__3to3 ? 3 : (__3to4 ? 4 : -1))));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
tb_config.input_port_4_active = (__4to1 || __4to2 ||__4to3 ||__4to4|| __4toall || __alltoall);
tb_config.input_port_4_random_dest_flag = (__4toall || __alltoall);
/////////////////////////////////////////////////Have to work on destination port here below.............
tb_config.input_port_4_destination_port = (__4to1 ? 1 : (__4to2 ? 2 : (__4to3 ? 3 : (__4to4 ? 4 : -1))));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// start the receivers
//
PTHREAD_DECL(output_port_1_receiver);
PTHREAD_CREATE(output_port_1_receiver);
PTHREAD_DECL(output_port_2_receiver);
PTHREAD_CREATE(output_port_2_receiver);
PTHREAD_DECL(output_port_3_receiver);
PTHREAD_CREATE(output_port_3_receiver);
PTHREAD_DECL(output_port_4_receiver);
PTHREAD_CREATE(output_port_4_receiver);
// start the senders.
PTHREAD_DECL(input_port_1_sender);
PTHREAD_CREATE(input_port_1_sender);
PTHREAD_DECL(input_port_2_sender);
PTHREAD_CREATE(input_port_2_sender);
PTHREAD_DECL(input_port_3_sender);
PTHREAD_CREATE(input_port_3_sender);
PTHREAD_DECL(input_port_4_sender);
PTHREAD_CREATE(input_port_4_sender);
// wait on the two output threads
PTHREAD_JOIN(output_port_1_receiver);
PTHREAD_JOIN(output_port_2_receiver);
PTHREAD_JOIN(output_port_3_receiver);
PTHREAD_JOIN(output_port_4_receiver);
if(__err_flag__)
{
fprintf(stderr,"\nFAILURE.. there were errors\n");
}
return(0);
}