Run make
for a single-threaded build, or make MT=y
for a multi-threaded
build. Copy libgfxd.a
to your lib directory, and gfxd.h
to your include
directory.
Example source code:
#include <stdio.h>
#include <gfxd.h>
static int macro_fn(void)
{
/* Print a tab before each macro, and a comma and newline after each
macro */
gfxd_puts("\t");
gfxd_macro_dflt(); /* Execute the default macro handler */
gfxd_puts(",\n");
return 0;
}
int main()
{
/* Read from stdin and write to stdout */
gfxd_input_fd(fileno(stdin));
gfxd_output_fd(fileno(stdout));
/* Override the default macro handler to make the output prettier */
gfxd_macro_fn(macro_fn);
/* Select F3DEX as the target microcode */
gfxd_target(gfxd_f3dex);
/* Set the input endianness to big endian, and the word size to 4 */
gfxd_endian(gfxd_endian_big, 4);
/* Print an opening brace */
gfxd_puts("{\n");
/* Execute until the end of input, or until encountering an invalid
command */
gfxd_execute();
/* Print a closing brace */
gfxd_puts("}\n");
}
Example input (binary):
0xe7000000, 0x00000000,
0xfc127e03, 0xfffffdf8,
0xb900031d, 0xc8112078,
0xb6000000, 0x000e0000,
0xb7000000, 0x00012000,
0xfa000000, 0xffffffff,
0x040030bf, 0x000002e0,
0xb1000204, 0x00020604,
0xb1080a0c, 0x000a0e0c,
0xb10a1012, 0x000a120e,
0xb1140200, 0x00140016,
0xb8000000, 0x00000000,
Example output:
{
gsDPPipeSync(),
gsDPSetCombineLERP(TEXEL0, 0, SHADE, 0, 0, 0, 0, 1, COMBINED, 0, PRIMITIVE, 0, 0, 0, 0, COMBINED),
gsDPSetRenderMode(G_RM_FOG_SHADE_A, G_RM_AA_ZB_OPA_SURF2),
gsSPClearGeometryMode(G_LIGHTING | G_TEXTURE_GEN | G_TEXTURE_GEN_LINEAR),
gsSPSetGeometryMode(G_CULL_BACK | G_FOG),
gsDPSetPrimColor(0, 0, 0xFF, 0xFF, 0xFF, 0xFF),
gsSPVertex(0x000002E0, 12, 0),
gsSP2Triangles(0, 1, 2, 0, 1, 3, 2, 0),
gsSP2Triangles(4, 5, 6, 0, 5, 7, 6, 0),
gsSP1Quadrangle(5, 8, 9, 7, 0),
gsSP1Quadrangle(10, 1, 0, 11, 0),
gsSPEndDisplayList(),
}
The input consists of any number of Gfx
packets, and the output is the
decompiled macros in plain-text. The endianness and microcode type of the input
can be set using gfxd_endian
and gfxd_target
.
Several methods of doing I/O are available. No method is selected by default, meaning there will be no input, and any output will be discarded.
Use the buffer pointed to by buf
, of size
bytes.
Use read()
/ write()
with the provided file descriptor, fd
.
Use the provided callback function, fn
. fn
should copy at most count
bytes to/from buf
, and return the number of bytes actually copied. The input
callback should return 0 to signal end of input.
The macro handler function is responsible for writing the output of each
decompiled macro. The default macro handler is gfxd_macro_dflt
, but this can
be changed with gfxd_macro_fn
. The new handler can extend the default
function by calling gfxd_macro_dflt
within it, or it can override it
completely.
The default macro handler. Outputs the macro name, dynamic display list pointer
if one has been specified, and then each argument in order using the function
registered using gfxd_arg_fn
(gfxd_arg_dflt
by default), and returns zero.
Because it is designed to be extended, it only outputs the macro text, without
any whitespace or punctuation before or after. When this function is used as
the sole macro handler, it will output the entire display list on one line
without any separation between macros, which is probably not what you want.
Set fn
to be the macro handler function. fn
can be null, in which case the
handler is reset to the default. If fn
returns a value other than 0, execution
stops (see gfxd_execute
).
The default argument handler for gfxd_macro_dflt
. For the argument with index
arg_num
, calls gfxd_arg_callbacks
, and prints the argument value if the
callback returns zero, or if there is no callback for the given argument.
Set fn
to be the argument handler function, called by gfxd_macro_dflt
,
for each argument in the current macro, not counting the dynamic display list
pointer if one has been specified. fn
can be null, in which case the handler
is reset to the default. This only affects the output of gfxd_macro_dflt
, and
has no observable effect if gfxd_macro_dflt
is overridden (not extended).
Callbacks can be registered that will be executed when an argument of a certain
type is encountered. The default argument handler gfxd_arg_dflt
will execute
callbacks as needed using gfxd_arg_callbacks
. If a callback returns non-zero,
gfxd_arg_dflt
will not output anything. This is to allow callbacks to
override the default argument output. Otherwise, gfxd_arg_dflt
will output
the argument value after the callback function's output.
Examines the argument with index arg_num
and executes the callback function
for that argument type, if such a callback is supported and has been
registered. This function returns the value that was returned by the callback
function. If no callback function has been registered for the argument type,
zero is returned.
Most argument callbacks have some extra parameters containing information that
might be relevant to the argument that triggered the callback. The extra
information is extracted only from the current macro, as gfxd does not retain
any context information from previous or subsequent macros. If any of the extra
parameter values is not available in the current macro, the value for that
parameter is substituted with -1
for signed parameters, and zero for unsigned
parameters.
Set the callback function for palette arguments. The argument type is
gfxd_Tlut
. The palette index is in idx
and the number of colors in count
.
typedef int gfxd_timg_fn_t(uint32_t timg, int32_t fmt, int32_t siz, int32_t width, int32_t height, int32_t pal)
Set the callback function for texture arguments. The argument type is
gfxd_Timg
. The image format is in fmt
and siz
, the dimensions in width
and height
, and the palette index in pal
.
Set the callback function for frame buffer arguments. The argument type is
gfxd_Cimg
. The image format is in fmt
and siz
, and the horizontal
resolution in width
.
Set the callback function for depth buffer arguments. The argument type is
gfxd_Zimg
.
Set the callback function for display list arguments. The argument type is
gfxd_Dl
.
Set the callback function for matrix arguments. The argument type is
gfxd_Mtxptr
.
Set the callback function for lookat array arguments. The argument type is
gfxd_Lookatptr
. The number of lookat structures (1 or 2) is in count
.
Set the callback function for diffuse (Light *
) or ambient (Ambient *
)
light arguments. The argument type is gfxd_Lightptr
.
Set the callback function for Lights_M_ arguments. The argument type is
gfxd_Lightsn
. The number of diffuse lights used is in num
.
Set the callback function for segment base arguments. The argument type is
gfxd_Segptr
. The segment number is in num
.
Set the callback function for vertex array arguments. The argument type is
gfxd_Vtxptr
. The number of vertex structures is in num
.
Set the callback function for viewport arguments. The argument type is
gfxd_Vp
.
Set the callback function for microcode text arguments. The argument type is
gfxd_Uctext
. The size of the text segment is in size
.
Set the callback function for microcode data arguments. The argument type is
gfxd_Ucdata
. The size of the data segment is in size
.
Set the callback function for generic pointer arguments. The argument type is
gfxd_Dram
. The size of the data is in size
.
These functions control general input and output settings.
Select ucode
as the target microcode. ucode
can be gfxd_f3d
, gfxd_f3db
,
gfxd_f3dex
, gfxd_f3dexb
, or gfxd_f3dex2
. The microcode must be selected
before gfxd_execute
, as no microcode is selected by default.
Select endian
as the endianness of the input, and wordsize
as the size of
each word in number of bytes. endian
can be gfxd_endian_big
,
gfxd_endian_little
, or gfxd_endian_host
(the endianness of the host
machine). wordsize
can be 1, 2, 4, or 8. Big endian is selected by default,
with a word size of 4.
Enable or disable the use of dynamic g
macros instead of static gs
macros,
and select the dynamic display list pointer argument to be used. arg
will be
used by gfxd_macro_dflt
as the first argument to dynamic macros. If arg
is
null, dynamic macros are disabled, and gs
macros are used. Also affects the
result of gfxd_macro_name
, as it will return either the dynamic or static
version of the macro name as selected by this setting.
Enable or disable the feature specified by cap
. Can be one of the following;
gfxd_stop_on_invalid
: Stop execution when encountering an invalid macro. Enabled by default.gfxd_stop_on_end
: Stop execution when encountering aSPBranchList
orSPEndDisplayList
. Enabled by default.gfxd_emit_dec_color
: Print color components as decimal instead of hexadecimal. Disabled by default.gfxd_emit_q_macro
: Print fixed-point conversionq
macros for fixed-point values. Disabled by default.gfxd_emit_ext_macro
: Emit non-standard macros. Some commands are valid (though possibly meaningless), but have no macros associated with them, such as a standaloneG_RDPHALF_1
. When this feature is enabled, such a command will produce a non-standardgsDPHalf1
macro instead of a raw hexadecimal command. Also enables some non-standard multi-packet texture loading macros. Disabled by default.
Set or get a generic pointer that can be used to pass user-defined data in and out of callback functions.
Decompilation is started using the gfxd_execute
function. When gfxd is
executing (i.e. after gfxd_execute
has been entered, and before it returns),
the general settings and the I/O settings should not be changed.
Start executing gfxd with the current settings. For each macro, the macro
handler registered with gfxd_macro_fn
is called. Execution ends when the
input ends, the macro handler returns non-zero, when an invalid macro is
encountered and gfxd_stop_on_invalid
is enabled, or when SPBranchList
or
SPEndDisplayList
is encountered and gfxd_stop_on_end
is enabled. If
execution ends due to an invalid macro, -1
is returned. If execution ends
because the macro handler returns non-zero, the return value from the macro
handler is returned. Otherwise zero is returned.
The following functions can be used to obtain information about the current
macro and its arguments. They should only be used in custom handlers and
callbacks from within gfxd_execute
. If used elsewhere, their behavior is
undefined.
Returns the offset in the input data of the current macro. The offset starts
at zero when gfxd_execute
is called.
Returns the number of Gfx
packets within the current macro.
Run fn
for each individual sub-packet the current macro is made up of. During
execution of fn
, the current sub-packet becomes the current macro that is
used by other macro information functions. If the current macro is made up of
only a single packet it is processed as a single sub-packet, there is no need
to check if the current macro is a multi-packet macro. If at any point fn
returns a value other than 0, the remaining sub-packets are skipped and the
return value of fn
is returned. If fn
is null no processing is done and 0 is
returned.
Returns a pointer to the input data for the current macro. The data is not
byte-swapped. The data has a length of sizeof(Gfx) * gfxd_macro_packets()
.
Returns a number that uniquely identifies the current macro. The number will
be one of the constants in gfxd.h
.
Returns the name of the current macro. If the macro does not have a name (i.e.
it's invalid), null is returned. If a dynamic display list pointer has been
specified, the dynamic g
version is returned. Otherwise the static gs
version is returned. The returned pointer is invalidated by a subsequent call
to gfxd_macro_name
.
Returns the number of arguments to the current macro, not including a dynamic display list pointer if one has been specified.
Returns a number that identifies the type of the argument with index arg_num
.
The number will be one of the constants in gfxd.h
.
Returns the name of the argument with index arg_num
. Argument names are not
canonical, nor are they needed for macro disassembly, but they can be useful
for informational and diagnostic purposes.
Returns the data format of the argument with index arg_num
. The return value
will be gfxd_argfmt_i
for int32_t
, gfxd_argfmt_u
for uint32_t
, or
gfxd_argfmt_f
for float
. When accessing the value of the argument with
gfxd_arg_value
, the member with the corresponding type should be used.
Returns a pointer to the value of the argument with index arg_num
. The value
is a union of type gfxd_value_t
with the following layout;
typedef union
{
int32_t i;
uint32_t u;
float f;
} gfxd_value_t
Returns a pointer to the value of the argument that is of type
, and has order
idx
in all arguments of that type. An idx
of zero returns the first
argument that has the specified type. If there is no argument with the given
type and order, null is returned.
Returns non-zero if the argument with index arg_num
is "valid", for some
definition of valid. An invalid argument generally means that the disassembler
found inconsistencies in the input data, or that the data can not be reproduced
by the current macro type. The argument still has a value that can be printed,
though the value is not guaranteed to make any sense.
When the default handlers are overridden or extended, the custom handler functions will want to do some output of their own. The following methods are available for inserting custom text into the gfxd output.
Insert count
bytes from the buffer at buf
into the output. The number of
characters written is returned.
Insert the null-terminated string at str
into the output. The number of
characters written is returned.
Insert the printf-formatted string described by fmt
and additional arguments
into the output. Limited to 255 characters. The number of characters written is
returned.
Insert the type-formatted value into the output. The type should be one of the
constants in gfxd.h
. The number of characters written is returned. The
macro argument with index n
can be printed with
gfxd_print_value(gfxd_arg_type(n), gfxd_arg_value(n))
.