Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
boytacean
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
João Magalhães
boytacean
Commits
40a3f4c8
Verified
Commit
40a3f4c8
authored
2 years ago
by
João Magalhães
Browse files
Options
Downloads
Patches
Plain Diff
feat: initial support for key pressing
parent
10c0fa0c
No related branches found
No related tags found
No related merge requests found
Pipeline
#915
passed
2 years ago
Stage: build
Stage: deploy
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
examples/sdl/src/main.rs
+28
-2
28 additions, 2 deletions
examples/sdl/src/main.rs
src/cpu.rs
+6
-0
6 additions, 0 deletions
src/cpu.rs
src/gb.rs
+13
-1
13 additions, 1 deletion
src/gb.rs
src/mmu.rs
+4
-0
4 additions, 0 deletions
src/mmu.rs
src/pad.rs
+30
-0
30 additions, 0 deletions
src/pad.rs
with
81 additions
and
3 deletions
examples/sdl/src/main.rs
+
28
−
2
View file @
40a3f4c8
use
boytacean
::{
use
boytacean
::{
gb
::
GameBoy
,
gb
::
GameBoy
,
pad
::
PadKey
,
ppu
::{
DISPLAY_HEIGHT
,
DISPLAY_WIDTH
},
ppu
::{
DISPLAY_HEIGHT
,
DISPLAY_WIDTH
},
};
};
use
sdl2
::{
use
sdl2
::{
event
::
Event
,
image
::
LoadSurface
,
pixels
::
PixelFormatEnum
,
surface
::
Surface
,
video
::
Window
,
event
::
Event
,
image
::
LoadSurface
,
keyboard
::
Keycode
,
pixels
::
PixelFormatEnum
,
surface
::
Surface
,
AudioSubsystem
,
EventPump
,
TimerSubsystem
,
VideoSubsystem
,
video
::
Window
,
AudioSubsystem
,
EventPump
,
TimerSubsystem
,
VideoSubsystem
,
};
};
/// The base title to be used in the window.
/// The base title to be used in the window.
...
@@ -112,6 +113,17 @@ fn main() {
...
@@ -112,6 +113,17 @@ fn main() {
while
let
Some
(
event
)
=
graphics
.event_pump
.poll_event
()
{
while
let
Some
(
event
)
=
graphics
.event_pump
.poll_event
()
{
match
event
{
match
event
{
Event
::
Quit
{
..
}
=>
break
'main
,
Event
::
Quit
{
..
}
=>
break
'main
,
Event
::
KeyDown
{
keycode
:
Some
(
keycode
),
..
}
=>
game_boy
.key_press
(
key_to_pad
(
keycode
)),
Event
::
KeyUp
{
keycode
:
Some
(
keycode
),
..
}
=>
game_boy
.key_lift
(
key_to_pad
(
keycode
)),
_
=>
(),
_
=>
(),
}
}
}
}
...
@@ -146,3 +158,17 @@ fn main() {
...
@@ -146,3 +158,17 @@ fn main() {
graphics
.timer_subsystem
.delay
(
17
);
graphics
.timer_subsystem
.delay
(
17
);
}
}
}
}
fn
key_to_pad
(
keycode
:
Keycode
)
->
PadKey
{
match
keycode
{
Keycode
::
Up
=>
PadKey
::
Up
,
Keycode
::
Down
=>
PadKey
::
Down
,
Keycode
::
Left
=>
PadKey
::
Left
,
Keycode
::
Right
=>
PadKey
::
Right
,
Keycode
::
Space
=>
PadKey
::
Start
,
Keycode
::
Return
=>
PadKey
::
Select
,
Keycode
::
A
=>
PadKey
::
A
,
Keycode
::
S
=>
PadKey
::
B
,
_
=>
PadKey
::
A
,
}
}
This diff is collapsed.
Click to expand it.
src/cpu.rs
+
6
−
0
View file @
40a3f4c8
...
@@ -3,6 +3,7 @@ use core::panic;
...
@@ -3,6 +3,7 @@ use core::panic;
use
crate
::{
use
crate
::{
inst
::{
EXTENDED
,
INSTRUCTIONS
},
inst
::{
EXTENDED
,
INSTRUCTIONS
},
mmu
::
Mmu
,
mmu
::
Mmu
,
pad
::
Pad
,
ppu
::
Ppu
,
ppu
::
Ppu
,
};
};
...
@@ -177,6 +178,11 @@ impl Cpu {
...
@@ -177,6 +178,11 @@ impl Cpu {
self
.mmu
()
.ppu
()
self
.mmu
()
.ppu
()
}
}
#[inline(always)]
pub
fn
pad
(
&
mut
self
)
->
&
mut
Pad
{
self
.mmu
()
.pad
()
}
#[inline(always)]
#[inline(always)]
pub
fn
halted
(
&
self
)
->
bool
{
pub
fn
halted
(
&
self
)
->
bool
{
self
.halted
self
.halted
...
...
This diff is collapsed.
Click to expand it.
src/gb.rs
+
13
−
1
View file @
40a3f4c8
...
@@ -2,7 +2,7 @@ use crate::{
...
@@ -2,7 +2,7 @@ use crate::{
cpu
::
Cpu
,
cpu
::
Cpu
,
data
::{
BootRom
,
DMG_BOOT
,
DMG_BOOTIX
,
MGB_BOOTIX
,
SGB_BOOT
},
data
::{
BootRom
,
DMG_BOOT
,
DMG_BOOTIX
,
MGB_BOOTIX
,
SGB_BOOT
},
mmu
::
Mmu
,
mmu
::
Mmu
,
pad
::
Pad
,
pad
::
{
Pad
,
PadKey
},
ppu
::{
Ppu
,
Tile
,
FRAME_BUFFER_SIZE
},
ppu
::{
Ppu
,
Tile
,
FRAME_BUFFER_SIZE
},
util
::
read_file
,
util
::
read_file
,
};
};
...
@@ -35,6 +35,14 @@ impl GameBoy {
...
@@ -35,6 +35,14 @@ impl GameBoy {
self
.cpu
.reset
();
self
.cpu
.reset
();
}
}
pub
fn
key_press
(
&
mut
self
,
key
:
PadKey
)
{
self
.pad
()
.key_press
(
key
);
}
pub
fn
key_lift
(
&
mut
self
,
key
:
PadKey
)
{
self
.pad
()
.key_lift
(
key
);
}
pub
fn
pc
(
&
self
)
->
u16
{
pub
fn
pc
(
&
self
)
->
u16
{
self
.cpu
.pc
()
self
.cpu
.pc
()
}
}
...
@@ -132,6 +140,10 @@ impl GameBoy {
...
@@ -132,6 +140,10 @@ impl GameBoy {
self
.cpu
.ppu
()
self
.cpu
.ppu
()
}
}
pub
fn
pad
(
&
mut
self
)
->
&
mut
Pad
{
self
.cpu
.pad
()
}
pub
fn
frame_buffer
(
&
mut
self
)
->
&
Box
<
[
u8
;
FRAME_BUFFER_SIZE
]
>
{
pub
fn
frame_buffer
(
&
mut
self
)
->
&
Box
<
[
u8
;
FRAME_BUFFER_SIZE
]
>
{
&
(
self
.ppu
()
.frame_buffer
)
&
(
self
.ppu
()
.frame_buffer
)
}
}
...
...
This diff is collapsed.
Click to expand it.
src/mmu.rs
+
4
−
0
View file @
40a3f4c8
...
@@ -45,6 +45,10 @@ impl Mmu {
...
@@ -45,6 +45,10 @@ impl Mmu {
&
mut
self
.ppu
&
mut
self
.ppu
}
}
pub
fn
pad
(
&
mut
self
)
->
&
mut
Pad
{
&
mut
self
.pad
}
pub
fn
boot_active
(
&
self
)
->
bool
{
pub
fn
boot_active
(
&
self
)
->
bool
{
self
.boot_active
self
.boot_active
}
}
...
...
This diff is collapsed.
Click to expand it.
src/pad.rs
+
30
−
0
View file @
40a3f4c8
#[cfg(feature
=
"wasm"
)]
use
wasm_bindgen
::
prelude
::
*
;
pub
struct
Pad
{
pub
struct
Pad
{
down
:
bool
,
down
:
bool
,
up
:
bool
,
up
:
bool
,
...
@@ -16,6 +19,7 @@ pub enum PadSelection {
...
@@ -16,6 +19,7 @@ pub enum PadSelection {
Direction
,
Direction
,
}
}
#[cfg_attr(feature
=
"wasm"
,
wasm_bindgen)]
pub
enum
PadKey
{
pub
enum
PadKey
{
Up
,
Up
,
Down
,
Down
,
...
@@ -87,4 +91,30 @@ impl Pad {
...
@@ -87,4 +91,30 @@ impl Pad {
addr
=>
panic!
(
"Writing to unknown Pad location 0x{:04x}"
,
addr
),
addr
=>
panic!
(
"Writing to unknown Pad location 0x{:04x}"
,
addr
),
}
}
}
}
pub
fn
key_press
(
&
mut
self
,
key
:
PadKey
)
{
match
key
{
PadKey
::
Up
=>
self
.up
=
true
,
PadKey
::
Down
=>
self
.down
=
true
,
PadKey
::
Left
=>
self
.left
=
true
,
PadKey
::
Right
=>
self
.right
=
true
,
PadKey
::
Start
=>
self
.start
=
true
,
PadKey
::
Select
=>
self
.select
=
true
,
PadKey
::
A
=>
self
.a
=
true
,
PadKey
::
B
=>
self
.b
=
true
,
}
}
pub
fn
key_lift
(
&
mut
self
,
key
:
PadKey
)
{
match
key
{
PadKey
::
Up
=>
self
.up
=
false
,
PadKey
::
Down
=>
self
.down
=
false
,
PadKey
::
Left
=>
self
.left
=
false
,
PadKey
::
Right
=>
self
.right
=
false
,
PadKey
::
Start
=>
self
.start
=
false
,
PadKey
::
Select
=>
self
.select
=
false
,
PadKey
::
A
=>
self
.a
=
false
,
PadKey
::
B
=>
self
.b
=
false
,
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment