(** * Copyright (c) 2016, Fabien Freling * All rights reserved. * * This source code is licensed under the BSD 2-clause license found in the * LICENSE file at the top level directory of this source tree. *) 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