subrepo:
  subdir:   "ZAPDTR"
  merged:   "a53a53ea4"
upstream:
  origin:   "https://github.com/HarbourMasters/ZAPDTR.git"
  branch:   "master"
  commit:   "a53a53ea4"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"
This commit is contained in:
M4xw 2022-03-22 02:54:48 +01:00
commit 5dda5762ba
217 changed files with 47152 additions and 0 deletions

View file

@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = git@github.com:glankk/libgfxd.git
branch = master
commit = 008f73dca8ebc9151b205959b17773a19c5bd0da
parent = c92cfda733aa740a53129a21f69c8abb08a465c7
method = merge
cmdver = 0.4.3

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016-2021 glank (glankk@github.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,25 @@
CFLAGS = -Wall -O2 -g
UC_OBJ = uc_f3d.o uc_f3db.o uc_f3dex.o uc_f3dexb.o uc_f3dex2.o
OBJ = gfxd.o $(UC_OBJ)
LIB = libgfxd.a
CPPFLAGS-$(MT) += -DCONFIG_MT
CPPFLAGS += $(CPPFLAGS-y)
.PHONY: all
all: $(LIB)
.PHONY: clean
clean:
rm -f $(OBJ) $(LIB)
.INTERMEDIATE: $(OBJ)
$(OBJ): gbi.h gfxd.h priv.h
$(UC_OBJ): uc.c uc_argfn.c uc_argtbl.c uc_macrofn.c uc_macrotbl.c
$(LIB): $(OBJ)
$(AR) rcs $@ $^
%.o: %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<

View file

@ -0,0 +1,478 @@
## Installing
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 usage
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(),
}
```
## Input/output methods
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.
---
##### `void gfxd_input_buffer(const void *buf, int size)`
##### `void gfxd_output_buffer(char *buf, int size)`
Use the buffer pointed to by `buf`, of `size` bytes.
---
##### `void gfxd_input_fd(int fd)`
##### `void gfxd_output_fd(int fd)`
Use `read()` / `write()` with the provided file descriptor, `fd`.
---
##### `typedef int gfxd_input_fn_t(void *buf, int count)`
##### `typedef int gfxd_output_fn_t(const char *buf, int count)`
##### `void gfxd_input_callback(gfxd_input_fn_t *fn)`
##### `void gfxd_output_callback(gfxd_output_fn_t *fn)`
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.
## Handlers
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.
---
##### `int gfxd_macro_dflt()`
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.
---
##### `void gfxd_macro_fn(gfxd_macro_fn_t *fn)`
Set `fn` to be the macro handler function. `fn` can be null, in which case the
handler is reset to the default.
---
##### `void gfxd_arg_dflt(int arg_num)`
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.
---
##### `void gfxd_arg_fn(gfxd_arg_fn_t *fn)`
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).
## Argument callbacks
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.
---
##### `int gfxd_arg_callbacks(int arg_num)`
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.
---
##### `typedef int gfxd_tlut_fn_t(uint32_t tlut, int32_t idx, int32_t count)`
##### `void gfxd_tlut_callback(gfxd_tlut_fn_t *fn)`
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)`
##### `void gfxd_timg_callback(gfxd_timg_fn_t *fn)`
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`.
---
##### `typedef int gfxd_cimg_fn_t(uint32_t cimg, int32_t fmt, int32_t siz, int32_t width)`
##### `void gfxd_cimg_callback(gfxd_cimg_fn_t *fn)`
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`.
---
##### `typedef int gfxd_zimg_fn_t(uint32_t zimg)`
##### `void gfxd_zimg_callback(gfxd_zimg_fn_t *fn)`
Set the callback function for depth buffer arguments. The argument type is
`gfxd_Zimg`.
---
##### `typedef int gfxd_dl_fn_t(uint32_t dl)`
##### `void gfxd_dl_callback(gfxd_dl_fn_t *fn)`
Set the callback function for display list arguments. The argument type is
`gfxd_Dl`.
---
##### `typedef int gfxd_mtx_fn_t(uint32_t mtx)`
##### `void gfxd_mtx_callback(gfxd_mtx_fn_t *fn)`
Set the callback function for matrix arguments. The argument type is
`gfxd_Mtxptr`.
---
##### `typedef int gfxd_lookat_fn_t(uint32_t lookat, int32_t count)`
##### `void gfxd_lookat_callback(gfxd_lookat_fn_t *fn)`
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`.
---
##### `typedef int gfxd_light_fn_t(uint32_t light, int32_t count)`
##### `void gfxd_light_callback(gfxd_light_fn_t *fn)`
Set the callback function for light array arguments. The argument type is
`gfxd_Lightptr`. The number of light structures is in `count`.
---
##### `typedef int gfxd_seg_fn_t(uint32_t seg, int32_t num)`
##### `void gfxd_seg_callback(gfxd_seg_fn_t *fn)`
Set the callback function for segment base arguments. The argument type is
`gfxd_Segptr`. The segment number is in `num`.
---
##### `typedef int gfxd_vtx_fn_t(uint32_t vtx, int32_t num)`
##### `void gfxd_vtx_callback(gfxd_vtx_fn_t *fn)`
Set the callback function for vertex array arguments. The argument type is
`gfxd_Vtxptr`. The number of vertex structures is in `num`.
---
##### `typedef int gfxd_vp_fn_t(uint32_t vp)`
##### `void gfxd_vp_callback(gfxd_vp_fn_t *fn)`
Set the callback function for viewport arguments. The argument type is
`gfxd_Vp`.
---
##### `typedef int gfxd_uctext_fn_t(uint32_t text, uint32_t size)`
##### `void gfxd_uctext_callback(gfxd_uctext_fn_t *fn)`
Set the callback function for microcode text arguments. The argument type is
`gfxd_Uctext`. The size of the text segment is in `size`.
---
##### `typedef int gfxd_ucdata_fn_t(uint32_t data, uint32_t size)`
##### `void gfxd_ucdata_callback(gfxd_ucdata_fn_t *fn)`
Set the callback function for microcode data arguments. The argument type is
`gfxd_Ucdata`. The size of the data segment is in `size`.
---
##### `typedef int gfxd_dram_fn_t(uint32_t dram, uint32_t size)`
##### `void gfxd_dram_callback(gfxd_dram_fn_t *fn)`
Set the callback function for generic pointer arguments. The argument type is
`gfxd_Dram`. The size of the data is in `size`.
## General settings
These functions control general input and output settings.
---
##### `void gfxd_target(gfxd_ucode_t ucode)`
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.
---
##### `void gfxd_endian(int endian, int wordsize)`
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.
---
##### `void gfxd_dynamic(const char *arg)`
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.
---
##### `void gfxd_enable(int cap)`
##### `void gfxd_disable(int cap)`
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 a `SPBranchList` or
`SPEndDisplayList`. 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 conversion `q` 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 standalone `G_RDPHALF_1`. When this feature is enabled, such a command will
produce a non-standard `gsDPHalf1` macro instead of a raw hexadecimal command.
Also enables some non-standard multi-packet texture loading macros. Disabled by
default.
---
##### `void gfxd_udata_set(void *ptr)`
##### `void *gfxd_udata_get(void)`
Set or get a generic pointer that can be used to pass user-defined data in and
out of callback functions.
## Execution
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.
---
##### `int gfxd_execute()`
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.
## Macro information
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.
---
##### `int gfxd_macro_offset()`
Returns the offset in the input data of the current macro. The offset starts
at zero when `gfxd_execute` is called.
---
##### `int gfxd_macro_packets()`
Returns the number of `Gfx` packets within the current macro.
---
##### `const void *gfxd_macro_data()`
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()`.
---
##### `int gfxd_macro_id()`
Returns a number that uniquely identifies the current macro. The number will
be one of the constants in `gfxd.h`.
---
##### `const char *gfxd_macro_name()`
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`.
---
##### `int gfxd_arg_count()`
Returns the number of arguments to the current macro, not including a dynamic
display list pointer if one has been specified.
---
##### `int gfxd_arg_type(int arg_num)`
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`.
---
##### `const char *gfxd_arg_name(int arg_num)`
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.
---
##### `int gfxd_arg_fmt(int arg_num)`
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.
---
##### `const gfxd_value_t *gfxd_arg_value(int arg_num)`
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
```
---
##### `const gfxd_value_t *gfxd_value_by_type(int type, int idx)`
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.
---
##### `int gfxd_arg_valid(int arg_num)`
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.
## Custom output
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.
---
##### `int gfxd_write(const void *buf, int count)`
Insert `count` bytes from the buffer at `buf` into the output. The number of
characters written is returned.
---
##### `int gfxd_puts(const char *str)`
Insert the null-terminated string at `str` into the output. The number of
characters written is returned.
---
##### `int gfxd_printf(const char *fmt, ...)`
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.
---
##### `int gfxd_print_value(int type, const gfxd_value_t *value)`
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))`.

3838
ZAPDTR/lib/libgfxd/gbi.h Normal file

File diff suppressed because it is too large Load diff

863
ZAPDTR/lib/libgfxd/gfxd.c Normal file
View file

@ -0,0 +1,863 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
# include <io.h>
# define read _read
# define write _write
#else
# include <unistd.h>
#endif
#include "gbi.h"
#include "gfxd.h"
#include "priv.h"
static TLOCAL struct gfxd_state state;
static int buffer_input_fn(void *buf, int count)
{
if (count > config.input_buf_size)
count = config.input_buf_size;
memcpy(buf, config.input_buf, count);
config.input_buf += count;
config.input_buf_size -= count;
return count;
}
static int buffer_output_fn(const char *buf, int count)
{
if (count > config.output_buf_size)
count = config.output_buf_size;
memcpy(config.output_buf, buf, count);
config.output_buf += count;
config.output_buf_size -= count;
return count;
}
static int fd_input_fn(void *buf, int count)
{
return read(config.input_fd, buf, count);
}
static int fd_output_fn(const char *buf, int count)
{
return write(config.output_fd, buf, count);
}
static void swap_words(Gfx *gfx)
{
uint8_t b[8];
uint8_t *pw = (void *) gfx;
uint8_t *pb = b;
int endian = config.endian;
int wordsize = config.wordsize;
for (int i = 0; i < 8 / wordsize; i++)
{
if (endian == gfxd_endian_host)
{
switch (wordsize)
{
case 1:
{
uint8_t w = *(uint8_t *) pw;
*pb++ = w >> 0;
break;
}
case 2:
{
uint16_t w = *(uint16_t *) pw;
*pb++ = w >> 8;
*pb++ = w >> 0;
break;
}
case 4:
{
uint32_t w = *(uint32_t *) pw;
*pb++ = w >> 24;
*pb++ = w >> 16;
*pb++ = w >> 8;
*pb++ = w >> 0;
break;
}
case 8:
{
uint64_t w = *(uint64_t *) pw;
*pb++ = w >> 56;
*pb++ = w >> 48;
*pb++ = w >> 40;
*pb++ = w >> 32;
*pb++ = w >> 24;
*pb++ = w >> 16;
*pb++ = w >> 8;
*pb++ = w >> 0;
break;
}
}
}
else
{
for (int j = 0; j < wordsize; j++)
{
if (endian == gfxd_endian_little)
*pb++ = pw[wordsize - 1 - j];
else
*pb++ = pw[j];
}
}
pw += wordsize;
}
gfx->hi = ((uint32_t) b[0] << 24)
| ((uint32_t) b[1] << 16)
| ((uint32_t) b[2] << 8)
| ((uint32_t) b[3] << 0);
gfx->lo = ((uint32_t) b[4] << 24)
| ((uint32_t) b[5] << 16)
| ((uint32_t) b[6] << 8)
| ((uint32_t) b[7] << 0);
}
static void get_more_input(void)
{
if (state.end_input != 0)
return;
char *recv_buf = (void *) &state.gfx[0];
while (state.n_gfx < sizeof(state.gfx) / sizeof(state.gfx[0]))
{
int n_read = sizeof(state.gfx) - state.n_byte;
n_read = config.input_fn(&recv_buf[state.n_byte], n_read);
if (n_read == 0)
return;
state.n_byte += n_read;
while (state.n_gfx < state.n_byte / sizeof(Gfx))
{
Gfx gfx = state.gfx[state.n_gfx];
gfxd_macro_t *m = &state.macro[state.n_gfx];
swap_words(&gfx);
int ret = config.ucode->disas_fn(m, gfx.hi, gfx.lo);
if (ret != 0 && config.stop_on_invalid != 0)
{
state.end_input = 1;
state.ret = ret;
return;
}
state.n_gfx++;
}
}
}
static int32_t typed_arg_i(int type, int idx)
{
const gfxd_value_t *v = gfxd_value_by_type(type, idx);
if (v != NULL)
return v->i;
else
return -1;
}
static uint32_t typed_arg_u(int type, int idx)
{
const gfxd_value_t *v = gfxd_value_by_type(type, idx);
if (v != NULL)
return v->u;
else
return 0;
}
TLOCAL struct gfxd_config config =
{
.ucode = NULL,
.endian = gfxd_endian_big,
.wordsize = 4,
.arg = NULL,
.stop_on_invalid = 1,
.stop_on_end = 1,
.emit_dec_color = 0,
.emit_q_macro = 0,
.emit_ext_macro = 0,
.input_buf = NULL,
.input_buf_size = 0,
.input_fn = &buffer_input_fn,
.output_buf = NULL,
.output_buf_size = 0,
.output_fn = &buffer_output_fn,
.macro_fn = &gfxd_macro_dflt,
.arg_fn = &gfxd_arg_dflt,
.tlut_fn = NULL,
.timg_fn = NULL,
.cimg_fn = NULL,
.zimg_fn = NULL,
.dl_fn = NULL,
.mtx_fn = NULL,
.lookat_fn = NULL,
.light_fn = NULL,
.seg_fn = NULL,
.vtx_fn = NULL,
.vp_fn = NULL,
.uctext_fn = NULL,
.ucdata_fn = NULL,
.dram_fn = NULL,
};
void gfxd_input_buffer(const void *buf, int size)
{
config.input_buf = buf;
config.input_buf_size = size;
config.input_fn = &buffer_input_fn;
}
void gfxd_output_buffer(char *buf, int size)
{
config.output_buf = buf;
config.output_buf_size = size;
config.output_fn = &buffer_output_fn;
}
void gfxd_input_fd(int fd)
{
config.input_fd = fd;
config.input_fn = &fd_input_fn;
}
void gfxd_output_fd(int fd)
{
config.output_fd = fd;
config.output_fn = &fd_output_fn;
}
void gfxd_input_callback(gfxd_input_fn_t *fn)
{
if (fn != NULL)
config.input_fn = fn;
else
gfxd_input_buffer(NULL, 0);
}
void gfxd_output_callback(gfxd_output_fn_t *fn)
{
if (fn != NULL)
config.output_fn = fn;
else
gfxd_output_buffer(NULL, 0);
}
void gfxd_macro_fn(gfxd_macro_fn_t *fn)
{
if (fn != NULL)
config.macro_fn = fn;
else
config.macro_fn = gfxd_macro_dflt;
}
void gfxd_arg_fn(gfxd_arg_fn_t *fn)
{
if (fn != NULL)
config.arg_fn = fn;
else
config.arg_fn = gfxd_arg_dflt;
}
int gfxd_write(const void *buf, int count)
{
return config.output_fn(buf, count);
}
int gfxd_puts(const char *str)
{
return gfxd_write(str, strlen(str));
}
int gfxd_printf(const char *fmt, ...)
{
char s[256];
va_list arg;
va_start(arg, fmt);
int n = vsnprintf(s, sizeof(s), fmt, arg);
va_end(arg);
if (n > sizeof(s) - 1)
n = sizeof(s) - 1;
return gfxd_write(s, n);
}
int gfxd_print_value(int type, const gfxd_value_t *value)
{
return config.ucode->arg_tbl[type].fn(value);
}
int gfxd_macro_dflt(void)
{
gfxd_macro_t *m = &state.macro[0];
const gfxd_macro_type_t *t = &config.ucode->macro_tbl[m->id];
const char *name = gfxd_macro_name();
if (name == NULL)
{
if (config.arg != NULL)
{
gfxd_puts(config.arg);
gfxd_puts(" = ");
}
gfxd_puts("(Gfx){");
}
else
{
gfxd_puts(name);
gfxd_puts("(");
if (config.arg != NULL)
{
gfxd_puts(config.arg);
if (t->n_arg != 0)
gfxd_puts(", ");
}
}
for (int i = 0; i < t->n_arg; i++)
{
if (i != 0)
gfxd_puts(", ");
config.arg_fn(i);
}
if (name == NULL)
gfxd_puts("}");
else
gfxd_puts(")");
return 0;
}
int gfxd_arg_callbacks(int arg_num)
{
int id = gfxd_macro_id();
switch (gfxd_arg_type(arg_num))
{
case gfxd_Tlut:
{
if (config.tlut_fn != NULL)
{
int32_t num;
if (id == gfxd_DPLoadTLUT_pal16)
num = 16;
else if (id == gfxd_DPLoadTLUT_pal256)
num = 256;
else
num = typed_arg_i(gfxd_Num, 0);
return config.tlut_fn(
typed_arg_u(gfxd_Tlut, 0),
typed_arg_i(gfxd_Pal, 0),
num);
}
break;
}
case gfxd_Timg:
{
if (config.timg_fn != NULL)
{
int32_t siz = typed_arg_i(gfxd_Siz, 0);
if (siz == -1)
siz = G_IM_SIZ_4b;
return config.timg_fn(
typed_arg_u(gfxd_Timg, 0),
typed_arg_i(gfxd_Fmt, 0),
siz,
typed_arg_i(gfxd_Dim, 0),
typed_arg_i(gfxd_Dim, 1),
typed_arg_i(gfxd_Pal, 0));
}
break;
}
case gfxd_Cimg:
{
if (config.cimg_fn != NULL)
{
return config.cimg_fn(
typed_arg_u(gfxd_Cimg, 0),
typed_arg_i(gfxd_Fmt, 0),
typed_arg_i(gfxd_Siz, 0),
typed_arg_i(gfxd_Dim, 0));
}
break;
}
case gfxd_Zimg:
{
if (config.zimg_fn != NULL)
{
return config.zimg_fn(
typed_arg_u(gfxd_Zimg, 0));
}
break;
}
case gfxd_Dl:
{
if (config.dl_fn != NULL)
{
return config.dl_fn(
typed_arg_u(gfxd_Dl, 0));
}
break;
}
case gfxd_Mtxptr:
{
if (config.mtx_fn != NULL)
{
return config.mtx_fn(
typed_arg_u(gfxd_Mtxptr, 0));
}
break;
}
case gfxd_Lookatptr:
{
if (config.lookat_fn != NULL)
{
int32_t num;
if (id == gfxd_SPLookAt)
num = 2;
else
num = 1;
return config.lookat_fn(
typed_arg_u(gfxd_Lookatptr, 0),
num);
}
break;
}
case gfxd_Lightptr:
{
if (config.light_fn != NULL)
{
int32_t num;
if (id == gfxd_SPSetLights1)
num = 1;
else if (id == gfxd_SPSetLights2)
num = 2;
else if (id == gfxd_SPSetLights3)
num = 3;
else if (id == gfxd_SPSetLights4)
num = 4;
else if (id == gfxd_SPSetLights5)
num = 5;
else if (id == gfxd_SPSetLights6)
num = 6;
else if (id == gfxd_SPSetLights7)
num = 7;
else
num = 1;
return config.light_fn(
typed_arg_u(gfxd_Lightptr, 0),
num);
}
break;
}
case gfxd_Segptr:
{
if (config.seg_fn != NULL)
{
return config.seg_fn(
typed_arg_u(gfxd_Segptr, 0),
typed_arg_i(gfxd_Seg, 0));
}
break;
}
case gfxd_Vtxptr:
{
if (config.vtx_fn != NULL)
{
return config.vtx_fn(
typed_arg_u(gfxd_Vtxptr, 0),
typed_arg_i(gfxd_Num, 0));
}
break;
}
case gfxd_Vpptr:
{
if (config.vp_fn != NULL)
{
return config.vp_fn(
typed_arg_u(gfxd_Vpptr, 0));
}
break;
}
case gfxd_Uctext:
{
if (config.uctext_fn != NULL)
{
return config.uctext_fn(
typed_arg_u(gfxd_Uctext, 0),
0x1000);
}
break;
}
case gfxd_Ucdata:
{
if (config.ucdata_fn != NULL)
{
uint32_t size;
if (id == gfxd_SPLoadUcodeEx)
size = typed_arg_u(gfxd_Size, 0);
else
size = 0x800;
return config.ucdata_fn(
typed_arg_u(gfxd_Ucdata, 0),
size);
}
break;
}
case gfxd_Dram:
{
if (config.dram_fn != NULL)
{
return config.dram_fn(
typed_arg_u(gfxd_Dram, 0),
typed_arg_u(gfxd_Size, 0));
}
break;
}
}
return 0;
}
void gfxd_arg_dflt(int arg_num)
{
if (gfxd_arg_callbacks(arg_num) == 0)
{
gfxd_arg_t *a = &state.macro[0].arg[arg_num];
gfxd_print_value(a->type, &a->value);
}
}
void gfxd_tlut_callback(gfxd_tlut_fn_t *fn)
{
config.tlut_fn = fn;
}
void gfxd_timg_callback(gfxd_timg_fn_t *fn)
{
config.timg_fn = fn;
}
void gfxd_cimg_callback(gfxd_cimg_fn_t *fn)
{
config.cimg_fn = fn;
}
void gfxd_zimg_callback(gfxd_zimg_fn_t *fn)
{
config.zimg_fn = fn;
}
void gfxd_dl_callback(gfxd_dl_fn_t *fn)
{
config.dl_fn = fn;
}
void gfxd_mtx_callback(gfxd_mtx_fn_t *fn)
{
config.mtx_fn = fn;
}
void gfxd_lookat_callback(gfxd_lookat_fn_t *fn)
{
config.lookat_fn = fn;
}
void gfxd_light_callback(gfxd_light_fn_t *fn)
{
config.light_fn = fn;
}
void gfxd_seg_callback(gfxd_seg_fn_t *fn)
{
config.seg_fn = fn;
}
void gfxd_vtx_callback(gfxd_vtx_fn_t *fn)
{
config.vtx_fn = fn;
}
void gfxd_vp_callback(gfxd_vp_fn_t *fn)
{
config.vp_fn = fn;
}
void gfxd_uctext_callback(gfxd_uctext_fn_t *fn)
{
config.uctext_fn = fn;
}
void gfxd_ucdata_callback(gfxd_ucdata_fn_t *fn)
{
config.ucdata_fn = fn;
}
void gfxd_dram_callback(gfxd_dram_fn_t *fn)
{
config.dram_fn = fn;
}
void gfxd_target(gfxd_ucode_t ucode)
{
config.ucode = ucode;
}
void gfxd_endian(int endian, int wordsize)
{
config.endian = endian;
config.wordsize = wordsize;
}
void gfxd_dynamic(const char *arg)
{
config.arg = arg;
}
void gfxd_enable(int cap)
{
switch (cap)
{
case gfxd_stop_on_invalid:
config.stop_on_invalid = 1;
break;
case gfxd_stop_on_end:
config.stop_on_end = 1;
break;
case gfxd_emit_dec_color:
config.emit_dec_color = 1;
break;
case gfxd_emit_q_macro:
config.emit_q_macro = 1;
break;
case gfxd_emit_ext_macro:
config.emit_ext_macro = 1;
break;
}
}
void gfxd_disable(int cap)
{
switch (cap)
{
case gfxd_stop_on_invalid:
config.stop_on_invalid = 0;
return;
case gfxd_stop_on_end:
config.stop_on_end = 0;
return;
case gfxd_emit_dec_color:
config.emit_dec_color = 0;
break;
case gfxd_emit_q_macro:
config.emit_q_macro = 0;
break;
case gfxd_emit_ext_macro:
config.emit_ext_macro = 0;
break;
}
}
void gfxd_udata_set(void *ptr)
{
config.udata = ptr;
}
void *gfxd_udata_get(void)
{
return config.udata;
}
int gfxd_execute(void)
{
state.macro_offset = 0;
state.n_byte = 0;
state.n_gfx = 0;
state.end_input = 0;
state.ret = 0;
for (;;)
{
get_more_input();
if (state.n_gfx == 0)
break;
gfxd_macro_t *m = &state.macro[0];
config.ucode->combine_fn(m, state.n_gfx);
const gfxd_macro_type_t *t = &config.ucode->macro_tbl[m->id];
if (t->ext != 0 && config.emit_ext_macro == 0)
{
Gfx gfx = state.gfx[0];
swap_words(&gfx);
t = &config.ucode->macro_tbl[gfxd_Invalid];
t->disas_fn(m, gfx.hi, gfx.lo);
}
int ret = config.macro_fn();
if (ret != 0)
{
state.ret = ret;
break;
}
if (config.stop_on_end != 0
&& (m->id == gfxd_SPBranchList
|| m->id == gfxd_SPEndDisplayList))
{
break;
}
int n_pop = config.ucode->macro_tbl[m->id].n_gfx;
int n_rem = state.n_gfx - n_pop;
{
int n_byte = n_rem * sizeof(gfxd_macro_t);
memmove(&state.macro[0], &state.macro[n_pop], n_byte);
state.n_gfx = n_rem;
}
{
int n_byte = n_rem * sizeof(Gfx);
memmove(&state.gfx[0], &state.gfx[n_pop], n_byte);
state.n_byte = n_byte;
}
state.macro_offset += n_pop * sizeof(Gfx);
}
return state.ret;
}
int gfxd_macro_offset(void)
{
return state.macro_offset;
}
int gfxd_macro_packets(void)
{
return config.ucode->macro_tbl[state.macro[0].id].n_gfx;
}
const void *gfxd_macro_data(void)
{
return state.gfx;
}
int gfxd_macro_id(void)
{
return state.macro[0].id;
}
const char *gfxd_macro_name(void)
{
int id = state.macro[0].id;
const gfxd_macro_type_t *t = &config.ucode->macro_tbl[id];
if (t->prefix == NULL && t->suffix == NULL)
{
return NULL;
}
else
{
static TLOCAL char buf[32];
char *p = buf;
if (t->prefix != NULL)
{
const char *s = t->prefix;
while (*s != '\0')
*p++ = *s++;
}
*p++ = 'g';
if (config.arg == NULL)
*p++ = 's';
if (t->suffix != NULL)
{
const char *s = t->suffix;
while (*s != '\0')
*p++ = *s++;
}
*p++ = '\0';
return buf;
}
}
int gfxd_arg_count(void)
{
return config.ucode->macro_tbl[state.macro[0].id].n_arg;
}
int gfxd_arg_type(int arg_num)
{
return state.macro[0].arg[arg_num].type;
}
const char *gfxd_arg_name(int arg_num)
{
return state.macro[0].arg[arg_num].name;
}
int gfxd_arg_fmt(int arg_num)
{
return config.ucode->arg_tbl[state.macro[0].arg[arg_num].type].fmt;
}
const gfxd_value_t *gfxd_arg_value(int arg_num)
{
return &state.macro[0].arg[arg_num].value;
}
const gfxd_value_t *gfxd_value_by_type(int type, int idx)
{
gfxd_macro_t *m = &state.macro[0];
const gfxd_macro_type_t *t = &config.ucode->macro_tbl[m->id];
for (int i = 0; i < t->n_arg; i++)
{
gfxd_arg_t *a = &m->arg[i];
if (a->type == type)
{
if (idx == 0)
return &a->value;
else
idx--;
}
}
return NULL;
}
int gfxd_arg_valid(int arg_num)
{
return state.macro[0].arg[arg_num].bad == 0;
}

387
ZAPDTR/lib/libgfxd/gfxd.h Normal file
View file

@ -0,0 +1,387 @@
#ifndef GFXD_H
#define GFXD_H
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
enum
{
gfxd_Word, /* generic word */
gfxd_Opcode, /* command opcode (G_*) */
gfxd_Coordi, /* integer coordinate */
gfxd_Coordq, /* fractional (q10.2) coordinate */
gfxd_Pal, /* palette index */
gfxd_Tlut, /* tlut pointer */
gfxd_Timg, /* texture image pointer */
gfxd_Tmem, /* tmem address */
gfxd_Tile, /* tile index */
gfxd_Fmt, /* texture format */
gfxd_Siz, /* texture pixel size */
gfxd_Dim, /* integer dimension (width / height) */
gfxd_Cm, /* clamp and mirror flags */
gfxd_Tm, /* tile mask */
gfxd_Ts, /* tile shift */
gfxd_Dxt, /* texture dxt */
gfxd_Tag, /* generic tag */
gfxd_Pm, /* pipeline mode */
gfxd_Colorpart, /* color component */
gfxd_Color, /* color */
gfxd_Lodfrac, /* lod fraction (q0.8) */
gfxd_Cimg, /* color image pointer */
gfxd_Zimg, /* depth image pointer */
gfxd_Ac, /* alpha compare mode */
gfxd_Ad, /* alpha dither mode */
gfxd_Cd, /* color dither mode */
gfxd_Ccpre, /* color combiner preset index */
gfxd_Ccmuxa, /* color mux operand (a) */
gfxd_Ccmuxb, /* color mux operand (b) */
gfxd_Ccmuxc, /* color mux operand (c) */
gfxd_Ccmuxd, /* color mux operand (d) */
gfxd_Acmuxabd, /* alpha mux operand (a, b, or d) */
gfxd_Acmuxc, /* alpha mux operand (c) */
gfxd_Cv, /* color convert operand */
gfxd_Tc, /* texture convert mode */
gfxd_Cyc, /* cycle type */
gfxd_Zs, /* depth source mode */
gfxd_Ck, /* combine key mode */
gfxd_Keyscale, /* combine key scale */
gfxd_Keywidth, /* combine key width */
gfxd_Zi, /* integer depth */
gfxd_Rm1, /* cycle 1 render mode */
gfxd_Rm2, /* cycle 2 render mode */
gfxd_Sc, /* scissor mode */
gfxd_Td, /* texture detail mode */
gfxd_Tf, /* texture filter mode */
gfxd_Tl, /* texture LOD mode */
gfxd_Tt, /* textuure LUT mode */
gfxd_Tp, /* texture perspective mode */
gfxd_Line, /* texture line size */
gfxd_Vtx, /* vertex index */
gfxd_Vtxflag, /* vertex flag */
gfxd_Dl, /* display list pointer */
gfxd_Zraw, /* raw depth value (q16.16) */
gfxd_Dlflag, /* display list flag */
gfxd_Cr, /* clip ratio */
gfxd_Num, /* element count */
gfxd_Fogz, /* fog factor */
gfxd_Fogp, /* fog position (0 - 1000) */
gfxd_Mtxptr, /* matrix pointer */
gfxd_Gm, /* geometry mode */
gfxd_Mwo_matrix, /* matrix moveword offset */
gfxd_Linewd, /* line width (1.5 + q7.1) */
gfxd_Uctext, /* microcode text pointer */
gfxd_Ucdata, /* microcode data pointer */
gfxd_Size, /* data size */
gfxd_Lookatptr, /* lookat pointer */
gfxd_Mtxparam, /* matrix param */
gfxd_Mtxstack, /* matrix param (stack select only) */
gfxd_Mwo_point, /* vertex moveword offset */
gfxd_Wscale, /* w-component scale (perspnorm) */
gfxd_Seg, /* segment number */
gfxd_Segptr, /* segment pointer */
gfxd_Lightsn, /* dereferenced Lighstn pointer */
gfxd_Numlights, /* light count (NUMLIGHTS_*) */
gfxd_Lightnum, /* light number (LIGHT_*) */
gfxd_Lightptr, /* light pointer */
gfxd_Tcscale, /* texture coordinate scale */
gfxd_Switch, /* on-off value */
gfxd_St, /* vertex coordinate (q10.5) */
gfxd_Stdelta, /* vertex coordinate delta (q5.10) */
gfxd_Vtxptr, /* vertex pointer */
gfxd_Vpptr, /* viewport pointer */
gfxd_Dram, /* generic dram address */
gfxd_Sftlo, /* othermode lo shift */
gfxd_Othermodelo, /* othermode lo value */
gfxd_Sfthi, /* othermode hi shift */
gfxd_Othermodehi, /* othermode hi value */
gfxd_Mw, /* moveword index */
gfxd_Mwo, /* moveword offset */
gfxd_Mwo_clip, /* clip ratio moveword offset */
gfxd_Mwo_lightcol, /* light color moveword offset */
gfxd_Mv, /* movemem index */
gfxd_Mvo, /* movemem offset */
gfxd_Dmem, /* dmem address */
gfxd_Dmaflag, /* dma io flag */
};
enum
{
gfxd_Invalid,
gfxd_DPFillRectangle,
gfxd_DPFullSync,
gfxd_DPLoadSync,
gfxd_DPTileSync,
gfxd_DPPipeSync,
gfxd_DPLoadTLUT_pal16,
gfxd_DPLoadTLUT_pal256,
gfxd_DPLoadMultiBlockYuvS,
gfxd_DPLoadMultiBlockYuv,
gfxd_DPLoadMultiBlock_4bS,
gfxd_DPLoadMultiBlock_4b,
gfxd_DPLoadMultiBlockS,
gfxd_DPLoadMultiBlock,
gfxd__DPLoadTextureBlockYuvS,
gfxd__DPLoadTextureBlockYuv,
gfxd__DPLoadTextureBlock_4bS,
gfxd__DPLoadTextureBlock_4b,
gfxd__DPLoadTextureBlockS,
gfxd__DPLoadTextureBlock,
gfxd_DPLoadTextureBlockYuvS,
gfxd_DPLoadTextureBlockYuv,
gfxd_DPLoadTextureBlock_4bS,
gfxd_DPLoadTextureBlock_4b,
gfxd_DPLoadTextureBlockS,
gfxd_DPLoadTextureBlock,
gfxd_DPLoadMultiTileYuv,
gfxd_DPLoadMultiTile_4b,
gfxd_DPLoadMultiTile,
gfxd__DPLoadTextureTileYuv,
gfxd__DPLoadTextureTile_4b,
gfxd__DPLoadTextureTile,
gfxd_DPLoadTextureTileYuv,
gfxd_DPLoadTextureTile_4b,
gfxd_DPLoadTextureTile,
gfxd_DPLoadBlock,
gfxd_DPNoOp,
gfxd_DPNoOpTag,
gfxd_DPPipelineMode,
gfxd_DPSetBlendColor,
gfxd_DPSetEnvColor,
gfxd_DPSetFillColor,
gfxd_DPSetFogColor,
gfxd_DPSetPrimColor,
gfxd_DPSetColorImage,
gfxd_DPSetDepthImage,
gfxd_DPSetTextureImage,
gfxd_DPSetAlphaCompare,
gfxd_DPSetAlphaDither,
gfxd_DPSetColorDither,
gfxd_DPSetCombineMode,
gfxd_DPSetCombineLERP,
gfxd_DPSetConvert,
gfxd_DPSetTextureConvert,
gfxd_DPSetCycleType,
gfxd_DPSetDepthSource,
gfxd_DPSetCombineKey,
gfxd_DPSetKeyGB,
gfxd_DPSetKeyR,
gfxd_DPSetPrimDepth,
gfxd_DPSetRenderMode,
gfxd_DPSetScissor,
gfxd_DPSetScissorFrac,
gfxd_DPSetTextureDetail,
gfxd_DPSetTextureFilter,
gfxd_DPSetTextureLOD,
gfxd_DPSetTextureLUT,
gfxd_DPSetTexturePersp,
gfxd_DPSetTile,
gfxd_DPSetTileSize,
gfxd_SP1Triangle,
gfxd_SP2Triangles,
gfxd_SP1Quadrangle,
gfxd_SPBranchLessZraw,
gfxd_SPBranchList,
gfxd_SPClipRatio,
gfxd_SPCullDisplayList,
gfxd_SPDisplayList,
gfxd_SPEndDisplayList,
gfxd_SPFogFactor,
gfxd_SPFogPosition,
gfxd_SPForceMatrix,
gfxd_SPSetGeometryMode,
gfxd_SPClearGeometryMode,
gfxd_SPLoadGeometryMode,
gfxd_SPInsertMatrix,
gfxd_SPLine3D,
gfxd_SPLineW3D,
gfxd_SPLoadUcode,
gfxd_SPLookAtX,
gfxd_SPLookAtY,
gfxd_SPLookAt,
gfxd_SPMatrix,
gfxd_SPModifyVertex,
gfxd_SPPerspNormalize,
gfxd_SPPopMatrix,
gfxd_SPPopMatrixN,
gfxd_SPSegment,
gfxd_SPSetLights1,
gfxd_SPSetLights2,
gfxd_SPSetLights3,
gfxd_SPSetLights4,
gfxd_SPSetLights5,
gfxd_SPSetLights6,
gfxd_SPSetLights7,
gfxd_SPNumLights,
gfxd_SPLight,
gfxd_SPLightColor,
gfxd_SPTexture,
gfxd_SPTextureRectangle,
gfxd_SPTextureRectangleFlip,
gfxd_SPVertex,
gfxd_SPViewport,
gfxd_DPLoadTLUTCmd,
gfxd_DPLoadTLUT,
gfxd_BranchZ,
gfxd_DisplayList,
gfxd_DPHalf1,
gfxd_DPHalf2,
gfxd_DPWord,
gfxd_DPLoadTile,
gfxd_SPGeometryMode,
gfxd_SPSetOtherMode,
gfxd_SPSetOtherModeLo,
gfxd_SPSetOtherModeHi,
gfxd_DPSetOtherMode,
gfxd_MoveWd,
gfxd_MoveMem,
gfxd_SPDma_io,
gfxd_SPDmaRead,
gfxd_SPDmaWrite,
gfxd_LoadUcode,
gfxd_SPLoadUcodeEx,
gfxd_TexRect,
gfxd_TexRectFlip,
gfxd_SPNoOp,
gfxd_Special3,
gfxd_Special2,
gfxd_Special1,
};
enum
{
gfxd_stop_on_invalid,
gfxd_stop_on_end,
gfxd_emit_dec_color,
gfxd_emit_q_macro,
gfxd_emit_ext_macro,
};
enum
{
gfxd_endian_big,
gfxd_endian_little,
gfxd_endian_host,
};
enum
{
gfxd_argfmt_i,
gfxd_argfmt_u,
gfxd_argfmt_f,
};
typedef union
{
int32_t i;
uint32_t u;
float f;
} gfxd_value_t;
typedef const struct gfxd_ucode *gfxd_ucode_t;
typedef int gfxd_input_fn_t(void *buf, int count);
void gfxd_input_buffer(const void *buf, int size);
void gfxd_input_fd(int fd);
void gfxd_input_callback(gfxd_input_fn_t *fn);
typedef int gfxd_output_fn_t(const char *buf, int count);
void gfxd_output_buffer(char *buf, int size);
void gfxd_output_fd(int fd);
void gfxd_output_callback(gfxd_output_fn_t *fn);
typedef int gfxd_macro_fn_t(void);
void gfxd_macro_fn(gfxd_macro_fn_t *fn);
gfxd_macro_fn_t gfxd_macro_dflt;
typedef void gfxd_arg_fn_t(int arg_num);
void gfxd_arg_fn(gfxd_arg_fn_t *fn);
gfxd_arg_fn_t gfxd_arg_dflt;
typedef int gfxd_tlut_fn_t(uint32_t tlut, int32_t idx, int32_t count);
void gfxd_tlut_callback(gfxd_tlut_fn_t *fn);
typedef int gfxd_timg_fn_t(uint32_t timg, int32_t fmt, int32_t siz,
int32_t width, int32_t height, int32_t pal);
void gfxd_timg_callback(gfxd_timg_fn_t *fn);
typedef int gfxd_cimg_fn_t(uint32_t cimg, int32_t fmt, int32_t siz,
int32_t width);
void gfxd_cimg_callback(gfxd_cimg_fn_t *fn);
typedef int gfxd_zimg_fn_t(uint32_t zimg);
void gfxd_zimg_callback(gfxd_zimg_fn_t *fn);
typedef int gfxd_dl_fn_t(uint32_t dl);
void gfxd_dl_callback(gfxd_dl_fn_t *fn);
typedef int gfxd_mtx_fn_t(uint32_t mtx);
void gfxd_mtx_callback(gfxd_mtx_fn_t *fn);
typedef int gfxd_lookat_fn_t(uint32_t lookat, int32_t count);
void gfxd_lookat_callback(gfxd_lookat_fn_t *fn);
typedef int gfxd_light_fn_t(uint32_t light, int32_t count);
void gfxd_light_callback(gfxd_light_fn_t *fn);
typedef int gfxd_seg_fn_t(uint32_t seg, int32_t num);
void gfxd_seg_callback(gfxd_seg_fn_t *fn);
typedef int gfxd_vtx_fn_t(uint32_t vtx, int32_t num);
void gfxd_vtx_callback(gfxd_vtx_fn_t *fn);
typedef int gfxd_vp_fn_t(uint32_t vp);
void gfxd_vp_callback(gfxd_vp_fn_t *fn);
typedef int gfxd_uctext_fn_t(uint32_t text, uint32_t size);
void gfxd_uctext_callback(gfxd_uctext_fn_t *fn);
typedef int gfxd_ucdata_fn_t(uint32_t data, uint32_t size);
void gfxd_ucdata_callback(gfxd_ucdata_fn_t *fn);
typedef int gfxd_dram_fn_t(uint32_t dram, uint32_t size);
void gfxd_dram_callback(gfxd_dram_fn_t *fn);
int gfxd_write(const void *buf, int count);
int gfxd_puts(const char *str);
int gfxd_printf(const char *fmt, ...);
int gfxd_print_value(int type, const gfxd_value_t *value);
void gfxd_target(gfxd_ucode_t ucode);
void gfxd_endian(int endian, int wordsize);
void gfxd_dynamic(const char *arg);
void gfxd_enable(int cap);
void gfxd_disable(int cap);
void gfxd_udata_set(void *ptr);
void *gfxd_udata_get(void);
int gfxd_execute(void);
int gfxd_macro_offset(void);
int gfxd_macro_packets(void);
const void *gfxd_macro_data(void);
int gfxd_macro_id(void);
const char *gfxd_macro_name(void);
int gfxd_arg_count(void);
int gfxd_arg_type(int arg_num);
const char *gfxd_arg_name(int arg_num);
int gfxd_arg_fmt(int arg_num);
const gfxd_value_t *gfxd_arg_value(int arg_num);
const gfxd_value_t *gfxd_value_by_type(int type, int idx);
int gfxd_arg_valid(int arg_num);
int gfxd_arg_callbacks(int arg_num);
extern const gfxd_ucode_t gfxd_f3d;
extern const gfxd_ucode_t gfxd_f3db;
extern const gfxd_ucode_t gfxd_f3dex;
extern const gfxd_ucode_t gfxd_f3dexb;
extern const gfxd_ucode_t gfxd_f3dex2;
#ifdef __cplusplus
}
#endif
#endif

123
ZAPDTR/lib/libgfxd/priv.h Normal file
View file

@ -0,0 +1,123 @@
#ifndef GFXD_PRIV_H
#define GFXD_PRIV_H
#include "gfxd.h"
#ifdef CONFIG_MT
# ifdef _MSC_VER
# define TLOCAL __declspec(thread)
# else
# define TLOCAL _Thread_local
# endif
#else
# define TLOCAL
#endif
#define UCFUNC static inline
#define config gfxd_config__
typedef int gfxd_argfn_t(const gfxd_value_t *v);
typedef struct
{
int fmt;
gfxd_argfn_t * fn;
} gfxd_arg_type_t;
typedef struct
{
int type;
const char * name;
gfxd_value_t value;
int bad;
} gfxd_arg_t;
typedef struct
{
int id;
gfxd_arg_t arg[18];
} gfxd_macro_t;
typedef int gfxd_disas_fn_t(gfxd_macro_t *macro, uint32_t hi, uint32_t lo);
typedef int gfxd_combine_fn_t(gfxd_macro_t *macro, int n_macro);
typedef struct
{
const char * prefix;
const char * suffix;
int opcode;
int n_arg;
int n_gfx;
gfxd_disas_fn_t * disas_fn;
gfxd_combine_fn_t * combine_fn;
int alias;
int ext;
} gfxd_macro_type_t;
struct gfxd_ucode
{
gfxd_disas_fn_t * disas_fn;
gfxd_combine_fn_t * combine_fn;
const gfxd_arg_type_t * arg_tbl;
const gfxd_macro_type_t * macro_tbl;
};
struct gfxd_state
{
int macro_offset;
Gfx gfx[9];
int n_byte;
int n_gfx;
gfxd_macro_t macro[9];
int end_input;
int ret;
};
struct gfxd_config
{
gfxd_ucode_t ucode;
int endian;
int wordsize;
const char * arg;
void * udata;
int stop_on_invalid;
int stop_on_end;
int emit_dec_color;
int emit_q_macro;
int emit_ext_macro;
const char * input_buf;
int input_buf_size;
int input_fd;
gfxd_input_fn_t * input_fn;
char * output_buf;
int output_buf_size;
int output_fd;
gfxd_output_fn_t * output_fn;
gfxd_macro_fn_t * macro_fn;
gfxd_arg_fn_t * arg_fn;
gfxd_tlut_fn_t * tlut_fn;
gfxd_timg_fn_t * timg_fn;
gfxd_cimg_fn_t * cimg_fn;
gfxd_zimg_fn_t * zimg_fn;
gfxd_dl_fn_t * dl_fn;
gfxd_mtx_fn_t * mtx_fn;
gfxd_lookat_fn_t * lookat_fn;
gfxd_light_fn_t * light_fn;
gfxd_seg_fn_t * seg_fn;
gfxd_vtx_fn_t * vtx_fn;
gfxd_vp_fn_t * vp_fn;
gfxd_uctext_fn_t * uctext_fn;
gfxd_ucdata_fn_t * ucdata_fn;
gfxd_dram_fn_t * dram_fn;
};
extern TLOCAL struct gfxd_config gfxd_config__;
#endif

54
ZAPDTR/lib/libgfxd/uc.c Normal file
View file

@ -0,0 +1,54 @@
#include <inttypes.h>
#include <string.h>
#include <stdint.h>
#include "gbi.h"
#include "gfxd.h"
#include "priv.h"
#include "uc_argfn.c"
#include "uc_argtbl.c"
#include "uc_macrofn.c"
#include "uc_macrotbl.c"
UCFUNC int disas(gfxd_macro_t *m, uint32_t hi, uint32_t lo)
{
int opcode = (hi >> 24) & 0xFF;
for (int i = 0; i < sizeof(macro_tbl) / sizeof(macro_tbl[0]); i++)
{
const gfxd_macro_type_t *t = &macro_tbl[i];
if (t->disas_fn != NULL && t->opcode == opcode)
return t->disas_fn(m, hi, lo);
}
return d_Invalid(m, hi, lo);
}
UCFUNC int combine(gfxd_macro_t *m, int num)
{
int opcode = macro_tbl[m->id].opcode;
for (int i = 0; i < sizeof(macro_tbl) / sizeof(macro_tbl[0]); i++)
{
const gfxd_macro_type_t *t = &macro_tbl[i];
if (t->combine_fn != NULL
&& t->opcode == opcode
&& (t->ext == 0 || config.emit_ext_macro != 0))
{
if (t->combine_fn(m, num) == 0)
return 0;
}
}
return -1;
}
static const struct gfxd_ucode uc =
{
.disas_fn = disas,
.combine_fn = combine,
.arg_tbl = arg_tbl,
.macro_tbl = macro_tbl,
};
const gfxd_ucode_t uc_name = &uc;

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,485 @@
static const gfxd_arg_type_t arg_tbl[] =
{
[gfxd_Word] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Opcode] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_opc,
},
[gfxd_Coordi] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_u,
},
[gfxd_Coordq] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_qu102,
},
[gfxd_Pal] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Tlut] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Timg] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Tmem] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x16,
},
[gfxd_Tile] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_tile,
},
[gfxd_Fmt] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_fmt,
},
[gfxd_Siz] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_siz,
},
[gfxd_Dim] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Cm] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_cm,
},
[gfxd_Tm] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_tm,
},
[gfxd_Ts] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ts,
},
[gfxd_Dxt] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_u,
},
[gfxd_Tag] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Pm] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_pm,
},
[gfxd_Colorpart] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_color,
},
[gfxd_Color] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Lodfrac] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_qu08,
},
[gfxd_Cimg] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Zimg] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Ac] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_ac,
},
[gfxd_Ad] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_ad,
},
[gfxd_Cd] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_cd,
},
[gfxd_Ccpre] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_ccpre,
},
[gfxd_Ccmuxa] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ccmuxa,
},
[gfxd_Ccmuxb] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ccmuxb,
},
[gfxd_Ccmuxc] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ccmuxc,
},
[gfxd_Ccmuxd] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ccmuxd,
},
[gfxd_Acmuxabd] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_acmuxabd,
},
[gfxd_Acmuxc] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_acmuxc,
},
[gfxd_Cv] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_cv,
},
[gfxd_Tc] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_tc,
},
[gfxd_Cyc] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_cyc,
},
[gfxd_Zs] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_zs,
},
[gfxd_Ck] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_ck,
},
[gfxd_Keyscale] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_color,
},
[gfxd_Keywidth] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_qs48,
},
[gfxd_Zi] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Rm1] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_rm1,
},
[gfxd_Rm2] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_rm2,
},
[gfxd_Sc] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_sc,
},
[gfxd_Td] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_td,
},
[gfxd_Tf] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_tf,
},
[gfxd_Tl] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_tl,
},
[gfxd_Tt] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_tt,
},
[gfxd_Tp] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_tp,
},
[gfxd_Line] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Vtx] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Vtxflag] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Dl] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
#if defined(F3DEX_GBI) || defined(F3DEX_GBI_2)
[gfxd_Zraw] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_qs1616,
},
#endif
[gfxd_Dlflag] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_dlf,
},
[gfxd_Cr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_cr,
},
[gfxd_Num] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Fogz] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Fogp] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Mtxptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Gm] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_gm,
},
[gfxd_Mwo_matrix] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_mwo_matrix,
},
[gfxd_Linewd] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Uctext] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Ucdata] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Size] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x16,
},
[gfxd_Lookatptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Mtxparam] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_mp,
},
[gfxd_Mtxstack] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_ms,
},
[gfxd_Mwo_point] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_mwo_point,
},
[gfxd_Wscale] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_qu016,
},
[gfxd_Seg] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x8,
},
[gfxd_Segptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Lightsn] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_lightsn,
},
[gfxd_Numlights] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
[gfxd_Lightnum] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_lightnum,
},
[gfxd_Lightptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Tcscale] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_qu016,
},
[gfxd_Switch] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_switch,
},
[gfxd_St] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_qs105,
},
[gfxd_Stdelta] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_qs510,
},
[gfxd_Vtxptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Vpptr] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Dram] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x32,
},
[gfxd_Sftlo] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_sftlo,
},
[gfxd_Othermodelo] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_othermodelo,
},
[gfxd_Sfthi] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_sfthi,
},
[gfxd_Othermodehi] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_othermodehi,
},
[gfxd_Mw] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_mw,
},
[gfxd_Mwo] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x16,
},
[gfxd_Mwo_clip] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_mwo_clip,
},
[gfxd_Mwo_lightcol] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_mwo_lightcol,
},
[gfxd_Mv] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_mv,
},
[gfxd_Mvo] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x16,
},
[gfxd_Dmem] =
{
.fmt = gfxd_argfmt_u,
.fn = argfn_x16,
},
[gfxd_Dmaflag] =
{
.fmt = gfxd_argfmt_i,
.fn = argfn_i,
},
};

View file

@ -0,0 +1,4 @@
#define uc_name gfxd_f3d
#define F3D_GBI
#include "uc.c"

View file

@ -0,0 +1,5 @@
#define uc_name gfxd_f3db
#define F3D_GBI
#define F3D_BETA
#include "uc.c"

View file

@ -0,0 +1,4 @@
#define uc_name gfxd_f3dex
#define F3DEX_GBI
#include "uc.c"

View file

@ -0,0 +1,4 @@
#define uc_name gfxd_f3dex2
#define F3DEX_GBI_2
#include "uc.c"

View file

@ -0,0 +1,5 @@
#define uc_name gfxd_f3dexb
#define F3DEX_GBI
#define F3D_BETA
#include "uc.c"

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff