Shortened field builder, added builder for functions

This commit is contained in:
Łukasz Czyż 2019-12-02 10:10:29 +01:00
parent 0605e58c05
commit c05c8fa341
3 changed files with 54 additions and 16 deletions

View file

@ -1,10 +1,20 @@
#include <Builders.hpp>
#include <iostream>
#include <bitset>
void ZeroPointerBuilder::build(std::stringstream& ss) const{
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{
@ -29,19 +39,36 @@ unsigned int RegisterBuilder::getRegisterAddress() const{
}
void FieldBuilder::build(std::stringstream& ss) const{
ss << " " << "struct " << field.name << "{\n";
ss << " " << " " << "constexpr static inline unsigned int bitOffset(){"
<< "return " << field.bitOffset << ";}\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";
ss << " " << "__FIELD(" << field.name << ", "
<< field.bitOffset << ", "
<< field.bitWidth << ", "
<< std::hex << "0x" << getAddress() << std::dec << ")\n";
}
std::string FieldBuilder::getAddress() const{
return std::to_string(registerAddress);
unsigned int FieldBuilder::getAddress() const{
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";
}

View file

@ -10,6 +10,10 @@ struct ZeroPointerBuilder : public IBuilder{
void build(std::stringstream& ss) const final;
};
struct FieldDefineBuilder : public IBuilder{
void build(std::stringstream& ss) const final;
};
struct PeripheralBuilder : public IBuilder{
PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {}
void build(std::stringstream& ss) const final;
@ -32,10 +36,15 @@ struct FieldBuilder : public IBuilder{
FieldBuilder(const Field& field_, const unsigned int registerAddress_)
: field(field_), registerAddress(registerAddress_) {}
void build(std::stringstream& ss) const final;
std::string getAddress() const;
unsigned int getAddress() const;
private:
const Field& field;
const unsigned int registerAddress;
};
struct FunctionsBuilder : public IBuilder{
void build(std::stringstream& ss) const final;
};
#endif

View file

@ -11,9 +11,11 @@ ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results_,
void ClassBuilder::setupBuilders(){
builders.push_back(std::make_unique<ZeroPointerBuilder>());
builders.push_back(std::make_unique<FieldDefineBuilder>());
for(auto& peripheral : peripherals){
builders.push_back(std::make_unique<PeripheralBuilder>(peripheral));
}
builders.push_back(std::make_unique<FunctionsBuilder>());
}
void ClassBuilder::build(){