Files
OpenSTA/search/Sim.cc
T
James Cherry f6253af8a9 ccs ceff delay calc
commit 87130be63ddbf1a7fb65986b02839eb4c0b13168
Author: James Cherry <[email protected]>
Date:   Tue Feb 27 09:49:02 2024 -0700

    ccs ceff delay calc

    Signed-off-by: James Cherry <[email protected]>

commit de0dd38dabda2f7ef51b49c196c2787a0d3c5784
Author: James Cherry <[email protected]>
Date:   Tue Feb 27 07:40:11 2024 -0700

    dcalc public funcs

    Signed-off-by: James Cherry <[email protected]>

commit dd7fcb12f929b9b0a391653cad42e617f9cbdd3b
Author: James Cherry <[email protected]>
Date:   Mon Feb 26 09:08:37 2024 -0700

    mv CircuitSim.hh to include

    Signed-off-by: James Cherry <[email protected]>

commit 9663e46d28ece544ee1453f229990c9db9e0efec
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 17:58:57 2024 -0700

    ArcDcalcArg

    Signed-off-by: James Cherry <[email protected]>

commit 76b0588034faaefd2302c865c441975f76386d3f
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 15:36:46 2024 -0700

    ensureVoltageWaveforms

    Signed-off-by: James Cherry <[email protected]>

commit f88e67b861c56752e5b36efe2b552ba0077a7180
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 15:00:02 2024 -0700

    const

    Signed-off-by: James Cherry <[email protected]>

commit 8f32cc571dcadee0185b08f951a1f79d46e7984d
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 14:57:51 2024 -0700

    Graph::gateEdgeArc

    Signed-off-by: James Cherry <[email protected]>

commit ac3cb35cb6732d7ecbf0532d7351a3ff2a917fc9
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 14:31:30 2024 -0700

    ConcreteParasiticSubNodeMap, ConcreteParasiticPinNodeMap use id cmp

    Signed-off-by: James Cherry <[email protected]>

commit cbfe4eac463036c26a64701239d7651d91a09778
Author: James Cherry <[email protected]>
Date:   Sun Feb 25 14:08:41 2024 -0700

    WriteSpice

    Signed-off-by: James Cherry <[email protected]>

commit 8b5d30f1a8b1ccb8c9cbd9d7ba93418907c41b2a
Author: James Cherry <[email protected]>
Date:   Sat Feb 24 09:45:46 2024 -0700

    emplace_push

    Signed-off-by: James Cherry <[email protected]>

commit 5335a2eaaf737ed7c7a8cff30654a68c4ac4c8e4
Author: James Cherry <[email protected]>
Date:   Fri Feb 23 16:19:30 2024 -0700

    Parasitics::findParasiticNode

    Signed-off-by: James Cherry <[email protected]>

commit ce92f3caf28afb0e0384799f08166cfb0aecfea0
Author: James Cherry <[email protected]>
Date:   Fri Feb 23 15:53:28 2024 -0700

    Parasitics::findParasiticNode

    Signed-off-by: James Cherry <[email protected]>

commit 0c591430c725a3ebd50d2892673dca76e023dc32
Author: James Cherry <[email protected]>
Date:   Fri Feb 23 09:03:18 2024 -0700

    Parsitics::name(node) const

    Signed-off-by: James Cherry <[email protected]>

commit 499c297e64d1487388f549843ff9ea05e8555cfc
Author: James Cherry <[email protected]>
Date:   Fri Feb 23 09:03:07 2024 -0700

    write_spice umr

    Signed-off-by: James Cherry <[email protected]>

commit 6984c398dbce9e6266fab8377a844bc518481d9d
Author: James Cherry <[email protected]>
Date:   Thu Feb 22 18:42:34 2024 -0700

    gcc warning

    Signed-off-by: James Cherry <[email protected]>

commit edec16519806013623194d8201e804dec81a51dd
Author: James Cherry <[email protected]>
Date:   Thu Feb 22 17:54:11 2024 -0700

    no cuddification

    Signed-off-by: James Cherry <[email protected]>

commit 4a0e1070c179b2f8615b604c362359ce4b3a0e2e
Author: James Cherry <[email protected]>
Date:   Thu Feb 22 17:29:46 2024 -0700

    sim const

    Signed-off-by: James Cherry <[email protected]>

commit 2e941fafa631f6b9bc0f82784b9146de2449e9c5
Author: James Cherry <[email protected]>
Date:   Thu Feb 22 17:29:39 2024 -0700

    sdc comment

    Signed-off-by: James Cherry <[email protected]>

commit 1c12f56aee7115fcb06807b5b6c626d1a419ccdc
Author: James Cherry <[email protected]>
Date:   Wed Feb 21 13:13:29 2024 -0700

    Sim use Bdd class

    Signed-off-by: James Cherry <[email protected]>

commit b70c41d5caec56c3001b834141b6dab89bb933ed
Author: James Cherry <[email protected]>
Date:   Tue Feb 20 12:18:27 2024 -0700

    write_spice coupling caps

    Signed-off-by: James Cherry <[email protected]>

commit 614d2cd41a1a9cf850dbe480954a5f58ee0dc21e
Author: James Cherry <[email protected]>
Date:   Mon Feb 19 14:37:30 2024 -0700

    write_spice time offset

    Signed-off-by: James Cherry <[email protected]>

commit f0ba1fca0dfca384e6fb0be302bba9ced71ee41c
Author: James Cherry <[email protected]>
Date:   Mon Feb 19 10:59:18 2024 -0700

    class Bdd for cudd

    Signed-off-by: James Cherry <[email protected]>

commit 24c94756334fce5e70e97ce0ee31375ae4e59b84
Author: James Cherry <[email protected]>
Date:   Sun Feb 18 08:58:30 2024 -0700

    WriteSpice

    Signed-off-by: James Cherry <[email protected]>

commit 47a4505d88bdfe4a85056895f8b7d842e07dce8d
Author: James Cherry <[email protected]>
Date:   Fri Feb 16 21:34:23 2024 -0700

    default sim ngspice

    Signed-off-by: James Cherry <[email protected]>

commit 06e279555a076e218f0a9c308e8937a6fc8fdea4
Author: James Cherry <[email protected]>
Date:   Fri Feb 16 21:34:01 2024 -0700

    WriteSpice refactor

    Signed-off-by: James Cherry <[email protected]>

commit 06e3f0734edbbbd69ad063e97d1d8cca92a83aea
Author: James Cherry <[email protected]>
Date:   Thu Feb 15 15:18:35 2024 -0700

    mv report_dcalc to DelayCalc.tcl

    Signed-off-by: James Cherry <[email protected]>

commit 922056471a6d380699bbd0623f95637401d23eff
Author: James Cherry <[email protected]>
Date:   Thu Feb 15 14:27:31 2024 -0700

    WriteSpice::cell_spice_port_names_

    Signed-off-by: James Cherry <[email protected]>

commit 732922ead68097e3f7da268ecc5ae2ca2daa4492
Author: James Cherry <[email protected]>
Date:   Thu Feb 15 13:35:13 2024 -0700

    WritePathSpice.hh

    Signed-off-by: James Cherry <[email protected]>

commit 8cd6e2ffc6ad66e831630273b5eacd192259191e
Author: James Cherry <[email protected]>
Date:   Thu Feb 15 10:11:39 2024 -0700

    small

    Signed-off-by: James Cherry <[email protected]>

commit f7f6bfb49f43ddc3e45c294f89c8814d60df5220
Author: James Cherry <[email protected]>
Date:   Thu Feb 15 09:48:09 2024 -0700

    refactor WritePathSpice

    Signed-off-by: James Cherry <[email protected]>

commit f74db730c3e8c67a24d531266510e4376db463d3
Author: James Cherry <[email protected]>
Date:   Wed Feb 14 09:22:01 2024 -0700

    Sta.hh

    Signed-off-by: James Cherry <[email protected]>

commit 051532deef203cae97e32e8af7a2348bfd8912cc
Author: James Cherry <[email protected]>
Date:   Wed Feb 14 08:14:44 2024 -0700

    PowerClass.hh

    Signed-off-by: James Cherry <[email protected]>

commit bfb8357d1093e5d3da14e708acd21fc21ba3b0dd
Author: James Cherry <[email protected]>
Date:   Wed Feb 14 08:08:56 2024 -0700

    doc

    Signed-off-by: James Cherry <[email protected]>

commit 8fe28ec91b234d9d8210019aa46a2e8107aa497a
Author: James Cherry <[email protected]>
Date:   Wed Feb 14 07:32:34 2024 -0700

    ClkSkew use seq instead of set

    Signed-off-by: James Cherry <[email protected]>

commit c4e3a3a0315ab4f6160a707e838423bb734f5363
Author: James Cherry <[email protected]>
Date:   Tue Feb 13 19:26:45 2024 -0700

    report_clock_latency

    Signed-off-by: James Cherry <[email protected]>

commit 51fb6657d9706c7443e1c269cfe63cf080b05d50
Author: James Cherry <[email protected]>
Date:   Tue Feb 13 11:10:11 2024 -0700

    report_clock_latency

    Signed-off-by: James Cherry <[email protected]>

commit e639ee129d13e1c11b34bca0762b8136b18563f3
Author: James Cherry <[email protected]>
Date:   Mon Feb 12 11:19:06 2024 -0700

    ClkSkew use map

    Signed-off-by: James Cherry <[email protected]>

commit e91d3ea8142a73b7b607dfdf53b3fce8e2f16984
Author: James Cherry <[email protected]>
Date:   Mon Feb 12 10:18:27 2024 -0700

    report_clock_skew report format

    Signed-off-by: James Cherry <[email protected]>

commit c650b7ec63b83382ba9cec7d187ffee8a031c2ce
Author: James Cherry <[email protected]>
Date:   Mon Feb 12 09:22:29 2024 -0700

    report_clock_skew include macro clock_tree_path_delay

    Signed-off-by: James Cherry <[email protected]>

commit cf14b230a9944b95ba43ef7c09e553d9014990eb
Author: James Cherry <[email protected]>
Date:   Sun Feb 11 11:03:29 2024 -0700

    clk skew range iter

    Signed-off-by: James Cherry <[email protected]>

commit e7e0342e063ac876d00d03fd1ff0eab1715cfde4
Author: James Cherry <[email protected]>
Date:   Sun Feb 11 08:11:29 2024 -0700

    write_spice sensitize and3

    Signed-off-by: James Cherry <[email protected]>

commit 743ceb676c763ac5bcbf05e630a4da1b507c537d
Author: James Cherry <[email protected]>
Date:   Sat Feb 10 18:07:04 2024 -0700

    write spice

    Signed-off-by: James Cherry <[email protected]>

Signed-off-by: James Cherry <[email protected]>
2024-02-27 10:00:48 -07:00

1292 lines
35 KiB
C++

// OpenSTA, Static Timing Analyzer
// Copyright (c) 2024, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Sim.hh"
#include "StaConfig.hh" // CUDD
#include "Error.hh"
#include "Mutex.hh"
#include "Debug.hh"
#include "Report.hh"
#include "Stats.hh"
#include "FuncExpr.hh"
#include "TimingRole.hh"
#include "TimingArc.hh"
#include "Liberty.hh"
#include "PortDirection.hh"
#include "Sequential.hh"
#include "Network.hh"
#include "Sdc.hh"
#include "Graph.hh"
#if CUDD
// https://davidkebo.com/cudd
#include "cudd.h"
#else
#define Cudd_Init(ignore1, ignore2, ignore3, ignore4, ignore5) nullptr
#define Cudd_Quit(ignore1)
#endif
namespace sta {
static LogicValue
logicNot(LogicValue value);
static const Pin *
findDrvrPin(const Pin *pin,
Network *network);
Sim::Sim(StaState *sta) :
StaState(sta),
observer_(nullptr),
valid_(false),
incremental_(false),
const_func_pins_(network_),
const_func_pins_valid_(false),
invalid_insts_(network_),
invalid_drvr_pins_(network_),
invalid_load_pins_(network_),
instances_with_const_pins_(network_),
instances_to_annotate_(network_),
bdd_(sta)
{
}
Sim::~Sim()
{
delete observer_;
}
#if CUDD
TimingSense
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst)
{
debugPrint(debug_, "sim", 4, "find sense pin %s %s",
network_->pathName(input_pin),
expr->asString());
bool increasing, decreasing;
{
UniqueLock lock(bdd_lock_);
DdNode *bdd = funcBddSim(expr, inst);
DdManager *cudd_mgr = bdd_.cuddMgr();
LibertyPort *input_port = network_->libertyPort(input_pin);
DdNode *input_node = bdd_.ensureNode(input_port);
unsigned int input_index = Cudd_NodeReadIndex(input_node);
increasing = (Cudd_Increasing(cudd_mgr, bdd, input_index)
== Cudd_ReadOne(cudd_mgr));
decreasing = (Cudd_Decreasing(cudd_mgr, bdd, input_index)
== Cudd_ReadOne(cudd_mgr));
Cudd_RecursiveDeref(cudd_mgr, bdd);
bdd_.clearVarMap();
}
TimingSense sense;
if (increasing && decreasing)
sense = TimingSense::none;
else if (increasing)
sense = TimingSense::positive_unate;
else if (decreasing)
sense = TimingSense::negative_unate;
else
sense = TimingSense::non_unate;
debugPrint(debug_, "sim", 4, " %s", timingSenseString(sense));
return sense;
}
LogicValue
Sim::evalExpr(const FuncExpr *expr,
const Instance *inst)
{
UniqueLock lock(bdd_lock_);
DdNode *bdd = funcBddSim(expr, inst);
LogicValue value = LogicValue::unknown;
DdManager *cudd_mgr = bdd_.cuddMgr();
if (bdd == Cudd_ReadLogicZero(cudd_mgr))
value = LogicValue::zero;
else if (bdd == Cudd_ReadOne(cudd_mgr))
value = LogicValue::one;
if (bdd) {
Cudd_RecursiveDeref(bdd_.cuddMgr(), bdd);
bdd_.clearVarMap();
}
return value;
}
// BDD with instance pin values substituted.
DdNode *
Sim::funcBddSim(const FuncExpr *expr,
const Instance *inst)
{
DdNode *bdd = bdd_.funcBdd(expr);
DdManager *cudd_mgr = bdd_.cuddMgr();
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
const Pin *pin = pin_iter->next();
const LibertyPort *port = network_->libertyPort(pin);
DdNode *port_node = bdd_.findNode(port);
if (port_node) {
LogicValue value = logicValue(pin);
int var_index = Cudd_NodeReadIndex(port_node);
//printf("%s %d %c\n", port->name(), var_index, logicValueString(value));
switch (value) {
case LogicValue::zero:
bdd = Cudd_bddCompose(cudd_mgr, bdd, Cudd_ReadLogicZero(cudd_mgr), var_index);
Cudd_Ref(bdd);
break;
case LogicValue::one:
bdd = Cudd_bddCompose(cudd_mgr, bdd, Cudd_ReadOne(cudd_mgr), var_index);
Cudd_Ref(bdd);
break;
default:
break;
}
}
}
return bdd;
}
#else
// No CUDD.
static LogicValue
logicOr(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_or[5][5] =
{{LogicValue::zero, LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::one, LogicValue::one, LogicValue::one, LogicValue::one, LogicValue::one},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown}};
return logic_or[int(value1)][int(value2)];
}
static LogicValue
logicAnd(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_and[5][5] =
{{LogicValue::zero,LogicValue::zero, LogicValue::zero, LogicValue::zero, LogicValue::zero},
{LogicValue::zero,LogicValue::one, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown}};
return logic_and[int(value1)][int(value2)];
}
static LogicValue
logicXor(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_xor[5][5]=
{{LogicValue::zero, LogicValue::one, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::one, LogicValue::zero, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown}};
return logic_xor[int(value1)][int(value2)];
}
static TimingSense
senseNot(TimingSense sense)
{
static TimingSense sense_not[5] = {TimingSense::negative_unate,
TimingSense::positive_unate,
TimingSense::non_unate,
TimingSense::none,
TimingSense::unknown};
return sense_not[int(sense)];
}
static TimingSense
senseAndOr(TimingSense sense1,
TimingSense sense2)
{
static TimingSense sense_and_or[5][5] =
{{TimingSense::positive_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::positive_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::negative_unate,
TimingSense::non_unate, TimingSense::negative_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::unknown},
{TimingSense::positive_unate, TimingSense::negative_unate,
TimingSense::non_unate, TimingSense::none, TimingSense::unknown},
{TimingSense::unknown, TimingSense::unknown,
TimingSense::unknown, TimingSense::non_unate, TimingSense::unknown}};
return sense_and_or[int(sense1)][int(sense2)];
}
static TimingSense
senseXor(TimingSense sense1,
TimingSense sense2)
{
static TimingSense xor_sense[5][5] =
{{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::none, TimingSense::unknown},
{TimingSense::unknown, TimingSense::unknown,
TimingSense::unknown, TimingSense::unknown, TimingSense::unknown}};
return xor_sense[int(sense1)][int(sense2)];
}
TimingSense
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst)
{
TimingSense sense = TimingSense::none;
LogicValue value = LogicValue::unknown;
functionSense(expr, input_pin, inst, sense, value);
return sense;
}
void
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst,
// return values
TimingSense &sense,
LogicValue &value) const
{
switch (expr->op()) {
case FuncExpr::op_port: {
Pin *pin = network_->findPin(inst, expr->port());
if (pin) {
if (pin == input_pin)
sense = TimingSense::positive_unate;
else
sense = TimingSense::none;
value = logicValue(pin);
}
else {
sense = TimingSense::none;
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_not: {
TimingSense sense1;
LogicValue value1;
functionSense(expr->left(), input_pin, inst, sense1, value1);
if (value1 == LogicValue::zero) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::one) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else {
sense = senseNot(sense1);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_or: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if (value1 == LogicValue::one || value2 == LogicValue::one) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::zero) {
sense = sense2;
value = value2;
}
else if (value2 == LogicValue::zero) {
sense = sense1;
value = value1;
}
else {
sense = senseAndOr(sense1, sense2);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_and: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if (value1 == LogicValue::zero || value2 == LogicValue::zero) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else if (value1 == LogicValue::one) {
sense = sense2;
value = value2;
}
else if (value2 == LogicValue::one) {
sense = sense1;
value = value1;
}
else {
sense = senseAndOr(sense1, sense2);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_xor: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if ((value1 == LogicValue::zero && value2 == LogicValue::zero)
|| (value1 == LogicValue::one && value2 == LogicValue::one)) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else if ((value1 == LogicValue::zero && value2 == LogicValue::one)
|| (value1 == LogicValue::one && value2 == LogicValue::zero)) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::zero) {
sense = sense2;
value = value2;
}
else if (value1 == LogicValue::one) {
sense = senseNot(sense2);
value = logicNot(value2);
}
else if (value2 == LogicValue::zero) {
sense = sense1;
value = value1;
}
else if (value2 == LogicValue::one) {
sense = senseNot(sense1);
value = logicNot(value1);
}
else {
sense = senseXor(sense1, sense2);
value = logicXor(value1, value2);
}
break;
}
case FuncExpr::op_one:
sense = TimingSense::none;
value = LogicValue::one;
break;
case FuncExpr::op_zero:
sense = TimingSense::none;
value = LogicValue::zero;
break;
}
}
LogicValue
Sim::evalExpr(const FuncExpr *expr,
const Instance *inst)
{
switch (expr->op()) {
case FuncExpr::op_port: {
LibertyPort *port = expr->port();
if (port) {
Pin *pin = network_->findPin(inst, port->name());
if (pin)
return logicValue(pin);
}
// Internal ports don't have instance pins.
return LogicValue::unknown;
}
case FuncExpr::op_not:
return logicNot(evalExpr(expr->left(), inst));
case FuncExpr::op_or:
return logicOr(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_and:
return logicAnd(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_xor:
return logicXor(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_one:
return LogicValue::one;
case FuncExpr::op_zero:
return LogicValue::zero;
}
// Prevent warnings from lame compilers.
return LogicValue::zero;
}
#endif // CUDD
static LogicValue
logicNot(LogicValue value)
{
static LogicValue logic_not[5] = {LogicValue::one, LogicValue::zero,
LogicValue::unknown, LogicValue::unknown,
LogicValue::unknown};
return logic_not[int(value)];
}
void
Sim::clear()
{
valid_ = false;
incremental_ = false;
const_func_pins_.clear();
const_func_pins_valid_ = false;
instances_with_const_pins_.clear();
instances_to_annotate_.clear();
invalid_insts_.clear();
invalid_drvr_pins_.clear();
invalid_load_pins_.clear();
}
void
Sim::setObserver(SimObserver *observer)
{
delete observer_;
observer_ = observer;
}
void
Sim::ensureConstantsPropagated()
{
if (!valid_) {
Stats stats(debug_, report_);
ensureConstantFuncPins();
instances_to_annotate_.clear();
if (incremental_) {
seedInvalidConstants();
propagateToInvalidLoads();
propagateFromInvalidDrvrsToLoads();
}
else {
clearSimValues();
seedConstants();
}
invalid_insts_.clear();
propagateConstants(false);
annotateGraphEdges();
valid_ = true;
incremental_ = true;
stats.report("Propagate constants");
}
}
void
Sim::findLogicConstants()
{
clear();
ensureConstantFuncPins();
setConstFuncPins();
enqueueConstantPinInputs();
propagateConstants(true);
valid_ = true;
}
void
Sim::seedInvalidConstants()
{
for (const Instance *inst : invalid_insts_)
eval_queue_.push(inst);
}
void
Sim::propagateToInvalidLoads()
{
for (const Pin *load_pin : invalid_load_pins_) {
const Net *net = network_->net(load_pin);
if (net && network_->isGround(net))
setPinValue(load_pin, LogicValue::zero);
else if (net && network_->isPower(net))
setPinValue(load_pin, LogicValue::one);
else {
const Pin *drvr_pin = findDrvrPin(load_pin, network_);
if (drvr_pin)
propagateDrvrToLoad(drvr_pin, load_pin);
}
}
invalid_load_pins_.clear();
}
void
Sim::propagateFromInvalidDrvrsToLoads()
{
for (const Pin *drvr_pin : invalid_drvr_pins_) {
LogicValue value = const_func_pins_.hasKey(drvr_pin)
? pinConstFuncValue(drvr_pin)
: logicValue(drvr_pin);
PinConnectedPinIterator *load_iter=network_->connectedPinIterator(drvr_pin);
while (load_iter->hasNext()) {
const Pin *load_pin = load_iter->next();
if (load_pin != drvr_pin
&& network_->isLoad(load_pin))
setPinValue(load_pin, value);
}
delete load_iter;
}
invalid_drvr_pins_.clear();
}
void
Sim::propagateDrvrToLoad(const Pin *drvr_pin,
const Pin *load_pin)
{
LogicValue value = logicValue(drvr_pin);
setPinValue(load_pin, value);
}
void
Sim::constantsInvalid()
{
valid_ = false;
incremental_ = false;
}
void
Sim::ensureConstantFuncPins()
{
if (!const_func_pins_valid_) {
LeafInstanceIterator *inst_iter = network_->leafInstanceIterator();
while (inst_iter->hasNext()) {
Instance *inst = inst_iter->next();
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
const Pin *pin = pin_iter->next();
recordConstPinFunc(pin);
}
delete pin_iter;
}
delete inst_iter;
const_func_pins_valid_ = true;
}
}
void
Sim::recordConstPinFunc(const Pin *pin)
{
LibertyPort *port = network_->libertyPort(pin);
if (port) {
FuncExpr *expr = port->function();
if (expr
// Tristate outputs do not force the output to be constant.
&& port->tristateEnable() == nullptr
&& (expr->op() == FuncExpr::op_zero
|| expr->op() == FuncExpr::op_one))
const_func_pins_.insert(pin);
}
}
void
Sim::deleteInstanceBefore(const Instance *inst)
{
instances_with_const_pins_.erase(inst);
invalid_insts_.erase(inst);
}
void
Sim::makePinAfter(const Pin *pin)
{
// Incrementally update const_func_pins_.
recordConstPinFunc(pin);
}
void
Sim::deletePinBefore(const Pin *pin)
{
// Incrementally update const_func_pins_.
const_func_pins_.erase(pin);
invalid_load_pins_.erase(pin);
invalid_drvr_pins_.erase(pin);
invalid_insts_.insert(network_->instance(pin));
}
void
Sim::connectPinAfter(const Pin *pin)
{
if (incremental_) {
recordConstPinFunc(pin);
if (network_->isLoad(pin))
invalid_load_pins_.insert(pin);
if (network_->isDriver(pin))
invalid_drvr_pins_.insert(pin);
valid_ = false;
}
}
void
Sim::disconnectPinBefore(const Pin *pin)
{
if (incremental_) {
if (network_->isLoad(pin)) {
invalid_load_pins_.insert(pin);
removePropagatedValue(pin);
}
if (network_->isDriver(pin))
invalid_drvr_pins_.insert(pin);
}
}
void
Sim::pinSetFuncAfter(const Pin *pin)
{
if (incremental_) {
Instance *inst = network_->instance(pin);
if (instances_with_const_pins_.hasKey(inst))
invalid_insts_.insert(inst);
valid_ = false;
}
// Incrementally update const_func_pins_.
const_func_pins_.erase(pin);
recordConstPinFunc(pin);
}
void
Sim::seedConstants()
{
// Propagate constants from inputs tied hi/low in the network.
enqueueConstantPinInputs();
// Propagate set_LogicValue::zero, set_LogicValue::one, set_logic_dc constants.
setConstraintConstPins(sdc_->logicValues());
// Propagate set_case_analysis constants.
setConstraintConstPins(sdc_->caseLogicValues());
// Propagate 0/1 constant functions.
setConstFuncPins();
}
void
Sim::propagateConstants(bool thru_sequentials)
{
while (!eval_queue_.empty()) {
const Instance *inst = eval_queue_.front();
eval_queue_.pop();
evalInstance(inst, thru_sequentials);
}
}
void
Sim::setConstraintConstPins(LogicValueMap &value_map)
{
for (auto pin_value : value_map) {
const Pin *pin = pin_value.first;
LogicValue value = pin_value.second;
debugPrint(debug_, "sim", 2, "case pin %s = %c",
network_->pathName(pin),
logicValueString(value));
if (network_->isHierarchical(pin)) {
// Set the logic value on pins inside the instance of a hierarchical pin.
bool pin_is_output = network_->direction(pin)->isAnyOutput();
PinConnectedPinIterator *pin_iter=network_->connectedPinIterator(pin);
while (pin_iter->hasNext()) {
const Pin *pin1 = pin_iter->next();
if (network_->isLeaf(pin1)
&& network_->direction(pin1)->isAnyInput()
&& ((pin_is_output && !network_->isInside(pin1, pin))
|| (!pin_is_output && network_->isInside(pin1, pin))))
setPinValue(pin1, value);
}
delete pin_iter;
}
else
setPinValue(pin, value);
}
}
// Propagate constants from outputs with constant functions
// (tie high and tie low cell instances).
void
Sim::setConstFuncPins()
{
for (const Pin *pin : const_func_pins_) {
LogicValue value = pinConstFuncValue(pin);
setPinValue(pin, value);
debugPrint(debug_, "sim", 2, "func pin %s = %c",
network_->pathName(pin),
logicValueString(value));
}
}
LogicValue
Sim::pinConstFuncValue(const Pin *pin)
{
LibertyPort *port = network_->libertyPort(pin);
if (port) {
FuncExpr *expr = port->function();
if (expr->op() == FuncExpr::op_zero)
return LogicValue::zero;
else if (expr->op() == FuncExpr::op_one)
return LogicValue::one;
}
return LogicValue::unknown;
}
void
Sim::enqueueConstantPinInputs()
{
ConstantPinIterator *const_iter = network_->constantPinIterator();
while (const_iter->hasNext()) {
LogicValue value;
const Pin *pin;
const_iter->next(pin, value);
debugPrint(debug_, "sim", 2, "network constant pin %s = %c",
network_->pathName(pin),
logicValueString(value));
setPinValue(pin, value);
}
delete const_iter;
}
void
Sim::removePropagatedValue(const Pin *pin)
{
Instance *inst = network_->instance(pin);
if (instances_with_const_pins_.hasKey(inst)) {
invalid_insts_.insert(inst);
valid_ = false;
LogicValue constraint_value;
bool exists;
sdc_->caseLogicValue(pin, constraint_value, exists);
if (!exists) {
sdc_->logicValue(pin, constraint_value, exists);
if (!exists) {
debugPrint(debug_, "sim", 2, "pin %s remove prop constant",
network_->pathName(pin));
Vertex *vertex, *bidirect_drvr_vertex;
graph_->pinVertices(pin, vertex, bidirect_drvr_vertex);
if (vertex)
setSimValue(vertex, LogicValue::unknown);
if (bidirect_drvr_vertex)
setSimValue(bidirect_drvr_vertex, LogicValue::unknown);
}
}
}
}
void
Sim::setPinValue(const Pin *pin,
LogicValue value)
{
LogicValue constraint_value;
bool exists;
sdc_->caseLogicValue(pin, constraint_value, exists);
if (!exists)
sdc_->logicValue(pin, constraint_value, exists);
if (exists
&& value != constraint_value) {
if (value != LogicValue::unknown)
report_->warn(1521, "propagated logic value %c differs from constraint value of %c on pin %s.",
logicValueString(value),
logicValueString(constraint_value),
sdc_network_->pathName(pin));
}
else {
debugPrint(debug_, "sim", 3, "pin %s = %c",
network_->pathName(pin),
logicValueString(value));
Vertex *vertex, *bidirect_drvr_vertex;
graph_->pinVertices(pin, vertex, bidirect_drvr_vertex);
// Set vertex constant flags.
if (vertex)
setSimValue(vertex, value);
if (bidirect_drvr_vertex)
setSimValue(bidirect_drvr_vertex, value);
Instance *inst = network_->instance(pin);
if (logicValueZeroOne(value))
instances_with_const_pins_.insert(inst);
instances_to_annotate_.insert(inst);
if (network_->isLeaf(inst)
&& network_->direction(pin)->isAnyInput()) {
if (eval_queue_.empty()
|| (eval_queue_.back() != inst))
eval_queue_.push(inst);
}
else if (network_->isDriver(pin)) {
// Enqueue instances with input pins connected to net.
PinConnectedPinIterator *pin_iter=network_->connectedPinIterator(pin);
while (pin_iter->hasNext()) {
const Pin *pin1 = pin_iter->next();
if (pin1 != pin
&& network_->isLoad(pin1))
setPinValue(pin1, value);
}
delete pin_iter;
}
}
}
void
Sim::evalInstance(const Instance *inst,
bool thru_sequentials)
{
debugPrint(debug_, "sim", 2, "eval %s", network_->pathName(inst));
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
Pin *pin = pin_iter->next();
LibertyPort *port = network_->libertyPort(pin);
if (port) {
PortDirection *dir = port->direction();
if (dir->isAnyOutput()) {
LogicValue value = LogicValue::unknown;
FuncExpr *expr = port->function();
LibertyCell *cell = port->libertyCell();
if (expr) {
FuncExpr *tri_en_expr = port->tristateEnable();
if (tri_en_expr) {
if (evalExpr(tri_en_expr, inst) == LogicValue::one) {
value = evalExpr(expr, inst);
debugPrint(debug_, "sim", 2, " %s tri_en=1 %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
}
else {
LibertyPort *expr_port = expr->port();
Sequential *sequential = (thru_sequentials && expr_port)
? cell->outputPortSequential(expr_port)
: nullptr;
if (sequential) {
value = evalExpr(sequential->data(), inst);
if (expr_port == sequential->outputInv())
value = logicNot(value);
debugPrint(debug_, "sim", 2, " %s seq %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
else {
value = evalExpr(expr, inst);
debugPrint(debug_, "sim", 2, " %s %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
}
}
else if (port->isClockGateOut()) {
value = clockGateOutValue(inst);
debugPrint(debug_, "sim", 2, " %s gated_clk = %c",
port->name(),
logicValueString(value));
}
if (value != logicValue(pin))
setPinValue(pin, value);
}
}
}
delete pin_iter;
}
LogicValue
Sim::clockGateOutValue(const Instance *inst)
{
LibertyCell *cell = network_->libertyCell(inst);
LibertyCellPortIterator port_iter(cell);
while (port_iter.hasNext()) {
LibertyPort *port = port_iter.next();
if (port->isClockGateClock()
|| port->isClockGateEnable()) {
Pin *gclk_pin = network_->findPin(inst, port);
if (gclk_pin) {
Vertex *gclk_vertex = graph_->pinLoadVertex(gclk_pin);
if (gclk_vertex->simValue() == LogicValue::zero)
return LogicValue::zero;
}
}
}
return LogicValue::unknown;
}
void
Sim::setSimValue(Vertex *vertex,
LogicValue value)
{
if (value != vertex->simValue()) {
vertex->setSimValue(value);
if (observer_)
observer_->valueChangeAfter(vertex);
}
}
TimingSense
Sim::functionSense(const Instance *inst,
const Pin *from_pin,
const Pin *to_pin)
{
if (logicZeroOne(from_pin))
return TimingSense::none;
else {
LibertyPort *from_port = network_->libertyPort(from_pin);
LibertyPort *to_port = network_->libertyPort(to_pin);
if (to_port) {
const FuncExpr *func = to_port->function();
if (func) {
PortDirection *to_dir = to_port->direction();
if (to_dir->isAnyTristate()) {
FuncExpr *tri_func = to_port->tristateEnable();
if (tri_func) {
if (func->hasPort(from_port)) {
// from_pin is an input to the to_pin function.
LogicValue tri_enable = evalExpr(tri_func, inst);
if (tri_enable == LogicValue::zero)
// Tristate is disabled.
return TimingSense::none;
else
return functionSense(func, from_pin, inst);
}
}
else {
// Missing tristate enable function.
if (func->hasPort(from_port))
// from_pin is an input to the to_pin function.
return functionSense(func, from_pin, inst);
}
}
else {
if (func->hasPort(from_port))
// from_pin is an input to the to_pin function.
return functionSense(func, from_pin, inst);
}
}
}
return TimingSense::unknown;
}
}
LogicValue
Sim::logicValue(const Pin *pin) const
{
Vertex *vertex = graph_->pinLoadVertex(pin);
if (vertex)
return vertex->simValue();
else {
if (network_->isHierarchical(pin)) {
const Pin *drvr_pin = findDrvrPin(pin, network_);
if (drvr_pin)
return logicValue(drvr_pin);
}
return LogicValue::unknown;
}
}
static const Pin *
findDrvrPin(const Pin *pin,
Network *network)
{
PinSet *drvrs = network->drivers(pin);
if (drvrs) {
PinSet::Iterator drvr_iter(drvrs);
if (drvr_iter.hasNext())
return drvr_iter.next();
}
return nullptr;
}
bool
logicValueZeroOne(LogicValue value)
{
return value == LogicValue::zero || value == LogicValue::one;
}
bool
Sim::logicZeroOne(const Pin *pin) const
{
return logicValueZeroOne(logicValue(pin));
}
bool
Sim::logicZeroOne(const Vertex *vertex) const
{
return logicValueZeroOne(vertex->simValue());
}
void
Sim::clearSimValues()
{
for (const Instance *inst : instances_with_const_pins_) {
// Clear sim values on all pins before evaling functions.
clearInstSimValues(inst);
annotateVertexEdges(inst, false);
}
instances_with_const_pins_.clear();
}
void
Sim::clearInstSimValues(const Instance *inst)
{
debugPrint(debug_, "sim", 4, "clear %s",
network_->pathName(inst));
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
Pin *pin = pin_iter->next();
Vertex *vertex, *bidirect_drvr_vertex;
graph_->pinVertices(pin, vertex, bidirect_drvr_vertex);
if (vertex)
setSimValue(vertex, LogicValue::unknown);
if (bidirect_drvr_vertex)
setSimValue(bidirect_drvr_vertex, LogicValue::unknown);
}
delete pin_iter;
}
// Annotate graph edges disabled by constant values.
void
Sim::annotateGraphEdges()
{
for (const Instance *inst : instances_to_annotate_)
annotateVertexEdges(inst, true);
}
void
Sim::annotateVertexEdges(const Instance *inst,
bool annotate)
{
debugPrint(debug_, "sim", 4, "annotate %s %s",
network_->pathName(inst),
annotate ? "true" : "false");
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
Pin *pin = pin_iter->next();
Vertex *vertex = graph_->pinDrvrVertex(pin);
if (vertex)
annotateVertexEdges(inst, pin, vertex, annotate);
}
delete pin_iter;
}
void
Sim::annotateVertexEdges(const Instance *inst,
const Pin *pin,
Vertex *vertex,
bool annotate)
{
bool fanin_disables_changed = false;
VertexInEdgeIterator edge_iter(vertex, graph_);
while (edge_iter.hasNext()) {
Edge *edge = edge_iter.next();
if (!edge->role()->isWire()) {
Vertex *from_vertex = edge->from(graph_);
Pin *from_pin = from_vertex->pin();
TimingSense sense = TimingSense::unknown;
bool is_disabled_cond = false;
if (annotate) {
// Set timing sense on edges in instances that have constant pins.
if (logicZeroOne(from_vertex))
sense = TimingSense::none;
else
sense = functionSense(inst, from_pin, pin);
if (sense != TimingSense::none)
// Disable conditional timing edges based on constant pins.
is_disabled_cond = isCondDisabled(edge, inst, from_pin,
pin, network_,sim_)
// Disable mode conditional timing
// edges based on constant pins.
|| isModeDisabled(edge,inst,network_,sim_)
|| isTestDisabled(inst, from_pin, pin,
network_, sim_);
}
bool disables_changed = false;
if (sense != edge->simTimingSense()) {
edge->setSimTimingSense(sense);
disables_changed = true;
fanin_disables_changed = true;
}
if (is_disabled_cond != edge->isDisabledCond()) {
edge->setIsDisabledCond(is_disabled_cond);
disables_changed = true;
fanin_disables_changed = true;
}
if (observer_ && disables_changed)
observer_->fanoutEdgesChangeAfter(from_vertex);
}
}
if (observer_ && fanin_disables_changed)
observer_->faninEdgesChangeAfter(vertex);
}
bool
isCondDisabled(Edge *edge,
const Instance *inst,
const Pin *from_pin,
const Pin *to_pin,
const Network *network,
Sim *sim)
{
bool is_disabled;
FuncExpr *disable_cond;
isCondDisabled(edge, inst, from_pin, to_pin, network, sim,
is_disabled, disable_cond);
return is_disabled;
}
void
isCondDisabled(Edge *edge,
const Instance *inst,
const Pin *from_pin,
const Pin *to_pin,
const Network *network,
Sim *sim,
bool &is_disabled,
FuncExpr *&disable_cond)
{
TimingArcSet *arc_set = edge->timingArcSet();
FuncExpr *cond = arc_set->cond();
if (cond) {
LogicValue cond_value = sim->evalExpr(cond, inst);
disable_cond = cond;
is_disabled = (cond_value == LogicValue::zero);
}
else {
// Unconditional "default" arc set is disabled if another
// conditional arc from/to the same pins is enabled (condition
// evals to logic one).
LibertyCell *cell = network->libertyCell(inst);
LibertyPort *from_port = network->libertyPort(from_pin);
LibertyPort *to_port = network->libertyPort(to_pin);
is_disabled = false;
for (TimingArcSet *cond_set : cell->timingArcSets(from_port, to_port)) {
FuncExpr *cond = cond_set->cond();
if (cond && sim->evalExpr(cond, inst) == LogicValue::one) {
disable_cond = cond;
is_disabled = true;
break;
}
}
}
}
bool
isModeDisabled(Edge *edge,
const Instance *inst,
const Network *network,
Sim *sim)
{
bool is_disabled;
FuncExpr *disable_cond;
isModeDisabled(edge, inst, network, sim,
is_disabled, disable_cond);
return is_disabled;
}
void
isModeDisabled(Edge *edge,
const Instance *inst,
const Network *network,
Sim *sim,
bool &is_disabled,
FuncExpr *&disable_cond)
{
// Default values.
is_disabled = false;
disable_cond = 0;
TimingArcSet *arc_set = edge->timingArcSet();
const char *mode_name = arc_set->modeName();
const char *mode_value = arc_set->modeValue();
if (mode_name && mode_value) {
LibertyCell *cell = network->libertyCell(inst);
ModeDef *mode_def = cell->findModeDef(mode_name);
if (mode_def) {
ModeValueDef *value_def = mode_def->findValueDef(mode_value);
if (value_def) {
FuncExpr *cond = value_def->cond();
if (cond) {
LogicValue cond_value = sim->evalExpr(cond, inst);
if (cond_value == LogicValue::zero) {
// For a mode value to be disabled by having a value of
// logic zero one mode value must logic one.
for (auto name_mode : *mode_def->values()) {
ModeValueDef *value_def1 = name_mode.second;
if (value_def1) {
FuncExpr *cond1 = value_def1->cond();
if (cond1) {
LogicValue cond_value1 = sim->evalExpr(cond1, inst);
if (cond_value1 == LogicValue::one) {
disable_cond = cond;
is_disabled = true;
break;
}
}
}
}
}
}
}
}
}
}
bool
isTestDisabled(const Instance *inst,
const Pin *from_pin,
const Pin *to_pin,
const Network *network,
const Sim *sim)
{
bool is_disabled;
Pin *scan_enable;
isTestDisabled(inst, from_pin, to_pin, network, sim,
is_disabled, scan_enable);
return is_disabled;
}
void
isTestDisabled(const Instance *inst,
const Pin *from_pin,
const Pin *to_pin,
const Network *network,
const Sim *sim,
bool &is_disabled,
Pin *&scan_enable)
{
is_disabled = false;
LibertyCell *cell = network->libertyCell(inst);
if (cell) {
TestCell *test = cell->testCell();
if (test) {
LibertyPort *from_port = network->libertyPort(from_pin);
LibertyPort *to_port = network->libertyPort(to_pin);
LibertyPort *data_in_port = test->dataIn();
LibertyPort *scan_in_port = test->scanIn();
if (from_port == data_in_port
|| to_port == data_in_port
|| from_port == scan_in_port
|| to_port == scan_in_port) {
LibertyPort *scan_enable_port = test->scanEnable();
if (scan_enable_port) {
scan_enable = network->findPin(inst, scan_enable_port);
if (scan_enable) {
LogicValue scan_enable_value = sim->logicValue(scan_enable);
is_disabled = ((scan_enable_value == LogicValue::zero
&& (from_port == scan_in_port
|| to_port == scan_in_port))
|| (scan_enable_value == LogicValue::one
&& (from_port == data_in_port
|| to_port == data_in_port)));
}
}
}
}
}
}
} // namespace