From c05c8fa341f19d2b8771726073db85738d7c4cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czy=C5=BC?= Date: Mon, 2 Dec 2019 10:10:29 +0100 Subject: [PATCH] Shortened field builder, added builder for functions --- Src/Builders.cpp | 57 ++++++++++++++++++++++++++++++++------------ Src/Builders.hpp | 11 ++++++++- Src/ClassBuilder.cpp | 2 ++ 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Src/Builders.cpp b/Src/Builders.cpp index 237194a..659d86d 100644 --- a/Src/Builders.cpp +++ b/Src/Builders.cpp @@ -1,10 +1,20 @@ #include #include - +#include void ZeroPointerBuilder::build(std::stringstream& ss) const{ ss << "template \n" - "constexpr unsigned int *zeroVal = reinterpret_cast(zero);\n"; + "constexpr unsigned int *zeroVal = reinterpret_cast(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\n" + "constexpr inline void set(){\n" + " *FIELD::address() |= 1 << FIELD::bitOffset();\n" + "}\n" + "\n" + "template\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\n" + "constexpr inline void reset(){\n" + " *FIELD::address() &= ~(1 << FIELD::bitOffset());\n" + "}\n" + "\n" + "template\n" + "constexpr inline unsigned int read(){\n" + " return (*FIELD::address() & FIELD::bitMask()) >> FIELD::bitOffset();\n" + "}" + "\n\n"; +} \ No newline at end of file diff --git a/Src/Builders.hpp b/Src/Builders.hpp index 058de84..2ab0f06 100644 --- a/Src/Builders.hpp +++ b/Src/Builders.hpp @@ -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 diff --git a/Src/ClassBuilder.cpp b/Src/ClassBuilder.cpp index e135ec8..6d9ed65 100644 --- a/Src/ClassBuilder.cpp +++ b/Src/ClassBuilder.cpp @@ -11,9 +11,11 @@ ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results_, void ClassBuilder::setupBuilders(){ builders.push_back(std::make_unique()); + builders.push_back(std::make_unique()); for(auto& peripheral : peripherals){ builders.push_back(std::make_unique(peripheral)); } + builders.push_back(std::make_unique()); } void ClassBuilder::build(){