PSYQo
kernel.hh
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2022 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <EASTL/functional.h>
30 #include <stdint.h>
31 
32 namespace psyqo {
33 
34 namespace Kernel {
35 
36 namespace Internal {
37 static inline uint32_t getCop0Status() {
38  uint32_t r;
39  asm("mfc0 %0, $12 ; nop" : "=r"(r));
40  return r;
41 }
42 
43 static inline void setCop0Status(uint32_t r) { asm("mtc0 %0, $12 ; nop" : : "r"(r)); }
44 } // namespace Internal
45 
46 static inline bool fastEnterCriticalSection() {
47  uint32_t sr = Internal::getCop0Status();
48  Internal::setCop0Status(sr & ~0x401);
49  return (sr & 0x401) == 0x401;
50 }
51 
52 static inline void fastLeaveCriticalSection() {
53  uint32_t sr = Internal::getCop0Status();
54  sr |= 0x401;
55  Internal::setCop0Status(sr);
56 }
57 
58 enum class DMA : unsigned {
59  MDECin,
60  MDECout,
61  GPU,
62  CDRom,
63  SPU,
64  EXP1,
65  OTC,
66  Max,
67 };
68 
69 void abort(const char* msg);
70 uint32_t openEvent(uint32_t classId, uint32_t spec, uint32_t mode, eastl::function<void()>&& lambda);
71 unsigned registerDmaEvent(DMA channel, eastl::function<void()>&& lambda);
72 void enableDma(DMA channel, unsigned priority = 7);
73 void disableDma(DMA channel);
74 void unregisterDmaEvent(unsigned slot);
75 
76 void queueCallback(eastl::function<void()>&& lambda);
77 void queueCallbackFromISR(eastl::function<void()>&& lambda);
78 
79 void pumpCallbacks();
80 
81 namespace Internal {
82 void prepare();
83 void addOnFrame(eastl::function<void()>&& lambda);
84 void beginFrame();
85 } // namespace Internal
86 
87 inline void assert(bool condition, const char* message) {
88  if (!condition) abort(message);
89 }
90 
91 } // namespace Kernel
92 
93 } // namespace psyqo