Skip to content

Instantly share code, notes, and snippets.

View hrydgard's full-sized avatar

Henrik Rydgård hrydgard

View GitHub Profile
@hrydgard
hrydgard / ps2emu.md
Last active July 4, 2025 18:29
PS2 emulator - implementation tips for the early stages

Tips and tricks when implementing a PS2 emulator

For console output, just log characters written to KPUTCHAR: 1000F180h

On the IOP, you can, at least initially, ignore load delay slots (but of course not branch delay slots). Also the EE doesn't have load delay slots.

You can start with either EE or IOP but you'll need both fairly soon. EE is more interesting but you have to do the IOP too. If you have a PSX emulator, that's a big boost, just grab its core, and add the things it's missing.

The BIOS, during bootup, will write to or read from all kinds of wacky addresses outside the documented registers. These can generally be ignored.

// .h file
// Similar to strncpy but with safe zero-termination behavior
// and doesn't fill extra space with zeroes.
// Returns true if the string fit without truncation.
// Always performs the copy though.
bool truncate_cpy(char *dest, size_t destSize, const char *src);
template<size_t Count>
inline bool truncate_cpy(char(&out)[Count], const char *src) {
@hrydgard
hrydgard / gist:f73b8d9f2bab46e716d09902f08bcb43
Last active June 9, 2019 21:05
Attempt at exponent-aligned fp addition
#include <iostream>
uint32_t bit_cast(float f) {
uint32_t x;
memcpy(&x, &f, 4);
return x;
}
float bit_cast(uint32_t x) {
float f;
@hrydgard
hrydgard / timinggame.ino
Created October 5, 2017 14:44
Simple Arduino timing game
// Simple Arduino timing game.
// Made for Nano but probably works on Uno too.
// by Henrik Rydgård
// [email protected]
//
// Connect:
// * a 0.96" no-name I2C OLED screen to pins SDA=A4,SCL=A5
// * a button to pin 4 and ground (we use internal pullup to avoid adding a resistor)
// * a 8-LED RGB strip to pin 2
// and that's pretty much it.
@hrydgard
hrydgard / OpenSLWrap.cpp
Created July 8, 2012 19:48
A minimal implementation of audio streaming using OpenSL in the Android NDK
// Minimal audio streaming using OpenSL.
//
// Loosely based on the Android NDK sample code.
// Hardcoded to 44.1kHz stereo 16-bit audio, because as far as I'm concerned,
// that's the only format that makes any sense.
#include <assert.h>
#include <string.h>
// for native audio