Skip to content

Commit 3ad2753

Browse files
committed
project: refactored string_format and printf to fmt
1 parent 3c1f064 commit 3ad2753

53 files changed

Lines changed: 421 additions & 483 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

premake/fmt.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
group "externals"
2+
project "fmt"
3+
uuid "ffc1977c-c7bc-4d62-b7c7-6fa0b55c3625"
4+
kind "StaticLib"
5+
language "c++"
6+
location "../build/libs/fmt"
7+
includedirs {
8+
"../externals/fmt/include"
9+
}
10+
files {
11+
"../externals/fmt/src/*.cc",
12+
}

premake5.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ include "premake/glad.lua"
129129
include "premake/imgui.lua"
130130
include "premake/lzma.lua"
131131
include "premake/miniz.lua"
132+
include "premake/fmt.lua"
132133

133134
project "core"
134135
uuid "176665c5-37ff-4a42-bef8-02edaeb1b426"
@@ -143,6 +144,7 @@ project "core"
143144
"externals/libchdr/src",
144145
"externals/EventBus/lib/include",
145146
"externals/magic_enum/include",
147+
"externals/fmt/include",
146148
}
147149

148150
files {
@@ -161,6 +163,7 @@ project "core"
161163
"lzma",
162164
"flac",
163165
"chdr",
166+
"fmt",
164167
}
165168

166169
if _ACTION ~= nil then
@@ -184,6 +187,7 @@ project "avocado"
184187
"externals/filesystem/include",
185188
"externals/EventBus/lib/include",
186189
"externals/magic_enum/include",
190+
"externals/fmt/include",
187191
}
188192

189193
links {
@@ -192,6 +196,7 @@ project "avocado"
192196
"chdr",
193197
"lzma",
194198
"flac",
199+
"fmt",
195200
}
196201

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

306-
-- filter {"system:android"}
307-
-- kind "SharedLib"
308-
309311
project "avocado_autotest"
310312
uuid "fcc880bc-c6fe-4b2b-80dc-d247345a1274"
311313
kind "ConsoleApp"
@@ -324,7 +326,4 @@ project "avocado_autotest"
324326

325327
links {
326328
"core"
327-
}
328-
329-
-- filter {"system:android"}
330-
-- kind "SharedLib"
329+
}

src/bios/functions.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "functions.h"
2-
#include <unordered_map>
2+
#include <fmt/color.h>
3+
#include <fmt/core.h>
4+
#include <magic_enum.hpp>
35
#include "debugger/debugger.h"
46
#include "system.h"
5-
#include "utils/logger.h"
67
#include "utils/string.h"
78

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

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

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

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

93-
logger::printf("🔴Unresolved exception⚪️: 🅱️%s❌‍⚪️ (%u), epc=🔵0x%08x⚪️, ra=🔵0x%08x\n",
94-
cause.getExceptionName(), cause.exception, epc, sys->cpu->reg[31]);
94+
#define RED fmt::fg(fmt::terminal_color::red)
95+
#define WHITE fmt::fg(fmt::terminal_color::white)
96+
#define BLUE fmt::fg(fmt::terminal_color::blue)
97+
#define BOLD fmt::emphasis::bold
98+
99+
// Unresolved exception: {EXCEPTION_NAME} (EXCEPTION_NUMBER), epc={EPC}, ra={RA}
100+
fmt::print(RED, "Unresolved exception");
101+
fmt::print(": ");
102+
fmt::print(WHITE | BOLD, "{}", magic_enum::enum_name(cause.exception));
103+
fmt::print(" ({})", static_cast<int>(cause.exception));
104+
fmt::print(", epc=");
105+
fmt::print(BLUE, "0x{:08x}", epc);
106+
fmt::print(", ra=");
107+
fmt::print(BLUE, "0x{:08x}\n", sys->cpu->reg[31]);
108+
95109
for (uint32_t addr = epc - howManyInstructionsToDisassemble * 4; addr <= epc; addr += 4) {
96110
auto opcode = mips::Opcode(sys->readMemory32(addr));
97111
auto ins = debugger::decodeInstruction(opcode);
98112

113+
fmt::print(BLUE, "0x{:08x}: ", addr);
114+
fmt::print(WHITE, "{:<8} {}", ins.mnemonic, ins.parameters);
115+
99116
if (addr == epc) {
100-
ins.parameters += "🅱️ <---- Caused the exception";
117+
fmt::print(WHITE | BOLD, " <---- Caused the exception\n");
118+
} else {
119+
fmt::print("\n");
101120
}
102-
103-
logger::printf("🔵0x%08x:⚪️ %-8s %s\n", addr, ins.mnemonic.c_str(), ins.parameters.c_str());
104121
}
105-
logger::printf("🔴This is most likely bug in Avocado, please report it.\n");
106-
logger::printf("🔴 🅱️Emulation stopped.\n");
122+
fmt::print(RED, "This is most likely bug in Avocado, please report it.\n");
123+
fmt::print(RED | BOLD, "Emulation stopped.\n");
107124

108125
sys->state = System::State::halted;
109126
return false;

src/bios/functions.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22
#include <cstdint>
33
#include <functional>
4+
#include <string>
45
#include <unordered_map>
5-
#include "debugger/debugger.h"
6-
#include "system.h"
7-
#include "utils/logger.h"
8-
#include "utils/string.h"
6+
#include <vector>
7+
8+
struct System;
99

1010
namespace bios {
1111

src/config.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "config.h"
2+
#include <fmt/core.h>
23
#include "utils/file.h"
34

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

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

200200
// Repair these with invalid types
201201
if (newField.value().type() != oldField.value().type()) {
202-
printf("[CONFIG]: Invalid type of field \"%s\" (%s), changing to %s\n", newField.key().c_str(), newField.value().type_name(),
203-
oldField.value().type_name());
204202
newconfig[oldField.key()] = oldField.value();
205203
continue;
206204
}

src/cpu/cop0.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ void COP0::write(int reg, uint32_t value) {
2929
case 11: bpcm = value; break;
3030
case 12:
3131
if (value & (1 << 17)) {
32-
// printf("Panic, SwC not handled\n");
33-
// cpu->state = CPU::State::halted;
32+
// TODO: Handle SwC
3433
}
3534
status._reg = value;
3635
break;
3736
case 13:
3837
cause.interruptPending &= 3;
3938
cause.interruptPending |= (value & 0x300) >> 8;
40-
// TODO: Check software interrupt [psxtest_cpx: Interrupts]
4139
break;
4240
case 14: epc = value; break;
4341
default: break;

src/cpu/cop0.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct COP0 {
3939
};
4040
// cop0r13 cause, ro, bit8-9 are rw
4141
union CAUSE {
42-
enum class Exception : uint32_t {
42+
enum class Exception {
4343
interrupt = 0,
4444
addressErrorLoad = 4,
4545
addressErrorStore = 5,
@@ -72,22 +72,6 @@ struct COP0 {
7272
*/
7373
};
7474

75-
const char* getExceptionName() const {
76-
switch (exception) {
77-
case Exception::interrupt: return "interrupt";
78-
case Exception::addressErrorLoad: return "addressErrorLoad";
79-
case Exception::addressErrorStore: return "addressErrorStore";
80-
case Exception::busErrorInstruction: return "busErrorInstruction";
81-
case Exception::busErrorData: return "busErrorData";
82-
case Exception::syscall: return "syscall";
83-
case Exception::breakpoint: return "breakpoint";
84-
case Exception::reservedInstruction: return "reservedInstruction";
85-
case Exception::coprocessorUnusable: return "coprocessorUnusable";
86-
case Exception::arithmeticOverflow: return "arithmeticOverflow";
87-
default: return "unknown";
88-
}
89-
}
90-
9175
void clearForException() {
9276
auto _interruptPending = interruptPending;
9377
_reg = 0;

src/cpu/instructions.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,11 @@ void dummy(CPU *cpu, Opcode i) {
216216

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

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

226-
void notImplemented(CPU *cpu, Opcode i) {
227-
printf("Opcode not implemented at 0x%08x: 0x%08x\n", cpu->PC, i.opcode);
228-
cpu->sys->state = System::State::halted;
229-
// TODO: cpu->sys kinda sucks
230-
}
231-
232224
void special(CPU *cpu, Opcode i) {
233225
const auto &instruction = SpecialTable[i.fun];
234226
instruction.instruction(cpu, i);
@@ -604,10 +596,7 @@ void op_cop2(CPU *cpu, Opcode i) {
604596
gte::Command command(i.opcode);
605597
if (i.opcode & (1 << 25)) {
606598
cpu->gte.log.push_back({GTE::GTE_ENTRY::MODE::func, command.cmd, 0});
607-
608-
if (!cpu->gte.command(command)) {
609-
// printf("Unhandled gte command 0x%x\n", command.cmd);
610-
}
599+
cpu->gte.command(command);
611600
return;
612601
}
613602

src/cpu/instructions.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ void exception(CPU* cpu, COP0::CAUSE::Exception cause);
1717

1818
void dummy(CPU* cpu, Opcode i);
1919
void invalid(CPU* cpu, Opcode i);
20-
void notImplemented(CPU* cpu, Opcode i);
2120
void special(CPU* cpu, Opcode i);
2221
void branch(CPU* cpu, Opcode i);
2322

0 commit comments

Comments
 (0)