implement key controls

This commit is contained in:
Colby 2016-09-01 12:01:18 +10:00
parent 5ee093760d
commit 212406677b
5 changed files with 42 additions and 49 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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