module_name.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  module_name.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
40int numbers[] = { 49597, 41218, 20635, 40894, 16767, 17233, 36246, 28171, 60879, 49566, 10971, 24107, 30561, 49648, 50031, 12559, 23787, 35674, 43320, 37558, 840, 18689, 62466, 6308, 46271, 49801, 43433, 22683, 35494, 35259, 29020, 19555, 10941, 49656, 60450, 27709, 1353, 31160, 55880, 62232, 15190, 1315, 20803, 45751, 50963, 5298, 58311, 9215, 2378 };
41
42int numbers_index = 0;
43
44struct example : sc_module {
45    sc_in_clk  clk;
46    sc_in<int> a;
47    sc_in<int> b;
48    sc_out<int> c;
49
50    sc_signal<int> d;
51    sc_signal<int> e;
52
53    void block_a();
54    void block_b();
55    void block_c();
56    void block_d();
57    void block_e();
58    void block_f();
59
60    SC_CTOR(example)
61    {
62        SC_METHOD( block_a );
63        sensitive << a;
64        sensitive << b;
65
66        SC_METHOD( block_b );
67        sensitive << a << b;
68
69        SC_METHOD( block_c );
70        sensitive << d << e;
71
72        SC_CTHREAD( block_d, clk.neg() );
73
74        SC_CTHREAD( block_e, clk.pos() );
75
76        SC_CTHREAD( block_f, clk.pos() );
77    }
78};
79
80void
81example::block_a()
82{
83    d = a + b;
84}
85
86void
87example::block_b()
88{
89    e = a - b;
90}
91
92void
93example::block_c()
94{
95    c = d * e;
96}
97
98void
99example::block_d()
100{
101    int i = 0;
102    while (true) {
103        cout << "block_d " << i << endl;
104        i++;
105        wait();
106    }
107}
108
109void
110example::block_e()
111{
112    int i = 0;
113    while (true) {
114        cout << "block_e " << i << endl;
115        i++;
116        wait();
117    }
118}
119
120void
121example::block_f()
122{
123    int i = 32;
124    while (true) {
125        cout << "block_f " << i << endl;
126        i++;
127        wait();
128    }
129}
130
131struct tb : sc_module {
132    sc_in_clk   clk;
133    sc_out<int> a;
134
135    void tb_proc();
136
137    SC_CTOR(tb)
138    {
139        SC_CTHREAD( tb_proc, clk.pos() );
140    }
141};
142
143void
144tb::tb_proc()
145{
146    while (true) {
147        a = numbers[numbers_index % (sizeof(numbers)/sizeof(numbers[0]))];
148        numbers_index++;
149        cout << "tb_proc " << endl;
150        wait();
151    }
152}
153
154struct tb2 : sc_module {
155    sc_in_clk   clk;
156    sc_out<int> b;
157
158    void tb2_proc();
159
160    SC_CTOR(tb2)
161    {
162        SC_CTHREAD( tb2_proc, clk.pos() );
163    }
164};
165
166void
167tb2::tb2_proc()
168{
169    while (true) {
170        b = numbers[numbers_index % (sizeof(numbers)/sizeof(numbers[0]))];
171        numbers_index++;
172        cout << "tb2_proc " << endl;
173        wait();
174    }
175}
176
177SC_MODULE( monitor )
178{
179    SC_HAS_PROCESS( monitor );
180
181    const sc_signal<int>& a;
182    const sc_signal<int>& b;
183    const sc_signal<int>& c;
184
185    monitor( sc_module_name,
186             const sc_signal<int>& A,
187             const sc_signal<int>& B,
188             const sc_signal<int>& C ) :
189        a(A), b(B), c(C)
190    {
191	SC_METHOD( entry );
192        sensitive << a;
193        sensitive << b;
194        sensitive << c;
195    }
196    void entry();
197};
198
199void
200monitor::entry()
201{
202    if (a.event()) cout << "a = " << a << endl;
203    if (b.event()) cout << "b = " << b << endl;
204    if (c.event()) cout << "c = " << c << endl;
205}
206
207int
208sc_main( int argc, char* argv[] )
209{
210    sc_signal<int> a("a");
211    sc_signal<int> b("b");
212    sc_signal<int> c("c");
213    sc_clock clk("clk", 10, SC_NS);
214
215    example ex1("ex1");
216    ex1(clk, a, b, c);
217
218    tb tbb1("tbb1");
219    tbb1(clk, a);
220
221    tb2 tbb2("tbb2");
222    tbb2(clk, b);
223
224    monitor mon("mon", a, b, c);
225
226    sc_start(200, SC_NS);
227    return 0;
228}
229