Compare commits

...
3 Commits
2 changed files with 30 additions and 18 deletions
+22 -12
View File
@@ -5,11 +5,13 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <cstdint> // uint8_t, uint32_t
#include "cpu.h" #include "cpu.h"
#include "emu.h" #include "emu.h"
#include <iostream> #include "ruc/format/print.h"
CPU::CPU(unsigned int frequency) CPU::CPU(uint32_t frequency)
: ProcessingUnit(frequency) : ProcessingUnit(frequency)
{ {
} }
@@ -20,26 +22,34 @@ CPU::~CPU()
void CPU::update() void CPU::update()
{ {
printf("This is an update from the CPU\n"); m_wait_cycles--;
if (m_wait_cycles <= 0) {
// Read next opcode
}
print("This is an update from the CPU\n");
Emu::the().writeMemory("RAM", 123, 42); Emu::the().writeMemory("RAM", 123, 42);
printf("fff"); print("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 case 0xc6: // ADD A,d8
// program_counter += 2; // Program counter +2
// clock += 8; m_pc += 2;
// Clock cycles +8
m_wait_cycles += 8;
// Flags: Z0HC // Flags: Z0HC
m_z = (m_af >> 8) + immediate == 0; m_z = m_a + immediate == 0;
m_n = 0; m_n = 0;
m_h = (m_af >> 8) + immediate > 16; m_h = m_a + immediate > 16;
m_c = (m_af >> 8) + immediate > 255; m_c = m_a + immediate > 255;
// A = A + r // A = A + r
m_af = m_af + (immediate << 8); m_a += immediate;
break; break;
default: default:
break; break;
+8 -6
View File
@@ -7,20 +7,20 @@
#pragma once #pragma once
#include <cstdint> // uint8_t, uint16_t #include <cstdint> // int8_t, uint8_t, uint16_t, uint32_t
#include "processing-unit.h" #include "processing-unit.h"
class CPU final : public ProcessingUnit { class CPU final : public ProcessingUnit {
public: public:
CPU(unsigned int frequency); explicit CPU(uint32_t frequency);
virtual ~CPU(); virtual ~CPU();
void update() override; void update() override;
// 8-bit Arithmetic and Logic Instructions // 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 // 16-bit Arithmetic Instructions
@@ -37,17 +37,19 @@ public:
// Miscellaneous Instructions // Miscellaneous Instructions
private: private:
// 16-bit registers // Registers
uint16_t m_af { 0 }; // Accumulator & Flags uint8_t m_a { 0 }; // Accumulator
uint16_t m_bc { 0 }; // BC uint16_t m_bc { 0 }; // BC
uint16_t m_de { 0 }; // DE uint16_t m_de { 0 }; // DE
uint16_t m_hl { 0 }; // HL uint16_t m_hl { 0 }; // HL
uint16_t m_sp { 0 }; // Stack Pointer uint16_t m_sp { 0 }; // Stack Pointer
uint16_t m_pc { 0 }; // Program Counter uint16_t m_pc { 0 }; // Program Counter
// 8-bit registers (flags) // Flags
uint8_t m_z { 0 }; // Zero flag uint8_t m_z { 0 }; // Zero flag
uint8_t m_n { 0 }; // Subtraction flag (BCD) uint8_t m_n { 0 }; // Subtraction flag (BCD)
uint8_t m_h { 0 }; // Half Carry flag (BCD) uint8_t m_h { 0 }; // Half Carry flag (BCD)
uint8_t m_c { 0 }; // Carry flag uint8_t m_c { 0 }; // Carry flag
int8_t m_wait_cycles { 0 };
}; };