diff --git a/elab_net.cc b/elab_net.cc index b39a49497..866750ef8 100644 --- a/elab_net.cc +++ b/elab_net.cc @@ -2620,25 +2620,9 @@ NetNet* PENumber::elaborate_net(Design*des, NetScope*scope, net->local_flag(true); net->set_signed(value_->has_sign()); - /* when expanding a constant to fit into the net, extend - the Vx or Vz values if they are in the sign position, - otherwise extend the number with 0 bits. */ - verinum::V top_v = verinum::V0; - switch (value_->get(value_->len()-1)) { - case verinum::Vx: - top_v = verinum::Vx; - break; - case verinum::Vz: - top_v = verinum::Vz; - break; - default: /* V0 and V1, do nothing */ - break; - } - - verinum num(top_v, net->vector_width()); - unsigned idx; - for (idx = 0 ; idx < num.len() && idx < value_->len(); idx += 1) - num.set(idx, value_->get(idx)); + verinum num = pad_to_width(*value_, lwidth); + if (num.len() > lwidth) + num = verinum(num, lwidth); NetConst*tmp = new NetConst(scope, scope->local_symbol(), num); tmp->pin(0).drive0(drive0); diff --git a/pad_to_width.cc b/pad_to_width.cc index efd10fc6e..784e98918 100644 --- a/pad_to_width.cc +++ b/pad_to_width.cc @@ -25,6 +25,7 @@ # include "netlist.h" # include "netmisc.h" + /* * This function transforms an expression by padding the high bits * with V0 until the expression has the desired width. This may mean @@ -39,18 +40,7 @@ NetExpr*pad_to_width(NetExpr*expr, unsigned wid) /* If the expression is a const, then replace it with a wider const. This is a more efficient result. */ if (NetEConst*tmp = dynamic_cast(expr)) { - verinum eval = tmp->value(); - bool signed_flag = eval.has_sign(); - - verinum::V pad = verinum::V0; - if (signed_flag) - pad = eval.get(eval.len()-1); - verinum oval (pad, wid, eval.has_len()); - - for (unsigned idx = 0 ; idx < eval.len() ; idx += 1) - oval.set(idx, eval.get(idx)); - - oval.has_sign(signed_flag); + verinum oval = pad_to_width(tmp->value(), wid); tmp = new NetEConst(oval); delete expr; return tmp;