Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# recompiled code calls, routed to a RuntimeServices implementation; the EE kernel and the dispatcher (bozo/ee/); the
# per-frame reference record (bozo/reference.h); the native libraries (bozo/libs/).
add_library(bozo_runtime_core STATIC
core/precise_sleep.cpp
core/runtime_core.cpp
core/state.cpp
ee/kernel.cpp
Expand Down
2 changes: 1 addition & 1 deletion runtime/app/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ namespace bozo::app
if (t > nextPresent + period)
nextPresent = t;
if (nextPresent - t > std::chrono::milliseconds(2))
std::this_thread::sleep_until(nextPresent - std::chrono::milliseconds(1));
sleepUntil(nextPresent - std::chrono::milliseconds(1));
while (Clock::now() < nextPresent)
std::this_thread::yield();
}
Expand Down
60 changes: 60 additions & 0 deletions runtime/core/precise_sleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root).
#include "bozo/app/precise_sleep.h"

#include <thread>

#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
#endif
#endif

namespace bozo::app
{
#if defined(_WIN32)
namespace
{
struct Timer
{
HANDLE handle = nullptr;
Timer()
{
handle = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (!handle)
handle = CreateWaitableTimerW(nullptr, FALSE, nullptr);
}
~Timer()
{
if (handle)
CloseHandle(handle);
}
Timer(const Timer &) = delete;
Timer &operator=(const Timer &) = delete;
};
}

void sleepUntil(std::chrono::steady_clock::time_point until)
{
const auto left = until - std::chrono::steady_clock::now();
if (left <= std::chrono::steady_clock::duration::zero())
return;
thread_local Timer timer;
LARGE_INTEGER due;
due.QuadPart = -static_cast<LONGLONG>(std::chrono::duration_cast<std::chrono::nanoseconds>(left).count() / 100);
if (timer.handle && due.QuadPart < 0 && SetWaitableTimer(timer.handle, &due, 0, nullptr, nullptr, FALSE) &&
WaitForSingleObject(timer.handle, INFINITE) == WAIT_OBJECT_0)
return;
std::this_thread::sleep_until(until);
}
#else
void sleepUntil(std::chrono::steady_clock::time_point until) { std::this_thread::sleep_until(until); }
#endif
}
3 changes: 2 additions & 1 deletion runtime/include/bozo/app/frame_pacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <thread>

#include "bozo/app/determinism.h"
#include "bozo/app/precise_sleep.h"

// Native frame pacing: a game that opts in runs its own loop on real time instead of on the PS2's vblanks. Its frame
// end calls frameEnd(), which holds the frame to the cap (frames per second, 0 = unlimited: only the host's speed
Expand Down Expand Up @@ -92,7 +93,7 @@ class FramePacer
due = m_previous + period;
// Sleep most of the wait, then spin the last stretch: Windows sleeps overshoot by up to a millisecond.
if (due - now > std::chrono::milliseconds(2))
std::this_thread::sleep_until(due - std::chrono::milliseconds(1));
sleepUntil(due - std::chrono::milliseconds(1));
while ((now = Clock::now()) < due)
std::this_thread::yield();
}
Expand Down
11 changes: 11 additions & 0 deletions runtime/include/bozo/app/precise_sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root).
#pragma once

#include <chrono>

namespace bozo::app
{
// On Windows a waitable timer: MinGW's sleep_until oversleeps by up to a timer tick under some Wine builds.
void sleepUntil(std::chrono::steady_clock::time_point until);
}
2 changes: 2 additions & 0 deletions runtime/include/bozo/ops_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#define BOZO_LIKELY(x) __builtin_expect(!!(x), 1)
#define BOZO_UNLIKELY(x) __builtin_expect(!!(x), 0)
#define BOZO_INLINE inline __attribute__((always_inline))
#define BOZO_NOINLINE inline __attribute__((noinline))
#else
#define BOZO_LIKELY(x) (x)
#define BOZO_UNLIKELY(x) (x)
#define BOZO_INLINE inline
#define BOZO_NOINLINE inline
#endif

namespace bozo
Expand Down
60 changes: 36 additions & 24 deletions runtime/include/bozo/ops_vu0.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "bozo/context.h"
#include "bozo/ops_fpu.h"
#include "bozo/ops_mem.h"
#include "bozo/runtime_api.h"

#include <cstring>
Expand All @@ -23,13 +24,13 @@ namespace bozo::vu0
inline bool fieldOn(uint32_t dest, int i) { return (dest >> (3 - i)) & 1u; }

// The second operand of an FMAC instruction: a vector, one field broadcast, Q or I.
inline Vec broadcast(uint32_t bits)
BOZO_INLINE Vec broadcast(uint32_t bits)
{
Vec v;
v.u[0] = v.u[1] = v.u[2] = v.u[3] = bits;
return v;
}
inline Vec bc(const Vec &t, uint32_t field) { return broadcast(t.u[field]); }
BOZO_INLINE Vec bc(const Vec &t, uint32_t field) { return broadcast(t.u[field]); }

// ---- Arithmetic of one field ----
// VU0's adder rounds to nearest even (unlike the FPU's, ops_fpu.h); its multiplier is the FPU's.
Expand Down Expand Up @@ -123,24 +124,24 @@ namespace bozo::vu0
Madd,
Msub
};
inline __m128i load(const Vec &v) { return _mm_loadu_si128(reinterpret_cast<const __m128i *>(v.u)); }
inline __m128i exponent(__m128i x) { return _mm_and_si128(_mm_srli_epi32(x, 23), _mm_set1_epi32(0xFF)); }
inline __m128i zero(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_setzero_si128()); } // or denormal
inline __m128i top(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(0xFF)); }
inline __m128i abnormal(__m128i x) { return _mm_or_si128(zero(x), top(x)); }
BOZO_INLINE __m128i load(const Vec &v) { return _mm_loadu_si128(reinterpret_cast<const __m128i *>(v.u)); }
BOZO_INLINE __m128i exponent(__m128i x) { return _mm_and_si128(_mm_srli_epi32(x, 23), _mm_set1_epi32(0xFF)); }
BOZO_INLINE __m128i zero(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_setzero_si128()); } // or denormal
BOZO_INLINE __m128i top(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(0xFF)); }
BOZO_INLINE __m128i abnormal(__m128i x) { return _mm_or_si128(zero(x), top(x)); }
// Exponent 1, the smallest normals: a product just below 2^-126 rounds up to them on the host, where the PS2
// flushes it to zero with the underflow flags (ps2f::mul's shortcut leaves them to the exact path too).
inline __m128i bottom(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(1)); }
inline __m128i select(__m128i mask, __m128i a, __m128i b) { return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)); }
inline __m128i signBits() { return _mm_set1_epi32(static_cast<int32_t>(0x80000000u)); }
BOZO_INLINE __m128i bottom(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(1)); }
BOZO_INLINE __m128i select(__m128i mask, __m128i a, __m128i b) { return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)); }
BOZO_INLINE __m128i signBits() { return _mm_set1_epi32(static_cast<int32_t>(0x80000000u)); }
// Lane i on when dest enables field i (x = dest bit 3 = lane 0).
inline __m128i lanes(uint32_t dest)
BOZO_INLINE __m128i lanes(uint32_t dest)
{
return _mm_set_epi32(-static_cast<int32_t>(dest & 1u), -static_cast<int32_t>((dest >> 1) & 1u),
-static_cast<int32_t>((dest >> 2) & 1u), -static_cast<int32_t>((dest >> 3) & 1u));
}
// s * t; `bad` collects the lanes outside the cases above.
inline __m128i mul(__m128i s, __m128i t, __m128i &bad)
BOZO_INLINE __m128i mul(__m128i s, __m128i t, __m128i &bad)
{
const __m128i z = _mm_or_si128(zero(s), zero(t));
const __m128i p = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(s), _mm_castsi128_ps(t)));
Expand All @@ -149,7 +150,7 @@ namespace bozo::vu0
return select(z, _mm_and_si128(_mm_xor_si128(s, t), signBits()), p);
}
// a + b, likewise.
inline __m128i add(__m128i a, __m128i b, __m128i &bad)
BOZO_INLINE __m128i add(__m128i a, __m128i b, __m128i &bad)
{
const __m128i za = zero(a), zb = zero(b);
const __m128i sum = _mm_castps_si128(_mm_add_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b)));
Expand All @@ -163,7 +164,7 @@ namespace bozo::vu0
// d (op) on the enabled fields with the flags, when every enabled field is a case above; false (nothing
// written) otherwise.
template <Op op>
inline bool fmac(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest)
BOZO_INLINE bool fmac(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest)
{
const __m128i S = load(s), T = load(t), on = lanes(dest);
__m128i bad = _mm_setzero_si128(), R;
Expand Down Expand Up @@ -225,28 +226,30 @@ namespace bozo::vu0
#else
#define BOZO_VU0_FAST(op, acc)
#endif
inline void add(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest)
BOZO_NOINLINE void addExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, addf); }
BOZO_NOINLINE void subExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, subf); }
BOZO_NOINLINE void mulExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, mulf); }
BOZO_INLINE void add(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest)
{
BOZO_VU0_FAST(Add, s)
fmac(ctx, d, s, t, dest, addf);
addExact(ctx, d, s, t, dest);
}
inline void sub(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest)
BOZO_INLINE void sub(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest)
{
BOZO_VU0_FAST(Sub, s)
fmac(ctx, d, s, t, dest, subf);
subExact(ctx, d, s, t, dest);
}
inline void mul(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest)
BOZO_INLINE void mul(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest)
{
BOZO_VU0_FAST(Mul, s)
fmac(ctx, d, s, t, dest, mulf);
mulExact(ctx, d, s, t, dest);
}
// max / mini do not change the flags.
inline void max(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::max); }
inline void mini(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::min); }
// madd: d = acc + s * t (the product rounded first, then the sum: Q6). msub: d = acc - s * t.
inline void madd(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest)
BOZO_NOINLINE void maddExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest)
{
BOZO_VU0_FAST(Madd, acc)
uint32_t r[4] = {}, fl[4] = {};
for (int i = 0; i < 4; ++i)
if (fieldOn(dest, i))
Expand All @@ -259,9 +262,13 @@ namespace bozo::vu0
d.u[i] = r[i];
setFlags(ctx, dest, r, fl);
}
inline void msub(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest)
BOZO_INLINE void madd(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest)
{
BOZO_VU0_FAST(Madd, acc)
maddExact(ctx, d, acc, s, t, dest);
}
BOZO_NOINLINE void msubExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest)
{
BOZO_VU0_FAST(Msub, acc)
uint32_t r[4] = {}, fl[4] = {};
for (int i = 0; i < 4; ++i)
if (fieldOn(dest, i))
Expand All @@ -274,6 +281,11 @@ namespace bozo::vu0
d.u[i] = r[i];
setFlags(ctx, dest, r, fl);
}
BOZO_INLINE void msub(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest)
{
BOZO_VU0_FAST(Msub, acc)
msubExact(ctx, d, acc, s, t, dest);
}
// The outer product pair: opmula acc.xyz = (s.y*t.z, s.z*t.x, s.x*t.y); opmsub d.xyz = acc.xyz - (s.y*t.z, ...).
// The instruction's dest is xyz by definition (w untouched).
#undef BOZO_VU0_FAST
Expand Down
Loading