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

Subversion Repositories xge_mac

[/] [xge_mac/] [trunk/] [tbench/] [systemc/] [sc_pkt_generator.cpp] - Blame information for rev 29

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 antanguay
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  File name "sc_pkt_generator.cpp"                            ////
4
////                                                              ////
5
////  This file is part of the "10GE MAC" project                 ////
6
////  http://www.opencores.org/cores/xge_mac/                     ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - A. Tanguay (antanguay@opencores.org)                  ////
10
////                                                              ////
11
//////////////////////////////////////////////////////////////////////
12
////                                                              ////
13
//// Copyright (C) 2008 AUTHORS. All rights reserved.             ////
14
////                                                              ////
15
//// This source file may be used and distributed without         ////
16
//// restriction provided that this copyright statement is not    ////
17
//// removed from the file and that any derivative work contains  ////
18
//// the original copyright notice and the associated disclaimer. ////
19
////                                                              ////
20
//// This source file is free software; you can redistribute it   ////
21
//// and/or modify it under the terms of the GNU Lesser General   ////
22
//// Public License as published by the Free Software Foundation; ////
23
//// either version 2.1 of the License, or (at your option) any   ////
24
//// later version.                                               ////
25
////                                                              ////
26
//// This source is distributed in the hope that it will be       ////
27
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
28
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
29
//// PURPOSE.  See the GNU Lesser General Public License for more ////
30
//// details.                                                     ////
31
////                                                              ////
32
//// You should have received a copy of the GNU Lesser General    ////
33
//// Public License along with this source; if not, download it   ////
34
//// from http://www.opencores.org/lgpl.shtml                     ////
35
////                                                              ////
36
//////////////////////////////////////////////////////////////////////
37
 
38
#include <stdio.h>
39
#include <iostream>
40
#include <sys/times.h>
41
#include <sys/stat.h>
42
 
43
#include "systemc.h"
44
 
45
#include "sc_pkt_generator.h"
46
 
47
 
48
void pkt_generator::init(void) {
49
    crc_interval = 0;
50
    fragment_interval = 0;
51 29 antanguay
    lenght_err_interval = 0;
52
    lenght_err_size = 10000;
53 2 antanguay
    coding_interval = 0;
54
    local_fault_interval = 0;
55
    remote_fault_interval = 0;
56
}
57
 
58
void pkt_generator::connect_fifo(sc_fifo<packet_t*> * fifo) {
59
    tx_fifo = fifo;
60
}
61
 
62
 
63
void pkt_generator::gen_packet() {
64
 
65
    int len = 0;
66
    int crc_int = 0;
67
    int fragment_int = 0;
68 29 antanguay
    int lenght_err_int = 0;
69 2 antanguay
    int coding_int = 0;
70
    int local_fault_int = 0;
71
    int remote_fault_int = 0;
72
    int fault_spacing = 120;
73
    int pause_int = 0;
74
    char running_cnt = 0;
75
 
76
    while (true) {
77 29 antanguay
 
78 2 antanguay
        wait(5, SC_NS);
79
 
80
        if (tx_bucket != 0 && tx_fifo->num_available() == 0) {
81
 
82
            //--
83
            // Check fifo
84
 
85
            if (tx_fifo == NULL || tx_fifo->num_free() == 0) {
86
                cout << "ERROR: FIFO not defined or full" << endl;
87
                sc_stop();
88
            }
89
 
90
            //---
91
            // Update constraints
92 29 antanguay
 
93 2 antanguay
            if (len < min_pkt_size) {
94
                len = min_pkt_size;
95
            }
96
 
97
            if (len > max_pkt_size) {
98
                len = min_pkt_size;
99
            }
100
 
101
            //--
102
            // Generate packet
103
 
104
            packet_t* pkt = new(packet_t);
105
 
106
            for (int i = 0; i < len+8; i++) {
107
                pkt->payload[i] = len+i;
108
            }
109
            pkt->payload[0] = running_cnt;
110
            running_cnt++;
111
 
112
            pkt->length = len;
113
 
114
            //---
115
            // Inject errors
116
 
117
            if (crc_interval != 0) {
118
                if (crc_int >= crc_interval) {
119
                    pkt->err_flags |= PKT_FLAG_ERR_CRC;
120
                    crc_int = 0;
121
                }
122
                else {
123
                    crc_int++;
124
                }
125
            }
126
            else {
127
                crc_int = 0;
128
            }
129
 
130
            if (fragment_interval != 0) {
131
                if (fragment_int >= fragment_interval) {
132
                    pkt->err_flags |= PKT_FLAG_ERR_FRG;
133
                    fragment_int = 0;
134
                }
135
                else {
136
                    fragment_int++;
137
                }
138
            }
139
            else {
140
                fragment_int = 0;
141
            }
142
 
143 29 antanguay
            if (lenght_err_interval != 0) {
144
                if (lenght_err_int >= lenght_err_interval) {
145
                    pkt->err_flags |= PKT_FLAG_ERR_LENGHT;
146
                    lenght_err_int = 0;
147
                    pkt->length = lenght_err_size;
148
                }
149
                else {
150
                    lenght_err_int++;
151
                }
152
            }
153
            else {
154
                lenght_err_int = 0;
155
            }
156
 
157 2 antanguay
            if (coding_interval != 0) {
158
                if (coding_int >= coding_interval) {
159
                    pkt->err_flags |= PKT_FLAG_ERR_CODING;
160
                    coding_int = 0;
161
                }
162
                else {
163
                    coding_int++;
164
                }
165
            }
166
            else {
167
                coding_int = 0;
168
            }
169
 
170
            //--
171
            // Inject local / remote faults
172
 
173
            if (local_fault_interval != 0) {
174
                if (local_fault_int >= local_fault_interval) {
175
 
176
                    pkt->err_flags |= PKT_FLAG_LOCAL_FAULT;
177
                    local_fault_int = 0;
178 29 antanguay
 
179 2 antanguay
                    fault_spacing++;
180
                    if (fault_spacing > (132 - 4)) {
181
                        fault_spacing = 120;
182
                    }
183
                    pkt->err_info = fault_spacing;
184
                }
185
                else {
186
                    local_fault_int++;
187
                }
188
            }
189
            else {
190
                local_fault_int = 0;
191
            }
192
 
193
            if (remote_fault_interval != 0) {
194
                if (remote_fault_int >= remote_fault_interval) {
195
 
196
                    pkt->err_flags |= PKT_FLAG_REMOTE_FAULT;
197
                    remote_fault_int = 0;
198 29 antanguay
 
199 2 antanguay
                    fault_spacing++;
200
                    if (fault_spacing > (132 - 4)) {
201
                        fault_spacing = 120;
202
                    }
203
                    pkt->err_info = fault_spacing;
204
                }
205
                else {
206
                    remote_fault_int++;
207
                }
208
            }
209
            else {
210
                remote_fault_int = 0;
211
            }
212
 
213
            //--
214
            // Inject PAUSE frames
215
 
216
            if (inject_pause_interval != 0) {
217
                if (pause_int >= inject_pause_interval) {
218
 
219
                    pkt->dest_addr = 0x0180c2;
220
                    pkt->dest_addr = (pkt->dest_addr << 24) | 0x000001;
221
                    pause_int = 0;
222 29 antanguay
 
223 2 antanguay
                }
224
                else {
225
                    pause_int++;
226
                }
227
            }
228
 
229
            //--
230
            // Send packet
231
 
232
            tx_fifo->write(pkt);
233
 
234
            tx_bucket--;
235
            len++;
236 29 antanguay
 
237 2 antanguay
        }
238
        else {
239
            wait(50, SC_NS);
240
        }
241
 
242
    }
243
}
244
 
245
void pkt_generator::set_tx_bucket(int cnt) {
246
    tx_bucket = cnt;
247
}
248
 
249
int pkt_generator::get_tx_bucket(void) {
250
    return tx_bucket;
251
}
252
 
253
void pkt_generator::set_pkt_size(int min, int max) {
254
 
255
    min_pkt_size = min;
256
    max_pkt_size = max;
257
 
258
}
259
 
260
void pkt_generator::set_crc_errors(int interval) {
261
    crc_interval = interval;
262
}
263
 
264
void pkt_generator::set_fragment_errors(int interval) {
265
    fragment_interval = interval;
266
}
267
 
268 29 antanguay
void pkt_generator::set_lenght_errors(int interval, int size) {
269
    lenght_err_interval = interval;
270
    lenght_err_size = size;
271
}
272
 
273 2 antanguay
void pkt_generator::set_coding_errors(int interval) {
274
    coding_interval = interval;
275
}
276
 
277
void pkt_generator::set_local_fault(int interval) {
278
    local_fault_interval = interval;
279
}
280
 
281
void pkt_generator::set_remote_fault(int interval) {
282
    remote_fault_interval = interval;
283
}
284
 
285
void pkt_generator::set_inject_pause(int interval) {
286
    inject_pause_interval = interval;
287
}

powered by: WebSVN 2.1.0

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