1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#![allow(stable_features)]
#![feature(
alloc_error_handler,
llvm_asm,
global_asm,
untagged_unions,
core_intrinsics,
const_loop,
const_if_match,
const_generics,
c_variadic,
lang_items,
)]
#![feature(std_internals, panic_info_message, panic_internals, unwind_attributes)]
#![cfg_attr(not(feature = "stub-only"), feature(panic_unwind))]
#![allow(incomplete_features)]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use] extern crate paste;
#[cfg(not(feature = "stub-only"))] extern crate alloc;
#[cfg(not(feature = "stub-only"))] extern crate panic_unwind;
#[macro_use]
#[doc(hidden)]
#[cfg(not(feature = "stub-only"))]
pub mod debug;
#[macro_use] mod vfpu;
mod eabi;
pub mod math;
pub mod sys;
#[cfg(not(feature = "stub-only"))] pub mod test_runner;
#[cfg(not(feature = "stub-only"))] pub mod vram_alloc;
#[cfg(not(feature = "stub-only"))] mod alloc_impl;
#[cfg(not(feature = "stub-only"))] pub mod panic;
#[cfg(not(feature = "stub-only"))] mod screenshot;
#[cfg(not(feature = "stub-only"))] pub use screenshot::*;
#[cfg(not(feature = "stub-only"))] mod benchmark;
#[cfg(not(feature = "stub-only"))] pub use benchmark::*;
#[cfg(not(feature = "stub-only"))] mod constants;
#[cfg(not(feature = "stub-only"))] pub use constants::*;
#[cfg(not(feature = "std"))]
#[cfg(feature = "stub-only")]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! { loop {} }
#[cfg(not(feature = "std"))]
#[no_mangle]
extern "C" fn __rust_foreign_exception() -> ! { loop {} }
#[cfg(feature = "std")]
pub use std::panic::catch_unwind;
#[cfg(all(not(feature = "std"), not(feature = "stub-only")))]
pub use panic::catch_unwind;
#[cfg(feature="embedded-graphics")]
pub mod embedded_graphics;
#[repr(align(16))]
#[derive(Copy, Clone)]
pub struct Align16<T>(pub T);
#[cfg(all(target_os = "psp", not(feature = "stub-only")))]
global_asm!(
r#"
.section .lib.ent.top, "a", @progbits
.align 2
.word 0
.global __lib_ent_top
__lib_ent_top:
.section .lib.ent.btm, "a", @progbits
.align 2
.global __lib_ent_bottom
__lib_ent_bottom:
.word 0
.section .lib.stub.top, "a", @progbits
.align 2
.word 0
.global __lib_stub_top
__lib_stub_top:
.section .lib.stub.btm, "a", @progbits
.align 2
.global __lib_stub_bottom
__lib_stub_bottom:
.word 0
"#
);
#[macro_export]
macro_rules! module {
($name:expr, $version_major:expr, $version_minor: expr) => {
#[doc(hidden)]
mod __psp_module {
#[no_mangle]
#[link_section = ".rodata.sceModuleInfo"]
#[used]
static MODULE_INFO: $crate::Align16<$crate::sys::SceModuleInfo> = $crate::Align16(
$crate::sys::SceModuleInfo {
mod_attribute: 0,
mod_version: [$version_major, $version_minor],
mod_name: $crate::sys::SceModuleInfo::name($name),
terminal: 0,
gp_value: unsafe { &_gp },
stub_top: unsafe { &__lib_stub_top },
stub_end: unsafe { &__lib_stub_bottom },
ent_top: unsafe { &__lib_ent_top },
ent_end: unsafe { &__lib_ent_bottom },
}
);
extern {
static _gp: u8;
static __lib_ent_bottom: u8;
static __lib_ent_top: u8;
static __lib_stub_bottom: u8;
static __lib_stub_top: u8;
}
#[no_mangle]
#[link_section = ".lib.ent"]
#[used]
static LIB_ENT: $crate::sys::SceLibraryEntry = $crate::sys::SceLibraryEntry {
name: core::ptr::null(),
version: ($version_major, $version_minor),
attribute: $crate::sys::SceLibAttr::SCE_LIB_IS_SYSLIB,
entry_len: 4,
var_count: 1,
func_count: 1,
entry_table: &LIB_ENT_TABLE,
};
#[no_mangle]
#[link_section = ".rodata.sceResident"]
#[used]
static LIB_ENT_TABLE: $crate::sys::SceLibraryEntryTable = $crate::sys::SceLibraryEntryTable {
module_start_nid: 0xd632acdb,
module_info_nid: 0xf01d73a7,
module_start: module_start,
module_info: &MODULE_INFO.0,
};
#[no_mangle]
extern "C" fn module_start(_argc: isize, _argv: *const *const u8) -> isize {
use $crate::sys::ThreadAttributes;
use core::ffi::c_void;
unsafe {
extern fn main_thread(_argc: usize, _argv: *mut c_void) -> i32 {
let _ = $crate::catch_unwind(|| {
super::psp_main();
});
0
}
let id = $crate::sys::sceKernelCreateThread(
&b"main_thread\0"[0],
main_thread,
32,
256 * 1024,
ThreadAttributes::USER,
core::ptr::null_mut(),
);
$crate::sys::sceKernelStartThread(id, 0, core::ptr::null_mut());
}
0
}
}
}
}
pub fn enable_home_button() {
use core::{ptr, ffi::c_void};
use sys::ThreadAttributes;
unsafe {
unsafe extern fn exit_thread(_args: usize, _argp: *mut c_void) -> i32 {
unsafe extern fn exit_callback(_arg1: i32, _arg2: i32, _arg: *mut c_void) -> i32 {
sys::sceKernelExitGame();
0
}
let id = sys::sceKernelCreateCallback(
&b"exit_callback\0"[0],
exit_callback,
ptr::null_mut(),
);
sys::sceKernelRegisterExitCallback(id);
sys::sceKernelSleepThreadCB();
0
}
let id = sys::sceKernelCreateThread(
&b"exit_thread\0"[0],
exit_thread,
32,
0x1000,
ThreadAttributes::empty(),
ptr::null_mut(),
);
sys::sceKernelStartThread(id, 0, ptr::null_mut());
}
}