Nugget
Loading...
Searching...
No Matches
Namespaces | Enumerations | Functions
psyqo::Kernel Namespace Reference

The Kernel namespace for internal use. More...

Namespaces

namespace  Internal
 

Enumerations

enum class  DMA : unsigned {
  MDECin , MDECout , GPU , CDRom ,
  SPU , EXP1 , OTC , Max
}
 
enum class  IRQ : unsigned {
  VBlank , GPU , CDRom , DMA ,
  Timer0 , Timer1 , Timer2 , Controller ,
  SIO , SPU , PIO , Max
}
 

Functions

void takeOverKernel ()
 Takes over the kernel. Can only be called once inside the main function.
 
bool isKernelTakenOver ()
 Returns whether the kernel has been taken over.
 
void queueIRQHandler (IRQ irq, eastl::function< void()> &&lambda)
 Queues an IRQ handler to be called from the exception handler.
 
uint32_t openEvent (uint32_t classId, uint32_t spec, uint32_t mode, eastl::function< void()> &&lambda)
 A C++ wrapper around the openEvent syscall.
 
unsigned registerDmaEvent (DMA channel, eastl::function< void()> &&lambda)
 Sets an ISR callback for a given DMA channel.
 
void flushCache ()
 Flushes the i-cache.
 
void enableDma (DMA channel, unsigned priority=7)
 Enables the given DMA channel.
 
void disableDma (DMA channel)
 Disables the given DMA channel.
 
void unregisterDmaEvent (unsigned slot)
 Frees the given DMA callback slot.
 
void queueCallback (eastl::function< void()> &&lambda)
 Queues a callback to be called from the main thead.
 
void queueCallbackFromISR (eastl::function< void()> &&lambda)
 Queues a callback to be called from the main thead.
 
void setBreakHandler (unsigned category, eastl::function< bool(uint32_t)> &&handler)
 Sets a break handler for a given category.
 
void queuePsyqoBreakHandler (eastl::function< bool(uint32_t)> &&handler)
 Queues a break handler for psyqo's reserved category.
 
void assert (bool condition, const char *message, std::source_location location=std::source_location::current())
 A simple assert macro.
 

Detailed Description

The Kernel namespace for internal use.

The Kernel namespace is technically for internal use only, but it is included in the public API for convenience. It contains various glue to the actual PS1 kernel, as well as some useful utility functions.

Enumeration Type Documentation

◆ DMA

enum class psyqo::Kernel::DMA : unsigned
strong
Enumerator
MDECin 
MDECout 
GPU 
CDRom 
SPU 
EXP1 
OTC 
Max 

◆ IRQ

enum class psyqo::Kernel::IRQ : unsigned
strong
Enumerator
VBlank 
GPU 
CDRom 
DMA 
Timer0 
Timer1 
Timer2 
Controller 
SIO 
SPU 
PIO 
Max 

Function Documentation

◆ assert()

void psyqo::Kernel::assert ( bool  condition,
const char *  message,
std::source_location  location = std::source_location::current() 
)
inline

A simple assert macro.

◆ disableDma()

void psyqo::Kernel::disableDma ( DMA  channel)

Disables the given DMA channel.

Parameters
channelthe DMA channel to disable.

◆ enableDma()

void psyqo::Kernel::enableDma ( DMA  channel,
unsigned  priority = 7 
)

Enables the given DMA channel.

Parameters
channelthe DMA channel to enable.
prioritythe priority of the channel.

◆ flushCache()

void psyqo::Kernel::flushCache ( )

Flushes the i-cache.

This function is used to flush the i-cache. This is required when the application has written some code to memory.

◆ isKernelTakenOver()

bool psyqo::Kernel::isKernelTakenOver ( )

Returns whether the kernel has been taken over.

◆ openEvent()

uint32_t psyqo::Kernel::openEvent ( uint32_t  classId,
uint32_t  spec,
uint32_t  mode,
eastl::function< void()> &&  lambda 
)

A C++ wrapper around the openEvent syscall.

This enables the application to register a C++ lambda for the kernel's OpenEvent call. This will allocate an internal slot, with currently no mechanism to free it. This means that calling closeEvent on the resulting event will leak resources. If psyqo took over the kernel, this function will no longer work.

◆ queueCallback()

void psyqo::Kernel::queueCallback ( eastl::function< void()> &&  lambda)

Queues a callback to be called from the main thead.

This function is used to queue a callback to be called from the main thread, during idle moments like various blocking operations. This variant is safe to call from the main thread only. Its usefulness from the main thread is limited, and could be considered the same as JavaScript's process.nextTick(), meaning it's a great way to avoid get out of a deep callstack.

◆ queueCallbackFromISR()

void psyqo::Kernel::queueCallbackFromISR ( eastl::function< void()> &&  lambda)

Queues a callback to be called from the main thead.

This function is used to queue a callback to be called from the main thead, during idle moments like various blocking operations. This variant is safe to call from an interrupt handler. This is how to idiomatically execute something safely from an interrupt handler.

◆ queueIRQHandler()

void psyqo::Kernel::queueIRQHandler ( IRQ  irq,
eastl::function< void()> &&  lambda 
)

Queues an IRQ handler to be called from the exception handler.

This function is used to queue an IRQ handler to be called from the exception handler when the kernel has been taken over. While it is technically possible to queue VBlank, it should solely be reserved for the GPU object instead. Also, note that the kernel has its own DMA IRQ handler, and that the registerDmaEvent function should be used instead of trying to queue a handler for the DMA IRQ. The specified handler will be called from the exception handler, with the same restrictions as for any other interrupt handler. The queued handlers will be called in the order they were queued, but it is recommended to only queue one handler per IRQ.

Parameters
irqThe IRQ to handle.
lambdaThe function to call when the IRQ is triggered.

◆ queuePsyqoBreakHandler()

void psyqo::Kernel::queuePsyqoBreakHandler ( eastl::function< bool(uint32_t)> &&  handler)

Queues a break handler for psyqo's reserved category.

Parameters
handlerThe handler to call when a break occurs.

◆ registerDmaEvent()

unsigned psyqo::Kernel::registerDmaEvent ( DMA  channel,
eastl::function< void()> &&  lambda 
)

Sets an ISR callback for a given DMA channel.

The PSYQo kernel registers a dispatcher interrupt handler for DMA interrupts, and this function registers a callback function for a given DMA channel. Multiple callbacks can be registered for a given channel. All the callbacks registered will be called sequentially during the dispatcher interrupt handler. Note this means the callbacks will be called from the interrupt handler, with the same restrictions as for any other interrupt handler.

Returns
unsigned A slot id for the given callback.

◆ setBreakHandler()

void psyqo::Kernel::setBreakHandler ( unsigned  category,
eastl::function< bool(uint32_t)> &&  handler 
)

Sets a break handler for a given category.

This function is used to set a break handler for a given category. The category is technically the upper 10 bits of the break code, and the handler is a function that takes the lower 10 bits of the break code. The handler should return true if it handled the break, and false otherwise. The handler will be called from the exception handler, with the same restrictions as for any other interrupt handler. Note that the category is actually limited to 16 categories by psyqo, from 0 to 15. It is also worth noting that category 0 is usually reserved for pcdrv, category 7 is reserved by the compiler to emit division by zero checks, and psyqo uses category 14 for its own purposes. Only one handler can be set per category, and trying to set a handler for a category that already has a handler will cause an assertion failure.

Parameters
categoryThe category to handle.

◆ takeOverKernel()

void psyqo::Kernel::takeOverKernel ( )

Takes over the kernel. Can only be called once inside the main function.

This function will make psyqo take over the retail kernel. This means the application will no longer be able to call any of the kernel functions, and will have to rely on the psyqo kernel instead. Debugging features from third party addons which hook into the kernel will no longer work. Most calls to the kernel will either be no-ops or will crash the application. Most notably, only the printf call will be redirected to psyqo's printf, but will not be printing anywhere, so only emulators hooking into A0 calls will be able to see the output.

Disabling the kernel is a one-way operation, and cannot be undone. The kernel will be taken over before the first call to prepare. The exception handler that psyqo installs will not be able to catch problems, but is much more lightweight and faster than the retail one. Also, 60kB of memory can be reclaimed, and linking the binary with -Xlinker –defsym=TLOAD_ADDR=0x80001000 will allow the application to do just that. This requires a loader able to write into the kernel while disabling interrupts. The ps1-packer tool can achieve that. The first 4kB of memory is reserved for the psyqo kernel.

It is noteworthy that while the pros of taking over the kernel are significant, the cons are also significant. The loss of debugging, flexibility, and retail kernel features may not be worth it for most application cases, and should be considered carefully.

Last but not least, like with most psyqo features, the added payload to the binary to support the feature will only occur if this function is called.

◆ unregisterDmaEvent()

void psyqo::Kernel::unregisterDmaEvent ( unsigned  slot)

Frees the given DMA callback slot.

Parameters
slotThe slot to free, as returned by registerDmaEvent.