Go to the documentation of this file.
37 #include <boost/format.hpp>
47 #include <unordered_map>
48 #include <unordered_set>
50 #include <type_traits>
100 class UnsignedVariable;
101 class IntegerConstant;
102 class CastLogicToUnsigned;
103 class CastLogicToSigned;
104 class BinaryConstant;
125 virtual void hdl(std::ostream& stream,
Language lang,
int level) {
130 std::string(
"Attempted to generate virtual class");
131 throw std::runtime_error(err);
164 void hdl(std::ostream& stream,
Language lang,
int level)
override {
166 throw std::runtime_error(__PRETTY_FUNCTION__);
168 for (
auto&& line :
impl_) {
195 throw std::runtime_error(__PRETTY_FUNCTION__);
228 stream <<
name() <<
" : integer";
230 stream <<
"parameter integer " <<
name();
232 throw std::runtime_error(__PRETTY_FUNCTION__);
254 std::string binVal =
"";
256 for (
int i =
width_ - 1; i >= 0; --i) {
257 long power =
static_cast<long>(std::pow(2, i));
258 if (power <= tempVal) {
268 <<
" : std_logic_vector(" <<
width_ - 1
269 <<
" downto 0) := \"" << binVal <<
"\";\n";
272 <<
"localparam [" <<
width_-1 <<
":0] "
273 <<
name() <<
" = " <<
width_ <<
"'b" << binVal <<
";\n";
275 throw std::runtime_error(__PRETTY_FUNCTION__);
299 <<
" : integer := " <<
value() <<
";\n";
304 throw std::runtime_error(__PRETTY_FUNCTION__);
330 stream <<
"std_logic_vector("
331 << std::to_string(
width_ - 1)
334 stream <<
"std_logic_vector(" <<
strWidth_
335 <<
"-1 downto 0);\n";
338 stream <<
"std_logic;\n";
342 if (width_ < 0 || width_ > 1) {
344 stream <<
"[" << std::to_string(
width_ - 1) <<
":0] ";
349 stream <<
name() <<
";\n";
351 throw std::runtime_error(__PRETTY_FUNCTION__);
373 void hdl(std::ostream& stream,
Language lang,
int level)
override {
379 std::string def =
"-";
381 def =
value_.substr(0, 1);
383 std::string assign =
" <= ";
391 stream << delim << def << delim <<
";\n";
393 stream <<
"(others => '" << def <<
"');\n";
396 std::string def =
"x";
398 def =
value_.substr(0, 1);
401 <<
name() <<
" = 'b" << def <<
";\n";
403 throw std::runtime_error(__PRETTY_FUNCTION__);
431 <<
" " <<
name() <<
";\n";
433 throw std::runtime_error(__PRETTY_FUNCTION__);
442 return "(" +
width +
"-1 downto 0)";
450 return "[" +
width +
"-1:0] ";
454 throw std::runtime_error(__PRETTY_FUNCTION__);
458 std::string decl =
"reg";
528 std::string decl =
"reg signed";
557 void hdl(std::ostream& stream,
Language lang,
int level)
override {
559 throw std::runtime_error(
560 "assigning to register '" +
name() +
561 "' is only allowed in synchronous context");
563 !parentIs<Asynchronous>())) {
564 throw std::runtime_error(
"Not allowed to assign to '" +
565 name() +
"' in this context.");
573 stream <<
"(" <<
index_ <<
")";
581 if (!(parentIs<Synchronous>() || parentIs<Asynchronous>())) {
590 stream <<
"[" <<
index_ <<
"]";
598 throw std::runtime_error(__PRETTY_FUNCTION__);
628 void hdl(std::ostream& stream,
Language lang,
int level)
override {
637 throw std::runtime_error(__PRETTY_FUNCTION__);
651 template<
typename SS>
653 std::shared_ptr<SequentialStatement> ptr = std::make_shared<SS>(op);
683 void hdl(std::ostream& stream,
Language lang,
int level)
override {
689 std::string separator =
"";
691 stream << separator << std::to_string(c);
695 std::string separator =
"";
702 throw std::runtime_error(
"Case has no case");
711 std::string separator =
"";
713 stream << separator << std::to_string(c);
717 std::string separator =
"";
724 throw std::runtime_error(
"Case has no case");
726 stream <<
": begin\n";
730 throw std::runtime_error(__PRETTY_FUNCTION__);
761 void hdl(std::ostream& stream,
Language lang,
int level)
override {
775 throw std::runtime_error(__PRETTY_FUNCTION__);
788 template<
typename SS>
791 std::shared_ptr<SequentialStatement> ptr
792 = std::make_shared<SS>(ifBlock);
796 template<
typename SS>
798 std::shared_ptr<SequentialStatement> ptr
799 = std::make_shared<SS>(ifBlock);
803 template<
typename SS>
806 throw std::runtime_error(
"Cannot to add a second else block.");
808 std::shared_ptr<SequentialStatement> ptr
809 = std::make_shared<SS>(elseBlock);
823 iter->first.hdl(stream, lang);
825 iter->second->hdl(stream, lang, level + 1);
840 iter->first.hdl(stream, lang);
841 stream <<
") begin\n";
842 iter->second->hdl(stream, lang, level + 1);
850 throw std::runtime_error(__PRETTY_FUNCTION__);
858 block.second->setParent(
this);
859 block.second->build();
868 std::vector<std::pair<LHSValue,std::shared_ptr<SequentialStatement> > >
880 template<
typename SS>
882 std::shared_ptr<SequentialStatement> ptr = std::make_shared<SS>(cc);
886 void hdl(std::ostream& stream,
Language lang,
int level)
override {
892 throw std::runtime_error(__PRETTY_FUNCTION__);
905 template<
typename SS>
907 std::shared_ptr<SequentialStatement> ptr = std::make_shared<SS>(op);
912 template<
typename Var>
914 std::shared_ptr<Variable> ptr = std::make_shared<Var>(op);
918 virtual void reads(
const std::string& var)
override {
920 if (
parent() !=
nullptr) {
925 virtual void build()
override;
932 std::string separator =
"";
935 stream << separator << r;
941 v->declare(stream, lang, level + 1);
966 stream <<
" begin\n";
970 throw std::runtime_error(__PRETTY_FUNCTION__);
986 template<
typename SS>
988 std::shared_ptr<SequentialStatement> ptr = std::make_shared<SS>(op);
993 template<
typename Var>
995 std::shared_ptr<Variable> ptr = std::make_shared<Var>(op);
999 virtual void build()
override;
1001 virtual void writes(
const std::string& var)
override {
1007 std::cerr <<
"Trying to write nonregister " << var <<
"\n";
1008 throw std::runtime_error(__PRETTY_FUNCTION__);
1022 reg.
reset(stream, lang, level + 1);
1034 stream <<
"(clk, rstx)\n";
1036 stream <<
"(clk)\n";
1040 v->declare(stream, lang, level + 1);
1047 <<
"elsif clk = '1' and clk'event then\n";
1051 <<
"if clk = '1' and clk'event then\n";
1059 <<
"end process " <<
name() <<
";\n";
1065 <<
"always @(posedge clk or negedge rstx) begin\n";
1068 <<
"always @(posedge clk) begin\n";
1071 <<
"if (~rstx) begin\n";
1076 reg.
reset(stream, lang, level + 2);
1084 throw std::runtime_error(__PRETTY_FUNCTION__);
1152 forAll([&](std::shared_ptr<Generatable> c) {
1153 c->hdl(stream, lang, level);
1176 for (
auto&& p : info.
ports) {
1180 *
this <<
Port(p.name, dir, p.left +
"+1");
1182 *
this <<
Port(p.name, dir);
1188 auto now = std::chrono::system_clock::now();
1189 auto now_c = std::chrono::system_clock::to_time_t(now);
1191 std::stringstream ss;
1192 std::strftime(buffer, 30,
"%c", std::localtime(&now_c));
1195 "Module generated by TTA Codesign Environment");
1198 std::string(
"Generated on ") + ss.str());
1279 if (r.name() ==
name) {
1288 if (r.name() ==
name) {
1293 if (r.name() ==
name) {
1302 if (v->name() ==
name) {
1311 if (v->name() == var->name()) {
1312 throw std::runtime_error(
"tried to register variable " +
1313 var->name() +
" multiple times");
1321 if (v.name() ==
name) {
1326 if (v.name() ==
name) {
1331 if (v.name() ==
name) {
1335 for (
auto&& v :
wires_) {
1336 if (v->name() ==
name) {
1341 if (v->name() ==
name) {
1345 for (
auto&& v :
ports_) {
1346 if (v.name() ==
name) {
1351 throw std::runtime_error(
"Couldn't find width for " +
name);
1355 for (
auto&& v :
ports_) {
1356 if (v.name() ==
name) {
1357 return v.wireType();
1360 for (
auto&& v :
wires_) {
1361 if (v->name() ==
name) {
1362 return v->wireType();
1366 if (v->name() ==
name) {
1367 return v->wireType();
1371 throw std::runtime_error(
"Couldn't find wire type for " +
name);
1374 void reads(
const std::string& var)
final { (void)var; }
1376 void writes(
const std::string& var)
final { (void)var; }
1381 <<
name() <<
" is\n";
1384 std::string separator =
"";
1388 parameter.declare(stream, lang);
1395 std::string separator =
"";
1397 for (
auto&& port :
ports_) {
1399 port.declare(stream, lang);
1409 throw std::runtime_error(__PRETTY_FUNCTION__);
1414 std::string instance =
prefix_ +
"_" + std::to_string(
id_);
1417 << instance <<
" : " <<
name() <<
"\n";
1421 std::string separator =
"";
1425 << p.name() <<
" => " << p.strValue();
1430 std::string separator =
"";
1432 for (
auto&& p :
ports_) {
1434 << p.name() <<
" => ";
1435 if (p.name() ==
"clk" || p.name() ==
"rstx" ||
1436 p.name() ==
"glock_in") {
1439 stream << instance <<
"_" << p.name();
1448 std::string separator =
"";
1450 stream << separator;
1451 stream <<
"." << p.name() <<
"(" << p.strValue() <<
")";
1456 std::string separator =
"";
1457 stream << instance <<
" (\n";
1458 for (
auto&& p :
ports_) {
1459 stream << separator;
1462 if (p.name() ==
"clk" || p.name() ==
"rstx" ||
1463 p.name() ==
"glock_in") {
1464 stream << p.name() <<
")";
1466 stream << instance <<
"_" <<
name() <<
")";
1472 throw std::runtime_error(__PRETTY_FUNCTION__);
1483 stream << ident <<
"-- " << line <<
"\n";
1486 stream << ident <<
"\n"
1487 << ident <<
"library ieee;\n"
1488 << ident <<
"use ieee.std_logic_1164.all;\n"
1489 << ident <<
"use ieee.numeric_std.all;\n"
1490 << ident <<
"use ieee.std_logic_misc.all;\n"
1493 << ident <<
"entity " <<
name() <<
" is\n";
1496 std::string separator =
"";
1499 stream << separator;
1500 parameter.declare(stream, lang, level + 2);
1507 std::string separator =
"";
1509 for (
auto&& port :
ports_) {
1510 stream << separator;
1511 port.declare(stream, lang, level + 2);
1516 stream << ident <<
"end entity " <<
name() <<
";\n"
1519 << ident <<
"architecture rtl of "
1520 <<
name() <<
" is\n";
1526 c.declare(stream, lang, level + 1);
1529 c.declare(stream, lang, level + 1);
1535 for (
auto&& w :
wires_) {
1536 w->declare(stream, lang, level + 1);
1543 r.declare(stream, lang, level + 1);
1546 std::vector<std::string> declared;
1548 if (std::find(declared.begin(), declared.end(),
1549 m.name()) != declared.end()) {
1553 m.declare(stream, lang, level + 1);
1554 declared.emplace_back(m.name());
1559 m.instantiate(stream, lang, level + 1);
1564 b->behaviour(stream, lang, level + 1);
1568 <<
"end architecture rtl;\n\n";
1575 <<
" * " << line <<
"\n";
1583 std::string separator =
"";
1586 stream << separator;
1587 parameter.declare(stream, lang, level + 2);
1594 std::string separator =
"";
1596 for (
auto&& port :
ports_) {
1597 stream << separator;
1598 port.declare(stream, lang, level + 2);
1610 c.declare(stream, lang, level + 1);
1613 c.declare(stream, lang, level + 1);
1619 for (
auto&& w :
wires_) {
1620 w->declare(stream, lang, level + 1);
1626 v->declare(stream, lang, level + 1);
1634 r.declare(stream, lang, level + 1);
1639 m.instantiate(stream, lang, level + 1);
1644 b->behaviour(stream, lang, level + 1);
1650 throw std::runtime_error(__PRETTY_FUNCTION__);
1661 if (r.name() == var) {
1665 throw std::runtime_error(
"Couldn't find register '" + var +
"'");
1675 return l.name() == r.name();
std::vector< std::shared_ptr< Behaviour > > behaviours_
virtual void writes(const std::string &var)
virtual bool isConstant(const std::string &name) final
SignedVariable(std::string name, std::string width)
void pushComponent(std::shared_ptr< Generatable > c)
std::shared_ptr< SequentialStatement > elseBlock_
void behaviour(std::ostream &stream, Language lang, int level)
void elseIfClause(LHSValue cls, SS ifBlock)
virtual void build() override
virtual void hdl(std::ostream &stream, Language lang, int level) override
HDLOperation(std::string name, std::deque< std::string > impl, Language lang)
Module & operator<<(Module &rhs)
std::vector< std::shared_ptr< Variable > > variables_
virtual void vhdlReset(std::ostream &stream, Language lang, int level)
BinaryConstant(std::string name, int width, int value)
Module & operator<<(BinaryConstant &&constant)
std::vector< IntegerConstant > constants_
std::vector< Module > modules_
void appendToHeader(const std::string &line)
std::string verilogRange()
Module & operator<<(Register ®)
std::vector< std::shared_ptr< Variable > > variables_
virtual ~Behaviour()=default
void declare(std::ostream &stream, Language lang, int level)
void set_prefix(std::string prefix)
void declare(std::ostream &stream, Language lang, int level=0)
void writes(const std::string &var) final
virtual void build() override
void elseClause(SS elseBlock)
Variable(std::string name, int width=1)
DefaultCase & operator<<(DefaultAssign &rhs)
DefaultAssign(std::string name)
ResetOption resetOption() const noexcept
void hdl(std::ostream &stream, Language lang, int level) override
virtual void hdl(std::ostream &stream, Language lang, int level) override
Case & operator<<(std::string &rhs)
void addComponent(Component c)
Wire(std::string name, int width=1, WireType wt=WireType::Auto)
void hdl(std::ostream &stream, Language lang, int level) override
std::vector< Port > ports
virtual Register & getRegister(const std::string &var)
std::vector< Port > ports_
Behaviour & operator<<(Synchronous &&rhs)
std::unordered_set< std::string > registers_
Case & operator<<(BinaryLiteral &&rhs)
Case(std::string stringCase)
Module & operator<<(Option &&opt)
Width width(const std::string &name) final
Assign(std::string var, LHSValue value)
DefaultCase & operator<<(DefaultAssign &&rhs)
Behaviour & operator<<(Asynchronous &rhs)
virtual bool hasOption(const std::string &var)
virtual void writes(const std::string &var) override
std::vector< std::shared_ptr< Variable > > variables_
Module & operator<<(Behaviour &&rhs)
virtual void hdl(std::ostream &stream, Language lang, int level)
virtual std::string vhdlTypeDeclaration()
Module & operator<<(Wire &&wire)
std::unordered_set< std::string > options_
void hdl(std::ostream &stream, Language lang, int level) override
std::deque< std::string > impl_
Synchronous & operator<<(SS op)
void hdl(std::ostream &stream, Language lang, int level) final
UnsignedVariable(std::string name, std::string width)
void reads(const std::string &var) final
std::vector< std::string > headerComment_
int value() const noexcept
Assign(std::string var, LHSValue value, int ub, int lb)
virtual void reads(const std::string &var) override
std::vector< BinaryLiteral > binaryCases_
void declare(std::ostream &stream, Language lang, int level)
virtual bool isConstant(const std::string &name)
void instantiate(std::ostream &stream, Language lang, int level)
void registerVariable(const std::shared_ptr< Variable > var)
Variable(std::string name, std::string width)
virtual bool isRegister(const std::string &name)
Module & operator<<(IntegerConstant &&constant)
std::string vhdlTypeDeclaration()
Parameter(std::string name, std::string value)
virtual void hdl(std::ostream &stream, Language lang, int level) override
virtual bool isVariable(const std::string &name) final
Generatable * parent() const noexcept
Behaviour & operator<<(NewLine &&rhs)
int value() const noexcept
void declare(std::ostream &stream, Language lang, int ident)
Module & operator<<(Module &&rhs)
RawCodeLine(std::string vhdl, std::string verilog)
Parameter(std::string name, int value=-1)
Case & operator<<(BinaryLiteral &rhs)
bool hasOption(const std::string &var) final
LogicVariable(std::string name, int width=1)
void implement(std::ostream &stream, Language lang, int level=0)
SequentialStatement(std::string name)
virtual void implementAll(std::ostream &stream, Language lang)
std::string verilogTypeDeclaration()
void hdl(std::ostream &stream, Language lang, int level) final
virtual void reads(const std::string &var)
Module & operator<<(Behaviour &rhs)
virtual bool isRegister(const std::string &name) final
Behaviour & operator<<(Synchronous &rhs)
std::string vhdlTypeDeclaration()
Register & getRegister(const std::string &var) final
WireType wireType(const std::string &name) final
std::vector< Register > registers_
std::vector< std::pair< LHSValue, std::shared_ptr< SequentialStatement > > > ifBlocks_
std::vector< std::shared_ptr< Wire > > wires_
Case & operator<<(std::string &&rhs)
HDLOperation & operator<<(const std::string &&rhs)
Behaviour & operator<<(NewLine &rhs)
void declare(std::ostream &stream, Language lang, int level)
void addCase(DefaultCase rhs)
std::string verilogTypeDeclaration()
find Finds info of the inner loops in the false
virtual bool isVariable(const std::string &name)
Case & operator<<(int rhs)
UnsignedVariable(std::string name, int width=1)
Module & operator<<(Port &&port)
void hdl(std::ostream &stream, Language lang, int level)
std::vector< Parameter > parameters_
void hdl(std::ostream &stream, Language lang, int level) override
If(LHSValue cls, SS ifBlock)
const std::string & name() const noexcept
Asynchronous(const std::string &name)
virtual WireType wireType() const
Behaviour & operator<<(Assign &assignment)
std::unordered_set< std::string > readList_
HDLOperation & operator<<(const std::string &rhs)
Module & operator<<(Register &®)
Behaviour & operator<<(Assign &&assignment)
void hdl(std::ostream &stream, Language lang, int level) override
Module & operator<<(Parameter &¶m)
void hdl(std::ostream &stream, Language lang, int level) override
std::vector< BinaryConstant > binaryConstants_
IntegerConstant(std::string name, int value)
LogicVariable(std::string name, std::string width)
std::string vhdlTypeDeclaration()
DefaultAssign(std::string name, std::string value)
SignedVariable(std::string name, int width=1)
Asynchronous & operator<<(SS op)
Assign(std::string var, LHSValue value, int idx)
Synchronous(std::string name)
std::vector< std::string > readList_
Wire(std::string name, std::string width)
Module(ipxact::ModuleInfo info, int id)
WireType wireType() const final
std::vector< Parameter > parameters
void reset(std::ostream &stream, Language lang, int ident)
void declare(std::ostream &stream, Language lang, int level)
void hdl(std::ostream &stream, Language lang, int level) override
std::vector< int > intCases_