1/* Copyright (c) 2012 Massachusetts Institute of Technology
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#include "model/std_cells/NAND2.h"
23
24#include <cmath>
25
26#include "model/PortInfo.h"
27#include "model/TransitionInfo.h"
28#include "model/EventInfo.h"
29#include "model/std_cells/StdCellLib.h"
30#include "model/std_cells/CellMacros.h"
31#include "model/timing_graph/ElectricalNet.h"
32#include "model/timing_graph/ElectricalDriver.h"
33#include "model/timing_graph/ElectricalLoad.h"
34#include "model/timing_graph/ElectricalDelay.h"
35
36namespace DSENT
37{
38    using std::ceil;
39    using std::max;
40
41    NAND2::NAND2(const String& instance_name_, const TechModel* tech_model_)
42        : StdCell(instance_name_, tech_model_)
43    {
44        initProperties();
45    }
46
47    NAND2::~NAND2()
48    {}
49
50    void NAND2::initProperties()
51    {
52        return;
53    }
54
55    void NAND2::constructModel()
56    {
57        // All constructModel should do is create Area/NDDPower/Energy Results as
58        // well as instantiate any sub-instances using only the hard parameters
59
60        createInputPort("A");
61        createInputPort("B");
62        createOutputPort("Y");
63
64        createLoad("A_Cap");
65        createLoad("B_Cap");
66        createDelay("A_to_Y_delay");
67        createDelay("B_to_Y_delay");
68        createDriver("Y_Ron", true);
69
70        ElectricalLoad* a_cap = getLoad("A_Cap");
71        ElectricalLoad* b_cap = getLoad("A_Cap");
72        ElectricalDelay* a_to_y_delay = getDelay("A_to_Y_delay");
73        ElectricalDelay* b_to_y_delay = getDelay("B_to_Y_delay");
74        ElectricalDriver* y_ron = getDriver("Y_Ron");
75
76        getNet("A")->addDownstreamNode(a_cap);
77        getNet("B")->addDownstreamNode(b_cap);
78        a_cap->addDownstreamNode(a_to_y_delay);
79        b_cap->addDownstreamNode(b_to_y_delay);
80        a_to_y_delay->addDownstreamNode(y_ron);
81        b_to_y_delay->addDownstreamNode(y_ron);
82        y_ron->addDownstreamNode(getNet("Y"));
83
84        // Create Area result
85        // Create NDD Power result
86        createElectricalAtomicResults();
87        // Create NAND Event Energy Result
88        createElectricalEventAtomicResult("NAND2");
89
90        getEventInfo("Idle")->setStaticTransitionInfos();
91
92        return;
93    }
94
95    void NAND2::updateModel()
96    {
97        // All updateModel should do is calculate numbers for the Area/NDDPower/Energy
98        // Results as anything else that needs to be done using either soft or hard parameters
99
100        // Get parameters
101        double drive_strength = getDrivingStrength();
102        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
103
104        // Standard cell cache string
105        String cell_name = "NAND2_X" + (String) drive_strength;
106
107        // Get timing parameters
108        getLoad("A_Cap")->setLoadCap(cache->get(cell_name + "->Cap->A"));
109        getLoad("B_Cap")->setLoadCap(cache->get(cell_name + "->Cap->B"));
110        getDelay("A_to_Y_delay")->setDelay(cache->get(cell_name + "->Delay->A_to_Y"));
111        getDelay("B_to_Y_delay")->setDelay(cache->get(cell_name + "->Delay->B_to_Y"));
112        getDriver("Y_Ron")->setOutputRes(cache->get(cell_name + "->DriveRes->Y"));
113
114        // Set the cell area
115        getAreaResult("Active")->setValue(cache->get(cell_name + "->Area->Active"));
116        getAreaResult("Metal1Wire")->setValue(cache->get(cell_name + "->Area->Active"));
117
118        return;
119    }
120
121    void NAND2::useModel()
122    {
123        // Get parameters
124        double drive_strength = getDrivingStrength();
125        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
126
127        // Standard cell cache string
128        String cell_name = "NAND2_X" + (String) drive_strength;
129
130        // Propagate the transition info and get the 0->1 transtion count
131        propagateTransitionInfo();
132        double P_A = getInputPort("A")->getTransitionInfo().getProbability1();
133        double P_B = getInputPort("B")->getTransitionInfo().getProbability1();
134        double Y_num_trans_01 = getOutputPort("Y")->getTransitionInfo().getNumberTransitions01();
135
136        // Calculate leakage
137        double leakage = 0;
138        leakage += cache->get(cell_name + "->Leakage->!A!B") * (1 - P_A) * (1 - P_B);
139        leakage += cache->get(cell_name + "->Leakage->!AB") * (1 - P_A) * P_B;
140        leakage += cache->get(cell_name + "->Leakage->A!B") * P_A * (1 - P_B);
141        leakage += cache->get(cell_name + "->Leakage->AB") * P_A * P_B;
142        getNddPowerResult("Leakage")->setValue(leakage);
143
144        // Get capacitances
145        double y_cap = cache->get(cell_name + "->Cap->Y");
146        double y_load_cap = getNet("Y")->getTotalDownstreamCap();
147
148        // Get VDD
149        double vdd = getTechModel()->get("Vdd");
150
151        // Calculate NAND2Event energy
152        double energy_per_trans_01 = (y_cap + y_load_cap) * vdd * vdd;
153        getEventResult("NAND2")->setValue(energy_per_trans_01 * Y_num_trans_01);
154
155        return;
156    }
157
158    void NAND2::propagateTransitionInfo()
159    {
160        // Get input signal transition info
161        const TransitionInfo& trans_A = getInputPort("A")->getTransitionInfo();
162        const TransitionInfo& trans_B = getInputPort("B")->getTransitionInfo();
163
164        double max_freq_mult = max(trans_A.getFrequencyMultiplier(), trans_B.getFrequencyMultiplier());
165        const TransitionInfo& scaled_trans_A = trans_A.scaleFrequencyMultiplier(max_freq_mult);
166        const TransitionInfo& scaled_trans_B = trans_B.scaleFrequencyMultiplier(max_freq_mult);
167
168        double A_prob_00 = scaled_trans_A.getNumberTransitions00() / max_freq_mult;
169        double A_prob_01 = scaled_trans_A.getNumberTransitions01() / max_freq_mult;
170        double A_prob_10 = A_prob_01;
171        double A_prob_11 = scaled_trans_A.getNumberTransitions11() / max_freq_mult;
172        double B_prob_00 = scaled_trans_B.getNumberTransitions00() / max_freq_mult;
173        double B_prob_01 = scaled_trans_B.getNumberTransitions01() / max_freq_mult;
174        double B_prob_10 = B_prob_01;
175        double B_prob_11 = scaled_trans_B.getNumberTransitions11() / max_freq_mult;
176
177        // Set output transition info
178        double Y_prob_00 = A_prob_11 * B_prob_11;
179        double Y_prob_01 = A_prob_11 * B_prob_10 +
180                        A_prob_10 * (B_prob_11 + B_prob_10);
181        double Y_prob_11 = A_prob_00 +
182                        A_prob_01 * (B_prob_00 + B_prob_10) +
183                        A_prob_10 * (B_prob_00 + B_prob_01) +
184                        A_prob_11 * B_prob_00;
185
186        // Check that probabilities add up to 1.0 with some finite tolerance
187        ASSERT(LibUtil::Math::isEqual((Y_prob_00 + Y_prob_01 + Y_prob_01 + Y_prob_11), 1.0),
188            "[Error] " + getInstanceName() +  "Output transition probabilities must add up to 1 (" +
189            (String) Y_prob_00 + ", " + (String) Y_prob_01 + ", " + (String) Y_prob_11 + ")!");
190
191        // Turn probability of transitions per cycle into number of transitions per time unit
192        TransitionInfo trans_Y(Y_prob_00 * max_freq_mult, Y_prob_01 * max_freq_mult, Y_prob_11 * max_freq_mult);
193        getOutputPort("Y")->setTransitionInfo(trans_Y);
194        return;
195    }
196
197    void NAND2::cacheStdCell(StdCellLib* cell_lib_, double drive_strength_)
198    {
199        // Standard cell cache string
200        String cell_name = "NAND2_X" + (String) drive_strength_;
201
202        Log::printLine("=== " + cell_name + " ===");
203
204        // Get parameters
205        double gate_pitch = cell_lib_->getTechModel()->get("Gate->PitchContacted");
206        Map<double>* cache = cell_lib_->getStdCellCache();
207
208        // Now actually build the full standard cell model
209        // Create the two input ports
210        createInputPort("A");
211        createInputPort("B");
212        createOutputPort("Y");
213
214        // Adds macros
215        CellMacros::addNand2(this, "NAND", true, true, true, "A", "B", "Y");
216        CellMacros::updateNand2(this, "NAND", drive_strength_);
217
218        // Cache area result
219        double area = gate_pitch * getTotalHeight() * (1 + getGenProperties()->get("NAND_GatePitches").toDouble());
220        cache->set(cell_name + "->Area->Active", area);
221        Log::printLine(cell_name + "->Area->Active=" + (String) area);
222
223        // --------------------------------------------------------------------
224        // Leakage Model Calculation
225        // --------------------------------------------------------------------
226        double leakage_00 = getGenProperties()->get("NAND_LeakagePower_00").toDouble();
227        double leakage_01 = getGenProperties()->get("NAND_LeakagePower_01").toDouble();
228        double leakage_10 = getGenProperties()->get("NAND_LeakagePower_10").toDouble();
229        double leakage_11 = getGenProperties()->get("NAND_LeakagePower_11").toDouble();
230        cache->set(cell_name + "->Leakage->!A!B", leakage_00);
231        cache->set(cell_name + "->Leakage->!AB", leakage_01);
232        cache->set(cell_name + "->Leakage->A!B", leakage_10);
233        cache->set(cell_name + "->Leakage->AB", leakage_11);
234        Log::printLine(cell_name + "->Leakage->!A!B=" + (String) leakage_00);
235        Log::printLine(cell_name + "->Leakage->!AB=" + (String) leakage_01);
236        Log::printLine(cell_name + "->Leakage->A!B=" + (String) leakage_10);
237        Log::printLine(cell_name + "->Leakage->AB=" + (String) leakage_11);
238        // --------------------------------------------------------------------
239
240        // Cache event energy results
241        /*
242        double event_a_flip = getGenProperties()->get("NAND_A1_Flip").toDouble();
243        double event_b_flip = getGenProperties()->get("NAND_A2_Flip").toDouble();
244        double event_y_flip = getGenProperties()->get("NAND_ZN_Flip").toDouble();
245
246        cache->set(cell_name + "->Event_A_Flip", event_a_flip);
247        cache->set(cell_name + "->Event_B_Flip", event_b_flip);
248        cache->set(cell_name + "->Event_Y_Flip", event_y_flip);
249        Log::printLine(cell_name + "->Event_A_Flip=" + (String) event_a_flip);
250        Log::printLine(cell_name + "->Event_B_Flip=" + (String) event_b_flip);
251        Log::printLine(cell_name + "->Event_Y_Flip=" + (String) event_y_flip);
252        */
253        // --------------------------------------------------------------------
254        // Get Node Capacitances
255        // --------------------------------------------------------------------
256        double a_cap = getNet("A")->getTotalDownstreamCap();
257        double b_cap = getNet("B")->getTotalDownstreamCap();
258        double y_cap = getNet("Y")->getTotalDownstreamCap();
259
260        cache->set(cell_name + "->Cap->A", a_cap);
261        cache->set(cell_name + "->Cap->B", b_cap);
262        cache->set(cell_name + "->Cap->Y", y_cap);
263        Log::printLine(cell_name + "->Cap->A=" + (String) a_cap);
264        Log::printLine(cell_name + "->Cap->B=" + (String) b_cap);
265        Log::printLine(cell_name + "->Cap->Y=" + (String) y_cap);
266        // --------------------------------------------------------------------
267
268        // --------------------------------------------------------------------
269        // Build Internal Delay Model
270        // --------------------------------------------------------------------
271        double y_ron = getDriver("NAND_RonZN")->getOutputRes();
272        double a_to_y_delay = getDriver("NAND_RonZN")->calculateDelay();
273        double b_to_y_delay = getDriver("NAND_RonZN")->calculateDelay();
274
275        cache->set(cell_name + "->DriveRes->Y", y_ron);
276        cache->set(cell_name + "->Delay->A_to_Y", a_to_y_delay);
277        cache->set(cell_name + "->Delay->B_to_Y", b_to_y_delay);
278        Log::printLine(cell_name + "->DriveRes->Y=" + (String) y_ron);
279        Log::printLine(cell_name + "->Delay->A_to_Y=" + (String) a_to_y_delay);
280        Log::printLine(cell_name + "->Delay->B_to_Y=" + (String) b_to_y_delay);
281        // --------------------------------------------------------------------
282
283        return;
284
285    }
286
287} // namespace DSENT
288
289