Skip to content

Commit

Permalink
Make update frequency adjustable in interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aceiii committed Apr 6, 2024
1 parent b174b71 commit a281dd6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
17 changes: 14 additions & 3 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ bool Interface::update() {

if (show_emulation) {
if (ImGui::Begin("Emulation", &show_emulation)) {
ImGuiStyle &style = ImGui::GetStyle();

auto push_disabled_btn_flags = [] () {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
Expand All @@ -320,12 +322,15 @@ bool Interface::update() {
};

ImVec2 size = ImGui::GetWindowSize();
ImVec2 frame_padding {16.0f, 8.0f};
float num_buttons = 4.0f;
float button_width = ImGui::CalcTextSize(ICON_FA_PLAY).x;
float buttons_width = (button_width + 32 + 5) * 4;
float buttons_width = (button_width + (2 * frame_padding.x) + style.ItemSpacing.x) * num_buttons;
float slider_width = 192.0f;

ImGui::SameLine((size.x - buttons_width) / 2);
ImGui::SameLine((size.x - buttons_width - slider_width) / 2);

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{16.0f, 8.0f});
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, frame_padding);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 4.0f);

bool is_playing = interpreter->is_playing();
Expand Down Expand Up @@ -381,6 +386,12 @@ bool Interface::update() {
}

ImGui::PopStyleVar(2);

ImGui::SameLine();

ImGui::SetNextItemWidth(slider_width);
ImGui::SetCursorPosY(32);
ImGui::SliderInt("IPS", &interpreter->update_play_rate, 10, 3200);
}
ImGui::End();
}
Expand Down
27 changes: 18 additions & 9 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
#include <_types/_uint16_t.h>
#include <_types/_uint8_t.h>
#include <optional>
#include <utility>
#include <spdlog/spdlog.h>
#include <utility>

const double kTimerFrequency = 1 / 60.0;
const double kInstrFrequency = 1 / 2400.0;

Interpreter::Interpreter(std::shared_ptr<registers> regs) : regs(std::move(regs)) {}
Interpreter::Interpreter(std::shared_ptr<registers> regs)
: regs(std::move(regs)) {}

void Interpreter::initialize() {
timer.reset();
Expand All @@ -20,6 +18,8 @@ void Interpreter::initialize() {
}

void Interpreter::update() {
double update_frequency = 1.0 / static_cast<double>(update_play_rate);

double dt = timer.duration();
timer.reset();

Expand All @@ -28,9 +28,9 @@ void Interpreter::update() {
if (playing) {
last_update += dt;

while (last_update > kInstrFrequency) {
while (last_update > update_frequency) {
step();
last_update -= kInstrFrequency;
last_update -= update_frequency;
}
} else {
last_update = 0;
Expand Down Expand Up @@ -180,6 +180,8 @@ void Interpreter::step() {
regs->v[0xf] = msb;
}
break;
default:
spdlog::warn("Invalid instruction: {:x}", instr);
}
break;
case 0x9:
Expand Down Expand Up @@ -219,6 +221,8 @@ void Interpreter::step() {
regs->pc += 2;
}
break;
default:
spdlog::warn("Invalid instruction: {:x}", instr);
}
break;
case 0xf: {
Expand Down Expand Up @@ -276,8 +280,12 @@ void Interpreter::step() {
regs->v[n] = regs->mem[i];
}
break;
default:
spdlog::warn("Invalid instruction: {:x}", instr);
}
} break;
default:
spdlog::warn("Invalid instruction: {:x}", instr);
}
}

Expand All @@ -299,14 +307,15 @@ void Interpreter::update_timers(double dt) {
void Interpreter::stack_push(uint16_t val) {
spdlog::trace("Push stack: {}", val);
uint8_t *sp = &regs->mem[kStackPtrIndex];
uint16_t *stack = reinterpret_cast<uint16_t *>(&regs->mem[kStackStartIndex]);
auto stack = reinterpret_cast<uint16_t *>(&regs->mem[kStackStartIndex]);
stack[(*sp)++] = val;

}

uint16_t Interpreter::stack_pop() {
spdlog::trace("Pop stack");
uint8_t *sp = &regs->mem[kStackPtrIndex];
uint16_t *stack = reinterpret_cast<uint16_t *>(&regs->mem[kStackStartIndex]);
auto stack = reinterpret_cast<uint16_t *>(&regs->mem[kStackStartIndex]);
return stack[--(*sp)];
}

Expand Down
8 changes: 7 additions & 1 deletion src/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
#include <memory>
#include <optional>

const double kTimerFrequency = 1.0 / 60.0;
const int kDefaultPlayingUpdateRate = 1200;

class Interpreter {
public:
Interpreter(std::shared_ptr<registers> regs);
int update_play_rate = kDefaultPlayingUpdateRate;

public:
explicit Interpreter(std::shared_ptr<registers> regs);

void initialize();
void update();
Expand Down

0 comments on commit a281dd6

Please sign in to comment.