This repository was archived by the owner on Oct 28, 2021. It is now read-only.
Commit 72d7e32 Bill Lord
committed
1 parent 1e70960 commit 72d7e32 Copy full SHA for 72d7e32
File tree 2 files changed +6
-4
lines changed
2 files changed +6
-4
lines changed Original file line number Diff line number Diff line change 5
5
#include < sstream>
6
6
#include " Chip8.h"
7
7
8
- Chip8::Chip8 () {
8
+ Chip8::Chip8 () : rng(device()), randomDistribution( 0 , 255 ) {
9
9
initState ();
10
- // Seed RNG
11
- srand (static_cast <unsigned int >(time (nullptr )));
12
10
}
13
11
14
12
void Chip8::initState () {
@@ -133,7 +131,7 @@ void Chip8::step() {
133
131
state.pc = addr (opcode) + state.v [0 ];
134
132
} else if (opidx (opcode) == 0xC ) {
135
133
// Random uint8 & Vx
136
- state.v [x (opcode)] &= rand () % 256 ; // C random is discouraged but should be fine for an insecure PRNG
134
+ state.v [x (opcode)] = randomDistribution (rng) & lowByte (opcode);
137
135
} else if (opidx (opcode) == 0xD ) {
138
136
// Read [nibble] bytes from RAM starting at $[register I] and XOR them into VRAM at (Vx, Vy), wrapping on OOB
139
137
state.v [0xf ] = 0 ; // set on sprite collision
Original file line number Diff line number Diff line change 3
3
4
4
#include < array>
5
5
#include < cstdint>
6
+ #include < random>
6
7
#include " Display.h"
7
8
8
9
constexpr int PROGRAM_OFFSET = 0x200 ;
@@ -67,6 +68,9 @@ class Chip8 {
67
68
inline uint16_t x (uint16_t op) {return (op & 0x0F00 ) >> 8 ;} // 0X00
68
69
inline uint16_t y (uint16_t op) {return (op & 0x00F0 ) >> 4 ;} // 00X0
69
70
inline uint8_t lowByte (uint16_t op) {return uint8_t (op & 0x00FF );} // 00XX
71
+ std::random_device device;
72
+ std::mt19937 rng;
73
+ std::uniform_int_distribution<int > randomDistribution;
70
74
};
71
75
72
76
#endif // CHIP8_CHIP8_H
You can’t perform that action at this time.
0 commit comments