source: trunk/modules/vci_synthetic_initator/caba/sources/src/vci_synthetic_initiator.cpp @ 145

Last change on this file since 145 was 145, checked in by choichil, 13 years ago

Adding function to get Latencies and a period for broadcast calculated with ramdomize

File size: 13.6 KB
Line 
1
2/* -*- c++ -*-
3 * File         : vci_synthetic_initiator.cpp
4 * Date         : 23/12/2010
5 * Copyright    : UPMC / LIP6
6 * Authors      : Christophe Choichillon
7 * Version      : 2.1
8 *
9 * SOCLIB_LGPL_HEADER_BEGIN
10 *
11 * This file is part of SoCLib, GNU LGPLv2.1.
12 *
13 * SoCLib is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published
15 * by the Free Software Foundation; version 2.1 of the License.
16 *
17 * SoCLib is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with SoCLib; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 *
27 * SOCLIB_LGPL_HEADER_END
28 *
29 * Maintainers: christophe.choichillon@lip6.fr
30 */
31
32#include "../include/vci_synthetic_initiator.h"
33#include <iostream>
34
35
36//#define DETERMINISTIC
37#define RANDOM_PERIOD
38
39namespace soclib { namespace caba {
40
41
42#define tmpl(x) template<typename vci_param> x VciSyntheticInitiator<vci_param>
43
44  //using soclib::common::uint32_log2; 
45 
46  ////////////////////////////////
47  //    Constructor
48  ////////////////////////////////
49
50  tmpl(/**/)::VciSyntheticInitiator( 
51      sc_module_name name,
52      const soclib::common::MappingTable &mt,
53      const soclib::common::IntTab       &vci_index,
54      const uint32_t length,    // Packet length (flit numbers)
55      const uint32_t rho,       // Offered load * 1000
56      const uint32_t depth,     // Fifo depth
57      const uint32_t xmesh,     
58      const uint32_t ymesh,
59      const uint32_t bc_period, // Broadcast period, if no broadcast => 0
60      const uint32_t xmin, 
61      const uint32_t xmax,
62      const uint32_t ymin,
63      const uint32_t ymax
64      )
65
66    : soclib::caba::BaseModule(name),
67
68    p_clk("clk"),
69    p_resetn("resetn"),
70    p_vci("vci_ini"),
71    //  FIFOs
72    m_srcid( mt.indexForId(vci_index) ),
73    m_length(length),
74    m_rho(rho),
75    m_depth(depth),
76    m_xmesh(xmesh),
77    m_ymesh(ymesh),
78    m_bc_period(bc_period),
79    m_xmin(xmin),
80    m_xmax(xmax),
81    m_ymin(ymin),
82    m_ymax(ymax),
83    r_date_fifo("r_date_fifo", m_depth),
84    r_bc_fifo("r_bc_fifo", m_depth),
85    r_cmd_fsm("r_cmd_fsm"),
86    r_cmd_address("r_cmd_address"),             
87    r_cmd_trdid("r_cmd_trdid"), 
88    r_cmd_count("r_cmd_count"),         
89    r_cmd_seed("r_cmd_seed"),   
90    r_bc_nrsp("r_bc_nrsp"),     
91    r_cpt_cycles("r_cpt_cycles"),               
92    r_cpt_period("r_cpt_period"),               
93    r_nb_single("r_nb_single"), 
94    r_latency_single("r_latency_single"),       
95    r_nb_bc("r_nb_bc"), 
96    r_latency_bc("r_latency_bc")               
97{
98
99      r_pending_fsm = new sc_signal<bool>[m_tab_size];
100      r_pending_date = new sc_signal<uint64_t>[m_tab_size];
101
102      SC_METHOD(transition);
103      dont_initialize();
104      sensitive << p_clk.pos();
105
106      SC_METHOD(genMoore);
107      dont_initialize();
108      sensitive << p_clk.neg();
109
110    } // end constructor
111
112
113  /////////////////////////////////
114  tmpl(/**/)::~VciSyntheticInitiator()
115    /////////////////////////////////
116  {
117        delete r_pending_fsm;
118        delete r_pending_date;
119  }
120
121  ///////////////////////////////////
122  tmpl(uint32_t)::destAdress()
123  ///////////////////////////////////
124  {
125    return (uint32_t) (rand() % (m_xmesh * m_ymesh)) ;
126  }
127
128
129  ///////////////////////////////////
130  tmpl(uint32_t)::destAdress(uint32_t *rand_seed)
131  ///////////////////////////////////
132  {
133    return (uint32_t) (rand_r(rand_seed) % (m_xmesh * m_ymesh)) ;
134  }
135
136  //////////////////////////////////
137  tmpl(double)::getLatencySingle()
138  //////////////////////////////////
139  {
140        if (m_rho) 
141          return (double)(r_latency_single.read())/(double)(r_nb_single.read());
142        else
143          return 0;
144
145  }
146
147  //////////////////////////////////
148  tmpl(double)::getLatencyBC()
149  //////////////////////////////////
150  {
151        if(m_bc_period)
152          return ((double)r_latency_bc.read()/(double)r_nb_bc.read());
153        else
154          return 0;
155
156  }
157
158  //////////////////////////////////
159  tmpl(void)::print_trace()
160  //////////////////////////////////
161  {
162        const char* state_cmd_str[] = { "IDLE",
163                                        "SINGLE_SEND",
164                                        "BC_SEND"};
165
166        const char* state_bc_rsp_str[] = {"IDLE",
167                                          "WAIT_RSP"};
168
169        std::cout << "Vci_Synthetic_Initiator " << name()
170                  << " : " << std::dec << r_cpt_cycles.read() << " cycles " 
171                  << " : state_cmd_fsm = " << state_cmd_str[r_cmd_fsm] 
172                  << " : state_rsp_fsm = " << state_bc_rsp_str[r_pending_fsm[0].read()] 
173                  << " Adresse to send : " << std::hex << r_cmd_address.read()
174                  << " Number of broadcast to receive : " << std::dec << r_bc_nrsp.read() 
175                  << " Number of packets sent : " << std::dec << r_nb_single.read() << " " << r_cmd_trdid.read() 
176                  << " Cycles to the next BC : " << r_cpt_period.read() << std::endl;
177        for(int i = 0; i < (1<<vci_param::T) ; i++){
178          std::cout << "ID : " << i << " " << (uint64_t)(r_pending_date[i].read()) << std::endl;
179        }
180  }
181
182  //////////////////////////////////
183  tmpl(void)::printStats()
184  //////////////////////////////////
185  {
186        std::cout << name() << " : "<< std::dec << r_cpt_cycles.read() << " cycles, " << r_nb_single.read() << " packets sent" << std::endl;
187        if (m_rho) 
188        {
189          std::cout << "Average latency : " << (double)(r_latency_single.read())/(double)(r_nb_single.read()) << std::endl;
190        }
191        if(m_bc_period)
192        {
193          std::cout << "Number of broadcast sent and received : " << r_nb_bc.read() << std::endl;
194          std::cout << "Average latency : " << ((double)r_latency_bc.read()/(double)r_nb_bc.read()) << std::endl;
195        }
196  }
197
198  //////////////////////////////////
199  tmpl(void)::transition()
200  //////////////////////////////////
201  {
202    //  RESET         
203    if ( ! p_resetn.read() ) 
204    {
205      // Initializing seed for random numbers generation
206
207#ifndef DETERMINISTIC
208      srand(time(NULL));
209#endif
210
211      // Initializing FSMs
212      r_cmd_fsm = VCI_IDLE;
213      for(size_t i=0 ; i<m_tab_size ; i++) r_pending_fsm[i] = false;
214
215      // Initializing FIFOs
216      r_date_fifo.init();
217      r_bc_fifo.init();
218
219      // Initializing the instrumentation registers
220      r_latency_single          = 0;
221      r_nb_single               = 0;
222      r_latency_bc              = 0;
223      r_nb_bc                   = 0;
224      r_cpt_cycles              = 0;
225      r_cpt_period              = 0;
226     
227      r_cmd_seed                = (uint32_t)m_srcid;
228
229      return;
230    }
231
232    bool    fifo_put = false;
233    bool    fifo_get = false;
234    bool    fifo_bc  = false;
235
236
237#ifdef DETERMINISTIC
238    uint32_t m_local_seed ;
239#endif
240
241    //////////////////
242    // VCI CMD FSM
243    //////////////////
244    switch ( r_cmd_fsm.read() ) {
245      case VCI_IDLE:
246        {
247          if (r_date_fifo.rok())
248          {
249            if ( r_bc_fifo.read() == true )     // its a broadcast request
250            {
251              if ( r_pending_fsm[0].read() == false )   // no current broadcast
252              {
253                r_cmd_fsm = VCI_BC_SEND ;
254                r_cmd_address = 0x3 | (0x7c1f << vci_param::N-20) ;
255              }
256            }
257            else                        // its a single request
258            {
259              int id = -1;
260              for(size_t i = 1; i < m_tab_size; i++){   // ID 0 reserved for broadcast transactions
261                if(r_pending_fsm[i].read() == false)
262                {
263                  id = (int)i;
264                  break;
265                }
266              }
267              if(id != -1){
268                r_cmd_fsm = VCI_SINGLE_SEND ;
269                r_cmd_count = 0;
270                r_cmd_trdid = id;
271              }
272#ifdef DETERMINISTIC
273              m_local_seed = r_cmd_seed.read();
274              r_cmd_address = destAdress(&m_local_seed) << (vci_param::N)-(soclib::common::uint32_log2((uint32_t)m_xmesh)+soclib::common::uint32_log2((uint32_t)m_ymesh));
275              r_cmd_seed = m_local_seed;
276#else
277              r_cmd_address = destAdress() << (vci_param::N)-(soclib::common::uint32_log2((uint32_t)m_xmesh)+soclib::common::uint32_log2((uint32_t)m_ymesh));
278#endif
279            }
280          }
281          break;
282        }
283      case VCI_SINGLE_SEND:
284        {
285          if ( p_vci.cmdack.read())
286          {
287            r_cmd_count = r_cmd_count.read() + 1;
288            if (r_cmd_count.read() == m_length-1) 
289            {
290              //r_nb_single = r_nb_single.read() + 1;
291              r_cmd_fsm = VCI_SINGLE_REGISTER ;
292            }
293          }
294          break;
295        }
296      case VCI_SINGLE_REGISTER:
297        {
298          r_pending_date[r_cmd_trdid.read()] = (uint64_t)(r_date_fifo.read());
299          r_pending_fsm[r_cmd_trdid.read()] = true;
300          fifo_get = true;
301          r_cmd_fsm = VCI_IDLE;
302        }
303      case VCI_BC_SEND:
304        {
305          if (p_vci.cmdack.read()) 
306          {
307            r_bc_nrsp = (m_xmax - m_xmin) * (m_ymax - m_ymin) ;
308            r_pending_fsm[0] = true;
309            r_pending_date[0] = (uint64_t)(r_date_fifo.read());
310            fifo_get = true;
311            r_cmd_fsm = VCI_IDLE;
312            break;
313          }
314        }
315    } // end switch vci_fsm
316
317   
318    ///////////////////
319    // PENDING FSMs
320    //////////////////
321    if(p_vci.rspval.read())
322    {
323      if(p_vci.rtrdid.read() == 0)      // not a broadcast
324      {
325        assert( ( r_pending_fsm[0].read() == true ) && 
326                "illegal broadcast response received");
327        r_bc_nrsp = r_bc_nrsp.read() - 1 ;
328        if(r_bc_nrsp.read() == 1) 
329        {
330          r_pending_fsm[0] = false;
331          r_latency_bc = r_latency_bc.read() + (r_cpt_cycles.read() - r_pending_date[0].read());
332        }
333      }
334      else
335      {
336        assert( ( r_pending_fsm[(int)p_vci.rtrdid.read()] == true ) && 
337                "illegal single response received");
338        r_pending_fsm[p_vci.rtrdid.read()] = false;
339        r_latency_single = r_latency_single.read() + 
340                           (r_cpt_cycles.read() - r_pending_date[(int)p_vci.rtrdid.read()].read());
341      }
342    }
343
344    ////////////////////////
345    //  traffic regulator
346    ////////////////////////
347#ifdef RANDOM_PERIOD
348    if ( m_bc_period && (r_cpt_period.read() == 0) )
349#else
350    if ( m_bc_period && (r_cpt_period.read() > m_bc_period) )
351#endif
352    { 
353      fifo_put = true ;
354      fifo_bc  = true;
355      if (r_date_fifo.wok())   
356      {
357        r_nb_bc = r_nb_bc.read() + 1;
358#ifdef RANDOM_PERIOD
359        r_cpt_period = (uint64_t) (rand()%(2*m_bc_period));
360#else
361        r_cpt_period = 0;
362#endif
363      }
364    }
365    else if( ( (uint64_t)(m_rho*r_cpt_cycles.read()) > (uint64_t)(m_length*r_nb_single.read()*1000)) )
366    {
367      fifo_put = true ;
368      fifo_bc  = false;
369      if (r_date_fifo.wok())   r_nb_single = r_nb_single.read() + 1;
370    }
371
372#ifdef RANDOM_PERIOD
373    if ( m_bc_period && (r_cpt_period.read() == 0) && r_date_fifo.wok() ) 
374        r_cpt_period = (uint64_t) (rand()%(2*m_bc_period));
375    else
376      r_cpt_period = r_cpt_period.read() - 1;
377#else
378    if ( m_bc_period && (r_cpt_period.read() > m_bc_period) && r_date_fifo.wok() ) 
379      r_cpt_period = 0;
380    else
381      r_cpt_period = r_cpt_period.read() + 1;
382#endif
383
384    ////////////////////////
385    //  update fifos
386    ////////////////////////
387    if (fifo_put){
388      if (fifo_get){
389        r_date_fifo.put_and_get(r_cpt_cycles.read());
390        r_bc_fifo.put_and_get(fifo_bc);
391      } else {
392        r_date_fifo.simple_put(r_cpt_cycles.read());
393        r_bc_fifo.simple_put(fifo_bc);
394      }
395    } else {
396      if (fifo_get){
397        r_date_fifo.simple_get();
398        r_bc_fifo.simple_get();
399      }
400    }
401   
402    ///////////////////////////
403    //  increment local time
404    ///////////////////////////
405    r_cpt_cycles = r_cpt_cycles.read() + 1;
406
407    return;
408
409  } // end transition()
410
411  /////////////////////////////
412  tmpl(void)::genMoore()
413  /////////////////////////////
414  {
415    ////////////////////////////////////////////////////////////
416    // Command signals on the p_vci port
417    ////////////////////////////////////////////////////////////
418     p_vci.cmd        = vci_param::CMD_WRITE;   
419     p_vci.be         = 0xF;                             
420     p_vci.srcid      = m_srcid;   
421     p_vci.cons       = false;       
422     p_vci.wrap       = false;       
423     p_vci.contig     = true;       
424     p_vci.clen       = 0;         
425     p_vci.cfixed     = false;           
426     p_vci.rspack     = true;
427
428
429    switch ( r_cmd_fsm.read() ) {
430
431      //////////////////
432      case VCI_IDLE:
433        {
434          p_vci.cmdval  = false;                 
435          p_vci.address = 0; 
436          p_vci.plen    = 0;                                         
437          p_vci.wdata   = 0;                                       
438          p_vci.trdid   = 0;                 
439          p_vci.pktid      = 0;     
440          p_vci.eop     = false;                                   
441          break;
442        }
443        //////////////////
444      case VCI_SINGLE_SEND:
445        {
446          p_vci.cmdval  = true;                 
447          p_vci.address = (addr_t)(r_cmd_address.read() + (r_cmd_count.read()*4)); 
448          p_vci.plen    = m_length*4;                                         
449          p_vci.wdata   = 0;                                       
450          p_vci.trdid   = r_cmd_trdid.read();                 
451          p_vci.pktid   = 0;     
452          if (r_cmd_count.read() == m_length - 1 ) {
453            p_vci.eop     = true;                                   
454          } else {
455            p_vci.eop     = false;                                   
456          }
457          break;
458        }
459        ///////////////////
460      case VCI_BC_SEND:
461        {
462          p_vci.cmdval  = true;                 
463          p_vci.address = (addr_t) r_cmd_address.read(); 
464          p_vci.plen    = 4;                                         
465          p_vci.wdata   = 0;                                       
466          p_vci.trdid   = 0;                 
467          p_vci.pktid   = 0;     
468          p_vci.eop     = true;                                   
469          break;
470        }
471      //////////////////
472      case VCI_SINGLE_REGISTER:
473        {
474          p_vci.cmdval  = false;                 
475          p_vci.address = 0; 
476          p_vci.plen    = 0;                                         
477          p_vci.wdata   = 0;                                       
478          p_vci.trdid   = 0;                 
479          p_vci.pktid   = 0;     
480          p_vci.eop     = false;                                   
481          break;
482        }
483    } // end switch vci_cmd_fsm
484
485  } // end genMoore()
486
487}} // end name space
Note: See TracBrowser for help on using the repository browser.