scc  2022.4.0
SystemC components library
time_n_tick.cpp
1 /*******************************************************************************
2  * Copyright 2021 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 #ifndef SC_INCLUDE_DYNAMIC_PROCESSES
17 #define SC_INCLUDE_DYNAMIC_PROCESSES
18 #endif
19 #include "tick2time.h"
20 #include "time2tick.h"
21 
22 #include <systemc>
23 #ifdef CWR_SYSTEMC
24 #include "scml_clock/scml_clock_if.h"
25 #endif
26 
27 namespace scc {
28 
29 tick2time::tick2time(sc_core::sc_module_name nm)
30 : sc_core::sc_module(nm) {}
31 
32 void tick2time::end_of_elaboration() {
33 #ifdef CWR_SYSTEMC
34  if(auto scml_clk_if = scml2::get_scml_clock(clk_i)) {
35  scml_clk_if->register_observer(this);
36  handle_clock_parameters_updated(scml_clk_if);
37  return;
38  }
39 #endif
40  if(auto clk_if = dynamic_cast<sc_core::sc_clock*>(clk_i.get_interface())) {
41  this->clk_period = clk_if->period();
42  clk_o.write(clk_period);
43  } else {
44  sc_core::sc_spawn_options opts;
45  opts.set_stack_size(0x1000);
46  sc_core::sc_spawn(
47  [this]() {
48  while(true) {
49  wait(clk_i.posedge_event());
50  clk_period = sc_core::sc_time_stamp() - last_tick;
51  clk_o.write(clk_period);
52  last_tick = sc_core::sc_time_stamp();
53  }
54  },
55  nullptr, &opts);
56  }
57 }
58 #ifdef CWR_SYSTEMC
59 void tick2time::handle_clock_parameters_updated(scml_clock_if* clk_if) {
60  this->clk_period = clk_if->get_period();
61  clk_o.write(clk_period);
62 }
63 void tick2time::handle_clock_deleted(scml_clock_if*){};
64 #endif
65 
66 inline void time2tick::clocker() {
67  while(true) {
68  auto t = clk_i.read();
69  if(t == sc_core::SC_ZERO_TIME) {
70  wait(clk_i.value_changed_event());
71  t = clk_i.read();
72  }
73  clk_o = true;
74  wait(t / 2);
75  clk_o = false;
76  wait(t - t / 2);
77  }
78 }
79 } // namespace scc
SCC SystemC utilities.
tick2time(sc_core::sc_module_name nm)
Definition: time_n_tick.cpp:29
sc_core::sc_out< sc_core::sc_time > clk_o
the clock output
Definition: tick2time.h:46
sc_core::sc_in< bool > clk_i
the clock input
Definition: tick2time.h:48
sc_core::sc_out< bool > clk_o
the clock output
Definition: time2tick.h:39
sc_core::sc_in< sc_core::sc_time > clk_i
the clock input
Definition: time2tick.h:37