From 5f82fe975c94780526517c70e3fa3946382795f4 Mon Sep 17 00:00:00 2001 From: DarkSharpness <2040703891@qq.com> Date: Tue, 9 Jul 2024 23:19:36 +0800 Subject: [PATCH] feat(bit): add a demo and add some functions for the bit class --- demo/bit.cpp | 39 +++++++++++++++++++++++++++++++++ demo/{demo.cpp => hardware.cpp} | 4 +--- include/bit.h | 12 +++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 demo/bit.cpp rename demo/{demo.cpp => hardware.cpp} (90%) diff --git a/demo/bit.cpp b/demo/bit.cpp new file mode 100644 index 0000000..956caea --- /dev/null +++ b/demo/bit.cpp @@ -0,0 +1,39 @@ +#include "../include/tools" + +signed main() { + [[maybe_unused]] + Bit <1> a; // Create a 1-bit object, default to 0 + + Bit <10> b(10); // Create a 10-bit object + + auto ref = b.slice <4, 2> (); // A copy of [2, 3, 4] bit + + std::cout << b << std::endl; // 10 + + b.set <3, 1> (ref); // Set [1, 2, 3] bit to [2, 3, 4] bit + + std::cout << b << std::endl; // 4 + + b.set <0> (~a); // Set the 0-th bit to ~a (= 1 in this case) + + std::cout << b << std::endl; // 5 + + auto sec = b.slice <2> (); // A copy of the 2-th bit (= 1 in this case) + + auto d = sec.zero_extend(); // Zero extend (default to 32-bit) + + std::cout << d << std::endl; // 1 + + auto c = sec.sign_extend <3> (); // Sign extend (default to 32-bit) + + std::cout << c << std::endl; // 7 + + // c + d; // Error: different size + // a += 1; // Error: no assignment-operation operator + + auto e = b - 1; // normal integer can be assumed as any size + + std::cout << e << std::endl; // 4 + + return 0; +} diff --git a/demo/demo.cpp b/demo/hardware.cpp similarity index 90% rename from demo/demo.cpp rename to demo/hardware.cpp index 9060079..a7d09ae 100644 --- a/demo/demo.cpp +++ b/demo/hardware.cpp @@ -19,9 +19,7 @@ struct MyModule : Input, Output, private Content { using Tags = SyncTags ; friend class Visitor; - void demo() { - this->d <= this->a + this->b; - } + void demo() { this->d <= this->a + this->b; } }; signed main() { diff --git a/include/bit.h b/include/bit.h index ba5e8e2..38881e6 100644 --- a/include/bit.h +++ b/include/bit.h @@ -26,7 +26,7 @@ struct Bit { public: static constexpr std::size_t _Bit_Len = _Nm; - constexpr Bit(target_size_t data = 0) : _M_data(data) {} + constexpr Bit(target_size_t data = {}) : _M_data(data) {} template constexpr Bit(Bit<_Lens> ...args) requires ((_Lens + ...) == _Nm) : @@ -54,6 +54,16 @@ struct Bit { constexpr operator target_size_t() const { return this->_M_data; } + template + constexpr auto set(Bit <_Hi - _Lo + 1> val) { + static_cast (this->slice<_Hi, _Lo>()); + const auto mask = // Mask those bit in the middle + (target_size_t(1) << (_Hi + 1)) - (target_size_t(1) << _Lo); + const auto data = // Set those bit in the middle + static_cast (val) << _Lo; + this->_M_data = (this->_M_data & ~mask) | data; + } + template constexpr auto slice() const -> Bit <_Hi - _Lo + 1> { static_assert(_Lo <= _Hi, "Bit::slice: _Lo should be no greater than _Hi");