pcengine_rust/machine.rs
1//! The machine table and the clock constants every module shares
2//! (`machine.py`).
3//!
4//! One board, a table of rows: the PC Engine and the TurboGrafx-16 differ by a
5//! pin (the reference's docs/start-here.md). Add-ons (SuperGrafx, CD) add rows
6//! or build on this board; they do not fork it.
7//!
8//! Differences forced by Rust: time is `i64` master clocks everywhere (the
9//! reference's unbounded `int`; a line's start may be negative with
10//! `start_offset`).
11
12/// [hw: pcetech 2.4] 21.47727 MHz master clock (6 x NTSC colour burst).
13pub const MASTER_HZ: i64 = 21_477_272;
14
15/// [doc: wiki "Timing"; emu: Mednafen vce.cpp] a line is 1365 master clocks whatever the dot clock
16/// (C5: "roughly 342 dots" at 5 MHz is 1365 / 4 = 341.25).
17pub const LINE_CLOCKS: i64 = 1365;
18
19/// [emu: Mednafen vce.cpp l.561 `hblank_counter = hblank ? 237 : 1128`] the VCE's horizontal sync
20/// is 237 master clocks at the start of a line. Tier 5; the real width is not published (U19).
21pub const HSYNC_CLOCKS: i64 = 237;
22
23/// [emu: Mednafen vce.cpp `vblank_counter = vblank ? 4095 : ...`] the vertical sync is the last
24/// three lines of a frame (4095 = 3 x 1365). [hw: pcetech 7] "3 lines for the sync area".
25pub const VSYNC_LINES: i64 = 3;
26
27/// [hw: pcetech 2.4] CPU clock is master/3 (high) or master/12 (low); power-up is low.
28pub const CPU_CLOCKS_HIGH: i64 = 3;
29pub const CPU_CLOCKS_LOW: i64 = 12;
30
31/// [hw: pcetech 2.2] the timer counts once per 1024 high-speed CPU cycles, whatever the CPU speed.
32pub const TIMER_TICK_CLOCKS: i64 = 1024 * 3;
33
34/// [hw: pcetech 7] the frame layout: 14 top blank, 242 active, 4 bottom blank, 3 sync.
35pub const TOP_BLANK_LINES: i64 = 14;
36pub const ACTIVE_LINES: i64 = 242;
37
38/// [hw: pcetech 7; doc: wiki] 263-line frame; the 262-line form follows VCE bit 2 (C3).
39pub const FRAME_LINES_LONG: i64 = 263;
40pub const FRAME_LINES_SHORT: i64 = 262;
41
42/// A page of the page table is 256 bytes over the 21-bit space: 8192 pages (docs/sockets.md).
43pub const PAGE_BITS: u32 = 8;
44pub const PAGE_COUNT: usize = 1 << 13;
45pub const ADDRESS_MASK: u32 = (1 << 21) - 1;
46
47/// The hardware page, bank $FF: physical $1FE000-$1FFFFF (pages 0x1FE0-0x1FFF).
48pub const HARDWARE_BASE: u32 = 0x1FE000;
49pub const HARDWARE_FIRST_PAGE: usize = (HARDWARE_BASE >> PAGE_BITS) as usize;
50
51/// One row of the machine table.
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub struct Machine {
54 pub name: &'static str,
55 /// [hw: pcetech 4] I/O port D6: 1 = PC Engine, 0 = TurboGrafx-16 (US games check it).
56 pub country_pc_engine: bool,
57 /// 8 KiB of work RAM, mirrored through banks $F8-$FB; the SuperGrafx add-on has 32 KiB.
58 pub work_ram_bytes: usize,
59 pub vdc_count: usize,
60 /// C3: MacDonald calls VCE bit 2 "blur"; Mednafen, ares and Mesen2 take it as 263 lines when
61 /// set and 262 when clear; MAME ignores it. True follows the three emulators (tier 5).
62 pub lines_follow_vce_bit2: bool,
63}
64
65impl Machine {
66 /// A row with the reference's defaults (8 KiB, one VDC, lines follow VCE bit 2).
67 pub const fn new(name: &'static str, country_pc_engine: bool) -> Machine {
68 Machine {
69 name,
70 country_pc_engine,
71 work_ram_bytes: 8 * 1024,
72 vdc_count: 1,
73 lines_follow_vce_bit2: true,
74 }
75 }
76}
77
78pub const PC_ENGINE: Machine = Machine::new("PC Engine", true);
79pub const TURBOGRAFX_16: Machine = Machine::new("TurboGrafx-16", false);
80
81/// Python's `//` on integers: the quotient rounded toward minus infinity.
82#[inline]
83pub fn py_floordiv(a: i64, b: i64) -> i64 {
84 a.div_euclid(b) - if b < 0 && a.rem_euclid(b) != 0 { 1 } else { 0 }
85}
86
87/// Python's `round()` of a float: ties to even.
88#[inline]
89pub fn round_ties_even(value: f64) -> i64 {
90 value.round_ties_even() as i64
91}