test1.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  test1.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/*
39  Corner case testing for new scheduler.
40  Case 1. Checking triggering of a sc_async/sc_aproc
41  vis-a-vis a synchronous process.
42  This test checks to ensure that a synch. and async. sensitive
43  to a clock are triggered correctly.
44  */
45
46#include "systemc.h"
47
48SC_MODULE( syncproc )
49{
50  SC_HAS_PROCESS( syncproc );
51
52  sc_in<bool> clk;
53
54  const sc_signal<int>& in1;
55  const sc_signal<int>& in2;
56  sc_signal<int>& out;
57
58  syncproc(sc_module_name NAME,
59	   sc_signal_in_if<bool>& CLK,
60	   const sc_signal<int>& IN1,
61	   const sc_signal<int>& IN2,
62	   sc_signal<int>& OUT_)
63    : in1(IN1), in2(IN2), out(OUT_)
64  {
65    SC_CTHREAD( entry, clk.pos() );
66    clk(CLK);
67    out = 0;
68  }
69
70  void entry()
71  {
72    int i = 100;
73    while (true) {
74      out = i;
75      wait();
76      while (in1.read() != i) {
77	cout << "Sync: Value written = " << i << "  value1 read = " << in1.read() << "  value2 read = " << in2.read() << endl;
78	wait();
79	cout << "Waited one cycle\n" << endl;
80      }
81      i++;
82    }
83  }
84};
85
86SC_MODULE( asyncproc )
87{
88  SC_HAS_PROCESS( asyncproc );
89
90  const sc_signal<int>& in;
91  sc_signal<int>& out;
92  sc_in<bool> clock;
93
94  asyncproc(sc_module_name NAME,
95	    const sc_signal<int>& IN_,
96	    sc_signal<int>& OUT_,
97	    sc_signal_in_if<bool>& CLOCK)
98    : in(IN_), out(OUT_)
99  {
100    out = 0;
101    clock(CLOCK);
102    SC_THREAD( entry );
103    sensitive << clock.pos();
104  }
105
106  void entry()
107  {
108    wait();
109    while (true) {
110      if (clock.posedge()) {
111	out = in;
112	cout << "AsyncProc: Value read = " << in.read() << endl;
113      }
114      else {
115	cout << "Error" << endl;
116      }
117      wait();
118    }
119  }
120};
121
122SC_MODULE( asyncblock )
123{
124  SC_HAS_PROCESS( asyncblock );
125
126  const sc_signal<int>& in;
127  sc_signal<int>& out;
128  sc_in<bool> clock;
129
130  asyncblock(sc_module_name NAME,
131	     const sc_signal<int>& IN_,
132	     sc_signal<int>& OUT_,
133	     sc_signal_in_if<bool>& CLOCK)
134    : in(IN_), out(OUT_)
135  {
136    clock(CLOCK);
137    out = 0;
138    SC_METHOD( entry );
139    sensitive << clock;
140  }
141
142  void entry()
143  {
144    if (clock.posedge()) {
145      out = in;
146      cout << "AsyncBlock: Value read = " << in.read() << endl;
147    }
148    else {
149      cout << "Seen other edge" << endl;
150    }
151  }
152};
153
154
155int
156sc_main(int ac, char *av[])
157{
158  sc_signal<int> a, b, c;
159
160  sc_clock clock("Clock", 20, SC_NS, 0.5);
161
162  syncproc P1("P1", clock, a, b, c);
163  asyncproc P2("P2", c, a, clock);
164  asyncblock P3("P3", c, b, clock);
165
166  sc_trace_file *tf = sc_create_vcd_trace_file("systemc");
167  tf->set_time_unit(1, SC_NS);
168  sc_trace(tf, a, "SYNC-IN1");
169  sc_trace(tf, b, "SYNC-IN2");
170  sc_trace(tf, c, "SYNC2-OUT");
171  sc_trace(tf, clock, "Clock");
172
173  sc_start(160, SC_NS);
174  return 0;
175
176}
177