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/XOR2.h"
23
24#include <cmath>
25
26#include "model/PortInfo.h"
27#include "model/EventInfo.h"
28#include "model/TransitionInfo.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    XOR2::XOR2(const String& instance_name_, const TechModel* tech_model_)
42        : StdCell(instance_name_, tech_model_)
43    {
44        initProperties();
45    }
46
47    XOR2::~XOR2()
48    {}
49
50    void XOR2::initProperties()
51    {
52        return;
53    }
54
55    void XOR2::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("B_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 XOR2 Event Energy Result
88        createElectricalEventAtomicResult("XOR2");
89
90        getEventInfo("Idle")->setStaticTransitionInfos();
91
92        return;
93    }
94
95    void XOR2::updateModel()
96    {
97        // Get parameters
98        double drive_strength = getDrivingStrength();
99        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
100
101        // Standard cell cache string
102        String cell_name = "XOR2_X" + (String) drive_strength;
103
104        // Get timing parameters
105        getLoad("A_Cap")->setLoadCap(cache->get(cell_name + "->Cap->A"));
106        getLoad("B_Cap")->setLoadCap(cache->get(cell_name + "->Cap->B"));
107
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
111        getDriver("Y_Ron")->setOutputRes(cache->get(cell_name + "->DriveRes->Y"));
112
113        // Set the cell area
114        getAreaResult("Active")->setValue(cache->get(cell_name + "->ActiveArea"));
115        getAreaResult("Metal1Wire")->setValue(cache->get(cell_name + "->ActiveArea"));
116
117        return;
118    }
119
120    void XOR2::evaluateModel()
121    {
122        return;
123    }
124
125    void XOR2::useModel()
126    {
127        // Get parameters
128        double drive_strength = getDrivingStrength();
129        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
130
131        // Standard cell cache string
132        String cell_name = "XOR2_X" + (String) drive_strength;
133
134        // Propagate the transition info and get the 0->1 transtion count
135        propagateTransitionInfo();
136        double P_A = getInputPort("A")->getTransitionInfo().getProbability1();
137        double P_B = getInputPort("B")->getTransitionInfo().getProbability1();
138        double A_num_trans_01 = getInputPort("A")->getTransitionInfo().getNumberTransitions01();
139        double B_num_trans_01 = getInputPort("B")->getTransitionInfo().getNumberTransitions01();
140        double Y_num_trans_01 = getOutputPort("Y")->getTransitionInfo().getNumberTransitions01();
141
142        // Calculate leakage
143        double leakage = 0;
144        leakage += cache->get(cell_name + "->Leakage->!A!B") * (1 - P_A) * (1 - P_B);
145        leakage += cache->get(cell_name + "->Leakage->!AB") * (1 - P_A) * P_B;
146        leakage += cache->get(cell_name + "->Leakage->A!B") * P_A * (1 - P_B);
147        leakage += cache->get(cell_name + "->Leakage->AB") * P_A * P_B;
148        getNddPowerResult("Leakage")->setValue(leakage);
149
150        // Get VDD
151        double vdd = getTechModel()->get("Vdd");
152
153        // Get capacitances
154        double a_b_cap = cache->get(cell_name + "->Cap->A_b");
155        double b_b_cap = cache->get(cell_name + "->Cap->B_b");
156        double y_cap = cache->get(cell_name + "->Cap->Y");
157        double y_load_cap = getNet("Y")->getTotalDownstreamCap();
158
159        // Calculate XOR Event energy
160        double xor2_event_result = 0.0;
161        xor2_event_result += a_b_cap * A_num_trans_01;
162        xor2_event_result += b_b_cap * B_num_trans_01;
163        xor2_event_result += (y_cap + y_load_cap) * Y_num_trans_01;
164        xor2_event_result *= vdd * vdd;
165        getEventResult("XOR2")->setValue(xor2_event_result);
166
167        return;
168    }
169
170    void XOR2::propagateTransitionInfo()
171    {
172        // Get input signal transition info
173        const TransitionInfo& trans_A = getInputPort("A")->getTransitionInfo();
174        const TransitionInfo& trans_B = getInputPort("B")->getTransitionInfo();
175
176        double max_freq_mult = max(trans_A.getFrequencyMultiplier(), trans_B.getFrequencyMultiplier());
177        const TransitionInfo& scaled_trans_A = trans_A.scaleFrequencyMultiplier(max_freq_mult);
178        const TransitionInfo& scaled_trans_B = trans_B.scaleFrequencyMultiplier(max_freq_mult);
179
180
181        double A_prob_00 = scaled_trans_A.getNumberTransitions00() / max_freq_mult;
182        double A_prob_01 = scaled_trans_A.getNumberTransitions01() / max_freq_mult;
183        double A_prob_10 = A_prob_01;
184        double A_prob_11 = scaled_trans_A.getNumberTransitions11() / max_freq_mult;
185        double B_prob_00 = scaled_trans_B.getNumberTransitions00() / max_freq_mult;
186        double B_prob_01 = scaled_trans_B.getNumberTransitions01() / max_freq_mult;
187        double B_prob_10 = B_prob_01;
188        double B_prob_11 = scaled_trans_B.getNumberTransitions11() / max_freq_mult;
189
190        // Set output transition info
191        double Y_prob_00 = A_prob_00 * B_prob_00 +
192                                A_prob_01 * B_prob_01 +
193                                A_prob_10 * B_prob_10 +
194                                A_prob_11 * B_prob_11;
195        double Y_prob_01 = A_prob_00 * B_prob_01 +
196                                A_prob_01 * B_prob_00 +
197                                A_prob_10 * B_prob_11 +
198                                A_prob_11 * B_prob_10;
199        double Y_prob_11 = A_prob_00 * B_prob_11 +
200                                A_prob_01 * B_prob_10 +
201                                A_prob_10 * B_prob_01 +
202                                A_prob_11 * B_prob_00;
203
204        // Check that probabilities add up to 1.0 with some finite tolerance
205        ASSERT(LibUtil::Math::isEqual((Y_prob_00 + Y_prob_01 + Y_prob_01 + Y_prob_11), 1.0),
206            "[Error] " + getInstanceName() +  "Output transition probabilities must add up to 1 (" +
207            (String) Y_prob_00 + ", " + (String) Y_prob_01 + ", " + (String) Y_prob_11 + ")!");
208
209        // Turn probability of transitions per cycle into number of transitions per time unit
210        TransitionInfo trans_Y(Y_prob_00 * max_freq_mult, Y_prob_01 * max_freq_mult, Y_prob_11 * max_freq_mult);
211        getOutputPort("Y")->setTransitionInfo(trans_Y);
212        return;
213    }
214
215    // Creates the standard cell, characterizes and abstracts away the details
216    void XOR2::cacheStdCell(StdCellLib* cell_lib_, double drive_strength_)
217    {
218        // Get parameters
219        double gate_pitch = cell_lib_->getTechModel()->get("Gate->PitchContacted");
220        Map<double>* cache = cell_lib_->getStdCellCache();
221
222        // Standard cell cache string
223        String cell_name = "XOR2_X" + (String) drive_strength_;
224
225        Log::printLine("=== " + cell_name + " ===");
226
227        // Now actually build the full standard cell model
228        createInputPort("A");
229        createInputPort("B");
230        createOutputPort("Y");
231
232        createNet("A_b");
233        createNet("B_b");
234
235        // Adds macros
236        CellMacros::addInverter(this, "INV1", false, true, "A", "A_b");
237        CellMacros::addInverter(this, "INV2", false, true, "B", "B_b");
238        CellMacros::addTristate(this, "INVZ1", true, true, true, true, "B", "A", "A_b", "Y");
239        CellMacros::addTristate(this, "INVZ2", true, true, true, true, "B_b", "A_b", "A", "Y");
240
241        // I have no idea how to size each of the parts haha
242        CellMacros::updateInverter(this, "INV1", drive_strength_ * 0.500);
243        CellMacros::updateInverter(this, "INV2", drive_strength_ * 0.500);
244        CellMacros::updateTristate(this, "INVZ1", drive_strength_ * 1.000);
245        CellMacros::updateTristate(this, "INVZ2", drive_strength_ * 1.000);
246
247        // Cache area result
248        double area = 0.0;
249        area += gate_pitch * getTotalHeight() * 1;
250        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV1_GatePitches").toDouble();
251        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV2_GatePitches").toDouble();
252        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INVZ1_GatePitches").toDouble();
253        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INVZ2_GatePitches").toDouble();
254        cache->set(cell_name + "->ActiveArea", area);
255        Log::printLine(cell_name + "->ActiveArea=" + (String) area);
256
257        // --------------------------------------------------------------------
258        // Leakage Model Calculation
259        // --------------------------------------------------------------------
260        // Cache leakage power results (for every single signal combination)
261        double leakage_00 = 0;          //!A, !B
262        double leakage_01 = 0;          //!A, B
263        double leakage_10 = 0;          //A, !B
264        double leakage_11 = 0;          //A, B
265
266        //This is so painful...
267        leakage_00 += getGenProperties()->get("INV1_LeakagePower_0").toDouble();
268        leakage_00 += getGenProperties()->get("INV2_LeakagePower_0").toDouble();
269        leakage_00 += getGenProperties()->get("INVZ1_LeakagePower_010_0").toDouble();
270        leakage_00 += getGenProperties()->get("INVZ2_LeakagePower_101_0").toDouble();
271
272        leakage_01 += getGenProperties()->get("INV1_LeakagePower_0").toDouble();
273        leakage_01 += getGenProperties()->get("INV2_LeakagePower_1").toDouble();
274        leakage_01 += getGenProperties()->get("INVZ1_LeakagePower_011_1").toDouble();
275        leakage_01 += getGenProperties()->get("INVZ2_LeakagePower_100_1").toDouble();
276
277        leakage_10 += getGenProperties()->get("INV1_LeakagePower_1").toDouble();
278        leakage_10 += getGenProperties()->get("INV2_LeakagePower_0").toDouble();
279        leakage_10 += getGenProperties()->get("INVZ1_LeakagePower_100_1").toDouble();
280        leakage_10 += getGenProperties()->get("INVZ2_LeakagePower_011_1").toDouble();
281
282        leakage_11 += getGenProperties()->get("INV1_LeakagePower_1").toDouble();
283        leakage_11 += getGenProperties()->get("INV2_LeakagePower_1").toDouble();
284        leakage_11 += getGenProperties()->get("INVZ1_LeakagePower_101_0").toDouble();
285        leakage_11 += getGenProperties()->get("INVZ2_LeakagePower_010_0").toDouble();
286
287        cache->set(cell_name + "->Leakage->!A!B", leakage_00);
288        cache->set(cell_name + "->Leakage->!AB", leakage_01);
289        cache->set(cell_name + "->Leakage->A!B", leakage_10);
290        cache->set(cell_name + "->Leakage->AB", leakage_11);
291        Log::printLine(cell_name + "->Leakage->!A!B=" + (String) leakage_00);
292        Log::printLine(cell_name + "->Leakage->!AB=" + (String) leakage_01);
293        Log::printLine(cell_name + "->Leakage->A!B=" + (String) leakage_10);
294        Log::printLine(cell_name + "->Leakage->AB=" + (String) leakage_11);
295        // --------------------------------------------------------------------
296
297        // Cache event energy results
298        /*
299        double event_a_flip = 0.0;
300        event_a_flip += getGenProperties()->get("INV1_A_Flip").toDouble() + getGenProperties()->get("INV1_ZN_Flip").toDouble();
301        event_a_flip += getGenProperties()->get("INVZ1_OE_Flip").toDouble() + getGenProperties()->get("INVZ1_OEN_Flip").toDouble();
302        event_a_flip += getGenProperties()->get("INVZ2_OE_Flip").toDouble() + getGenProperties()->get("INVZ2_OEN_Flip").toDouble();
303        cache->set(cell_name + "->Event_A_Flip", event_a_flip);
304        Log::printLine(cell_name + "->Event_A_Flip=" + (String) event_a_flip);
305
306        double event_b_flip = 0.0;
307        event_b_flip += getGenProperties()->get("INV2_A_Flip").toDouble() + getGenProperties()->get("INV2_ZN_Flip").toDouble();
308        event_b_flip += getGenProperties()->get("INVZ1_A_Flip").toDouble();
309        event_b_flip += getGenProperties()->get("INVZ2_A_Flip").toDouble();
310        cache->set(cell_name + "->Event_B_Flip", event_b_flip);
311        Log::printLine(cell_name + "->Event_B_Flip=" + (String) event_b_flip);
312
313        double event_y_flip = 0.0;
314        event_y_flip += getGenProperties()->get("INVZ1_ZN_Flip").toDouble();
315        event_y_flip += getGenProperties()->get("INVZ2_ZN_Flip").toDouble();
316        cache->set(cell_name + "->Event_Y_Flip", event_y_flip);
317        Log::printLine(cell_name + "->Event_Y_Flip=" + (String) event_y_flip);
318        */
319
320        // --------------------------------------------------------------------
321        // Get Node Capacitances
322        // --------------------------------------------------------------------
323        // Build abstracted timing model
324        double a_cap = getNet("A")->getTotalDownstreamCap();
325        double b_cap = getNet("B")->getTotalDownstreamCap();
326        double a_b_cap = getNet("A_b")->getTotalDownstreamCap();
327        double b_b_cap = getNet("B_b")->getTotalDownstreamCap();
328        double y_cap = getNet("Y")->getTotalDownstreamCap();
329
330        cache->set(cell_name + "->Cap->A", a_cap);
331        cache->set(cell_name + "->Cap->B", b_cap);
332        cache->set(cell_name + "->Cap->A_b", a_b_cap);
333        cache->set(cell_name + "->Cap->B_b", b_b_cap);
334        cache->set(cell_name + "->Cap->Y", y_cap);
335        Log::printLine(cell_name + "->Cap->A=" + (String) a_cap);
336        Log::printLine(cell_name + "->Cap->B=" + (String) b_cap);
337        Log::printLine(cell_name + "->Cap->A=" + (String) a_b_cap);
338        Log::printLine(cell_name + "->Cap->B=" + (String) b_b_cap);
339        Log::printLine(cell_name + "->Cap->Y=" + (String) y_cap);
340        // --------------------------------------------------------------------
341
342        // --------------------------------------------------------------------
343        // Build Internal Delay Model
344        // --------------------------------------------------------------------
345        double y_ron = (getDriver("INVZ1_RonZN")->getOutputRes() + getDriver("INVZ2_RonZN")->getOutputRes()) / 2;
346
347        double a_to_y_delay = 0.0;
348        a_to_y_delay += getDriver("INV1_RonZN")->calculateDelay();
349        a_to_y_delay += max(getDriver("INVZ1_RonZN")->calculateDelay(), getDriver("INVZ2_RonZN")->calculateDelay());
350
351        double b_to_y_delay = 0.0;
352        b_to_y_delay += max(getDriver("INVZ1_RonZN")->calculateDelay(), getDriver("INV2_RonZN")->calculateDelay() + getDriver("INVZ2_RonZN")->calculateDelay());
353
354        cache->set(cell_name + "->DriveRes->Y", y_ron);
355        cache->set(cell_name + "->Delay->A_to_Y", a_to_y_delay);
356        cache->set(cell_name + "->Delay->B_to_Y", b_to_y_delay);
357        Log::printLine(cell_name + "->DriveRes->Y=" + (String) y_ron);
358        Log::printLine(cell_name + "->Delay->A_to_Y=" + (String) a_to_y_delay);
359        Log::printLine(cell_name + "->Delay->B_to_Y=" + (String) b_to_y_delay);
360        // --------------------------------------------------------------------
361
362        return;
363    }
364
365} // namespace DSENT
366
367