mirror of
https://github.com/colby-swandale/waterfoul
synced 2025-01-14 08:01:51 +01:00
implement key controls
This commit is contained in:
parent
5ee093760d
commit
212406677b
5 changed files with 42 additions and 49 deletions
|
@ -13,7 +13,7 @@ require "waterfoul/screen"
|
||||||
require "waterfoul/gpu"
|
require "waterfoul/gpu"
|
||||||
require "waterfoul/cartridge"
|
require "waterfoul/cartridge"
|
||||||
require "waterfoul/emulator"
|
require "waterfoul/emulator"
|
||||||
# require "waterfoul/input"
|
require "waterfoul/input"
|
||||||
require "byebug"
|
require "byebug"
|
||||||
|
|
||||||
module Waterfoul
|
module Waterfoul
|
||||||
|
|
|
@ -11,7 +11,6 @@ module Waterfoul
|
||||||
@cpu = CPU.new
|
@cpu = CPU.new
|
||||||
@cpu = SkipBoot.set_state(@cpu) if options.has_key?('skip_boot')
|
@cpu = SkipBoot.set_state(@cpu) if options.has_key?('skip_boot')
|
||||||
@gpu = GPU.new
|
@gpu = GPU.new
|
||||||
# @input = Input.new
|
|
||||||
@screen = Screen.new
|
@screen = Screen.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,7 +19,6 @@ module Waterfoul
|
||||||
@cpu.step
|
@cpu.step
|
||||||
@gpu.step @cpu.m
|
@gpu.step @cpu.m
|
||||||
@screen.render @gpu.framebuffer if @gpu.vblank?
|
@screen.render @gpu.framebuffer if @gpu.vblank?
|
||||||
# @input.step @cpu.m
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
require 'sdl2'
|
|
||||||
|
|
||||||
module Waterfoul
|
module Waterfoul
|
||||||
class Input
|
class Input
|
||||||
|
|
||||||
|
@ -13,54 +11,37 @@ module Waterfoul
|
||||||
SELECT_INPUT_KEYCODE = 229
|
SELECT_INPUT_KEYCODE = 229
|
||||||
POLL_RATE = 5000
|
POLL_RATE = 5000
|
||||||
|
|
||||||
def initialize
|
SDL.InitSubSystem(SDL::INIT_KEYBOARD)
|
||||||
@modeclock = 0
|
|
||||||
@current_keys = 0
|
|
||||||
$mmu.write_byte 0xFF00, 0xFF, hardware_operation: true
|
|
||||||
end
|
|
||||||
|
|
||||||
def step(cycles = 1)
|
def self.read_keyboard(joyp)
|
||||||
@modeclock += cycles
|
SDL.PumpEvents # update keyboard state
|
||||||
event = SDL2::Event.poll
|
keyboard = SDL.GetKeyboardState(nil)
|
||||||
|
keyboard_state = keyboard.read_array_of_uint8(229)
|
||||||
|
|
||||||
if @modeclock >= POLL_RATE
|
input = 0xF
|
||||||
@modeclock -= POLL_RATE
|
if joyp & 0x20 == 0x00
|
||||||
|
if keyboard_state[SDL::SDL_SCANCODE_RETURN] == 1
|
||||||
input = $mmu.read_byte 0xFF00
|
input ^= 0x8
|
||||||
|
elsif keyboard_state[SDL::SDL_SCANCODE_RSHIFT] == 1
|
||||||
if input & 0x10 == 0x00
|
input ^= 0x4
|
||||||
case @current_key
|
elsif keyboard_state[SDL::SDL_SCANCODE_A] == 1
|
||||||
when UP_INPUT_KEYCODE
|
input ^= 0x1
|
||||||
input ^= 0x4
|
elsif keyboard_state[SDL::SDL_SCANCODE_Z] == 1
|
||||||
when DOWN_INPUT_KEYCODE
|
input ^= 0x2
|
||||||
input ^= 0x8
|
end
|
||||||
when RIGHT_INPUT_KEYCODE
|
elsif joyp & 0x10 == 0x0
|
||||||
input ^= 0x1
|
if keyboard_state[SDL::SDL_SCANCODE_UP] == 1
|
||||||
when LEFT_INPUT_KEYCODE
|
input ^= 0x4
|
||||||
input ^= 0x2
|
elsif keyboard_state[SDL::SDL_SCANCODE_DOWN] == 1
|
||||||
end
|
input ^= 0x8
|
||||||
@current_key = 0
|
elsif keyboard_state[SDL::SDL_SCANCODE_LEFT] == 1
|
||||||
$mmu.write_byte 0xFF0F, input
|
input ^= 0x2
|
||||||
Interrupt.request_interrupt(Interrupt::INTERRUPT_JOYPAD)
|
elsif keyboard_state[SDL::SDL_SCANCODE_RIGHT] == 1
|
||||||
elsif input & 0x20 == 0x0
|
input ^= 0x1
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
elsif event.kind_of?(SDL2::Event::KeyDown)
|
|
||||||
@current_key = event.scancode
|
|
||||||
p @current_key
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
(0xF0 & joyp) | input
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,6 +33,8 @@ module Waterfoul
|
||||||
raise MemoryOutOfBounds if i > MEMORY_SIZE || i < 0
|
raise MemoryOutOfBounds if i > MEMORY_SIZE || i < 0
|
||||||
|
|
||||||
case i
|
case i
|
||||||
|
when 0xFF00
|
||||||
|
Input.read_keyboard @memory[i]
|
||||||
when 0x0000...0x8000 # ROM Bank 0 + n
|
when 0x0000...0x8000 # ROM Bank 0 + n
|
||||||
# if the boot rom is enabled and the address is < 0x100
|
# if the boot rom is enabled and the address is < 0x100
|
||||||
if @map_boot_rom && i <= BOOT_ROM_END_MEM_LOC
|
if @map_boot_rom && i <= BOOT_ROM_END_MEM_LOC
|
||||||
|
|
|
@ -7,6 +7,7 @@ module Waterfoul
|
||||||
|
|
||||||
INIT_TIMER = 0x01
|
INIT_TIMER = 0x01
|
||||||
INIT_VIDEO = 0x20
|
INIT_VIDEO = 0x20
|
||||||
|
INIT_KEYBOARD = 0x200
|
||||||
WINDOW_RESIZABLE = 0x20
|
WINDOW_RESIZABLE = 0x20
|
||||||
|
|
||||||
SDL_PIXELFORMAT_ARGB8888 =
|
SDL_PIXELFORMAT_ARGB8888 =
|
||||||
|
@ -16,6 +17,15 @@ module Waterfoul
|
||||||
(6 << 16) |
|
(6 << 16) |
|
||||||
(32 << 8) |
|
(32 << 8) |
|
||||||
(4 << 0)
|
(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 :InitSubSystem, 'SDL_InitSubSystem', [ :uint32 ], :int
|
||||||
attach_function :CreateWindow, 'SDL_CreateWindow', [ :string, :int, :int, :int, :int, :uint32 ], :pointer
|
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 :RenderClear, 'SDL_RenderClear', [:pointer], :int
|
||||||
attach_function :RenderCopy, 'SDL_RenderCopy', [:pointer, :pointer, :pointer, :pointer], :int
|
attach_function :RenderCopy, 'SDL_RenderCopy', [:pointer, :pointer, :pointer, :pointer], :int
|
||||||
attach_function :RenderPresent, 'SDL_RenderPresent', [:pointer], :int
|
attach_function :RenderPresent, 'SDL_RenderPresent', [:pointer], :int
|
||||||
|
attach_function :PumpEvents, 'SDL_PumpEvents', [], :void
|
||||||
|
attach_function :GetKeyboardState, 'SDL_GetKeyboardState', [:pointer], :pointer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue