Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 10, 2024
1 parent 17591e1 commit c08f7ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
42 changes: 23 additions & 19 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void Cpu::loadSpritesIntoMemory() {
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};

for (int i = 0; i < sprites.size(); i++) {
this->memory[i] = sprites[i];
for (unsigned int i = 0; i < sprites.size(); ++i) {
this->memory[0x50 + i] = sprites[i];
}
}

Expand All @@ -63,7 +63,7 @@ void Cpu::cycle() {
continue;
}

runInstruction((this->memory[this->pc] << 8 | this->memory[this->pc + 1]));
runInstruction((this->memory[this->pc] << 8) | this->memory[this->pc + 1]);
}

if (!this->paused) {
Expand Down Expand Up @@ -93,8 +93,7 @@ void Cpu::runInstruction(const uint16_t opcode) {
break;
case 0x00EE:
--this->sp;
this->pc = this->stack[sp];
// this->stack.po();
this->pc = this->stack[this->sp];
break;
default:
break;
Expand All @@ -105,8 +104,8 @@ void Cpu::runInstruction(const uint16_t opcode) {
this->pc = (opcode & 0xFFF);
break;
case 0x2000:
stack[sp] = pc;
++sp;
this->stack[this->sp] = this->pc;
++this->sp;
this->pc = (opcode & 0xFFF);
break;
case 0x3000:
Expand Down Expand Up @@ -145,15 +144,15 @@ void Cpu::runInstruction(const uint16_t opcode) {
this->registers[x] ^= this->registers[y];
break;
case 0x4: {
const uint16_t sum = (this->registers[x] + this->registers[y]);
const uint16_t sum = this->registers[x] + this->registers[y];

this->registers[0xF] = 0;

if (sum > 0xFF) {
this->registers[0xF] = 1;
}

this->registers[x] = sum;
this->registers[x] = sum & 0xFF;
}
break;
case 0x5:
Expand Down Expand Up @@ -202,10 +201,10 @@ void Cpu::runInstruction(const uint16_t opcode) {
case 0xC000: {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, 0xFF);
std::uniform_int_distribution<uint8_t> dis(0, 0xFF);

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

this->registers[x] = rand & (opcode & 0xFF);
break;
Expand Down Expand Up @@ -275,22 +274,27 @@ void Cpu::runInstruction(const uint16_t opcode) {
this->address += this->registers[x];
break;
case 0x29:
this->address = this->registers[x] * 5;
this->address = 0x50 + this->registers[x] * 5;
break;
case 0x33:
this->memory[this->address] = this->registers[x] / 100;
case 0x33: {
uint8_t value = this->registers[x];

this->memory[this->address + 1] = (this->registers[x] % 100) / 10;
this->memory[this->address + 2] = value % 10;
value /= 10;

this->memory[this->address + 2] = this->registers[x] % 10;
break;
this->memory[this->address + 1] = (value % 10);
value /= 10;

this->memory[this->address] = value % 10;
}
break;
case 0x55:
for (uint8_t registerIndex = 0; registerIndex <= x; registerIndex++) {
for (uint8_t registerIndex = 0; registerIndex <= x; ++registerIndex) {
this->memory[this->address + registerIndex] = this->registers[registerIndex];
}
break;
case 0x65:
for (uint8_t registerIndex = 0; registerIndex <= x; registerIndex++) {
for (uint8_t registerIndex = 0; registerIndex <= x; ++registerIndex) {
this->registers[registerIndex] = this->memory[this->address + registerIndex];
}
break;
Expand Down
9 changes: 4 additions & 5 deletions src/emulator/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <cstdint>
#include <fstream>
#include <vector>

#include "Keyboard.h"
#include "Renderer.h"
Expand All @@ -22,13 +21,13 @@ class Cpu {
private:
std::array<uint8_t, 4096> memory;
std::array<uint8_t, 16> registers;
uint16_t address;
uint16_t address{};
uint16_t pc{};
uint8_t sp{};
std::array<uint16_t, 16> stack;
uint8_t delay;
uint8_t soundTimer;

uint16_t pc;
uint16_t sp{};
std::array<uint16_t, 16> stack;
bool paused;
uint8_t speed;

Expand Down

0 comments on commit c08f7ea

Please sign in to comment.