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/NOR2.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    NOR2::NOR2(const String& instance_name_, const TechModel* tech_model_)
42        : StdCell(instance_name_, tech_model_)
43    {
44        initProperties();
45    }
46
47    NOR2::~NOR2()
48    {}
49
50    void NOR2::initProperties()
51    {
52        return;
53    }
54
55    void NOR2::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 NOR Event Energy Result
88        createElectricalEventAtomicResult("NOR2");
89
90        getEventInfo("Idle")->setStaticTransitionInfos();
91
92        return;
93    }
94
95    void NOR2::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 = "NOR2_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 + "->ActiveArea"));
116        getAreaResult("Metal1Wire")->setValue(cache->get(cell_name + "->ActiveArea"));
117
118        return;
119    }
120
121    void NOR2::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 = "NOR2_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 VDD
145        double vdd = getTechModel()->get("Vdd");
146        // Get capacitances
147        double y_cap = cache->get(cell_name + "->Cap->Y");
148        double y_load_cap = getNet("Y")->getTotalDownstreamCap();
149
150        // Calculate NOR2Event energy
151        double energy_per_trans_01 = (y_cap + y_load_cap) * vdd * vdd;
152        getEventResult("NOR2")->setValue(energy_per_trans_01 * Y_num_trans_01);
153
154        return;
155    }
156
157    void NOR2::propagateTransitionInfo()
158    {
159        // Get input signal transition info
160        const TransitionInfo& trans_A = getInputPort("A")->getTransitionInfo();
161        const TransitionInfo& trans_B = getInputPort("B")->getTransitionInfo();
162
163        double max_freq_mult = max(trans_A.getFrequencyMultiplier(), trans_B.getFrequencyMultiplier());
164        const TransitionInfo& scaled_trans_A = trans_A.scaleFrequencyMultiplier(max_freq_mult);
165        const TransitionInfo& scaled_trans_B = trans_B.scaleFrequencyMultiplier(max_freq_mult);
166
167        double A_prob_00 = scaled_trans_A.getNumberTransitions00() / max_freq_mult;
168        double A_prob_01 = scaled_trans_A.getNumberTransitions01() / max_freq_mult;
169        double A_prob_10 = A_prob_01;
170        double A_prob_11 = scaled_trans_A.getNumberTransitions11() / max_freq_mult;
171        double B_prob_00 = scaled_trans_B.getNumberTransitions00() / max_freq_mult;
172        double B_prob_01 = scaled_trans_B.getNumberTransitions01() / max_freq_mult;
173        double B_prob_10 = B_prob_01;
174        double B_prob_11 = scaled_trans_B.getNumberTransitions11() / max_freq_mult;
175
176        // Set output transition info
177        double Y_prob_00 = A_prob_00 * B_prob_11 +
178                        A_prob_01 * (B_prob_10 + B_prob_11) +
179                        A_prob_10 * (B_prob_01 + B_prob_11) +
180                        A_prob_11;
181        double Y_prob_01 = A_prob_00 * B_prob_10 +
182                        A_prob_10 * (B_prob_00 + B_prob_10);
183        double Y_prob_11 = A_prob_00 * B_prob_00;
184
185        // Check that probabilities add up to 1.0 with some finite tolerance
186        ASSERT(LibUtil::Math::isEqual((Y_prob_00 + Y_prob_01 + Y_prob_01 + Y_prob_11), 1.0),
187            "[Error] " + getInstanceName() +  "Output transition probabilities must add up to 1 (" +
188            (String) Y_prob_00 + ", " + (String) Y_prob_01 + ", " + (String) Y_prob_11 + ")!");
189
190        // Turn probability of transitions per cycle into number of transitions per time unit
191        TransitionInfo trans_Y(Y_prob_00 * max_freq_mult, Y_prob_01 * max_freq_mult, Y_prob_11 * max_freq_mult);
192        getOutputPort("Y")->setTransitionInfo(trans_Y);
193        return;
194    }
195
196    void NOR2::cacheStdCell(StdCellLib* cell_lib_, double drive_strength_)
197    {
198        // Standard cell cache string
199        String cell_name = "NOR2_X" + (String) drive_strength_;
200
201        Log::printLine("=== " + cell_name + " ===");
202
203        // Get parameters
204        double gate_pitch = cell_lib_->getTechModel()->get("Gate->PitchContacted");
205        Map<double>* cache = cell_lib_->getStdCellCache();
206
207        // Now actually build the full standard cell model
208        // Create the two input ports
209        createInputPort("A");
210        createInputPort("B");
211        createOutputPort("Y");
212
213        // Adds macros
214        CellMacros::addNor2(this, "NOR", true, true, true, "A", "B", "Y");
215        CellMacros::updateNor2(this, "NOR", drive_strength_);
216
217        // Cache area result
218        double area = gate_pitch * getTotalHeight() * (1 + getGenProperties()->get("NOR_GatePitches").toDouble());
219        cache->set(cell_name + "->ActiveArea", area);
220        Log::printLine(cell_name + "->ActiveArea=" + (String) area);
221
222        // --------------------------------------------------------------------
223        // Leakage Model Calculation
224        // --------------------------------------------------------------------
225        double leakage_00 = getGenProperties()->get("NOR_LeakagePower_00").toDouble();
226        double leakage_01 = getGenProperties()->get("NOR_LeakagePower_01").toDouble();
227        double leakage_10 = getGenProperties()->get("NOR_LeakagePower_10").toDouble();
228        double leakage_11 = getGenProperties()->get("NOR_LeakagePower_11").toDouble();
229        cache->set(cell_name + "->Leakage->!A!B", leakage_00);
230        cache->set(cell_name + "->Leakage->!AB", leakage_01);
231        cache->set(cell_name + "->Leakage->A!B", leakage_10);
232        cache->set(cell_name + "->Leakage->AB", leakage_11);
233        Log::printLine(cell_name + "->Leakage->!A!B=" + (String) leakage_00);
234        Log::printLine(cell_name + "->Leakage->!AB=" + (String) leakage_01);
235        Log::printLine(cell_name + "->Leakage->A!B=" + (String) leakage_10);
236        Log::printLine(cell_name + "->Leakage->AB=" + (String) leakage_11);
237        // --------------------------------------------------------------------
238
239        /*
240        // Cache event energy results
241        double event_a_flip = getGenProperties()->get("NOR_A1_Flip").toDouble();
242        double event_b_flip = getGenProperties()->get("NOR_A2_Flip").toDouble();
243        double event_zn_flip = getGenProperties()->get("NOR_ZN_Flip").toDouble();
244
245        cache->set(cell_name + "->Event_A_Flip", event_a_flip);
246        cache->set(cell_name + "->Event_B_Flip", event_b_flip);
247        cache->set(cell_name + "->Event_ZN_Flip", event_zn_flip);
248        Log::printLine(cell_name + "->Event_A_Flip=" + (String) event_a_flip);
249        Log::printLine(cell_name + "->Event_B_Flip=" + (String) event_b_flip);
250        Log::printLine(cell_name + "->Event_ZN_Flip=" + (String) event_zn_flip);
251        */
252
253        // --------------------------------------------------------------------
254        // Get Node Capacitances
255        // --------------------------------------------------------------------
256        // Build abstracted timing model
257        double a_cap = getNet("A")->getTotalDownstreamCap();
258        double b_cap = getNet("B")->getTotalDownstreamCap();
259        double y_cap = getNet("Y")->getTotalDownstreamCap();
260
261        cache->set(cell_name + "->Cap->A", a_cap);
262        cache->set(cell_name + "->Cap->B", b_cap);
263        cache->set(cell_name + "->Cap->Y", y_cap);
264        Log::printLine(cell_name + "->Cap->A_Cap=" + (String) a_cap);
265        Log::printLine(cell_name + "->Cap->B_Cap=" + (String) b_cap);
266        Log::printLine(cell_name + "->Cap->Y_Cap=" + (String) y_cap);
267        // --------------------------------------------------------------------
268
269        // --------------------------------------------------------------------
270        // Build Internal Delay Model
271        // --------------------------------------------------------------------
272        double y_ron = getDriver("NOR_RonZN")->getOutputRes();
273        double a_to_y_delay = getDriver("NOR_RonZN")->calculateDelay();
274        double b_to_y_delay = getDriver("NOR_RonZN")->calculateDelay();
275
276        cache->set(cell_name + "->DriveRes->Y", y_ron);
277        cache->set(cell_name + "->Delay->A_to_Y", a_to_y_delay);
278        cache->set(cell_name + "->Delay->B_to_Y", b_to_y_delay);
279        Log::printLine(cell_name + "->DriveRes->Y=" + (String) y_ron);
280        Log::printLine(cell_name + "->Delay->A_to_Y=" + (String) a_to_y_delay);
281        Log::printLine(cell_name + "->Delay->B_to_Y=" + (String) b_to_y_delay);
282        // --------------------------------------------------------------------
283
284        return;
285
286    }
287
288} // namespace DSENT
289
290