From 212406677b4c0ad1ad4d05452e4439ed77429677 Mon Sep 17 00:00:00 2001 From: Colby Date: Thu, 1 Sep 2016 12:01:18 +1000 Subject: [PATCH] implement key controls --- lib/waterfoul.rb | 2 +- lib/waterfoul/emulator.rb | 2 -- lib/waterfoul/input.rb | 73 +++++++++++++++------------------------ lib/waterfoul/mmu.rb | 2 ++ lib/waterfoul/sdl.rb | 12 +++++++ 5 files changed, 42 insertions(+), 49 deletions(-) diff --git a/lib/waterfoul.rb b/lib/waterfoul.rb index 084e153..8dd957c 100644 --- a/lib/waterfoul.rb +++ b/lib/waterfoul.rb @@ -13,7 +13,7 @@ require "waterfoul/screen" require "waterfoul/gpu" require "waterfoul/cartridge" require "waterfoul/emulator" -# require "waterfoul/input" +require "waterfoul/input" require "byebug" module Waterfoul diff --git a/lib/waterfoul/emulator.rb b/lib/waterfoul/emulator.rb index fc65104..2c236ab 100644 --- a/lib/waterfoul/emulator.rb +++ b/lib/waterfoul/emulator.rb @@ -11,7 +11,6 @@ module Waterfoul @cpu = CPU.new @cpu = SkipBoot.set_state(@cpu) if options.has_key?('skip_boot') @gpu = GPU.new - # @input = Input.new @screen = Screen.new end @@ -20,7 +19,6 @@ module Waterfoul @cpu.step @gpu.step @cpu.m @screen.render @gpu.framebuffer if @gpu.vblank? - # @input.step @cpu.m end end end diff --git a/lib/waterfoul/input.rb b/lib/waterfoul/input.rb index 0ecd506..17d447b 100644 --- a/lib/waterfoul/input.rb +++ b/lib/waterfoul/input.rb @@ -1,5 +1,3 @@ -require 'sdl2' - module Waterfoul class Input @@ -13,54 +11,37 @@ module Waterfoul SELECT_INPUT_KEYCODE = 229 POLL_RATE = 5000 - def initialize - @modeclock = 0 - @current_keys = 0 - $mmu.write_byte 0xFF00, 0xFF, hardware_operation: true - end + SDL.InitSubSystem(SDL::INIT_KEYBOARD) - def step(cycles = 1) - @modeclock += cycles - event = SDL2::Event.poll + def self.read_keyboard(joyp) + SDL.PumpEvents # update keyboard state + keyboard = SDL.GetKeyboardState(nil) + keyboard_state = keyboard.read_array_of_uint8(229) - if @modeclock >= POLL_RATE - @modeclock -= POLL_RATE - - input = $mmu.read_byte 0xFF00 - - if input & 0x10 == 0x00 - case @current_key - when UP_INPUT_KEYCODE - input ^= 0x4 - when DOWN_INPUT_KEYCODE - input ^= 0x8 - when RIGHT_INPUT_KEYCODE - input ^= 0x1 - when LEFT_INPUT_KEYCODE - input ^= 0x2 - end - @current_key = 0 - $mmu.write_byte 0xFF0F, input - Interrupt.request_interrupt(Interrupt::INTERRUPT_JOYPAD) - elsif input & 0x20 == 0x0 - case @current_key - when A_INPUT_KEYCODE - input ^= 0x1 - when B_INPUT_KEYCODE - input ^= 0x2 - when START_INPUT_KEYCODE - input ^= 0x8 - when SELECT_INPUT_KEYCODE - input ^= 0x4 - end - @current_key = 0 - $mmu.write_byte 0xFF0F, input - Interrupt.request_interrupt(Interrupt::INTERRUPT_JOYPAD) + input = 0xF + if joyp & 0x20 == 0x00 + if keyboard_state[SDL::SDL_SCANCODE_RETURN] == 1 + input ^= 0x8 + elsif keyboard_state[SDL::SDL_SCANCODE_RSHIFT] == 1 + input ^= 0x4 + elsif keyboard_state[SDL::SDL_SCANCODE_A] == 1 + input ^= 0x1 + elsif keyboard_state[SDL::SDL_SCANCODE_Z] == 1 + input ^= 0x2 + end + elsif joyp & 0x10 == 0x0 + if keyboard_state[SDL::SDL_SCANCODE_UP] == 1 + input ^= 0x4 + elsif keyboard_state[SDL::SDL_SCANCODE_DOWN] == 1 + input ^= 0x8 + elsif keyboard_state[SDL::SDL_SCANCODE_LEFT] == 1 + input ^= 0x2 + elsif keyboard_state[SDL::SDL_SCANCODE_RIGHT] == 1 + input ^= 0x1 end - elsif event.kind_of?(SDL2::Event::KeyDown) - @current_key = event.scancode - p @current_key end + + (0xF0 & joyp) | input end end end diff --git a/lib/waterfoul/mmu.rb b/lib/waterfoul/mmu.rb index 11eb0e5..501ec19 100644 --- a/lib/waterfoul/mmu.rb +++ b/lib/waterfoul/mmu.rb @@ -33,6 +33,8 @@ module Waterfoul raise MemoryOutOfBounds if i > MEMORY_SIZE || i < 0 case i + when 0xFF00 + Input.read_keyboard @memory[i] when 0x0000...0x8000 # ROM Bank 0 + n # if the boot rom is enabled and the address is < 0x100 if @map_boot_rom && i <= BOOT_ROM_END_MEM_LOC diff --git a/lib/waterfoul/sdl.rb b/lib/waterfoul/sdl.rb index 9bb9ef2..4336415 100644 --- a/lib/waterfoul/sdl.rb +++ b/lib/waterfoul/sdl.rb @@ -7,6 +7,7 @@ module Waterfoul INIT_TIMER = 0x01 INIT_VIDEO = 0x20 + INIT_KEYBOARD = 0x200 WINDOW_RESIZABLE = 0x20 SDL_PIXELFORMAT_ARGB8888 = @@ -16,6 +17,15 @@ module Waterfoul (6 << 16) | (32 << 8) | (4 << 0) + # keyboard key maps + SDL_SCANCODE_RETURN = 40 # start + SDL_SCANCODE_RSHIFT = 229 # select + SDL_SCANCODE_RIGHT = 79 + SDL_SCANCODE_LEFT = 80 + SDL_SCANCODE_DOWN = 81 + SDL_SCANCODE_UP = 82 + SDL_SCANCODE_A = 4 # A + SDL_SCANCODE_Z = 29 # B attach_function :InitSubSystem, 'SDL_InitSubSystem', [ :uint32 ], :int attach_function :CreateWindow, 'SDL_CreateWindow', [ :string, :int, :int, :int, :int, :uint32 ], :pointer @@ -27,5 +37,7 @@ module Waterfoul attach_function :RenderClear, 'SDL_RenderClear', [:pointer], :int attach_function :RenderCopy, 'SDL_RenderCopy', [:pointer, :pointer, :pointer, :pointer], :int attach_function :RenderPresent, 'SDL_RenderPresent', [:pointer], :int + attach_function :PumpEvents, 'SDL_PumpEvents', [], :void + attach_function :GetKeyboardState, 'SDL_GetKeyboardState', [:pointer], :pointer end end