mirror of
https://github.com/zoriya/ComSquare.git
synced 2026-05-26 07:50:04 +00:00
Creating a main and a cartridge reader
This commit is contained in:
+7
-2
@@ -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)
|
||||
|
||||
@@ -2,7 +2,17 @@
|
||||
// Created by anonymus-raccoon on 1/24/20.
|
||||
//
|
||||
|
||||
int main(void)
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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<ComSquare::MemoryBus>(bus), std::string(argv[1]));
|
||||
return 0;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// Created by anonymus-raccoon on 1/24/20.
|
||||
//
|
||||
|
||||
#include "Cpu.hpp"
|
||||
#include "CPU.hpp"
|
||||
|
||||
namespace ComSquare::CPU
|
||||
{
|
||||
@@ -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<MemoryBus> _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<MemoryBus> 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();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by anonymus-raccoon on 1/27/20.
|
||||
//
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by anonymus-raccoon on 1/27/20.
|
||||
//
|
||||
|
||||
#ifndef COMSQUARE_CARTRIDGE_HPP
|
||||
#define COMSQUARE_CARTRIDGE_HPP
|
||||
|
||||
#include <string>
|
||||
#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
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by anonymus-raccoon on 1/27/20.
|
||||
//
|
||||
|
||||
#ifndef COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP
|
||||
#define COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace ComSquare
|
||||
{
|
||||
class NotImplementedException : std::exception {
|
||||
public:
|
||||
explicit NotImplementedException() = default;
|
||||
const char *what() const noexcept override { return "Not implemented yet."; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COMSQUARE_NOTIMPLEMENTEDEXCEPTION_HPP
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by anonymus-raccoon on 1/27/20.
|
||||
//
|
||||
|
||||
#include "SNES.hpp"
|
||||
|
||||
namespace ComSquare
|
||||
{
|
||||
SNES::SNES(const std::shared_ptr<MemoryBus> &bus, const std::string &romPath)
|
||||
: _cpu(bus), _cartridge(romPath)
|
||||
{ }
|
||||
}
|
||||
@@ -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<MemoryBus> &bus, const std::string &ramPath);
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COMSQUARE_SNES_HPP
|
||||
Reference in New Issue
Block a user