summaryrefslogtreecommitdiff
path: root/src/intersim/buffer_state.cpp
blob: 72a327d14096377adb9ad942f34f92fb5412b395 (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
#include "booksim.hpp"
#include <iostream>
#include <stdlib.h>
#include <assert.h>

#include "buffer_state.hpp"
#include "random_utils.hpp"

void BufferState::init( const Configuration& config ) 
{
   _Init( config );
}

BufferState::BufferState( const Configuration& config, 
                          Module *parent, const string& name ) : 
Module( parent, name )
{
   _Init( config );
}

void BufferState::_Init( const Configuration& config )
{
   _buf_size     = config.GetInt( "vc_buf_size" );
   _vcs          = config.GetInt( "num_vcs" );

   _wait_for_tail_credit = config.GetInt( "wait_for_tail_credit" );

   _in_use       = new bool [_vcs];
   _tail_sent    = new bool [_vcs];
   _cur_occupied = new int  [_vcs];

   _last_avail   = 0;

   for ( int v = 0; v < _vcs; ++v ) {
      _in_use[v]       = false;
      _tail_sent[v]    = false;
      _cur_occupied[v] = 0;
   }
}

BufferState::~BufferState( )
{
   delete [] _in_use;
   delete [] _tail_sent;
   delete [] _cur_occupied;
}

void BufferState::ProcessCredit( Credit *c )
{
   assert( c );

   for ( int v = 0; v < c->vc_cnt; ++v ) {
      assert( ( c->vc[v] >= 0 ) && ( c->vc[v] < _vcs ) );

      if ( ( _wait_for_tail_credit ) && 
           ( !_in_use[c->vc[v]] ) ) {
         Error( "Received credit for idle buffer" );
      }

      if ( _cur_occupied[c->vc[v]] > 0 ) {
         --_cur_occupied[c->vc[v]];

         if ( ( _cur_occupied[c->vc[v]] == 0 ) && 
              ( _tail_sent[c->vc[v]] ) ) {
            _in_use[c->vc[v]] = false;
         }
      } else {
         cout << "VC = " << c->vc[v] << endl;
         Error( "Buffer occupancy fell below zero" );
      }
   }
}

void BufferState::SendingFlit( Flit *f )
{
   assert( f && ( f->vc >= 0 ) && ( f->vc < _vcs ) );

   if ( _cur_occupied[f->vc] < _buf_size ) {
      ++_cur_occupied[f->vc];

      if ( f->tail ) {
         _tail_sent[f->vc] = true;

         if ( !_wait_for_tail_credit ) {
            _in_use[f->vc] = false;
         }
      }
   } else {
      Error( "Flit sent to full buffer" );
   }
}

void BufferState::TakeBuffer( int vc )
{
   assert( ( vc >= 0 ) && ( vc < _vcs ) );

   if ( _in_use[vc] ) {
      Error( "Buffer taken while in use" );
   }

   _in_use[vc]    = true;
   _tail_sent[vc] = false;
}

bool BufferState::IsFullFor( int vc  ) const
{
   assert( ( vc >= 0 ) && ( vc < _vcs ) );
   return( _cur_occupied[vc] == _buf_size ) ? true : false;
}

bool BufferState::IsAvailableFor( int vc ) const
{
   assert( ( vc >= 0 ) && ( vc < _vcs ) );
   return !_in_use[vc];
}

int BufferState::FindAvailable( )
{
   int available_vc = -1;
   int vc;

   _last_avail = RandomInt( _vcs - 1 );

   for ( int v = 0; v < _vcs; ++v ) {
      vc = ( v + _last_avail + 1 ) % _vcs; // Round-robin

      if ( IsAvailableFor( vc ) ) {
         available_vc = vc;
         _last_avail  = vc;
         break;
      }
   }

   return available_vc;
}

void BufferState::Display( ) const
{
   cout << _fullname << " :" << endl;
   for ( int v = 0; v < _vcs; ++v ) {
      cout << "  buffer class " << v << endl;
      cout << "    in_use = " << _in_use[v] << " tail_sent = " << _tail_sent[v] << endl;
      cout << "    occupied = " << _cur_occupied[v] << endl;
   }
}