24 std::stringstream
function;
32 std::string internal_state =
"IQ";
33 std::string internal_negated_state =
"IQN";
35 function <<
"reg " << internal_state <<
";" << std::endl;
36 function <<
"reg " << internal_negated_state <<
";" << std::endl;
37 function << std::endl;
40 init_component !=
nullptr)
42 function <<
"initial begin" << std::endl;
43 function <<
"\t" << internal_state <<
" = INIT;" << std::endl;
44 function <<
"\t" << internal_negated_state <<
" = !(INIT);" << std::endl;
45 function <<
"end" << std::endl;
47 function << std::endl;
49 auto async_reset_function = latch_component->get_async_reset_function();
50 auto async_set_function = latch_component->get_async_set_function();
51 auto enable_function = latch_component->get_enable_function();
52 auto data_function = latch_component->get_data_in_function();
54 bool reset_async =
false;
55 bool set_async =
false;
59 if (!enable_function.is_empty())
62 function <<
"wire new_enable;" << std::endl;
64 function << std::endl;
66 if (!async_reset_function.is_empty())
69 function <<
"wire new_async_reset;" << std::endl;
70 function <<
"assign new_async_reset = " << async_reset_function.to_string(
verilog_function_printer) <<
";" << std::endl;
71 function << std::endl;
73 if (!async_set_function.is_empty())
76 function <<
"wire new_async_set;" << std::endl;
78 function << std::endl;
81 function << std::endl;
86 function <<
"always @(new_enable";
90 function <<
" or new_async_reset";
95 function <<
" or new_async_set";
100 function <<
"always @(new_async_set";
104 function <<
" or new_async_set";
109 function <<
"always @(new_async_set";
113 log_error(
"verilator",
"gate '{}' has no signals in process, please check gate lib, aborting...", gt->
get_name());
114 return std::string();
117 function <<
")" << std::endl;
118 function <<
"begin" << std::endl;
120 bool if_used =
false;
122 if (reset_async && set_async)
124 auto [behav_state, behave_neg_state] = latch_component->get_async_set_reset_behavior();
126 function << (if_used ?
"\telse if (" :
"\tif (") << async_reset_function.to_string(
verilog_function_printer) <<
" & " << async_set_function <<
") begin" << std::endl;
131 function <<
"\t\t" << internal_state <<
" <= 1'b0;" << std::endl;
134 function <<
"\t\t" << internal_state <<
" <= 1'b1;" << std::endl;
137 function <<
"\t\t" << internal_state <<
" <= !(" << internal_state <<
");" << std::endl;
140 function <<
"\t\t" << internal_state <<
" <= " << internal_state <<
";" << std::endl;
143 function <<
"\t\t" << internal_state <<
" <= 1'bX;" << std::endl;
147 log_error(
"verilator",
"unimplemented reached: found weird AsyncSetResetBehaviour for gate: {}", gt->
get_name());
148 return std::string();
152 switch (behave_neg_state)
155 function <<
"\t\t" << internal_negated_state <<
" <= 1'b0;" << std::endl;
158 function <<
"\t\t" << internal_negated_state <<
" <= 1'b1;" << std::endl;
161 function <<
"\t\t" << internal_negated_state <<
" <= !(" << internal_negated_state <<
");" << std::endl;
164 function <<
"\t\t" << internal_negated_state <<
" <= " << internal_negated_state <<
";" << std::endl;
167 function <<
"\t\t" << internal_negated_state <<
" <= 1'bX;" << std::endl;
171 log_error(
"verilator",
"unimplemented reached: found weird AsyncSetResetBehaviour for gate: {}", gt->
get_name());
172 return std::string();
175 function <<
"\tend" << std::endl;
181 function << (if_used ?
"\telse if (" :
"\tif (") << async_reset_function.to_string(
verilog_function_printer) <<
") begin" << std::endl;
182 function <<
"\t\t" << internal_state <<
" <= 1'b0;" << std::endl;
183 function <<
"\t\t" << internal_negated_state <<
" <= 1'b1;" << std::endl;
184 function <<
"\tend" << std::endl;
189 function << (if_used ?
"\telse if (" :
"\tif (") << async_set_function.to_string(
verilog_function_printer) <<
") begin" << std::endl;
190 function <<
"\t\t" << internal_state <<
" <= 1'b1;" << std::endl;
191 function <<
"\t\t" << internal_negated_state <<
" <= 1'b0;" << std::endl;
192 function <<
"\tend" << std::endl;
199 function << (if_used ?
"\telse if (" :
"\tif (") << enable_function.to_string(
verilog_function_printer) <<
") begin" << std::endl;
200 function << (if_used ?
"\t" :
"") <<
"\t" << internal_state <<
" <= " << data_function <<
";" << std::endl;
201 function << (if_used ?
"\t" :
"") <<
"\t" << internal_negated_state <<
" <= !(" << data_function <<
");" << std::endl;
202 function <<
"\tend" << std::endl;
205 function <<
"\telse begin" << std::endl;
206 function << (if_used ?
"\t" :
"") <<
"\t" << internal_state <<
" <= " << internal_state <<
";" << std::endl;
207 function << (if_used ?
"\t" :
"") <<
"\t" << internal_negated_state <<
" <= " << internal_negated_state <<
";" << std::endl;
208 function <<
"\tend" << std::endl;
215 if (data_function.is_empty())
217 function <<
"\telse begin" << std::endl;
218 function << (if_used ?
"\t" :
"") <<
"\t" << internal_state <<
" <= " << internal_state <<
";" << std::endl;
219 function << (if_used ?
"\t" :
"") <<
"\t" << internal_negated_state <<
" <= " << internal_negated_state <<
";" << std::endl;
220 function <<
"\tend" << std::endl;
224 function << (if_used ?
"\telse begin\n" :
"");
225 function << (if_used ?
"\t" :
"") <<
"\t" << internal_state <<
" <= " << data_function <<
";" << std::endl;
226 function << (if_used ?
"\t" :
"") <<
"\t" << internal_negated_state <<
" <= !(" << data_function <<
");" << std::endl;
227 function <<
"\tend" << std::endl;
231 function <<
"end" << std::endl;
234 for (
const GatePin* output_pin : output_pins)
236 function <<
"assign " << output_pin->get_name() <<
" = " << gt->
get_boolean_function(output_pin) <<
";" << std::endl;
241 log_error(
"verilator",
"cannot get FFComponent, aborting...");
242 return function.str();
245 return function.str();
T * get_component_as(const std::function< bool(const GateTypeComponent *)> &filter=nullptr)
std::vector< GatePin * > get_output_pins() const
const std::string & get_name() const
T * get_component_as(const std::function< bool(const GateTypeComponent *)> &filter=nullptr) const
BooleanFunction get_boolean_function(const std::string &name) const
#define log_error(channel,...)
std::string get_function_for_latch(const GateType *gt)
Result< std::string > verilog_function_printer(const BooleanFunction::Node &node, std::vector< std::string > &&operands)