PowerBlocks SDK
 
Loading...
Searching...
No Matches
system.h File Reference

Access parts of the base system. More...

#include <stdint.h>
#include <stddef.h>
#include "powerblocks/core/system/syscall.h"

Go to the source code of this file.

Classes

struct  system_argv_t
 Command line arguments passed to us from launcher. More...
 

Macros

#define SYSTEM_BUS_CLOCK_HZ   243000000
 Memory bus clock speed.
 
#define SYSTEM_CORE_CLOCK_HZ   729000000
 Core clock speed.
 
#define SYSTEM_TB_CLOCK_HZ   (SYSTEM_BUS_CLOCK_HZ / 4)
 Time base (internal CPU counter) clock speed.
 
#define SYSTEM_US_TO_TICKS(us)
 Convert microseconds to time base ticks.
 
#define SYSTEM_MS_TO_TICKS(ms)
 Convert miliseconds to time base ticks.
 
#define SYSTEM_S_TO_TICKS(s)
 Convert seconds to time base ticks.
 
#define SYSTEM_MEM_UNCACHED(address)
 Convert a memory address into a uncached virtual address.
 
#define SYSTEM_MEM_CACHED(address)
 Convert a memory address into a cached virtual address.
 
#define SYSTEM_MEM_PHYSICAL(address)
 Convert a memory address into a physical address.
 
#define PACKED   __attribute__((packed))
 Pack a data structure.
 
#define MEM2   __attribute__((section(".mem2")))
 
#define SYSTEM_MAIN_STACK_SIZE   (1024*1024*4)
 Size of the stack of the application main.
 
#define SYSTEM_GET_MSR(msr)
 Gets the value of the MSR register.
 
#define SYSTEM_SET_MSR(msr)
 Sets the value of the MSR register.
 
#define SYSTEM_GET_DEC(msr)
 Gets the value of the decrementing register.
 
#define SYSTEM_SET_DEC(msr)
 Sets the value of the decrementing register.
 
#define SYSTEM_DISABLE_ISR(ee_enabled)
 Disables interrupts.
 
#define SYSTEM_ENABLE_ISR(ee_enabled)
 Enables interrupts if ee_enabled is set.
 
#define SYSTEM_SYNC()
 Insures all previous instructions have executed.
 
#define SYSTEM_ISYNC()
 Refetches any instructions that may have been fetched earlier.
 
#define ASSERT(x)
 Triggers a crash with debug info if a condition fails.
 
#define ASSERT_OUT_OF_MEMORY(x)
 Triggers a out of memory assertion if the condition fails.
 
#define ALIGN(x)
 Add an alignment attribute to some data.
 
#define SYSTEM_SWITCH_SP(x)
 Switches the stack pointer with pxCurrentTCB in a task switch.
 

Functions

uint64_t system_get_time_base_int ()
 Gets the time base register of the CPU.
 
void system_delay_int (uint64_t ticks)
 Sleeps for a certain number of ticks.
 
void system_flush_dcache (const void *data, uint32_t size)
 Flushes data cache in a range.
 
void system_invalidate_dcache (void *data, uint32_t size)
 Invalidates data cache in a range.
 
void system_invalidate_icache (void *data, uint32_t size)
 Invalidates the instruction cache in a range.
 
void * system_aligned_malloc (uint32_t bytes, uint32_t alignment)
 Allocate memory with alignment.
 
void system_aligned_free (void *ptr)
 Free allocated memory with alignment.
 
void system_initialize ()
 Initializes the system.
 
void system_get_boot_path (const char *device, char *buffer, size_t length)
 Looks in the argv command line for a specific boot path.
 

Variables

system_argv_t system_argv
 

Detailed Description

Access parts of the base system.

Access parts of the base system. Like time and such.

Author
Samuel Fitzsimons (rainbain)
Date
2025 @license MIT (see LICENSE file)

Macro Definition Documentation

◆ ALIGN

#define ALIGN ( x)
Value:
__attribute__((aligned(x)))

Add an alignment attribute to some data.

Used to make a variable memory aligned for hardware

◆ ASSERT

#define ASSERT ( x)
Value:
if((x) == 0) { \
SYSCALL_ASSERT("ASSERTION FAILED", __LINE__, __FILE__); \
}

Triggers a crash with debug info if a condition fails.

Triggers a crash with debug info if a condition fails.

◆ ASSERT_OUT_OF_MEMORY

#define ASSERT_OUT_OF_MEMORY ( x)
Value:
if((x) == 0) { \
SYSCALL_ASSERT("OUT OF MEMORY", __LINE__, __FILE__); \
}

Triggers a out of memory assertion if the condition fails.

Normally out of memory errors are propagated up as a return value. But in the event a driver, or some critical system fails to allocate the heap at runtime, and must crash, then this assertion can be used.

Games can also use it if they find a memory condition fatal for whatever reason.

◆ PACKED

#define PACKED   __attribute__((packed))

Pack a data structure.

Attribute to make it so fields in a data structure are not alignment padded.

◆ SYSTEM_BUS_CLOCK_HZ

#define SYSTEM_BUS_CLOCK_HZ   243000000

Memory bus clock speed.

Clock speed of the memory bus. It also defines the time of the CPU.

◆ SYSTEM_CORE_CLOCK_HZ

#define SYSTEM_CORE_CLOCK_HZ   729000000

Core clock speed.

Clock speed of the CPU core.

◆ SYSTEM_DISABLE_ISR

#define SYSTEM_DISABLE_ISR ( ee_enabled)
Value:
do { \
uint32_t msr; \
SYSTEM_GET_MSR(msr); \
ee_enabled = (msr >> 15) & 1; \
msr &= ~(0x8000); \
SYSTEM_SET_MSR(msr); \
} while(0)

Disables interrupts.

Disables interrupts, returns the external interrupt enable bit.

◆ SYSTEM_ENABLE_ISR

#define SYSTEM_ENABLE_ISR ( ee_enabled)
Value:
if(ee_enabled) { \
uint32_t msr; \
SYSTEM_GET_MSR(msr); \
msr |= 0x8000; \
SYSTEM_SET_MSR(msr); \
}

Enables interrupts if ee_enabled is set.

Enables interrupts if ee_enabled is set. Can be used along with SYSTEM_DISABLE_ISR, to only reenable interrupts if they were enabled in the first place.

◆ SYSTEM_GET_DEC

#define SYSTEM_GET_DEC ( msr)
Value:
__asm__ __volatile__( \
"mfdec %0" : "=r"(msr) \
);

Gets the value of the decrementing register.

Gets the value of the decrementing register. When it his zero an interrupt will be triggered. Used for context switching.

◆ SYSTEM_GET_MSR

#define SYSTEM_GET_MSR ( msr)
Value:
__asm__ __volatile__( \
"mfmsr %0" : "=r"(msr) \
);

Gets the value of the MSR register.

Gets the value of the MSR register.

◆ SYSTEM_ISYNC

#define SYSTEM_ISYNC ( )
Value:
__asm__ __volatile__( \
"isync" \
);

Refetches any instructions that may have been fetched earlier.

Refetches any instructions that may have been fetched earlier.

◆ SYSTEM_MAIN_STACK_SIZE

#define SYSTEM_MAIN_STACK_SIZE   (1024*1024*4)

Size of the stack of the application main.

Size of the stack of the application main. When the system starts, the main() will be a FreeRTOS task of this stack size.

◆ SYSTEM_MEM_CACHED

#define SYSTEM_MEM_CACHED ( address)
Value:
(((uint32_t)(address) & 0x1FFFFFFF) | 0x80000000)

Convert a memory address into a cached virtual address.

Converts a cached address into a cached virtual address. This is done by setting the MSB nibble.

◆ SYSTEM_MEM_PHYSICAL

#define SYSTEM_MEM_PHYSICAL ( address)
Value:
((uint32_t)(address) & 0x1FFFFFFF)

Convert a memory address into a physical address.

Converts a cached address into a physical address. This is done by setting the MSB nibble.

◆ SYSTEM_MEM_UNCACHED

#define SYSTEM_MEM_UNCACHED ( address)
Value:
(((uint32_t)(address) & 0x1FFFFFFF) | 0xC0000000)

Convert a memory address into a uncached virtual address.

Converts a cached address into a uncached virtual address. This is done by setting the MSB nibble.

◆ SYSTEM_MS_TO_TICKS

#define SYSTEM_MS_TO_TICKS ( ms)
Value:
(SYSTEM_TB_CLOCK_HZ / 1000 * (ms))
#define SYSTEM_TB_CLOCK_HZ
Time base (internal CPU counter) clock speed.
Definition system.h:39

Convert miliseconds to time base ticks.

Convert miliseconds to time base ticks.

◆ SYSTEM_S_TO_TICKS

#define SYSTEM_S_TO_TICKS ( s)
Value:

Convert seconds to time base ticks.

Convert seconds to time base ticks.

◆ SYSTEM_SET_DEC

#define SYSTEM_SET_DEC ( msr)
Value:
__asm__ __volatile__( \
"mtdec %0" : : "r"(msr) \
);

Sets the value of the decrementing register.

Sets the value of the decrementing register. When it his zero an interrupt will be triggered. Used for context switching.

◆ SYSTEM_SET_MSR

#define SYSTEM_SET_MSR ( msr)
Value:
__asm__ __volatile__( \
"mtmsr %0" : : "r"(msr) \
);

Sets the value of the MSR register.

Sets the value of the MSR register.

◆ SYSTEM_SWITCH_SP

#define SYSTEM_SWITCH_SP ( x)
Value:
__asm__ __volatile__ ( \
"lis r3, pxCurrentTCB@ha\n\t" \
"lwz r4, pxCurrentTCB@l(r3)\n\t" /* r4 = pxCurrentTCB */ \
"mr r5, r1\n\t" /* r5 = current SP */ \
"stw r5, 0(r4)\n\t" /* pxCurrentTCB->pxTopOfStack = SP */ \
\
/* Call vTaskSwitchContext() */ \
"bl vTaskSwitchContext\n\t" \
\
"lis r3, pxCurrentTCB@ha\n\t" \
"lwz r4, pxCurrentTCB@l(r3)\n\t" /* r4 = pxCurrentTCB */ \
"lwz r1, 0(r4)\n\t" /* SP = pxCurrentTCB->pxTopOfStack */ \
)

Switches the stack pointer with pxCurrentTCB in a task switch.

Switches the stack pointer with pxCurrentTCB in a task switch. Do not call this outside of exception handers/syscalls that switch execution context.

◆ SYSTEM_SYNC

#define SYSTEM_SYNC ( )
Value:
__asm__ __volatile__( \
"sync" \
);

Insures all previous instructions have executed.

Insures all previous instructions have executed.

◆ SYSTEM_TB_CLOCK_HZ

#define SYSTEM_TB_CLOCK_HZ   (SYSTEM_BUS_CLOCK_HZ / 4)

Time base (internal CPU counter) clock speed.

Clock speed of the CPU's internal clock. It is 1/4th the bus clock speed.

◆ SYSTEM_US_TO_TICKS

#define SYSTEM_US_TO_TICKS ( us)
Value:
(SYSTEM_TB_CLOCK_HZ / 1000000 * (us))

Convert microseconds to time base ticks.

Convert microseconds to time base ticks.

Function Documentation

◆ system_aligned_free()

void system_aligned_free ( void * ptr)
extern

Free allocated memory with alignment.

Free allocated memory with alignment.

◆ system_aligned_malloc()

void * system_aligned_malloc ( uint32_t bytes,
uint32_t alignment )
extern

Allocate memory with alignment.

Allocate memory aligned to a certain number of bytes. A lot of hardware requires aligned memory. Usually 32 bytes.

Must be a power of 2.

Offsets the pointer by the alignment and stores the original unaligned pointer before the pointer returned.

◆ system_delay_int()

void system_delay_int ( uint64_t ticks)
extern

Sleeps for a certain number of ticks.

Waits for a certain number of ticks to elapse.

No interrupts version.

Recommend using FreeRTOS vTaskDelay instead.

Parameters
ticksNumber of ticks to wait for

◆ system_flush_dcache()

void system_flush_dcache ( const void * data,
uint32_t size )
extern

Flushes data cache in a range.

Flushes the CPUs data cache in a range such that all changes from CPU be visible to hardware.

Does not call sync after, so right after this the cache may not be done flushing.

◆ system_get_boot_path()

void system_get_boot_path ( const char * device,
char * buffer,
size_t length )
extern

Looks in the argv command line for a specific boot path.

When mounting a file system, this can be used to change the directory into the one the program was launched from if the file system is the one its running from.

For example passing a device of "sd" into device, it will fill buffer with the /apps/my_app/ path if the game was launched from the sd. If not it will return / into it.

Parameters
deviceBoot device
bufferOutputted path buffer
lengthLength of path buffer.

◆ system_get_time_base_int()

uint64_t system_get_time_base_int ( )
extern

Gets the time base register of the CPU.

Returns the 64 bit internal timer value of the CPU. Ticks up at speed of SYSTEM_TB_CLOCK_HZ.

No interrupts version.

◆ system_initialize()

void system_initialize ( )
extern

Initializes the system.

Initializes the system. Called from start.s, so this all happens before the C program starts running.

◆ system_invalidate_dcache()

void system_invalidate_dcache ( void * data,
uint32_t size )
extern

Invalidates data cache in a range.

Invalidates the CPUs data cache in a range such that all changes from hardware are visible to the CPU.

◆ system_invalidate_icache()

void system_invalidate_icache ( void * data,
uint32_t size )
extern

Invalidates the instruction cache in a range.

Invalidates the CPUs instruction cache in a range such that all changes from CPU be visible to instruction fetching.

Does not call isync after, so right after this the cache may not be done invalidating.