From 3b96a6f44d7668330b9209ca0898749e28cc294b Mon Sep 17 00:00:00 2001 From: AnonymusRaccoon Date: Mon, 27 Jan 2020 18:18:12 +0100 Subject: [PATCH] Creating a main and a cartridge reader --- CMakeLists.txt | 9 +++- main.cpp | 12 ++++- sources/CPU/{Cpu.cpp => CPU.cpp} | 2 +- sources/CPU/{Cpu.hpp => CPU.hpp} | 48 ++++++++--------- sources/Cartridge/Cartridge.cpp | 51 +++++++++++++++++++ sources/Cartridge/Cartridge.hpp | 33 ++++++++++++ .../Exceptions/NotImplementedException.hpp | 19 +++++++ sources/SNES.cpp | 12 +++++ sources/SNES.hpp | 25 +++++++++ 9 files changed, 184 insertions(+), 27 deletions(-) rename sources/CPU/{Cpu.cpp => CPU.cpp} (88%) rename sources/CPU/{Cpu.hpp => CPU.hpp} (92%) create mode 100644 sources/Cartridge/Cartridge.cpp create mode 100644 sources/Cartridge/Cartridge.hpp create mode 100644 sources/Exceptions/NotImplementedException.hpp create mode 100644 sources/SNES.cpp create mode 100644 sources/SNES.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a988dd..712bc6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,5 +23,10 @@ add_executable(ComSquare sources/Memory/MemoryBus.hpp sources/Memory/IMemory.hpp sources/Memory/IMemory.cpp - sources/CPU/Cpu.cpp - sources/CPU/Cpu.hpp) + sources/CPU/CPU.cpp + sources/CPU/CPU.hpp + sources/SNES.cpp + sources/SNES.hpp + sources/Cartridge/Cartridge.cpp + sources/Cartridge/Cartridge.hpp + sources/Exceptions/NotImplementedException.hpp) diff --git a/main.cpp b/main.cpp index cffe016..b618ebc 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,17 @@ // Created by anonymus-raccoon on 1/24/20. // -int main(void) +#include +#include +#include "sources/SNES.hpp" + +int main(int argc, char **argv) { + if (argc != 2) { + std::cout << "ComSquare:" << std::endl << "\tUsage: " << argv[0] << "rom_path" << std::endl; + return 1; + } + ComSquare::MemoryBus bus; + ComSquare::SNES snes(std::make_shared(bus), std::string(argv[1])); return 0; } \ No newline at end of file diff --git a/sources/CPU/Cpu.cpp b/sources/CPU/CPU.cpp similarity index 88% rename from sources/CPU/Cpu.cpp rename to sources/CPU/CPU.cpp index 397caba..36291b3 100644 --- a/sources/CPU/Cpu.cpp +++ b/sources/CPU/CPU.cpp @@ -2,7 +2,7 @@ // Created by anonymus-raccoon on 1/24/20. // -#include "Cpu.hpp" +#include "CPU.hpp" namespace ComSquare::CPU { diff --git a/sources/CPU/Cpu.hpp b/sources/CPU/CPU.hpp similarity index 92% rename from sources/CPU/Cpu.hpp rename to sources/CPU/CPU.hpp index a847226..578561d 100644 --- a/sources/CPU/Cpu.hpp +++ b/sources/CPU/CPU.hpp @@ -15,8 +15,8 @@ namespace ComSquare::CPU //! @brief The Accumulator union { struct { - unsigned char al; unsigned char ah; + unsigned char al; }; unsigned short a; }; @@ -25,8 +25,8 @@ namespace ComSquare::CPU //! @brief The Direct register; union { struct { - unsigned char dl; unsigned char dh; + unsigned char dl; }; unsigned short d; }; @@ -35,32 +35,32 @@ namespace ComSquare::CPU //! @brief The Program Counter; union { struct { - unsigned char pcl; unsigned char pch; + unsigned char pcl; }; unsigned short pc; }; //! @brief The Stack pointer union { struct { - unsigned char sl; unsigned char sh; + unsigned char sl; }; unsigned short s; }; //! @brief The X index register union { struct { - unsigned char xl; unsigned char xh; + unsigned char xl; }; unsigned short x; }; //! @brief The Y index register union { struct { - unsigned char yl; unsigned char yh; + unsigned char yl; }; unsigned short y; }; @@ -69,31 +69,31 @@ namespace ComSquare::CPU bool e; //! @brief The Processor status register; union p { - //! @brief The Carry flag - bool c : 1; - //! @brief The Zero flag - bool z : 1; - //! @brief The Interrupt disable flag - bool i : 1; - //! @brief The Decimal mode flag - bool d : 1; + //! @brief The Negative flag + bool n : 1; + //! @brief The oVerflow flag + bool v : 1; + //! @brief The accumulator and Memory width flag (in native mode only) + bool m : 1; union { //! @brief The indeX register width flag (in native mode only) bool x : 1; //! @brief The Break flag (in emulation mode only) bool b : 1; }; - //! @brief The accumulator and Memory width flag (in native mode only) - bool m : 1; - //! @brief The oVerflow flag - bool v : 1; - //! @brief The Negative flag - bool n : 1; + //! @brief The Decimal mode flag + bool d : 1; + //! @brief The Interrupt disable flag + bool i : 1; + //! @brief The Zero flag + bool z : 1; + //! @brief The Carry flag + bool c : 1; }; }; //! @brief The main CPU - class CPU : IMemory { + class CPU { private: //! @brief All the registers of the CPU Registers _registers; @@ -102,11 +102,13 @@ namespace ComSquare::CPU //! @brief The memory bus to use for read/write. std::shared_ptr _bus; - //! @brief Execute a single instruction and return the number of cycles tooked. + //! @brief Execute a single instruction. + //! @return The number of CPU cycles that the instruction took. int executreInstruction(); public: explicit CPU(std::shared_ptr bus); - //! @brief This function continue to execute the Cartridge code and return the number of CPU cycles that elapsed. + //! @brief This function continue to execute the Cartridge code. + //! @return The number of CPU cycles that elapsed int update(); }; } diff --git a/sources/Cartridge/Cartridge.cpp b/sources/Cartridge/Cartridge.cpp new file mode 100644 index 0000000..69ccee2 --- /dev/null +++ b/sources/Cartridge/Cartridge.cpp @@ -0,0 +1,51 @@ +// +// Created by anonymus-raccoon on 1/27/20. +// + +#include +#include +#include +#include "Cartridge.hpp" +#include "../Exceptions/NotImplementedException.hpp" + +namespace ComSquare::Cartridge +{ + size_t Cartridge::getRomSize(const std::string &romPath) + { + struct stat info; + + if (stat(romPath.c_str(), &info) < 0) + throw InvalidRomException("File not found."); + return info.st_size; + } + + Cartridge::Cartridge(const std::string &romPath) + { + try { + size_t size = this->getRomSize(romPath); + FILE *rom = fopen(romPath.c_str(), "rb"); + + if (!rom) + throw InvalidRomException("Could not open the rom file at " + romPath + ". " + strerror(errno)); + this->_size = size; + this->_data = new unsigned char[size]; + std::memset(this->_data, 0, size); + fread(this->_data, 1, size, rom); + } catch (InvalidRomException &ex) { + std::cerr << "Invalid Rom Error: " << ex.what() << std::endl; + } + } + + uint8_t Cartridge::read(uint32_t addr) + { + (void)addr; + throw NotImplementedException(); + } + + void Cartridge::write(uint32_t addr, uint8_t data) + { + (void)addr; + (void)data; + throw NotImplementedException(); + } +} \ No newline at end of file diff --git a/sources/Cartridge/Cartridge.hpp b/sources/Cartridge/Cartridge.hpp new file mode 100644 index 0000000..d898a5d --- /dev/null +++ b/sources/Cartridge/Cartridge.hpp @@ -0,0 +1,33 @@ +// +// Created by anonymus-raccoon on 1/27/20. +// + +#ifndef COMSQUARE_CARTRIDGE_HPP +#define COMSQUARE_CARTRIDGE_HPP + +#include +#include "../Memory/IMemory.hpp" + +namespace ComSquare::Cartridge +{ + class InvalidRomException : std::exception { + private: + std::string _msg; + public: + explicit InvalidRomException(const std::string &msg) : _msg(msg) {} + const char *what() const noexcept override { return this->_msg.c_str(); } + }; + + class Cartridge : IMemory { + private: + unsigned char *_data; + size_t _size; + static size_t getRomSize(const std::string &romPath); + public: + explicit Cartridge(const std::string &romPath); + uint8_t read(uint32_t addr) override; + void write(uint32_t addr, uint8_t data) override; + }; +} + +#endif //COMSQUARE_CARTRIDGE_HPP diff --git a/sources/Exceptions/NotImplementedException.hpp b/sources/Exceptions/NotImplementedException.hpp new file mode 100644 index 0000000..bb66385 --- /dev/null +++ b/sources/Exceptions/NotImplementedException.hpp @@ -0,0 +1,19 @@ +// +// Created by anonymus-raccoon on 1/27/20. +// + +#ifndef COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP +#define COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP + +#include + +namespace ComSquare +{ + class NotImplementedException : std::exception { + public: + explicit NotImplementedException() = default; + const char *what() const noexcept override { return "Not implemented yet."; } + }; +} + +#endif //COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP diff --git a/sources/SNES.cpp b/sources/SNES.cpp new file mode 100644 index 0000000..9f15064 --- /dev/null +++ b/sources/SNES.cpp @@ -0,0 +1,12 @@ +// +// Created by anonymus-raccoon on 1/27/20. +// + +#include "SNES.hpp" + +namespace ComSquare +{ + SNES::SNES(const std::shared_ptr &bus, const std::string &romPath) + : _cpu(bus), _cartridge(romPath) + { } +} diff --git a/sources/SNES.hpp b/sources/SNES.hpp new file mode 100644 index 0000000..946b604 --- /dev/null +++ b/sources/SNES.hpp @@ -0,0 +1,25 @@ +// +// Created by anonymus-raccoon on 1/27/20. +// + +#ifndef COMSQUARE_SNES_HPP +#define COMSQUARE_SNES_HPP + +#include "Memory/MemoryBus.hpp" +#include "CPU/CPU.hpp" +#include "Cartridge/Cartridge.hpp" + +namespace ComSquare +{ + //! @brief Container of all the components of the SNES. + class SNES { + private: + CPU::CPU _cpu; + Cartridge::Cartridge _cartridge; + public: + //! @brief Create all the components using a common memory bus for all of them. + SNES(const std::shared_ptr &bus, const std::string &ramPath); + }; +} + +#endif //COMSQUARE_SNES_HPP