Skip to content

Commit

Permalink
Make key input less hacky and implement touchscreen pen pressed state
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jan 4, 2024
1 parent 61a3ddc commit a5d8cb2
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 45 deletions.
5 changes: 0 additions & 5 deletions src/dual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,3 @@ target_include_directories(dual PRIVATE src)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(dual PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fbracket-depth=8192>)
endif()

# @todo: remove this when properly implementing key input
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED IMPORTED_TARGET sdl2)
target_link_libraries(dual PRIVATE PkgConfig::SDL2)
1 change: 1 addition & 0 deletions src/dual/include/dual/nds/arm7/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace dual::nds::arm7 {
RTC& rtc;
APU& apu;
WIFI& wifi;
u32& key_input;
};

MemoryBus(SystemMemory& memory, const HW& hw);
Expand Down
1 change: 1 addition & 0 deletions src/dual/include/dual/nds/arm9/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace dual::nds::arm9 {
Math& math;
VideoUnit& video_unit;
Cartridge& cartridge;
u32& key_input;
};

struct TCM {
Expand Down
15 changes: 15 additions & 0 deletions src/dual/include/dual/nds/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@

namespace dual::nds {

enum class Key : u8 {
A = 0,
B = 1,
Select = 2,
Start = 3,
Right = 4,
Left = 5,
Up = 6,
Down = 7,
R = 8,
L = 10,
X = 16,
Y = 17
};

enum class CPU {
ARM7 = 0,
ARM9 = 1
Expand Down
18 changes: 12 additions & 6 deletions src/dual/include/dual/nds/nds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <dual/nds/rom.hpp>
#include <dual/nds/system_memory.hpp>
#include <dual/nds/timer.hpp>
#include <dual/nds/enums.hpp>
#include <memory>
#include <span>

Expand All @@ -45,6 +46,7 @@ namespace dual::nds {
return m_arm7.apu;
}

void SetKeyState(Key key, bool pressed);
void SetTouchState(bool pen_down, u8 x, u8 y);

private:
Expand All @@ -56,6 +58,8 @@ namespace dual::nds {

Cartridge m_cartridge{m_scheduler, m_arm9.irq, m_arm7.irq, m_arm9.dma, m_arm7.dma, m_memory};

u32 m_key_input{};

struct ARM9 {
CycleCounter cycle_counter{1};
std::unique_ptr<arm::CPU> cpu{};
Expand All @@ -66,7 +70,7 @@ namespace dual::nds {
arm9::DMA dma{bus, irq};
arm9::Math math{};

ARM9(Scheduler& scheduler, SystemMemory& memory, IPC& ipc, VideoUnit& video_unit, Cartridge& cartridge)
ARM9(Scheduler& scheduler, SystemMemory& memory, IPC& ipc, VideoUnit& video_unit, Cartridge& cartridge, u32& key_input)
: bus{memory, {
irq,
timer,
Expand All @@ -76,10 +80,11 @@ namespace dual::nds {
memory.vram,
math,
video_unit,
cartridge
cartridge,
key_input
}}
, timer{scheduler, cycle_counter, irq} {}
} m_arm9{m_scheduler, m_memory, m_ipc, m_video_unit, m_cartridge};
} m_arm9{m_scheduler, m_memory, m_ipc, m_video_unit, m_cartridge, m_key_input};

struct ARM7 {
CycleCounter cycle_counter{0};
Expand All @@ -93,7 +98,7 @@ namespace dual::nds {
arm7::APU apu;
arm7::WIFI wifi{};

ARM7(Scheduler& scheduler, SystemMemory& memory, IPC& ipc, VideoUnit& video_unit, Cartridge& cartridge)
ARM7(Scheduler& scheduler, SystemMemory& memory, IPC& ipc, VideoUnit& video_unit, Cartridge& cartridge, u32& key_input)
: bus{memory, {
irq,
timer,
Expand All @@ -106,11 +111,12 @@ namespace dual::nds {
cartridge,
rtc,
apu,
wifi
wifi,
key_input
}}
, timer{scheduler, cycle_counter, irq}
, apu{scheduler, bus} {}
} m_arm7{m_scheduler, m_memory, m_ipc, m_video_unit, m_cartridge};
} m_arm7{m_scheduler, m_memory, m_ipc, m_video_unit, m_cartridge, m_key_input};

IPC m_ipc{m_arm9.irq, m_arm7.irq};

Expand Down
4 changes: 4 additions & 0 deletions src/dual/src/nds/arm7/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ namespace dual::nds::arm7 {
case REG(0x04000108): return hw.timer.Read_TMCNT(2);
case REG(0x0400010C): return hw.timer.Read_TMCNT(3);

// Key Input
case REG(0x04000130): return (u16)hw.key_input;
case REG(0x04000134): return hw.key_input & ~0xFFFFu;

// RTC
case REG(0x04000138): return hw.rtc.Read_RTC();

Expand Down
13 changes: 0 additions & 13 deletions src/dual/src/nds/arm7/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <SDL.h>

#include <dual/nds/arm7/memory.hpp>

namespace dual::nds::arm7 {
Expand Down Expand Up @@ -37,17 +35,6 @@ namespace dual::nds::arm7 {
return atom::read<T>(m_swram.arm7.data, address & m_swram.arm7.mask);
}
case 0x04: {
if(address == 0x04000136) {
const u8* key_state = SDL_GetKeyboardState(nullptr);

u16 extkeyinput = 0x3Fu;

if(key_state[SDL_SCANCODE_Q]) extkeyinput &= ~1u;
if(key_state[SDL_SCANCODE_W]) extkeyinput &= ~2u;

return extkeyinput;
}

if constexpr(std::is_same_v<T, u8 >) return m_io.ReadByte(address);
if constexpr(std::is_same_v<T, u16>) return m_io.ReadHalf(address);
if constexpr(std::is_same_v<T, u32>) return m_io.ReadWord(address);
Expand Down
3 changes: 3 additions & 0 deletions src/dual/src/nds/arm9/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ namespace dual::nds::arm9 {
case REG(0x04000108): return hw.timer.Read_TMCNT(2);
case REG(0x0400010C): return hw.timer.Read_TMCNT(3);

// Key Input
case REG(0x04000130): return (u16)hw.key_input;

// IPC
case REG(0x04000180): return hw.ipc.Read_SYNC(CPU::ARM9);
case REG(0x04000184): return hw.ipc.Read_FIFOCNT(CPU::ARM9);
Expand Down
21 changes: 0 additions & 21 deletions src/dual/src/nds/arm9/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <SDL.h>

#include <dual/nds/arm9/memory.hpp>

namespace dual::nds::arm9 {
Expand Down Expand Up @@ -61,25 +59,6 @@ namespace dual::nds::arm9 {
return atom::read<T>(m_swram.arm9.data, address & m_swram.arm9.mask);
}
case 0x04: {
if(address == 0x04000130) {
const u8* key_state = SDL_GetKeyboardState(nullptr);

u16 keyinput = 0x03FFu;

if(key_state[SDL_SCANCODE_A]) keyinput &= ~1u;
if(key_state[SDL_SCANCODE_S]) keyinput &= ~2u;
if(key_state[SDL_SCANCODE_BACKSPACE]) keyinput &= ~4u;
if(key_state[SDL_SCANCODE_RETURN]) keyinput &= ~8u;
if(key_state[SDL_SCANCODE_RIGHT]) keyinput &= ~16u;
if(key_state[SDL_SCANCODE_LEFT]) keyinput &= ~32u;
if(key_state[SDL_SCANCODE_UP]) keyinput &= ~64u;
if(key_state[SDL_SCANCODE_DOWN]) keyinput &= ~128u;
if(key_state[SDL_SCANCODE_F]) keyinput &= ~256u;
if(key_state[SDL_SCANCODE_D]) keyinput &= ~512u;

return keyinput;
}

if constexpr(std::is_same_v<T, u8 >) return m_io.ReadByte(address);
if constexpr(std::is_same_v<T, u16>) return m_io.ReadHalf(address);
if constexpr(std::is_same_v<T, u32>) return m_io.ReadWord(address);
Expand Down
16 changes: 16 additions & 0 deletions src/dual/src/nds/nds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace dual::nds {

m_cartridge.Reset();

m_key_input = 0x007F03FFu;

m_memory.ewram.fill(0);
m_memory.swram.Reset();
m_memory.vram.Reset();
Expand Down Expand Up @@ -198,8 +200,22 @@ namespace dual::nds {
m_video_unit.DirectBoot();
}

void NDS::SetKeyState(Key key, bool pressed) {
if(pressed) {
m_key_input &= ~(1u << (int)key);
} else {
m_key_input |= 1u << (int)key;
}
}

void NDS::SetTouchState(bool pen_down, u8 x, u8 y) {
m_arm7.spi.GetTouchScreen().SetTouchState(pen_down, x, y);

if(pen_down) {
m_key_input &= ~(1 << 22);
} else {
m_key_input |= 1 << 22;
}
}

} // namespace dual::nds
25 changes: 25 additions & 0 deletions src/platform/sdl/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ void Application::MainLoop() {
UpdateFPS();
}

// @todo: move this logic into HandleEvent()

m_emu_thread.SetFastForward(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_SPACE]);

if(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_F11]) {
Expand Down Expand Up @@ -226,6 +228,29 @@ void Application::HandleEvent(const SDL_Event& event) {

set_touch_state(window_x, window_y);
}

if(type == SDL_KEYUP || type == SDL_KEYDOWN) {
const SDL_KeyboardEvent& keyboard_event = (const SDL_KeyboardEvent&)event;

const auto update_key = [&](dual::nds::Key key) {
m_emu_thread.SetKeyState(key, type == SDL_KEYDOWN);
};

switch(keyboard_event.keysym.sym) {
case SDLK_a: update_key(dual::nds::Key::A); break;
case SDLK_s: update_key(dual::nds::Key::B); break;
case SDLK_d: update_key(dual::nds::Key::L); break;
case SDLK_f: update_key(dual::nds::Key::R); break;
case SDLK_q: update_key(dual::nds::Key::X); break;
case SDLK_w: update_key(dual::nds::Key::Y); break;
case SDLK_BACKSPACE: update_key(dual::nds::Key::Select); break;
case SDLK_RETURN: update_key(dual::nds::Key::Start); break;
case SDLK_UP: update_key(dual::nds::Key::Up); break;
case SDLK_DOWN: update_key(dual::nds::Key::Down); break;
case SDLK_LEFT: update_key(dual::nds::Key::Left); break;
case SDLK_RIGHT: update_key(dual::nds::Key::Right); break;
}
}
}

void Application::UpdateFPS() {
Expand Down
11 changes: 11 additions & 0 deletions src/platform/sdl/src/emulator_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ std::unique_ptr<dual::nds::NDS> EmulatorThread::Stop() {
return std::move(m_nds);
}

void EmulatorThread::SetKeyState(dual::nds::Key key, bool pressed) {
PushMessage({
.type = MessageType::SetKeyState,
.set_key_state = { .key = key, .pressed = (u8)(pressed ? 1 : 0) }
});
}

void EmulatorThread::SetTouchState(bool pen_down, u8 x, u8 y) {
PushMessage({
.type = MessageType::SetTouchState,
Expand Down Expand Up @@ -66,6 +73,10 @@ void EmulatorThread::ProcessMessages() {
const Message& message = m_msg_queue.front();

switch(message.type) {
case MessageType::SetKeyState: {
m_nds->SetKeyState(message.set_key_state.key, message.set_key_state.pressed);
break;
}
case MessageType::SetTouchState: {
m_nds->SetTouchState(message.set_touch_state.pen_down, message.set_touch_state.x, message.set_touch_state.y);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/platform/sdl/src/emulator_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EmulatorThread {
void Start(std::unique_ptr<dual::nds::NDS> nds);
std::unique_ptr<dual::nds::NDS> Stop();

void SetKeyState(dual::nds::Key key, bool pressed);
void SetTouchState(bool pen_down, u8 x, u8 y);

[[nodiscard]] bool GetFastForward() const;
Expand All @@ -30,12 +31,17 @@ class EmulatorThread {

private:
enum class MessageType : u8 {
SetKeyState,
SetTouchState
};

struct Message {
MessageType type;
union {
struct {
dual::nds::Key key;
u8 pressed;
} set_key_state;
struct {
u8 pen_down;
u8 x;
Expand Down

0 comments on commit a5d8cb2

Please sign in to comment.