CellMacros.cc revision 10447
110447Snilay@cs.wisc.edu#include "model/std_cells/CellMacros.h"
210447Snilay@cs.wisc.edu
310447Snilay@cs.wisc.edu#include <cmath>
410447Snilay@cs.wisc.edu#include <vector>
510447Snilay@cs.wisc.edu
610447Snilay@cs.wisc.edu#include "model/std_cells/StdCell.h"
710447Snilay@cs.wisc.edu#include "model/timing_graph/ElectricalNet.h"
810447Snilay@cs.wisc.edu#include "model/timing_graph/ElectricalDriver.h"
910447Snilay@cs.wisc.edu#include "model/timing_graph/ElectricalLoad.h"
1010447Snilay@cs.wisc.edu
1110447Snilay@cs.wisc.edunamespace DSENT
1210447Snilay@cs.wisc.edu{
1310447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
1410447Snilay@cs.wisc.edu    // NOR2 Macro (TODO: Generalize to N-input macro once leakage calc is done)
1510447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
1610447Snilay@cs.wisc.edu    void CellMacros::addNor2(StdCell* cell_, const String& name_,
1710447Snilay@cs.wisc.edu        bool sizable_, bool a1_to_zn_path_, bool a2_to_zn_path_,
1810447Snilay@cs.wisc.edu        const String& a1_net_, const String& a2_net_, const String& zn_net_)
1910447Snilay@cs.wisc.edu    {
2010447Snilay@cs.wisc.edu        //Create electrical timing model for the nand
2110447Snilay@cs.wisc.edu        // Construct loads and drivers
2210447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA1");
2310447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA2");
2410447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CdZN");
2510447Snilay@cs.wisc.edu        cell_->createDriver(name_ + "_RonZN", sizable_);
2610447Snilay@cs.wisc.edu
2710447Snilay@cs.wisc.edu        //Get references to loads and drivers
2810447Snilay@cs.wisc.edu        ElectricalLoad* gate_a1_load = cell_->getLoad(name_ + "_CgA1");
2910447Snilay@cs.wisc.edu        ElectricalLoad* gate_a2_load = cell_->getLoad(name_ + "_CgA2");
3010447Snilay@cs.wisc.edu        ElectricalLoad* drain_load = cell_->getLoad(name_ + "_CdZN");
3110447Snilay@cs.wisc.edu        ElectricalDriver* zn_drive = cell_->getDriver(name_ + "_RonZN");
3210447Snilay@cs.wisc.edu        ElectricalNet* a1_net = cell_->getNet(a1_net_);
3310447Snilay@cs.wisc.edu        ElectricalNet* a2_net = cell_->getNet(a2_net_);
3410447Snilay@cs.wisc.edu        ElectricalNet* zn_net = cell_->getNet(zn_net_);
3510447Snilay@cs.wisc.edu
3610447Snilay@cs.wisc.edu        //Add loads and drivers to the specified nets
3710447Snilay@cs.wisc.edu        a1_net->addDownstreamNode(gate_a1_load);
3810447Snilay@cs.wisc.edu        a2_net->addDownstreamNode(gate_a2_load);
3910447Snilay@cs.wisc.edu        zn_net->addDownstreamNode(drain_load);
4010447Snilay@cs.wisc.edu        if (a1_to_zn_path_) gate_a1_load->addDownstreamNode(zn_drive);
4110447Snilay@cs.wisc.edu        if (a2_to_zn_path_) gate_a2_load->addDownstreamNode(zn_drive);
4210447Snilay@cs.wisc.edu        zn_drive->addDownstreamNode(zn_net);
4310447Snilay@cs.wisc.edu
4410447Snilay@cs.wisc.edu        return;
4510447Snilay@cs.wisc.edu    }
4610447Snilay@cs.wisc.edu
4710447Snilay@cs.wisc.edu    void CellMacros::updateNor2(StdCell* cell_, const String& name_, double normalized_size_)
4810447Snilay@cs.wisc.edu    {
4910447Snilay@cs.wisc.edu        ASSERT(normalized_size_ >= 0.0, "[Error] " + cell_->getInstanceName() +
5010447Snilay@cs.wisc.edu            " -> Cannot update a macro with a negative normalized size!");
5110447Snilay@cs.wisc.edu
5210447Snilay@cs.wisc.edu        //Grab pointer to tech model
5310447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
5410447Snilay@cs.wisc.edu
5510447Snilay@cs.wisc.edu        // Get technology parameters
5610447Snilay@cs.wisc.edu        double vdd = tech->get("Vdd");
5710447Snilay@cs.wisc.edu        double gate_cap = tech->get("Gate->CapPerWidth");
5810447Snilay@cs.wisc.edu        double drain_cap = tech->get("Drain->CapPerWidth");
5910447Snilay@cs.wisc.edu        double nmos_eff_res = tech->get("Nmos->EffResWidth");
6010447Snilay@cs.wisc.edu        double pmos_eff_res = tech->get("Pmos->EffResWidth");
6110447Snilay@cs.wisc.edu        double pmos_eff_res_stack_ratio = tech->get("Pmos->EffResStackRatio");
6210447Snilay@cs.wisc.edu        double gate_pitch_contacted = tech->get("Gate->PitchContacted");
6310447Snilay@cs.wisc.edu        double metal1_wire_min_width = tech->get("Wire->Metal1->MinWidth");
6410447Snilay@cs.wisc.edu
6510447Snilay@cs.wisc.edu        //Calculate number of folds and gate pitches needed
6610447Snilay@cs.wisc.edu        unsigned int folds = (normalized_size_ < 1.0) ? 1 : (unsigned int)ceil(normalized_size_);
6710447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_GatePitches", 2 * folds);
6810447Snilay@cs.wisc.edu
6910447Snilay@cs.wisc.edu        //Calculate widths, making sure they are above the minimum width
7010447Snilay@cs.wisc.edu        double nmos_width = std::max(calculateNmosWidth(cell_, 1, 2, 1) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
7110447Snilay@cs.wisc.edu        double pmos_width = std::max(calculatePmosWidth(cell_, 1, 2, 2) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
7210447Snilay@cs.wisc.edu
7310447Snilay@cs.wisc.edu        //Calculate leakage power for each given input state
7410447Snilay@cs.wisc.edu        double leakage_power_00 = vdd * folds * 2 * tech->calculateNmosLeakageCurrent(1, nmos_width, 0x0);
7510447Snilay@cs.wisc.edu        double leakage_power_01 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x1);
7610447Snilay@cs.wisc.edu        double leakage_power_10 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x2);
7710447Snilay@cs.wisc.edu        double leakage_power_11 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x3);
7810447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_00", leakage_power_00);
7910447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_01", leakage_power_01);
8010447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_10", leakage_power_10);
8110447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_11", leakage_power_11);
8210447Snilay@cs.wisc.edu
8310447Snilay@cs.wisc.edu        //Calculate R_on and capacitances
8410447Snilay@cs.wisc.edu        double pmos_stack2_balance = 1.0 + pmos_eff_res_stack_ratio;
8510447Snilay@cs.wisc.edu        double c_g = (nmos_width + pmos_width) * gate_cap * folds;
8610447Snilay@cs.wisc.edu        double c_d = (2 * pmos_width + 2 * nmos_width) * drain_cap * folds;
8710447Snilay@cs.wisc.edu        double r_on = (nmos_eff_res / nmos_width + pmos_stack2_balance * pmos_eff_res / pmos_width) / (folds * 2.0);
8810447Snilay@cs.wisc.edu
8910447Snilay@cs.wisc.edu        // Estimate the wire cap and add them all at the output
9010447Snilay@cs.wisc.edu        double cell_height = cell_->getTotalHeight();
9110447Snilay@cs.wisc.edu        double wire_width = metal1_wire_min_width;
9210447Snilay@cs.wisc.edu        double wire_spacing = gate_pitch_contacted - metal1_wire_min_width;
9310447Snilay@cs.wisc.edu        double wire_length = 2.0 * folds * cell_height;
9410447Snilay@cs.wisc.edu        double wire_cap = tech->calculateWireCapacitance("Metal1", wire_width, wire_spacing, wire_length);
9510447Snilay@cs.wisc.edu
9610447Snilay@cs.wisc.edu        // Construct equivalent load and drive strength
9710447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA1")->setLoadCap(c_g);
9810447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA2")->setLoadCap(c_g);
9910447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CdZN")->setLoadCap(c_d + wire_cap);
10010447Snilay@cs.wisc.edu        cell_->getDriver(name_ + "_RonZN")->setOutputRes(r_on);
10110447Snilay@cs.wisc.edu
10210447Snilay@cs.wisc.edu        // Calculate flip energies
10310447Snilay@cs.wisc.edu        double zn_flip_energy = 0.5 * (c_d + wire_cap) * vdd * vdd;
10410447Snilay@cs.wisc.edu        double a1_flip_energy = 0.5 * c_g * vdd * vdd;
10510447Snilay@cs.wisc.edu        double a2_flip_energy = 0.5 * c_g * vdd * vdd;
10610447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_ZN_Flip", zn_flip_energy);
10710447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A1_Flip", a1_flip_energy);
10810447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A2_Flip", a2_flip_energy);
10910447Snilay@cs.wisc.edu    }
11010447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
11110447Snilay@cs.wisc.edu
11210447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
11310447Snilay@cs.wisc.edu    // NAND2 Macro (TODO: Generalize to N-input macro once leakage calc is done)
11410447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
11510447Snilay@cs.wisc.edu    //Adds a NAND2 to the standard cell, normalized to some size
11610447Snilay@cs.wisc.edu    void CellMacros::addNand2(StdCell* cell_, const String& name_,
11710447Snilay@cs.wisc.edu        bool sizable_, bool a1_to_zn_path_, bool a2_to_zn_path_,
11810447Snilay@cs.wisc.edu        const String& a1_net_, const String& a2_net_, const String& zn_net_)
11910447Snilay@cs.wisc.edu    {
12010447Snilay@cs.wisc.edu        //Create electrical timing model for the nor
12110447Snilay@cs.wisc.edu        // Construct loads and drivers
12210447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA1");
12310447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA2");
12410447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CdZN");
12510447Snilay@cs.wisc.edu        cell_->createDriver(name_ + "_RonZN", sizable_);
12610447Snilay@cs.wisc.edu
12710447Snilay@cs.wisc.edu        //Get references to loads and drivers
12810447Snilay@cs.wisc.edu        ElectricalLoad* gate_a1_load = cell_->getLoad(name_ + "_CgA1");
12910447Snilay@cs.wisc.edu        ElectricalLoad* gate_a2_load = cell_->getLoad(name_ + "_CgA2");
13010447Snilay@cs.wisc.edu        ElectricalLoad* drain_load = cell_->getLoad(name_ + "_CdZN");
13110447Snilay@cs.wisc.edu        ElectricalDriver* zn_drive = cell_->getDriver(name_ + "_RonZN");
13210447Snilay@cs.wisc.edu        ElectricalNet* a1_net = cell_->getNet(a1_net_);
13310447Snilay@cs.wisc.edu        ElectricalNet* a2_net = cell_->getNet(a2_net_);
13410447Snilay@cs.wisc.edu        ElectricalNet* zn_net = cell_->getNet(zn_net_);
13510447Snilay@cs.wisc.edu
13610447Snilay@cs.wisc.edu        a1_net->addDownstreamNode(gate_a1_load);
13710447Snilay@cs.wisc.edu        a2_net->addDownstreamNode(gate_a2_load);
13810447Snilay@cs.wisc.edu        zn_net->addDownstreamNode(drain_load);
13910447Snilay@cs.wisc.edu        if (a1_to_zn_path_) gate_a1_load->addDownstreamNode(zn_drive);
14010447Snilay@cs.wisc.edu        if (a2_to_zn_path_) gate_a2_load->addDownstreamNode(zn_drive);
14110447Snilay@cs.wisc.edu        zn_drive->addDownstreamNode(zn_net);
14210447Snilay@cs.wisc.edu
14310447Snilay@cs.wisc.edu        return;
14410447Snilay@cs.wisc.edu    }
14510447Snilay@cs.wisc.edu
14610447Snilay@cs.wisc.edu    //Updates a NAND2 to to the standard cell, normalized to some size
14710447Snilay@cs.wisc.edu    void CellMacros::updateNand2(StdCell* cell_, const String& name_, double normalized_size_)
14810447Snilay@cs.wisc.edu    {
14910447Snilay@cs.wisc.edu        ASSERT(normalized_size_ >= 0.0, "[Error] " + cell_->getInstanceName() +
15010447Snilay@cs.wisc.edu            " -> Cannot update a macro with a negative normalized size!");
15110447Snilay@cs.wisc.edu
15210447Snilay@cs.wisc.edu        //Grab pointer to tech model
15310447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
15410447Snilay@cs.wisc.edu
15510447Snilay@cs.wisc.edu        // Get technology parameters
15610447Snilay@cs.wisc.edu        double vdd = tech->get("Vdd");
15710447Snilay@cs.wisc.edu        double gate_cap = tech->get("Gate->CapPerWidth");
15810447Snilay@cs.wisc.edu        double drain_cap = tech->get("Drain->CapPerWidth");
15910447Snilay@cs.wisc.edu        double nmos_eff_res = tech->get("Nmos->EffResWidth");
16010447Snilay@cs.wisc.edu        double pmos_eff_res = tech->get("Pmos->EffResWidth");
16110447Snilay@cs.wisc.edu        double nmos_eff_res_stack_ratio = tech->get("Nmos->EffResStackRatio");
16210447Snilay@cs.wisc.edu        double gate_pitch_contacted = tech->get("Gate->PitchContacted");
16310447Snilay@cs.wisc.edu        double metal1_wire_min_width = tech->get("Wire->Metal1->MinWidth");
16410447Snilay@cs.wisc.edu
16510447Snilay@cs.wisc.edu        //Calculate number of folds needed
16610447Snilay@cs.wisc.edu        unsigned int folds = (normalized_size_ < 1.0) ? 1 : (unsigned int)ceil(normalized_size_);
16710447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_GatePitches", 2 * folds);
16810447Snilay@cs.wisc.edu
16910447Snilay@cs.wisc.edu        //Calculate widths, making sure they are above the minimum width
17010447Snilay@cs.wisc.edu        double nmos_width = std::max(calculateNmosWidth(cell_, 2, 1, 2) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
17110447Snilay@cs.wisc.edu        double pmos_width = std::max(calculatePmosWidth(cell_, 2, 1, 1) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
17210447Snilay@cs.wisc.edu
17310447Snilay@cs.wisc.edu        // Leakage power calculation
17410447Snilay@cs.wisc.edu        double leakage_power_00 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x0);
17510447Snilay@cs.wisc.edu        double leakage_power_01 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x1);
17610447Snilay@cs.wisc.edu        double leakage_power_10 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x2);
17710447Snilay@cs.wisc.edu        double leakage_power_11 = vdd * folds * 2 * tech->calculatePmosLeakageCurrent(1, pmos_width, ~0x3);
17810447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_00", leakage_power_00);
17910447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_01", leakage_power_01);
18010447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_10", leakage_power_10);
18110447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_11", leakage_power_11);
18210447Snilay@cs.wisc.edu
18310447Snilay@cs.wisc.edu        // Get input parameters
18410447Snilay@cs.wisc.edu        double nmos_stack2_balance = 1.0 + nmos_eff_res_stack_ratio;
18510447Snilay@cs.wisc.edu
18610447Snilay@cs.wisc.edu        //Calculate caps
18710447Snilay@cs.wisc.edu        double c_g = (nmos_width + pmos_width) * gate_cap * folds;
18810447Snilay@cs.wisc.edu        double c_d = (2 * pmos_width + 2 * nmos_width) * drain_cap * folds;
18910447Snilay@cs.wisc.edu        double r_on = (nmos_stack2_balance * nmos_eff_res / nmos_width + pmos_eff_res / pmos_width) / (folds * 2.0);
19010447Snilay@cs.wisc.edu
19110447Snilay@cs.wisc.edu        // Estimate the wire cap and add them all at the output
19210447Snilay@cs.wisc.edu        double cell_height = cell_->getTotalHeight();
19310447Snilay@cs.wisc.edu        double wire_width = metal1_wire_min_width;
19410447Snilay@cs.wisc.edu        double wire_spacing = gate_pitch_contacted - metal1_wire_min_width;
19510447Snilay@cs.wisc.edu        double wire_length = 2.0 * folds * cell_height;
19610447Snilay@cs.wisc.edu        double wire_cap = tech->calculateWireCapacitance("Metal1", wire_width, wire_spacing, wire_length);
19710447Snilay@cs.wisc.edu
19810447Snilay@cs.wisc.edu        // Construct equivalent load and drive strength
19910447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA1")->setLoadCap(c_g);
20010447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA2")->setLoadCap(c_g);
20110447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CdZN")->setLoadCap(c_d + wire_cap);
20210447Snilay@cs.wisc.edu        cell_->getDriver(name_ + "_RonZN")->setOutputRes(r_on);
20310447Snilay@cs.wisc.edu
20410447Snilay@cs.wisc.edu        // Calculate flip energies
20510447Snilay@cs.wisc.edu        double zn_flip_energy = 0.5 * (c_d + wire_cap) * vdd * vdd;
20610447Snilay@cs.wisc.edu        double a1_flip_energy = 0.5 * c_g * vdd * vdd;
20710447Snilay@cs.wisc.edu        double a2_flip_energy = 0.5 * c_g * vdd * vdd;
20810447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_ZN_Flip", zn_flip_energy);
20910447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A1_Flip", a1_flip_energy);
21010447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A2_Flip", a2_flip_energy);
21110447Snilay@cs.wisc.edu    }
21210447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
21310447Snilay@cs.wisc.edu
21410447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
21510447Snilay@cs.wisc.edu    // INV Macro
21610447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
21710447Snilay@cs.wisc.edu    //Adds an inverter to the model, normalized to some size
21810447Snilay@cs.wisc.edu    void CellMacros::addInverter(StdCell* cell_, const String& name_,
21910447Snilay@cs.wisc.edu        bool sizable_, bool a_to_zn_path_,
22010447Snilay@cs.wisc.edu        const String& a_net_, const String& zn_net_)
22110447Snilay@cs.wisc.edu    {
22210447Snilay@cs.wisc.edu        //Create electrical timing model for the inverter
22310447Snilay@cs.wisc.edu        // Construct loads and drivers
22410447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA");
22510447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CdZN");
22610447Snilay@cs.wisc.edu        cell_->createDriver(name_ + "_RonZN", sizable_);
22710447Snilay@cs.wisc.edu
22810447Snilay@cs.wisc.edu        //Get references to loads and drivers
22910447Snilay@cs.wisc.edu        ElectricalLoad* gate_load = cell_->getLoad(name_ + "_CgA");
23010447Snilay@cs.wisc.edu        ElectricalLoad* drain_load = cell_->getLoad(name_ + "_CdZN");
23110447Snilay@cs.wisc.edu        ElectricalDriver* out_drive = cell_->getDriver(name_ + "_RonZN");
23210447Snilay@cs.wisc.edu        ElectricalNet* a_net = cell_->getNet(a_net_);
23310447Snilay@cs.wisc.edu        ElectricalNet* zn_net = cell_->getNet(zn_net_);
23410447Snilay@cs.wisc.edu
23510447Snilay@cs.wisc.edu        // Setup connectivity of loads and drivers
23610447Snilay@cs.wisc.edu        a_net->addDownstreamNode(gate_load);
23710447Snilay@cs.wisc.edu        if (a_to_zn_path_) gate_load->addDownstreamNode(out_drive);
23810447Snilay@cs.wisc.edu        zn_net->addDownstreamNode(drain_load);
23910447Snilay@cs.wisc.edu        out_drive->addDownstreamNode(zn_net);
24010447Snilay@cs.wisc.edu
24110447Snilay@cs.wisc.edu        return;
24210447Snilay@cs.wisc.edu    }
24310447Snilay@cs.wisc.edu
24410447Snilay@cs.wisc.edu    //Updates the numbers of an inverter for some normalized size
24510447Snilay@cs.wisc.edu    void CellMacros::updateInverter(StdCell* cell_, const String& name_, double normalized_size_)
24610447Snilay@cs.wisc.edu    {
24710447Snilay@cs.wisc.edu        ASSERT(normalized_size_ >= 0.0, "[Error] " + cell_->getInstanceName() +
24810447Snilay@cs.wisc.edu            " -> Cannot update a macro with a negative normalized size!");
24910447Snilay@cs.wisc.edu
25010447Snilay@cs.wisc.edu        //Grab pointer to tech model
25110447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
25210447Snilay@cs.wisc.edu
25310447Snilay@cs.wisc.edu        //Get values from technology library
25410447Snilay@cs.wisc.edu        double vdd = tech->get("Vdd");
25510447Snilay@cs.wisc.edu        double gate_cap = tech->get("Gate->CapPerWidth");
25610447Snilay@cs.wisc.edu        double drain_cap = tech->get("Drain->CapPerWidth");
25710447Snilay@cs.wisc.edu        double nmos_eff_res = tech->get("Nmos->EffResWidth");
25810447Snilay@cs.wisc.edu        double pmos_eff_res = tech->get("Pmos->EffResWidth");
25910447Snilay@cs.wisc.edu        double gate_pitch_contacted = tech->get("Gate->PitchContacted");
26010447Snilay@cs.wisc.edu        double metal1_wire_min_width = tech->get("Wire->Metal1->MinWidth");
26110447Snilay@cs.wisc.edu
26210447Snilay@cs.wisc.edu        //Calculate number of folds needed
26310447Snilay@cs.wisc.edu        unsigned int folds = (normalized_size_ < 1.0) ? 1 : (unsigned int)ceil(normalized_size_);
26410447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_GatePitches", folds);
26510447Snilay@cs.wisc.edu
26610447Snilay@cs.wisc.edu        //Calculate widths, making sure they are above the minimum width
26710447Snilay@cs.wisc.edu        double nmos_width = std::max(calculateNmosWidth(cell_, 1, 1, 1) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
26810447Snilay@cs.wisc.edu        double pmos_width = std::max(calculatePmosWidth(cell_, 1, 1, 1) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
26910447Snilay@cs.wisc.edu
27010447Snilay@cs.wisc.edu        //Calculate leakage power for each given input state
27110447Snilay@cs.wisc.edu        double leakage_power_0 = vdd * folds * tech->calculateNmosLeakageCurrent(1, nmos_width, 0x0);
27210447Snilay@cs.wisc.edu        double leakage_power_1 = vdd * folds * tech->calculatePmosLeakageCurrent(1, pmos_width, ~0x1);
27310447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_0", leakage_power_0);
27410447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_1", leakage_power_1);
27510447Snilay@cs.wisc.edu
27610447Snilay@cs.wisc.edu        //Calculate caps
27710447Snilay@cs.wisc.edu        double c_g = (nmos_width + pmos_width) * gate_cap * folds;
27810447Snilay@cs.wisc.edu        double c_d = (pmos_width + nmos_width) * drain_cap * folds;
27910447Snilay@cs.wisc.edu        double r_on = (nmos_eff_res / nmos_width + pmos_eff_res / pmos_width) / (folds * 2.0);
28010447Snilay@cs.wisc.edu
28110447Snilay@cs.wisc.edu        // Estimate the wire cap and add them all at the output
28210447Snilay@cs.wisc.edu        double cell_height = cell_->getTotalHeight();
28310447Snilay@cs.wisc.edu        double wire_width = metal1_wire_min_width;
28410447Snilay@cs.wisc.edu        double wire_spacing = gate_pitch_contacted - metal1_wire_min_width;
28510447Snilay@cs.wisc.edu        double wire_length = folds * cell_height;
28610447Snilay@cs.wisc.edu        double wire_cap = tech->calculateWireCapacitance("Metal1", wire_width, wire_spacing, wire_length);
28710447Snilay@cs.wisc.edu
28810447Snilay@cs.wisc.edu        // Construct equivalent load and drive strength
28910447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA")->setLoadCap(c_g);
29010447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CdZN")->setLoadCap(c_d + wire_cap);
29110447Snilay@cs.wisc.edu        cell_->getDriver(name_ + "_RonZN")->setOutputRes(r_on);
29210447Snilay@cs.wisc.edu
29310447Snilay@cs.wisc.edu        // Calculate flip energy (output flip)
29410447Snilay@cs.wisc.edu        // Calculate flip energies
29510447Snilay@cs.wisc.edu        double zn_flip_energy = 0.5 * (c_d + wire_cap) * vdd * vdd;
29610447Snilay@cs.wisc.edu        double a_flip_energy = 0.5 * c_g * vdd * vdd;
29710447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_ZN_Flip", zn_flip_energy);
29810447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A_Flip", a_flip_energy);
29910447Snilay@cs.wisc.edu
30010447Snilay@cs.wisc.edu        return;
30110447Snilay@cs.wisc.edu    }
30210447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
30310447Snilay@cs.wisc.edu
30410447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
30510447Snilay@cs.wisc.edu    // INVZ Macro
30610447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
30710447Snilay@cs.wisc.edu    //Adds a tristated inverter to the model, normalized to some size
30810447Snilay@cs.wisc.edu    void CellMacros::addTristate(StdCell* cell_, const String& name_,
30910447Snilay@cs.wisc.edu        bool sizable_, bool a_to_zn_path_, bool oe_to_zn_path_, bool oen_to_zn_path_,
31010447Snilay@cs.wisc.edu        const String& a_net_, const String& oe_net_, const String& oen_net_, const String& zn_net_)
31110447Snilay@cs.wisc.edu    {
31210447Snilay@cs.wisc.edu        // Construct loads and drivers
31310447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgA");
31410447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgOE");
31510447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CgOEN");
31610447Snilay@cs.wisc.edu        cell_->createLoad(name_ + "_CdZN");
31710447Snilay@cs.wisc.edu        cell_->createDriver(name_ + "_RonZN", sizable_);
31810447Snilay@cs.wisc.edu
31910447Snilay@cs.wisc.edu        // Get references to loads, nets and drivers
32010447Snilay@cs.wisc.edu        ElectricalLoad* gate_a_load = cell_->getLoad(name_ + "_CgA");
32110447Snilay@cs.wisc.edu        ElectricalLoad* gate_oe_load = cell_->getLoad(name_ + "_CgOE");
32210447Snilay@cs.wisc.edu        ElectricalLoad* gate_oen_load = cell_->getLoad(name_ + "_CgOEN");
32310447Snilay@cs.wisc.edu        ElectricalLoad* drain_load = cell_->getLoad(name_ + "_CdZN");
32410447Snilay@cs.wisc.edu        ElectricalDriver* out_drive = cell_->getDriver(name_ + "_RonZN");
32510447Snilay@cs.wisc.edu        ElectricalNet* a_net = cell_->getNet(a_net_);
32610447Snilay@cs.wisc.edu        ElectricalNet* oe_net = cell_->getNet(oe_net_);
32710447Snilay@cs.wisc.edu        ElectricalNet* oen_net = cell_->getNet(oen_net_);
32810447Snilay@cs.wisc.edu        ElectricalNet* zn_net = cell_->getNet(zn_net_);
32910447Snilay@cs.wisc.edu
33010447Snilay@cs.wisc.edu        // Setup connectivity of loads and drivers
33110447Snilay@cs.wisc.edu        a_net->addDownstreamNode(gate_a_load);
33210447Snilay@cs.wisc.edu        oe_net->addDownstreamNode(gate_oe_load);
33310447Snilay@cs.wisc.edu        oen_net->addDownstreamNode(gate_oen_load);
33410447Snilay@cs.wisc.edu        if (a_to_zn_path_) gate_a_load->addDownstreamNode(out_drive);
33510447Snilay@cs.wisc.edu        if (oe_to_zn_path_) gate_oe_load->addDownstreamNode(out_drive);
33610447Snilay@cs.wisc.edu        if (oen_to_zn_path_) gate_oen_load->addDownstreamNode(out_drive);
33710447Snilay@cs.wisc.edu        zn_net->addDownstreamNode(drain_load);
33810447Snilay@cs.wisc.edu        out_drive->addDownstreamNode(zn_net);
33910447Snilay@cs.wisc.edu
34010447Snilay@cs.wisc.edu        return;
34110447Snilay@cs.wisc.edu    }
34210447Snilay@cs.wisc.edu
34310447Snilay@cs.wisc.edu    //Updates the numbers of an inverter for some normalized size
34410447Snilay@cs.wisc.edu    void CellMacros::updateTristate(StdCell* cell_, const String& name_, double normalized_size_)
34510447Snilay@cs.wisc.edu    {
34610447Snilay@cs.wisc.edu        ASSERT(normalized_size_ >= 0.0, "[Error] " + cell_->getInstanceName() +
34710447Snilay@cs.wisc.edu            " -> Cannot update a macro with a negative normalized size!");
34810447Snilay@cs.wisc.edu
34910447Snilay@cs.wisc.edu        //Grab pointer to tech model
35010447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
35110447Snilay@cs.wisc.edu
35210447Snilay@cs.wisc.edu        //Get values from technology library
35310447Snilay@cs.wisc.edu        double vdd = tech->get("Vdd");
35410447Snilay@cs.wisc.edu        double gate_cap = tech->get("Gate->CapPerWidth");
35510447Snilay@cs.wisc.edu        double drain_cap = tech->get("Drain->CapPerWidth");
35610447Snilay@cs.wisc.edu        double nmos_eff_res = tech->get("Nmos->EffResWidth");
35710447Snilay@cs.wisc.edu        double pmos_eff_res = tech->get("Pmos->EffResWidth");
35810447Snilay@cs.wisc.edu        double pmos_eff_res_stack_ratio = tech->get("Pmos->EffResStackRatio");
35910447Snilay@cs.wisc.edu        double nmos_eff_res_stack_ratio = tech->get("Nmos->EffResStackRatio");
36010447Snilay@cs.wisc.edu        double gate_pitch_contacted = tech->get("Gate->PitchContacted");
36110447Snilay@cs.wisc.edu        double metal1_wire_min_width = tech->get("Wire->Metal1->MinWidth");
36210447Snilay@cs.wisc.edu
36310447Snilay@cs.wisc.edu        //Calculate number of folds and gate pitches needed
36410447Snilay@cs.wisc.edu        unsigned int folds = (normalized_size_ < 1.0) ? 1 : (unsigned int)ceil(normalized_size_);
36510447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_GatePitches", 2 * folds);
36610447Snilay@cs.wisc.edu
36710447Snilay@cs.wisc.edu        //Calculate widths, making sure they are above the minimum width
36810447Snilay@cs.wisc.edu        double nmos_width = std::max(calculateNmosWidth(cell_, 2, 2, 2) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
36910447Snilay@cs.wisc.edu        double pmos_width = std::max(calculatePmosWidth(cell_, 2, 2, 2) * normalized_size_ / folds, (double) tech->get("Gate->MinWidth"));
37010447Snilay@cs.wisc.edu
37110447Snilay@cs.wisc.edu        //Calculate leakage power for each given input state
37210447Snilay@cs.wisc.edu        //if output_enable = 0, then it is possible that the PMOS may leak (if output = 0),
37310447Snilay@cs.wisc.edu        //or the NMOS will leak (if output = 1)
37410447Snilay@cs.wisc.edu
37510447Snilay@cs.wisc.edu        //OE OEN A _ ZN
37610447Snilay@cs.wisc.edu        double leakage_power_010_0 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x2);
37710447Snilay@cs.wisc.edu        double leakage_power_010_1 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x0);
37810447Snilay@cs.wisc.edu        double leakage_power_011_0 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x3);
37910447Snilay@cs.wisc.edu        double leakage_power_011_1 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x1);
38010447Snilay@cs.wisc.edu        double leakage_power_100_1 = vdd * folds * tech->calculateNmosLeakageCurrent(2, nmos_width, 0x2);
38110447Snilay@cs.wisc.edu        double leakage_power_101_0 = vdd * folds * tech->calculatePmosLeakageCurrent(2, pmos_width, ~0x1);
38210447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_010_0", leakage_power_010_0);
38310447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_010_1", leakage_power_010_1);
38410447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_011_0", leakage_power_011_0);
38510447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_011_1", leakage_power_011_1);
38610447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_100_1", leakage_power_100_1);
38710447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_LeakagePower_101_0", leakage_power_101_0);
38810447Snilay@cs.wisc.edu
38910447Snilay@cs.wisc.edu        //Caculate stack balance
39010447Snilay@cs.wisc.edu        double pmos_stack2_balance = 1.0 + pmos_eff_res_stack_ratio;
39110447Snilay@cs.wisc.edu        double nmos_stack2_balance = 1.0 + nmos_eff_res_stack_ratio;
39210447Snilay@cs.wisc.edu
39310447Snilay@cs.wisc.edu        //Calculate caps
39410447Snilay@cs.wisc.edu        double c_g_a = (nmos_width + pmos_width) * gate_cap * folds;
39510447Snilay@cs.wisc.edu        double c_g_oe = nmos_width * gate_cap * folds;
39610447Snilay@cs.wisc.edu        double c_g_oen = pmos_width * gate_cap * folds;
39710447Snilay@cs.wisc.edu        double c_d = (2 * pmos_width + 2 * nmos_width) * drain_cap * folds;
39810447Snilay@cs.wisc.edu        double r_on = (nmos_stack2_balance * nmos_eff_res / nmos_width + pmos_stack2_balance * pmos_eff_res / pmos_width) / (folds * 2.0);
39910447Snilay@cs.wisc.edu
40010447Snilay@cs.wisc.edu        // Estimate the wire cap and add them all at the output
40110447Snilay@cs.wisc.edu        double cell_height = cell_->getTotalHeight();
40210447Snilay@cs.wisc.edu        double wire_width = metal1_wire_min_width;
40310447Snilay@cs.wisc.edu        double wire_spacing = gate_pitch_contacted - metal1_wire_min_width;
40410447Snilay@cs.wisc.edu        double wire_length = 2.0 * folds * cell_height;
40510447Snilay@cs.wisc.edu        double wire_cap = tech->calculateWireCapacitance("Metal1", wire_width, wire_spacing, wire_length);
40610447Snilay@cs.wisc.edu
40710447Snilay@cs.wisc.edu        // Construct equivalent load and drive strength
40810447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgA")->setLoadCap(c_g_a);
40910447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgOE")->setLoadCap(c_g_oe);
41010447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CgOEN")->setLoadCap(c_g_oen);
41110447Snilay@cs.wisc.edu        cell_->getLoad(name_ + "_CdZN")->setLoadCap(c_d + wire_cap);
41210447Snilay@cs.wisc.edu        cell_->getDriver(name_ + "_RonZN")->setOutputRes(r_on);
41310447Snilay@cs.wisc.edu
41410447Snilay@cs.wisc.edu        // Calculate flip energy (output flip)
41510447Snilay@cs.wisc.edu        double zn_flip_energy = 0.5 * (c_d + wire_cap) * vdd * vdd;
41610447Snilay@cs.wisc.edu        double a_flip_energy = 0.5 * c_g_a * vdd * vdd;
41710447Snilay@cs.wisc.edu        double oe_flip_energy = 0.5 * c_g_oe * vdd * vdd;
41810447Snilay@cs.wisc.edu        double oen_flip_energy = 0.5 * c_g_oen * vdd * vdd;
41910447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_ZN_Flip", zn_flip_energy);
42010447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_A_Flip", a_flip_energy);
42110447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_OE_Flip", oe_flip_energy);
42210447Snilay@cs.wisc.edu        cell_->getGenProperties()->set(name_ + "_OEN_Flip", oen_flip_energy);
42310447Snilay@cs.wisc.edu        return;
42410447Snilay@cs.wisc.edu    }
42510447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
42610447Snilay@cs.wisc.edu
42710447Snilay@cs.wisc.edu
42810447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
42910447Snilay@cs.wisc.edu    // Helper Functions
43010447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
43110447Snilay@cs.wisc.edu    //Returns the width of NMOS transistors, given the NMOS and PMOS stacking
43210447Snilay@cs.wisc.edu    double CellMacros::calculateNmosWidth(const StdCell* cell_, unsigned int max_stacked_nmos_, unsigned int max_stacked_pmos_, unsigned int current_stacked_nmos_)
43310447Snilay@cs.wisc.edu    {
43410447Snilay@cs.wisc.edu        //Grab pointer to tech model
43510447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
43610447Snilay@cs.wisc.edu
43710447Snilay@cs.wisc.edu        double nmos_eff_res_stack_ratio = tech->get("Nmos->EffResStackRatio");
43810447Snilay@cs.wisc.edu        double pmos_eff_res_stack_ratio = tech->get("Pmos->EffResStackRatio");
43910447Snilay@cs.wisc.edu
44010447Snilay@cs.wisc.edu        double nmos_stack_balance = 1.0 + nmos_eff_res_stack_ratio * (double) (max_stacked_nmos_ - 1);
44110447Snilay@cs.wisc.edu        double pmos_stack_balance = 1.0 + pmos_eff_res_stack_ratio * (double) (max_stacked_pmos_ - 1);
44210447Snilay@cs.wisc.edu        double current_nmos_stack_balance = 1.0 + nmos_eff_res_stack_ratio * (double) (current_stacked_nmos_ - 1);
44310447Snilay@cs.wisc.edu
44410447Snilay@cs.wisc.edu        double pn_ratio = cell_->getPToNRatio();
44510447Snilay@cs.wisc.edu        double active_height = cell_->getActiveHeight();
44610447Snilay@cs.wisc.edu
44710447Snilay@cs.wisc.edu        //Calculate the width of the current device
44810447Snilay@cs.wisc.edu        double nmos_width = active_height * current_nmos_stack_balance / (nmos_stack_balance + pn_ratio * pmos_stack_balance);
44910447Snilay@cs.wisc.edu
45010447Snilay@cs.wisc.edu        return nmos_width;
45110447Snilay@cs.wisc.edu    }
45210447Snilay@cs.wisc.edu
45310447Snilay@cs.wisc.edu    //Returns the width of PMOS transistors, given the NMOS and PMOS stacking
45410447Snilay@cs.wisc.edu    double CellMacros::calculatePmosWidth(const StdCell* cell_, unsigned int max_stacked_nmos_, unsigned int max_stacked_pmos_, unsigned int current_stacked_pmos_)
45510447Snilay@cs.wisc.edu    {
45610447Snilay@cs.wisc.edu        //Grab pointer to tech model
45710447Snilay@cs.wisc.edu        const TechModel* tech = cell_->getTechModel();
45810447Snilay@cs.wisc.edu
45910447Snilay@cs.wisc.edu        double nmos_eff_res_stack_ratio = tech->get("Nmos->EffResStackRatio");
46010447Snilay@cs.wisc.edu        double pmos_eff_res_stack_ratio = tech->get("Pmos->EffResStackRatio");
46110447Snilay@cs.wisc.edu
46210447Snilay@cs.wisc.edu        double nmos_stack_balance = 1.0 + nmos_eff_res_stack_ratio * (double) (max_stacked_nmos_ - 1);
46310447Snilay@cs.wisc.edu        double pmos_stack_balance = 1.0 + pmos_eff_res_stack_ratio * (double) (max_stacked_pmos_ - 1);
46410447Snilay@cs.wisc.edu        double current_pmos_stack_balance = 1.0 + pmos_eff_res_stack_ratio * (double) (current_stacked_pmos_ - 1);
46510447Snilay@cs.wisc.edu
46610447Snilay@cs.wisc.edu        double pn_ratio = cell_->getPToNRatio();
46710447Snilay@cs.wisc.edu        double active_height = cell_->getActiveHeight();
46810447Snilay@cs.wisc.edu
46910447Snilay@cs.wisc.edu        //Calculate the width of the current device
47010447Snilay@cs.wisc.edu        double pmos_width = active_height * current_pmos_stack_balance * pn_ratio / (nmos_stack_balance + pn_ratio * pmos_stack_balance);
47110447Snilay@cs.wisc.edu
47210447Snilay@cs.wisc.edu        return pmos_width;
47310447Snilay@cs.wisc.edu    }
47410447Snilay@cs.wisc.edu    //-------------------------------------------------------------------------
47510447Snilay@cs.wisc.edu
47610447Snilay@cs.wisc.edu} // namespace DSENT
47710447Snilay@cs.wisc.edu
478