SaturnRingLibrary 0.9.2
SGL wrapper
Loading...
Searching...
No Matches
SRL::Tickstamp Class Referencefinal

Detailed Description

High-precision 48-bit timestamp with DVU hardware acceleration.

Stores timer values as a 48-bit composite (32-bit overflow counter + 16-bit FRT). The internal representation uses 64-bit storage (high=Timer32, low=FRT<<16) to enable hardware-accelerated division via the SH-2 DVU.

Architecture & PHI_128 Mode:
This class is designed specifically for the Sega Saturn's FRT (Free Running Timer) running in PHI_128 mode (~222 kHz, ~4.47μs precision). This matches SGL's default FRT configuration. Changing the FRT clock mode after initialization will break all timing calculations.
  • FRT (Free Running Timer): 16-bit hardware counter at PHI_128 (~222 kHz)
  • timer32: 32-bit overflow counter incremented each time FRT wraps (~295ms)
  • 48-bit total range: 0 to 2^48-1 ticks (~673 days at PHI_128)
Divider Strategy:
The DVU performs 64-bit / 32-bit division: (ticks << 16) / divisor where divisor = frequency / 128, matching the PHI_128 tick rate directly. This gives correct Fxp 16.16 results without any tick normalization shifts.

Example: 1 second at 26.6875 MHz

  • Ticks = 26,687,500 / 128 = 208,496
  • Dividend = 208,496 << 16 = 13,667,041,280
  • Divisor = 26,687,500 / 128 = 208,496
  • Result = 65,536 = 1.0 in Fxp 16.16 ✓
Fxp Conversion Limits:
While the Tickstamp can store up to ~673 days, conversion functions return Fxp (16.16 format) which has a hard limit of 32767 in the integer part:
Usage Example:
Tickstamp start = Timer::Capture(); // Capture start time
// ... do some work ...
Tickstamp end = Timer::Capture(); // Capture end time
Tickstamp elapsed = end - start; // Calculate elapsed (64-bit subtraction)
Fxp seconds = elapsed.ToSeconds(); // Convert to seconds (Fxp 16.16)
Math::Types::Fxp ToSeconds() const noexcept
Converts ticks to seconds using DVU hardware acceleration.
Definition srl_timer.hpp:528
static Tickstamp Capture() noexcept
Captures current hardware state into a Tickstamp.
Definition srl_timer.hpp:1082
Warning
This class assumes FRT is configured in PHI_128 mode (SGL default). Behavior is undefined if the FRT clock mode is changed after initialization.

#include <srl_timer.hpp>

Classes

struct  ClockTime
 Immutable clock display format (HH:MM:SS.mmm). More...

Public Member Functions

 Tickstamp ()=default
 Default constructor. Initializes to zero.
Core Operations

Essential timer operations for time calculations.

Tickstamp operator- (const Tickstamp &other) const noexcept
 64-bit subtraction with borrow (hardware-accelerated).
Tickstamp operator+ (const Tickstamp &other) const noexcept
 64-bit addition with carry (hardware-accelerated).
bool operator== (const Tickstamp &other) const noexcept
 Equality comparison.
bool operator!= (const Tickstamp &other) const noexcept
 Inequality comparison.
bool operator< (const Tickstamp &other) const noexcept
 Less-than comparison.
bool operator> (const Tickstamp &other) const noexcept
 Greater-than comparison.
bool operator<= (const Tickstamp &other) const noexcept
 Less-than-or-equal comparison.
bool operator>= (const Tickstamp &other) const noexcept
 Greater-than-or-equal comparison.
Math::Types::Fxp ToMilliseconds () const noexcept
 Converts ticks to milliseconds using DVU hardware acceleration.
Math::Types::Fxp ToSeconds () const noexcept
 Converts ticks to seconds using DVU hardware acceleration.
Math::Types::Fxp ToMinutes () const noexcept
 Converts ticks to minutes using DVU hardware acceleration.
ClockTime ToClock () const noexcept
 Converts ticks to clock display format (HH:MM:SS.mmm) using 1 DVU division.

Static Public Member Functions

Compile-Time Builders

Create Tickstamps at compile time with no runtime conversion overhead. These builders calculate both 26MHz and 28MHz tick counts at compile time, storing two complete Tickstamps, then return a reference to the appropriate one at runtime based on the active divider.

static constexpr Tickstamp FromTicks (uint64_t ticks)
 Create Tickstamp from raw tick count at compile time.
template<float Seconds>
static const Tickstamp & FromSeconds ()
 Create Tickstamp from seconds at compile time (dual-frequency).
template<float Milliseconds>
static const Tickstamp & FromMilliseconds ()
 Create Tickstamp from milliseconds at compile time (dual-frequency).
template<float Minutes>
static const Tickstamp & FromMinutes ()
 Create Tickstamp from minutes at compile time (dual-frequency).

Public Attributes

uint32_t High
 High 32 bits of the 48-bit timestamp.
uint32_t Low
 Low 32 bits formatted for DVU division.