libnds
Data Structures | Macros | Typedefs | Enumerations | Functions
system.h File Reference

NDS hardware definitions. More...

#include <stddef.h>
#include <nds/ndstypes.h>

Data Structures

struct  RTCtime
 Struct containing time and day of the real time clock. More...
 
struct  rtcTime
 Struct containing time of the real time clock. More...
 
struct  rtcTimeAndDate
 Struct containing time and day of the real time clock. More...
 
struct  sysVectors
 A struct with all the CPU exeption vectors. More...
 
struct  tPERSONAL_DATA
 User's DS settings. More...
 

Macros

#define PersonalData   ((PERSONAL_DATA *)0x2FFFC80)
 Default location for the user's personal data (see PERSONAL_DATA).
 
#define PM_LED_CONTROL_MASK   (3 << 4)
 PM control register bits - LED control.
 
#define REG_DISPSTAT   (*(vu16*)0x04000004)
 LCD status register.
 
#define REG_HALTCNT   (*(vu16*)0x04000300)
 Halt control register. More...
 
#define REG_POWERCNT   *(vu16 *)0x4000304
 Power control register. More...
 
#define REG_VCOUNT   (*(vu16*)0x4000006)
 Current display scanline.
 
#define SCREEN_HEIGHT   192
 Screen height in pixels.
 
#define SCREEN_WIDTH   256
 Screen width in pixels.
 

Typedefs

typedef struct tPERSONAL_DATA PERSONAL_DATA
 User's DS settings. More...
 
typedef struct sysVectors sysVectors_t
 A struct with all the CPU exeption vectors. More...
 

Enumerations

enum  ARM7_power {
  POWER_SOUND = BIT(0) ,
  PM_CONTROL_REG = 0 ,
  PM_BATTERY_REG = 1 ,
  PM_AMPLIFIER_REG = 2 ,
  PM_READ_REGISTER = (1 << 7) ,
  PM_AMP_OFFSET = 2 ,
  PM_GAIN_OFFSET = 3 ,
  PM_BACKLIGHT_LEVEL = 4 ,
  PM_GAIN_20 = 0 ,
  PM_GAIN_40 = 1 ,
  PM_GAIN_80 = 2 ,
  PM_GAIN_160 = 3 ,
  PM_AMP_ON = 1 ,
  PM_AMP_OFF = 0
}
 Power-controlled hardware devices accessable to the ARM7. More...
 
enum  BACKLIGHT_LEVELS {
  BACKLIGHT_LOW ,
  BACKLIGHT_MED ,
  BACKLIGHT_HIGH ,
  BACKLIGHT_MAX
}
 Backlight level settings. More...
 
enum  DISP_BITS {
  DISP_IN_VBLANK = BIT(0) ,
  DISP_IN_HBLANK = BIT(1) ,
  DISP_YTRIGGERED = BIT(2) ,
  DISP_VBLANK_IRQ = BIT(3) ,
  DISP_HBLANK_IRQ = BIT(4) ,
  DISP_YTRIGGER_IRQ = BIT(5)
}
 LCD Status register defines. More...
 
enum  PM_Bits {
  PM_SOUND_AMP = BIT(0) ,
  PM_SOUND_MUTE = BIT(1) ,
  PM_BACKLIGHT_BOTTOM = BIT(2) ,
  PM_BACKLIGHT_TOP = BIT(3) ,
  PM_SYSTEM_PWR = BIT(6) ,
  PM_ARM9_DIRECT = BIT(16) ,
  POWER_LCD = PM_ARM9_DIRECT | BIT(0) ,
  POWER_2D_A = PM_ARM9_DIRECT | BIT(1) ,
  POWER_MATRIX = PM_ARM9_DIRECT | BIT(2) ,
  POWER_3D_CORE = PM_ARM9_DIRECT | BIT(3) ,
  POWER_2D_B = PM_ARM9_DIRECT | BIT(9) ,
  POWER_SWAP_LCDS = PM_ARM9_DIRECT | BIT(15) ,
  POWER_ALL_2D = PM_ARM9_DIRECT | POWER_LCD | POWER_2D_A | POWER_2D_B ,
  POWER_ALL = PM_ARM9_DIRECT | POWER_ALL_2D | POWER_3D_CORE | POWER_MATRIX
}
 Power Management control bits for powerOn() and powerOff(). More...
 

Functions

void disableSleep (void)
 Disables sleep mode from ARM9.
 
void enableSleep (void)
 Enables sleep mode from ARM9.
 
u32 getBatteryLevel (void)
 Gets the DS battery level. More...
 
u8getHeapEnd (void)
 Returns current end of heap space. More...
 
u8getHeapLimit (void)
 Returns current heap limit. More...
 
u8getHeapStart (void)
 Returns current start of heap space. More...
 
void installSystemFIFO (void)
 Install the system FIFO handlers. More...
 
static bool isDSiMode (void)
 Checks whether the application is running in DSi mode. More...
 
static bool isHwDebugger (void)
 Checks whether the application is running in a debugger or retail console. More...
 
static void lcdMainOnBottom (void)
 Forces the main core to display on the bottom.
 
static void lcdMainOnTop (void)
 Forces the main core to display on the top.
 
static void lcdSwap (void)
 Switches the screens.
 
void ledBlink (int bm)
 Set the LED blink mode. More...
 
void * memCached (void *address)
 Returns a cached mirror of an address. More...
 
void * memUncached (void *address)
 Returns an uncached mirror of an address. More...
 
void peripheralSlot2DisableCache (void)
 Disable data cache for the DS slot-2 memory region. If write-back was enabled, additionally clear and flush the data cache. More...
 
void peripheralSlot2EnableCache (bool write_back)
 Enable data cache for the DS slot-2 memory region. More...
 
void powerOff (uint32_t bits)
 Turns off specified hardware. More...
 
void powerOn (uint32_t bits)
 Turns on specified hardware. More...
 
void readFirmware (u32 address, void *buffer, u32 length)
 Read bytes at a specified address from firmware flash.
 
static int readPowerManagement (int reg)
 Read from a power management register.
 
bool readUserSettings (void)
 Read user settings/personal data from firmware flash to a shared memory location.
 
bool setCpuClock (bool speed)
 Sets the ARM9 clock speed, only possible in DSi mode. More...
 
void setVectorBase (int highVector)
 Set the arm9 vector base. More...
 
static void SetYtrigger (int Yvalue)
 Sets the LCD refresh scanline Y trigger. More...
 
static void systemShutDown (void)
 Powers down the DS.
 
void systemSleep (void)
 Causes the NDS to go to sleep. More...
 
int writeFirmware (u32 address, void *buffer, u32 length)
 Write bytes at a specified address to firmware flash.
 
int writePowerManagement (int reg, int command)
 Write to a power management register.
 

Detailed Description

NDS hardware definitions.

These definitions are usually only touched during the initialization of the program.

Macro Definition Documentation

◆ REG_HALTCNT

#define REG_HALTCNT   (*(vu16*)0x04000300)

Halt control register.

Writing 0x40 to REG_HALTCNT activates GBA mode. REG_HALTCNT can only be accessed via the BIOS.

◆ REG_POWERCNT

#define REG_POWERCNT   *(vu16 *)0x4000304

Power control register.

This register controls what hardware should be turned ON or OFF.

Typedef Documentation

◆ PERSONAL_DATA

typedef struct tPERSONAL_DATA PERSONAL_DATA

User's DS settings.

Defines the structure the DS firmware uses for transfer of the user's settings to the booted program.

Theme/Color values:

  • 0 = Gray
  • 1 = Brown
  • 2 = Red
  • 3 = Pink
  • 4 = Orange
  • 5 = Yellow
  • 6 = Yellow/Green-ish
  • 7 = Green
  • 8 = Dark Green
  • 9 = Green/Blue-ish
  • 10 = Light Blue
  • 11 = Blue
  • 12 = Dark Blue
  • 13 = Dark Purple
  • 14 = Purple
  • 15 = Purple/Red-ish

Language values:

  • 0 = Japanese
  • 1 = English
  • 2 = French
  • 3 = German
  • 4 = Italian
  • 5 = Spanish
  • 6 = Chinese(?)
  • 7 = Unknown/Reserved

◆ sysVectors_t

typedef struct sysVectors sysVectors_t

A struct with all the CPU exeption vectors.

Each member contains an ARM instuction that will be executed when an exeption occurs.

See gbatek for more information.

Enumeration Type Documentation

◆ ARM7_power

enum ARM7_power

Power-controlled hardware devices accessable to the ARM7.

Note
These should only be used when programming for the ARM7. Trying to boot up these hardware devices via the ARM9 would lead to unexpected results. ARM7 only.
Enumerator
POWER_SOUND 

Controls the power for the sound controller.

PM_CONTROL_REG 

Selects the PM control register.

PM_BATTERY_REG 

Selects the PM battery register.

PM_AMPLIFIER_REG 

Selects the PM amplifier register.

PM_READ_REGISTER 

Selects the PM read register.

PM_AMP_OFFSET 

Selects the PM amp register.

PM_GAIN_OFFSET 

Selects the PM gain register.

PM_BACKLIGHT_LEVEL 

Selects the DS Lite backlight register.

PM_GAIN_20 

Sets the mic gain to 20db.

PM_GAIN_40 

Sets the mic gain to 40db.

PM_GAIN_80 

Sets the mic gain to 80db.

PM_GAIN_160 

Sets the mic gain to 160db.

PM_AMP_ON 

Turns the sound amp ON.

PM_AMP_OFF 

Turns the sound amp OFF.

◆ BACKLIGHT_LEVELS

Backlight level settings.

Note
Only available on DS Lite.
Enumerator
BACKLIGHT_LOW 

Low backlight setting.

BACKLIGHT_MED 

Medium backlight setting.

BACKLIGHT_HIGH 

High backlight setting.

BACKLIGHT_MAX 

Max backlight setting.

◆ DISP_BITS

enum DISP_BITS

LCD Status register defines.

Enumerator
DISP_IN_VBLANK 

The display currently in a vertical blank.

DISP_IN_HBLANK 

The display currently in a horizontal blank.

DISP_YTRIGGERED 

Current scanline and DISP_Y match.

DISP_VBLANK_IRQ 

Interrupt on vertical blank.

DISP_HBLANK_IRQ 

Interrupt on horizontal blank.

DISP_YTRIGGER_IRQ 

Interrupt when current scanline and DISP_Y match.

◆ PM_Bits

enum PM_Bits

Power Management control bits for powerOn() and powerOff().

Enumerator
PM_SOUND_AMP 

Power the sound hardware (needed for GBA mode too)

PM_SOUND_MUTE 

Mute the main speakers, headphone output will still work.

PM_BACKLIGHT_BOTTOM 

Enable the bottom backlight if set.

PM_BACKLIGHT_TOP 

Enable the top backlight if set.

PM_SYSTEM_PWR 

Turn the NDS power OFF if set.

PM_ARM9_DIRECT 

Internal: Write to REG_POWERCNT directly instead of sending a FIFO message.

POWER_LCD 

Controls the power for both LCD screens.

POWER_2D_A 

Controls the power for the main 2D core.

POWER_MATRIX 

Controls the power for the 3D matrix.

POWER_3D_CORE 

Controls the power for the main 3D core.

POWER_2D_B 

Controls the power for the sub 2D core.

POWER_SWAP_LCDS 

Controls which screen should use the main core.

POWER_ALL_2D 

Power 2D hardware.

POWER_ALL 

Power everything.

Function Documentation

◆ getBatteryLevel()

u32 getBatteryLevel ( void  )

Gets the DS battery level.

This returns a value with two fields. Bits 0 to 3 are the battery level, and bit 7 is set to 1 if an external power source is connected to the DS.

On DSi the battery level is the one reported by the hardware. On DS, the battery level is only "high" or "low", and libnds returns 15 or 3 respectively as the equivalent DSi battery level.

Returns
Battery level and external power source status.

◆ getHeapEnd()

u8 * getHeapEnd ( void  )

Returns current end of heap space.

Returns
Returns a pointer to the end of the heap.

◆ getHeapLimit()

u8 * getHeapLimit ( void  )

Returns current heap limit.

Returns
Returns a pointer to the limit of the heap. It won't grow past this address.

◆ getHeapStart()

u8 * getHeapStart ( void  )

Returns current start of heap space.

Returns
Returns a pointer to the start of the heap.

◆ installSystemFIFO()

void installSystemFIFO ( void  )

Install the system FIFO handlers.

This handles power management, DSi SD card access, and firmware flash access.

◆ isDSiMode()

static bool isDSiMode ( void  )
inlinestatic

Checks whether the application is running in DSi mode.

Returns
Returns true if the application is running in DSi mode.

◆ isHwDebugger()

static bool isHwDebugger ( void  )
inlinestatic

Checks whether the application is running in a debugger or retail console.

It works on DS and DSi consoles.

Note
swiIsDebugger() only works with the cache disabled, this function doesn't have any restrictions.
Returns
Returns true if running on a debugger unit, false on retail units.

◆ ledBlink()

void ledBlink ( int  bm)

Set the LED blink mode.

Parameters
bmWhat to power on.

◆ memCached()

void * memCached ( void *  address)

Returns a cached mirror of an address.

Parameters
addressan address.
Returns
a pointer to the cached mirror of that address.

◆ memUncached()

void * memUncached ( void *  address)

Returns an uncached mirror of an address.

Parameters
addressan address.
Returns
a pointer to the uncached mirror of that address.

◆ peripheralSlot2DisableCache()

void peripheralSlot2DisableCache ( void  )

Disable data cache for the DS slot-2 memory region. If write-back was enabled, additionally clear and flush the data cache.

Note that flushing the data cache may still be required to remove stale "read" pages, such as if you write to external RAM uncached and then re-enable cache.

◆ peripheralSlot2EnableCache()

void peripheralSlot2EnableCache ( bool  write_back)

Enable data cache for the DS slot-2 memory region.

Note that this is not safe to enable if you're using Slot-2 memory for purposes other than external RAM, such as rumble or other I/O.

Parameters
write_backSet as write-back. This allows writes to skip the slow Slot-2 bus, at the expense of requiring a full memory flush when calling peripheralSlot2DisableCache().

◆ powerOff()

static void powerOff ( uint32_t  bits)
inline

Turns off specified hardware.

It may be called from the ARM7 or ARM9 (ARM9 power bits will be ignored by the ARM7, ARM7 power bits will be passed to the ARM7 from the ARM9).

Parameters
bitsWhat to power OFF (PM_Bits).

◆ powerOn()

static void powerOn ( uint32_t  bits)
inline

Turns on specified hardware.

It may be called from the ARM7 or ARM9 (ARM9 power bits will be ignored by the ARM7, ARM7 power bits will be passed to the ARM7 from the ARM9).

Parameters
bitsWhat to power ON (PM_Bits).

◆ setCpuClock()

bool setCpuClock ( bool  speed)

Sets the ARM9 clock speed, only possible in DSi mode.

Parameters
speedCPU speed (false = 67.03MHz, true = 134.06MHz)
Returns
The old CPU speed value

◆ setVectorBase()

void setVectorBase ( int  highVector)

Set the arm9 vector base.

Parameters
highVectorHigh vector.
Note
ARM9 only

◆ SetYtrigger()

static void SetYtrigger ( int  Yvalue)
inlinestatic

Sets the LCD refresh scanline Y trigger.

Parameters
YvalueThe value for the Y trigger.

◆ systemSleep()

void systemSleep ( void  )

Causes the NDS to go to sleep.

The NDS will be reawakened when the lid is opened.

Note
By default, this is automatically called when closing the lid.