KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
G2 Bus

Driver for accessing the devices on the G2 Bus More...

Files

file  g2bus.h
 G2 bus memory interface.
 

Data Structures

struct  g2_ctx_t
 G2 context. More...
 

Macros

#define G2_DMA_TO_G2   0
 G2Bus DMA direction.
 
#define G2_DMA_TO_SH4   1
 

Typedefs

typedef void(* g2_dma_callback_t) (void *data)
 G2Bus DMA interrupt callback type.
 

Functions

int g2_dma_transfer (void *sh4, void *g2bus, size_t length, uint32_t block, g2_dma_callback_t callback, void *cbdata, uint32_t dir, uint32_t mode, uint32_t g2chn, uint32_t sh4chn)
 Perform a DMA transfer between SH-4 RAM and G2 Bus.
 
int g2_dma_init (void)
 Initialize DMA support.
 
void g2_dma_shutdown (void)
 Shutdown DMA support.
 
static g2_ctx_t g2_lock (void)
 Disable IRQs and G2 DMA.
 
static void g2_unlock (g2_ctx_t ctx)
 Enable IRQs and G2 DMA.
 
uint8_t g2_read_8 (uintptr_t address)
 Read one byte from G2.
 
void g2_write_8 (uintptr_t address, uint8_t value)
 Write a single byte to G2.
 
uint16 g2_read_16 (uintptr_t address)
 Read one 16-bit word from G2.
 
void g2_write_16 (uintptr_t address, uint16_t value)
 Write a 16-bit word to G2.
 
uint32_t g2_read_32 (uintptr_t address)
 Read one 32-bit dword from G2.
 
void g2_write_32 (uintptr_t address, uint32_t value)
 Write a 32-bit dword to G2.
 
void g2_read_block_8 (uint8_t *output, uintptr_t address, size_t amt)
 Read a block of bytes from G2.
 
void g2_write_block_8 (const uint8_t *input, uintptr_t address, size_t amt)
 Write a block of bytes to G2.
 
void g2_read_block_16 (uint16_t *output, uintptr_t address, size_t amt)
 Read a block of words from G2.
 
void g2_write_block_16 (const uint16_t *input, uintptr_t address, size_t amt)
 Write a block of words to G2.
 
void g2_read_block_32 (uint32_t *output, uintptr_t address, size_t amt)
 Read a block of dwords from G2.
 
void g2_write_block_32 (const uint32_t *input, uintptr_t address, size_t amt)
 Write a block of dwords to G2.
 
void g2_memset_8 (uintptr_t address, uint8_t c, size_t amt)
 Set a block of bytes to G2.
 
void g2_fifo_wait (void)
 Wait for the G2 write FIFO to empty.
 

List of G2 Bus channels

AICA (SPU) is channel 0, BBA uses channel 1.

CH2_DMA_G2CHN and CH3_DMA_G2CHN are not currently tied to any specific device.

Note
A change in the implementation has rendered *_DMA_MODE and *_DMA_SHCHN obsolete.

In the current implementation, *_DMA_MODE should always be set to zero (representing CPU_TRIGGER). There is also no involvement of SH4-DMA with G2-DMA; therefore, the *_DMA_SHCHN values have been deprecated.

#define G2_DMA_CHAN_SPU   0
 AICA: G2 channel 0.
 
#define G2_DMA_CHAN_BBA   1
 BBA: G2 channel 1.
 
#define G2_DMA_CHAN_CH2   2
 CH2: G2 channel 2.
 
#define G2_DMA_CHAN_CH3   3
 CH3: G2 channel 3.
 

Detailed Description

Driver for accessing the devices on the G2 Bus

Macro Definition Documentation

◆ G2_DMA_CHAN_BBA

#define G2_DMA_CHAN_BBA   1

BBA: G2 channel 1.

◆ G2_DMA_CHAN_CH2

#define G2_DMA_CHAN_CH2   2

CH2: G2 channel 2.

◆ G2_DMA_CHAN_CH3

#define G2_DMA_CHAN_CH3   3

CH3: G2 channel 3.

◆ G2_DMA_CHAN_SPU

#define G2_DMA_CHAN_SPU   0

AICA: G2 channel 0.

◆ G2_DMA_TO_G2

#define G2_DMA_TO_G2   0

G2Bus DMA direction.

The direction you want the data to go. SH4 to AICA, BBA, etc use SH4TOG2BUS, otherwise G2BUSTOSH4.

◆ G2_DMA_TO_SH4

#define G2_DMA_TO_SH4   1

Typedef Documentation

◆ g2_dma_callback_t

typedef void(* g2_dma_callback_t) (void *data)

G2Bus DMA interrupt callback type.

Functions that act as callbacks when G2-DMA completes should be of this type. These functions will be called inside an interrupt context, so don't try to use anything that might stall.

Parameters
dataUser data passed in to the g2_dma_transfer() function.

Function Documentation

◆ g2_dma_init()

int g2_dma_init ( void )

Initialize DMA support.

This function sets up the DMA support for transfers to/from the G2 Bus.

Return values
0On success (no error conditions defined).

◆ g2_dma_shutdown()

void g2_dma_shutdown ( void )

Shutdown DMA support.

◆ g2_dma_transfer()

int g2_dma_transfer ( void * sh4,
void * g2bus,
size_t length,
uint32_t block,
g2_dma_callback_t callback,
void * cbdata,
uint32_t dir,
uint32_t mode,
uint32_t g2chn,
uint32_t sh4chn )

Perform a DMA transfer between SH-4 RAM and G2 Bus.

This function copies a block of data between SH-4 RAM and G2 Bus via DMA. You specify the direction of the copy (SH4TOG2BUS/G2BUSTOSH4). There are all kinds of constraints that must be fulfilled to actually do this, so make sure to read all the fine print with the parameter list.

If a callback is specified, it will be called in an interrupt context, so keep that in mind in writing the callback.

Parameters
sh4Where to copy from/to. Must be 32-byte aligned.
g2busWhere to copy from/to. Must be 32-byte aligned.
lengthThe number of bytes to copy. Must be a multiple of 32.
blockNon-zero if you want the function to block until the DMA completes.
callbackA function to call upon completion of the DMA.
cbdataData to pass to the callback function.
dirSH4TOG2BUS or G2BUSTOSH4.
modeIgnored; for compatibility only.
g2chnSee g2b_channels.
sh4chnIgnored; for compatibility only.
Return values
0On success.
-1On failure. Sets errno as appropriate.
Error Conditions:
EINPROGRESS - DMA already in progress
EFAULT - sh4 and/or g2bus is not 32-byte aligned
EINVAL - Invalid g2chn EIO - I/O error

◆ g2_fifo_wait()

void g2_fifo_wait ( void )

Wait for the G2 write FIFO to empty.

This function will spinwait until the G2 FIFO indicates that it has been drained. The FIFO is 32 bytes in length, and thus when accessing AICA you must do this at least for every 8 32-bit writes that you execute.

◆ g2_lock()

static g2_ctx_t g2_lock ( void )
inlinestatic

Disable IRQs and G2 DMA.

This function makes the following g2_read_*()/g2_write_*() functions atomic by disabling IRQs and G2 DMA and storing their states. Pass the context created by this function to g2_unlock() to re-enable IRQs and G2 DMA.

Returns
The context containing the IRQ and G2 DMA states.

References FIFO_G2, FIFO_SH4, FIFO_STATUS, irq_disable(), and g2_ctx_t::irq_state.

◆ g2_memset_8()

void g2_memset_8 ( uintptr_t address,
uint8_t c,
size_t amt )

Set a block of bytes to G2.

This function acts as memset() for setting a block of bytes on G2. It will take the necessary precautions for accessing G2.

Parameters
addressThe address in G2-space to write to.
cThe byte to write.
amtThe number of bytes to write.

◆ g2_read_16()

uint16 g2_read_16 ( uintptr_t address)

Read one 16-bit word from G2.

This function reads a single word from the specified address, taking all necessary precautions that are required for accessing G2.

Parameters
addressThe address in memory to read.
Returns
The word read from the address specified.

◆ g2_read_32()

uint32_t g2_read_32 ( uintptr_t address)

Read one 32-bit dword from G2.

This function reads a single dword from the specified address, taking all necessary precautions that are required for accessing G2.

Parameters
addressThe address in memory to read.
Returns
The dword read from the address specified.

◆ g2_read_8()

uint8_t g2_read_8 ( uintptr_t address)

Read one byte from G2.

This function reads a single byte from the specified address, taking all necessary precautions that are required for accessing G2.

Parameters
addressThe address in memory to read.
Returns
The byte read from the address specified.

◆ g2_read_block_16()

void g2_read_block_16 ( uint16_t * output,
uintptr_t address,
size_t amt )

Read a block of words from G2.

This function acts as memcpy() for copying data from G2 to system memory, but it copies 16 bits at a time. It will take the necessary precautions before accessing G2 for you as well.

Parameters
outputPointer in system memory to write to.
addressThe address in G2-space to read from.
amtThe number of words to read.

◆ g2_read_block_32()

void g2_read_block_32 ( uint32_t * output,
uintptr_t address,
size_t amt )

Read a block of dwords from G2.

This function acts as memcpy() for copying data from G2 to system memory, but it copies 32 bits at a time. It will take the necessary precautions before accessing G2 for you as well.

Parameters
outputPointer in system memory to write to.
addressThe address in G2-space to read from.
amtThe number of dwords to read.

◆ g2_read_block_8()

void g2_read_block_8 ( uint8_t * output,
uintptr_t address,
size_t amt )

Read a block of bytes from G2.

This function acts as memcpy() for copying data from G2 to system memory. It will take the necessary precautions before accessing G2 for you as well.

Parameters
outputPointer in system memory to write to.
addressThe address in G2-space to read from.
amtThe number of bytes to read.

◆ g2_unlock()

static void g2_unlock ( g2_ctx_t ctx)
inlinestatic

Enable IRQs and G2 DMA.

This function restores the IRQ and G2 DMA states using the context value generated by g2_lock().

Parameters
ctxThe context containing IRQ and G2 DMA states.

References irq_restore(), and g2_ctx_t::irq_state.

◆ g2_write_16()

void g2_write_16 ( uintptr_t address,
uint16_t value )

Write a 16-bit word to G2.

This function writes one word to the specified address, taking all the necessary precautions to ensure your write actually succeeds.

Parameters
addressThe address in memory to write to.
valueThe value to write to that address.

◆ g2_write_32()

void g2_write_32 ( uintptr_t address,
uint32_t value )

Write a 32-bit dword to G2.

This function writes one dword to the specified address, taking all the necessary precautions to ensure your write actually succeeds.

Parameters
addressThe address in memory to write to.
valueThe value to write to that address.

◆ g2_write_8()

void g2_write_8 ( uintptr_t address,
uint8_t value )

Write a single byte to G2.

This function writes one byte to the specified address, taking all the necessary precautions to ensure your write actually succeeds.

Parameters
addressThe address in memory to write to.
valueThe value to write to that address.

◆ g2_write_block_16()

void g2_write_block_16 ( const uint16_t * input,
uintptr_t address,
size_t amt )

Write a block of words to G2.

This function acts as memcpy() for copying data to G2 from system memory, copying 16 bits at a time. It will take the necessary precautions for accessing G2.

Parameters
inputThe pointer in system memory to read from.
addressThe address in G2-space to write to.
amtThe number of words to write.

◆ g2_write_block_32()

void g2_write_block_32 ( const uint32_t * input,
uintptr_t address,
size_t amt )

Write a block of dwords to G2.

This function acts as memcpy() for copying data to G2 from system memory, copying 32 bits at a time. It will take the necessary precautions for accessing G2.

Parameters
inputThe pointer in system memory to read from.
addressThe address in G2-space to write to.
amtThe number of dwords to write.

◆ g2_write_block_8()

void g2_write_block_8 ( const uint8_t * input,
uintptr_t address,
size_t amt )

Write a block of bytes to G2.

This function acts as memcpy() for copying data to G2 from system memory. It will take the necessary precautions for accessing G2.

Parameters
inputThe pointer in system memory to read from.
addressThe address in G2-space to write to.
amtThe number of bytes to write.