11 u32 get_data_word(
const std::vector<u64>& data,
u32 address,
u32 width)
14 u32 row = start_pos >> 6;
15 u32 start_bit = start_pos & 0x3F;
17 if (row >=
data.size() || 64 % width != 0 || width > 32)
19 log_error(
"hal_simulator",
"cannot read data word of width {} at address {0:x}.", width, address);
24 return (
u32)((
data.at(row) >> start_bit) & ((1 << width) - 1));
27 void set_data_word(std::vector<u64>& data,
u32 in_data,
u32 address,
u32 width)
30 u32 row = start_pos >> 6;
31 u32 start_bit = start_pos & 0x3F;
33 if (row >=
data.size() || 64 % width != 0 || width > 32)
35 log_error(
"hal_simulator",
"cannot write data word {0:x} of width {} to address {0:x}.", in_data, width, address);
40 u64 mask = ~((((
u64)1 << width) - 1) << start_bit);
41 data.at(row) = (
data.at(row) & mask) ^ ((
u64)in_data << start_bit);
45 NetlistSimulator::SimulationGateRAM::SimulationGateRAM(
const Gate* gate) : SimulationGateSequential(gate)
47 const GateType* gate_type = gate->get_type();
48 const RAMComponent* ram_component = gate_type->get_component_as<RAMComponent>([](
const GateTypeComponent* c) {
return RAMComponent::is_class_of(c); });
49 assert(ram_component !=
nullptr);
51 m_bit_size = ram_component->get_bit_size();
53 for (
const GateTypeComponent* component : ram_component->get_components([](
const GateTypeComponent* c) { return RAMPortComponent::is_class_of(c); }))
55 const RAMPortComponent* port_component = component->convert_to<RAMPortComponent>();
58 simulation_port.clock_func = port_component->get_clock_function();
59 simulation_port.enable_func = port_component->get_enable_function();
60 simulation_port.is_write = port_component->is_write_port();
62 for (GatePin* pin : gate_type->get_pin_group_by_name(port_component->get_data_group())->get_pins())
64 simulation_port.data_pins.push_back(pin);
67 for (GatePin* pin : gate_type->get_pin_group_by_name(port_component->get_address_group())->get_pins())
69 simulation_port.address_pins.push_back(pin);
73 for (
const std::string& var : simulation_port.clock_func.get_variable_names())
75 if (
const GatePin* pin = gate_type->get_pin_by_name(var); pin !=
nullptr && pin->get_type() == PinType::clock)
77 simulation_port.clock_net = gate->get_fan_in_net(pin);
81 assert(simulation_port.clock_net !=
nullptr);
83 m_ports.push_back(simulation_port);
89 GateType* gate_type = m_gate->get_type();
91 const RAMComponent* ram_component = gate_type->get_component_as<RAMComponent>([](
const GateTypeComponent* c) {
return RAMComponent::is_class_of(c); });
92 if (ram_component ==
nullptr)
94 log_error(
"hal_simulator",
"cannot find RAM properties for RAM gate '{}' with ID {} of type '{}'.", m_gate->get_name(), m_gate->get_id(), gate_type->get_name());
100 const InitComponent* init_component = gate_type->get_component_as<InitComponent>([](
const GateTypeComponent* c) {
return InitComponent::is_class_of(c); });
101 if (init_component ==
nullptr)
103 log_error(
"hal_simulator",
"cannot find initialization data for RAM gate '{}' with ID {} of type '{}'.", m_gate->get_name(), m_gate->get_id(), gate_type->get_name());
107 const std::string& category = init_component->get_init_category();
109 for (
const std::string&
identifier : init_component->get_init_identifiers())
111 const std::string
data = std::get<1>(m_gate->get_data(category,
identifier));
114 assert(data_len % 16 == 0);
116 for (
u32 i = 0; i < data_len; i += 16)
118 m_data.push_back(strtoull(
data.substr(data_len - i - 16, 16).c_str(),
nullptr, 16));
122 if (ram_component->get_bit_size() != m_data.size() * 64)
124 log_error(
"hal_simulator",
"initialization data does not fit memory size for RAM gate '{}' with ID {} of type '{}'.", m_gate->get_name(), m_gate->get_id(), gate_type->get_name());
135 case BooleanFunction::Value::ONE:
136 init_val = 0xFFFFFFFFFFFFFFFF;
138 case BooleanFunction::Value::ZERO:
141 case BooleanFunction::Value::X:
142 case BooleanFunction::Value::Z:
143 log_error(
"hal_simulator",
"RAM gate '{}' with ID {} of type {} cannot be initialized with value '{}'.", m_gate->get_name(), m_gate->get_id(), gate_type->get_name(), BooleanFunction::to_string(value));
147 assert(ram_component->get_bit_size() % 64 == 0);
148 u32 num_words = ram_component->get_bit_size() / 64;
150 for (
u32 i = 0; i < num_words; i++)
152 m_data.push_back(init_val);
157 for (
const Port& port : m_ports)
161 u32 data_size = port.data_pins.size();
162 for (
u32 j = 0; j < data_size; j++)
164 const Net* out_net = m_gate->get_fan_out_net(port.data_pins.at(j));
165 new_events[out_net] = BooleanFunction::Value::ZERO;
171 bool NetlistSimulator::SimulationGateRAM::simulate(
const Simulation& simulation,
const WaveEvent& event, std::map<std::pair<const Net*, u64>, BooleanFunction::Value>& new_events)
177 for (
size_t i = 0; i < m_ports.size(); i++)
179 Port& port = m_ports.at(i);
180 if (event.affected_net == port.clock_net && port.clock_func.evaluate(m_input_values).get() == BooleanFunction::ONE)
182 if (port.enable_func.evaluate(m_input_values).get() == BooleanFunction::Value::ONE)
186 m_clocked_write_ports.push_back(i);
190 m_clocked_read_ports.push_back(i);
196 if (!m_clocked_read_ports.empty() || !m_clocked_write_ports.empty())
205 void NetlistSimulator::SimulationGateRAM::clock(
const u64 current_time, std::map<std::pair<const Net*, u64>, BooleanFunction::Value>& new_events)
210 std::unordered_map<std::string, BooleanFunction> functions = m_gate->get_boolean_functions();
212 for (
size_t index : m_clocked_read_ports)
214 Port port = m_ports.at(
index);
216 std::vector<BooleanFunction::Value> address_values;
217 for (
const GatePin* pin : port.address_pins)
219 address_values.push_back(m_input_values.at(pin->get_name()));
223 u32 data_size = port.data_pins.size();
226 u32 read_data = get_data_word(m_data, address, data_size);
229 assert(data_values.size() == data_size);
232 for (
u32 i = 0; i < data_size; i++)
234 const Net* out_net = m_gate->get_fan_out_net(port.data_pins.at(i));
235 new_events[std::make_pair(out_net, current_time + delay)] = data_values.at(i);
239 for (
size_t index : m_clocked_write_ports)
241 Port port = m_ports.at(
index);
243 u32 data_size = port.data_pins.size();
248 for (
u32 i = 0; i < data_size; i++)
250 const GatePin* pin = port.data_pins.at(i);
253 if (
auto func_it = functions.find(pin->get_name()); func_it != functions.end() && func_it->second.evaluate(m_input_values).get() != BooleanFunction::Value::ONE)
258 data_values[i] = m_input_values.at(pin->get_name());
261 set_data_word(m_data, write_data, address, data_size);
264 m_clocked_read_ports.clear();
265 m_clocked_write_ports.clear();
#define log_error(channel,...)
u32 get_int_bus_value(const std::unordered_map< std::string, BooleanFunction::Value > &signal_values, const std::vector< GatePin * > &ordered_pins)
std::vector< BooleanFunction::Value > int_to_values(u32 integer, u32 len)
u32 values_to_int(const std::vector< BooleanFunction::Value > &values)