vvp: Initialize automatic 2-state vectors to 0

Automatic 2-state vectors currently get initialized to 'hx, while their
default value should be 0.

Make sure the vector is initialized to 0 at the beginning of the automatic
context.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2022-06-05 18:39:56 +02:00
parent 999bcb6935
commit 0dfbcbdf47
5 changed files with 36 additions and 20 deletions

View File

@ -1794,16 +1794,28 @@ void vvp_vector4_t::change_z2x()
} }
} }
void vvp_vector4_t::set_to_x() void vvp_vector4_t::fill_bits(vvp_bit4_t bit)
{ {
/* note: this relies on the bit encoding for the vvp_bit4_t. */
static const unsigned long init_atable[4] = {
WORD_0_ABITS,
WORD_1_ABITS,
WORD_Z_ABITS,
WORD_X_ABITS };
static const unsigned long init_btable[4] = {
WORD_0_BBITS,
WORD_1_BBITS,
WORD_Z_BBITS,
WORD_X_BBITS };
if (size_ <= BITS_PER_WORD) { if (size_ <= BITS_PER_WORD) {
abits_val_ = vvp_vector4_t::WORD_X_ABITS; abits_val_ = init_atable[bit];
bbits_val_ = vvp_vector4_t::WORD_X_BBITS; bbits_val_ = init_btable[bit];
} else { } else {
unsigned words = (size_ + BITS_PER_WORD - 1) / BITS_PER_WORD; unsigned words = (size_ + BITS_PER_WORD - 1) / BITS_PER_WORD;
for (unsigned idx = 0; idx < words; idx += 1) { for (unsigned idx = 0; idx < words; idx += 1) {
abits_ptr_[idx] = vvp_vector4_t::WORD_X_ABITS; abits_ptr_[idx] = init_atable[bit];
bbits_ptr_[idx] = vvp_vector4_t::WORD_X_BBITS; bbits_ptr_[idx] = init_btable[bit];
} }
} }
} }

View File

@ -312,8 +312,11 @@ class vvp_vector4_t {
// Change all Z bits to X bits. // Change all Z bits to X bits.
void change_z2x(); void change_z2x();
// Set all bits to the specified value.
void fill_bits(vvp_bit4_t bit);
// Change all bits to X bits. // Change all bits to X bits.
void set_to_x(); void set_to_x() { fill_bits(BIT4_X); }
// Display the value into the buf as a string. // Display the value into the buf as a string.
char*as_string(char*buf, size_t buf_len) const; char*as_string(char*buf, size_t buf_len) const;

View File

@ -333,12 +333,9 @@ const vvp_vector4_t& vvp_fun_signal4_sa::vec4_unfiltered_value() const
vvp_fun_signal4_aa::vvp_fun_signal4_aa(unsigned wid, vvp_bit4_t init) vvp_fun_signal4_aa::vvp_fun_signal4_aa(unsigned wid, vvp_bit4_t init)
{ {
/* To make init work we would need to save it and then use the
* saved value when we ran reset_instance(). For now just make
* sure it matches the value we use in reset_instance(). */
assert(init == BIT4_X);
context_idx_ = vpip_add_item_to_context(this, vpip_peek_context_scope()); context_idx_ = vpip_add_item_to_context(this, vpip_peek_context_scope());
size_ = wid; size_ = wid;
init_ = init;
} }
vvp_fun_signal4_aa::~vvp_fun_signal4_aa() vvp_fun_signal4_aa::~vvp_fun_signal4_aa()
@ -348,7 +345,7 @@ vvp_fun_signal4_aa::~vvp_fun_signal4_aa()
void vvp_fun_signal4_aa::alloc_instance(vvp_context_t context) void vvp_fun_signal4_aa::alloc_instance(vvp_context_t context)
{ {
vvp_set_context_item(context, context_idx_, new vvp_vector4_t(size_)); vvp_set_context_item(context, context_idx_, new vvp_vector4_t(size_, init_));
} }
void vvp_fun_signal4_aa::reset_instance(vvp_context_t context) void vvp_fun_signal4_aa::reset_instance(vvp_context_t context)
@ -356,7 +353,7 @@ void vvp_fun_signal4_aa::reset_instance(vvp_context_t context)
vvp_vector4_t*bits = static_cast<vvp_vector4_t*> vvp_vector4_t*bits = static_cast<vvp_vector4_t*>
(vvp_get_context_item(context, context_idx_)); (vvp_get_context_item(context, context_idx_));
bits->set_to_x(); bits->fill_bits(init_);
} }
#ifdef CHECK_WITH_VALGRIND #ifdef CHECK_WITH_VALGRIND

View File

@ -190,6 +190,7 @@ class vvp_fun_signal4_aa : public vvp_fun_signal_vec, public automatic_signal_ba
private: private:
unsigned context_idx_; unsigned context_idx_;
unsigned size_; unsigned size_;
vvp_bit4_t init_;
}; };
class vvp_fun_signal_real : public vvp_fun_signal_base { class vvp_fun_signal_real : public vvp_fun_signal_base {

View File

@ -176,15 +176,18 @@ void compile_variable(char*label, char*name,
vvp_net_t*net = new vvp_net_t; vvp_net_t*net = new vvp_net_t;
vvp_bit4_t init;
if (vpi_type_code == vpiIntVar)
init = BIT4_0;
else
init = BIT4_X;
if (vpip_peek_current_scope()->is_automatic()) { if (vpip_peek_current_scope()->is_automatic()) {
vvp_fun_signal4_aa*tmp = new vvp_fun_signal4_aa(wid); vvp_fun_signal4_aa*tmp = new vvp_fun_signal4_aa(wid, init);
net->fil = tmp; net->fil = tmp;
net->fun = tmp; net->fun = tmp;
} else if (vpi_type_code == vpiIntVar) {
net->fil = new vvp_wire_vec4(wid, BIT4_0);
net->fun = new vvp_fun_signal4_sa(wid);
} else { } else {
net->fil = new vvp_wire_vec4(wid, BIT4_X); net->fil = new vvp_wire_vec4(wid, init);
net->fun = new vvp_fun_signal4_sa(wid); net->fun = new vvp_fun_signal4_sa(wid);
} }
vvp_signal_value*vfil = dynamic_cast<vvp_signal_value*>(net->fil); vvp_signal_value*vfil = dynamic_cast<vvp_signal_value*>(net->fil);