mirror of
https://github.com/supleed2/svd2cpp.git
synced 2024-12-22 21:55:49 +00:00
Shortened field builder, added builder for functions
This commit is contained in:
parent
0605e58c05
commit
c05c8fa341
|
@ -1,10 +1,20 @@
|
||||||
#include <Builders.hpp>
|
#include <Builders.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <bitset>
|
||||||
|
|
||||||
void ZeroPointerBuilder::build(std::stringstream& ss) const{
|
void ZeroPointerBuilder::build(std::stringstream& ss) const{
|
||||||
ss << "template <unsigned int zero = 0>\n"
|
ss << "template <unsigned int zero = 0>\n"
|
||||||
"constexpr unsigned int *zeroVal = reinterpret_cast<unsigned int *>(zero);\n";
|
"constexpr unsigned int *zeroVal = reinterpret_cast<unsigned int *>(zero);\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void FieldDefineBuilder::build(std::stringstream& ss) const{
|
||||||
|
ss << "#define __FIELD(_FieldName, _bitOffset, _bitMask, _address) \\\n"
|
||||||
|
"struct _FieldName \\\n"
|
||||||
|
"{ \\\n"
|
||||||
|
" constexpr static inline unsigned int bitOffset() { return _bitOffset; } \\\n"
|
||||||
|
" constexpr static inline unsigned int bitMask() { return _bitMask; } \\\n"
|
||||||
|
" constexpr static inline unsigned int *address() { return zeroVal<> + _address; } \\\n"
|
||||||
|
"};\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeripheralBuilder::build(std::stringstream& ss) const{
|
void PeripheralBuilder::build(std::stringstream& ss) const{
|
||||||
|
@ -29,19 +39,36 @@ unsigned int RegisterBuilder::getRegisterAddress() const{
|
||||||
}
|
}
|
||||||
|
|
||||||
void FieldBuilder::build(std::stringstream& ss) const{
|
void FieldBuilder::build(std::stringstream& ss) const{
|
||||||
ss << " " << "struct " << field.name << "{\n";
|
ss << " " << "__FIELD(" << field.name << ", "
|
||||||
|
<< field.bitOffset << ", "
|
||||||
ss << " " << " " << "constexpr static inline unsigned int bitOffset(){"
|
<< field.bitWidth << ", "
|
||||||
<< "return " << field.bitOffset << ";}\n";
|
<< std::hex << "0x" << getAddress() << std::dec << ")\n";
|
||||||
|
|
||||||
ss << " " << " " << "constexpr static inline unsigned int bitMask(){"
|
|
||||||
<< "return " << field.bitWidth << ";}\n";
|
|
||||||
|
|
||||||
ss << " " << " " << "constexpr static inline unsigned int* address(){"
|
|
||||||
<< "return zeroVal<> + " << getAddress() << ";}\n";
|
|
||||||
ss << " };\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FieldBuilder::getAddress() const{
|
unsigned int FieldBuilder::getAddress() const{
|
||||||
return std::to_string(registerAddress);
|
return registerAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FunctionsBuilder::build(std::stringstream& ss) const{
|
||||||
|
ss << "template<class FIELD>\n"
|
||||||
|
"constexpr inline void set(){\n"
|
||||||
|
" *FIELD::address() |= 1 << FIELD::bitOffset();\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"template<class FIELD, unsigned int VAL>\n"
|
||||||
|
"constexpr inline void set(){\n"
|
||||||
|
" static_assert(VAL & (FIELD::bitMask() >> FIELD::bitOffset()), \"Value is too big\");\n"
|
||||||
|
" *FIELD::address() = *FIELD::address() & ~FIELD::bitMask() | VAL << FIELD::bitOffset();\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"template<class FIELD>\n"
|
||||||
|
"constexpr inline void reset(){\n"
|
||||||
|
" *FIELD::address() &= ~(1 << FIELD::bitOffset());\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"template<class FIELD>\n"
|
||||||
|
"constexpr inline unsigned int read(){\n"
|
||||||
|
" return (*FIELD::address() & FIELD::bitMask()) >> FIELD::bitOffset();\n"
|
||||||
|
"}"
|
||||||
|
"\n\n";
|
||||||
}
|
}
|
|
@ -10,6 +10,10 @@ struct ZeroPointerBuilder : public IBuilder{
|
||||||
void build(std::stringstream& ss) const final;
|
void build(std::stringstream& ss) const final;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FieldDefineBuilder : public IBuilder{
|
||||||
|
void build(std::stringstream& ss) const final;
|
||||||
|
};
|
||||||
|
|
||||||
struct PeripheralBuilder : public IBuilder{
|
struct PeripheralBuilder : public IBuilder{
|
||||||
PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {}
|
PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {}
|
||||||
void build(std::stringstream& ss) const final;
|
void build(std::stringstream& ss) const final;
|
||||||
|
@ -32,10 +36,15 @@ struct FieldBuilder : public IBuilder{
|
||||||
FieldBuilder(const Field& field_, const unsigned int registerAddress_)
|
FieldBuilder(const Field& field_, const unsigned int registerAddress_)
|
||||||
: field(field_), registerAddress(registerAddress_) {}
|
: field(field_), registerAddress(registerAddress_) {}
|
||||||
void build(std::stringstream& ss) const final;
|
void build(std::stringstream& ss) const final;
|
||||||
std::string getAddress() const;
|
unsigned int getAddress() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Field& field;
|
const Field& field;
|
||||||
const unsigned int registerAddress;
|
const unsigned int registerAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FunctionsBuilder : public IBuilder{
|
||||||
|
void build(std::stringstream& ss) const final;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -11,9 +11,11 @@ ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results_,
|
||||||
|
|
||||||
void ClassBuilder::setupBuilders(){
|
void ClassBuilder::setupBuilders(){
|
||||||
builders.push_back(std::make_unique<ZeroPointerBuilder>());
|
builders.push_back(std::make_unique<ZeroPointerBuilder>());
|
||||||
|
builders.push_back(std::make_unique<FieldDefineBuilder>());
|
||||||
for(auto& peripheral : peripherals){
|
for(auto& peripheral : peripherals){
|
||||||
builders.push_back(std::make_unique<PeripheralBuilder>(peripheral));
|
builders.push_back(std::make_unique<PeripheralBuilder>(peripheral));
|
||||||
}
|
}
|
||||||
|
builders.push_back(std::make_unique<FunctionsBuilder>());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassBuilder::build(){
|
void ClassBuilder::build(){
|
||||||
|
|
Loading…
Reference in a new issue