Skip to content

Instantly share code, notes, and snippets.

@typeswitch-dev
typeswitch-dev / quickgen.py
Created November 27, 2023 00:50
quickgen -- procedurally generating a map by repeated subdivision
#!/usr/bin/env python3
# quickgen by azul (@typeswitch@gamedev.lgbt) -- generates a map
# by repeated subdivision. a simple approach to generating maps
# quickly, starting from a very rough sketch.
#
# this code is public domain! use it however you like.
import random
@typeswitch-dev
typeswitch-dev / minimal-elf64.asm
Created September 3, 2022 00:24
Minimal Linux x86-64 program written in NASM assembly
bits 64
org 0x4000000
elf_header:
.size equ .end - $
.e_ident db 0x7F, 'E', 'L', 'F' ; EI_MAG0 ... EI_MAG3
db 2 ; EI_CLASS: 1 => 32 bits, 2 => 64 bits
db 1 ; EI_DATA: 1 => lil endian, 2 => big "
db 1 ; EI_VERSION: original version
db 0 ; EI_OSABI: 0 => System V ABI
@typeswitch-dev
typeswitch-dev / minimal.asm
Created September 2, 2022 17:07
Minimal win64 executable in NASM assembly.
org 0 ; We use "org 0" so Relative Virtual Addresses (RVAs) are easy.
; This means that when we want an absolute Virtual Address we have
; to add IMAGE_BASE to the RVA (or whatever the base of that section is)
IMAGE_BASE equ 0x400000
SECT_ALIGN equ 0x200
FILE_ALIGN equ 0x200
msdos_header:
.magic db 'MZ'
@typeswitch-dev
typeswitch-dev / self_modifying.asm
Last active April 11, 2024 01:12
NASM source for a minimal self-modifying Mach-O executable
bits 64
org 0x1000
mach_header:
.magic dd 0xFEEDFACF ; MH_MAGIC_64
.cputype dd 0x01000007 ; CPU_ARCH_ABI64 | CPU_TYPE_I386
.cpusubtype dd 0x00000003 ; CPU_SUBTYPE_LIB64 | CPU_SUBTYPE_I386_ALL
.filetype dd 0x2 ; MH_EXECUTE
.ncmds dd 3
.sizeofcmds dd mach_cmds_end - mach_cmds_start
// deobfuscated version of "FIRST and THIRD"
// deobfuscated by Sofia Faro (@typeswitch)
// original by Sean Barrett (@nothings)
//
// original design document: ftp.funet.fi/pub/doc/IOCCC/1992/buzzard.2.design
// original obfuscated code: ftp.funet.fi/pub/doc/IOCCC/1992/buzzard.2.c
#define comma m[m[0]++] =
char s[5000];
@typeswitch-dev
typeswitch-dev / daiyon.c
Last active October 12, 2023 23:25
第四 (Daiyon) — a Japanese & Forth inspired postfix language
#include <stdio.h>
#include <string.h>
#include <assert.h>
FILE *in; long M[1<<24]={0}, *D, *R, H=0x130000, IP=0, T;
long getu() { long t, h = getc(in); if (h < 0xC0) return h;
t = ((h&0x1F) << 6) | (getc(in) & 0x3F); if (h < 0xE0) return t;
t = ( t << 6) | (getc(in) & 0x3F); if (h < 0xF0) return t;
t = ( t << 6) | (getc(in) & 0x3F); return t & 0x1FFFFF; }
void putu(long c) { if (c < 0x80) { putchar(c); return; }
if (c < 0x7FF) { putchar(0xC0|(c>>6)); } else {
CFLAGS=-std=c99 -pedantic -Wall -Werror
.PHONY: run forth
run: forth
./forth test.fs
forth: forth.c
$(CC) $(CFLAGS) -o forth forth.c
@typeswitch-dev
typeswitch-dev / helloworld.json
Created December 16, 2021 20:31
JoySON - a tiny concatenative programming language with JSON syntax
{
"main": ["lit", "Hello, world!", "print"]
}
@typeswitch-dev
typeswitch-dev / bogosort_approximator.py
Last active March 1, 2020 11:30
Approximates e using bogosort.
# Calculates e using randomized bogosort. The average number
# of comparisons per round should be close to e-1 due to
# shortcircuiting, so we run bogosort and keep track of
# the number of rounds and number of comparisons, and use
# that to calculate e.
import random
n = 10
@typeswitch-dev
typeswitch-dev / beep-boop.c
Created June 26, 2019 23:14
Making the computer go beep boop.
#include "SDL.h"
#define SAMPLE_RATE 48000
#define CHANNELS 2
#define SAMPLES (SAMPLE_RATE*2)
int main (int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);