pub type RomFnTableCode = [u8; 2];
type RomTableLookupFn<T> = unsafe extern "C" fn(*const u16, u32) -> T;
const ROM_TABLE_LOOKUP_PTR: *const u16 = 0x18 as _;
const FUNC_TABLE: *const u16 = 0x14 as _;
const DATA_TABLE: *const u16 = 0x16 as _;
fn rom_table_lookup<T>(table: *const u16, tag: RomFnTableCode) -> T {
unsafe {
let rom_table_lookup_ptr: *const u32 = rom_hword_as_ptr(ROM_TABLE_LOOKUP_PTR);
let rom_table_lookup: RomTableLookupFn<T> = core::mem::transmute(rom_table_lookup_ptr);
rom_table_lookup(rom_hword_as_ptr(table) as *const u16, u16::from_le_bytes(tag) as u32)
}
}
unsafe fn rom_hword_as_ptr(rom_address: *const u16) -> *const u32 {
let ptr: u16 = *rom_address;
ptr as *const u32
}
macro_rules! rom_funcs {
(
$(
$(#[$outer:meta])*
$c:literal $name:ident (
$( $aname:ident : $aty:ty ),*
) -> $ret:ty ;
)*
) => {
$(
$(#[$outer])*
pub fn $name($( $aname:$aty ),*) -> $ret{
let func: extern "C" fn( $( $aty ),* ) -> $ret = rom_table_lookup(FUNC_TABLE, *$c);
func($( $aname ),*)
}
)*
}
}
rom_funcs! {
b"P3" popcount32(value: u32) -> u32;
b"R3" reverse32(value: u32) -> u32;
b"L3" clz32(value: u32) -> u32;
b"T3" ctz32(value: u32) -> u32;
b"MS" memset(ptr: *mut u8, c: u8, n: u8) -> *mut u8;
b"M4" memset4(ptr: *mut u32, c: u8, n: u32) -> *mut u32;
b"MC" memcpy(dest: *mut u8, src: *mut u8, n: u32) -> u8;
b"C4" memcpy44(dest: *mut u32, src: *mut u32, n: u32) -> *mut u8;
b"IF" connect_internal_flash() -> ();
b"EX" flash_exit_xip() -> ();
b"RE" flash_range_erase(addr: u32, count: usize, block_size: u32, block_cmd: u8) -> ();
b"RP" flash_range_program(addr: u32, data: *const u8, count: usize) -> ();
b"FC" flash_flush_cache() -> ();
b"CX" flash_enter_cmd_xip() -> ();
b"UB" reset_to_usb_boot(gpio_activity_pin_mask: u32, disable_interface_mask: u32) -> ();
b"WV" wait_for_vector() -> !;
}
unsafe fn convert_str(s: *const u8) -> &'static str {
let mut end = s;
while *end != 0 {
end = end.add(1);
}
let s = core::slice::from_raw_parts(s, end.offset_from(s) as usize);
core::str::from_utf8_unchecked(s)
}
pub fn copyright_string() -> &'static str {
let s: *const u8 = rom_table_lookup(DATA_TABLE, *b"CR");
unsafe { convert_str(s) }
}
pub fn git_revision() -> &'static str {
let s: *const u8 = rom_table_lookup(DATA_TABLE, *b"GR");
unsafe { convert_str(s) }
}
pub fn fplib_start() -> *const u8 {
rom_table_lookup(DATA_TABLE, *b"FS")
}
pub fn soft_float_table() -> *const u16 {
rom_table_lookup(DATA_TABLE, *b"SF")
}
pub fn fplib_end() -> *const u8 {
rom_table_lookup(DATA_TABLE, *b"FE")
}
pub fn soft_double_table() -> *const u16 {
rom_table_lookup(DATA_TABLE, *b"SD")
}
macro_rules! float_funcs {
(
$(
$(#[$outer:meta])*
$offset:literal $name:ident (
$( $aname:ident : $aty:ty ),*
) -> $ret:ty;
)*
) => {
$(
$(#[$outer])*
pub fn $name() -> extern "C" fn( $( $aname : $aty ),* ) -> $ret {
let table: *const *const u16 = rom_table_lookup(DATA_TABLE, *b"SF");
unsafe {
core::mem::transmute_copy(&table.add($offset))
}
}
)*
}
}
float_funcs! {
0x00 fadd(a: f32, b: f32) -> f32;
0x04 fsub(a: f32, b: f32) -> f32;
0x08 fmul(a: f32, b: f32) -> f32;
0x0c fdiv(a: f32, b: f32) -> f32;
0x18 fsqrt(v: f32) -> f32;
0x1c float_to_int(v: f32) -> i32;
0x20 float_to_fix(v: f32, n: i32) -> i32;
0x24 float_to_uint(v: f32) -> u32;
0x28 float_to_ufix(v: f32, n: i32) -> u32;
0x2c int_to_float(v: i32) -> f32;
0x30 fix_to_float(v: i32, n: i32) -> f32;
0x34 uint_to_float(v: u32) -> f32;
0x38 ufix_to_float(v: u32, n: i32) -> f32;
0x3c fcos(angle: f32) -> f32;
0x40 fsin(angle: f32) -> f32;
0x44 ftan(angle: f32) -> f32;
0x4c fexp(v: f32) -> f32;
0x50 fln(v: f32) -> f32;
0x54 fcmp(a: f32, b: f32) -> i32;
0x58 fatan2(y: f32, x: f32) -> f32;
0x5c int64_to_float(v: i64) -> f32;
0x60 fix64_to_float(v: i64, n: i32) -> f32;
0x64 uint64_to_float(v: u64) -> f32;
0x68 ufix64_to_float(v: u64, n: i32) -> f32;
0x6c float_to_int64(v: f32) -> i64;
0x70 float_to_fix64(v: f32, n: i32) -> f32;
0x74 float_to_uint64(v: f32) -> u64;
0x7c float_to_double(v: f32) -> f64;
}
macro_rules! double_funcs {
(
$(
$(#[$outer:meta])*
$offset:literal $name:ident (
$( $aname:ident : $aty:ty ),*
) -> $ret:ty;
)*
) => {
$(
$(#[$outer])*
pub fn $name() -> extern "C" fn( $( $aname : $aty ),* ) -> $ret {
let table: *const *const u16 = rom_table_lookup(DATA_TABLE, *b"SD");
unsafe {
core::mem::transmute_copy(&table.add($offset))
}
}
)*
}
}
double_funcs! {
0x00 dadd(a: f64, b: f64) -> f64;
0x04 dsub(a: f64, b: f64) -> f64;
0x08 dmul(a: f64, b: f64) -> f64;
0x0c ddiv(a: f64, b: f64) -> f64;
0x18 dsqrt(v: f64) -> f64;
0x1c double_to_int(v: f64) -> i32;
0x20 double_to_fix(v: f64, n: i32) -> i32;
0x28 double_to_ufix(v: f64, n: i32) -> u32;
0x2c int_to_double(v: i32) -> f64;
0x30 fix_to_double(v: i32, n: i32) -> f64;
0x34 uint_to_double(v: u32) -> f64;
0x38 ufix_to_double(v: u32, n: i32) -> f64;
0x3c dcos(angle: f64) -> f64;
0x40 dsin(angle: f64) -> f64;
0x44 dtan(angle: f64) -> f64;
0x4c dexp(v: f64) -> f64;
0x50 dln(v: f64) -> f64;
0x54 dcmp(a: f64, b: f64) -> i32;
0x58 datan2(y: f64, x: f64) -> f64;
0x5c int64_to_double(v: i64) -> f64;
0x60 fix64_to_doubl(v: i64, n: i32) -> f64;
0x64 uint64_to_double(v: u64) -> f64;
0x68 ufix64_to_double(v: u64, n: i32) -> f64;
0x6c double_to_int64(v: f64) -> i64;
0x70 double_to_fix64(v: f64, n: i32) -> i64;
0x74 double_to_uint64(v: f64) -> u64;
0x78 double_to_ufix64(v: f64, n: i32) -> u64;
0x7c double_to_float(v: f64) -> f32;
}