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/AND2.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::max;
39
40    AND2::AND2(const String& instance_name_, const TechModel* tech_model_)
41        : StdCell(instance_name_, tech_model_)
42    {
43        initProperties();
44    }
45
46    AND2::~AND2()
47    {}
48
49    void AND2::initProperties()
50    {
51        return;
52    }
53
54    void AND2::constructModel()
55    {
56        // All constructModel should do is create Area/NDDPower/Energy Results as
57        // well as instantiate any sub-instances using only the hard parameters
58
59        createInputPort("A");
60        createInputPort("B");
61        createOutputPort("Y");
62
63        createLoad("A_Cap");
64        createLoad("B_Cap");
65        createDelay("A_to_Y_delay");
66        createDelay("B_to_Y_delay");
67        createDriver("Y_Ron", true);
68
69        ElectricalLoad* a_cap = getLoad("A_Cap");
70        ElectricalLoad* b_cap = getLoad("B_Cap");
71        ElectricalDelay* a_to_y_delay = getDelay("A_to_Y_delay");
72        ElectricalDelay* b_to_y_delay = getDelay("B_to_Y_delay");
73        ElectricalDriver* y_ron = getDriver("Y_Ron");
74
75        getNet("A")->addDownstreamNode(a_cap);
76        getNet("B")->addDownstreamNode(b_cap);
77        a_cap->addDownstreamNode(a_to_y_delay);
78        b_cap->addDownstreamNode(b_to_y_delay);
79        a_to_y_delay->addDownstreamNode(y_ron);
80        b_to_y_delay->addDownstreamNode(y_ron);
81        y_ron->addDownstreamNode(getNet("Y"));
82
83        // Create Area result
84        // Create NDD Power result
85        createElectricalAtomicResults();
86        getEventInfo("Idle")->setStaticTransitionInfos();
87        // Create AND Event Energy Result
88        createElectricalEventAtomicResult("AND2");
89
90        return;
91    }
92
93    void AND2::updateModel()
94    {
95        // All updateModel should do is calculate numbers for the Area/NDDPower/Energy
96        // Results as anything else that needs to be done using either soft or hard parameters
97
98        // Get parameters
99        double drive_strength = getDrivingStrength();
100        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
101
102        // Standard cell cache string
103        String cell_name = "AND2_X" + (String) drive_strength;
104
105        // Get timing parameters
106        getLoad("A_Cap")->setLoadCap(cache->get(cell_name + "->Cap->A"));
107        getLoad("B_Cap")->setLoadCap(cache->get(cell_name + "->Cap->B"));
108        getDelay("A_to_Y_delay")->setDelay(cache->get(cell_name + "->Delay->A_to_Y"));
109        getDelay("B_to_Y_delay")->setDelay(cache->get(cell_name + "->Delay->B_to_Y"));
110        getDriver("Y_Ron")->setOutputRes(cache->get(cell_name + "->DriveRes->Y"));
111
112        // Set the cell area
113        getAreaResult("Active")->setValue(cache->get(cell_name + "->ActiveArea"));
114        getAreaResult("Metal1Wire")->setValue(cache->get(cell_name + "->ActiveArea"));
115
116        return;
117    }
118
119    void AND2::evaluateModel()
120    {
121        return;
122    }
123
124    void AND2::useModel()
125    {
126        // Get parameters
127        double drive_strength = getDrivingStrength();
128        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
129
130        // Standard cell cache string
131        String cell_name = "AND2_X" + (String) drive_strength;
132
133        // Propagate the transition info and get the 0->1 transtion count
134        propagateTransitionInfo();
135        double P_A = getInputPort("A")->getTransitionInfo().getProbability1();
136        double P_B = getInputPort("B")->getTransitionInfo().getProbability1();
137        double Y_num_trans_01 = getOutputPort("Y")->getTransitionInfo().getNumberTransitions01();
138
139        // Calculate leakage
140        double leakage = 0;
141        leakage += cache->get(cell_name + "->Leakage->!A!B") * (1 - P_A) * (1 - P_B);
142        leakage += cache->get(cell_name + "->Leakage->!AB") * (1 - P_A) * P_B;
143        leakage += cache->get(cell_name + "->Leakage->A!B") * P_A * (1 - P_B);
144        leakage += cache->get(cell_name + "->Leakage->AB") * P_A * P_B;
145        getNddPowerResult("Leakage")->setValue(leakage);
146
147        // Get VDD
148        double vdd = getTechModel()->get("Vdd");
149
150        // Get capacitances
151        double y_b_cap = cache->get(cell_name + "->Cap->Y_b");
152        double y_cap = cache->get(cell_name + "->Cap->Y");
153        double y_load_cap = getNet("Y")->getTotalDownstreamCap();
154
155        // Calculate AND2Event energy
156        double energy_per_trans_01 = (y_b_cap + y_cap + y_load_cap) * vdd * vdd;
157        getEventResult("AND2")->setValue(energy_per_trans_01 * Y_num_trans_01);
158
159        return;
160    }
161
162    void AND2::propagateTransitionInfo()
163    {
164        // Get input signal transition info
165        const TransitionInfo& trans_A = getInputPort("A")->getTransitionInfo();
166        const TransitionInfo& trans_B = getInputPort("B")->getTransitionInfo();
167
168        double max_freq_mult = max(trans_A.getFrequencyMultiplier(), trans_B.getFrequencyMultiplier());
169        const TransitionInfo& scaled_trans_A = trans_A.scaleFrequencyMultiplier(max_freq_mult);
170        const TransitionInfo& scaled_trans_B = trans_B.scaleFrequencyMultiplier(max_freq_mult);
171
172        double A_prob_00 = scaled_trans_A.getNumberTransitions00() / max_freq_mult;
173        double A_prob_01 = scaled_trans_A.getNumberTransitions01() / max_freq_mult;
174        double A_prob_10 = A_prob_01;
175        double A_prob_11 = scaled_trans_A.getNumberTransitions11() / max_freq_mult;
176        double B_prob_00 = scaled_trans_B.getNumberTransitions00() / max_freq_mult;
177        double B_prob_01 = scaled_trans_B.getNumberTransitions01() / max_freq_mult;
178        double B_prob_10 = B_prob_01;
179        double B_prob_11 = scaled_trans_B.getNumberTransitions11() / max_freq_mult;
180
181        // Set output transition info
182        double Y_prob_00 = A_prob_00 +
183                        A_prob_01 * (B_prob_00 + B_prob_10) +
184                        A_prob_10 * (B_prob_00 + B_prob_01) +
185                        A_prob_11 * B_prob_00;
186        double Y_prob_01 = A_prob_01 * (B_prob_01 + B_prob_11) +
187                        A_prob_11 * B_prob_01;
188        double Y_prob_11 = A_prob_11 * B_prob_11;
189
190        // Check that probabilities add up to 1.0 with some finite tolerance
191        ASSERT(LibUtil::Math::isEqual(Y_prob_00 + Y_prob_01 + Y_prob_01 + Y_prob_11, 1.0), "[Error] " + getInstanceName() +
192            "Output transition probabilities must add up to 1 (" + (String) Y_prob_00 + ", " +
193            (String) Y_prob_01 + ", " + (String) Y_prob_11 + ")!");
194
195        // Turn probability of transitions per cycle into number of transitions per time unit
196        TransitionInfo trans_Y(Y_prob_00 * max_freq_mult, Y_prob_01 * max_freq_mult, Y_prob_11 * max_freq_mult);
197        getOutputPort("Y")->setTransitionInfo(trans_Y);
198        return;
199    }
200
201    void AND2::cacheStdCell(StdCellLib* cell_lib_, double drive_strength_)
202    {
203        // Standard cell cache string
204        String cell_name = "AND2_X" + (String) drive_strength_;
205
206        Log::printLine("=== " + cell_name + " ===");
207
208        // Get parameters
209        double gate_pitch = cell_lib_->getTechModel()->get("Gate->PitchContacted");
210        Map<double>* cache = cell_lib_->getStdCellCache();
211
212        // Now actually build the full standard cell model
213        // Create the two input ports
214        createInputPort("A");
215        createInputPort("B");
216        createOutputPort("Y");
217
218        createNet("Y_b");
219
220        // Adds macros
221        CellMacros::addNand2(this, "NAND2", false, true, true, "A", "B", "Y_b");
222        CellMacros::addInverter(this, "INV", false, true, "Y_b", "Y");
223        CellMacros::updateNand2(this, "NAND2", drive_strength_ * 0.5);
224        CellMacros::updateInverter(this, "INV", drive_strength_ * 1.0);
225
226        // Cache area result
227        double area = 0.0;
228        area += gate_pitch * getTotalHeight() * 1;
229        area += gate_pitch * getTotalHeight() * getGenProperties()->get("NAND2_GatePitches").toDouble();
230        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV_GatePitches").toDouble();
231        cache->set(cell_name + "->ActiveArea", area);
232        Log::printLine(cell_name + "->ActiveArea=" + (String) area);
233
234        // --------------------------------------------------------------------
235        // Leakage Model Calculation
236        // --------------------------------------------------------------------
237        double leakage_00 = getGenProperties()->get("NAND2_LeakagePower_00").toDouble() +
238                            getGenProperties()->get("INV_LeakagePower_0").toDouble();
239        double leakage_01 = getGenProperties()->get("NAND2_LeakagePower_01").toDouble() +
240                            getGenProperties()->get("INV_LeakagePower_0").toDouble();
241        double leakage_10 = getGenProperties()->get("NAND2_LeakagePower_10").toDouble() +
242                            getGenProperties()->get("INV_LeakagePower_0").toDouble();
243        double leakage_11 = getGenProperties()->get("NAND2_LeakagePower_11").toDouble() +
244                            getGenProperties()->get("INV_LeakagePower_1").toDouble();
245        cache->set(cell_name + "->Leakage->!A!B", leakage_00);
246        cache->set(cell_name + "->Leakage->!AB", leakage_01);
247        cache->set(cell_name + "->Leakage->A!B", leakage_10);
248        cache->set(cell_name + "->Leakage->AB", leakage_11);
249        Log::printLine(cell_name + "->Leakage->!A!B=" + (String) leakage_00);
250        Log::printLine(cell_name + "->Leakage->!AB=" + (String) leakage_01);
251        Log::printLine(cell_name + "->Leakage->A!B=" + (String) leakage_10);
252        Log::printLine(cell_name + "->Leakage->AB=" + (String) leakage_11);
253        // --------------------------------------------------------------------
254
255        // --------------------------------------------------------------------
256        // Get Node Capacitances
257        // --------------------------------------------------------------------
258        double a_cap = getNet("A")->getTotalDownstreamCap();
259        double b_cap = getNet("B")->getTotalDownstreamCap();
260        double y_b_cap = getNet("Y_b")->getTotalDownstreamCap();
261        double y_cap = getNet("Y")->getTotalDownstreamCap();
262
263        cache->set(cell_name + "->Cap->A", a_cap);
264        cache->set(cell_name + "->Cap->B", b_cap);
265        cache->set(cell_name + "->Cap->Y_b", y_b_cap);
266        cache->set(cell_name + "->Cap->Y", y_cap);
267        Log::printLine(cell_name + "->Cap->A=" + (String) a_cap);
268        Log::printLine(cell_name + "->Cap->B=" + (String) b_cap);
269        Log::printLine(cell_name + "->Cap->Y=" + (String) y_b_cap);
270        Log::printLine(cell_name + "->Cap->Y=" + (String) y_cap);
271        // --------------------------------------------------------------------
272
273        // --------------------------------------------------------------------
274        // Build Internal Delay Model
275        // --------------------------------------------------------------------
276        double y_ron = getDriver("INV_RonZN")->getOutputRes();
277        double a_to_y_delay = getDriver("NAND2_RonZN")->calculateDelay() +
278                              getDriver("INV_RonZN")->calculateDelay();
279        double b_to_y_delay = getDriver("NAND2_RonZN")->calculateDelay() +
280                              getDriver("INV_RonZN")->calculateDelay();
281
282        cache->set(cell_name + "->DriveRes->Y", y_ron);
283        cache->set(cell_name + "->Delay->A_to_Y", a_to_y_delay);
284        cache->set(cell_name + "->Delay->B_to_Y", b_to_y_delay);
285        Log::printLine(cell_name + "->DriveRes->Y=" + (String) y_ron);
286        Log::printLine(cell_name + "->Delay->A_to_Y=" + (String) a_to_y_delay);
287        Log::printLine(cell_name + "->Delay->B_to_Y=" + (String) b_to_y_delay);
288        // --------------------------------------------------------------------
289
290        return;
291
292    }
293} // namespace DSENT
294