From ff70ca6427faf788e1c0ee63a953c6f975140147 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Thu, 19 Dec 2013 17:31:56 +0100 Subject: [PATCH] mmio: provide bits method for bitsets ref #1006 --- base/include/util/register.h | 25 +++++++++++++++++++++++++ base/src/test/util_mmio/main.cc | 10 ++++++++++ 2 files changed, 35 insertions(+) diff --git a/base/include/util/register.h b/base/include/util/register.h index b405c66aa..543d3cf7a 100644 --- a/base/include/util/register.h +++ b/base/include/util/register.h @@ -235,6 +235,18 @@ namespace Genode }; typedef typename Trait::Uint_width::Type access_t; typedef Bitset_2 Bitset_2_base; + + /** + * Get register with the bitset set to a given value and rest left 0 + * + * \param T access type of register + * \param v bitset value + */ + template + static inline T bits(T const v) + { + return Bits_0::bits(v) | Bits_1::bits(v >> Bits_0::WIDTH); + } }; /** @@ -260,6 +272,19 @@ namespace Genode }; typedef typename Trait::Uint_width::Type access_t; typedef Bitset_3 Bitset_3_base; + + /** + * Get register with the bitset set to a given value and rest left 0 + * + * \param T access type of register + * \param v bitset value + */ + template + static inline T bits(T const v) + { + typedef Bitset_2 Bits_0_1; + return Bits_0_1::bits(v) | Bits_2::bits(v >> Bits_0_1::WIDTH); + } }; } diff --git a/base/src/test/util_mmio/main.cc b/base/src/test/util_mmio/main.cc index 88fb54ddd..a0e385eb8 100644 --- a/base/src/test/util_mmio/main.cc +++ b/base/src/test/util_mmio/main.cc @@ -126,15 +126,18 @@ struct Test_mmio : public Mmio { struct Bits_0 : Bitfield<1, 3> { }; struct Bits_1 : Bitfield<12, 4> { }; + struct Bits_2 : Bitfield<6, 2> { }; }; struct Reg_2 : Register<0x4, 32> { struct Bits_0 : Bitfield<4, 5> { }; struct Bits_1 : Bitfield<17, 12> { }; + struct Bits_2 : Bitfield<1, 3> { }; }; struct My_bitset_2 : Bitset_2 { }; struct My_bitset_3 : Bitset_3 { }; struct My_bitset_4 : Bitset_2 { }; + struct My_bitset_5 : Bitset_3 { }; }; @@ -467,6 +470,13 @@ int main() if (mmio.read() != 0x5679) return test_failed(19); + /** + * Test 20, bitfield methods of bitsets + */ + if (Test_mmio::My_bitset_5::bits(0b110010110) != 0b1100000010001010) { + return test_failed(20); + } + printf("Test done\n"); return 0; }