Skip to content
Permalink
Browse files

fmt for string formating and logging (#61)

* externals: added fmt library

* project: refactored string_format and printf to fmt
  • Loading branch information
JaCzekanski committed Sep 8, 2019
1 parent a9d64b5 commit 2ec57f5ca475063befb8077020586ba07e6adcef
Showing with 425 additions and 483 deletions.
  1. +3 βˆ’0 .gitmodules
  2. +1 βˆ’0 externals/fmt
  3. +12 βˆ’0 premake/fmt.lua
  4. +6 βˆ’7 premake5.lua
  5. +29 βˆ’12 src/bios/functions.cpp
  6. +4 βˆ’4 src/bios/functions.h
  7. +1 βˆ’3 src/config.cpp
  8. +1 βˆ’3 src/cpu/cop0.cpp
  9. +1 βˆ’17 src/cpu/cop0.h
  10. +1 βˆ’12 src/cpu/instructions.cpp
  11. +0 βˆ’1 src/cpu/instructions.h
  12. +34 βˆ’34 src/debugger/debugger.cpp
  13. +30 βˆ’25 src/device/cdrom/cdrom.cpp
  14. +40 βˆ’37 src/device/cdrom/commands.cpp
  15. +0 βˆ’1 src/device/controller/peripherals/abstract_device.cpp
  16. +6 βˆ’4 src/device/controller/peripherals/analog_controller.cpp
  17. +3 βˆ’3 src/device/controller/peripherals/digital_controller.cpp
  18. +1 βˆ’1 src/device/controller/peripherals/digital_controller.h
  19. +8 βˆ’7 src/device/controller/peripherals/memory_card.cpp
  20. +2 βˆ’2 src/device/controller/peripherals/mouse.cpp
  21. +2 βˆ’2 src/device/dma/dma.cpp
  22. +12 βˆ’8 src/device/dma/dma_channel.cpp
  23. +8 βˆ’11 src/device/gpu/gpu.cpp
  24. +7 βˆ’7 src/device/mdec/mdec.cpp
  25. +3 βˆ’3 src/device/memory_control.cpp
  26. +3 βˆ’3 src/device/serial.cpp
  27. +4 βˆ’3 src/device/spu/spu.cpp
  28. +4 βˆ’3 src/device/timer.cpp
  29. +7 βˆ’6 src/disc/disc.cpp
  30. +7 βˆ’7 src/disc/format/chd_format.cpp
  31. +2 βˆ’1 src/disc/format/cue.cpp
  32. +10 βˆ’10 src/disc/format/cue_parser.cpp
  33. +3 βˆ’3 src/disc/position.cpp
  34. +15 βˆ’10 src/platform/windows/gui/debug.cpp
  35. +11 βˆ’5 src/platform/windows/gui/debug/cdrom/cdrom.cpp
  36. +10 βˆ’18 src/platform/windows/gui/debug/cpu/cpu.cpp
  37. +2 βˆ’2 src/platform/windows/gui/debug/timers/timers.cpp
  38. +25 βˆ’22 src/platform/windows/gui/debug_spu.cpp
  39. +3 βˆ’3 src/platform/windows/gui/file.cpp
  40. +4 βˆ’4 src/platform/windows/gui/images.cpp
  41. +11 βˆ’7 src/platform/windows/gui/options.cpp
  42. +2 βˆ’2 src/platform/windows/gui/options/memory_card/memory_card.cpp
  43. +3 βˆ’3 src/platform/windows/input/key.cpp
  44. +4 βˆ’4 src/platform/windows/input/sdl_input_manager.cpp
  45. +16 βˆ’16 src/platform/windows/main.cpp
  46. +3 βˆ’2 src/platform/windows/sound/sound.cpp
  47. +7 βˆ’7 src/renderer/opengl/opengl.cpp
  48. +2 βˆ’2 src/renderer/opengl/shader/framebuffer.cpp
  49. +3 βˆ’2 src/renderer/opengl/shader/program.cpp
  50. +42 βˆ’43 src/system.cpp
  51. +0 βˆ’59 src/utils/logger.cpp
  52. +0 βˆ’5 src/utils/logger.h
  53. +7 βˆ’6 src/utils/psf.cpp
  54. +0 βˆ’20 src/utils/string.cpp
  55. +0 βˆ’1 src/utils/string.h
@@ -35,3 +35,6 @@
[submodule "externals/magic_enum"]
path = externals/magic_enum
url = https://github.com/Neargye/magic_enum.git
[submodule "externals/fmt"]
path = externals/fmt
url = https://github.com/fmtlib/fmt
Submodule fmt added at 7512a5
@@ -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",
}
@@ -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"
@@ -143,6 +144,7 @@ project "core"
"externals/libchdr/src",
"externals/EventBus/lib/include",
"externals/magic_enum/include",
"externals/fmt/include",
}

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

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

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

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

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

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

links {
"core"
}

-- filter {"system:android"}
-- kind "SharedLib"
}
@@ -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 {
@@ -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));
@@ -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);
}
@@ -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);
@@ -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;
@@ -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 {

@@ -1,4 +1,5 @@
#include "config.h"
#include <fmt/core.h>
#include "utils/file.h"

const char* CONFIG_NAME = "config.json";
@@ -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;
}
@@ -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;
@@ -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,
@@ -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;
@@ -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);
@@ -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;
}

@@ -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);

0 comments on commit 2ec57f5

Please sign in to comment.
You can’t perform that action at this time.