Skip to content

Commit

Permalink
Update getting random
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 10, 2024
1 parent 9b86fe6 commit 2ecf6de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "Cpu.h"

#include <chrono>
#include <fstream>
#include <iostream>
#include <random>

Cpu::Cpu(Renderer* renderer, Keyboard* keyboard, Speaker * speaker) {
Cpu::Cpu(Renderer* renderer, Keyboard* keyboard, Speaker * speaker) : randomEngine(std::chrono::system_clock::now().time_since_epoch().count()) {
this->address = 0;
this->delay = 0;
this->soundTimer = 0;
this->pc = 0x200; // Starting position for reading instructions. At least for most programs
this->paused = false;
this->speed = 20; // Game speed
this->rand = std::uniform_int_distribution<uint8_t>(0, 255);

this->renderer = renderer;
this->keyboard = keyboard;
Expand Down Expand Up @@ -199,14 +201,7 @@ void Cpu::runInstruction(const uint16_t opcode) {
this->pc = (opcode & 0xFFF) + this->registers[0];
break;
case 0xC000: {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint8_t> dis(0, 0xFF);

int randInt = dis(gen); // Generate a random number
uint8_t rand = static_cast<uint8_t>(randInt);

this->registers[x] = rand & (opcode & 0xFF);
this->registers[x] = this->rand(this->randomEngine) & (opcode & 0xFF);
break;
}
case 0xD000: {
Expand Down
3 changes: 3 additions & 0 deletions src/emulator/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <array>
#include <cstdint>
#include <fstream>
#include <random>

#include "Keyboard.h"
#include "Renderer.h"
Expand Down Expand Up @@ -31,6 +32,8 @@ class Cpu {

bool paused;
uint8_t speed;
std::default_random_engine randomEngine;
std::uniform_int_distribution<uint8_t> rand;

Renderer* renderer;
Keyboard* keyboard;
Expand Down

0 comments on commit 2ecf6de

Please sign in to comment.