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
2bbb0a04
Verified
Commit
2bbb0a04
authored
7 months ago
by
João Magalhães
Browse files
Options
Downloads
Patches
Plain Diff
chore: initial state management for timer
parent
4412e07f
No related branches found
No related tags found
1 merge request
!59
`StateComponent` creation and implementation
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
crates/common/src/data.rs
+24
-0
24 additions, 0 deletions
crates/common/src/data.rs
crates/common/src/lib.rs
+1
-0
1 addition, 0 deletions
crates/common/src/lib.rs
src/timer.rs
+77
-1
77 additions, 1 deletion
src/timer.rs
with
102 additions
and
1 deletion
crates/common/src/data.rs
0 → 100644
+
24
−
0
View file @
2bbb0a04
use
std
::
io
::{
Cursor
,
Read
};
use
crate
::
error
::
Error
;
#[inline(always)]
pub
fn
read_u8
(
data
:
&
mut
Cursor
<&
[
u8
]
>
)
->
Result
<
u8
,
Error
>
{
let
mut
buffer
=
[
0x00
;
size_of
::
<
u8
>
()];
data
.read_exact
(
&
mut
buffer
)
?
;
Ok
(
u8
::
from_le_bytes
(
buffer
))
}
#[inline(always)]
pub
fn
read_u16
(
data
:
&
mut
Cursor
<&
[
u8
]
>
)
->
Result
<
u16
,
Error
>
{
let
mut
buffer
=
[
0x00
;
size_of
::
<
u16
>
()];
data
.read_exact
(
&
mut
buffer
)
?
;
Ok
(
u16
::
from_le_bytes
(
buffer
))
}
#[inline(always)]
pub
fn
read_u32
(
data
:
&
mut
Cursor
<&
[
u8
]
>
)
->
Result
<
u32
,
Error
>
{
let
mut
buffer
=
[
0x00
;
size_of
::
<
u32
>
()];
data
.read_exact
(
&
mut
buffer
)
?
;
Ok
(
u32
::
from_le_bytes
(
buffer
))
}
This diff is collapsed.
Click to expand it.
crates/common/src/lib.rs
+
1
−
0
View file @
2bbb0a04
pub
mod
bench
;
pub
mod
bench
;
pub
mod
data
;
pub
mod
error
;
pub
mod
error
;
pub
mod
util
;
pub
mod
util
;
...
...
This diff is collapsed.
Click to expand it.
src/timer.rs
+
77
−
1
View file @
2bbb0a04
//! Timer functions and structures.
//! Timer functions and structures.
use
std
::
io
::{
Cursor
,
Write
};
use
boytacean_common
::{
data
::{
read_u16
,
read_u8
},
error
::
Error
,
};
use
crate
::{
use
crate
::{
consts
::{
DIV_ADDR
,
TAC_ADDR
,
TIMA_ADDR
,
TMA_ADDR
},
consts
::{
DIV_ADDR
,
TAC_ADDR
,
TIMA_ADDR
,
TMA_ADDR
},
mmu
::
BusComponent
,
mmu
::
BusComponent
,
panic_gb
,
warnln
,
panic_gb
,
state
::
StateComponent
,
warnln
,
};
};
pub
struct
Timer
{
pub
struct
Timer
{
...
@@ -161,8 +170,75 @@ impl BusComponent for Timer {
...
@@ -161,8 +170,75 @@ impl BusComponent for Timer {
}
}
}
}
impl
StateComponent
for
Timer
{
fn
state
(
&
self
)
->
Result
<
Vec
<
u8
>
,
Error
>
{
let
mut
cursor
=
Cursor
::
new
(
vec!
[]);
cursor
.write
(
&
self
.div
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.tima
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.tma
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.tac
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.div_clock
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.tima_clock
.to_le_bytes
())
?
;
cursor
.write
(
&
(
self
.tima_enabled
as
u8
)
.to_le_bytes
())
?
;
cursor
.write
(
&
self
.tima_ratio
.to_le_bytes
())
?
;
cursor
.write
(
&
(
self
.int_tima
as
u8
)
.to_le_bytes
())
?
;
Ok
(
cursor
.into_inner
())
}
fn
set_state
(
&
mut
self
,
data
:
&
[
u8
])
->
Result
<
(),
boytacean_common
::
error
::
Error
>
{
let
mut
cursor
=
Cursor
::
new
(
data
);
self
.div
=
read_u8
(
&
mut
cursor
)
?
;
self
.tima
=
read_u8
(
&
mut
cursor
)
?
;
self
.tma
=
read_u8
(
&
mut
cursor
)
?
;
self
.tac
=
read_u8
(
&
mut
cursor
)
?
;
self
.div_clock
=
read_u16
(
&
mut
cursor
)
?
;
self
.tima_clock
=
read_u16
(
&
mut
cursor
)
?
;
self
.tima_enabled
=
read_u8
(
&
mut
cursor
)
?
==
1
;
self
.tima_ratio
=
read_u16
(
&
mut
cursor
)
?
;
self
.int_tima
=
read_u8
(
&
mut
cursor
)
?
==
1
;
Ok
(())
}
}
impl
Default
for
Timer
{
impl
Default
for
Timer
{
fn
default
()
->
Self
{
fn
default
()
->
Self
{
Self
::
new
()
Self
::
new
()
}
}
}
}
#[cfg(test)]
mod
tests
{
use
super
::
Timer
;
use
crate
::
state
::
StateComponent
;
#[test]
fn
test_state_and_set_state
()
{
let
mut
timer
=
Timer
::
new
();
timer
.div
=
0x12
;
timer
.tima
=
0x34
;
timer
.tma
=
0x56
;
timer
.tac
=
0x78
;
timer
.div_clock
=
0x9abc
;
timer
.tima_clock
=
0xdef0
;
timer
.tima_enabled
=
true
;
timer
.tima_ratio
=
0x1234
;
timer
.int_tima
=
true
;
let
state
=
timer
.state
()
.unwrap
();
assert_eq!
(
state
.len
(),
12
);
let
mut
new_timer
=
Timer
::
new
();
new_timer
.set_state
(
&
state
)
.unwrap
();
assert_eq!
(
timer
.div
,
new_timer
.div
);
assert_eq!
(
timer
.tima
,
new_timer
.tima
);
assert_eq!
(
timer
.tma
,
new_timer
.tma
);
assert_eq!
(
timer
.tac
,
new_timer
.tac
);
assert_eq!
(
timer
.div_clock
,
new_timer
.div_clock
);
assert_eq!
(
timer
.tima_clock
,
new_timer
.tima_clock
);
assert_eq!
(
timer
.tima_enabled
,
new_timer
.tima_enabled
);
assert_eq!
(
timer
.tima_ratio
,
new_timer
.tima_ratio
);
assert_eq!
(
timer
.int_tima
,
new_timer
.int_tima
);
}
}
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