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/electrical/Decoder.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/StdCell.h"
31
32namespace DSENT
33{
34    using std::ceil;
35
36    Decoder::Decoder(const String& instance_name_, const TechModel* tech_model_)
37        : ElectricalModel(instance_name_, tech_model_)
38    {
39        initParameters();
40        initProperties();
41    }
42
43    Decoder::~Decoder()
44    {}
45
46    void Decoder::initParameters()
47    {
48        addParameterName("NumberOutputs");
49    }
50
51    void Decoder::initProperties()
52    {
53        return;
54    }
55
56    Decoder* Decoder::clone() const
57    {
58        // TODO
59        return NULL;
60    }
61
62    void Decoder::constructModel()
63    {
64        // Get parameters
65        unsigned int number_outputs = getParameter("NumberOutputs").toUInt();
66
67        ASSERT(number_outputs > 0, "[Error] " + getInstanceName() + " -> Number of outputs must be > 0!");
68
69        unsigned int number_addr_bits = (unsigned int)ceil(log2(number_outputs));
70
71        // Create ports
72        for(unsigned int i = 0; i < number_addr_bits; ++i)
73        {
74            createInputPort("Addr" + (String)i);
75        }
76        for(unsigned int i = 0; i < number_outputs; ++i)
77        {
78            createOutputPort("Out" + (String)i);
79        }
80
81        // Create energy, power, and area results
82        createElectricalResults();
83        createElectricalEventResult("Decode");
84        Result* decode_event = getEventResult("Decode");
85
86        getEventInfo("Idle")->setStaticTransitionInfos();
87
88        if(number_addr_bits == 0)
89        {
90            // Do not need a decoder
91        }
92        else if(number_addr_bits == 1)
93        {
94            const String& inv0_name = "Inv0";
95
96            StdCell* inv0 = getTechModel()->getStdCellLib()->createStdCell("INV", inv0_name);
97            inv0->construct();
98
99            // Connect inputs and outputs
100            portConnect(inv0, "A", "Addr0");
101            portConnect(inv0, "Y", "Out0");
102            assign("Out1", "Addr0");
103
104            // Add area, power, and event results
105            addSubInstances(inv0, 1.0);
106            addElectricalSubResults(inv0, 1.0);
107            decode_event->addSubResult(inv0->getEventResult("INV"), inv0_name, 1.0);
108        }
109        else
110        {
111            unsigned int number_addr_bits_0 = (unsigned int)ceil((double)number_addr_bits / 2.0);
112            unsigned int number_addr_bits_1 = (unsigned int)floor((double)number_addr_bits / 2.0);
113
114            unsigned int number_outputs_0 = (unsigned int)pow(2.0, number_addr_bits_0);
115            unsigned int number_outputs_1 = (unsigned int)ceil((double)number_outputs / (double)number_outputs_0);
116
117            const String& dec0_name = "Dec_way0";
118            const String& dec1_name = "Dec_way1";
119            vector<String> nand2_names(number_outputs, "");
120            vector<String> inv_names(number_outputs, "");
121            for(unsigned int i = 0; i < number_outputs; ++i)
122            {
123                nand2_names[i] = "NAND2_" + (String)i;
124                inv_names[i] = "INV_" + (String)i;
125            }
126
127            Decoder* dec0 = new Decoder(dec0_name, getTechModel());
128            dec0->setParameter("NumberOutputs", number_outputs_0);
129            dec0->construct();
130
131            Decoder* dec1 = new Decoder(dec1_name, getTechModel());
132            dec1->setParameter("NumberOutputs", number_outputs_1);
133            dec1->construct();
134
135            vector<StdCell*> nand2s(number_outputs, NULL);
136            vector<StdCell*> invs(number_outputs, NULL);
137            for(unsigned int i = 0; i < number_outputs; ++i)
138            {
139                nand2s[i] = getTechModel()->getStdCellLib()->createStdCell("NAND2", nand2_names[i]);
140                nand2s[i]->construct();
141                invs[i] = getTechModel()->getStdCellLib()->createStdCell("INV", inv_names[i]);
142                invs[i]->construct();
143            }
144
145            // Connect inputs and outputs
146            for(unsigned int i = 0; i < number_addr_bits_0; ++i)
147            {
148                portConnect(dec0, "Addr" + (String)i, "Addr" + (String)i);
149            }
150            for(unsigned int i = 0; i < number_addr_bits_1; ++i)
151            {
152                portConnect(dec1, "Addr" + (String)i, "Addr" + (String)(i + number_addr_bits_0));
153            }
154            for(unsigned int i = 0; i < number_outputs_0; ++i)
155            {
156                createNet("way0Out" + (String)i);
157                portConnect(dec0, "Out" + (String)i, "way0Out" + (String)i);
158            }
159            for(unsigned int i = 0; i < number_outputs_1; ++i)
160            {
161                createNet("way1Out" + (String)i);
162                portConnect(dec1, "Out" + (String)i, "way1Out" + (String)i);
163            }
164
165            for(unsigned int i = 0; i < number_outputs; ++i)
166            {
167                createNet("nand" + (String)i + "Out");
168                portConnect(nand2s[i], "A", "way0Out" + (String)(i%number_outputs_0));
169                portConnect(nand2s[i], "B", "way1Out" + (String)((unsigned int)floor(i/number_outputs_0)));
170                portConnect(nand2s[i], "Y", "nand" + (String)i + "Out");
171                portConnect(invs[i], "A", "nand" + (String)i + "Out");
172                portConnect(invs[i], "Y", "Out" + (String)i);
173            }
174
175            // Add area, power, and event results
176            addSubInstances(dec0, 1.0);
177            addElectricalSubResults(dec0, 1.0);
178            decode_event->addSubResult(dec0->getEventResult("Decode"), dec0_name, 1.0);
179            addSubInstances(dec1, 1.0);
180            addElectricalSubResults(dec1, 1.0);
181            decode_event->addSubResult(dec1->getEventResult("Decode"), dec1_name, 1.0);
182            for(unsigned int i = 0; i < number_outputs; ++i)
183            {
184                addSubInstances(nand2s[i], 1.0);
185                addElectricalSubResults(nand2s[i], 1.0);
186                decode_event->addSubResult(nand2s[i]->getEventResult("NAND2"), nand2_names[i], 1.0);
187
188                addSubInstances(invs[i], 1.0);
189                addElectricalSubResults(invs[i], 1.0);
190                decode_event->addSubResult(invs[i]->getEventResult("INV"), inv_names[i], 1.0);
191            }
192        }
193        return;
194    }
195
196    void Decoder::propagateTransitionInfo()
197    {
198        // The only thing can be updated are the input probabilities
199        unsigned int number_outputs = getParameter("NumberOutputs").toUInt();
200
201        unsigned int number_addr_bits = (unsigned int)ceil(log2(number_outputs));
202
203        if(number_addr_bits == 0)
204        {
205            // Do not need a decoder
206        }
207        else if(number_addr_bits == 1)
208        {
209            ElectricalModel* inv0 = (ElectricalModel*)getSubInstance("Inv0");
210            propagatePortTransitionInfo(inv0, "A", "Addr0");
211            inv0->use();
212
213            // Since # addr bits is 1, the output 0 is directly connected
214            propagatePortTransitionInfo("Out0", inv0, "Y");
215            propagatePortTransitionInfo("Out1", "Addr0");
216        }
217        else
218        {
219            unsigned int number_addr_bits_0 = (unsigned int)ceil((double)number_addr_bits / 2.0);
220            unsigned int number_addr_bits_1 = (unsigned int)floor((double)number_addr_bits / 2.0);
221
222            unsigned int number_outputs_0 = (unsigned int)pow(2.0, number_addr_bits_0);
223
224            // Update decoders with probabilities
225            ElectricalModel* dec0 = (ElectricalModel*)getSubInstance("Dec_way0");
226            for(unsigned int i = 0; i < number_addr_bits_0; ++i)
227            {
228                propagatePortTransitionInfo(dec0, "Addr" + (String)i, "Addr" + (String)i);
229            }
230            dec0->use();
231            ElectricalModel* dec1 = (ElectricalModel*)getSubInstance("Dec_way1");
232            for(unsigned int i = 0; i < number_addr_bits_1; ++i)
233            {
234                propagatePortTransitionInfo(dec1, "Addr" + (String)i, "Addr" + (String)(i + number_addr_bits_0));
235            }
236            dec1->use();
237
238            for(unsigned int i = 0; i < number_outputs; ++i)
239            {
240                ElectricalModel* nand2 = (ElectricalModel*)getSubInstance("NAND2_" + (String)i);
241                propagatePortTransitionInfo(nand2, "A", dec0, "Out" + (String)(i%number_outputs_0));
242                propagatePortTransitionInfo(nand2, "B", dec1, "Out" + (String)((unsigned int)floor(i/number_outputs_0)));
243                nand2->use();
244
245                ElectricalModel* inv = (ElectricalModel*)getSubInstance("INV_" + (String)i);
246                propagatePortTransitionInfo(inv, "A", nand2, "Y");
247                inv->use();
248
249                propagatePortTransitionInfo("Out" + (String)i, inv, "Y");
250            }
251        }
252        return;
253    }
254
255} // namespace DSENT
256
257