cgen.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  cgen.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
40/* Filename seqgen.h */
41/* This is the interface file for asynchronous process 'seqgen' */
42
43SC_MODULE( seqgen )
44{
45  SC_HAS_PROCESS( seqgen );
46
47  sc_in<bool> clock_i;
48  sc_out<int> data_o;
49
50  // Constructor
51  seqgen( sc_module_name NAME,
52	  sc_signal<bool>& CLOCK_I,
53	  sc_signal<int>& DATA_O )
54  {
55    clock_i(CLOCK_I);
56	data_o(DATA_O);
57    SC_THREAD( entry );
58    sensitive << clock_i;
59  }
60
61  // Functionality of process
62  void entry();
63};
64
65
66/* Filename seqgen.cc */
67/* This is the implementation file for asynchronous process 'seqgen' */
68
69void seqgen::entry()
70{
71  // Initialization code can go here
72  int i = 17;
73  wait();
74
75  while (true) { // Infinite loop encompassing functionality
76    if (clock_i.posedge()) {
77      // Generate output only on positive edge of clock
78      data_o.write(i);
79      i = ((i * i) % 1019) - 9;
80    }
81    wait();
82  }
83}
84
85
86// Interface and implementation files for
87// Asynchronous block for code generation
88
89SC_MODULE( codegen )
90{
91  SC_HAS_PROCESS( codegen );
92
93  sc_in<int>  data_i;
94  sc_out<int> code_o;
95
96  int sum, num;
97
98  // Constructor
99  codegen( sc_module_name NAME,
100	   sc_signal<int>& DATA_I,
101	   sc_signal<int>& CODE_O )
102  {
103    data_i(DATA_I);
104	code_o(CODE_O);
105    SC_METHOD( entry );
106    sensitive << data_i;
107
108    sum = 0;
109    num = 0;
110  }
111
112  // Process functionality
113  void entry();
114};
115
116void codegen::entry()
117{
118  // Simple code generation routine
119  sum += data_i.read();
120  num++;
121  code_o.write(sum % num);
122}
123
124void testbench(const sc_signal<int>& data,
125	       const sc_signal<int>& code,
126	       sc_signal<bool>& clock)
127{
128  int i;
129
130  sc_start(0, SC_NS);
131  for (i=0; i<100; i++) {
132    sc_start( 10, SC_NS );
133    clock.write(1);
134    sc_start( 10, SC_NS );
135    clock.write(0);
136    char buf[BUFSIZ];
137    sprintf( buf, "Data = %4d\tCode = %4d", data.read(), code.read() );
138    cout << buf << endl;
139  }
140}
141
142int
143sc_main(int ac, char *av[])
144{
145  sc_signal<bool> clock("Clock");
146  sc_signal<int> data("Data");
147  sc_signal<int> code("Code");
148
149   clock = 0;
150   data = 0;
151   code = 0;
152
153  seqgen S("S", clock, data);
154  codegen C("C", data, code);
155
156  testbench(data, code, clock);
157  return 0;
158}
159