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/LATQ.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    using std::min;
41
42    LATQ::LATQ(const String& instance_name_, const TechModel* tech_model_)
43        : StdCell(instance_name_, tech_model_)
44    {
45        initProperties();
46    }
47
48    LATQ::~LATQ()
49    {}
50
51    void LATQ::initProperties()
52    {
53        return;
54    }
55
56    void LATQ::constructModel()
57    {
58        // All constructModel should do is create Area/NDDPower/Energy Results as
59        // well as instantiate any sub-instances using only the hard parameters
60
61        createInputPort("D");
62        createInputPort("G");
63        createOutputPort("Q");
64
65        createLoad("D_Cap");
66        createLoad("G_Cap");
67        createDelay("D_to_Q_delay");
68        createDelay("G_to_Q_delay");
69        createDriver("Q_Ron", true);
70
71        ElectricalLoad* d_cap = getLoad("D_Cap");
72        ElectricalLoad* g_cap = getLoad("G_Cap");
73        ElectricalDelay* d_to_q_delay = getDelay("D_to_Q_delay");
74        ElectricalDelay* g_to_q_delay = getDelay("G_to_Q_delay");
75        ElectricalDriver* q_ron = getDriver("Q_Ron");
76
77        getNet("D")->addDownstreamNode(d_cap);
78        getNet("G")->addDownstreamNode(g_cap);
79        d_cap->addDownstreamNode(d_to_q_delay);
80        g_cap->addDownstreamNode(g_to_q_delay);
81        g_to_q_delay->addDownstreamNode(q_ron);
82        q_ron->addDownstreamNode(getNet("Q"));
83
84        // Create Area result
85        // Create NDD Power result
86        createElectricalAtomicResults();
87        // Create G Event Energy Result
88        createElectricalEventAtomicResult("G");
89        // Create DFF Event Energy Result
90        createElectricalEventAtomicResult("LATD");
91        createElectricalEventAtomicResult("LATQ");
92        // Create Idle event for leakage
93        // G pin is assumed to be on all the time
94        //createElectricalEventAtomicResult("Idle");
95        getEventInfo("Idle")->setStaticTransitionInfos();
96        return;
97    }
98
99    void LATQ::updateModel()
100    {
101        // Get parameters
102        double drive_strength = getDrivingStrength();
103        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
104
105        // Standard cell cache string
106        String cell_name = "LATQ_X" + (String) drive_strength;
107
108        // Get timing parameters
109        getLoad("D_Cap")->setLoadCap(cache->get(cell_name + "->Cap->D"));
110        getLoad("G_Cap")->setLoadCap(cache->get(cell_name + "->Cap->G"));
111        getDriver("Q_Ron")->setOutputRes(cache->get(cell_name + "->DriveRes->Q"));
112        getDelay("G_to_Q_delay")->setDelay(cache->get(cell_name + "->Delay->G_to_Q"));
113        getDelay("D_to_Q_delay")->setDelay(cache->get(cell_name + "->Delay->D_to_Q"));
114
115        // Set the cell area
116        getAreaResult("Active")->setValue(cache->get(cell_name + "->Area->Active"));
117        getAreaResult("Metal1Wire")->setValue(cache->get(cell_name + "->Area->Metal1Wire"));
118
119        return;
120    }
121
122    void LATQ::evaluateModel()
123    {
124        return;
125    }
126
127    void LATQ::useModel()
128    {
129        // Get parameters
130        double drive_strength = getDrivingStrength();
131        Map<double>* cache = getTechModel()->getStdCellLib()->getStdCellCache();
132
133        // Standard cell cache string
134        String cell_name = "LATQ_X" + (String) drive_strength;
135
136        // Propagate the transition info and get P_D, P_M, and P_Q
137        propagateTransitionInfo();
138        double P_D = getInputPort("D")->getTransitionInfo().getProbability1();
139        double P_G = getInputPort("G")->getTransitionInfo().getProbability1();
140        double P_Q = getOutputPort("Q")->getTransitionInfo().getProbability1();
141        double G_num_trans_01 = getInputPort("G")->getTransitionInfo().getNumberTransitions01();
142        double D_num_trans_01 = getInputPort("D")->getTransitionInfo().getNumberTransitions01();
143        double Q_num_trans_01 = getOutputPort("Q")->getTransitionInfo().getNumberTransitions01();
144
145        // Calculate leakage
146        double leakage = 0;
147        leakage += cache->get(cell_name + "->Leakage->!D!G!Q") * (1 - P_D) * (1 - P_G) * (1 - P_Q);
148        leakage += cache->get(cell_name + "->Leakage->!D!GQ") * (1 - P_D) * (1 - P_G) * P_Q;
149        leakage += cache->get(cell_name + "->Leakage->!DG!Q") * (1 - P_D) * P_G * (1 - P_Q);
150        leakage += cache->get(cell_name + "->Leakage->D!G!Q") * P_D * (1 - P_G) * (1 - P_Q);
151        leakage += cache->get(cell_name + "->Leakage->D!GQ") * P_D * (1 - P_G) * P_Q;
152        leakage += cache->get(cell_name + "->Leakage->DGQ") * P_D * P_G * P_Q;
153        getNddPowerResult("Leakage")->setValue(leakage);
154
155        // Get VDD
156        double vdd = getTechModel()->get("Vdd");
157
158        // Get capacitances
159        double g_b_cap = cache->get(cell_name + "->Cap->G_b");
160        double d_b_cap = cache->get(cell_name + "->Cap->D_b");
161        double q_i_cap = cache->get(cell_name + "->Cap->Q_i");
162        double q_b_cap = cache->get(cell_name + "->Cap->Q_b");
163        double q_cap = cache->get(cell_name + "->Cap->Q");
164        double q_load_cap = getNet("Q")->getTotalDownstreamCap();
165
166        // Calculate G Event energy
167        double g_event_energy = 0.0;
168        g_event_energy += (g_b_cap) * G_num_trans_01;
169        g_event_energy *= vdd * vdd;
170        getEventResult("G")->setValue(g_event_energy);
171        // Calculate LATD Event energy
172        double latd_event_energy = 0.0;
173        latd_event_energy += (d_b_cap) * D_num_trans_01;
174        latd_event_energy *= vdd * vdd;
175        getEventResult("LATD")->setValue(latd_event_energy);
176        // Calculate LATQ Event energy
177        double latq_event_energy = 0.0;
178        latq_event_energy += (q_i_cap + q_b_cap + q_cap + q_load_cap) * Q_num_trans_01;
179        latq_event_energy *= vdd * vdd;
180        getEventResult("LATQ")->setValue(latq_event_energy);
181
182        return;
183    }
184
185    void LATQ::propagateTransitionInfo()
186    {
187        const TransitionInfo& trans_G = getInputPort("G")->getTransitionInfo();
188        const TransitionInfo& trans_D = getInputPort("D")->getTransitionInfo();
189
190        double G_num_trans_01 = trans_G.getNumberTransitions01();
191        double G_num_trans_10 = G_num_trans_01;
192        double G_num_trans_00 = trans_G.getNumberTransitions00();
193        double D_freq_mult = trans_D.getFrequencyMultiplier();
194
195        // If the latch is sampling just as fast or faster than input data signal
196        // Then it can capture all transitions (though it should be normalized to clock)
197        if((G_num_trans_10 + G_num_trans_00) >= D_freq_mult)
198        {
199            const TransitionInfo& trans_Q = trans_D.scaleFrequencyMultiplier(G_num_trans_10 + G_num_trans_00);
200            getOutputPort("Q")->setTransitionInfo(trans_Q);
201        }
202        // If the latch is sampling slower than the input data signal, then input
203        // will look like they transition more
204        else
205        {
206            // Calculate scale ratio
207            double scale_ratio = (G_num_trans_10 + G_num_trans_00) / D_freq_mult;
208            // 00 and 11 transitions become fewer
209            double D_scaled_diff = 0.5 * (1 - scale_ratio) * (trans_D.getNumberTransitions00() + trans_D.getNumberTransitions11());
210            double D_scaled_num_trans_00 = trans_D.getNumberTransitions00() * scale_ratio;
211            double D_scaled_num_trans_11 = trans_D.getNumberTransitions11() * scale_ratio;
212            // 01 and 10 transitions become more frequent
213            double D_scaled_num_trans_10 = trans_D.getNumberTransitions01() + D_scaled_diff;
214
215            // Create final transition info, remembering to apply scaling ratio to normalize to G
216            const TransitionInfo trans_Q(   D_scaled_num_trans_00 * scale_ratio,
217                                            D_scaled_num_trans_10 * scale_ratio,
218                                            D_scaled_num_trans_11 * scale_ratio);
219            getOutputPort("Q")->setTransitionInfo(trans_Q);
220        }
221
222        return;
223    }
224
225    // Creates the standard cell, characterizes and abstracts away the details
226    void LATQ::cacheStdCell(StdCellLib* cell_lib_, double drive_strength_)
227    {
228        // Get parameters
229        double gate_pitch = cell_lib_->getTechModel()->get("Gate->PitchContacted");
230        Map<double>* cache = cell_lib_->getStdCellCache();
231
232        // Standard cell cache string
233        String cell_name = "LATQ_X" + (String) drive_strength_;
234
235        Log::printLine("=== " + cell_name + " ===");
236
237
238        // Now actually build the full standard cell model
239        createInputPort("D");
240        createInputPort("G");
241        createOutputPort("Q");
242
243        createNet("D_b");
244        createNet("Q_i");
245        createNet("Q_b");
246        createNet("G_b");
247
248        // Adds macros
249        CellMacros::addInverter(this, "INV1", false, true, "D", "D_b");
250        CellMacros::addInverter(this, "INV2", false, true, "Q_i", "Q_b");
251        CellMacros::addInverter(this, "INV3", false, true, "Q_b", "Q");
252        CellMacros::addInverter(this, "INV4", false, true, "G", "G_b");
253        CellMacros::addTristate(this, "INVZ1", false, true, false, false, "D_b", "G", "G_b", "Q_i");        //trace timing through A->ZN path only
254        CellMacros::addTristate(this, "INVZ2", false, false, false, false, "Q_b", "G_b", "G", "Q_i");       //don't trace timing through the feedback path
255
256        // Update macros
257        CellMacros::updateInverter(this, "INV1", drive_strength_ * 0.125);
258        CellMacros::updateInverter(this, "INV2", drive_strength_ * 0.5);
259        CellMacros::updateInverter(this, "INV3", drive_strength_ * 1.0);
260        CellMacros::updateInverter(this, "INV4", drive_strength_ * 0.125);
261        CellMacros::updateTristate(this, "INVZ1", drive_strength_ * 0.5);
262        CellMacros::updateTristate(this, "INVZ2", drive_strength_ * 0.0625);
263
264        // Cache area result
265        double area = 0.0;
266        area += gate_pitch * getTotalHeight() * 1;
267        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV1_GatePitches").toDouble();
268        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV2_GatePitches").toDouble();
269        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV3_GatePitches").toDouble();
270        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INV4_GatePitches").toDouble();
271        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INVZ1_GatePitches").toDouble();
272        area += gate_pitch * getTotalHeight() * getGenProperties()->get("INVZ2_GatePitches").toDouble();
273        cache->set(cell_name + "->Area->Active", area);
274        cache->set(cell_name + "->Area->Metal1Wire", area);     //Cover-block m1 area
275        Log::printLine(cell_name + "->Area->Active=" + (String) area);
276        Log::printLine(cell_name + "->Area->Metal1Wire=" + (String) area);
277
278        // --------------------------------------------------------------------
279        // Leakage Model Calculation
280        // --------------------------------------------------------------------
281        // Cache leakage power results (for every single signal combination)
282        double leakage_000 = 0;         //!D, !G, !Q
283        double leakage_001 = 0;         //!D, !G, Q
284        double leakage_010 = 0;         //!D, G, !Q
285        double leakage_100 = 0;         //D, !G, !Q
286        double leakage_101 = 0;         //D, !G, Q
287        double leakage_111 = 0;         //D, G, Q
288
289        //This is so painful...
290        leakage_000 += getGenProperties()->get("INV1_LeakagePower_0").toDouble();
291        leakage_000 += getGenProperties()->get("INV2_LeakagePower_0").toDouble();
292        leakage_000 += getGenProperties()->get("INV3_LeakagePower_1").toDouble();
293        leakage_000 += getGenProperties()->get("INV4_LeakagePower_0").toDouble();
294        leakage_000 += getGenProperties()->get("INVZ1_LeakagePower_011_0").toDouble();
295        leakage_000 += getGenProperties()->get("INVZ2_LeakagePower_101_0").toDouble();
296
297        leakage_001 += getGenProperties()->get("INV1_LeakagePower_0").toDouble();
298        leakage_001 += getGenProperties()->get("INV2_LeakagePower_0").toDouble();
299        leakage_001 += getGenProperties()->get("INV3_LeakagePower_0").toDouble();
300        leakage_001 += getGenProperties()->get("INV4_LeakagePower_0").toDouble();
301        leakage_001 += getGenProperties()->get("INVZ1_LeakagePower_011_1").toDouble();
302        leakage_001 += getGenProperties()->get("INVZ2_LeakagePower_100_1").toDouble();
303
304        leakage_010 += getGenProperties()->get("INV1_LeakagePower_0").toDouble();
305        leakage_010 += getGenProperties()->get("INV2_LeakagePower_0").toDouble();
306        leakage_010 += getGenProperties()->get("INV3_LeakagePower_1").toDouble();
307        leakage_010 += getGenProperties()->get("INV4_LeakagePower_1").toDouble();
308        leakage_010 += getGenProperties()->get("INVZ1_LeakagePower_101_0").toDouble();
309        leakage_010 += getGenProperties()->get("INVZ2_LeakagePower_011_0").toDouble();
310
311        leakage_100 += getGenProperties()->get("INV1_LeakagePower_1").toDouble();
312        leakage_100 += getGenProperties()->get("INV2_LeakagePower_1").toDouble();
313        leakage_100 += getGenProperties()->get("INV3_LeakagePower_1").toDouble();
314        leakage_100 += getGenProperties()->get("INV4_LeakagePower_0").toDouble();
315        leakage_100 += getGenProperties()->get("INVZ1_LeakagePower_010_0").toDouble();
316        leakage_100 += getGenProperties()->get("INVZ2_LeakagePower_101_0").toDouble();
317
318        leakage_101 += getGenProperties()->get("INV1_LeakagePower_1").toDouble();
319        leakage_101 += getGenProperties()->get("INV2_LeakagePower_1").toDouble();
320        leakage_101 += getGenProperties()->get("INV3_LeakagePower_0").toDouble();
321        leakage_101 += getGenProperties()->get("INV4_LeakagePower_0").toDouble();
322        leakage_101 += getGenProperties()->get("INVZ1_LeakagePower_010_1").toDouble();
323        leakage_101 += getGenProperties()->get("INVZ2_LeakagePower_100_1").toDouble();
324
325        leakage_111 += getGenProperties()->get("INV1_LeakagePower_1").toDouble();
326        leakage_111 += getGenProperties()->get("INV2_LeakagePower_1").toDouble();
327        leakage_111 += getGenProperties()->get("INV3_LeakagePower_0").toDouble();
328        leakage_111 += getGenProperties()->get("INV4_LeakagePower_1").toDouble();
329        leakage_111 += getGenProperties()->get("INVZ1_LeakagePower_100_1").toDouble();
330        leakage_111 += getGenProperties()->get("INVZ2_LeakagePower_010_1").toDouble();
331
332        cache->set(cell_name + "->Leakage->!D!G!Q", leakage_000);
333        cache->set(cell_name + "->Leakage->!D!GQ", leakage_001);
334        cache->set(cell_name + "->Leakage->!DG!Q", leakage_010);
335        cache->set(cell_name + "->Leakage->D!G!Q", leakage_100);
336        cache->set(cell_name + "->Leakage->D!GQ", leakage_101);
337        cache->set(cell_name + "->Leakage->DGQ", leakage_111);
338        Log::printLine(cell_name + "->Leakage->!D!G!Q=" + (String) leakage_000);
339        Log::printLine(cell_name + "->Leakage->!D!GQ=" + (String) leakage_001);
340        Log::printLine(cell_name + "->Leakage->!DG!Q=" + (String) leakage_010);
341        Log::printLine(cell_name + "->Leakage->D!G!Q=" + (String) leakage_100);
342        Log::printLine(cell_name + "->Leakage->D!GQ=" + (String) leakage_101);
343        Log::printLine(cell_name + "->Leakage->DGQ=" + (String) leakage_111);
344        // --------------------------------------------------------------------
345
346        // --------------------------------------------------------------------
347        // Get Node Capacitances
348        // --------------------------------------------------------------------
349        double d_cap = getNet("D")->getTotalDownstreamCap();
350        double d_b_cap = getNet("D_b")->getTotalDownstreamCap();
351        double q_i_cap = getNet("Q_i")->getTotalDownstreamCap();
352        double q_b_cap = getNet("Q_b")->getTotalDownstreamCap();
353        double q_cap = getNet("Q")->getTotalDownstreamCap();
354        double g_cap = getNet("G")->getTotalDownstreamCap();
355        double g_b_cap = getNet("G_b")->getTotalDownstreamCap();
356
357        cache->set(cell_name + "->Cap->D", d_cap);
358        cache->set(cell_name + "->Cap->D_b", d_b_cap);
359        cache->set(cell_name + "->Cap->Q_i", q_i_cap);
360        cache->set(cell_name + "->Cap->Q_b", q_b_cap);
361        cache->set(cell_name + "->Cap->Q", q_cap);
362        cache->set(cell_name + "->Cap->G", g_cap);
363        cache->set(cell_name + "->Cap->G_b", g_b_cap);
364
365        Log::printLine(cell_name + "->Cap->D=" + (String) d_cap);
366        Log::printLine(cell_name + "->Cap->D_b=" + (String) d_b_cap);
367        Log::printLine(cell_name + "->Cap->Q_i=" + (String) q_i_cap);
368        Log::printLine(cell_name + "->Cap->Q_b=" + (String) q_b_cap);
369        Log::printLine(cell_name + "->Cap->Q=" + (String) q_cap);
370        Log::printLine(cell_name + "->Cap->G=" + (String) g_cap);
371        Log::printLine(cell_name + "->Cap->G_b=" + (String) g_b_cap);
372        // --------------------------------------------------------------------
373
374        // --------------------------------------------------------------------
375        // Build Internal Delay Model
376        // --------------------------------------------------------------------
377        double q_ron = getDriver("INV3_RonZN")->getOutputRes();
378
379        double d_to_q_delay = getDriver("INV1_RonZN")->calculateDelay() +
380                                getDriver("INVZ1_RonZN")->calculateDelay() +
381                                getDriver("INV2_RonZN")->calculateDelay() +
382                                getDriver("INV3_RonZN")->calculateDelay();
383        double g_to_q_delay = getDriver("INV4_RonZN")->calculateDelay() +
384                                getDriver("INVZ1_RonZN")->calculateDelay() +
385                                getDriver("INV2_RonZN")->calculateDelay() +
386                                getDriver("INV3_RonZN")->calculateDelay();
387
388        cache->set(cell_name + "->DriveRes->Q", q_ron);
389        cache->set(cell_name + "->Delay->D_to_Q", d_to_q_delay);
390        cache->set(cell_name + "->Delay->G_to_Q", g_to_q_delay);
391        Log::printLine(cell_name + "->DriveRes->Q=" + (String) q_ron);
392        Log::printLine(cell_name + "->Delay->D_to_Q=" + (String) d_to_q_delay);
393        Log::printLine(cell_name + "->Delay->G_to_Q=" + (String) g_to_q_delay);
394
395        return;
396        // --------------------------------------------------------------------
397
398    }
399
400} // namespace DSENT
401
402