Decoder.cc revision 10448:bc1a3b7ab5ef
16145SN/A/* Copyright (c) 2012 Massachusetts Institute of Technology
28683SN/A *
38683SN/A * Permission is hereby granted, free of charge, to any person obtaining a copy
46145SN/A * of this software and associated documentation files (the "Software"), to deal
56145SN/A * in the Software without restriction, including without limitation the rights
66145SN/A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76145SN/A * copies of the Software, and to permit persons to whom the Software is
86145SN/A * furnished to do so, subject to the following conditions:
96145SN/A *
106145SN/A * The above copyright notice and this permission notice shall be included in
116145SN/A * all copies or substantial portions of the Software.
126145SN/A *
136145SN/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
146145SN/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
156145SN/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
166145SN/A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176145SN/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
186145SN/A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
196145SN/A * THE SOFTWARE.
206145SN/A */
216145SN/A
226145SN/A#include "model/electrical/Decoder.h"
236145SN/A
246145SN/A#include <cmath>
256145SN/A
266145SN/A#include "model/PortInfo.h"
276145SN/A#include "model/EventInfo.h"
286145SN/A#include "model/TransitionInfo.h"
296145SN/A#include "model/std_cells/StdCellLib.h"
308683SN/A#include "model/std_cells/StdCell.h"
3110301Snilay@cs.wisc.edu
328683SN/Anamespace DSENT
338683SN/A{
346145SN/A    using std::ceil;
357055SN/A
367055SN/A    Decoder::Decoder(const String& instance_name_, const TechModel* tech_model_)
377054SN/A        : ElectricalModel(instance_name_, tech_model_)
388683SN/A    {
396145SN/A        initParameters();
408683SN/A        initProperties();
418683SN/A    }
428683SN/A
438683SN/A    Decoder::~Decoder()
448683SN/A    {}
458683SN/A
468683SN/A    void Decoder::initParameters()
4710163SN/A    {
4810163SN/A        addParameterName("NumberOutputs");
498683SN/A    }
508683SN/A
518683SN/A    void Decoder::initProperties()
528683SN/A    {
538683SN/A        return;
5410163SN/A    }
5510163SN/A
568683SN/A    Decoder* Decoder::clone() const
578683SN/A    {
588683SN/A        // TODO
5910163SN/A        return NULL;
608683SN/A    }
6110163SN/A
6210163SN/A    void Decoder::constructModel()
6310163SN/A    {
6410163SN/A        // Get parameters
6510163SN/A        unsigned int number_outputs = getParameter("NumberOutputs").toUInt();
6610163SN/A
6710163SN/A        ASSERT(number_outputs > 0, "[Error] " + getInstanceName() + " -> Number of outputs must be > 0!");
6810163SN/A
6910163SN/A        unsigned int number_addr_bits = (unsigned int)ceil(log2(number_outputs));
708683SN/A
718683SN/A        // Create ports
728683SN/A        for(unsigned int i = 0; i < number_addr_bits; ++i)
738683SN/A        {
748683SN/A            createInputPort("Addr" + (String)i);
759627SN/A        }
768683SN/A        for(unsigned int i = 0; i < number_outputs; ++i)
778683SN/A        {
788683SN/A            createOutputPort("Out" + (String)i);
798683SN/A        }
808683SN/A
818683SN/A        // Create energy, power, and area results
828683SN/A        createElectricalResults();
838683SN/A        createElectricalEventResult("Decode");
848683SN/A        Result* decode_event = getEventResult("Decode");
858683SN/A
868683SN/A        getEventInfo("Idle")->setStaticTransitionInfos();
878683SN/A
8810163SN/A        if(number_addr_bits == 0)
898832SN/A        {
908683SN/A            // Do not need a decoder
918949SN/A        }
928683SN/A        else if(number_addr_bits == 1)
938683SN/A        {
948683SN/A            const String& inv0_name = "Inv0";
958683SN/A
968683SN/A            StdCell* inv0 = getTechModel()->getStdCellLib()->createStdCell("INV", inv0_name);
978683SN/A            inv0->construct();
988683SN/A
998683SN/A            // Connect inputs and outputs
1008683SN/A            portConnect(inv0, "A", "Addr0");
1018683SN/A            portConnect(inv0, "Y", "Out0");
1028683SN/A            assign("Out1", "Addr0");
1038683SN/A
1048683SN/A            // Add area, power, and event results
1058683SN/A            addSubInstances(inv0, 1.0);
1068683SN/A            addElectricalSubResults(inv0, 1.0);
1078683SN/A            decode_event->addSubResult(inv0->getEventResult("INV"), inv0_name, 1.0);
1088683SN/A        }
1098683SN/A        else
11010163SN/A        {
11110163SN/A            unsigned int number_addr_bits_0 = (unsigned int)ceil((double)number_addr_bits / 2.0);
11210163SN/A            unsigned int number_addr_bits_1 = (unsigned int)floor((double)number_addr_bits / 2.0);
11310163SN/A
11410163SN/A            unsigned int number_outputs_0 = (unsigned int)pow(2.0, number_addr_bits_0);
11510163SN/A            unsigned int number_outputs_1 = (unsigned int)ceil((double)number_outputs / (double)number_outputs_0);
11610163SN/A
11710163SN/A            const String& dec0_name = "Dec_way0";
11810163SN/A            const String& dec1_name = "Dec_way1";
11910163SN/A            vector<String> nand2_names(number_outputs, "");
12010163SN/A            vector<String> inv_names(number_outputs, "");
12110163SN/A            for(unsigned int i = 0; i < number_outputs; ++i)
12210163SN/A            {
12310163SN/A                nand2_names[i] = "NAND2_" + (String)i;
12410163SN/A                inv_names[i] = "INV_" + (String)i;
12510163SN/A            }
12610163SN/A
12710163SN/A            Decoder* dec0 = new Decoder(dec0_name, getTechModel());
12810163SN/A            dec0->setParameter("NumberOutputs", number_outputs_0);
12910163SN/A            dec0->construct();
13010163SN/A
13110163SN/A            Decoder* dec1 = new Decoder(dec1_name, getTechModel());
13210163SN/A            dec1->setParameter("NumberOutputs", number_outputs_1);
13310163SN/A            dec1->construct();
13410163SN/A
13510163SN/A            vector<StdCell*> nand2s(number_outputs, NULL);
1368683SN/A            vector<StdCell*> invs(number_outputs, NULL);
1378683SN/A            for(unsigned int i = 0; i < number_outputs; ++i)
13810163SN/A            {
1398683SN/A                nand2s[i] = getTechModel()->getStdCellLib()->createStdCell("NAND2", nand2_names[i]);
1408683SN/A                nand2s[i]->construct();
1418683SN/A                invs[i] = getTechModel()->getStdCellLib()->createStdCell("INV", inv_names[i]);
1428683SN/A                invs[i]->construct();
1438683SN/A            }
1448683SN/A
1458683SN/A            // Connect inputs and outputs
1468683SN/A            for(unsigned int i = 0; i < number_addr_bits_0; ++i)
1478683SN/A            {
1488683SN/A                portConnect(dec0, "Addr" + (String)i, "Addr" + (String)i);
14910163SN/A            }
1508683SN/A            for(unsigned int i = 0; i < number_addr_bits_1; ++i)
1518683SN/A            {
1528683SN/A                portConnect(dec1, "Addr" + (String)i, "Addr" + (String)(i + number_addr_bits_0));
1538683SN/A            }
1548683SN/A            for(unsigned int i = 0; i < number_outputs_0; ++i)
15510163SN/A            {
15610163SN/A                createNet("way0Out" + (String)i);
1578683SN/A                portConnect(dec0, "Out" + (String)i, "way0Out" + (String)i);
1587456SN/A            }
1596145SN/A            for(unsigned int i = 0; i < number_outputs_1; ++i)
1606145SN/A            {
1618683SN/A                createNet("way1Out" + (String)i);
1628683SN/A                portConnect(dec1, "Out" + (String)i, "way1Out" + (String)i);
1636145SN/A            }
1648683SN/A
1658683SN/A            for(unsigned int i = 0; i < number_outputs; ++i)
1668683SN/A            {
1678683SN/A                createNet("nand" + (String)i + "Out");
16810163SN/A                portConnect(nand2s[i], "A", "way0Out" + (String)(i%number_outputs_0));
1698683SN/A                portConnect(nand2s[i], "B", "way1Out" + (String)((unsigned int)floor(i/number_outputs_0)));
1708683SN/A                portConnect(nand2s[i], "Y", "nand" + (String)i + "Out");
1718683SN/A                portConnect(invs[i], "A", "nand" + (String)i + "Out");
1728683SN/A                portConnect(invs[i], "Y", "Out" + (String)i);
1738683SN/A            }
1748683SN/A
1758683SN/A            // Add area, power, and event results
1768683SN/A            addSubInstances(dec0, 1.0);
1778683SN/A            addElectricalSubResults(dec0, 1.0);
1788683SN/A            decode_event->addSubResult(dec0->getEventResult("Decode"), dec0_name, 1.0);
1798683SN/A            addSubInstances(dec1, 1.0);
1808683SN/A            addElectricalSubResults(dec1, 1.0);
1818683SN/A            decode_event->addSubResult(dec1->getEventResult("Decode"), dec1_name, 1.0);
1828683SN/A            for(unsigned int i = 0; i < number_outputs; ++i)
1838683SN/A            {
1848683SN/A                addSubInstances(nand2s[i], 1.0);
1858683SN/A                addElectricalSubResults(nand2s[i], 1.0);
1868683SN/A                decode_event->addSubResult(nand2s[i]->getEventResult("NAND2"), nand2_names[i], 1.0);
1878683SN/A
1888683SN/A                addSubInstances(invs[i], 1.0);
1898683SN/A                addElectricalSubResults(invs[i], 1.0);
1908683SN/A                decode_event->addSubResult(invs[i]->getEventResult("INV"), inv_names[i], 1.0);
1917054SN/A            }
1926145SN/A        }
1937456SN/A        return;
1948683SN/A    }
1956145SN/A
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