pcenginecd_rust/
seek.rs

1//! Seek time between two sectors, from Dave Shadoff's measurements on re-capped original
2//! hardware (`seek.py`; his `seektime.c` and `measurements/`, MIT: see NOTICE.md). Tier 3:
3//! measured by a person who published the method, not a manufacturer; resolution is one VSYNC
4//! (1/60 s).
5//!
6//! The disc is divided into 14 zones of constant sectors per revolution; the head travels about
7//! 1606.48 tracks across each full zone. The time is a step function of the track distance plus
8//! half a rotation at the target zone, in VSYNCs of 1000/60 ms:
9//!
10//! | track distance | time |
11//! | --- | --- |
12//! | sector gap < 5 | 9 VSYNC + half a rotation |
13//! | <= 80 | 16 VSYNC + half a rotation |
14//! | <= 160 | 22 VSYNC + half a rotation |
15//! | <= 644 | 22 VSYNC + half a rotation + (distance - 161) * 16.66 / 80 ms |
16//! | more | 36 VSYNC + (distance - 644) * 16.66 / 195 ms |
17//!
18//! C20: `seektime.c` begins with `if (gap < 2) {3 VSYNC}` and then `if (gap < 5)` (not `else
19//! if`), so the 3-VSYNC value is always overwritten and a gap of 0-1 costs 9 VSYNC + half a
20//! rotation as for 2-4; the printed number is that of the code as written, which is what is
21//! built here. The audio-seek measurement (`seekaud_huc`: confirmation versus actual playback)
22//! is not applied (U8).
23//!
24//! Differences forced by Rust: the reference's arithmetic mixes `int` and `float`; here the
25//! integer parts are `i64` with Python's floor division and every float operation is an `f64`
26//! one in the reference's order, so the result is the same double.
27
28use pcengine_rust::machine::py_floordiv;
29
30pub const VSYNC_MS: f64 = 1000.0 / 60.0;
31pub const TRACKS_PER_FULL_ZONE: f64 = 1606.48;
32
33/// (sectors per revolution, first sector, last sector, rotation ms)
34pub const ZONES: [(i64, i64, i64, f64); 14] = [
35    (10, 0, 12572, 133.47),
36    (11, 12573, 30244, 146.82),
37    (12, 30245, 49523, 160.17),
38    (13, 49524, 70408, 173.51),
39    (14, 70409, 92900, 186.86),
40    (15, 92901, 116998, 200.21),
41    (16, 116999, 142703, 213.56),
42    (17, 142704, 170014, 226.90),
43    (18, 170015, 198932, 240.25),
44    (19, 198933, 229456, 253.60),
45    (20, 229457, 261587, 266.95),
46    (21, 261588, 295324, 280.29),
47    (22, 295325, 330668, 293.64),
48    (23, 330669, 333012, 306.99),
49];
50
51fn zone(lba: i64) -> usize {
52    for (i, &(_, first, last, _)) in ZONES.iter().enumerate() {
53        if first <= lba && lba <= last {
54            return i;
55        }
56    }
57    if lba < 0 {
58        0
59    } else if lba > ZONES[ZONES.len() - 1].2 {
60        ZONES.len() - 1
61    } else {
62        0
63    }
64}
65
66pub fn seek_time_ms(start: i64, target: i64) -> f64 {
67    let (src, dst) = (zone(start), zone(target));
68    let per_rev = ZONES[dst].0;
69    let tracks: f64 = if src == dst {
70        py_floordiv((target - start).abs(), per_rev) as f64
71    } else if dst > src {
72        let whole = py_floordiv(ZONES[src].2 - start, ZONES[src].0)
73            + py_floordiv(target - ZONES[dst].1, per_rev);
74        whole as f64 + TRACKS_PER_FULL_ZONE * (dst - src - 1) as f64
75    } else {
76        let whole = py_floordiv(start - ZONES[src].1, ZONES[src].0)
77            + py_floordiv(ZONES[dst].2 - target, per_rev);
78        whole as f64 + TRACKS_PER_FULL_ZONE * (src - dst - 1) as f64
79    };
80    let half = ZONES[dst].3 / 2.0;
81    let gap = (target - start).abs();
82    if gap < 5 {
83        return 9.0 * VSYNC_MS + half;
84    }
85    if tracks <= 80.0 {
86        return 16.0 * VSYNC_MS + half;
87    }
88    if tracks <= 160.0 {
89        return 22.0 * VSYNC_MS + half;
90    }
91    if tracks <= 644.0 {
92        return 22.0 * VSYNC_MS + half + (tracks - 161.0) * 16.66 / 80.0;
93    }
94    36.0 * VSYNC_MS + (tracks - 644.0) * 16.66 / 195.0
95}