pspsdk 0.0.2

A SDK for creating PSP modules, including both PRX plugins and regular homebrew apps.
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
//! The low-level implementations, type definition, and system stubs for the PSP system.
use core::{marker::PhantomData, ops::ControlFlow};

use bitflag_attr::bitflag;

#[doc(hidden)]
#[cfg(target_os = "psp")]
pub mod macro_helpers;


mod error;
pub use error::{ErrorFacility, SceError};

pub mod audio;
pub mod ctrl;
pub mod display;
pub mod dma;
pub mod ge;
pub mod hprm;
pub mod io;
pub mod libc;
pub mod library;
pub mod loadexec;
pub mod mem;
pub mod module;
pub mod openpsid;
pub mod power;
pub mod suspend;
pub mod thread;
pub mod time;
pub mod usersystemlib;

#[cfg(feature = "non-stub-code")]
pub mod sync;

/// A `usize`-like with the guarantee to be the correct size on PSP.
pub type SceSize = cfg_select! {
    target_os = "psp" => usize,
    target_pointer_width = "32" => usize,
    _ => u32,
};

/// A `isize`-like with the guarantee to be the correct size on PSP.
pub type SceIsize = cfg_select! {
    target_os = "psp" => isize,
    target_pointer_width = "32" => isize,
    _ => i32,
};

/// Identification number for several kernel objects.
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct SceUid(pattern_type!(SceRawUid is 0..=0x7FFFFFFF));

/// Identification number for several kernel objects.
pub type SceRawUid = u32;

impl SceUid {
    /// Create a new SceUid structure from a raw value.
    ///
    /// This functions checks for the value of `raw` to be a in the range of possible SceUid
    /// values used by the PSP OS, returning an [`None`] otherwise.
    pub const fn from_raw(raw: u32) -> Option<Self> {
        if let 0..=0x7FFFFFFF = raw {
            Some(unsafe { Self::from_raw_unchecked(raw) })
        } else {
            None
        }
    }

    /// Create a new UID structure from a raw value without checking value range.
    ///
    /// # Safety
    ///
    /// Immediate language UB if `val` is not within the valid range for this
    /// type, as it violates the validity invariant.
    #[inline]
    pub const unsafe fn from_raw_unchecked(raw: u32) -> Self {
        // SAFETY: Caller promised that `val` is within the valid range.
        unsafe { core::mem::transmute(raw) }
    }

    #[inline]
    pub const fn to_inner(self) -> u32 {
        // SAFETY: pattern types are always legal values of their base type
        // (Not using `.0` because that has perf regressions.)
        unsafe { core::mem::transmute(self) }
    }
}

crate::impl_ranged_ty!(SceUid);

impl crate::private::Sealed for SceUid {}

impl Default for SceUid {
    fn default() -> Self {
        unsafe { Self::from_raw_unchecked(0) }
    }
}

impl core::fmt::Debug for SceUid {
    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        f.debug_tuple("SceUid").field(&self.to_inner()).finish()
    }
}

/// A type that represents the return value of many PSP OS APIs.
///
/// If its value is in the range of [`SceError`] ( 0x80000001..=0xFFFFFFFF), then it is an error
/// result, and a success value otherwise.
///
/// # Cloning behavior
///
/// Cloning this type is cheap, the reason it is not [`Copy`] is to incentivize the handling of the
/// error values.
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[must_use = "this `SceResult` may be an error value, which should be handled"]
pub struct SceResult<T>(u32, PhantomData<T>);

impl<T> SceResult<T> {
    /// Create a new SceResult.
    pub const fn new(raw: u32) -> Self {
        SceResult(raw, PhantomData)
    }

    /// Returns `true` if the result is a Ok value.
    #[inline]
    pub const fn is_ok(&self) -> bool {
        matches!(self.as_inner(), 0..=0x7FFFFFFF)
    }

    /// Returns `true` if the result is a error value.
    #[inline]
    pub const fn is_err(&self) -> bool {
        matches!(self.as_inner(), 0x80000001..=0xFFFFFFFF)
    }

    /// Get the internal value of the result.
    #[inline]
    pub(crate) const fn as_inner(&self) -> u32 {
        self.0
    }
}

impl<T: SceResultOk> SceResult<T> {
    /// Turn the SceResult into a [`Result`] type.
    pub fn into_result(self) -> Result<T, SceError> {
        match self.as_inner() {
            0..=0x7FFFFFFF => unsafe { T::handle_ok_value(self.as_inner()) },
            0x80000001..=0xFFFFFFFF => {
                Err(unsafe { SceError::from_raw_unchecked(self.as_inner()) })
            },
            0x80000000 => Err(SceError::INVALID_VALUE),
        }
    }

    /// Converts from `SceResult` to [`Option<T>`].
    ///
    /// Converts `self` into an [`Option<T>`], consuming `self`,
    /// and discarding the error, if any.
    pub fn ok(self) -> Option<T> {
        self.into_result().ok()
    }

    /// Converts from `SceResult<T>` to [`Option<SceError>`].
    ///
    /// Converts `self` into an [`Option<SceError>`], consuming `self`,
    /// and discarding the success value, if any.
    pub fn err(self) -> Option<SceError> {
        self.into_result().err()
    }

    // Maps a `SceResult<T>` to `Result<U, E>` by applying a function to a
    /// contained [`Ok`] value, leaving an [`Err`] value untouched.
    ///
    /// This function can be used to compose the results of two functions.
    pub fn map<U, F>(self, op: F) -> Result<U, SceError>
    where
        F: FnOnce(T) -> U,
    {
        self.into_result().map(op)
    }

    /// Returns the provided default (if `Err`), or
    /// applies a function to the contained value (if `Ok`).
    ///
    /// Arguments passed to `map_or` are eagerly evaluated; if you are passing
    /// the result of a function call, it is recommended to use [`map_or_else`],
    /// which is lazily evaluated.
    ///
    /// [`map_or_else`]: SceResult::map_or_else
    pub fn map_or<U, F>(self, default: U, f: F) -> U
    where
        F: FnOnce(T) -> U,
    {
        self.into_result().map_or(default, f)
    }

    /// Maps a `SceResult<T>` to `U` by applying fallback function `default` to
    /// a contained `Err` value, or function `f` to a contained `Ok` value.
    ///
    /// This function can be used to unpack a successful result
    /// while handling an error.
    pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
    where
        D: FnOnce(SceError) -> U,
        F: FnOnce(T) -> U,
    {
        self.into_result().map_or_else(default, f)
    }

    /// Maps a `SceResult<T>` to a `U` by applying function `f` to the contained
    /// value if the result is `Ok`], otherwise if `Err`, returns the
    /// [default value] for the type `U`.
    ///
    /// [default value]: Default::default
    pub fn map_or_default<U, F>(self, f: F) -> U
    where
        F: FnOnce(T) -> U,
        U: Default,
    {
        match self.into_result() {
            Ok(t) => f(t),
            Err(_) => U::default(),
        }
    }

    /// Maps a `ScwResult<T>` to `Result<T, F>` by applying a function to a
    /// contained `Err` value, leaving an `Ok` value untouched.
    ///
    /// This function can be used to pass through a successful result while handling
    /// an error.
    pub fn map_err<F, O>(self, op: O) -> Result<T, F>
    where
        O: FnOnce(SceError) -> F,
    {
        self.into_result().map_err(op)
    }

    /// Calls a function with a reference to the contained value if `Ok` value.
    ///
    /// Returns the original result.
    pub fn inspect<F>(self, f: F) -> Self
    where
        F: FnOnce(&T),
    {
        if self.is_ok() {
            // SAFETY: We know it is a Ok value
            let res = unsafe { T::handle_ok_value(self.as_inner()) };
            if let Ok(inner) = res {
                f(&inner)
            }
        }

        self
    }

    /// Calls a function with a reference to the contained value if `Err` value.
    ///
    /// Returns the original result.
    pub fn inspect_err<F>(self, f: F) -> Self
    where
        F: FnOnce(SceError),
    {
        if self.is_err() {
            // SAFETY: we know it is an error
            f(unsafe { SceError::from_raw_unchecked(self.as_inner()) })
        }

        self
    }
}

/// A type that represents the return value of some PSP OS APIs.
///
/// If its value is in the range of [`SceError`] ( 0xFFFFFFFF_80000001..=0xFFFFFFFF_FFFFFFFF), then
/// it is an error result, and a success value otherwise.
///
/// # Cloning behavior
///
/// Cloning this type is cheap, the reason it is not [`Copy`] is to incentivize the handling of the
/// error values.
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[must_use = "this `SceResult` may be an error value, which should be handled"]
pub struct SceResult64<T>(u64, PhantomData<T>);

impl<T> SceResult64<T> {
    /// Create a new SceResult.
    pub const fn new(raw: u64) -> Self {
        SceResult64(raw, PhantomData)
    }

    /// Returns `true` if the result is a Ok value.
    #[inline]
    pub const fn is_ok(&self) -> bool {
        matches!(self.as_inner(), 0..=0xFFFFFFFF_7FFFFFFF)
    }

    /// Returns `true` if the result is a error value.
    #[inline]
    pub const fn is_err(&self) -> bool {
        matches!(self.as_inner(), 0xFFFFFFFF_80000001..=0xFFFFFFFF_FFFFFFFF)
    }

    /// Get the internal value of the result.
    #[inline]
    pub(crate) const fn as_inner(&self) -> u64 {
        self.0
    }
}

impl<T: SceResultOk> SceResult64<T> {
    /// Turn the SceResult64 into a [`Result`] type.
    pub fn into_result(self) -> Result<T, SceError> {
        match self.as_inner() {
            0..=0xFFFFFFFF_7FFFFFFF => unsafe { T::handle_ok_value64(self.as_inner()) },
            0xFFFFFFFF_80000001..=0xFFFFFFFF_FFFFFFFF => {
                // Only lower bits
                let err = (self.as_inner() & 0xFFFFFFFF_00000000) as u32;
                Err(unsafe { SceError::from_raw_unchecked(err) })
            },
            0xFFFFFFFF_80000000 => Err(SceError::INVALID_VALUE),
        }
    }

    /// Converts from `SceResult` to [`Option<T>`].
    ///
    /// Converts `self` into an [`Option<T>`], consuming `self`,
    /// and discarding the error, if any.
    pub fn ok(self) -> Option<T> {
        self.into_result().ok()
    }

    /// Converts from `SceResult<T>` to [`Option<SceError>`].
    ///
    /// Converts `self` into an [`Option<SceError>`], consuming `self`,
    /// and discarding the success value, if any.
    pub fn err(self) -> Option<SceError> {
        self.into_result().err()
    }

    // Maps a `SceResult<T>` to `Result<U, E>` by applying a function to a
    /// contained [`Ok`] value, leaving an [`Err`] value untouched.
    ///
    /// This function can be used to compose the results of two functions.
    pub fn map<U, F>(self, op: F) -> Result<U, SceError>
    where
        F: FnOnce(T) -> U,
    {
        self.into_result().map(op)
    }

    /// Returns the provided default (if `Err`), or
    /// applies a function to the contained value (if `Ok`).
    ///
    /// Arguments passed to `map_or` are eagerly evaluated; if you are passing
    /// the result of a function call, it is recommended to use [`map_or_else`],
    /// which is lazily evaluated.
    ///
    /// [`map_or_else`]: SceResult::map_or_else
    pub fn map_or<U, F>(self, default: U, f: F) -> U
    where
        F: FnOnce(T) -> U,
    {
        self.into_result().map_or(default, f)
    }

    /// Maps a `SceResult<T>` to `U` by applying fallback function `default` to
    /// a contained `Err` value, or function `f` to a contained `Ok` value.
    ///
    /// This function can be used to unpack a successful result
    /// while handling an error.
    pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
    where
        D: FnOnce(SceError) -> U,
        F: FnOnce(T) -> U,
    {
        self.into_result().map_or_else(default, f)
    }

    /// Maps a `SceResult<T>` to a `U` by applying function `f` to the contained
    /// value if the result is `Ok`], otherwise if `Err`, returns the
    /// [default value] for the type `U`.
    ///
    /// [default value]: Default::default
    pub fn map_or_default<U, F>(self, f: F) -> U
    where
        F: FnOnce(T) -> U,
        U: Default,
    {
        match self.into_result() {
            Ok(t) => f(t),
            Err(_) => U::default(),
        }
    }

    /// Maps a `ScwResult<T>` to `Result<T, F>` by applying a function to a
    /// contained `Err` value, leaving an `Ok` value untouched.
    ///
    /// This function can be used to pass through a successful result while handling
    /// an error.
    pub fn map_err<F, O>(self, op: O) -> Result<T, F>
    where
        O: FnOnce(SceError) -> F,
    {
        self.into_result().map_err(op)
    }

    /// Calls a function with a reference to the contained value if `Ok` value.
    ///
    /// Returns the original result.
    pub fn inspect<F>(self, f: F) -> Self
    where
        F: FnOnce(&T),
    {
        if self.is_ok() {
            // SAFETY: We know it is a Ok value
            let res = unsafe { T::handle_ok_value64(self.as_inner()) };
            if let Ok(inner) = res {
                f(&inner)
            }
        }

        self
    }

    /// Calls a function with a reference to the contained value if `Err` value.
    ///
    /// Returns the original result.
    pub fn inspect_err<F>(self, f: F) -> Self
    where
        F: FnOnce(SceError),
    {
        if self.is_err() {
            // Only lower bits
            let err = (self.as_inner() & 0xFFFFFFFF_00000000) as u32;
            // SAFETY: we know it is an error
            let err = unsafe { SceError::from_raw_unchecked(err) };
            f(err)
        }

        self
    }
}

impl SceResult<()> {
    /// Everything ok.
    pub const OK: Self = SceResult::new(0);
}

/// Trait of types that can be the ok result of [`SceResult`] or [`SceResult64`].
///
/// # Safety
///
/// For this trait to be correct, the implementation must:
/// - Be valid for the `0..=0x7FFFFFFF` range
/// - Have a max size of 4 bytes
/// - If a struct, be `repr(transparent)`
pub unsafe trait SceResultOk: Sized + crate::private::Sealed {
    /// Turn the Ok value range (`(0,0x7FFFFFFF]`) into a result
    ///
    /// The `ok_value` is always in the ok value range when used by [`SceResult`].
    ///
    /// # Safety
    /// [`SceResult`] methods that use it will ensure to pass only the valid range and the
    /// implementation is free to not check for values outside of that range.
    ///
    /// For callers outside of [`SceResult`], it must ensure to pass a valid `ok_value`.
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError>;

    /// Turn the Ok value range (`(0,0xFFFFFFFF_7FFFFFFF]`) into a result
    ///
    /// The `ok_value` is always in the ok value range when used by [`SceResult64`].
    ///
    /// # Safety
    /// [`SceResult64`] methods that use it will ensure to pass only the valid range and the
    /// implementation is free to not check for values outside of that range.
    ///
    /// For callers outside of [`SceResult64`], it must ensure to pass a valid `ok_value`.
    unsafe fn handle_ok_value64(ok_value: u64) -> Result<Self, SceError> {
        match ok_value {
            0..=0x7FFFFFFF => unsafe { Self::handle_ok_value(ok_value as u32) },
            _ => Err(SceError::INVALID_VALUE),
        }
    }
}

/// Trait that specifies that it can safely be turned to a `SceResult` ok-valid value.
///
/// # Safety
///
/// For this trait to be correct, the implementation must:
/// - Be valid for the `0..=0x7FFFFFFF` range
/// - Have a max size of 4 bytes
/// - If a struct, be `repr(transparent)`
pub unsafe trait SceIntoOkValue: Sized + crate::private::Sealed {
    fn into_ok_value(self) -> u32;
}

/// Trait that specifies that it can safely be turned to a `SceResult64` ok-valid value.
///
/// # Safety
///
/// For this trait to be correct, the implementation must:
/// - Be valid for the `0..=0x7FFFFFFF` range
/// - Have a max size of 4 bytes
/// - If a struct, be `repr(transparent)`
pub unsafe trait SceInto64OkValue: Sized + crate::private::Sealed {
    fn into_ok_value64(self) -> u64;
}

unsafe impl<T: SceIntoOkValue> SceInto64OkValue for T {
    fn into_ok_value64(self) -> u64 {
        self.into_ok_value() as u64
    }
}

macro_rules! __result_ok_int {
    ($($ty:ty),+) => {
        $(
            unsafe impl SceResultOk for $ty {
                unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
                    <$ty>::try_from(ok_value).map_err(|_| SceError::INVALID_VALUE)
                }
            }

            unsafe impl SceIntoOkValue for $ty {
                fn into_ok_value(self) -> u32 {
                    self as u32
                }
            }
        )+
    };
}

__result_ok_int!(i8, u8, i16, u16, bool);


unsafe impl SceResultOk for i32 {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        debug_assert!(ok_value <= 0x7FFFFFFF);
        Ok(u32::cast_signed(ok_value))
    }
}
unsafe impl SceIntoOkValue for i32 {
    fn into_ok_value(self) -> u32 {
        self as u32
    }
}
unsafe impl SceResultOk for u32 {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        debug_assert!(ok_value <= 0x7FFFFFFF);
        Ok(ok_value)
    }
}
unsafe impl SceIntoOkValue for u32 {
    fn into_ok_value(self) -> u32 {
        self
    }
}
unsafe impl SceResultOk for i64 {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        Ok(ok_value as i64)
    }

    unsafe fn handle_ok_value64(ok_value: u64) -> Result<Self, SceError> {
        match ok_value {
            0..=0xFFFFFFFF_7FFFFFFF => Ok(u64::cast_signed(ok_value)),
            _ => Err(SceError::INVALID_VALUE),
        }
    }
}
unsafe impl SceResultOk for u64 {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        Ok(ok_value as u64)
    }

    unsafe fn handle_ok_value64(ok_value: u64) -> Result<Self, SceError> {
        match ok_value {
            0..=0xFFFFFFFF_7FFFFFFF => Ok(ok_value),
            _ => Err(SceError::INVALID_VALUE),
        }
    }
}
unsafe impl SceInto64OkValue for i64 {
    fn into_ok_value64(self) -> u64 {
        self as u64
    }
}
unsafe impl SceInto64OkValue for u64 {
    fn into_ok_value64(self) -> u64 {
        self
    }
}

#[cfg(target_pointer_width = "32")]
unsafe impl SceResultOk for isize {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        debug_assert!(ok_value <= 0x7FFFFFFF);
        Ok(ok_value as isize)
    }
}
#[cfg(target_pointer_width = "32")]
unsafe impl SceIntoOkValue for isize {
    fn into_ok_value(self) -> u32 {
        self as u32
    }
}
#[cfg(target_pointer_width = "32")]
unsafe impl SceResultOk for usize {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        debug_assert!(ok_value <= 0x7FFFFFFF);
        Ok(ok_value as usize)
    }
}
#[cfg(target_pointer_width = "32")]
unsafe impl SceIntoOkValue for usize {
    fn into_ok_value(self) -> u32 {
        self as u32
    }
}
unsafe impl SceResultOk for () {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        match ok_value {
            0x00 => Ok(()),
            _ => Err(SceError::INVALID_VALUE),
        }
    }
}
unsafe impl SceIntoOkValue for () {
    fn into_ok_value(self) -> u32 {
        0
    }
}
unsafe impl SceResultOk for ! {
    unsafe fn handle_ok_value(_ok_value: u32) -> Result<Self, SceError> {
        Err(SceError::INVALID_VALUE)
    }
}
unsafe impl SceIntoOkValue for ! {
    fn into_ok_value(self) -> u32 {
        0
    }
}
unsafe impl SceResultOk for core::convert::Infallible {
    unsafe fn handle_ok_value(_: u32) -> Result<Self, SceError> {
        Err(SceError::INVALID_VALUE)
    }
}
unsafe impl SceResultOk for SceUid {
    unsafe fn handle_ok_value(ok_value: u32) -> Result<Self, SceError> {
        debug_assert!(ok_value <= 0x7FFFFFFF);
        // SAFETY: SceUid is always on the ok value range
        Ok(unsafe { Self::from_raw_unchecked(ok_value) })
    }
}
unsafe impl SceIntoOkValue for SceUid {
    fn into_ok_value(self) -> u32 {
        self.to_inner()
    }
}

impl<T: SceResultOk + SceIntoOkValue> core::ops::Residual<T>
    for SceResult<core::convert::Infallible>
{
    type TryType = SceResult<T>;
}

impl<T: SceResultOk + SceIntoOkValue> core::ops::FromResidual for SceResult<T> {
    fn from_residual(residual: <Self as core::ops::Try>::Residual) -> Self {
        Self::new(residual.as_inner())
    }
}

impl<T: SceResultOk + SceIntoOkValue> core::ops::Try for SceResult<T> {
    type Output = T;
    type Residual = SceResult<core::convert::Infallible>;

    fn from_output(output: Self::Output) -> Self {
        SceResult::new(output.into_ok_value())
    }

    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
        match self.into_result() {
            Ok(v) => ControlFlow::Continue(v),
            Err(err) => ControlFlow::Break(SceResult::new(err.to_inner())),
        }
    }
}

impl<T: SceResultOk + SceInto64OkValue> core::ops::Residual<T>
    for SceResult64<core::convert::Infallible>
{
    type TryType = SceResult64<T>;
}

impl<T: SceResultOk + SceInto64OkValue> core::ops::FromResidual for SceResult64<T> {
    fn from_residual(residual: <Self as core::ops::Try>::Residual) -> Self {
        Self::new(residual.as_inner())
    }
}

impl<T: SceResultOk + SceInto64OkValue> core::ops::Try for SceResult64<T> {
    type Output = T;
    type Residual = SceResult64<core::convert::Infallible>;

    fn from_output(output: Self::Output) -> Self {
        SceResult64::new(output.into_ok_value64())
    }

    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
        match self.into_result() {
            Ok(v) => ControlFlow::Continue(v),
            Err(err) => ControlFlow::Break(SceResult64::new(err.to_inner() as u64)),
        }
    }
}

/// Resident/Stub library attributes.
///
/// Every library needs to have at least one of those attributes.
///
/// Resident libraries can have the members [`AutoExport`](LibFlags::AutoExport),
/// [`WeakExport`](LibFlags::WeakExport), [`NoLinkExport`](LibFlags::NoLinkExport),
/// [`SyscallExport`](LibFlags::SyscallExport) and [`IsSystemLib`](LibFlags::IsSystemLib).
///
/// Stub libraries can have [`NoSpecialFlags`](LibFlags::NoSpecialFlags) or
/// [`WeakImport`](LibFlags::WeakImport).
#[bitflag(u16)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum LibFlags {
    /// The library has no special attributes.
    NoSpecialFlags = 0x0,
    /// Automatically register the library to the system.
    AutoExport = 0x1,
    /// Indicates resident library can be overwritten.
    WeakExport = 0x2,
    /// Indicates resident library is NOT being linked.
    NoLinkExport = 0x4,
    /// Load module that references this library even if this library is not registered.
    WeakImport = 0x8,
    /// Indicates the use of the SYSCALL technique for linking.
    SyscallExport = 0x4000,
    /// The library is a system library (a mandatory library for all modules).
    IsSystemLib = 0x8000,
}

#[macro_export]
#[doc(hidden)]
macro_rules! impl_ranged_ty {
    ($t:ty) => {
        impl ::core::marker::StructuralPartialEq for $t {}

        impl Eq for $t {}

        impl PartialEq for $t {
            #[inline]
            fn eq(&self, other: &Self) -> bool {
                self.to_inner() == other.to_inner()
            }
        }

        impl Ord for $t {
            #[inline]
            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
                Ord::cmp(&self.to_inner(), &other.to_inner())
            }
        }

        impl PartialOrd for $t {
            #[inline]
            fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
                Some(Ord::cmp(self, other))
            }
        }

        impl ::core::hash::Hash for $t {
            // Required method
            fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
                ::core::hash::Hash::hash(&self.to_inner(), state);
            }
        }
    };
}

/// Sets the processor K1 register to a given value.
///
/// This function is for use in kernel mode syscall exports. The kernel sets the `k1` register to
/// indicate what mode called the function, i.e. whether it was directly called, was called via a
/// syscall from a kernel thread or called via a syscall from a user thread. By setting `k1` to `0`
/// before doing anything in your code you can make the other functions think you are calling from a
/// kernel thread and therefore disable numerous protections. But it's usage must be careful, as the
/// some system API also checks for `k1` not being from the kernel to work properly.
///
/// # Safety
///
/// Touching a privileged CPU register is unsafe and changing K1 value can cause issues on system
/// state.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub unsafe extern "C" fn set_k1(k1: u32) -> u32 {
    core::arch::naked_asm!(
        ".set noreorder",
        ".set noat",
        "move $v0, $k1",
        "jr	 $ra",
        "move $k1, $a0"
    )
}

/// Gets the current value of the processor K1 register.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub extern "C" fn get_k1() -> u32 {
    core::arch::naked_asm!(".set noreorder", ".set noat", "jr $ra", "move $v0, $k1")
}

/// Disables the CPU FPU exceptions.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub extern "C" fn disable_fpu_exceptions() {
    core::arch::naked_asm!(
        ".set noreorder",
        ".set noat",
        "cfc1 $2, $31",
        "lui $8, 0x80",
        "and $8, $2, $8",
        "ctc1 $8, $31",
        "jr $31",
        "nop",
    );
}

/// Disables interrupts, returning the previous state of the interrupt enable bit.
///
/// # Safety
///
/// Disabling interrupts for too long otherwise the watchdog will get you.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub unsafe extern "C" fn suspend_interrupts() -> u32 {
    core::arch::naked_asm!(
        ".set noreorder",
        ".set noat",
        ".word 0x70020024", // mfic $v0, $0
        ".word 0x70000026", // mtic $0, $0
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "jr $31",
        "nop"
    )
}


/// Enables interrupts to the `state` value.
///
/// # Safety
///
/// The `state` must be a value returned by [`suspend_interrupts`].
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub unsafe extern "C" fn resume_interrupts(state: u32) {
    core::arch::naked_asm!(
        ".set noreorder",
        ".set noat",
        ".word 0x70040026", // mtic $a0, $0
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "jr $31",
        "nop",
    )
}

/// Disables interrupts, returning the previous state of the interrupt enable bit.
///
/// # Safety
///
/// Disabling interrupts for too long otherwise the watchdog will get you.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
#[unsafe(naked)]
pub extern "C" fn get_current_interrupt_status() -> u32 {
    core::arch::naked_asm!(
        ".set noreorder",
        ".set noat",
        ".word 0x70020024", // mfic %0, $0
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "nop",
        "jr $31",
        "nop"
    )
}

/// Returns `true` if the the interrupt is enabled, `false` otherwise.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
pub fn is_interrupt_enabled() -> bool {
    get_current_interrupt_status() != 0
}

/// Intrinsic to hint a spin-loop to the mips processor
///
/// Rust hint on mips is a no-op, doing nothing and making the loop hot.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
pub fn spin_loop() {
    if is_interrupt_enabled() {
        let _ = thread::sceKernelDelayThread(1000);
    } else {
        core::hint::spin_loop();
    }
}

// SAFETY: must be called only once during runtime cleanup.
// NOTE: this is not guaranteed to run, for example when the program aborts.
#[cfg(all(target_os = "psp", feature = "non-stub-code"))]
pub(crate) unsafe fn cleanup() {}

/// Performs a volatile write of a memory location `addr` with the given `value` without
/// reading or dropping the old value.
///
/// Volatile operations are intended to act on I/O memory, and are guaranteed
/// to not be elided or reordered by the compiler across other volatile
/// operations.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `addr` must be either valid for writes, or `addr` must point to memory outside of all Rust
///   allocations and writing to that memory must:
///   - not trap, and
///   - not cause any memory inside a Rust allocation to be modified.
///
/// * `addr` must be properly aligned.
#[track_caller]
#[inline(always)]
#[cfg(feature = "non-stub-code")]
pub unsafe fn volatile_write<T>(addr: usize, value: T)
where
    T: crate::private::VolatileOpAllowed,
{
    unsafe { core::ptr::with_exposed_provenance_mut::<T>(addr).write_volatile(value) };
}

/// Performs a volatile read of the value from `addr` without moving it. This
/// leaves the memory in `addr` unchanged.
///
/// Volatile operations are intended to act on I/O memory, and are guaranteed
/// to not be elided or reordered by the compiler across other volatile
/// operations.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `addr` must be either valid for reads, or `addr` must point to memory outside of all Rust
///   allocations and reading from that memory must:
///   - not trap, and
///   - not cause any memory inside a Rust allocation to be modified.
///
/// * `addr` must be properly aligned.
#[track_caller]
#[inline(always)]
#[cfg(feature = "non-stub-code")]
pub unsafe fn volatile_read<T>(addr: usize) -> T
where
    T: crate::private::VolatileOpAllowed,
{
    unsafe { core::ptr::with_exposed_provenance_mut::<T>(addr).read_volatile() }
}