SDK example · full source

hello-fault

A deliberate panic for the abort gate

On this page

What this program does

Panic on purpose, so the panic gate has something to check. The example is built with -Cpanic=immediate-abort, the SDK's shared policy, so the out-of-range index in its source becomes a bare break at the faulting instruction. The runtime's exception handler records Cause, EPC and BadVAddr and halts, and psoxide-size faults reads them from a RAM dump and names the function around EPC. The program draws nothing.

Build and run

Use the documented SDK checkout and tool setup. Run these commands from the repository root.

make disc EXAMPLE=hello-fault

The check is sh tools/abort_gate.sh /path/to/frontend. It runs this program and fails unless the fault was recorded, runs hello-present and requires zero CPU exceptions, and compares the size of a build with and without panic messages.

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-rt.

Cargo.toml · 19 lines

View this file on GitHub

[package]
name    = "hello-fault"
version = "0.1.0"
edition = "2021"
license = "GPL-2.0-or-later"

[workspace]

[[bin]]
name = "hello-fault"
path = "src/main.rs"

[profile.release]
panic = "abort"
codegen-units = 1
lto = true

[dependencies]
psx-rt = { path = "../../crates/psx-rt" }
src/main.rs · 30 lines

View this file on GitHub

//! `hello-fault` -- a guest that panics on purpose, for the panic gate.
//!
//! Built with `-Cpanic=immediate-abort` (the shared policy, see
//! `tools/psoxide-size`), the out-of-range index below is a bare `break` at the
//! faulting instruction. psx-rt's exception handler records Cause, EPC and
//! BadVAddr in `__psx_rt_fault_*` and halts, and `psoxide-size faults` reads
//! them from a `--dump-ram` image and names the function around EPC.
//! `tools/abort_gate.sh` runs this and fails unless the fault was recorded.

#![no_std]
#![no_main]

extern crate psx_rt;

use psx_rt::interrupts;

static TABLE: [u8; 4] = [1, 2, 3, 4];
static mut INDEX: usize = 9;
static mut SINK: u8 = 0;

#[no_mangle]
fn main() {
    // Games install the runtime handler first; it is what records the fault.
    interrupts::install_vblank_counter();
    // A volatile read keeps the optimiser from proving the index out of range.
    let index = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(INDEX)) };
    let value = TABLE[index];
    unsafe { core::ptr::write_volatile(core::ptr::addr_of_mut!(SINK), value) };
    loop {}
}