Merge pull request #16 from SergioMartin86/refactoring

Refactoring for better use in JaffarPlus
This commit is contained in:
Sergio Martin 2024-01-28 18:21:12 +01:00 committed by GitHub
commit 06b4d82898
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
74 changed files with 242 additions and 324 deletions

2
extern/hqn/hqn.h vendored
View File

@ -3,7 +3,7 @@
#include <cstdint>
#include <stdio.h>
#include <emuInstance.hpp>
#include <nesInstance.hpp>
#define BLIT_SIZE 65536

34
source/logger.hpp Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <stdarg.h>
#include <stdexcept>
#include <stdio.h>
// If we use NCurses, we need to use the appropriate printing function
#ifdef NCURSES
#include <ncurses.h>
#define LOG printw
#else
#define LOG printf
#endif
#define EXIT_WITH_ERROR(...) exitWithError(__FILE__, __LINE__, __VA_ARGS__)
inline void exitWithError [[noreturn]] (const char *fileName, const int lineNumber, const char *format, ...)
{
char *outstr = 0;
va_list ap;
va_start(ap, format);
int ret = vasprintf(&outstr, format, ap);
if (ret < 0) exit(-1);
std::string outString = outstr;
free(outstr);
char info[1024];
snprintf(info, sizeof(info) - 1, " + From %s:%d\n", fileName, lineNumber);
outString += info;
throw std::runtime_error(outString.c_str());
}

View File

@ -1,8 +1,7 @@
#pragma once
#include "sha1/sha1.hpp"
#include <utils.hpp>
#include <controller.hpp>
#include "logger.hpp"
#include "controller.hpp"
#define _LOW_MEM_SIZE 0x800
#define _HIGH_MEM_SIZE 0x2000
@ -12,12 +11,12 @@
static const uint16_t image_width = 256;
static const uint16_t image_height = 240;
class EmuInstanceBase
class NESInstanceBase
{
public:
EmuInstanceBase() = default;
virtual ~EmuInstanceBase() = default;
NESInstanceBase() = default;
virtual ~NESInstanceBase() = default;
inline void advanceState(const std::string &move)
{
@ -61,56 +60,19 @@ class EmuInstanceBase
if (isTypeRecognized == false) EXIT_WITH_ERROR("Input type not recognized: '%s'\n", type.c_str());
}
inline std::string getRomSHA1() const { return _romSHA1String; }
inline hash_t getStateHash() const
{
MetroHash128 hash;
hash.Update(getLowMem(), _LOW_MEM_SIZE);
// hash.Update(getHighMem(), _HIGH_MEM_SIZE);
// hash.Update(getNametableMem(), _NAMETABLES_MEM_SIZE);
// hash.Update(getChrMem(), getChrMemSize());
hash_t result;
hash.Finalize(reinterpret_cast<uint8_t *>(&result));
return result;
}
inline void enableRendering() { _doRendering = true; };
inline void disableRendering() { _doRendering = false; };
inline void loadStateFile(const std::string &stateFilePath)
inline bool loadROM(const uint8_t* romData, const size_t romSize)
{
std::string stateData;
bool status = loadStringFromFile(stateData, stateFilePath);
if (status == false) EXIT_WITH_ERROR("Could not find/read state file: %s\n", stateFilePath.c_str());
deserializeState((uint8_t *)stateData.data());
}
inline void saveStateFile(const std::string &stateFilePath) const
{
std::string stateData;
stateData.resize(_stateSize);
serializeState((uint8_t *)stateData.data());
saveStringToFile(stateData, stateFilePath.c_str());
}
inline void loadROMFile(const std::string &romFilePath)
{
// Loading ROM data
bool status = loadStringFromFile(_romData, romFilePath);
if (status == false) EXIT_WITH_ERROR("Could not find/read ROM file: %s\n", romFilePath.c_str());
// Calculating ROM hash value
_romSHA1String = SHA1::GetHash((uint8_t *)_romData.data(), _romData.size());
// Actually loading rom file
status = loadROMFileImpl(_romData);
if (status == false) EXIT_WITH_ERROR("Could not process ROM file: %s\n", romFilePath.c_str());
auto status = loadROMImpl(romData, romSize);
// Detecting full state size
_stateSize = getStateSize();
// Returning status
return status;
}
void enableStateBlock(const std::string& block)
@ -151,7 +113,7 @@ class EmuInstanceBase
virtual void enableStateBlockImpl(const std::string& block) = 0;
virtual void disableStateBlockImpl(const std::string& block) = 0;
virtual bool loadROMFileImpl(const std::string &romFilePath) = 0;
virtual bool loadROMImpl(const uint8_t* romData, const size_t romSize) = 0;
virtual void advanceStateImpl(const Controller::port_t controller1, const Controller::port_t controller2) = 0;
// Storage for the light state size
@ -161,11 +123,6 @@ class EmuInstanceBase
bool _doRendering = true;
private:
// Storage for the ROM data
std::string _romData;
// SHA1 rom hash
std::string _romSHA1String;
// Controller class for input parsing
Controller _controller;

View File

@ -1,6 +1,6 @@
#pragma once
#include "emuInstance.hpp"
#include "nesInstance.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <hqn/hqn.h>
@ -30,14 +30,14 @@ class PlaybackInstance
step.input = input;
step.stateData = (uint8_t *)malloc(_emu->getStateSize());
_emu->serializeState(step.stateData);
step.hash = _emu->getStateHash();
step.hash = calculateStateHash(_emu);
// Adding the step into the sequence
_stepSequence.push_back(step);
}
// Initializes the playback module instance
PlaybackInstance(EmuInstance *emu, const std::vector<std::string> &sequence, const std::string &overlayPath = "") : _emu(emu)
PlaybackInstance(NESInstance *emu, const std::vector<std::string> &sequence, const std::string &overlayPath = "") : _emu(emu)
{
// Enabling emulation rendering
_emu->enableRendering();
@ -234,7 +234,7 @@ class PlaybackInstance
hqn::GUIController *_hqnGUI;
// Pointer to the contained emulator instance
EmuInstance *const _emu;
NESInstance *const _emu;
// Flag to store whether to use the button overlay
bool _useOverlay = false;

View File

@ -1,5 +1,5 @@
#include "argparse/argparse.hpp"
#include "emuInstance.hpp"
#include "nesInstance.hpp"
#include "playbackInstance.hpp"
#include "utils.hpp"
#include <cstdlib>
@ -91,17 +91,24 @@ int main(int argc, char *argv[])
refreshTerminal();
// Creating emulator instance
EmuInstance e;
NESInstance e;
// Setting controller types
e.setController1Type(controller1Type);
e.setController2Type(controller2Type);
// Loading ROM File
e.loadROMFile(romFilePath);
std::string romFileData;
if (loadStringFromFile(romFileData, romFilePath) == false) EXIT_WITH_ERROR("Could not rom file: %s\n", romFilePath.c_str());
e.loadROM((uint8_t*)romFileData.data(), romFileData.size());
// If an initial state is provided, load it now
if (stateFilePath != "") e.loadStateFile(stateFilePath);
if (stateFilePath != "")
{
std::string stateFileData;
if (loadStringFromFile(stateFileData, stateFilePath) == false) EXIT_WITH_ERROR("Could not initial state file: %s\n", stateFilePath.c_str());
e.deserializeState((uint8_t*)stateFileData.data());
}
// Creating playback instance
auto p = PlaybackInstance(&e, sequence);

View File

@ -1,8 +1,8 @@
#pragma once
#include <Nes_Emu.h>
#include <Nes_State.h>
#include <emuInstanceBase.hpp>
#include <quickNES/core/nes_emu/Nes_Emu.h>
#include <quickNES/core/nes_emu/Nes_State.h>
#include <nesInstanceBase.hpp>
#define _DUMMY_SIZE 65536
@ -12,10 +12,10 @@ extern void register_misc_mappers();
extern void register_extra_mappers();
extern void register_mapper_70();
class EmuInstance : public EmuInstanceBase
class NESInstance : public NESInstanceBase
{
public:
EmuInstance() : EmuInstanceBase()
NESInstance() : NESInstanceBase()
{
// Creating new emulator
_nes = new Nes_Emu;
@ -32,10 +32,10 @@ class EmuInstance : public EmuInstanceBase
register_mapper_70();
}
virtual bool loadROMFileImpl(const std::string &romData) override
virtual bool loadROMImpl(const uint8_t* romData, const size_t romSize) override
{
// Loading rom data
Mem_File_Reader romReader(romData.data(), (int)romData.size());
Mem_File_Reader romReader(romData, (int)romSize);
Auto_File_Reader romFile(romReader);
auto result = _nes->load_ines(romFile);
return result == 0;

View File

@ -1,8 +1,8 @@
// Nes_Emu 0.7.0. http://www.slack.net/~ant/libs/
#include "apu/NESEffectsBuffer.hpp"
#include "apu/apu.hpp"
#include "NESEffectsBuffer.hpp"
#include "apu.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -1,7 +1,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/libs/
#include "apu/buffer.hpp"
#include "apu/apu.hpp"
#include "buffer.hpp"
#include "apu.hpp"
/* Library Copyright (C) 2003-2006 Shay Green. This library is free software;
you can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -1,7 +1,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "apu/fme7/apu.hpp"
#include "apu.hpp"
#include <cstring>
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you

View File

@ -4,7 +4,7 @@
// Emu 0.7.0
#include <cstdint>
#include "apu/blipBuffer.hpp"
#include "../blipBuffer.hpp"
namespace quickerNES
{

View File

@ -1,8 +1,8 @@
// Snd_Emu 0.1.7. http://www.slack.net/~ant/
#include "apu/namco/apu.hpp"
#include "apu/blipBuffer.hpp"
#include "apu.hpp"
#include "../blipBuffer.hpp"
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -4,7 +4,7 @@
// Snd_Emu 0.1.7
#include <cstdint>
#include "apu/apu.hpp"
#include "../apu.hpp"
namespace quickerNES
{

View File

@ -1,7 +1,7 @@
// Snd_Emu 0.1.7. http://www.slack.net/~ant/
#include "apu/vrc6/apu.hpp"
#include "apu.hpp"
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -5,8 +5,8 @@
// Snd_Emu 0.1.7
#include <cstdint>
#include "apu/blipBuffer.hpp"
#include "apu/apu.hpp"
#include "../blipBuffer.hpp"
#include "../apu.hpp"
namespace quickerNES
{

View File

@ -1,6 +1,6 @@
#include <cstring>
#include "apu/vrc7/apu.hpp"
#include "apu/vrc7/emu2413.hpp"
#include "apu.hpp"
#include "emu2413.hpp"
namespace quickerNES
{

View File

@ -5,8 +5,8 @@
// Snd_Emu 0.1.7. Copyright (C) 2003-2005 Shay Green. GNU LGPL license.
#include <cstdint>
#include "apu/blipBuffer.hpp"
#include "apu/vrc7/emu2413_state.hpp"
#include "../blipBuffer.hpp"
#include "emu2413_state.hpp"
namespace quickerNES
{

View File

@ -1,8 +1,8 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "core.hpp"
#include "mapper.hpp"
#include "../core.hpp"
#include <cstring>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
@ -20,61 +20,61 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
New mapping distribution by Sergio Martin (eien86)
https://github.com/SergioMartin86/jaffarPlus
*/
#include "mappers/mapper000.hpp"
#include "mappers/mapper001.hpp"
#include "mappers/mapper002.hpp"
#include "mappers/mapper003.hpp"
#include "mappers/mapper004.hpp"
#include "mappers/mapper005.hpp"
#include "mappers/mapper007.hpp"
#include "mappers/mapper009.hpp"
#include "mappers/mapper010.hpp"
#include "mappers/mapper011.hpp"
#include "mappers/mapper015.hpp"
#include "mappers/mapper019.hpp"
#include "mappers/mapper021.hpp"
#include "mappers/mapper022.hpp"
#include "mappers/mapper023.hpp"
#include "mappers/mapper024.hpp"
#include "mappers/mapper025.hpp"
#include "mappers/mapper026.hpp"
#include "mappers/mapper030.hpp"
#include "mappers/mapper032.hpp"
#include "mappers/mapper033.hpp"
#include "mappers/mapper034.hpp"
#include "mappers/mapper060.hpp"
#include "mappers/mapper066.hpp"
#include "mappers/mapper069.hpp"
#include "mappers/mapper070.hpp"
#include "mappers/mapper071.hpp"
#include "mappers/mapper073.hpp"
#include "mappers/mapper075.hpp"
#include "mappers/mapper078.hpp"
#include "mappers/mapper079.hpp"
#include "mappers/mapper085.hpp"
#include "mappers/mapper086.hpp"
#include "mappers/mapper087.hpp"
#include "mappers/mapper088.hpp"
#include "mappers/mapper089.hpp"
#include "mappers/mapper093.hpp"
#include "mappers/mapper094.hpp"
#include "mappers/mapper097.hpp"
#include "mappers/mapper113.hpp"
#include "mappers/mapper140.hpp"
#include "mappers/mapper152.hpp"
#include "mappers/mapper154.hpp"
#include "mappers/mapper156.hpp"
#include "mappers/mapper180.hpp"
#include "mappers/mapper184.hpp"
#include "mappers/mapper190.hpp"
#include "mappers/mapper193.hpp"
#include "mappers/mapper206.hpp"
#include "mappers/mapper207.hpp"
#include "mappers/mapper232.hpp"
#include "mappers/mapper240.hpp"
#include "mappers/mapper241.hpp"
#include "mappers/mapper244.hpp"
#include "mappers/mapper246.hpp"
#include "mapper000.hpp"
#include "mapper001.hpp"
#include "mapper002.hpp"
#include "mapper003.hpp"
#include "mapper004.hpp"
#include "mapper005.hpp"
#include "mapper007.hpp"
#include "mapper009.hpp"
#include "mapper010.hpp"
#include "mapper011.hpp"
#include "mapper015.hpp"
#include "mapper019.hpp"
#include "mapper021.hpp"
#include "mapper022.hpp"
#include "mapper023.hpp"
#include "mapper024.hpp"
#include "mapper025.hpp"
#include "mapper026.hpp"
#include "mapper030.hpp"
#include "mapper032.hpp"
#include "mapper033.hpp"
#include "mapper034.hpp"
#include "mapper060.hpp"
#include "mapper066.hpp"
#include "mapper069.hpp"
#include "mapper070.hpp"
#include "mapper071.hpp"
#include "mapper073.hpp"
#include "mapper075.hpp"
#include "mapper078.hpp"
#include "mapper079.hpp"
#include "mapper085.hpp"
#include "mapper086.hpp"
#include "mapper087.hpp"
#include "mapper088.hpp"
#include "mapper089.hpp"
#include "mapper093.hpp"
#include "mapper094.hpp"
#include "mapper097.hpp"
#include "mapper113.hpp"
#include "mapper140.hpp"
#include "mapper152.hpp"
#include "mapper154.hpp"
#include "mapper156.hpp"
#include "mapper180.hpp"
#include "mapper184.hpp"
#include "mapper190.hpp"
#include "mapper193.hpp"
#include "mapper206.hpp"
#include "mapper207.hpp"
#include "mapper232.hpp"
#include "mapper240.hpp"
#include "mapper241.hpp"
#include "mapper244.hpp"
#include "mapper246.hpp"
namespace quickerNES
{

View File

@ -4,8 +4,8 @@
// Emu 0.7.0
#include <climits>
#include "cart.hpp"
#include "cpu.hpp"
#include "../cart.hpp"
#include "../cpu.hpp"
namespace quickerNES
{

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -2,7 +2,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
#include <cstring>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -2,9 +2,9 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
#include "core.hpp"
#include "../core.hpp"
#include <cstring>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you

View File

@ -4,8 +4,8 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "core.hpp"
#include "mappers/mapper.hpp"
#include "../core.hpp"
#include "mapper.hpp"
#include <cstring>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -1,6 +1,6 @@
#pragma once
#include "mappers/mapper.hpp"
#include "mapper.hpp"
#include <cstring>
namespace quickerNES

View File

@ -1,5 +1,5 @@
#pragma once
#include "mappers/mapper.hpp"
#include "mapper.hpp"
#include <cstring>
namespace quickerNES

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -17,7 +17,7 @@
* 100-in-1 Contra Function 16
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -4,8 +4,8 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "apu/namco/apu.hpp"
#include "mappers/mapper.hpp"
#include "../apu/namco/apu.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -26,7 +26,7 @@
* VRC-2/VRC-4 Konami
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -26,8 +26,8 @@
* VRC-2/VRC-4 Konami
*/
#include "mappers/mapper.hpp"
#include "mappers/mapper021.hpp"
#include "mapper.hpp"
#include "mapper021.hpp"
namespace quickerNES
{

View File

@ -26,7 +26,7 @@
* VRC-2/VRC-4 Konami
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -3,8 +3,8 @@
// Konami VRC6 mapper
// Emu 0.7.0. http://www.slack.net/~ant/
#include "apu/vrc6/apu.hpp"
#include "mappers/mapper.hpp"
#include "../apu/vrc6/apu.hpp"
#include "mapper.hpp"
#include <cstring>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you

View File

@ -26,7 +26,7 @@
* VRC-2/VRC-4 Konami
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -27,7 +27,7 @@
* Tested only on Troll Burner and Mystic Origins demo.
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Unrom512

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -17,7 +17,7 @@
* 4-in-1 Multicart ( Reset-based )
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// NROM-128 4-in-1 multicart

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -4,8 +4,8 @@
// Emu 0.7.0. http://www.slack.net/~ant/libs/
#include "apu/fme7/apu.hpp"
#include "mappers/mapper.hpp"
#include "../apu/fme7/apu.hpp"
#include "mapper.hpp"
/* Copyright (C) 2005 Chris Moeller */
/* Copyright (C) 2005-2006 Shay Green. This module is free software; you

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Mapper_74x161x162x32

View File

@ -4,7 +4,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -23,7 +23,7 @@
* VRC-3 Konami, Salamander
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -17,7 +17,7 @@
* VRC-1 Konami
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -1,6 +1,6 @@
#pragma once
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Holy Diver and Uchuusen - Cosmo Carrier.

View File

@ -25,7 +25,7 @@
* Nina-03 / Nina-06
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -2,8 +2,8 @@
// Emu 0.5.4. http://www.slack.net/~ant/
#include "apu/vrc7/apu.hpp"
#include "mappers/mapper.hpp"
#include "../apu/vrc7/apu.hpp"
#include "mapper.hpp"
#include <cstring>
/* Copyright (C) 2004-2005 Shay Green. This module is free software; you

View File

@ -3,7 +3,7 @@
// Optional less-common simple mappers
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -25,7 +25,7 @@
* Mapper 206
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -22,7 +22,7 @@
* Mapper 93 - Sunsoft-2
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Sunsoft2b

View File

@ -22,7 +22,7 @@
* Mapper 93 - Sunsoft-2
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Sunsoft2a

View File

@ -24,7 +24,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Un1rom

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Irem_Tam_S1

View File

@ -24,7 +24,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Jaleco_JF11

View File

@ -1,6 +1,6 @@
#pragma once
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// DIS23C01 DAOU ROM CONTROLLER

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// UxROM (inverted)

View File

@ -22,7 +22,7 @@
* Mapper 184 - Sunsoft-1
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Sunsoft1

View File

@ -1,6 +1,6 @@
#pragma once
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// Magic Kid Googoo

View File

@ -25,7 +25,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// NTDEC's TC-112 mapper IC.

View File

@ -25,7 +25,7 @@
* Mapper 206
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
namespace quickerNES
{

View File

@ -5,7 +5,7 @@
// Emu 0.7.0. http://www.slack.net/~ant/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser

View File

@ -21,7 +21,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// https://www.nesdev.org/wiki/INES_Mapper240

View File

@ -21,7 +21,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// https://www.nesdev.org/wiki/INES_Mapper241

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// https://www.nesdev.org/wiki/INES_Mapper244

View File

@ -23,7 +23,7 @@
*
*/
#include "mappers/mapper.hpp"
#include "mapper.hpp"
// https://www.nesdev.org/wiki/INES_Mapper246

View File

@ -5,7 +5,7 @@
#include <cstring>
#include "ppu.hpp"
#include "core.hpp"
#include "../core.hpp"
namespace quickerNES
{

View File

@ -32,6 +32,6 @@ quickerNESSrc = quickerNESAPUSrc + quickerNESPPUSrc + [
quickerNESDependency = declare_dependency(
compile_args : [ '-D_PAGE_SIZE="' + pageSize.to_string() + '"'],
include_directories : include_directories(['.', 'core']),
include_directories : include_directories(['.']),
sources : [ quickerNESSrc ]
)

View File

@ -1,14 +1,14 @@
#pragma once
#include <core/emu.hpp>
#include <emuInstanceBase.hpp>
#include <quickerNES/core/emu.hpp>
#include <nesInstanceBase.hpp>
typedef quickerNES::Emu emulator_t;
class EmuInstance : public EmuInstanceBase
class NESInstance : public NESInstanceBase
{
public:
EmuInstance() : EmuInstanceBase()
NESInstance() : NESInstanceBase()
{
// Creating new emulator
_nes = new emulator_t;
@ -20,12 +20,12 @@ class EmuInstance : public EmuInstanceBase
_nes->set_pixels(video_buffer, image_width + 8);
}
~EmuInstance() = default;
~NESInstance() = default;
virtual bool loadROMFileImpl(const std::string &romData) override
virtual bool loadROMImpl(const uint8_t* romData, const size_t romSize) override
{
// Loading rom data
_nes->load_ines((uint8_t *)romData.data());
_nes->load_ines(romData);
return true;
}

View File

@ -2,7 +2,7 @@
#include "nlohmann/json.hpp"
#include "sha1/sha1.hpp"
#include "utils.hpp"
#include "emuInstance.hpp"
#include "nesInstance.hpp"
#include <chrono>
#include <sstream>
#include <vector>
@ -94,17 +94,27 @@ int main(int argc, char *argv[])
std::string controller2Type = scriptJson["Controller 2 Type"].get<std::string>();
// Creating emulator instance
EmuInstance e;
NESInstance e;
// Setting controller types
e.setController1Type(controller1Type);
e.setController2Type(controller2Type);
// Loading ROM File
e.loadROMFile(romFilePath);
std::string romFileData;
if (loadStringFromFile(romFileData, romFilePath) == false) EXIT_WITH_ERROR("Could not rom file: %s\n", romFilePath.c_str());
e.loadROM((uint8_t*)romFileData.data(), romFileData.size());
// Calculating ROM SHA1
auto romSHA1 = SHA1::GetHash((uint8_t *)romFileData.data(), romFileData.size());
// If an initial state is provided, load it now
if (initialStateFilePath != "") e.loadStateFile(initialStateFilePath);
if (initialStateFilePath != "")
{
std::string stateFileData;
if (loadStringFromFile(stateFileData, initialStateFilePath) == false) EXIT_WITH_ERROR("Could not initial state file: %s\n", initialStateFilePath.c_str());
e.deserializeState((uint8_t*)stateFileData.data());
}
// Disabling requested blocks from state serialization
for (const auto& block : stateDisabledBlocks) e.disableStateBlock(block);
@ -115,9 +125,6 @@ int main(int argc, char *argv[])
// Getting state size
const auto stateSize = e.getStateSize();
// Getting actual ROM SHA1
auto romSHA1 = e.getRomSHA1();
// Checking with the expected SHA1 hash
if (romSHA1 != expectedROMSHA1) EXIT_WITH_ERROR("Wrong ROM SHA1. Found: '%s', Expected: '%s'\n", romSHA1.c_str(), expectedROMSHA1.c_str());
@ -174,11 +181,11 @@ int main(int argc, char *argv[])
double elapsedTimeSeconds = (double)dt * 1.0e-9;
// Calculating final state hash
const auto finalStateHash = e.getStateHash();
auto result = calculateStateHash(&e);
// Creating hash string
char hashStringBuffer[256];
sprintf(hashStringBuffer, "0x%lX%lX", finalStateHash.first, finalStateHash.second);
sprintf(hashStringBuffer, "0x%lX%lX", result.first, result.second);
// Printing time information
printf("[] Elapsed time: %3.3fs\n", (double)dt * 1.0e-9);

View File

@ -2,25 +2,20 @@
#include <algorithm>
#include <fstream>
#include <metrohash128/metrohash128.h>
#include <sstream>
#include <stdarg.h>
#include <stdexcept>
#include <stdio.h>
#include <unistd.h>
#include <vector>
// If we use NCurses, we need to use the appropriate printing function
#ifdef NCURSES
#include <ncurses.h>
#define LOG printw
#else
#define LOG printf
#endif
#include <metrohash128/metrohash128.h>
#include "nesInstance.hpp"
// If we use NCurses, define the following useful functions
#ifdef NCURSES
#include <ncurses.h>
// Function to check for keypress taken from https://github.com/ajpaulson/learning-ncurses/blob/master/kbhit.c
inline int kbhit()
{
@ -84,16 +79,6 @@ inline void refreshTerminal()
#endif // NCURSES
typedef _uint128_t hash_t;
inline hash_t calculateMetroHash(uint8_t *data, size_t size)
{
MetroHash128 hash;
hash.Update(data, size);
hash_t result;
hash.Finalize(reinterpret_cast<uint8_t *>(&result));
return result;
}
// Function to split a string into a sub-strings delimited by a character
// Taken from stack overflow answer to https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string
// By Evan Teran
@ -126,26 +111,6 @@ inline std::string slurp(std::ifstream &in)
return sstr.str();
}
#define EXIT_WITH_ERROR(...) exitWithError(__FILE__, __LINE__, __VA_ARGS__)
inline void exitWithError [[noreturn]] (const char *fileName, const int lineNumber, const char *format, ...)
{
char *outstr = 0;
va_list ap;
va_start(ap, format);
int ret = vasprintf(&outstr, format, ap);
if (ret < 0) exit(-1);
std::string outString = outstr;
free(outstr);
char info[1024];
snprintf(info, sizeof(info) - 1, " + From %s:%d\n", fileName, lineNumber);
outString += info;
throw std::runtime_error(outString.c_str());
}
// Loads a string from a given file
inline bool loadStringFromFile(std::string &dst, const std::string path)
{
@ -191,70 +156,18 @@ inline std::vector<T> splitVector(const T size, const T n)
return subSizes;
}
inline std::string simplifyMove(const std::string &move)
{
std::string simpleMove;
typedef _uint128_t hash_t;
bool isEmptyMove = true;
for (size_t i = 0; i < move.size(); i++)
if (move[i] != '.' && move[i] != '|')
inline hash_t calculateMetroHash(uint8_t *data, size_t size)
{
simpleMove += move[i];
isEmptyMove = false;
}
if (isEmptyMove) return ".";
return simpleMove;
MetroHash128 hash;
hash.Update(data, size);
hash_t result;
hash.Finalize(reinterpret_cast<uint8_t *>(&result));
return result;
}
inline bool getBitFlag(const uint8_t value, const uint8_t idx)
inline hash_t calculateStateHash(const NESInstance* nes)
{
if (((idx == 7) && (value & 0b10000000)) ||
((idx == 6) && (value & 0b01000000)) ||
((idx == 5) && (value & 0b00100000)) ||
((idx == 4) && (value & 0b00010000)) ||
((idx == 3) && (value & 0b00001000)) ||
((idx == 2) && (value & 0b00000100)) ||
((idx == 1) && (value & 0b00000010)) ||
((idx == 0) && (value & 0b00000001))) return true;
return false;
return calculateMetroHash(nes->getLowMem(), _LOW_MEM_SIZE);
}
inline size_t countButtonsPressedString(const std::string &input)
{
size_t count = 0;
for (size_t i = 0; i < input.size(); i++)
if (input[i] != '.') count++;
return count;
};
template <typename T>
inline uint16_t countButtonsPressedNumber(const T &input)
{
uint16_t count = 0;
if (input & 0b0000000000000001) count++;
if (input & 0b0000000000000010) count++;
if (input & 0b0000000000000100) count++;
if (input & 0b0000000000001000) count++;
if (input & 0b0000000000010000) count++;
if (input & 0b0000000000100000) count++;
if (input & 0b0000000001000000) count++;
if (input & 0b0000000010000000) count++;
if (input & 0b0000000100000000) count++;
if (input & 0b0000001000000000) count++;
if (input & 0b0000010000000000) count++;
if (input & 0b0000100000000000) count++;
if (input & 0b0001000000000000) count++;
if (input & 0b0010000000000000) count++;
if (input & 0b0100000000000000) count++;
if (input & 0b1000000000000000) count++;
return count;
};
static auto moveCountComparerString = [](const std::string &a, const std::string &b)
{
return countButtonsPressedString(a) < countButtonsPressedString(b);
};
static auto moveCountComparerNumber = [](const uint8_t a, const uint8_t b)
{
return countButtonsPressedNumber(a) < countButtonsPressedNumber(b);
};