libyggdrasil  v1.0.0
rng.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <cpp/common/registers.hpp>
32 
33 #include <array>
34 #include <cmath>
35 
36 namespace bsp::asg_coproc::drv {
37 
45  template<addr_t BaseAddress>
46  struct Random {
47  Random() = delete;
48  Random(const Random&) = delete;
49  auto operator=(const Random&) = delete;
50 
56  static bool init() {
57  return true;
58  }
59 
65  static bool deinit() {
66  return true;
67  }
68 
74  template<typename T>
75  [[nodiscard]] static T get() noexcept {
76  T data;
77 
78  RNGEN = true; // Enable rng
79  for (u32 offset = 0; offset < sizeof(T); offset += sizeof(u32)) { // Always get 4 baytes of random data
80  while (!DRDY); // Wait for the rng to finish
81 
82  u32 rng = RNGDATA; // Get random data (4 bytes)
83  std::memcpy(&data, &rng, std::min(u32(sizeof(u32)), sizeof(T) - offset)); // Fill up 4 bytes to the Type T
84  }
85  RNGEN = false; // Disable rng
86 
87  return data; // Return random data
88  }
89 
90  private:
91  enum class RegisterMap : u8 {
92  CR = 0x00,
93  SR = 0x04,
94  DR = 0x08,
95 
96  };
97 
101 
102  static inline auto RNGEN = typename CR::template Field<2, 2>();
103  static inline auto DRDY = typename SR::template Field<0, 0>();
104  static inline auto RNGDATA = typename DR::template Field<0, 31>();
105  };
106 
107 }
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
rng.hpp
Frontend for the RNG abstraction.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::asg_coproc::drv::Random::deinit
static bool deinit()
Deinit function.
Definition: rng.hpp:65
bsp::asg_coproc::drv
Definition: adc.hpp:32
bsp::asg_coproc::drv::Random::get
static T get() noexcept
Get random values seeded by true entropy.
Definition: rng.hpp:75
bsp::asg_coproc::drv::Random::Random
Random()=delete
u32
uint32_t u32
Definition: types.h:38
RegisterMap
RegisterMap
Register map.
Definition: hash.cpp:42
bsp::asg_coproc::drv::Random::Random
Random(const Random &)=delete
bsp::asg_coproc::drv::Random::init
static bool init()
Init function.
Definition: rng.hpp:56
registers.hpp
Zero-cost abstraction for accessing registers and bits/bitfields within them.
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::asg_coproc::drv::Random::operator=
auto operator=(const Random &)=delete
bsp::asg_coproc::drv::Random
RNG abstraction for Asgard.
Definition: rng.hpp:46