Nugget
Loading...
Searching...
No Matches
kernel.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2022 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <EASTL/functional.h>
30#include <stdint.h>
31
32#include <source_location>
33
34namespace psyqo {
35
36class Application;
37
47namespace Kernel {
48
49#ifdef PSYQO_RELEASE
50static constexpr bool debugMode = false;
51#else
52static constexpr bool debugMode = true;
53#endif
54
55namespace Internal {
56static inline uint32_t getCop0Status() {
57 uint32_t r;
58 asm("mfc0 %0, $12 ; nop" : "=r"(r));
59 return r;
60}
61
62static inline void setCop0Status(uint32_t r) { asm("mtc0 %0, $12 ; nop" : : "r"(r)); }
63
64[[noreturn]] void abort(const char* msg, std::source_location location = std::source_location::current());
65[[noreturn]] void abort();
66
67} // namespace Internal
68
75static inline void fastEnterCriticalSection() { asm volatile("mtc0 %0, $12 ; nop ; nop" : : "r"(0x40000000)); }
76
82static inline void fastLeaveCriticalSection() { asm volatile("mtc0 %0, $12" : : "r"(0x40000401)); }
83
84enum class DMA : unsigned {
85 MDECin,
86 MDECout,
87 GPU,
88 CDRom,
89 SPU,
90 EXP1,
91 OTC,
92 Max,
93};
94
95enum class IRQ : unsigned {
96 VBlank,
97 GPU,
98 CDRom,
99 DMA,
100 Timer0,
101 Timer1,
102 Timer2,
104 SIO,
105 SPU,
106 PIO,
107 Max,
108};
109
113[[noreturn]] static inline void abort(const char* msg,
114 std::source_location location = std::source_location::current()) {
115 if constexpr (debugMode) {
116 Internal::abort(msg, location);
117 } else if constexpr (!debugMode) {
118 (void)msg;
119 (void)location;
121 }
122}
123
155void takeOverKernel();
156
160bool isKernelTakenOver();
161
179void queueIRQHandler(IRQ irq, eastl::function<void()>&& lambda);
180
190uint32_t openEvent(uint32_t classId, uint32_t spec, uint32_t mode, eastl::function<void()>&& lambda);
191
205unsigned registerDmaEvent(DMA channel, eastl::function<void()>&& lambda);
206
214
221void enableDma(DMA channel, unsigned priority = 7);
222
228void disableDma(DMA channel);
229
235void unregisterDmaEvent(unsigned slot);
236
247void queueCallback(eastl::function<void()>&& lambda);
248
258void queueCallbackFromISR(eastl::function<void()>&& lambda);
259
280void setBreakHandler(unsigned category, eastl::function<bool(uint32_t)>&& handler);
281
287void queuePsyqoBreakHandler(eastl::function<bool(uint32_t)>&& handler);
288
289namespace Internal {
290void pumpCallbacks();
291void prepare(Application&);
292void addInitializer(eastl::function<void(Application&)>&& lambda);
293void addOnFrame(eastl::function<void()>&& lambda);
294void beginFrame();
295} // namespace Internal
296
300inline void assert(bool condition, const char* message,
301 std::source_location location = std::source_location::current()) {
302 if constexpr (debugMode) {
303 if (!condition) {
304 Internal::abort(message, location);
306 }
307 } else if constexpr (!debugMode) {
308 (void)message;
309 (void)location;
310 if (!condition) {
313 }
314 }
315}
316
317} // namespace Kernel
318
319} // namespace psyqo
The application class.
Definition application.hh:49
uint32_t r
Definition cpu.c:222
IRQ
Definition irq.h:31
void pumpCallbacks()
Definition kernel.cpp:388
void prepare(Application &)
Definition kernel.cpp:302
void addOnFrame(eastl::function< void()> &&lambda)
Definition kernel.cpp:407
void beginFrame()
Definition kernel.cpp:411
void addInitializer(eastl::function< void(Application &)> &&lambda)
Definition kernel.cpp:268
void abort()
Definition kernel.cpp:185
uint32_t openEvent(uint32_t classId, uint32_t spec, uint32_t mode, eastl::function< void()> &&lambda)
A C++ wrapper around the openEvent syscall.
Definition kernel.cpp:192
void queueCallbackFromISR(eastl::function< void()> &&lambda)
Queues a callback to be called from the main thead.
Definition kernel.cpp:382
void assert(bool condition, const char *message, std::source_location location=std::source_location::current())
A simple assert macro.
Definition kernel.hh:300
unsigned registerDmaEvent(DMA channel, eastl::function< void()> &&lambda)
Sets an ISR callback for a given DMA channel.
Definition kernel.cpp:201
DMA
Definition kernel.hh:84
void queuePsyqoBreakHandler(eastl::function< bool(uint32_t)> &&handler)
Queues a break handler for psyqo's reserved category.
Definition kernel.cpp:110
void disableDma(DMA channel)
Disables the given DMA channel.
Definition kernel.cpp:237
void setBreakHandler(unsigned category, eastl::function< bool(uint32_t)> &&handler)
Sets a break handler for a given category.
Definition kernel.cpp:104
void flushCache()
Flushes the i-cache.
IRQ
Definition kernel.hh:95
void queueCallback(eastl::function< void()> &&lambda)
Queues a callback to be called from the main thead.
Definition kernel.cpp:375
void unregisterDmaEvent(unsigned slot)
Frees the given DMA callback slot.
Definition kernel.cpp:250
bool isKernelTakenOver()
Returns whether the kernel has been taken over.
Definition kernel.cpp:114
void enableDma(DMA channel, unsigned priority=7)
Enables the given DMA channel.
Definition kernel.cpp:219
void queueIRQHandler(IRQ irq, eastl::function< void()> &&lambda)
Queues an IRQ handler to be called from the exception handler.
Definition kernel.cpp:168
void takeOverKernel()
Takes over the kernel. Can only be called once inside the main function.
Definition kernel.cpp:135
Definition cdrom-loader.hh:39
__builtin_unreachable()
static uint32_t spec
Definition syscalls.h:363
static uint32_t uint32_t void(* handler)())
Definition syscalls.h:370
static void uint32_t mode
Definition syscalls.h:230
void void(ptr, size)
void uint32_t(classId, spec)