Skip to content

Commit

Permalink
project: refactored string_format and printf to fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Sep 8, 2019
1 parent 3c1f064 commit 3ad2753
Show file tree
Hide file tree
Showing 53 changed files with 421 additions and 483 deletions.
12 changes: 12 additions & 0 deletions premake/fmt.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
group "externals"
project "fmt"
uuid "ffc1977c-c7bc-4d62-b7c7-6fa0b55c3625"
kind "StaticLib"
language "c++"
location "../build/libs/fmt"
includedirs {
"../externals/fmt/include"
}
files {
"../externals/fmt/src/*.cc",
}
13 changes: 6 additions & 7 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ include "premake/glad.lua"
include "premake/imgui.lua"
include "premake/lzma.lua"
include "premake/miniz.lua"
include "premake/fmt.lua"

project "core"
uuid "176665c5-37ff-4a42-bef8-02edaeb1b426"
Expand All @@ -143,6 +144,7 @@ project "core"
"externals/libchdr/src",
"externals/EventBus/lib/include",
"externals/magic_enum/include",
"externals/fmt/include",
}

files {
Expand All @@ -161,6 +163,7 @@ project "core"
"lzma",
"flac",
"chdr",
"fmt",
}

if _ACTION ~= nil then
Expand All @@ -184,6 +187,7 @@ project "avocado"
"externals/filesystem/include",
"externals/EventBus/lib/include",
"externals/magic_enum/include",
"externals/fmt/include",
}

links {
Expand All @@ -192,6 +196,7 @@ project "avocado"
"chdr",
"lzma",
"flac",
"fmt",
}

filter "options:headless"
Expand Down Expand Up @@ -303,9 +308,6 @@ project "avocado_test"
"core"
}

-- filter {"system:android"}
-- kind "SharedLib"

project "avocado_autotest"
uuid "fcc880bc-c6fe-4b2b-80dc-d247345a1274"
kind "ConsoleApp"
Expand All @@ -324,7 +326,4 @@ project "avocado_autotest"

links {
"core"
}

-- filter {"system:android"}
-- kind "SharedLib"
}
41 changes: 29 additions & 12 deletions src/bios/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "functions.h"
#include <unordered_map>
#include <fmt/color.h>
#include <fmt/core.h>
#include <magic_enum.hpp>
#include "debugger/debugger.h"
#include "system.h"
#include "utils/logger.h"
#include "utils/string.h"

namespace bios {
Expand All @@ -23,7 +24,7 @@ Function::Function(const std::string& prototype, std::function<bool(System* sys)
auto delim = sArg.find_last_of(' ');

if (delim == std::string::npos) {
throw std::runtime_error(string_format("%s -> Invalid parameter without type", prototype.c_str()).c_str());
throw std::runtime_error(fmt::format("{} -> Invalid parameter without type", prototype));
}

auto sType = trim(sArg.substr(0, delim));
Expand All @@ -44,7 +45,7 @@ Function::Function(const std::string& prototype, std::function<bool(System* sys)
else if (sType == "void*")
arg.type = Type::POINTER;
else
throw std::runtime_error(string_format("%s -> Invalid parameter type", prototype.c_str()).c_str());
throw std::runtime_error(fmt::format("{} -> Invalid parameter type", prototype));

this->args.push_back(arg);
}
Expand All @@ -65,7 +66,7 @@ inline bool dbgOutputString(System* sys) {
for (int i = 0; i < 80; i++) {
char c = sys->readMemory8(sys->cpu->reg[4] + i);
if (c == 0) {
printf("\n");
fmt::print("\n");
return false;
}
putchar(c);
Expand All @@ -90,20 +91,36 @@ inline bool unresolvedException(System* sys) {
auto cause = sys->cpu->cop0.cause;
uint32_t epc = sys->cpu->cop0.epc;

logger::printf("🔴Unresolved exception⚪️: 🅱️%s❌‍⚪️ (%u), epc=🔵0x%08x⚪️, ra=🔵0x%08x\n",
cause.getExceptionName(), cause.exception, epc, sys->cpu->reg[31]);
#define RED fmt::fg(fmt::terminal_color::red)
#define WHITE fmt::fg(fmt::terminal_color::white)
#define BLUE fmt::fg(fmt::terminal_color::blue)
#define BOLD fmt::emphasis::bold

// Unresolved exception: {EXCEPTION_NAME} (EXCEPTION_NUMBER), epc={EPC}, ra={RA}
fmt::print(RED, "Unresolved exception");
fmt::print(": ");
fmt::print(WHITE | BOLD, "{}", magic_enum::enum_name(cause.exception));
fmt::print(" ({})", static_cast<int>(cause.exception));
fmt::print(", epc=");
fmt::print(BLUE, "0x{:08x}", epc);
fmt::print(", ra=");
fmt::print(BLUE, "0x{:08x}\n", sys->cpu->reg[31]);

for (uint32_t addr = epc - howManyInstructionsToDisassemble * 4; addr <= epc; addr += 4) {
auto opcode = mips::Opcode(sys->readMemory32(addr));
auto ins = debugger::decodeInstruction(opcode);

fmt::print(BLUE, "0x{:08x}: ", addr);
fmt::print(WHITE, "{:<8} {}", ins.mnemonic, ins.parameters);

if (addr == epc) {
ins.parameters += "🅱️ <---- Caused the exception";
fmt::print(WHITE | BOLD, " <---- Caused the exception\n");
} else {
fmt::print("\n");
}

logger::printf("🔵0x%08x:⚪️ %-8s %s\n", addr, ins.mnemonic.c_str(), ins.parameters.c_str());
}
logger::printf("🔴This is most likely bug in Avocado, please report it.\n");
logger::printf("🔴 🅱️Emulation stopped.\n");
fmt::print(RED, "This is most likely bug in Avocado, please report it.\n");
fmt::print(RED | BOLD, "Emulation stopped.\n");

sys->state = System::State::halted;
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/bios/functions.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once
#include <cstdint>
#include <functional>
#include <string>
#include <unordered_map>
#include "debugger/debugger.h"
#include "system.h"
#include "utils/logger.h"
#include "utils/string.h"
#include <vector>

struct System;

namespace bios {

Expand Down
4 changes: 1 addition & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <fmt/core.h>
#include "utils/file.h"

const char* CONFIG_NAME = "config.json";
Expand Down Expand Up @@ -192,15 +193,12 @@ json fixObject(json oldconfig, json newconfig) {

// Add nonexisting fields
if (newField == newconfig.end()) {
printf("Config: No field %s \n", oldField.key().c_str());
newconfig.emplace(oldField.key(), oldField.value());
continue;
}

// Repair these with invalid types
if (newField.value().type() != oldField.value().type()) {
printf("[CONFIG]: Invalid type of field \"%s\" (%s), changing to %s\n", newField.key().c_str(), newField.value().type_name(),
oldField.value().type_name());
newconfig[oldField.key()] = oldField.value();
continue;
}
Expand Down
4 changes: 1 addition & 3 deletions src/cpu/cop0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ void COP0::write(int reg, uint32_t value) {
case 11: bpcm = value; break;
case 12:
if (value & (1 << 17)) {
// printf("Panic, SwC not handled\n");
// cpu->state = CPU::State::halted;
// TODO: Handle SwC
}
status._reg = value;
break;
case 13:
cause.interruptPending &= 3;
cause.interruptPending |= (value & 0x300) >> 8;
// TODO: Check software interrupt [psxtest_cpx: Interrupts]
break;
case 14: epc = value; break;
default: break;
Expand Down
18 changes: 1 addition & 17 deletions src/cpu/cop0.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct COP0 {
};
// cop0r13 cause, ro, bit8-9 are rw
union CAUSE {
enum class Exception : uint32_t {
enum class Exception {
interrupt = 0,
addressErrorLoad = 4,
addressErrorStore = 5,
Expand Down Expand Up @@ -72,22 +72,6 @@ struct COP0 {
*/
};

const char* getExceptionName() const {
switch (exception) {
case Exception::interrupt: return "interrupt";
case Exception::addressErrorLoad: return "addressErrorLoad";
case Exception::addressErrorStore: return "addressErrorStore";
case Exception::busErrorInstruction: return "busErrorInstruction";
case Exception::busErrorData: return "busErrorData";
case Exception::syscall: return "syscall";
case Exception::breakpoint: return "breakpoint";
case Exception::reservedInstruction: return "reservedInstruction";
case Exception::coprocessorUnusable: return "coprocessorUnusable";
case Exception::arithmeticOverflow: return "arithmeticOverflow";
default: return "unknown";
}
}

void clearForException() {
auto _interruptPending = interruptPending;
_reg = 0;
Expand Down
13 changes: 1 addition & 12 deletions src/cpu/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,11 @@ void dummy(CPU *cpu, Opcode i) {

void invalid(CPU *cpu, Opcode i) {
UNUSED(i);
// printf("Invalid opcode at 0x%08x: 0x%08x\n", cpu->PC, i.opcode);
// cpu->sys->state = System::State::halted;

exception(cpu, COP0::CAUSE::Exception::reservedInstruction);
// TODO: cpu->sys kinda sucks
}

void notImplemented(CPU *cpu, Opcode i) {
printf("Opcode not implemented at 0x%08x: 0x%08x\n", cpu->PC, i.opcode);
cpu->sys->state = System::State::halted;
// TODO: cpu->sys kinda sucks
}

void special(CPU *cpu, Opcode i) {
const auto &instruction = SpecialTable[i.fun];
instruction.instruction(cpu, i);
Expand Down Expand Up @@ -604,10 +596,7 @@ void op_cop2(CPU *cpu, Opcode i) {
gte::Command command(i.opcode);
if (i.opcode & (1 << 25)) {
cpu->gte.log.push_back({GTE::GTE_ENTRY::MODE::func, command.cmd, 0});

if (!cpu->gte.command(command)) {
// printf("Unhandled gte command 0x%x\n", command.cmd);
}
cpu->gte.command(command);
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/cpu/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ void exception(CPU* cpu, COP0::CAUSE::Exception cause);

void dummy(CPU* cpu, Opcode i);
void invalid(CPU* cpu, Opcode i);
void notImplemented(CPU* cpu, Opcode i);
void special(CPU* cpu, Opcode i);
void branch(CPU* cpu, Opcode i);

Expand Down

0 comments on commit 3ad2753

Please sign in to comment.