What this program does
Compare the SDK's hand-scheduled kernels with a portable Rust reference. Every kernel runs on random and hand-picked edge inputs, from the same xorshift32 stream on every run, and must match the reference bit for bit. Each kernel is then timed on a fixed workload. The verdict is ASMPROBE PASS or ASMPROBE FAIL on the TTY, and per-function cycles come from the emulator's attribution logs over the link map. The source needs the nightly asm_experimental_arch feature, which the pinned toolchain provides.
Build and run
Use the documented SDK checkout and tool setup. Run these commands from the repository root.
make disc EXAMPLE=hello-asmprobe
Open build/examples/mipsel-sony-psx/release/hello-asmprobe.cue in the emulator with its sibling BIN in the same directory. The first-program walkthrough explains installation and loading. Run it on the headless frontend to read the verdict.
For original hardware, read the burned-disc requirements and warning.
Complete source
Complete, unabridged files from SDK revision 80d1c329e977. Build inside the repository checkout: the manifest uses relative SDK paths, and some programs embed assets from elsewhere in the repository.
Runtime dependencies: psx-gpu, psx-io, psx-math, psx-rt.
Cargo.toml · 23 lines
# SPDX-License-Identifier: GPL-2.0-or-later
[package]
name = "hello-asmprobe"
version = "0.1.0"
edition = "2021"
license = "GPL-2.0-or-later"
[workspace]
[[bin]]
name = "hello-asmprobe"
path = "src/main.rs"
[profile.release]
panic = "abort"
codegen-units = 1
lto = true
[dependencies]
psx-gpu = { path = "../../crates/psx-gpu", features = ["asm-kernels"] }
psx-rt = { path = "../../crates/psx-rt" }
psx-io = { path = "../../crates/psx-io" }
psx-math = { path = "../../crates/psx-math", features = ["asm-kernels"] }
src/clip.rs · 194 lines
// SPDX-License-Identifier: GPL-2.0-or-later
//! One-plane lane clip: the portable definition against the kernel.
use crate::{print_dec, Rng};
use core::hint::black_box;
use psx_math::clip_lanes::{clip_lanes8_plane, clip_lanes8_plane_reference, LanePlane, Lanes8};
/// GoldSrc-shaped planes for a 320x240 view with projection distance `h`.
fn view_planes(h: i32, capacity: u32) -> [LanePlane; 5] {
let (ofx, ofy, vx0, vx1, vy0, vy1) = (160, 120, 0, 320, 0, 240);
let plane = |primary_offset, primary, depth, bias, force| LanePlane {
primary_offset,
primary,
depth,
bias,
force_depth: force,
forced_depth: 8,
capacity,
};
[
plane(0, 0, 1, -8, 1), // near: z - 8, crossing lands on z = 8
plane(0, h, ofx - vx0, 0, 0), // left
plane(0, -h, vx1 - ofx, 0, 0), // right
plane(4, h, ofy - vy0, 0, 0), // top
plane(4, -h, vy1 - ofy, 0, 0), // bottom
]
}
fn make_vertex(rng: &mut Rng, mode: u32) -> Lanes8 {
let r = rng.next();
let s = rng.next();
let t = rng.next();
match mode {
0 => [
((r & 0x1fff) as i32) - 4096,
(((r >> 13) & 0x1fff) as i32) - 4096,
((s & 0x3fff) as i32) - 2000,
(t & 0xff) as i32,
((t >> 8) & 0xff) as i32,
((t >> 16) & 0xff) as i32,
(s >> 14 & 0xff) as i32,
(s >> 22 & 0xff) as i32,
],
// The whole i32 range on every lane: products and differences wrap.
1 => [
r as i32,
s as i32,
t as i32,
rng.next() as i32,
rng.next() as i32,
rng.next() as i32,
rng.next() as i32,
rng.next() as i32,
],
// Small coordinates and a z around the near plane, so distances hit 0
// and equal x or y make the endpoint order fall through to later lanes.
2 => [
(r & 3) as i32 - 1,
((r >> 2) & 3) as i32 - 1,
4 + ((s & 7) as i32),
(t & 0xff) as i32,
((t >> 8) & 0xff) as i32,
((t >> 16) & 0xff) as i32,
(s >> 8 & 0xff) as i32,
(s >> 16 & 0xff) as i32,
],
// Extremes.
_ => {
let pick = |v: u32| match v % 5 {
0 => i32::MIN,
1 => i32::MAX,
2 => 0,
3 => -1,
_ => 1,
};
[
pick(r),
pick(r >> 4),
pick(r >> 8),
pick(s),
pick(s >> 4),
pick(s >> 8),
pick(t),
pick(t >> 4),
]
}
}
}
static mut SRC: [Lanes8; 8] = [[0; 8]; 8];
static mut DST_REF: [Lanes8; 9] = [[0; 8]; 9];
static mut DST_TST: [Lanes8; 9] = [[0; 8]; 9];
pub fn run(cases: &mut u32, failures: &mut u32) {
let mut rng = Rng(0x1357_9bdf);
unsafe {
let src = core::ptr::addr_of_mut!(SRC) as *mut Lanes8;
let dref = core::ptr::addr_of_mut!(DST_REF) as *mut Lanes8;
let dtst = core::ptr::addr_of_mut!(DST_TST) as *mut Lanes8;
for mode in 0..4u32 {
for iteration in 0..400u32 {
let h = [256, 320, 1, 4096, -7][(iteration % 5) as usize];
let capacity = if iteration % 7 == 0 {
2 + (iteration / 7) % 7
} else {
8
};
let planes = view_planes(h, capacity);
let n = (rng.next() % 9) as usize; // 0..=8
for i in 0..n {
*src.add(i) = make_vertex(&mut rng, mode);
}
if iteration % 11 == 0 && n > 2 {
// Duplicate vertices: equal endpoints, d_a == d_b.
*src.add(1) = *src;
}
let plane = &planes[(iteration % 5) as usize];
// Poison both destinations identically, including the spare slot.
for i in 0..9 {
*dref.add(i) = [0x5a5a_5a5au32 as i32; 8];
*dtst.add(i) = [0x5a5a_5a5au32 as i32; 8];
}
*cases += 1;
let a = clip_lanes8_plane_reference(src, n, dref, plane);
let b = clip_lanes8_plane(src, n, dtst, plane);
let mut same = a == b;
for i in 0..9 {
if (*dref.add(i)) != (*dtst.add(i)) {
same = false;
}
}
if !same {
*failures += 1;
if *failures < 6 {
crate::tty_fail("clip", mode, iteration, (a as u32) << 8 | b as u32);
}
}
}
}
}
}
/// Representative HL polygons: triangles with one or two vertices outside the
/// plane, the way the view clip sees them, cycled across the five planes.
pub fn bench() {
let mut rng = Rng(0x2468_ace1);
unsafe {
let src = core::ptr::addr_of_mut!(SRC) as *mut Lanes8;
let dst = core::ptr::addr_of_mut!(DST_TST) as *mut Lanes8;
let planes = view_planes(256, 8);
let mut total_ref = 0u32;
let mut total_k = 0u32;
let mut over = 0u32;
let mut calls = 0u32;
for iteration in 0..200u32 {
// A triangle across the near plane or one side plane: vertices
// straddle by construction.
let plane = &planes[(iteration % 5) as usize];
let n = 3 + (iteration % 3) as usize; // triangles, quads, pentagons
for i in 0..n {
let mut v = make_vertex(&mut rng, 0);
v[2] = v[2].abs() + 8 + (i as i32 * 700);
// Push alternate vertices to the far side of the plane.
if i % 2 == 0 {
match iteration % 5 {
0 => v[2] = 2 + (v[2] & 3),
1 => v[0] = -(v[2] + 100),
2 => v[0] = v[2] + 100,
3 => v[1] = -(v[2] + 100),
_ => v[1] = v[2] + 100,
}
}
*src.add(i) = v;
}
let a = crate::clock();
let b = crate::clock();
over += b.wrapping_sub(a) as u32;
let a = crate::clock();
let nr = black_box(clip_lanes8_plane_reference)(src, n, dst, plane);
let b = crate::clock();
total_ref += b.wrapping_sub(a) as u32;
let a = crate::clock();
let nk = black_box(clip_lanes8_plane)(src, n, dst, plane);
let b = crate::clock();
total_k += b.wrapping_sub(a) as u32;
black_box((nr, nk));
calls += 1;
}
crate::tty_label_clip("reference");
print_dec("", (total_ref - over) * 10 / calls);
crate::tty_label_clip("kernel");
print_dec("", (total_k - over) * 10 / calls);
}
}
src/link.rs · 333 lines
// SPDX-License-Identifier: GPL-2.0-or-later
//! Tagged-packet-stream link loop: the portable reference, the inline-asm loop
//! the SDK uses by default, and the `asm-kernels` kernel.
use crate::{print_dec, print_u32, Rng};
use core::arch::global_asm;
use core::hint::black_box;
pub const OT_N: usize = 2048;
const OT_END: u32 = 0x00ff_ffff;
const MASK: u32 = 0x00ff_ffff;
const ARENA_WORDS: usize = 1 << 16;
#[repr(C, align(4))]
pub struct Ot(pub [u32; OT_N]);
static mut OT_REF: Ot = Ot([0; OT_N]);
static mut OT_TST: Ot = Ot([0; OT_N]);
static mut ARENA_REF: [u32; ARENA_WORDS + 64] = [0; ARENA_WORDS + 64];
static mut ARENA_TST: [u32; ARENA_WORDS + 64] = [0; ARENA_WORDS + 64];
static mut ARENA_SRC: [u32; ARENA_WORDS + 64] = [0; ARENA_WORDS + 64];
pub fn ot_clear(ot: &mut Ot) {
let base = ot.0.as_ptr() as u32;
ot.0[0] = OT_END;
for i in 1..OT_N {
ot.0[i] = base.wrapping_add(4 * (i as u32 - 1)) & MASK;
}
}
/// The portable definition, line for line the host branch of
/// `OrderingTable::link_tagged_packet_stream_unchecked`.
pub unsafe fn link_ref(entries: *mut u32, first: *mut u32, end: *mut u32) {
let mut p = first;
while p < end {
let tag = p.read();
let words = (tag >> 24) as usize;
let slot = (tag & 0xffff) as usize;
let next = p.add(words + 1);
if slot != 0xffff {
let head = entries.add(slot).read() & MASK;
p.write((tag & 0xff00_0000) | head);
entries.add(slot).write(p as u32 & MASK);
}
p = next;
}
}
extern "C" {
fn asmprobe_link_cur(first: *mut u32, end: *mut u32, entries: *mut u32);
}
// The inline-asm loop `ot.rs` uses without the `asm-kernels` feature, as a leaf
// function: the baseline the kernel is timed against.
global_asm!(
r#"
.set noreorder
.set nomacro
.section .text.asmprobe_link,"ax",@progbits
.globl asmprobe_link_cur
.type asmprobe_link_cur,@function
asmprobe_link_cur:
nop
sltu $t0, $a0, $a1
beqz $t0, 9f
nop
lui $t7, 0x00ff
ori $t7, $t7, 0xffff
ori $t9, $zero, 0xffff
lui $t8, 0xff00
2:
lw $t1, 0($a0)
and $t3, $a0, $t7
srl $t2, $t1, 22
andi $t5, $t1, 0xffff
addu $t2, $a0, $t2
beq $t5, $t9, 3f
addiu $t2, $t2, 4
sll $t5, $t5, 2
addu $t5, $a2, $t5
lw $t6, 0($t5)
and $t1, $t1, $t8
or $t6, $t6, $t1
sw $t6, 0($a0)
sw $t3, 0($t5)
3:
bne $t2, $a1, 2b
move $a0, $t2
9:
jr $ra
nop
.size asmprobe_link_cur, .-asmprobe_link_cur
"#
);
/// A deliberately wrong kernel for the harness self-check: it forgets the
/// sentinel and links skipped packets into slot 0xffff & mask.
unsafe extern "C" fn mutant_link(first: *mut u32, end: *mut u32, entries: *mut u32) {
let mut p = first;
while p < end {
let tag = p.read();
let words = (tag >> 24) as usize;
let slot = (tag & 0xffff) as usize & (OT_N - 1);
let next = p.add(words + 1);
let head = entries.add(slot).read() & MASK;
p.write((tag & 0xff00_0000) | head);
entries.add(slot).write(p as u32 & MASK);
p = next;
}
}
/// The SDK's own entry point (`asm-kernels` selects the kernel under test).
unsafe extern "C" fn sdk_link(first: *mut u32, end: *mut u32, entries: *mut u32) {
let ot = &mut *(entries as *mut psx_gpu::ot::OrderingTable<OT_N>);
let mut frame = ot.resume_frame();
frame.add_tagged_packet_stream_unchecked(first, end);
}
/// Fill `arena` with a deterministic packet stream and return its end.
/// `mode` selects the slot distribution; every mode keeps bits 16..23 of a tag
/// clear, as the staged format requires.
unsafe fn make_stream(arena: *mut u32, rng: &mut Rng, packets: usize, mode: u32) -> *mut u32 {
let mut p = arena;
let mut asc = 0u32;
for i in 0..packets {
let r = rng.next();
let words = match mode {
7 => 0,
8 => (r >> 8) % 3, // zero-length mixed with tiny
12 => 15,
_ => match r % 16 {
0..=4 => 4,
5..=8 => 5 + (r >> 8) % 2,
9..=12 => 8 + (r >> 8) % 2,
13 => 1 + (r >> 8) % 3,
_ => 10 + (r >> 8) % 6,
},
};
let s = rng.next();
let slot = match mode {
0 => s % OT_N as u32,
1 => {
if s % 5 != 0 {
1000 + (s >> 8) % 64
} else {
(s >> 8) % OT_N as u32
}
}
2 => 0,
3 => OT_N as u32 - 1,
4 => {
if s % 20 == 0 {
0xffff
} else {
(s >> 4) % OT_N as u32
}
}
5 => {
asc = (asc + 1 + (s % 3)) % OT_N as u32;
asc
}
6 => (OT_N as u32 - 1) - (i as u32 % OT_N as u32),
9 => 0xffff,
10 => {
if i == 0 || i + 1 == packets {
0xffff
} else {
(s >> 4) % OT_N as u32
}
}
11 => {
if i % 2 == 0 {
0xffff
} else {
(s >> 4) % OT_N as u32
}
}
12 => 5,
_ => (s >> 4) % OT_N as u32,
};
p.write((words << 24) | slot);
for w in 0..words as usize {
p.add(1 + w).write(rng.next());
}
p = p.add(1 + words as usize);
}
p
}
unsafe fn run_variant(
kernel: unsafe extern "C" fn(*mut u32, *mut u32, *mut u32),
seed: u32,
packets: usize,
mode: u32,
) -> u32 {
let ot_ref = &mut *core::ptr::addr_of_mut!(OT_REF);
let ot_tst = &mut *core::ptr::addr_of_mut!(OT_TST);
let src = core::ptr::addr_of_mut!(ARENA_SRC) as *mut u32;
let arena_ref = core::ptr::addr_of_mut!(ARENA_REF) as *mut u32;
let arena_tst = core::ptr::addr_of_mut!(ARENA_TST) as *mut u32;
let mut rng = Rng(seed | 1);
let end_src = make_stream(src, &mut rng, packets, mode);
let n = end_src.offset_from(src) as usize;
core::ptr::copy_nonoverlapping(src, arena_ref, n);
core::ptr::copy_nonoverlapping(src, arena_tst, n);
// The table holds addresses of its own entries and of packets, so both runs
// must see the same addresses: link the reference into the reference arena
// with the reference table, the candidate into the test arena with the test
// table, and compare after mapping addresses to offsets.
ot_clear(ot_ref);
ot_clear(ot_tst);
link_ref(ot_ref.0.as_mut_ptr(), arena_ref, arena_ref.add(n));
kernel(arena_tst, arena_tst.add(n), ot_tst.0.as_mut_ptr());
let mut bad = 0u32;
// Entries: packet addresses are offsets from each arena's base.
let base_ref = arena_ref as u32 & MASK;
let base_tst = arena_tst as u32 & MASK;
let tbl_ref = ot_ref.0.as_ptr() as u32 & MASK;
let tbl_tst = ot_tst.0.as_ptr() as u32 & MASK;
for i in 0..OT_N {
let a = ot_ref.0[i];
let b = ot_tst.0[i];
let ok = if a >= base_ref && a < base_ref + (n as u32) * 4 {
b == a - base_ref + base_tst
} else if a >= tbl_ref && a < tbl_ref + (OT_N as u32) * 4 {
b == a - tbl_ref + tbl_tst
} else {
a == b
};
if !ok {
bad += 1;
}
}
for i in 0..n {
let a = *arena_ref.add(i);
let b = *arena_tst.add(i);
// Link words differ by the base; compare the count byte exactly and the
// address as an offset. Data words are bit-identical.
let is_tag = a & 0x00ff_0000 == 0 && false;
let _ = is_tag;
if a != b {
// A tag word holds an address: map it.
let (ra, rb) = (a & MASK, b & MASK);
let ok = (a >> 24) == (b >> 24)
&& ((ra >= base_ref
&& ra < base_ref + (n as u32) * 4
&& rb == ra - base_ref + base_tst)
|| (ra >= tbl_ref
&& ra < tbl_ref + (OT_N as u32) * 4
&& rb == ra - tbl_ref + tbl_tst)
|| ra == rb);
if !ok {
bad += 1;
}
}
}
bad
}
pub fn run(cases: &mut u32, failures: &mut u32) {
// Self-check: the harness must reject a wrong kernel on the sentinel mode.
let caught = unsafe { run_variant(mutant_link, 4242, 500, 4) };
*cases += 1;
if caught == 0 {
*failures += 1;
psx_rt::tty::println(" harness failed to catch the mutant");
}
let kernels: [(&str, unsafe extern "C" fn(*mut u32, *mut u32, *mut u32)); 2] =
[("cur", asmprobe_link_cur), ("sdk", sdk_link)];
for (name, k) in kernels.iter() {
let mut fails = 0u32;
psx_rt::tty::print("testing ");
psx_rt::tty::println(name);
for mode in 0..13u32 {
for &packets in &[0usize, 1, 2, 3, 7, 64, 500, 3000] {
for seed in 1..6u32 {
*cases += 1;
let bad = unsafe { run_variant(*k, seed * 7919 + mode, packets, mode) };
if bad != 0 {
fails += 1;
*failures += 1;
if fails < 4 {
crate::tty_fail(name, mode, packets as u32, bad);
}
}
}
}
}
}
}
/// Time `kernel` over `reps` fresh streams of `packets` packets; returns
/// (total cycles, total packets). The empty-timer overhead is removed.
unsafe fn time_kernel(
kernel: unsafe extern "C" fn(*mut u32, *mut u32, *mut u32),
mode: u32,
packets: usize,
reps: u32,
) -> (u32, u32) {
let ot_tst = &mut *core::ptr::addr_of_mut!(OT_TST);
let src = core::ptr::addr_of_mut!(ARENA_SRC) as *mut u32;
let arena_tst = core::ptr::addr_of_mut!(ARENA_TST) as *mut u32;
let mut rng = Rng(12345 + mode);
let end_src = make_stream(src, &mut rng, packets, mode);
let n = end_src.offset_from(src) as usize;
let mut total = 0u32;
let mut overhead = 0u32;
for _ in 0..reps {
core::ptr::copy_nonoverlapping(src, arena_tst, n);
ot_clear(ot_tst);
let a = crate::clock();
let b = crate::clock();
overhead += b.wrapping_sub(a) as u32;
let a = crate::clock();
black_box(kernel)(arena_tst, arena_tst.add(n), ot_tst.0.as_mut_ptr());
let b = crate::clock();
total += b.wrapping_sub(a) as u32;
}
(total.saturating_sub(overhead), packets as u32 * reps)
}
pub fn bench() {
let kernels: [(&str, unsafe extern "C" fn(*mut u32, *mut u32, *mut u32)); 2] =
[("cur", asmprobe_link_cur), ("sdk", sdk_link)];
for mode in [0u32, 1, 4] {
for (name, k) in kernels.iter() {
let (cycles, packets) = unsafe { time_kernel(*k, mode, 300, 16) };
crate::tty_label(name, mode);
// hundredths of a cycle per packet
print_dec("", cycles * 100 / packets);
}
}
}
src/main.rs · 121 lines
// SPDX-License-Identifier: GPL-2.0-or-later
//! `hello-asmprobe` -- differential probe and micro-benchmark for the SDK's
//! hand-scheduled kernels.
//!
//! Every kernel is compared bit for bit against a portable Rust reference on
//! random and hand-picked edge inputs, then timed on a fixed workload. The
//! verdict goes to the TTY (`ASMPROBE PASS` / `ASMPROBE FAIL`). Per-function
//! cycles come from the emulator's attribution logs over the link map.
#![no_std]
#![no_main]
#![feature(asm_experimental_arch)]
extern crate psx_rt;
mod clip;
mod link;
use psx_rt::tty;
/// xorshift32, the same stream on every run.
pub struct Rng(pub u32);
impl Rng {
pub fn next(&mut self) -> u32 {
let mut x = self.0;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
self.0 = x;
x
}
}
pub fn tty_fail(name: &str, mode: u32, packets: u32, bad: u32) {
tty::print(" FAIL ");
tty::print(name);
tty::print(" mode=");
tty::print_hex_u32(mode);
tty::print(" packets=");
tty::print_hex_u32(packets);
tty::print(" bad=");
tty::print_hex_u32(bad);
tty::println("");
}
/// The system clock as a 16-bit counter: root counter 1 on the system clock.
pub fn clock_start() {
psx_io::timers::set_mode(psx_io::timers::Timer::Timer1, 0);
}
#[inline(always)]
pub fn clock() -> u16 {
psx_io::timers::counter(psx_io::timers::Timer::Timer1)
}
pub fn print_dec(label: &str, v: u32) {
tty::print(label);
let mut buf = [0u8; 10];
let mut n = 0;
let mut x = v;
if x == 0 {
buf[0] = b'0';
n = 1;
}
while x > 0 {
buf[n] = b'0' + (x % 10) as u8;
x /= 10;
n += 1;
}
let mut out = [0u8; 10];
for i in 0..n {
out[i] = buf[n - 1 - i];
}
tty::print(core::str::from_utf8(&out[..n]).unwrap());
tty::println("");
}
pub fn tty_label_clip(name: &str) {
tty::print("BENCH clip ");
tty::print(name);
tty::print(" cyc/10call=");
}
pub fn tty_label(name: &str, mode: u32) {
tty::print("BENCH link ");
tty::print(name);
tty::print(" mode=");
tty::print_hex_u32(mode);
tty::print(" cyc/100pkt=");
}
pub fn print_u32(label: &str, v: u32) {
tty::print(label);
tty::print_hex_u32(v);
tty::println("");
}
#[no_mangle]
fn main() {
clock_start();
let mut failures = 0u32;
let mut cases = 0u32;
clip::run(&mut cases, &mut failures);
clip::bench();
link::run(&mut cases, &mut failures);
if failures == 0 {
tty::print("ASMPROBE PASS cases=");
tty::print_hex_u32(cases);
tty::println("");
} else {
tty::print("ASMPROBE FAIL ");
tty::print_hex_u32(failures);
tty::print(" of ");
tty::print_hex_u32(cases);
tty::println("");
}
link::bench();
tty::println("ASMPROBE DONE");
loop {}
}