Skip to content

Commit

Permalink
log: init
Browse files Browse the repository at this point in the history
  • Loading branch information
gapry committed Jul 31, 2024
1 parent 28479ae commit eddd820
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 44 deletions.
7 changes: 3 additions & 4 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <arabica/ui/window.hpp>
#include <arabica/log/log.hpp>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#include <fstream>
#include <sstream>
#else
#include <fmt/core.h>
#endif

#ifdef __EMSCRIPTEN__
std::string read_file_from_browser(const std::string& file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
// fmt::print("Failed to open file: {}\n", file_name);
ARABICA_LOG_INFO("Failed to open file: {}\n", file_name);
std::exit(1);
}
std::stringstream buffer;
Expand All @@ -27,7 +26,7 @@ int main(int argc, char* argv[]) {
std::string rom_file = "/roms/Tetris_Fran_Dachille_1991.ch8";
#else
if (argc != 2) {
// fmt::print("Usage: ./arabica.out rom-file\n");
ARABICA_LOG_INFO("{}\n", "Usage: ./arabica.out rom-file");
return 1;
}
std::string rom_file = argv[1];
Expand Down
3 changes: 2 additions & 1 deletion arabica/device/sound.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <arabica/log/log.hpp>
#include <SDL2/SDL.h>
#include <cmath>

Expand Down Expand Up @@ -41,7 +42,7 @@ class Sound {

_device = SDL_OpenAudioDevice(nullptr, 0, &_desired_spec, &_spec, 0);
if (_device == 0) {
SDL_Log("Failed to open audio: %s", SDL_GetError());
ARABICA_LOG_INFO("Failed to open audio: %s", SDL_GetError());
return false;
}

Expand Down
11 changes: 5 additions & 6 deletions arabica/emulator/emulator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <arabica/emulator/emulator.hpp>
#include <arabica/log/log.hpp>
#include <cstdint>
#include <vector>

Expand All @@ -13,17 +14,15 @@ bool Emulator::load(const std::string& rom) {
}

void Emulator::execute() {
is_enable_log = false;

log_info("PC = {:x}\n", cpu.pc);
ARABICA_LOG_INFO("PC = {:x}\n", cpu.pc);

// 500 Hz / 60 FPS = 500 (Instructions / Second) / 60 (Frames / Second) = 500 / 60 (Instructions / Frame)
const int instructions_pre_frames = cpu.clock_speed / fps;
for (int i = 0; i < instructions_pre_frames; ++i) {
single_step();
}

log_info("The current cycle is {}\n", cycle);
ARABICA_LOG_INFO("The current cycle is {}\n", cycle);
cycle++;

delay.tick();
Expand All @@ -35,7 +34,7 @@ void Emulator::single_step() {
uint16_t prefix = cpu.instruction & 0xF000;
cpu.opcode = static_cast<OP_CODE>(prefix);

log_info("cpu.instruction is {0:x}\n", cpu.instruction);
ARABICA_LOG_INFO("cpu.instruction is {0:x}\n", cpu.instruction);

switch (prefix) {
case 0x0: {
Expand Down Expand Up @@ -552,7 +551,7 @@ void Emulator::single_step() {
cpu.advance_pc();
} break;
default: {
log_info("Unknown opcode: 0x{:X}\n", static_cast<uint16_t>(cpu.opcode));
ARABICA_LOG_INFO("Unknown opcode: 0x{:X}\n", static_cast<uint16_t>(cpu.opcode));
} break;
}
}
Expand Down
22 changes: 3 additions & 19 deletions arabica/emulator/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include <arabica/device/display.hpp>
#include <arabica/device/sound.hpp>
#include <arabica/device/delay.hpp>

#ifdef __EMSCRIPTEN__
#else
#include <fmt/core.h>
#endif

#include <random>

namespace arabica {
Expand All @@ -20,7 +14,6 @@ class Emulator {
public:
Emulator()
: cycle(0)
, is_enable_log(false)
, cpu(memory) {
}

Expand All @@ -29,10 +22,9 @@ class Emulator {
void single_step();
void execute();

int fps = 60; // 60 FPS = 60 (Frames Per Second) = 60 (frames) / 1 (second)
int milliseconds_per_frame = 1000 / fps; // (FPS)^{-1} = 1 (s) / 60 (frames) = 1000 (milliseconds) / 60 (frames)
int cycle = 0;
bool is_enable_log = false;
int fps = 60; // 60 FPS = 60 (Frames Per Second) = 60 (frames) / 1 (second)
int milliseconds_per_frame = 1000 / fps; // (FPS)^{-1} = 1 (s) / 60 (frames) = 1000 (milliseconds) / 60 (frames)
int cycle = 0;

CPU cpu;
Memory memory;
Expand All @@ -49,14 +41,6 @@ class Emulator {
std::uniform_int_distribution<T> distr(range_from, range_to);
return distr(generator);
}

template<typename... Args>
inline void log_info(const char* const format, const Args&... args) {
if (is_enable_log) {
// fmt::print("[emulator log] ");
// fmt::print(format, args...);
}
}
};

} // namespace arabica
27 changes: 27 additions & 0 deletions arabica/log/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#define ARABICA_IS_ENABLE_LOG 0

#ifdef __EMSCRIPTEN__
#else
#include <fmt/core.h>
#endif

#if ARABICA_IS_ENABLE_LOG
#define ARABICA_LOG_INFO(fmt, ...) arabica::log_info(fmt, __VA_ARGS__)
#else
#define ARABICA_LOG_INFO(fmt, ...)
#endif

namespace arabica {

template<typename... Args>
inline void log_info(const char* const format, const Args&... args) {
#ifdef __EMSCRIPTEN__
#else
fmt::print("[emulator log] ");
fmt::print(format, args...);
#endif
}

} // namespace arabica
8 changes: 2 additions & 6 deletions arabica/memory/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#include <arabica/memory/memory.hpp>
#include <arabica/log/log.hpp>
#include <iostream>
#include <fstream>
#include <stdexcept>

#ifdef __EMSCRIPTEN__
#else
#include <fmt/core.h>
#endif

namespace arabica {

Memory::Memory() {
Expand Down Expand Up @@ -53,7 +49,7 @@ void Memory::is_valid(const address_t address) const {
bool Memory::load(const std::string& rom) {
std::ifstream file(rom, std::ios::binary | std::ios::ate);
if (!file) {
// fmt::print("Failed to open the file.");
ARABICA_LOG_INFO("{}\n", "Failed to open the file.");
return false;
}

Expand Down
15 changes: 7 additions & 8 deletions arabica/ui/window.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <arabica/ui/window.hpp>
#include <arabica/log/log.hpp>

#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#else
#include <fmt/core.h>
#endif

namespace arabica {
Expand All @@ -14,17 +13,17 @@ Uint32 _on_tick(const Uint32 interval, void* userdata) {

Window::Window(const std::string& title, const int width, const int height, const std::string& rom) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) {
// fmt::print("SDL could not initialize! SDL_Error: {}\n", SDL_GetError());
ARABICA_LOG_INFO("SDL could not initialize! SDL_Error: {}\n", SDL_GetError());
std::exit(1);
}

if (!emulator.init()) {
// fmt::print("Failed to initialize emulator");
ARABICA_LOG_INFO("{}\n", "Failed to initialize emulator");
std::exit(1);
}

if (!emulator.load(rom)) {
// fmt::print("Failed to load rom");
ARABICA_LOG_INFO("{}\n", "Failed to load rom");
std::exit(1);
}

Expand All @@ -40,13 +39,13 @@ Window::Window(const std::string& title, const int width, const int height, cons
height, //
SDL_WINDOW_SHOWN); //
if (_window == nullptr) {
// fmt::print("Window could not be created! SDL_Error: {}\n", SDL_GetError());
ARABICA_LOG_INFO("Window could not be created! SDL_Error: {}\n", SDL_GetError());
std::exit(1);
}

_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
if (_renderer == nullptr) {
// fmt::print("Renderer could not be created! SDL_Error: {}\n", SDL_GetError());
ARABICA_LOG_INFO("Renderer could not be created! SDL_Error: {}\n", SDL_GetError());
std::exit(1);
}

Expand Down Expand Up @@ -92,7 +91,7 @@ void Window::polling() {
case SDL_WINDOWEVENT: {
if (_event.window.event == SDL_WINDOWEVENT_CLOSE) {
if (SDL_FALSE == SDL_RemoveTimer(_timer_id)) {
// fmt::print("Failed to remove timer! SDL_Error: {}\n", SDL_GetError());
ARABICA_LOG_INFO("Failed to remove timer! SDL_Error: {}\n", SDL_GetError());
std::exit(1);
}
}
Expand Down

0 comments on commit eddd820

Please sign in to comment.