PSXSDK
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions | Variables
gpu.c File Reference
#include <psx.h>
#include <stdio.h>
#include <strings.h>
#include "font.h"
#include "costbl.h"

Macros

#define GPU_DATA_PORT_ADDR   0x1f801810
 
#define GPU_CONTROL_PORT_ADDR   0x1f801814
 
#define GPU_DATA_PORT   *((unsigned int*)GPU_DATA_PORT_ADDR)
 
#define GPU_CONTROL_PORT   *((unsigned int*)GPU_CONTROL_PORT_ADDR)
 
#define DPCR   *((unsigned int*)0x1f8010f0)
 
#define D2_MADR   *((unsigned int*)0x1f8010a0)
 
#define D2_BCR   *((unsigned int*)0x1f8010a4)
 
#define D2_CHCR   *((unsigned int*)0x1f8010a8)
 
#define get_clutid(cx, cy)   (((cx&0x3ff)>>4)|((cy&0x1ff)<<6))
 

Functions

unsigned int PRFONT_SCALEX (int i)
 
unsigned int PRFONT_SCALEY (int i)
 
unsigned int PRFONT_RL (unsigned char f)
 
unsigned int PRFONT_GL (unsigned char f)
 
unsigned int PRFONT_BL (unsigned char f)
 
unsigned int setup_attribs (unsigned char tpage, unsigned int attribute, unsigned char *packet)
 
int gs_calculate_scaled_size (int size, int scale)
 
void GsSetList (unsigned int *listptr)
 Assigns the internal pointer to the primitive list to the desired one, and resets the linked list position counter.
 
void GsDrawList ()
 Draws the primitive list.
 
void GsDrawListPIO ()
 Draws the primitive list using port I/O access.
 
void GsSortPoly3 (GsPoly3 *poly3)
 Adds a monochrome 3 point polygon to the packet list.
 
void GsSortPoly4 (GsPoly4 *poly4)
 Adds a monochrome 4 point polygon to the packet list.
 
void GsSortGPoly3 (GsGPoly3 *poly3)
 Adds a gradated 3 point polygon to the packet list.
 
void GsSortGPoly4 (GsGPoly4 *poly4)
 Adds a gradated 4 point polygon to the packet list.
 
void GsSortLine (GsLine *line)
 Adds a monochrome line to the packet list.
 
void GsSortGLine (GsGLine *line)
 Adds a gradated line to the packet list.
 
void GsSortDot (GsDot *dot)
 Adds a dot (pixel) to the packet list.
 
void GsSortSprite (GsSprite *sprite)
 Adds a sprite to the packet list.
 
void GsSortSimpleSprite (GsSprite *sprite)
 Always adds a REAL sprite to the packet list.
 
void GsSortRectangle (GsRectangle *rectangle)
 Adds a rectangle to the packet list.
 
void GsSortTPoly4 (GsTPoly4 *tpoly4)
 Adds a textured 4 point polygon to the packet list.
 
void GsSortTPoly3 (GsTPoly3 *tpoly3)
 Adds a textured 3 point polygon to the packet list.
 
void MoveImage (int src_x, int src_y, int dst_x, int dst_y, int w, int h)
 Moves image data from a part of the framebuffer to another.
 
void LoadImage (void *img, int x, int y, int w, int h)
 Loads image data into framebuffer memory.
 
void GsSetDrawEnv (GsDrawEnv *drawenv)
 Set drawing environment.
 
void GsSetDispEnv (GsDispEnv *dispenv)
 Set display environment.
 
void gpu_ctrl (unsigned int command, unsigned int param)
 Three functions which send data to the control port and to the data port.
 
void gpu_data (unsigned int data)
 
void gpu_data_ctrl (unsigned int command, unsigned int param)
 
unsigned int GsListPos ()
 Return pointer position in linked list.
 
void GsEnableDisplay (int enable)
 Enables the display (i.e.
 
void GsReset ()
 Resets the GPU.
 
void GsInitEx (unsigned int flags)
 Initializes the GPU.
 
void GsInit ()
 Initializes the GPU.
 
int GsSetVideoMode (int width, int height, int video_mode)
 Sets a video mode and enables the display.
 
int GsSetVideoModeEx (int width, int height, int video_mode, int rgb24, int inter, int reverse)
 Works like GsSetVideoMode() but offers more control.
 
void DrawFBRect (int x, int y, int w, int h, int r, int g, int b)
 Draws a rectangle in the framebuffer, without considering drawing and display environments (i.e.
 
void GsClearMem ()
 Clear Video RAM.
 
int GsImageFromTim (GsImage *image, void *timdata)
 Puts information about a TIM image passed in a buffer in a GsImage structure.
 
void GsUploadImage (GsImage *image)
 Uploads an image described by a GsImage structure to video memory.
 
int GsSpriteFromImage (GsSprite *sprite, GsImage *image, int do_upload)
 Fills a GsSprite structure with information from an image described by a GsImage structure, then optionally uploads data to video memory.
 
void GsSetMasking (unsigned char flag)
 Sets masking settings.
 
int GsIsDrawing ()
 Checks if the GPU is drawing.
 
void GsSetDrawEnvSimple (int x, int y, int w, int h)
 Sets drawing environment Enables drawing on the display area, disables dithering and disables all masking flags by default.
 
void GsSetDispEnvSimple (int x, int y)
 Sets display environment.
 
void GsLoadFont (int fb_x, int fb_y, int cx, int cy)
 Loads the built-in 8x8 font in Video RAM at the specified coordinates.
 
unsigned int GsPrintFont_Draw (int x, int y, int scalex, int scaley)
 
unsigned int GsVPrintFont (int x, int y, const char *fmt, va_list ap)
 Prints string using 8x8 font at screen coordinates x, y Apart from using a variable argument list, this function is identical to GsPrintFont()
 
unsigned int GsPrintFont (int x, int y, const char *fmt,...)
 Prints string using 8x8 font at screen coordinates x, y.
 
void GsSetFont (int fb_x, int fb_y, int cx, int cy)
 Change font coordinates without reloading it.
 
void GsSetFontAttrib (unsigned int flags)
 This is a function that sets an internal variable flags should be specified as an OR mask.
 
int GsIsWorking ()
 Checks if the GPU is working.
 
void GsSortCls (int r, int g, int b)
 Clear the entire drawing area with specified color.
 
void GsSetAutoWait ()
 Makes non-blocking gpu functions like GsDrawList() wait for completion.
 
void GsRotateVector (int x_a, int y_a, int z_a, double *v, double *n)
 This function can rotate a vector about the X, Y and Z axes If you don't want to rotate a vector about an axis, pass 0 as angle to that axis.
 
void GsSetListEx (unsigned int *listptr, unsigned int listpos)
 Assigns the internal pointer to the primitive list to the desired one, an sets the linked list position counter to the specified value.
 
void GsSortPolyLine (GsPolyLine *line)
 Adds a monochrome polyline to the packet list.
 
void GsSortGPolyLine (GsGPolyLine *line)
 Adds a gradated polyline to the packet list.
 
void GsSortGTPoly4 (GsGTPoly4 *tpoly4)
 Adds a gradated 4 point polygon to the packet list.
 
void GsSortGTPoly3 (GsGTPoly3 *tpoly3)
 Adds a gradated textured 3 point polygon to the packet list.
 

Variables

volatile int __psxsdk_gpu_dma_finished
 
int fb_font_x
 
int fb_font_y
 
int fb_font_cx
 
int fb_font_cy
 
unsigned short GsScreenW
 This global variable reports the current screen width, as set by GsSetVideoMode().
 
unsigned short GsScreenH
 This global variable reports the current screen height, as set by GsSetVideoMode().
 
unsigned char GsScreenM
 This global variable reports the current screen color mode (PAL or NTSC), as set by GsSetVideoMode().
 
unsigned short GsCurDrawEnvW
 This global variable reports the width of the current drawing environment.
 
unsigned short GsCurDrawEnvH
 This global variable reports the height of the current drawing environment.
 
double gs_vbuf [4][3]
 
unsigned int draw_mode_packet
 

Macro Definition Documentation

#define D2_BCR   *((unsigned int*)0x1f8010a4)
#define D2_CHCR   *((unsigned int*)0x1f8010a8)
#define D2_MADR   *((unsigned int*)0x1f8010a0)
#define DPCR   *((unsigned int*)0x1f8010f0)
#define get_clutid (   cx,
  cy 
)    (((cx&0x3ff)>>4)|((cy&0x1ff)<<6))
#define GPU_CONTROL_PORT   *((unsigned int*)GPU_CONTROL_PORT_ADDR)
#define GPU_CONTROL_PORT_ADDR   0x1f801814
#define GPU_DATA_PORT   *((unsigned int*)GPU_DATA_PORT_ADDR)
#define GPU_DATA_PORT_ADDR   0x1f801810

Function Documentation

void DrawFBRect ( int  x,
int  y,
int  w,
int  h,
int  r,
int  g,
int  b 
)

Draws a rectangle in the framebuffer, without considering drawing and display environments (i.e.

it does so in an absolute way)

Parameters
xTop-left X coordinate of area
yTop-left Y coordinate of area
wArea width
hArea height
rRed (0 - 255)
gGreen (0 - 255)
bBlue (0 - 255)
void gpu_ctrl ( unsigned int  command,
unsigned int  param 
)

Three functions which send data to the control port and to the data port.

void gpu_data ( unsigned int  data)
void gpu_data_ctrl ( unsigned int  command,
unsigned int  param 
)
int gs_calculate_scaled_size ( int  size,
int  scale 
)
void GsClearMem ( void  )

Clear Video RAM.

void GsDrawList ( void  )

Draws the primitive list.

This also has the effect of resetting the current drawing list position.

void GsDrawListPIO ( void  )

Draws the primitive list using port I/O access.

GsDrawList() uses DMA to transfer the primitive data in the linked list to the GPU.

This function which is of main interest to low-level programmers, uses GPU port I/O access instead, and it is slower (as reads and writes must be done by the main CPU), but works in all situations, even when you can't get DMA working.

This function due to its nature is blocking, and always waits for completion.

void GsEnableDisplay ( int  enable)

Enables the display (i.e.

generation of video output).

Unless you are doing low-level GPU programming, you don't need to call this function; the display is enabled automatically by GsSetVideoMode().

Parameters
enableIf TRUE (>=1) the display will be enabled, if FALSE (== 0) it will be disabled
int GsImageFromTim ( GsImage image,
void *  timdata 
)

Puts information about a TIM image passed in a buffer in a GsImage structure.

void GsInit ( void  )

Initializes the GPU.

Same as GsInitEx(0)

The display is left disabled. You can enable it with GsEnableDisplay() or more preferably with GsSetVideoMode()

void GsInitEx ( unsigned int  flags)

Initializes the GPU.

The display is left disabled. You can enable it with GsEnableDisplay() or more preferably with GsSetVideoMode()

Parameters
flagsFlag bitmask
int GsIsDrawing ( void  )

Checks if the GPU is drawing.

Returns
1 if GPU is drawing, 0 otherwise
int GsIsWorking ( void  )

Checks if the GPU is working.

Alias of GsIsDrawing()

Returns
1 if GPU is working, 0 otherwise.
unsigned int GsListPos ( void  )

Return pointer position in linked list.

Returns
Pointer position in linked list
void GsLoadFont ( int  fb_x,
int  fb_y,
int  cx,
int  cy 
)

Loads the built-in 8x8 font in Video RAM at the specified coordinates.

Specifying cx and cy as -1 will not load the black&white CLUT to video memory. Use GsSetFont() to specify clut x and clut y in that case.

The font occupies a space of 16x128 pixels in 16-bit mode, and 64x128 in 4-bit mode.

Parameters
fb_xX coordinate in framebuffer
fb_yY coordinate in framebuffer
cxX coordinate of black/white CLUT in framebuffer
cyY coordinate of black/white CLUT in framebuffer
unsigned int GsPrintFont ( int  x,
int  y,
const char *  fmt,
  ... 
)

Prints string using 8x8 font at screen coordinates x, y.

Parameters
xX coordinate
yY coordinate
fmtformat (like *printf())
Returns
Position identifier.
unsigned int GsPrintFont_Draw ( int  x,
int  y,
int  scalex,
int  scaley 
)
void GsReset ( void  )

Resets the GPU.

void GsRotateVector ( int  x_a,
int  y_a,
int  z_a,
double *  v,
double *  n 
)

This function can rotate a vector about the X, Y and Z axes If you don't want to rotate a vector about an axis, pass 0 as angle to that axis.

It is correct to pass the same argument to v and n, as calculations are first done in an internal buffer and then stored in the output array

Parameters
x_aNumber of degrees (0-359) to which the vector should be rotated about the X axis
y_aNumber of degrees (0-359) to which the vector should be rotated about the Y axis
z_aNumber of degrees (0-359) to which the vector should be rotated about the Z axis
vPointer to an array of coordinates for the source vector
nPointer to destination array for the coordinates of the rotated vector
void GsSetAutoWait ( void  )

Makes non-blocking gpu functions like GsDrawList() wait for completion.

Removes the need to use GsIsDrawing()/GsIsWorking()

void GsSetDispEnv ( GsDispEnv dispenv)

Set display environment.

Parameters
dispenvPointer to display environment structure
void GsSetDispEnvSimple ( int  x,
int  y 
)

Sets display environment.

Parameters
xTop-left X coordinate of framebuffer area to display
yTop-left Y coordinate of framebuffer area to display
void GsSetDrawEnv ( GsDrawEnv drawenv)

Set drawing environment.

Parameters
drawenvPointer to drawing environment structure
void GsSetDrawEnvSimple ( int  x,
int  y,
int  w,
int  h 
)

Sets drawing environment Enables drawing on the display area, disables dithering and disables all masking flags by default.

Parameters
xTop-left X coordinate of framebuffer area to use for drawing
yTop-left Y coordinate of framebuffer area to use for drawing
wWidth of area
hHeight of area
void GsSetFont ( int  fb_x,
int  fb_y,
int  cx,
int  cy 
)

Change font coordinates without reloading it.

void GsSetFontAttrib ( unsigned int  flags)

This is a function that sets an internal variable flags should be specified as an OR mask.

to set the wrap and scale attributes, with X scaling factor 2 and Y scaling factor 3, for example: GsSetFontAttrib(PRFONT_WRAP | PRFONT_SCALE | PRFONT_SCALEX(2) | PRFONT_SCALEY(3));

to remove all attributes (normal printing):

GsSetFontAttrib(0);

PRFONT_WRAP can't coexist with PRFONT_CENTER or PRFONT_RIGHT PRFONT_CENTER and PRFONT_RIGHT are justifying attributes and as such they are mutually exclusive - they cannot coexist with each other. Specifying both will give priority to PRFONT_CENTER.

Attribute list:

PRFONT_WRAP - Wrap to next row when the text would fall off the screen Ideal for debug PRFONT_SCALE - Enable font scaling PRFONT_SCALEX(i) - Specifies the factor "i" as the X scaling factor This has the same meaning as a sprite's scaling factor PRFONT_SCALEY(i) - Specify the factor "i" as the Y scaling factor This has the same meaning as a sprite's scaling factor PRFONT_RIGHT - Justifies text to the right PRFONT_CENTER - Justifies text at the center PRFONT_RL(f) - Luminance factor for the red component PRFONT_GL(f) - Luminance factor for the green component PRFONT_BL(f) - Luminance factor for the blue component

void GsSetList ( unsigned int *  listptr)

Assigns the internal pointer to the primitive list to the desired one, and resets the linked list position counter.

The memory address specified by your pointer has to have enough space free to contain all the packets which you want to send.

Parameters
listptrPointer to primitive list
void GsSetListEx ( unsigned int *  listptr,
unsigned int  listpos 
)

Assigns the internal pointer to the primitive list to the desired one, an sets the linked list position counter to the specified value.

The memory address specified by your pointer has to have enough space free to contain all the packets which you want to send, and you must ensure that the specified position is not out of bounds.

Parameters
listptrPointer to primitive list
listposList position
void GsSetMasking ( unsigned char  flag)

Sets masking settings.

Parameters
flagBitwise flags
int GsSetVideoMode ( int  width,
int  height,
int  video_mode 
)

Sets a video mode and enables the display.

It does so in a quicker way than GsSetVideoModeEx(), which wants more arguments but offers greater control

Parameters
widthWidth
  • 640, 384, 320 and 256 are supported by the PlayStation hardware.
heightHeight
  • 480 and 240 are supported by the PlayStation hardware. You probably want to use GsSetVideoModeEx() if you set 480 here, because this function doesn't enable interlacing.
video_modeVideo mode
  • VMODE_NTSC for NTSC, VMODE_PAL for PAL
Returns
1 on success, 0 on failure (such as unavailable video mode)
int GsSetVideoModeEx ( int  width,
int  height,
int  video_mode,
int  rgb24,
int  inter,
int  reverse 
)

Works like GsSetVideoMode() but offers more control.

Interlaced, 24-bit RGB, and "reverse" flags can be specified.

Parameters
widthWidth
  • 640, 384, 320 and 256 are supported by the PlayStation hardware.
heightHeight
  • 480 and 240 are supported by the PlayStation hardware.
video_modeVideo mode
  • VMODE_NTSC for NTSC, VMODE_PAL for PAL
rgb2424-bit RGB mode
  • If TRUE (!=0) enables the very rarely used 24-bit color mode. Unless you're very creative, don't enable this.
interInterlacing
  • If TRUE (!=0) enables interlacing. If you set the height paramater to 480, and you want to display your program on a real television screen, you must enable this.
reverseReverse??
  • The function of this is not really known. Stay safe and set this to FALSE (0).
Returns
1 on success, 0 on failure (such as unavailable video mode)
void GsSortCls ( int  r,
int  g,
int  b 
)

Clear the entire drawing area with specified color.

Parameters
rRed (0 - 255)
gGreen (0 - 255)
bBlue (0 - 255)
void GsSortDot ( GsDot dot)

Adds a dot (pixel) to the packet list.

Parameters
dotPointer to structure for dot
void GsSortGLine ( GsGLine line)

Adds a gradated line to the packet list.

Parameters
linePointer to structure for gradated line
void GsSortGPoly3 ( GsGPoly3 poly3)

Adds a gradated 3 point polygon to the packet list.

Parameters
poly3Pointer to structure for gradated 3 point polygon
void GsSortGPoly4 ( GsGPoly4 poly4)

Adds a gradated 4 point polygon to the packet list.

Parameters
poly4Pointer to structure for gradated 4 point polygon
void GsSortGPolyLine ( GsGPolyLine line)

Adds a gradated polyline to the packet list.

Parameters
linePointer to structure for monochrome line
void GsSortGTPoly3 ( GsGTPoly3 tpoly3)

Adds a gradated textured 3 point polygon to the packet list.

Parameters
tpoly3Pointer to structure for textured 3 point polygon
void GsSortGTPoly4 ( GsGTPoly4 tpoly4)

Adds a gradated 4 point polygon to the packet list.

Parameters
tpoly4Pointer to structure for textured 4 point polygon
void GsSortLine ( GsLine line)

Adds a monochrome line to the packet list.

Parameters
linePointer to structure for monochrome line
void GsSortPoly3 ( GsPoly3 poly3)

Adds a monochrome 3 point polygon to the packet list.

Parameters
poly3Pointer to structure for monochrome 3 point polygon
void GsSortPoly4 ( GsPoly4 poly4)

Adds a monochrome 4 point polygon to the packet list.

Parameters
poly4Pointer to structure for monochrome 4 point polygon
void GsSortPolyLine ( GsPolyLine line)

Adds a monochrome polyline to the packet list.

Parameters
linePointer to structure for monochrome polyline
void GsSortRectangle ( GsRectangle rectangle)

Adds a rectangle to the packet list.

Parameters
rectanglePointer to structure for rectangle
void GsSortSimpleSprite ( GsSprite sprite)

Always adds a REAL sprite to the packet list.

  • GsSortSprite() on the other hand checks for scaling and flipping, which are not supported by the "sprite" primitive on the PlayStation, but instead are done by using a textured 4-point polygon accordingly. GsSortSprite() only uses the sprite primitive when all the attributes can be done with a "sprite" primitive as well.
Parameters
spritePointer to structure for sprite
void GsSortSprite ( GsSprite sprite)

Adds a sprite to the packet list.

Parameters
spritePointer to structure for sprite
void GsSortTPoly3 ( GsTPoly3 tpoly3)

Adds a textured 3 point polygon to the packet list.

Parameters
tpoly3Pointer to structure for textured 3 point polygon
void GsSortTPoly4 ( GsTPoly4 tpoly4)

Adds a textured 4 point polygon to the packet list.

Parameters
tpoly4Pointer to structure for textured 4 point polygon
int GsSpriteFromImage ( GsSprite sprite,
GsImage image,
int  do_upload 
)

Fills a GsSprite structure with information from an image described by a GsImage structure, then optionally uploads data to video memory.

Sprite coordinates are set to 0.

void GsUploadImage ( GsImage image)

Uploads an image described by a GsImage structure to video memory.

unsigned int GsVPrintFont ( int  x,
int  y,
const char *  fmt,
va_list  ap 
)

Prints string using 8x8 font at screen coordinates x, y Apart from using a variable argument list, this function is identical to GsPrintFont()

Parameters
xX coordinate
yY coordinate
fmtformat (like *printf())
apVariable argument list
Returns
Position identifier.
void LoadImage ( void *  img,
int  x,
int  y,
int  w,
int  h 
)

Loads image data into framebuffer memory.

Parameters
imgPointer to raw image data
xTop-left X coordinate of destination area
yTop-left Y coordinate of destination area
wWidth of image data
hHeight of image data
void MoveImage ( int  src_x,
int  src_y,
int  dst_x,
int  dst_y,
int  w,
int  h 
)

Moves image data from a part of the framebuffer to another.

Actually it does a copy.

Parameters
src_xTop-left X coordinate of source area
src_yTop-left Y coordinate of source area
dst_xTop-left X coordinate of destination area
dst_yTop-left Y coordinate of destination area
wWidth of area
hHeight of area
unsigned int PRFONT_BL ( unsigned char  f)
unsigned int PRFONT_GL ( unsigned char  f)
unsigned int PRFONT_RL ( unsigned char  f)
unsigned int PRFONT_SCALEX ( int  i)
unsigned int PRFONT_SCALEY ( int  i)
unsigned int setup_attribs ( unsigned char  tpage,
unsigned int  attribute,
unsigned char *  packet 
)

Variable Documentation

volatile int __psxsdk_gpu_dma_finished
unsigned int draw_mode_packet
int fb_font_cx
int fb_font_cy
int fb_font_x
int fb_font_y
double gs_vbuf[4][3]
unsigned short GsCurDrawEnvH

This global variable reports the height of the current drawing environment.

unsigned short GsCurDrawEnvW

This global variable reports the width of the current drawing environment.

unsigned short GsScreenH

This global variable reports the current screen height, as set by GsSetVideoMode().

Do not modify.

unsigned char GsScreenM

This global variable reports the current screen color mode (PAL or NTSC), as set by GsSetVideoMode().

You can use the values in the psx_gpu_vmodes enum to evaluate this. Do not modify.

unsigned short GsScreenW

This global variable reports the current screen width, as set by GsSetVideoMode().

Do not modify.