1 /* Copyright (c) 2012 Massachusetts Institute of Technology
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 #include "model/electrical/Decoder.h"
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"
36 Decoder::Decoder(const String
& instance_name_
, const TechModel
* tech_model_
)
37 : ElectricalModel(instance_name_
, tech_model_
)
46 void Decoder::initParameters()
48 addParameterName("NumberOutputs");
51 void Decoder::initProperties()
56 Decoder
* Decoder::clone() const
62 void Decoder::constructModel()
65 unsigned int number_outputs
= getParameter("NumberOutputs").toUInt();
67 ASSERT(number_outputs
> 0, "[Error] " + getInstanceName() + " -> Number of outputs must be > 0!");
69 unsigned int number_addr_bits
= (unsigned int)ceil(log2(number_outputs
));
72 for(unsigned int i
= 0; i
< number_addr_bits
; ++i
)
74 createInputPort("Addr" + (String
)i
);
76 for(unsigned int i
= 0; i
< number_outputs
; ++i
)
78 createOutputPort("Out" + (String
)i
);
81 // Create energy, power, and area results
82 createElectricalResults();
83 createElectricalEventResult("Decode");
84 Result
* decode_event
= getEventResult("Decode");
86 getEventInfo("Idle")->setStaticTransitionInfos();
88 if(number_addr_bits
== 0)
90 // Do not need a decoder
92 else if(number_addr_bits
== 1)
94 const String
& inv0_name
= "Inv0";
96 StdCell
* inv0
= getTechModel()->getStdCellLib()->createStdCell("INV", inv0_name
);
99 // Connect inputs and outputs
100 portConnect(inv0
, "A", "Addr0");
101 portConnect(inv0
, "Y", "Out0");
102 assign("Out1", "Addr0");
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);
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);
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
);
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
)
123 nand2_names
[i
] = "NAND2_" + (String
)i
;
124 inv_names
[i
] = "INV_" + (String
)i
;
127 Decoder
* dec0
= new Decoder(dec0_name
, getTechModel());
128 dec0
->setParameter("NumberOutputs", number_outputs_0
);
131 Decoder
* dec1
= new Decoder(dec1_name
, getTechModel());
132 dec1
->setParameter("NumberOutputs", number_outputs_1
);
135 vector
<StdCell
*> nand2s(number_outputs
, NULL
);
136 vector
<StdCell
*> invs(number_outputs
, NULL
);
137 for(unsigned int i
= 0; i
< number_outputs
; ++i
)
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();
145 // Connect inputs and outputs
146 for(unsigned int i
= 0; i
< number_addr_bits_0
; ++i
)
148 portConnect(dec0
, "Addr" + (String
)i
, "Addr" + (String
)i
);
150 for(unsigned int i
= 0; i
< number_addr_bits_1
; ++i
)
152 portConnect(dec1
, "Addr" + (String
)i
, "Addr" + (String
)(i
+ number_addr_bits_0
));
154 for(unsigned int i
= 0; i
< number_outputs_0
; ++i
)
156 createNet("way0Out" + (String
)i
);
157 portConnect(dec0
, "Out" + (String
)i
, "way0Out" + (String
)i
);
159 for(unsigned int i
= 0; i
< number_outputs_1
; ++i
)
161 createNet("way1Out" + (String
)i
);
162 portConnect(dec1
, "Out" + (String
)i
, "way1Out" + (String
)i
);
165 for(unsigned int i
= 0; i
< number_outputs
; ++i
)
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
);
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
)
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);
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);
196 void Decoder::propagateTransitionInfo()
198 // The only thing can be updated are the input probabilities
199 unsigned int number_outputs
= getParameter("NumberOutputs").toUInt();
201 unsigned int number_addr_bits
= (unsigned int)ceil(log2(number_outputs
));
203 if(number_addr_bits
== 0)
205 // Do not need a decoder
207 else if(number_addr_bits
== 1)
209 ElectricalModel
* inv0
= (ElectricalModel
*)getSubInstance("Inv0");
210 propagatePortTransitionInfo(inv0
, "A", "Addr0");
213 // Since # addr bits is 1, the output 0 is directly connected
214 propagatePortTransitionInfo("Out0", inv0
, "Y");
215 propagatePortTransitionInfo("Out1", "Addr0");
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);
222 unsigned int number_outputs_0
= (unsigned int)pow(2.0, number_addr_bits_0
);
224 // Update decoders with probabilities
225 ElectricalModel
* dec0
= (ElectricalModel
*)getSubInstance("Dec_way0");
226 for(unsigned int i
= 0; i
< number_addr_bits_0
; ++i
)
228 propagatePortTransitionInfo(dec0
, "Addr" + (String
)i
, "Addr" + (String
)i
);
231 ElectricalModel
* dec1
= (ElectricalModel
*)getSubInstance("Dec_way1");
232 for(unsigned int i
= 0; i
< number_addr_bits_1
; ++i
)
234 propagatePortTransitionInfo(dec1
, "Addr" + (String
)i
, "Addr" + (String
)(i
+ number_addr_bits_0
));
238 for(unsigned int i
= 0; i
< number_outputs
; ++i
)
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
)));
245 ElectricalModel
* inv
= (ElectricalModel
*)getSubInstance("INV_" + (String
)i
);
246 propagatePortTransitionInfo(inv
, "A", nand2
, "Y");
249 propagatePortTransitionInfo("Out" + (String
)i
, inv
, "Y");