Skip to content

Commit

Permalink
Implement keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
aceiii committed Apr 8, 2024
1 parent b4689a8 commit 650dcf4
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ set(SOURCE_FILES
src/applog.cpp
src/screen.cpp
src/assembly.cpp
src/keyboard.cpp
src/keyboard.h
)

set(EXPECTED_BUILD_TESTS OFF)
Expand Down
42 changes: 5 additions & 37 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void Interface::initialize() {
screen.initialize(kScreenWidth, kScreenHeight, kDefaultScreenPixelSize,
regs->screen.data());
assembly.initialize(regs.get());

keyboard.initialize(regs);
}

bool Interface::update() {
Expand Down Expand Up @@ -151,6 +153,8 @@ bool Interface::update() {
UnloadDroppedFiles(droppedFiles);
}

keyboard.update();

play_sound = regs->st > 0;

screen.update();
Expand Down Expand Up @@ -440,43 +444,7 @@ bool Interface::update() {

if (show_keyboard) {
if (ImGui::Begin("Keyboard", &show_keyboard)) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(24, 18));
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 4);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 2));

ImGui::Button("1");
ImGui::SameLine();
ImGui::Button("2");
ImGui::SameLine();
ImGui::Button("3");
ImGui::SameLine();
ImGui::Button("C");

ImGui::Button("4");
ImGui::SameLine();
ImGui::Button("5");
ImGui::SameLine();
ImGui::Button("6");
ImGui::SameLine();
ImGui::Button("D");

ImGui::Button("7");
ImGui::SameLine();
ImGui::Button("8");
ImGui::SameLine();
ImGui::Button("9");
ImGui::SameLine();
ImGui::Button("E");

ImGui::Button("A");
ImGui::SameLine();
ImGui::Button("0");
ImGui::SameLine();
ImGui::Button("B");
ImGui::SameLine();
ImGui::Button("F");

ImGui::PopStyleVar(3);
keyboard.draw();
}
ImGui::End();
}
Expand Down
2 changes: 2 additions & 0 deletions src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "applog.h"
#include "screen.h"
#include "assembly.h"
#include "keyboard.h"

#include <imgui.h>
#include <imgui_memory_editor/imgui_memory_editor.h>
Expand Down Expand Up @@ -32,6 +33,7 @@ class Interface {
AppLog app_log;
Screen screen;
AssemblyViewer assembly;
Keyboard keyboard;

std::vector<uint8_t> rom;

Expand Down
16 changes: 14 additions & 2 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void Interpreter::initialize() {
}

void Interpreter::update() {
update_keyboard();

double update_frequency = 1.0 / static_cast<double>(update_play_rate);

double dt = timer.duration();
Expand Down Expand Up @@ -319,9 +321,19 @@ uint16_t Interpreter::stack_pop() {
return stack[--(*sp)];
}

std::optional<uint8_t> Interpreter::get_pressed_key() {
void Interpreter::update_keyboard() {
for (int key = 0; key < regs->kbd.size(); key += 1) {
if (regs->kbd[key]) {
if (key_down[key] && !regs->kbd[key]) {
key_released[key] = true;
spdlog::debug("Key pressed: 0x{:02x}", key);
}
}
key_down = regs->kbd;
}

std::optional<uint8_t> Interpreter::get_pressed_key() {
for (int key = 0; key < key_released.size(); key += 1) {
if (key_released[key]) {
return key;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Interpreter {
bool is_playing() const;

private:
void update_keyboard();
void update_timers(double dt);
void stack_push(uint16_t val);
uint16_t stack_pop();
Expand All @@ -47,4 +48,6 @@ class Interpreter {
bool playing = false;

std::shared_ptr<registers> regs;
std::array<bool, kKeyboardSize> key_down;
std::array<bool, kKeyboardSize> key_released;
};
80 changes: 80 additions & 0 deletions src/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "keyboard.h"

#include <spdlog/spdlog.h>
#include <raylib.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <vector>

Keyboard::Keyboard() {
mapping.push_back({"1", KEY_ONE, 0x1});
mapping.push_back({"2", KEY_TWO, 0x2});
mapping.push_back({"3", KEY_THREE, 0x3});
mapping.push_back({"C", KEY_FOUR, 0xc});

mapping.push_back({"4", KEY_Q, 0x4});
mapping.push_back({"5", KEY_W, 0x5});
mapping.push_back({"6", KEY_E, 0x6});
mapping.push_back({"D", KEY_R, 0xd});

mapping.push_back({"7", KEY_A, 0x7});
mapping.push_back({"8", KEY_S, 0x8});
mapping.push_back({"9", KEY_D, 0x9});
mapping.push_back({"E", KEY_F, 0xe});

mapping.push_back({"A", KEY_Z, 0xa});
mapping.push_back({"0", KEY_X, 0x0});
mapping.push_back({"B", KEY_C, 0xb});
mapping.push_back({"F", KEY_V, 0xf});
}

void Keyboard::initialize(std::shared_ptr<registers> regs_) {
regs = std::move(regs_);
}

void Keyboard::update() {
for (auto &m : mapping) {
regs->kbd[m.key] = IsKeyDown(m.keycode);
}
}

void Keyboard::draw() {
auto push_disabled_flags = [] () {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
};

auto pop_disabled_flags = [] () {
ImGui::PopItemFlag();
ImGui::PopStyleVar();
};

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(24, 18));
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 4);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 2));

int n = 0;
for (auto &m : mapping) {
if (n % 4 != 0) {
ImGui::SameLine();
}

bool is_disabled = regs->kbd[m.key];

if (is_disabled) {
push_disabled_flags();
}

if (ImGui::Button(m.label.c_str())) {
regs->kbd[m.key];
}

if (is_disabled) {
pop_disabled_flags();
}

n += 1;
}

ImGui::PopStyleVar(3);
}
24 changes: 24 additions & 0 deletions src/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "registers.h"
#include <vector>
#include <string>

struct key_mapping {
std::string label;
int keycode;
uint8_t key;
};

class Keyboard {
public:
Keyboard();

void initialize(std::shared_ptr<registers> regs);
void update();
void draw();

private:
std::vector<key_mapping> mapping;
std::shared_ptr<registers> regs;
};

0 comments on commit 650dcf4

Please sign in to comment.