Roman971
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # N64 Gfx Decompilation (F3DEX & S2DEX) This guide focuses on the decompilation of N64 graphics code from ASM or raw data to C. ## Display Lists A display list is simply a list of commands indicating to the RSP what should be rendered on the screen every frame. ### Structure Contrary to common misconceptions, display lists do not have any specific command to identify their start, but they do have a dedicated command used to indicate their end. Lists are usually pre-processed and used as data, but they can also be generated dynamically in code and inserted in display list buffers at run time. ### Commands A command (aka a display list entry) corresponds to an element of type `Gfx`, which has a size of 64 bits (8 bytes). This type is the union of many different structs but for the purpose of general decompilation, we can view it as 2 words (w0 and w1). The purpose of each of the 64 bits in the command varies from command to command, but the first byte is always used to indicate the type of the command (for example, the command mentioned previously to end display lists is `G_ENDDL` which corresponds to the byte `0xDF`). ### Macros In C, all commands are generated through the use of various macros described in files such as `gbi.h` and `gs2dex.h`. Unfortunately, there isn't a 1:1 mapping between command types and macros. Different macros can often generate the same type of command with different parameters. There are also many macros which generate a sequence of commands, acting as wrappers. Wrapper macros aren't necessarily needed for decompilation and matching purposes, but they should be used when possible since they are likely to be what was originally in the source. Every macro has 2 variants: one for static, pre-processed, commands (starting with `gsSP` or `gsDP`), and one for dynamic commands in code (starting with `gSP` or `gDP`). The only difference between the 2 variants is that the dynamic version takes an additional first argument as the pointer where the command should be inserted. When using dynamic variants, the pointer should almost always be post-incremented when provided in the macro (eg. `gDPPipeSync(displayListPointer++);`). This is the pattern used in most cases and is often required for matching decompilation. It's also required in order to use macros which generate multiple display lists since the incrementation has to be applied multiple times inside the macro. ## Decompiling Commands ### Static (Data) Decompiling display lists used as data is quite straightforward. Since they are processed at compile time, they are usually just arrays of raw `Gfx` elements. In order to translate the 8 hex bytes of a command to the right macro, you can often just use glankk's `gfxdis` CLI tool included in his [N64 tools](https://github.com/glankk/n64). It almost always manages to decompile command lists to proper macros and parameters. Note that you don't need to install the whole N64 toolchain, only the programs and their dependencies. When using `gfxdis`, you can provide the data inline using `-d` or provide a file to parse using `-f` (with an optional offset to start from). Other recommended options are: `-dc` if you want colors to use decimal values, and `-x` if you don't want `qu` macros to be used for fixed point values. In most cases, you want to use `gfxdis.f3dex2` which is based on F3DEX2's `gbi.h`, but other common GBI versions should be supported if you need that. Otherwise you can always fall back to identifying each element of the command and map it to the right macro/parameter, but this process can be very tedious for some commands. Refer to header files such as `gbi.h`/`gs2dex.h` or online documentation if you have to do this anyway. Here is a basic example of a static display list decompiled: ```c= Gfx displayList[] = { gsDPPipeSync(), gsDPSetPrimColor(0, 0, 0x80, 0x80, 0x80, 0x80), gsDPSetEnvColor(0x80, 0x80, 0x80, 0x80), gsSPEndDisplayList(), }; ``` ### Dynamic (Code) Decompiling dynamic commands used inside code can be a lot more challenging, especially if they are generated from variables and not just constants, but there are a few reliable steps you can follow. #### Identifying a command If you are using [mips_to_c](https://github.com/matt-kempster/mips_to_c) or a similar decompiler, the raw code of a dynamic command will look something like this for example: ```c= temp = pointer; pointer = (void *) (temp + 8); temp->unk4 = 0x80808080; temp->unk0 = 0xfb000000; ``` This code is fairly simple because both words (unk0 and unk4) are made of constants, but it may look a lot more complicated if they are composed of multiple variables with bitwise operations like it's often the case. Of course this is not how it looked originally so you need to replace it with a macro that includes the right parameters and the post-incremented pointer like we said before. #### Translating it to a raw macro For this purpose, you can essentially use the techniques explained in the [Static](#Static-Data) part. Simply take the `unk0` and `unk4` words, put them together to make a raw command, and use `gfxdis` or manual analysis to figure out the corresponding static `gs` macro. If a word (or parts of it) is generated from variables, you would want to stub these parts with random values to figure which arguments of the macro each variable belongs to. This is usually as simple as doing [unk0/a random word] to stub an address, but some cases may require trial and error to figure out where each variable should be and if it should be provided with extra computations (eg. casting or masking it). For the example given above, we can typically run the following command: ```bash= gfxdis.f3dex2 -x -d fb00000080808080 ``` And it will output appropriate macro and parameters: ```c= gsDPSetEnvColor(0x80, 0x80, 0x80, 0x80) ``` #### Turning it into a pointer-based macro Once you have this macro, you will want to use its dynamic variant instead, which will handle the pointer part. For this, simply change the `gs` prefix to `g` and add the post-incremented pointer as a first argument. In our example, we would end up with this: ```c= gDPSetEnvColor(pointer++, 0x80, 0x80, 0x80, 0x80); ``` #### Some more complex examples * gSPMatrix with a function call ```c= temp = ptr; ptr = (void *) (temp + 8); temp->unk4 = func_1234(); temp->unk0 = 0xda380003; ``` By simply stubbing the `func_1234()` function call with an arbitrary `0x12345678`, we can use `gfxdis` like this: ```bash= gfxdis.f3dex2 -x -d da38000312345678 ``` To get the following raw macro: ```c= gsSPMatrix(0x12345678, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW) ``` And based on this, we can build the complete pointer-based macro: ```c= gSPMatrix(ptr++, func_1234(), G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); ``` We can also conclude that `func_1234` returns a pointer to a `Mtx` since that's the type of pointer expected by the `gSPMatrix` macro. * gDPSetPrimColor with variables ```c= temp = ptr; ptr = (void *) (temp + 8); temp->unk0 = 0xfa000000; temp->unk4 = (var1 << 0x18) | (var2 << 0x10) | (var3 << 8) | var4; ``` This one is more complicated because we have 4 variables merged in 1 word, but from the shifts we can identify how they are used as the 4 bytes that form the word. So we can stub them with identifiable constants and use `gfxdis` to translate it: ```bash= gfxdis.f3dex2 -x -d fa00000022446688 ``` To get the following raw macro: ```c= gsDPSetPrimColor(0, 0, 0x22, 0x44, 0x66, 0x88) ``` And based on this, we can build the complete pointer-based macro: ```c= gDPSetPrimColor(ptr++, 0, 0, var1, var2, var3, var4); ``` Using existing documentation on the arguments expected by `gDPSetPrimColor` (eg. N64 developer guides), we can also identify the variables as red/green/blue/alpha intensities. ## Advanced Patterns Some graphic commands are generated through patterns that are more delicate to deal with, or at least more obscure to understand at first, so here I'll try to share what I know about them so far. ### Texture Loading To be done. ### Hiliting To be done. ### Lighting To be done. ### S2DEX To be done.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully