added json library for configuration purposes

This commit is contained in:
Jakub Czekański 2017-09-02 17:38:32 +02:00
parent 53bd8dd02b
commit 16a24e0a70
12 changed files with 141 additions and 18 deletions

1
.gitignore vendored
View file

@ -62,3 +62,4 @@ spu.bin
imgui.ini
todo.txt
config.json

3
.gitmodules vendored
View file

@ -8,3 +8,6 @@
[submodule "externals/glm"]
path = externals/glm
url = https://github.com/g-truc/glm.git
[submodule "externals/json"]
path = externals/json
url = https://github.com/nlohmann/json.git

1
externals/json vendored Submodule

@ -0,0 +1 @@
Subproject commit c0d511ea500e698061a4b288bd77ce43bd76fa70

View file

@ -1,5 +1,5 @@
workspace "Avocado"
configurations { "Debug", "Release" }
workspace "Avocado"
configurations { "Debug", "Release", "FastDebug" }
newoption {
trigger = "disable-load-delay-slots",
@ -59,7 +59,8 @@ project "Avocado"
"externals/imgui",
"externals/SDL2/include",
"externals/glad/include",
"externals/glm"
"externals/glm",
"externals/json/src"
}
files {
@ -80,7 +81,12 @@ project "Avocado"
filter "configurations:Release"
defines { "NDEBUG" }
optimize "Full"
filter "configurations:FastDebug"
defines { "DEBUG" }
symbols "On"
optimize "Speed"
configuration { "windows" }
defines { "WIN32" }
libdirs { os.findlib("SDL2") }

View file

@ -53,7 +53,7 @@ class CDROM : public Device {
}
}
StatusCode() : _reg(0) {}
StatusCode() : _reg(0) { shellOpen = true; }
};
union CDROM_Status {

View file

@ -438,11 +438,10 @@ bool CPU::loadExeFile(std::string exePath) {
return true;
}
bool CPU::loadBios(std::string name) {
std::string path = "data/bios/";
auto _bios = getFileContents(path + name);
bool CPU::loadBios(std::string path) {
auto _bios = getFileContents(path);
if (_bios.empty()) {
printf("Cannot open BIOS %s", name.c_str());
printf("Cannot open BIOS %s", path.c_str());
return false;
}
assert(_bios.size() == 512 * 1024);

View file

@ -0,0 +1,17 @@
#include "config.h"
#include "utils/file.h"
const char* CONFIG_NAME = "config.json";
nlohmann::json config = {{"initialized", false}, {"bios", ""}, {"extension", ""}, {"iso", ""}};
void saveConfigFile(const char* configName) { putFileContents(configName, config.dump(4)); }
void loadConfigFile(const char* configName) {
auto file = getFileContents(configName);
if (file.empty()) {
saveConfigFile(configName);
return;
}
config = nlohmann::json::parse(file);
}

View file

@ -0,0 +1,7 @@
#pragma once
#include <json.hpp>
extern const char* CONFIG_NAME;
extern nlohmann::json config;
void saveConfigFile(const char* configName);
void loadConfigFile(const char* configName);

View file

@ -1,8 +1,12 @@
#include "gui.h"
#include <imgui.h>
#include <cstdint>
#include <filesystem>
#include "utils/string.h"
#include "gte.h"
#include "platform/windows/config.h"
using namespace std::experimental::filesystem::v1;
const char *mapIo(uint32_t address) {
address -= 0x1f801000;
@ -39,6 +43,8 @@ bool skipRender = false;
bool showIo = false;
bool exitProgram = false;
bool showBiosWindow = false;
void replayCommands(mips::gpu::GPU *gpu, int to) {
auto commands = gpu->gpuLogList;
gpu->vram = gpu->prevVram;
@ -302,6 +308,48 @@ void ioLogWindow(mips::CPU *cpu) {
#endif
}
void biosSelectionWindow() {
static bool biosesFound = false;
static std::vector<std::string> bioses;
static int currentBiosIndex = 0;
if (!biosesFound) {
bioses.clear();
auto dir = directory_iterator("data/bios");
for (auto &e : dir) {
if (!is_regular_file(e)) continue;
auto path = e.path().string();
auto ext = getExtension(path);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (ext == "bin" || ext == "rom") {
bioses.push_back(path);
}
}
biosesFound = true;
}
ImGui::Begin("BIOS");
ImGui::ListBox("BIOS", &currentBiosIndex, [](void *data, int idx, const char **out_text) {
const std::vector<std::string> *v = (std::vector<std::string> *)data;
*out_text = v->at(idx).c_str();
return true;
}, (void *)&bioses, (int)bioses.size());
if (ImGui::Button("Select") && currentBiosIndex < bioses.size()) {
config["bios"] = bioses[currentBiosIndex];
config["initialized"] = true;
biosesFound = false;
showBiosWindow = false;
// Send event?
}
ImGui::End();
}
void renderImgui(mips::CPU *cpu) {
auto gte = cpu->gte;
@ -343,6 +391,10 @@ void renderImgui(mips::CPU *cpu) {
ImGui::MenuItem("Disassembly (slow)", "F6", &cpu->disassemblyEnabled);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Options")) {
if (ImGui::MenuItem("BIOS", NULL)) showBiosWindow = true;
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
@ -352,5 +404,20 @@ void renderImgui(mips::CPU *cpu) {
gpuLogWindow(cpu);
ioWindow(cpu);
if (showBiosWindow) biosSelectionWindow();
if (!config["initialized"]) {
ImGui::Begin("Avocado");
ImGui::Text("Avocado needs to be set up before running.");
ImGui::Text("You need one of BIOS files placed in data/bios directory.");
if (ImGui::Button("Select BIOS file")) {
showBiosWindow = true;
config["initialized"] = true;
}
ImGui::End();
}
ImGui::Render();
}

View file

@ -2,12 +2,15 @@
#include <string>
#include <algorithm>
#include <SDL.h>
#include <json.hpp>
#include "renderer/opengl/opengl.h"
#include "utils/string.h"
#include "mips.h"
#include "imgui/imgui_impl_sdl_gl3.h"
#include "gui.h"
#include "utils/cue/cueParser.h"
#include "utils/file.h"
#include "platform/windows/config.h"
#undef main
@ -89,8 +92,7 @@ void loadFile(std::unique_ptr<mips::CPU> &cpu, std::string path) {
}
int start(int argc, char **argv) {
std::string bios = "SCPH1001.bin";
std::string iso = "D:/Games/!PSX/Doom/Doom (Track 1).bin";
loadConfigFile(CONFIG_NAME);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Cannot init SDL\n");
@ -123,18 +125,27 @@ int start(int argc, char **argv) {
}
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
ImGui_ImplSdlGL3_Init(window);
std::unique_ptr<mips::CPU> cpu = std::make_unique<mips::CPU>();
if (cpu->loadBios(bios)) {
std::string bios = config["bios"];
if (!bios.empty() && cpu->loadBios(bios)) {
printf("Using bios %s\n", bios.c_str());
}
// cpu->loadExpansion("data/bios/expansion.rom");
cpu->cdrom->setShell(true); // open shell
loadFile(cpu, iso);
std::string extension = config["extension"];
if (!extension.empty() && cpu->loadExpansion(extension)) {
printf("Using extension %s\n", extension.c_str());
}
std::string iso = config["iso"];
if (!iso.empty()) {
loadFile(cpu, iso);
printf("Using iso %s\n", iso.c_str());
}
if (!config["initialized"]) cpu->state = mips::CPU::State::stop;
float startTime = SDL_GetTicks() / 1000.f;
float fps = 0.f;
@ -233,6 +244,7 @@ int start(int argc, char **argv) {
SDL_SetWindowTitle(window, title.c_str());
SDL_GL_SwapWindow(window);
}
saveConfigFile(CONFIG_NAME);
ImGui_ImplSdlGL3_Shutdown();
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);

View file

@ -48,8 +48,8 @@ bool fileExists(std::string name) {
return exists;
}
std::vector<unsigned char> getFileContents(std::string name) {
std::vector<unsigned char> contents;
std::vector<uint8_t> getFileContents(std::string name) {
std::vector<uint8_t> contents;
FILE *f = fopen(name.c_str(), "rb");
if (!f) return contents;
@ -74,6 +74,15 @@ void putFileContents(std::string name, std::vector<unsigned char> &contents) {
fclose(f);
}
void putFileContents(std::string name, std::string contents) {
FILE *f = fopen(name.c_str(), "wb");
if (!f) return;
fwrite(&contents[0], 1, contents.size(), f);
fclose(f);
}
std::string getFileContentsAsString(std::string name) {
std::string contents;

View file

@ -9,5 +9,6 @@ std::string getExtension(std::string name);
bool fileExists(std::string name);
std::vector<unsigned char> getFileContents(std::string name);
void putFileContents(std::string name, std::vector<unsigned char> &contents);
void putFileContents(std::string name, std::string contents);
std::string getFileContentsAsString(std::string name);
size_t getFileSize(std::string name);