From 7e4457f66fce8fd36a31d3607d9f28e56a0d93b5 Mon Sep 17 00:00:00 2001 From: Tony Di Nucci Date: Sun, 28 Apr 2019 13:16:57 +0100 Subject: [PATCH] Correct page addressing and NOP handler --- src/machine/memory.cpp | 6 ++++- .../system-opcode-handler-container.cpp | 7 ++++++ .../handler/system-opcode-handler-container.h | 21 ++++++++++++++++ src/opcode/opcode-handler-directory.cpp | 2 ++ test/system-opcode-handler-test.cpp | 25 +++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/opcode/handler/system-opcode-handler-container.cpp create mode 100644 src/opcode/handler/system-opcode-handler-container.h create mode 100644 test/system-opcode-handler-test.cpp diff --git a/src/machine/memory.cpp b/src/machine/memory.cpp index 1047acd..b7f0b93 100644 --- a/src/machine/memory.cpp +++ b/src/machine/memory.cpp @@ -2,7 +2,11 @@ namespace emu_6502 { inline uint16_t Memory::get_page_offset(uint8_t page) { - return page * 0xFF; + uint16_t address = page * 0xFF; + if (page > 0) + address += 1; + + return address; } Memory::Memory() { diff --git a/src/opcode/handler/system-opcode-handler-container.cpp b/src/opcode/handler/system-opcode-handler-container.cpp new file mode 100644 index 0000000..d977a85 --- /dev/null +++ b/src/opcode/handler/system-opcode-handler-container.cpp @@ -0,0 +1,7 @@ +#include "system-opcode-handler-container.h" + +namespace emu_6502 { + SystemOpcodeHandlerContainer::SystemOpcodeHandlerContainer() { + handlers.insert({Op::NOP, [](Machine&) {}}); + } +} \ No newline at end of file diff --git a/src/opcode/handler/system-opcode-handler-container.h b/src/opcode/handler/system-opcode-handler-container.h new file mode 100644 index 0000000..bbaa19f --- /dev/null +++ b/src/opcode/handler/system-opcode-handler-container.h @@ -0,0 +1,21 @@ +#ifndef INC_6502_EMULATOR_SYSTEM_OPCODE_HANDLER_CONTAINER_H +#define INC_6502_EMULATOR_SYSTEM_OPCODE_HANDLER_CONTAINER_H + +#include "opcode-handler-container.h" + +namespace emu_6502 { + class SystemOpcodeHandlerContainer : public OpcodeHandlerContainer { + private: + enum Op { + NOP = 0xEA + }; + + public: + SystemOpcodeHandlerContainer(); + SystemOpcodeHandlerContainer(const SystemOpcodeHandlerContainer& other) = delete; + SystemOpcodeHandlerContainer operator=(const SystemOpcodeHandlerContainer& other) = delete; + }; +} + + +#endif //INC_6502_EMULATOR_SYSTEM_OPCODE_HANDLER_CONTAINER_H diff --git a/src/opcode/opcode-handler-directory.cpp b/src/opcode/opcode-handler-directory.cpp index df14a6e..f54442b 100644 --- a/src/opcode/opcode-handler-directory.cpp +++ b/src/opcode/opcode-handler-directory.cpp @@ -9,6 +9,7 @@ #include "handler/stack-opcode-handler-container.h" #include "handler/branch-opcode-handler-container.h" #include "handler/jump-opcode-handler-container.h" +#include "handler/system-opcode-handler-container.h" #include "../utils.h" namespace emu_6502 { @@ -24,6 +25,7 @@ namespace emu_6502 { handler_containers.push_back(make_unique()); handler_containers.push_back(make_unique()); handler_containers.push_back(make_unique()); + handler_containers.push_back(make_unique()); init_handlers(); } diff --git a/test/system-opcode-handler-test.cpp b/test/system-opcode-handler-test.cpp new file mode 100644 index 0000000..b5502cd --- /dev/null +++ b/test/system-opcode-handler-test.cpp @@ -0,0 +1,25 @@ +#include "gtest/gtest.h" +#include "test-utils.h" + +using namespace std; +using namespace emu_6502; + +const uint8_t NOP = 0xEA; +const uint8_t LDA_IMM = 0xA9; + +TEST(SystemOpcodeHandlerContainer, NOP) { + auto machine = create_machine({NOP, NOP, NOP, NOP}); + machine->execute(); + + ASSERT_TRUE(are_flags_set(machine->get_cpu().get_ps(), RegisterFlagSet{})); + ASSERT_EQ(0x604, machine->get_cpu().get_pc().get_value()); +} + +TEST(SystemOpcodeHandlerContainer, NOP2) { + auto machine = create_machine({NOP, NOP, NOP, LDA_IMM, 0x35, NOP}); + machine->execute(); + + ASSERT_EQ(0x35, machine->get_cpu().get_a().get_value()); + ASSERT_TRUE(are_flags_set(machine->get_cpu().get_ps(), RegisterFlagSet{})); + ASSERT_EQ(0x606, machine->get_cpu().get_pc().get_value()); +}