Skip to content

Commit

Permalink
std main() support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin authored and overdrivenpotato committed Jul 11, 2022
1 parent ac224b6 commit 9546fa1
Showing 1 changed file with 56 additions and 31 deletions.
87 changes: 56 additions & 31 deletions psp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// For unwinding support
#![feature(std_internals, panic_info_message, panic_internals, c_unwind)]
#![cfg_attr(not(feature = "stub-only"), feature(panic_unwind))]
#![cfg_attr(feature = "std", feature(psp_std))]
// For the `const_generics` feature.
#![allow(incomplete_features)]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down Expand Up @@ -115,6 +116,58 @@ core::arch::global_asm!(
"#
);

#[cfg(feature = "std")]
extern "C" {
#[link_name = "main"]
#[doc(hidden)]
pub fn c_main(argc: isize, argv: *const *const u8) -> isize;
}

// Because code generated by `module!()` lives in user's crate, cfg attribute cannot be used there.
// Hence this macro.
#[cfg(feature = "std")]
#[doc(hidden)]
#[macro_export]
macro_rules! _start {
($_:expr, $argc:expr, $argv:expr) => {
unsafe { $crate::c_main($argc as _, $argv as _) as _ }
};
}
#[cfg(not(feature = "std"))]
#[doc(hidden)]
#[macro_export]
macro_rules! _start {
($psp_main:expr, $argc:expr, $argv:expr) => {{
unsafe fn init_cwd(arg0: *mut u8) {
let mut len = 0;
while *arg0.add(len) != 0 {
len += 1;
}

// Truncate until last '/'
while len > 0 && *arg0.add(len - 1) != b'/' {
len -= 1;
}

if len > 0 {
let tmp = *arg0.add(len);
*arg0.add(len) = 0;
$crate::sys::sceIoChdir(arg0 as *const u8);
*arg0.add(len) = tmp;
}
}

if $argc > 0 {
unsafe { init_cwd($argv as *mut u8) };
}

// TODO: Maybe print any error to debug screen?
let _ = $crate::catch_unwind($psp_main);

0
}};
}

/// Declare a PSP module.
///
/// You must also define a `fn psp_main() { ... }` function in conjunction with
Expand Down Expand Up @@ -177,36 +230,8 @@ macro_rules! module {

#[no_mangle]
extern "C" fn module_start(argc_bytes: usize, argv: *mut c_void) -> isize {
use $crate::sys::ThreadAttributes;

unsafe fn init_cwd(arg0: *mut u8) {
let mut len = 0;
while *arg0.add(len) != 0 {
len += 1;
}

// Truncate until last '/'
while len > 0 && *arg0.add(len - 1) != b'/' {
len -= 1;
}

if len > 0 {
let tmp = *arg0.add(len);
*arg0.add(len) = 0;
$crate::sys::sceIoChdir(arg0 as *const u8);
*arg0.add(len) = tmp;
}
}

extern "C" fn main_thread(argc_bytes: usize, argv: *mut c_void) -> i32 {
if argc_bytes > 0 {
unsafe { init_cwd(argv as *mut u8) };
}

// TODO: Maybe print any error to debug screen?
let _ = $crate::catch_unwind(super::psp_main);

0
extern "C" fn main_thread(argc: usize, argv: *mut c_void) -> i32 {
$crate::_start!(super::psp_main, argc, argv)
}

unsafe {
Expand All @@ -217,7 +242,7 @@ macro_rules! module {
32,
// 256kb stack
256 * 1024,
ThreadAttributes::USER | ThreadAttributes::VFPU,
$crate::sys::ThreadAttributes::USER | $crate::sys::ThreadAttributes::VFPU,
core::ptr::null_mut(),
);

Expand Down

0 comments on commit 9546fa1

Please sign in to comment.