hwsw.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  hwsw.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38#include "systemc.h"
39
40static int randa[] = { 3278, 3420, 1349, 13491, 9234028, 234902,
41                       13971, 1498710, 1348179, 1389810, 190101, 92384 };
42static int randb[] = { 1349, 20893, 2090092, 8813, 87472, 73231,
43                       59413, 42713, 3192, 45653, 565643, 78931, 573231 };
44static int randc[] = { 3419, 82093, 9013, 1831, 74372, 233861,
45                       421313, 47823, 3902192, 93245653, 77565643, 77234, 10192 };
46
47SC_MODULE( adder_sub )
48{
49  SC_HAS_PROCESS( adder_sub );
50
51  sc_in_clk clk;
52
53  sc_fifo<int>& Sa; //input
54  sc_fifo<int>& Sb; //input
55  sc_fifo<int>& Sc; //input
56  sc_fifo<int>& Sd; //output
57  sc_fifo<int>& Ssum; //output
58
59  // constructor
60  adder_sub( sc_module_name NAME,
61	     sc_clock& CLK,
62	     sc_fifo<int>& SA,
63	     sc_fifo<int>& SB,
64	     sc_fifo<int>& SC,
65	     sc_fifo<int>& SD,
66	     sc_fifo<int>& SSUM )
67    : Sa(SA), Sb(SB), Sc(SC), Sd(SD), Ssum(SSUM)
68  {
69    clk(CLK);
70	SC_THREAD( entry );
71	sensitive << clk.pos();
72  }
73
74  // Process functionality in member function below
75  void entry();
76};
77
78
79int add(int a, int b)
80{
81  return (a + b);
82}
83
84void adder_sub::entry()
85{
86  int sum;
87  int a, b, c, d;
88
89  while (true) {
90    // Read inputs
91    a = Sa.read();
92    b = Sb.read();
93    c = Sc.read();
94
95    // Perform the computation.
96    sum = add(a, b);
97    sum = add(sum, c);
98    d = a - b;
99
100    // Write outputs
101    Ssum.write(sum);
102    Sd.write(d);
103    // Loop back to do { wait(); } while .
104  }
105
106} // end of entry function
107
108SC_MODULE( testbench )
109{
110  SC_HAS_PROCESS( testbench );
111
112  sc_in_clk clk;
113
114  sc_fifo<int>& Ssum; //input
115  sc_fifo<int>& Sdiff; //input
116  sc_fifo<int>& Sa; //output
117  sc_fifo<int>& Sb; //output
118  sc_fifo<int>& Sc; //output
119
120  // constructor
121  testbench( sc_module_name NAME,
122	     sc_clock& CLK,
123	     sc_fifo<int>& SSUM,
124	     sc_fifo<int>& SDIFF,
125	     sc_fifo<int>& SA,
126	     sc_fifo<int>& SB,
127	     sc_fifo<int>& SC )
128    : Ssum(SSUM), Sdiff(SDIFF), Sa(SA), Sb(SB), Sc(SC)
129  {
130    clk(CLK);
131	SC_THREAD( entry );
132	sensitive << clk.pos();
133  }
134
135  // Process functionality in member function below
136  void entry();
137};
138
139
140void testbench::entry()
141{
142  int a, b, c, d;
143  int sum;
144  int i;
145  char buf[BUFSIZ];
146
147  for (i=0; i < 10; i++) {
148    a = randa[i % (sizeof(randa)/sizeof(randa[0]))] & 0x0ff;
149    b = randb[i % (sizeof(randb)/sizeof(randb[0]))] & 0x0ff;
150    c = randc[i % (sizeof(randc)/sizeof(randc[0]))] & 0x0ff;
151
152    Sa.write(a);
153    Sb.write(b);
154    Sc.write(c);
155    sum = Ssum.read();
156    d = Sdiff.read();
157    // printf("A = %d, B = %d, C = %d, D = %d, SUM = %d\n", a, b, c, d, sum);
158    sprintf(buf, "A = %d, B = %d, C = %d, D = %d, SUM = %d\n", a, b, c, d, sum);
159    cout << buf;
160  }
161  sc_stop();
162
163} // end of entry function
164
165// Main routine
166
167int sc_main(int ac, char *av[])
168{
169  sc_fifo<int> a;
170  sc_fifo<int> b;
171  sc_fifo<int> c;
172  sc_fifo<int> d;
173  sc_fifo<int> sum;
174  sc_clock clock("Clock", 10, SC_NS, 0.5, 0, SC_NS, 0);
175
176  testbench T("TB", clock, sum, d, a, b, c);
177  adder_sub AS("AS", clock, a, b, c, d, sum);
178
179  sc_start();
180  return 0;
181}
182