From 2c3109e795af09dd869611fb30a2677533ec7881 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 18 Aug 2022 14:35:24 +0200 Subject: [PATCH] Emulator: Add cycles to wait after executing opcode --- src/cpu.cpp | 22 ++++++++++++++++------ src/cpu.h | 8 +++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index 0e632d3..d51d381 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -5,11 +5,13 @@ * SPDX-License-Identifier: MIT */ +#include // uint8_t, uint32_t +#include + #include "cpu.h" #include "emu.h" -#include -CPU::CPU(unsigned int frequency) +CPU::CPU(uint32_t frequency) : ProcessingUnit(frequency) { } @@ -20,17 +22,25 @@ CPU::~CPU() void CPU::update() { + m_wait_cycles--; + if (m_wait_cycles <= 0) { + // Read next opcode + } + printf("This is an update from the CPU\n"); Emu::the().writeMemory("RAM", 123, 42); printf("fff"); } -void CPU::add(uint8_t byte, uint8_t immediate) +void CPU::add(uint8_t opcode, uint8_t immediate) { - switch (byte) { + switch (opcode) { case 0xc6: // ADD A,d8 - // program_counter += 2; - // clock += 8; + // Program counter +2 + m_pc += 2; + + // Clock cycles +8 + m_wait_cycles += 8; // Flags: Z0HC m_z = m_a + immediate == 0; diff --git a/src/cpu.h b/src/cpu.h index ba4955c..e654430 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -7,20 +7,20 @@ #pragma once -#include // uint8_t, uint16_t +#include // int8_t, uint8_t, uint16_t, uint32_t #include "processing-unit.h" class CPU final : public ProcessingUnit { public: - CPU(unsigned int frequency); + explicit CPU(uint32_t frequency); virtual ~CPU(); void update() override; // 8-bit Arithmetic and Logic Instructions - void add(uint8_t byte, uint8_t immediate = 0); + void add(uint8_t opcode, uint8_t immediate = 0); // 16-bit Arithmetic Instructions @@ -50,4 +50,6 @@ private: uint8_t m_n { 0 }; // Subtraction flag (BCD) uint8_t m_h { 0 }; // Half Carry flag (BCD) uint8_t m_c { 0 }; // Carry flag + + int8_t m_wait_cycles { 0 }; };