File: simple_perf.cpp

package info (click to toggle)
systemc 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,768 kB
  • sloc: cpp: 181,958; sh: 4,925; asm: 2,700; perl: 1,980; ansic: 1,931; makefile: 1,761; fortran: 492; python: 482; awk: 157; csh: 50
file content (264 lines) | stat: -rw-r--r-- 7,419 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  simple_fifo.cpp -- SystemC 2.0 Performance Modeling Example.

                     This example is derived from the design presented within
                     "An Introduction to System Level Modeling in SystemC 2.0"
                     By Stuart Swan, Cadence Design Systems.
                     Available at www.accellera.org

                     The system being modeled has a producer block that
                     sends characters to a consumer block via a fifo.
                     The fifo will suspend the producer or consumer as
                     necessary to insure all characters are reliably
                     delivered.

                     The consumer block will consume exactly one
                     character every 100 ns unless it is suspended
                     waiting for input from the fifo.

                     The producer block produces between one and
                     19 characters every 1000 ns unless it is
                     suspended waiting to write to the fifo.
                     On average, the producer block produces
                     one character every 100 ns (unless suspended by
                     the fifo) since a random linear distribution is 
                     used for the character count.

                     If the fifo size is sufficiently large, the average
                     transfer time per character will approach 100 ns
                     since the producer and consumer will rarely be
                     blocked. However, as the fifo size decreases,
                     the average transfer time will increase because
                     the producer will sometimes be suspended when
                     it writes (due to a full fifo) and the consumer
                     will sometimes be suspended when it reads 
                     (due to an empty fifo).

                     The fifo size can be set via a command line argument
                     when running this program. By default, the fifo size
                     is 10. When the design is simulated, one hundred
                     thousand characters are transferred from the 
                     producer to the consumer and then performance
                     statistics are displayed.

                     Using this system level model, determine the size
                     of the fifo needed to sustain:

                     A) An average transfer time of 110 ns per character
                     B) An average transfer time of 105 ns per character

                     Hint: The answer to (A) is between 10 and 20.

  Original Author: Stuart Swan, Cadence Design Systems, 2001-06-18

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date:
  Description of Modification:

 *****************************************************************************/


#include <systemc.h>

class write_if : virtual public sc_interface
{
  public:
    virtual void write(char) = 0;
    virtual void reset() = 0;
};

class read_if : virtual public sc_interface
{
  public:
    virtual void read(char &) = 0;
    virtual int num_available() = 0;
};

class fifo : public sc_channel, public write_if, public read_if
{
  public:
    fifo(sc_module_name name, int size_) : sc_channel(name), size(size_)
    {
	data = new char[size];
	num_elements = first = 0;
	num_read = max_used = average = 0;
        last_time = SC_ZERO_TIME;
    }

    ~fifo()
    {
      delete[] data;

      cout << endl << "Fifo size is: " << size << endl;
      cout << "Average fifo fill depth: " << 
		double(average) / num_read << endl;
      cout << "Maximum fifo fill depth: " << max_used << endl;
      cout << "Average transfer time per character: " 
		<< last_time / num_read << endl;
      cout << "Total characters transferred: " << num_read << endl;
      cout << "Total time: " << last_time << endl;
    }

    void write(char c) {
      if (num_elements == size)
        wait(read_event);

      data[(first + num_elements) % size] = c;
      ++ num_elements;
      write_event.notify();
    }

    void read(char &c){
      last_time = sc_time_stamp();
      if (num_elements == 0)
        wait(write_event);

      compute_stats();

      c = data[first];
      -- num_elements;
      first = (first + 1) % size;
      read_event.notify();
    }

    void reset() { num_elements = first = 0; }

    int num_available() { return num_elements;}

  private:
    char *data;
    int num_elements, first;
    sc_event write_event, read_event;
    int size, num_read, max_used, average;
    sc_time last_time;

    void compute_stats()
    {
      average += num_elements;

      if (num_elements > max_used)
         max_used = num_elements;

      ++num_read;
    }
};

class producer : public sc_module
{
  public:
    sc_port<write_if> out;


    producer(sc_module_name name) : sc_module(name)
    {
      SC_THREAD(main);
    }

    void main()
    {
      const char *str =
	"Visit www.accellera.org and see what SystemC can do for you today!\n";
      const char *p = str;
      int total = 100000;

      while (true)
      {
	int i = 1 + int(19.0 * rand() / RAND_MAX);  //  1 <= i <= 19

	while (--i >= 0)
	{
          out->write(*p++);
	  if (!*p) p = str;
	  -- total;
	}

	if (total <= 0)
	  break;

	wait(1000, SC_NS);
      }
    }
};

class consumer : public sc_module
{
  public:
    sc_port<read_if> in;

    consumer(sc_module_name name) : sc_module(name)
    {
      SC_THREAD(main);
    }

    void main()
    {
      char c;

      while (true) {
        in->read(c);
	wait(100, SC_NS);
      }
    }
};

class top : public sc_module
{
  public:
    fifo fifo_inst;
    producer prod_inst;
    consumer cons_inst;

    top(sc_module_name name, int size) :
        sc_module(name) ,
	fifo_inst("Fifo1", size) , 
	prod_inst("Producer1") , 
	cons_inst("Consumer1")
    {
      prod_inst.out(fifo_inst);
      cons_inst.in(fifo_inst);
    }
};

int sc_main (int argc , char *argv[]) 
{
  int size = 10;

  if (argc > 1)
    size = atoi(argv[1]);

  if (size < 1)
    size = 1;

  if (size > 100000)
    size = 100000;

  top top1("Top1", size);
  sc_start();
  return 0;
}