Skip to content

Instantly share code, notes, and snippets.

View phire's full-sized avatar

Scott Mansell phire

View GitHub Profile
@phire
phire / cavlc.rs
Last active December 19, 2025 06:27
Attempt at creating an efficient h264 CAVLC decoder for the n64 (which doesn't have CLZ, and really hates cache misses)
/// Like decode_huffman_lookup, but the first level is unrolled, and much bigger
/// Any code that fits in 5 bits and decodes to 5 bits or less can be done in a single table lookup
pub fn decode_huffman_unrolled(buffer: u64, mut table: &[u8]) -> (u32, usize) {
// Lookup first 5 bits (32 entry table)
let idx = buffer >> 59; // 1 cycle
let lookup = unsafe { *table.get_unchecked(idx as usize) }; // 2 cycles
let mut total_bits = 5; // 1 cycle (load interlock)
let lookup_5 = lookup >> 5; // 1 cycle
let mut neg_bits = lookup_5 as i32 - 3; // 1 cycle
from construct import *
def sizeof_struct(subcon, value, parent=None):
ctx = Container(value)
ctx._ = parent or Container()
ctx._params = Container()
ctx._parsing = False
ctx._sizing = True
ctx._building = False
@phire
phire / gist:e9752c3ff0ffc0db07f7960e70e7f22a
Created March 23, 2024 19:00
Interesting Gamecube patents
EP1189173A2 - Highly detailed XF pipeline
US6489963 - graphics api
US6580430 - fog
US6618048 near-far clipping
US6636214 - early depth (shows graphics pipeline)
US6664962 - shadow mapping (technique)
US6700586 - XF pipeline
US6717577 - Early vertex cache? might not be gamecube
US7034828 - tev
US7071945 - CP commands and vertex formats
@phire
phire / make.py
Created June 19, 2023 18:16
Custom metabuild system
#!/usr/bin/env python3
# Story time:
# I tend to use cmake for projects these days, but I'm not excatly happy with it's support
# for cross-compiling. Sure, it's flexible enough that you do almost anything, but anything
# advanced (like compiling host tools or building multiple platforms simultaneously)
# requires janky usage of external projects.
# So I set aside some time to explore other build systems to find one that better supported
# my needs.
# I spent some time exploring meson, it had first-party understanding of compiling host tools
@phire
phire / alternate2.cpp
Last active September 16, 2021 19:49
#include <FEXThunkDefs.h> // provides fexgen_config in this model
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
// For each exported function:
fexgen_config exports[] = {
// Variadic functions: The variadic parameters are automatically packed into an unsigned long array
@phire
phire / literalfn.hpp
Created June 17, 2021 09:24
Literal std::function experment. Can be passed as template argument.
#include <type_traits>
#include <utility>
template<class ReturnType, class...Xs>
struct CallableBase {
virtual ReturnType operator()(Xs...) const = 0;
virtual ReturnType operator()(Xs...) = 0;
virtual void copy(void*) const = 0;
};
%ifdef CONFIG
{
"Match": "All",
"RegData": {
"MM7": ["0xfb53d1c000000000", "0x4002"]
}
}
%endif
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdlib.h>
atomic_int count = 0;
void sig_handler(int signum) {
printf("Alarm %i\n", count++);
0001/0:0001: #version 120
0002/0:0002: #define GL2
0003/0:0003: #define texture texture2D
0004/0:0004: #define VSIN(index) attribute
0005/0:0005: #define VSOUT varying
0006/0:0006: #define FSIN varying
0007/0:0007: #define FRAGCOLOR(c) (gl_FragColor = c)
0008/1:0001: #define DIRECTIONALS
0009/1:0002: //#define POINTLIGHTS
MESA_SHADER_FRAGMENT prog 25/1 QPU:
0x3e90f186bb800000 nop ; nop ; ldvary.r3; wrtmuc (tex[0].p0 | 0x3)
0x56403106bbcc0000 nop ; fmul r4, r3, rf0 ; wrtmuc (tex[0].p1 | 0x0)
0x3dd0054805fec000 fadd rf8, r4, r5 ; mov rf21, 0 ; ldvary.r0
0x54003046bbc00000 nop ; fmul r1, r0, rf0
0x3d10a18705829000 fadd rf7, r1, r5 ; nop ; ldvary.r2
0x540030c6bbc80000 nop ; fmul r3, r2, rf0
0x3d1121850582b000 fadd rf5, r3, r5 ; nop ; ldvary.r4
0x54003006bbd00000 nop ; fmul r0, r4, rf0
0x3d10618605828000 fadd rf6, r0, r5 ; nop ; ldvary.r1