Implement timers
This commit is contained in:
parent
6c7544dffb
commit
67093196ea
3 changed files with 102 additions and 12 deletions
38
src/timer.ml
Normal file
38
src/timer.ml
Normal file
|
@ -0,0 +1,38 @@
|
|||
|
||||
let frequence = 4194304
|
||||
|
||||
type t = {
|
||||
clock_rate : int;
|
||||
period : int;
|
||||
mutable enabled : bool;
|
||||
mutable cycles_left : int;
|
||||
}
|
||||
|
||||
let create clock_rate enabled =
|
||||
let period = frequence / clock_rate in
|
||||
{
|
||||
clock_rate;
|
||||
period;
|
||||
enabled;
|
||||
cycles_left = period;
|
||||
}
|
||||
|
||||
(** TIMA - Timer counter *)
|
||||
let create_tima tac =
|
||||
let enabled = Bit.is_set tac 2 in
|
||||
let clock_select = tac land 0b00000011 in
|
||||
let clock_rate = match clock_select with
|
||||
| 0 -> 4096
|
||||
| 1 -> 262144
|
||||
| 2 -> 65536
|
||||
| 3 -> 16384
|
||||
| _ -> failwith "Unreachable clock rate code."
|
||||
in
|
||||
create clock_rate enabled
|
||||
|
||||
let update timer cycles =
|
||||
let remain = timer.cycles_left - cycles in
|
||||
let should_inc = remain <= 0 in
|
||||
if should_inc then
|
||||
timer.cycles_left <- remain + timer.period;
|
||||
should_inc
|
Loading…
Add table
Add a link
Reference in a new issue