Skip to content

Commit

Permalink
Add additional logging support with spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwalsh0 committed Sep 18, 2020
1 parent 9fb8a13 commit d2d50cd
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 80 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "deps/spdlog"]
path = deps/spdlog
url = https://github.com/gabime/spdlog.git
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ cmake_minimum_required(VERSION 3.10.2)
project(mboy_core)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DMBOY_DEBUG")

# spdlog
add_subdirectory(deps/spdlog)

# core
include_directories(include)
file(GLOB CORE_SOURCES "src/*/*.cpp", "include/*.h")

# output
add_library(mboy_core STATIC ${CORE_SOURCES})
target_link_libraries(mboy_core spdlog)
target_include_directories(mboy_core PUBLIC include)
1 change: 1 addition & 0 deletions deps/spdlog
Submodule spdlog added at 9cd25d
11 changes: 4 additions & 7 deletions include/CPU.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#ifndef MY_APPLICATION_CPU_H
#define MY_APPLICATION_CPU_H
#ifndef CPU_H
#define CPU_H

#include "Types.h"

#include "MemoryHook.h"
#include "LogFile.h"
#include "Memory.h"
#include "Log.h"

enum Register {
A,
Expand Down Expand Up @@ -56,8 +55,6 @@ class CPU {
bool interruptFlagsSerial = false;
bool interruptFlagsJoypad = false;

LogFile logFile;

CPU(Memory* memory);
u_int8_t get_8(Register cpuRegister);
u_int8_t get_f();
Expand All @@ -77,4 +74,4 @@ class CPU {
};


#endif //MY_APPLICATION_CPU_H
#endif
2 changes: 0 additions & 2 deletions include/GPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "Pixels.h"
#include "Display.h"
#include "GUI.h"
#include "LogFile.h"
#include "Config.h"
#include "Memory.h"
#include <chrono>
Expand All @@ -32,7 +31,6 @@ class GPU {
bool hblankInterrupt;

GUI* gui;
LogFile logFile;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
u_int16_t frameCount = 0;
u_int16_t hdmaTarget = 0;
Expand Down
29 changes: 29 additions & 0 deletions include/Log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef LOG_H
#define LOG_H

#ifdef MBOY_DEBUG

#include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h"

#define MBOY_TRACE(...) spdlog::trace(__VA_ARGS__)
#define MBOY_INFO(...) spdlog::info(__VA_ARGS__)
#define MBOY_WARN(...) spdlog::warn(__VA_ARGS__)
#define MBOY_ERROR(...) spdlog::error(__VA_ARGS__)

inline static void initLogging() {
spdlog::cfg::load_env_levels();
}

#else

#define MBOY_TRACE(...)
#define MBOY_INFO(...)
#define MBOY_WARN(...)
#define MBOY_ERROR(...)

inline static void initLogging() {}

#endif

#endif
19 changes: 0 additions & 19 deletions include/LogFile.h

This file was deleted.

17 changes: 8 additions & 9 deletions src/core/CPU.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "CPU.h"

#include <stdexcept>

#include "Types.h"
#include "Bytes.h"
#include "CPU.h"
#include "Instructions.h"
#include "MemoryMap.h"

Expand All @@ -12,9 +14,8 @@ const u_int16_t INTERRUPT_ROUTINE_LCD = 0x0048;
const u_int16_t INTERRUPT_ROUTINE_TIMER = 0x0050;
const u_int16_t INTERRUPT_ROUTINE_SERIAL = 0x0058;
const u_int16_t INTERRUPT_ROUTINE_JOYPAD = 0x0060;
const std::string LOG_PATH = "mboy_log_cpu.txt";

CPU::CPU(Memory* memory) : logFile(LOG_PATH) {
CPU::CPU(Memory* memory) {
a = 0x11;
b = 0x00;
c = 0x13;
Expand Down Expand Up @@ -188,12 +189,10 @@ u_int16_t CPU::step(MemoryHook* memory, u_int32_t count, bool debug) {
u_int16_t instructionDurationNoAction = instruction.cyclesNoAction;
u_int16_t totalCycles = 0;

//u_int8_t argLength = instruction.length - 1;

// logFile.write("%d - 0x%04X - 0x%04X - %10s - %02X - %04X - AF=%04X BC=%04X DE=%04X HL=%04X SP=%04X - Z=%d N=%d H=%d C=%d",
// count, pc, instructionCode, instruction.debug, arg_8, arg_16,
// get_16(AF), get_16(BC), get_16(DE), get_16(HL), sp,
// flag_z, flag_n, flag_h, flag_c);
MBOY_TRACE("{0:d} - 0x{1:04X} - 0x{2:04X} - {3:10} - {4:02X} - {5:04X} - AF={6:04X} BC={7:04X} DE={8:04X} HL={9:04X} SP={10:04X} - Z={11:d} N={12:d} H={13:d} C={14:d}",
count, pc, instructionCode, instruction.debug, arg_8, arg_16,
get_16(AF), get_16(BC), get_16(DE), get_16(HL), sp,
flag_z, flag_n, flag_h, flag_c);

if(!halt) {
pc += instructionLength;
Expand Down
6 changes: 2 additions & 4 deletions src/core/Gameboy.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//
// Created by matthew on 05/07/2020.
//

#include "Gameboy.h"
#include "Log.h"

Gameboy::Gameboy(std::string path, GUI* gui, Config* config) :
memory(),
Expand All @@ -15,6 +12,7 @@ Gameboy::Gameboy(std::string path, GUI* gui, Config* config) :

this->gui = gui;
this->config = config;
initLogging();

memory.set_8(LCD_CONTROL, 0x91);
memory.set_8(ADDRESS_JOYPAD, 0x0F);
Expand Down
32 changes: 0 additions & 32 deletions src/core/LogFile.cpp

This file was deleted.

9 changes: 5 additions & 4 deletions src/core/SaveFile.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//
// Created by matthew on 03/08/2020.
//
#include "SaveFile.h"

#include <fstream>
#include <iostream>
#include <regex>
#include "SaveFile.h"

#include "Log.h"

SaveFile::SaveFile(std::string name) {
this->name = name;
Expand Down Expand Up @@ -37,6 +36,8 @@ u_int8_t SaveFile::get_8(u_int32_t address) {
}

bool SaveFile::set_8(u_int32_t address, u_int8_t value) {
MBOY_INFO("Writing to save file - {0} - 0x{1:X} = 0x{2:X}", this->fileName, address, value);

const char data[1] = {(char) value};
this->data[address] = value;
this->file.seekp(address, std::ios::beg);
Expand Down
2 changes: 0 additions & 2 deletions src/graphics/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const u_int16_t DURATION_SCANLINE_SPRITE = 80;
const u_int16_t DURATION_SCANLINE_BACKGROUND = 172;
const u_int16_t DURATION_HORIZONTAL_BLANK = 204;
const u_int16_t DURATION_VERTICAL_BLANK = 456;
const std::string LOG_PATH = "/sdcard/Download/matterboy_log_gpu.txt";

const u_int16_t MODE_DURATIONS[] = {
DURATION_HORIZONTAL_BLANK,
Expand All @@ -23,7 +22,6 @@ const u_int16_t MODE_DURATIONS[] = {

GPU::GPU(Memory *memory, GUI* gui, Config* config) :
pixels(SCREEN_WIDTH, SCREEN_HEIGHT),
logFile(LOG_PATH),
display((MemoryHook*) memory, config) {
this->memory = (MemoryHook*) memory;
this->control = new Control((u_int8_t) 0);
Expand Down

0 comments on commit d2d50cd

Please sign in to comment.