remove use of arrays in gb joypad

This commit is contained in:
Matthew Berry 2022-11-11 09:04:59 -08:00
parent 8c2e1e9691
commit 37dcb6fcca
2 changed files with 7 additions and 28 deletions

View file

@ -44,24 +44,6 @@ def last_set_bit(n : Int) : Int
count
end
def array_to_uint8(array : Array(Bool | Int)) : UInt8
raise "Array needs to have a length of 8" if array.size != 8
value = 0_u8
array.each_with_index do |bit, index|
value |= (bit == false || bit == 0 ? 0 : 1) << (7 - index)
end
value
end
def array_to_uint16(array : Array(Bool | Int)) : UInt16
raise "Array needs to have a length of 16" if array.size != 16
value = 0_u16
array.each_with_index do |bit, index|
value |= (bit == false || bit == 0 ? 0 : 1) << (15 - index)
end
value
end
macro trace(value, newline = true)
{% if flag? :trace %}
{% if newline %}

View file

@ -18,16 +18,13 @@ module GB
end
def read : UInt8
array_to_uint8 [
1,
1,
!@button_keys,
!@direction_keys,
!((@down && @direction_keys) || (@start && @button_keys)),
!((@up && @direction_keys) || (@select && @button_keys)),
!((@left && @direction_keys) || (@b && @button_keys)),
!((@right && @direction_keys) || (@a && @button_keys)),
]
~(0x00_u8 |
@button_keys.to_unsafe << 5 |
@direction_keys.to_unsafe << 4 |
((@down && @direction_keys) || (@start && @button_keys)).to_unsafe << 3 |
((@up && @direction_keys) || (@select && @button_keys)).to_unsafe << 2 |
((@left && @direction_keys) || (@b && @button_keys)).to_unsafe << 1 |
((@right && @direction_keys) || (@a && @button_keys)).to_unsafe)
end
def write(value : UInt8) : Nil