Browse Source
* Started adding core <-> frontend interaction. * Began CHIP-8 core. * Implemented file I/O helpers (mostly untested so far).master
Ian Burgmyer
4 years ago
10 changed files with 222 additions and 0 deletions
@ -0,0 +1,24 @@
|
||||
/* PlipChip8.cpp
|
||||
* |
||||
* A CHIP-8 implementation for Plip. |
||||
* |
||||
* (hahaha, more like PLIP-8 amirite) |
||||
*/ |
||||
|
||||
#include "PlipChip8.h" |
||||
#include "../PlipIo.h" |
||||
|
||||
namespace Plip::Core { |
||||
PlipChip8::PlipChip8(PlipInput *input) : Plip::PlipCore(input) { |
||||
} |
||||
|
||||
PlipError PlipChip8::Load(const std::string &path) { |
||||
using io = Plip::PlipIo; |
||||
if(!io::FileExists(path)) return PlipError::FileNotFound; |
||||
|
||||
auto size = io::GetSize(path); |
||||
auto ram = io::ReadRam(path, io::GetSize(path), 0x200, 0x1000); |
||||
m_memoryMap->AddBlock(&ram); |
||||
return PlipError::Success; |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
/* PlipChip8.h
|
||||
* |
||||
* A CHIP-8 implementation for Plip. |
||||
* |
||||
* (hahaha, more like PLIP-8 amirite) |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
|
||||
#include "../PlipCore.h" |
||||
#include "../PlipError.h" |
||||
#include "../Input/PlipInput.h" |
||||
|
||||
namespace Plip::Core { |
||||
class PlipChip8 : public PlipCore { |
||||
public: |
||||
explicit PlipChip8(PlipInput *input); |
||||
|
||||
PlipError Load(const std::string &path) override; |
||||
}; |
||||
} |
@ -0,0 +1,12 @@
|
||||
/* PlipCore.cpp
|
||||
* |
||||
* Defines an emulation core. |
||||
*/ |
||||
|
||||
#include "PlipCore.h" |
||||
|
||||
namespace Plip { |
||||
PlipCore::PlipCore(PlipInput *input) { |
||||
m_input = input; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
/* PlipCore.h
|
||||
* |
||||
* Defines an emulation core. |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
|
||||
#include "PlipError.h" |
||||
#include "Input/PlipInput.h" |
||||
#include "Memory/PlipMemoryMap.h" |
||||
|
||||
namespace Plip { |
||||
enum class PlipValidCore { |
||||
Chip8 |
||||
}; |
||||
|
||||
class PlipCore { |
||||
public: |
||||
virtual const PlipMemoryMap* GetMemoryMap() final { return m_memoryMap; } |
||||
virtual PlipError Load(const std::string &path) = 0; |
||||
|
||||
protected: |
||||
explicit PlipCore(PlipInput *input); |
||||
|
||||
PlipInput *m_input; |
||||
PlipMemoryMap *m_memoryMap = new PlipMemoryMap(); |
||||
}; |
||||
} |
@ -0,0 +1,14 @@
|
||||
/* PlipError.h
|
||||
* |
||||
* Defines a set of errors that can be returned to the frontend. |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
namespace Plip { |
||||
enum class PlipError { |
||||
Success, |
||||
InvalidCore, |
||||
FileNotFound |
||||
}; |
||||
} |
@ -0,0 +1,65 @@
|
||||
/* PlipIo.cpp
|
||||
* |
||||
* Standardized functions for dealing with file I/O. |
||||
*/ |
||||
|
||||
#include <filesystem> |
||||
|
||||
#include "PlipIo.h" |
||||
|
||||
namespace Plip { |
||||
bool PlipIo::FileExists(const std::string &path) { |
||||
std::ifstream file(path.c_str()); |
||||
bool success = file.good(); |
||||
if(success) file.close(); |
||||
return success; |
||||
} |
||||
|
||||
uintmax_t PlipIo::GetSize(const std::string &path) { |
||||
namespace fs = std::filesystem; |
||||
return fs::file_size(fs::path(path)); |
||||
} |
||||
|
||||
std::ifstream PlipIo::LoadFile(const std::string &path) { |
||||
return std::ifstream(path.c_str()); |
||||
} |
||||
|
||||
std::vector<char> PlipIo::ReadFile(std::ifstream &file, uintmax_t size) { |
||||
std::vector<char> data(size); |
||||
|
||||
file.seekg(std::istream::beg); |
||||
file.read(data.data(), data.size()); |
||||
return data; |
||||
} |
||||
|
||||
std::vector<char> PlipIo::ReadFile(const std::string &path, uintmax_t size) { |
||||
auto file = LoadFile(path); |
||||
return ReadFile(file, size); |
||||
} |
||||
|
||||
PlipMemoryRam PlipIo::ReadRam(std::ifstream &file, uintmax_t dataSize, uint32_t offset, uint32_t ramSize) { |
||||
auto data = ReadFile(file, dataSize); |
||||
auto ram = PlipMemoryRam(ramSize); |
||||
|
||||
auto ramByte = offset; |
||||
auto dataByte = 0; |
||||
while(ramByte < dataSize && ramByte < ramSize) |
||||
ram.SetByte(ramByte++, data[dataByte++]); |
||||
|
||||
return ram; |
||||
} |
||||
|
||||
PlipMemoryRam PlipIo::ReadRam(const std::string &path, uintmax_t dataSize, uint32_t offset, uint32_t ramSize) { |
||||
auto file = LoadFile(path); |
||||
return ReadRam(file, dataSize, offset, ramSize); |
||||
} |
||||
|
||||
PlipMemoryRom PlipIo::ReadRom(std::ifstream &file, uintmax_t size) { |
||||
return PlipMemoryRom(ReadFile(file, size).data(), size); |
||||
} |
||||
|
||||
PlipMemoryRom PlipIo::ReadRom(const std::string &path, uintmax_t size) { |
||||
auto file = LoadFile(path); |
||||
return ReadRom(file, size); |
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
/* PlipIo.h
|
||||
* |
||||
* Standardized functions for dealing with file I/O. |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <fstream> |
||||
#include <istream> |
||||
#include <vector> |
||||
|
||||
#include "Input/PlipInput.h" |
||||
#include "Memory/PlipMemoryRam.h" |
||||
#include "Memory/PlipMemoryRom.h" |
||||
|
||||
namespace Plip { |
||||
class PlipIo { |
||||
public: |
||||
static bool FileExists(const std::string &path); |
||||
static uintmax_t GetSize(const std::string &path); |
||||
static std::ifstream LoadFile(const std::string &path); |
||||
static PlipMemoryRam ReadRam(std::ifstream &file, uintmax_t dataSize, uint32_t offset, uint32_t ramSize); |
||||
static PlipMemoryRam ReadRam(const std::string &path, uintmax_t dataSize, uint32_t offset, uint32_t ramSize); |
||||
static PlipMemoryRom ReadRom(std::ifstream &file, uintmax_t size); |
||||
static PlipMemoryRom ReadRom(const std::string &path, uintmax_t size); |
||||
|
||||
private: |
||||
PlipIo() = default; |
||||
|
||||
static std::vector<char> ReadFile(std::ifstream &file, uintmax_t size); |
||||
static std::vector<char> ReadFile(const std::string &path, uintmax_t size); |
||||
}; |
||||
} |
Loading…
Reference in new issue