Skip to content

Instantly share code, notes, and snippets.

@juj
juj / c64sprite.cpp
Last active October 16, 2023 22:22
llvm-mos small C64 sprite example
// C64 sprite example with llvm-mos.
// Jukka Jylänki. Released to public domain.
// Build with -Oz -DNDEBUG for smallest code size (640 bytes).
#include <stdint.h>
#include <stdio.h>
static void TestAssert(bool condition, const char *str, const char *file, int line, const char *func)
{
#ifndef NDEBUG // Build with -DNDEBUG to remove this bloating code size
if (!condition) {
@juj
juj / llvm-mos-Printf.cpp
Created October 13, 2023 21:31
Small (partial) llvm-mos Printf()
#include <stdarg.h>
#include "c64/c64_kernal.h"
// Prints the given uint16 to CHROUT.
static void chrout_u16(uint16_t num)
{
uint8_t zp0, zp1, zp2;
__asm__(R"(
SED // Enter BCD mode (affects ADC commands below)
@juj
juj / c64_kernal.h
Last active November 21, 2023 18:37
llvm-mos inline assembly stubs for C64 KERNAL ROM subroutines
#pragma once
// C64 KERNAL ROM functions
#include <stdint.h>
#include "mystdio.h"
#ifdef __C64__
// TODO: Might want to use this form, but can't due to https://github.com/llvm-mos/llvm-mos/issues/392
@juj
juj / sphere_vs_arc_sector_intersection.cpp
Created March 28, 2023 18:30
Rough sketch of sphere vs arc sector intersection test
struct Sphere
{
float2 pos;
float radius;
};
struct ArcSector
{
float2 pos;
float radius;
@juj
juj / dom_audio_library.js
Created February 28, 2023 16:46
Hypothetical Emscripten <audio> based player
mergeInto(LibraryManager.library, {
preloaded_audio: {},
preload_audio__deps: ['preloaded_audio'],
preload_audio: function(id, url) {
let audio = new Audio(UTF8ToString(url));
_preloaded_audio[id] = audio;
audio.preload = 'auto';
},
play_audio: function(id, loop) {
let audio = _preloaded_audio[id];
@juj
juj / audio.cpp
Created February 28, 2023 16:44
Emscripten WebAssembly Audio Worklets example
#include <emscripten/webaudio.h>
#include <emscripten/em_math.h>
float phase = 0.f;
EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs,
int numOutputs, AudioSampleFrame *outputs,
int numParams, const AudioParamFrame *params, void *userData) {
for(int i = 0; i < 128; ++i) {
outputs[0].data[i] = emscripten_math_sin(phase);
phase = emscripten_math_fmod(phase + 0.05f, 2.f * EM_MATH_PI);
}
@juj
juj / 60hz.cpp
Created January 11, 2023 20:54
How to reprogram VGA registers to a 320x200@60hz mode with either square pixels or 5:6 pixels.
// License: public domain.
#include <dos.h>
#include <conio.h>
void set_video_mode(int mode)
{
union REGS regs;
regs.x.ax = mode;
int86(0x10, &regs, &regs);
@juj
juj / cube_folding.py
Last active January 9, 2023 22:26
Advent of Code 2022 Day 22 Cube folding solver
# Usage: python cube_folding.py input.txt
import math, sys
lines = open(sys.argv[1] if len(sys.argv) > 1 else 'input.txt', 'r').read().split('\n')
# Remove instructions line from input
lines = lines[:-1]
if len(lines[-1]) == 0: lines.pop() # Remove possible empty line between map and instructions
# Compute cube surface area
@juj
juj / 6.cpp
Last active December 6, 2022 11:04
Advent of Code 2022, Day 6
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
char *read_file_to_string(const char *filename)
{
FILE *handle = fopen(filename, "r");
fseek(handle, 0, SEEK_END);
long size = ftell(handle);
fseek(handle, 0, SEEK_SET);
@juj
juj / 3B.cpp
Created December 4, 2022 17:28
Advent of Code Day 3, problem B.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *read_file_to_string(const char *filename)
{
FILE *handle = fopen(filename, "r");
fseek(handle, 0, SEEK_END);
long size = ftell(handle);
fseek(handle, 0, SEEK_SET);