gem5 v24.0.0.0
Loading...
Searching...
No Matches
lupio_rng.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 The Regents of the University of California
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
30
31#include <random>
32
33#include "debug/LupioRNG.hh"
34#include "mem/packet_access.hh"
35#include "params/LupioRNG.hh"
36
37/* CTRL fields */
38#define LUPIO_RNG_IRQE 0x1
39/* STAT fields */
40#define LUPIO_RNG_BUSY 0x1
41
42namespace gem5
43{
44
45LupioRNG::LupioRNG(const Params &params) :
46 BasicPioDevice(params, params.pio_size),
47 user_seed(params.seed)
48{
49 mt.seed(user_seed);
50 DPRINTF(LupioRNG, "LupioRNG initalized with seed: %d\n", user_seed);
51}
52
53uint64_t
55{
56 uint32_t r = 0;
57 std::uniform_int_distribution<uint32_t> distrib(0);
58
59 switch (addr >> 2) {
60 // returns a random number
61 case LUPIO_RNG_RAND:
62 r = distrib(mt);
63 DPRINTF(LupioRNG, "Random number: %#x\n", r );
64 break;
65 // returns the current seed being used
66 case LUPIO_RNG_SEED:
67 r = user_seed;
68 DPRINTF(LupioRNG, "RNG_SEED Call: %d\n", r);
69 break;
70 // returns status of the device
71 case LUPIO_RNG_STAT:
72 r = 0; /* Always ready */
73 DPRINTF(LupioRNG, "RNG_STAT Call: %d\n", r);
74 break;
75 default:
76 r = -1;
77 panic("Unexpected read to the LupioRTC device at address %d!",
78 addr);
79 break;
80 }
81 return r;
82}
83
84void
85LupioRNG::lupioRNGWrite(uint8_t addr, uint64_t val64)
86{
87 switch (addr >> 2) {
88 // allows the user to configure the seed value
89 case LUPIO_RNG_SEED:
90 user_seed = val64;
91 mt.seed(user_seed);
92 DPRINTF(LupioRNG, "RNG_SEED_WRITE: %d\n", user_seed);
93 break;
94 // configures the device using interrupts
95 case LUPIO_RNG_CTRL:
96 interrupt_enable = !!(val64 & LUPIO_RNG_IRQE);
97 DPRINTF(LupioRNG, "RNG_CTRL Call: %d\n", interrupt_enable);
98 break;
99 default:
100 panic("Unexpected write to the LupioRTC device at address %d!",
101 addr);
102 break;
103 }
104}
105
106Tick
108{
109 Addr rng_addr = pkt->getAddr() - pioAddr;
110
112 "Read request - addr: %#x, size: %#x\n", rng_addr, pkt->getSize());
113
114 uint64_t rand_read = lupioRNGRead(rng_addr);
115 DPRINTF(LupioRNG, "Packet Read: %#x\n", rand_read);
116 pkt->setUintX(rand_read, byteOrder);
117 pkt->makeResponse();
118
119 return pioDelay;
120}
121
122Tick
124{
125 Addr daddr = pkt->getAddr() - pioAddr;
126
127 DPRINTF(LupioRNG, "Write register %#x value %#x\n", daddr,
128 pkt->getUintX(byteOrder));
129
130 lupioRNGWrite(daddr, pkt->getUintX(byteOrder));
131 DPRINTF(LupioRNG, "Packet Write Value: %d\n", pkt->getUintX(byteOrder));
132
133 pkt->makeResponse();
134
135 return pioDelay;
136}
137} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
Addr pioAddr
Address that the device listens to.
Definition io_device.hh:151
Tick pioDelay
Delay that the device experinces on an access.
Definition io_device.hh:157
LupioRNG: A Random Number Generator virtual device that returns either a random value,...
Definition lupio_rng.hh:48
uint64_t user_seed
Definition lupio_rng.hh:63
std::mt19937 mt
Definition lupio_rng.hh:62
const ByteOrder byteOrder
Definition lupio_rng.hh:50
uint64_t lupioRNGRead(const uint8_t addr)
Function to return a random number, the current seed, or the device status.
Definition lupio_rng.cc:54
Tick read(PacketPtr pkt) override
Implement BasicPioDevice virtual functions.
Definition lupio_rng.cc:107
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition lupio_rng.cc:123
LupioRNG(const Params &params)
Definition lupio_rng.cc:45
void lupioRNGWrite(const uint8_t addr, const uint64_t val64)
Function to allow the user to change the current seed, and configure the device using interrupts.
Definition lupio_rng.cc:85
bool interrupt_enable
Definition lupio_rng.hh:64
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
Addr getAddr() const
Definition packet.hh:807
void setUintX(uint64_t w, ByteOrder endian)
Set the value in the word w after truncating it to the length of the packet and then byteswapping it ...
Definition packet.cc:361
void makeResponse()
Take a request packet and modify it in place to be suitable for returning as a response to that reque...
Definition packet.hh:1062
unsigned getSize() const
Definition packet.hh:817
uint64_t getUintX(ByteOrder endian) const
Get the data in the packet byte swapped from the specified endianness and zero-extended to 64 bits.
Definition packet.cc:352
PioDeviceParams Params
Definition io_device.hh:134
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define LUPIO_RNG_IRQE
Definition lupio_rng.cc:38
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
uint64_t Tick
Tick count type.
Definition types.hh:58

Generated on Tue Jun 18 2024 16:24:03 for gem5 by doxygen 1.11.0