pcengine_rust/cpu_io/timer.rs
1//! The HuC6280's 7-bit timer ($0C00-$0FFF, mirrored every 2 bytes) (`cpu_io/timer.py`).
2//!
3//! [hw: pcetech 2.2] One tick per 1024 high-speed CPU cycles (master/3072) whatever the CPU speed.
4//! `$0C00` is a latch (0 counts as 1, $7F as 128); `$0C01` bit 0 enables. The interrupt line is
5//! asserted when the counter goes from 0 to $7F; at that moment it is reloaded from the latch and
6//! keeps counting, whether or not the interrupt has been acknowledged.
7//!
8//! [mfr: HuC6280 hardware manual 2.7] "When 0 is written in the Timer Start/Stop flag, the timer
9//! stops and the prescaler is reset", and the down-counter is loaded from the reload register when the
10//! flag goes from 0 to 1. So a tick is one full prescaler period (1024 cycles of master/3, 3072 master
11//! clocks) after the enabling write, and a stop and restart always begin a fresh period (U24, closed
12//! as far as the sources go: tier 3 for the reset, the full first period follows from it; what stays
13//! tier 5 is Mednafen's one-clock-late reload after an underflow, which MAME does not have, and
14//! this model follows pcetech 2.2's "reloaded when it occurs"). The manual also cautions that the
15//! count can read invalid while it changes; that is not modelled.
16//!
17//! C25 (M5 sweep, Battle Royale): the count is visible as $7F for a short window after an underflow
18//! before the reload shows. Battle Royale programs a latch of 0 and polls `$0C00` until it reads
19//! non-zero, so with an instant reload it never leaves the poll (black screen; both detectors run it).
20//! pcetech 2.2's "goes from 0 to $7F" is the same statement. The window is `underflow_window_mclk`:
21//! 15 master clocks (Mesen2's `Scaler <= 5 * 3`, a comment there names this game) is the default [emu,
22//! tier 5]; Mednafen's is 1 master clock (`timer_inreload`), 0 is the instant reload of the first
23//! model. The width is not measured on hardware.
24
25use serde::{Deserialize, Serialize};
26
27pub const TICK_CLOCKS: i64 = 1024 * 3;
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
30pub struct Timer {
31 /// C25: master clocks for which the count reads $7F after an underflow (see the module doc).
32 /// A switch, not state: a loaded state keeps the system's own.
33 pub underflow_window_mclk: i64,
34 /// [hw: pcetech 2] power-up: count 0, disabled
35 pub latch: u8,
36 pub count: u8,
37 pub enabled: bool,
38 /// the TIQ line: held until $1403 is written
39 pub irq: bool,
40 next_tick: i64,
41 window_end: i64,
42}
43
44impl Default for Timer {
45 fn default() -> Self {
46 Timer::new()
47 }
48}
49
50impl Timer {
51 pub fn new() -> Timer {
52 Timer {
53 underflow_window_mclk: 15,
54 latch: 0,
55 count: 0,
56 enabled: false,
57 irq: false,
58 next_tick: 0,
59 window_end: 0,
60 }
61 }
62
63 pub fn reset(&mut self) {
64 self.latch = 0;
65 self.count = 0;
66 self.enabled = false;
67 self.irq = false;
68 self.window_end = 0;
69 }
70
71 pub fn catch_up(&mut self, mclk: i64) {
72 while self.enabled && self.next_tick <= mclk {
73 self.next_tick += TICK_CLOCKS;
74 if self.count == 0 {
75 // 0 -> $7F underflow: assert the line, reload from the latch (pcetech 2.2).
76 self.irq = true;
77 self.count = self.latch;
78 self.window_end = self.next_tick - TICK_CLOCKS + self.underflow_window_mclk;
79 } else {
80 self.count -= 1;
81 }
82 }
83 }
84
85 /// Master-clock time of the next underflow, or None while disabled.
86 pub fn next_event(&self) -> Option<i64> {
87 if !self.enabled {
88 return None;
89 }
90 Some(self.next_tick + self.count as i64 * TICK_CLOCKS)
91 }
92
93 pub fn read(&mut self, buffer: u8, mclk: i64) -> u8 {
94 self.catch_up(mclk);
95 // Bit 7 returns the I/O buffer; the low seven bits are the live count.
96 if mclk < self.window_end {
97 return 0x7F | (buffer & 0x80); // C25: $7F before the reload shows
98 }
99 self.count | (buffer & 0x80)
100 }
101
102 pub fn write(&mut self, offset: u32, value: u8, mclk: i64) {
103 self.catch_up(mclk);
104 if offset & 1 == 0 {
105 // The latch only; the running count is untouched until the next reload.
106 self.latch = value & 0x7F;
107 } else {
108 let enable = value & 1 != 0;
109 if enable && !self.enabled {
110 // Disabled -> enabled: reload from the latch, restart the prescaler (U24).
111 self.count = self.latch;
112 self.next_tick = mclk + TICK_CLOCKS;
113 }
114 self.enabled = enable;
115 }
116 }
117
118 /// [hw: pcetech 2.1] any write to $1403 acknowledges the timer.
119 pub fn acknowledge(&mut self) {
120 self.irq = false;
121 }
122}