Skip to content

Commit

Permalink
Support for VIP CHIP-8E (#11)
Browse files Browse the repository at this point in the history
* work on integrating CHIP-8E into VIP emulation

* small optimization on recompilation

* dissassembler work on chip-8e
  • Loading branch information
gulrak authored Dec 28, 2023
1 parent 725d9c8 commit 39f0c06
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cadmium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ class Cadmium : public emu::Chip8EmuHostEx
//SetNextWidth(_screenWidth - 383);
Begin();
Label("CHIP-8 variant / Core:");
if(DropdownBox("CHIP-8;CHIP-8-STRICT;CHIP-10;CHIP-8X;CHIP-48;SCHIP 1.0;SCHIP 1.1;SCHIP-COMP;SCHIP-MODERN;MEGACHIP8;XO-CHIP;VIP-CHIP-8;VIP-CHIP-8 64x64;VIP-HI-RES-CHIP-8;VIP-CHIP-8X;VIP-CHIP-8X-64x64;VIP-HI-RES-CHIP-8X;CHIP-8 DREAM6800", &_behaviorSel)) {
if(DropdownBox("CHIP-8;CHIP-8-STRICT;CHIP-10;CHIP-8X;CHIP-48;SCHIP 1.0;SCHIP 1.1;SCHIP-COMP;SCHIP-MODERN;MEGACHIP8;XO-CHIP;VIP-CHIP-8;VIP-CHIP-8 64x64;VIP-HI-RES-CHIP-8;VIP-CHIP-8E;VIP-CHIP-8X;VIP-CHIP-8X-64x64;VIP-HI-RES-CHIP-8X;CHIP-8 DREAM6800", &_behaviorSel)) {
auto preset = static_cast<emu::Chip8EmulatorOptions::SupportedPreset>(_behaviorSel);
_frameBoost = 1;
updateEmulatorOptions(emu::Chip8EmulatorOptions::optionsOfPreset(preset));
Expand Down
8 changes: 8 additions & 0 deletions src/chip8emuhostex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void Chip8EmuHostEx::updateEmulatorOptions(Chip8EmulatorOptions options)
if(_previousOptions != options || !_chipEmu) {
_previousOptions = _options = options;
if (_options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8VIP || _options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8VIP_TPD || _options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8VIP_FPD ||
_options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8EVIP ||
_options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8XVIP || _options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8XVIP_TPD || _options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8XVIP_FPD)
_chipEmu = emu::Chip8EmulatorBase::create(*this, emu::IChip8Emulator::eCHIP8VIP, _options, _chipEmu.get());
else if (_options.behaviorBase == emu::Chip8EmulatorOptions::eCHIP8DREAM)
Expand Down Expand Up @@ -190,6 +191,13 @@ bool Chip8EmuHostEx::loadBinary(std::string filename, const uint8_t* data, size_
}
updateEmulatorOptions(Chip8EmulatorOptions::optionsOfPreset(Chip8EmulatorOptions::eCHIP8VIP_TPD));
}
else if(endsWith(filename, ".c8e")) {
if (size < _chipEmu->memSize() - _options.startAddress) {
romImage = fileData;
valid = true;
}
updateEmulatorOptions(Chip8EmulatorOptions::optionsOfPreset(Chip8EmulatorOptions::eCHIP8EVIP));
}
else if(endsWith(filename, ".c8x")) {
if (size < _chipEmu->memSize() - _options.startAddress) {
romImage = fileData;
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

struct CadmiumConfiguration
{
float volume;
float volume{};
std::string workingDirectory;
std::string databaseDirectory;
emu::Chip8EmulatorOptions emuOptions;
Expand Down
4 changes: 2 additions & 2 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ void Editor::recompile()

void Editor::updateCompilerOptions(int startAddress)
{
_compiler.setStartAddress(startAddress);
recompile();
if(_compiler.setStartAddress(startAddress))
recompile();
}

void Editor::draw(Font& font, Rectangle rect)
Expand Down
6 changes: 6 additions & 0 deletions src/emulation/chip8options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Chip8Variant Chip8EmulatorOptions::variantForPreset(SupportedPreset preset)
case eCHIP8VIP: return Chip8Variant::CHIP_8;
case eCHIP8VIP_TPD: return Chip8Variant::CHIP_8_TPD;
case eCHIP8VIP_FPD: return Chip8Variant::HI_RES_CHIP_8;
case eCHIP8EVIP: return Chip8Variant::CHIP_8E;
case eCHIP8XVIP: return Chip8Variant::CHIP_8X;
case eCHIP8XVIP_TPD: return Chip8Variant::CHIP_8X_TPD;
case eCHIP8XVIP_FPD: return Chip8Variant::HI_RES_CHIP_8X;
Expand Down Expand Up @@ -81,6 +82,7 @@ std::string Chip8EmulatorOptions::nameOfPreset(SupportedPreset preset)
case eCHIP8VIP: return "VIP-CHIP-8";
case eCHIP8VIP_TPD: return "VIP-CHIP-8 64x64";
case eCHIP8VIP_FPD: return "VIP-HI-RES-CHIP-8";
case eCHIP8EVIP: return "VIP-CHIP-8E";
case eCHIP8XVIP: return "VIP-CHIP-8X";
case eCHIP8XVIP_TPD: return "VIP-CHIP-8X-TPD";
case eCHIP8XVIP_FPD: return "VIP-HI-RES-CHIP-8X";
Expand Down Expand Up @@ -109,6 +111,7 @@ const char* Chip8EmulatorOptions::shortNameOfPreset(SupportedPreset preset)
case eCHIP8VIP: return "VIPCHIP8";
case eCHIP8VIP_TPD: return "VIPCHIP8TPD";
case eCHIP8VIP_FPD: return "VIPCHIP8FPD";
case eCHIP8EVIP: return "VIPCHIP8E";
case eCHIP8XVIP: return "VIPCHIP8X";
case eCHIP8XVIP_TPD: return "VIPCHIP8XTPD";
case eCHIP8XVIP_FPD: return "VIPCHIP8XFPD";
Expand All @@ -129,6 +132,7 @@ static std::map<std::string, emu::Chip8EmulatorOptions::SupportedPreset> presetM
{"chip8te", Opts::eCHIP8TE},
{"chip8timing", Opts::eCHIP8TE},
{"chip10", Opts::eCHIP10},
{"chip8e", Opts::eCHIP8EVIP},
{"chip8x", Opts::eCHIP8X},
{"chip48", Opts::eCHIP48},
{"schip10", Opts::eSCHIP10},
Expand Down Expand Up @@ -168,6 +172,7 @@ static std::map<std::string, emu::Chip8EmulatorOptions::SupportedPreset> presetM
{"viphireschip8", Opts::eCHIP8VIP_FPD},
{"hireschip8vip", Opts::eCHIP8VIP_FPD},
{"vipchip8x", Opts::eCHIP8XVIP},
{"chip8evip", Opts::eCHIP8EVIP},
{"chip8xvip", Opts::eCHIP8XVIP},
{"chip8vipx", Opts::eCHIP8XVIP},
{"chip8xtpdvip", Opts::eCHIP8XVIP_TPD},
Expand Down Expand Up @@ -202,6 +207,7 @@ static std::map<Opts::SupportedPreset,std::string> presetOptionsProtoMap = {
{Opts::eCHIP8VIP, R"({})"},
{Opts::eCHIP8VIP_TPD, R"({"startAddress":608,"advanced":{"interpreter":"CHIP8TPD"}})"},
{Opts::eCHIP8VIP_FPD, R"({"startAddress":580,"advanced":{"interpreter":"CHIP8FPD"}})"},
{Opts::eCHIP8EVIP, R"({"advanced":{"interpreter":"CHIP8E"}})"},
{Opts::eCHIP8XVIP, R"({"startAddress":768,"advanced":{"interpreter":"CHIP8X"}})"},
{Opts::eCHIP8XVIP_TPD, R"({"startAddress":768,"advanced":{"interpreter":"CHIP8XTPD"}})"},
{Opts::eCHIP8XVIP_FPD, R"({"startAddress":768,"advanced":{"interpreter":"CHIP8XFPD"}})"},
Expand Down
2 changes: 1 addition & 1 deletion src/emulation/chip8options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace emu {

struct Chip8EmulatorOptions {
~Chip8EmulatorOptions();
enum SupportedPreset { eCHIP8, eCHIP8TE, eCHIP10, eCHIP8X, eCHIP48, eSCHIP10, eSCHIP11, eSCHPC, eSCHIP_MODERN, eMEGACHIP, eXOCHIP, eCHIP8VIP, eCHIP8VIP_TPD, eCHIP8VIP_FPD, eCHIP8XVIP, eCHIP8XVIP_TPD, eCHIP8XVIP_FPD, eCHIP8DREAM, eC8D68CHIPOSLO, eCHICUEYI, ePORTABLE, eNUM_PRESETS };
enum SupportedPreset { eCHIP8, eCHIP8TE, eCHIP10, eCHIP8X, eCHIP48, eSCHIP10, eSCHIP11, eSCHPC, eSCHIP_MODERN, eMEGACHIP, eXOCHIP, eCHIP8VIP, eCHIP8VIP_TPD, eCHIP8VIP_FPD, eCHIP8EVIP, eCHIP8XVIP, eCHIP8XVIP_TPD, eCHIP8XVIP_FPD, eCHIP8DREAM, eC8D68CHIPOSLO, eCHICUEYI, ePORTABLE, eNUM_PRESETS };
SupportedPreset behaviorBase{eCHIP8};
uint16_t startAddress{0x200};
bool optJustShiftVx{false};
Expand Down
53 changes: 49 additions & 4 deletions src/emulation/chip8vip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace emu {

class Chip8VIP::Private {
public:
static constexpr uint16_t FETCH_LOOP_ENTRY = 0x01B;
uint16_t FETCH_LOOP_ENTRY{0x01B};
static const uint64_t CPU_CLOCK_FREQUENCY = 1760640;
explicit Private(Chip8EmulatorHost& host, Cdp1802Bus& bus, const Chip8EmulatorOptions& options)
: _host(host)
Expand All @@ -26,6 +26,8 @@ class Chip8VIP::Private {
_colorRamMask = 0x3ff;
_colorRamMaskLores = 0x3e7;
}
if(options.behaviorBase == Chip8EmulatorOptions::eCHIP8EVIP)
FETCH_LOOP_ENTRY = 0x1f;
}
Chip8EmulatorHost& _host;
Cdp1802 _cpu;
Expand Down Expand Up @@ -261,10 +263,53 @@ static std::map<std::string,PatchSet> g_patchSets = {
{0x2fa, {0x02, 0xa0, 0x02, 0x3f, 0x00, 0x4b}}
}}
},

{
"CHIP8E",
{{
{0x000, {0xc4, 0x91, 0xbb, 0xff, 0x01, 0xb2, 0xb6, 0xf8, 0xcf, 0xa2, 0xf8, 0x00, 0xa5, 0xf8, 0x02, 0xb5,
0xf8, 0x81, 0xb1, 0xf8, 0x46, 0xa1, 0x90, 0xb4, 0xf8, 0x1d, 0xa4, 0x30, 0xe0}},
{0x01e, {0x69, 0x96, 0xb7, 0xe2, 0x94, 0xbc, 0x05}},
{0x026, {0xf6, 0xf6, 0xf6, 0x32, 0x42, 0xfe, 0xfc, 0x45, 0xac, 0x45, 0xf9, 0xf0, 0xa6, 0x05}},
{0x036, {0xf6, 0xf6, 0xf9, 0xf0, 0xa7, 0x4c, 0xb3, 0x0c, 0xa3, 0xd3, 0x30, 0x1f, 0x45, 0xb3, 0x45, 0x30,
0x3e, 0x00, 0xfa, 0x00, 0xf3, 0x01, 0x83, 0x01, 0x8b, 0x01, 0x75, 0x00, 0xd7, 0x00, 0xda}},
{0x056, {0xae}},
{0x058, {0x91}},
{0x05a, {0xca}},
{0x05c, {0xea, 0x01, 0xd1, 0x00, 0x65, 0x01, 0x9f, 0x01, 0x01, 0x06, 0xfa, 0x07, 0xbe, 0x06, 0xfa, 0x3f,
0xf6, 0xf6, 0xf6, 0x22, 0x52, 0x07, 0xfe, 0xfe, 0xfe, 0xf1, 0xac, 0x9b, 0xbc, 0x9a, 0xbf, 0x8a,
0xaf, 0x45, 0xfa, 0x0f, 0xad, 0xa7, 0xf8, 0xd0, 0xa6, 0x93, 0xb7, 0x87, 0x32, 0xa3, 0x27, 0x4f,
0xbd, 0x9e, 0xae, 0x8e, 0x32, 0x9b, 0x9d, 0xf6, 0xbd, 0x97, 0x76, 0xb7, 0x2e, 0x30, 0x8f}},
{0x09c, {0x56, 0x16, 0x97, 0x56, 0x16, 0x30, 0x85, 0x00, 0xec, 0xf8, 0xd0, 0xa6, 0x93, 0xa7, 0x8d, 0x32,
0xd0, 0x2d, 0x06, 0xf2, 0x32, 0xb5, 0xf8, 0x01, 0xa7, 0x46, 0xf3, 0x5c, 0x02, 0xfb, 0x07, 0x32,
0xc9, 0x1c, 0x06, 0xf2, 0x32, 0xc5, 0xf8, 0x01, 0xa7, 0x06, 0xf3, 0x5c, 0x2c, 0x16, 0x8c, 0xfc,
0x08, 0xac, 0x3b, 0xaa, 0xf8, 0xff, 0xa6, 0x87, 0x56, 0x12, 0xd4, 0x45, 0x56, 0xd4, 0x45, 0xe6,
0xf4}},
{0x0de, {0xd4, 0x00}},
{0x0e5, {0x94}},
{0x0e9, {0xf2}},
{0x0ed, {0x23}},
{0x0f3, {0x15, 0x85, 0x22, 0x73, 0x95, 0x52, 0x25, 0x45, 0xa5, 0x86, 0xfa, 0x0f, 0xb5, 0xd4, 0x45, 0xa3,
0xe6, 0x63, 0x26, 0xd4}},
{0x11b, {0xe6, 0x15, 0x30, 0xee, 0xe6, 0x8a, 0xf4, 0xaa, 0x9a, 0x7c, 0x00, 0xba, 0xd4, 0x00}},
{0x133, {0x1a, 0x1a, 0xea, 0xf8, 0xff, 0xae, 0xaf, 0x06, 0xff, 0x64, 0x1f, 0x33, 0x3b, 0xfc, 0x64, 0xff,
0x0a, 0x1e, 0x33, 0x42, 0xfc, 0x0a, 0x73, 0x8e, 0x73, 0x8f, 0x5a, 0xd4, 0x06, 0xb8, 0x98, 0x3a,
0x51, 0xd4}},
{0x175, {0x45, 0x76, 0x76, 0x33}},
{0x17a, {0x7e, 0x07, 0x3b, 0x84, 0xe6, 0xf7, 0x3b, 0x88}},
{0x195, {0x7e, 0x22, 0x87, 0x52, 0x86, 0xa7, 0x33, 0x6b, 0x30, 0x5b, 0x45, 0xf6, 0xe6, 0x62, 0x26, 0x33,
0xa9, 0x36, 0x88, 0xd4, 0x3e, 0x88, 0xd4, 0x00, 0x00, 0x45, 0xfa, 0x0f, 0x3a, 0xb6, 0x07, 0x56,
0xd4, 0xaf, 0x22, 0xf8, 0xd3, 0x73, 0x8f, 0xf9, 0xf0, 0x52, 0xe6, 0x07, 0xd2}},
{0x1c3, {0xf8, 0xff, 0xa6, 0x94, 0x7e, 0x56, 0xd4, 0x45, 0xaa, 0x86, 0xfa, 0x0f, 0xba, 0xd4, 0x19, 0x89,
0xae, 0x93, 0xbe, 0x99, 0xee, 0xf4, 0x56, 0x76, 0xe6, 0xf4, 0xb9, 0x56, 0x45, 0xf2}},
{0x1e2, {0xd4, 0x3f, 0xe3, 0x37, 0xe5, 0xe6, 0x6b, 0xd4, 0xe5, 0x86, 0xfc, 0x01, 0x85, 0x3b, 0xf9, 0xf4,
0xa5, 0x95, 0x7c}},
{0x1f6, {0xb5, 0x25, 0xd4, 0xe7, 0xa5, 0x95, 0x7f, 0x00, 0x30, 0xf6}}
}}
},
};



const uint8_t _rom_cvip[0x200] = {
0xf8, 0x80, 0xb2, 0xf8, 0x08, 0xa2, 0xe2, 0xd2, 0x64, 0x00, 0x62, 0x0c, 0xf8, 0xff, 0xa1, 0xf8, 0x0f, 0xb1, 0xf8, 0xaa, 0x51, 0x01, 0xfb, 0xaa, 0x32, 0x22, 0x91, 0xff, 0x04, 0x3b, 0x22, 0xb1, 0x30, 0x12, 0x36, 0x28, 0x90, 0xa0, 0xe0, 0xd0,
0xe1, 0xf8, 0x00, 0x73, 0x81, 0xfb, 0xaf, 0x3a, 0x29, 0xf8, 0xd2, 0x73, 0xf8, 0x9f, 0x51, 0x81, 0xa0, 0x91, 0xb0, 0xf8, 0xcf, 0xa1, 0xd0, 0x73, 0x20, 0x20, 0x40, 0xff, 0x01, 0x20, 0x50, 0xfb, 0x82, 0x3a, 0x3e, 0x92, 0xb3, 0xf8, 0x51, 0xa3,
Expand Down Expand Up @@ -447,15 +492,15 @@ bool Chip8VIP::executeCdp1802()
_host.vblank();
if(_options.optTraceLog && _impl->_cpu.getCpuState() != Cdp1802::eIDLE)
Logger::log(Logger::eBACKEND_EMU, _impl->_cpu.getCycles(), {_frames, fc}, fmt::format("{:24} ; {}", _impl->_cpu.disassembleInstructionWithBytes(-1, nullptr), _impl->_cpu.dumpStateLine()).c_str());
if(_impl->_cpu.PC() == Private::FETCH_LOOP_ENTRY) {
if(_impl->_cpu.PC() == _impl->FETCH_LOOP_ENTRY) {
_cycles++;
//std::cout << fmt::format("{:06d}:{:04x}", _impl->_cpu.getCycles()>>3, opcode()) << std::endl;
_impl->_currentOpcode = opcode();
if(_options.optTraceLog)
Logger::log(Logger::eCHIP8, _cycles, {_frames, fc}, fmt::format("CHIP8: {:30} ; {}", disassembleInstructionWithBytes(-1, nullptr), dumpStateLine()).c_str());
}
_impl->_cpu.executeInstruction();
if(_impl->_cpu.PC() == Private::FETCH_LOOP_ENTRY) {
if(_impl->_cpu.PC() == _impl->FETCH_LOOP_ENTRY) {
_impl->_lastOpcode = _impl->_currentOpcode;
#ifdef DIFFERENTIATE_CYCLES
static int64_t lastCycles{}, lastIdle{}, lastIrq{};
Expand Down
18 changes: 15 additions & 3 deletions src/emulation/hardware/integer.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//---------------------------------------------------------------------------------------
// src/emulation/hpsaturnl2.hpp
// src/emulation/integer.hpp
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2022, Steffen Schümann <s.schuemann@pobox.com>
// Copyright (c) 2023, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -55,7 +55,7 @@ class Integer
using safe_type = typename detail::next_safe<value_type>::type;

Integer() : _value{0} {}
explicit Integer(value_type val) : _value(val) {}
explicit constexpr Integer(value_type val) : _value(val) {}
Integer(Self other) : _value(other._value) {}

PodType asNative() const { return _value; }
Expand Down Expand Up @@ -101,6 +101,11 @@ class Integer
{
return Self(value_type(safe_type(_value) - other._value));
}
template<typename T = PodType, typename std::enable_if_t<std::is_signed_v<T>, Self>>
constexpr Self operator-() const
{
return Self(-_value);
}
template<typename Other, typename std::enable_if_t<std::is_integral_v<Other> && sizeof(Other) <= sizeof(PodType)>>
Self operator*(const Integer<Other>& other) const
{
Expand Down Expand Up @@ -171,6 +176,13 @@ using RU16 = Integer<uint16_t>;
using RI32 = Integer<int32_t>;
using RU32 = Integer<uint32_t>;

constexpr RI8 operator ""_ri8(unsigned long long int val) { return val > std::numeric_limits<int8_t>::max() ? throw std::exception() : RI8(val); }
constexpr RU8 operator ""_ru8(unsigned long long int val) { return val > std::numeric_limits<uint8_t>::max() ? throw std::exception() : RU8(val); }
constexpr RI16 operator ""_ri16(unsigned long long int val) { return val > std::numeric_limits<int16_t>::max() ? throw std::exception() : RI16(val); }
constexpr RU16 operator ""_ru16(unsigned long long int val) { return val > std::numeric_limits<uint16_t>::max() ? throw std::exception() : RU16(val); }
constexpr RI32 operator ""_ri32(unsigned long long int val) { return val > std::numeric_limits<int32_t>::max() ? throw std::exception() : RI32(val); }
constexpr RU32 operator ""_ru32(unsigned long long int val) { return val > std::numeric_limits<uint32_t>::max() ? throw std::exception() : RU32(val); }

template<typename ValueType = uint8_t>
class Bitfield {
public:
Expand Down
2 changes: 2 additions & 0 deletions src/librarian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,8 @@ bool Librarian::fetchDir(std::string directory)
type = Info::eROM_FILE, variant = emu::Chip8EmulatorOptions::eCHIP8VIP;
else if(ext == ".c8h")
type = Info::eROM_FILE, variant = emu::Chip8EmulatorOptions::eCHIP8VIP_TPD;
else if(ext == ".c8e")
type = Info::eROM_FILE, variant = emu::Chip8EmulatorOptions::eCHIP8EVIP;
else if(ext == ".c8x")
type = Info::eROM_FILE, variant = emu::Chip8EmulatorOptions::eCHIP8XVIP;
else if(ext == ".sc8")
Expand Down

0 comments on commit 39f0c06

Please sign in to comment.