Skip to content

Instantly share code, notes, and snippets.

@niconii
niconii / list.fs
Last active April 11, 2023 06:21
Simple linked list implementation in Forth
0 constant nil
: cons ( car cdr -- list ) here >r swap , , r> ;
: list ( x... #x -- list ) nil swap 0 ?do cons loop ;
: list: ( x... #x "name" -- ) list constant ;
: car ( list -- car ) @ ;
: car! ( car list -- ) ! ;
: cdr ( list -- cdr ) cell+ @ ;
: cdr! ( cdr list -- ) cell+ ! ;
: list. ( list -- ) begin ?dup while dup car . cdr repeat ;
@niconii
niconii / .gitignore
Last active January 3, 2022 23:37 — forked from typeswitch-dev/daiyon.c
第四 (Daiyon) — a Japanese & Forth inspired postfix language
daiyon
@niconii
niconii / snes_main_loop.asm
Created September 10, 2018 00:19
Example SNES main loop
reset:
; initialize everything...
main:
; update game state...
; process sprites in a copy of OAM stored in WRAM...
; other stuff...
inc ReadyFlag ; signal that we're done processing for this frame
spin:
lda ReadyFlag ; wait until flag is cleared by NMI routine
@niconii
niconii / clear_wram.asm
Last active September 9, 2018 03:01
Example for clearing SNES WRAM using DMA
zero:
.db $00
; assuming A is 8-bit, X/Y are 16-bit
clear_wram:
; From fixed CPU address to IO register,
; transfer unit is one byte
lda #%00001000
sta DMAP0 ; $4300
@niconii
niconii / nfmt.rs
Last active August 17, 2018 02:12
Forth number formatting, ported to Rust
use nfmt::Nfmt;
fn main() {
println!("{}", number(25252)); // 25,252
println!("{}", number(-500)); // -500
println!("{}", cents(1234567890)); // $12,345,678.90
println!("{}", cents(-200000)); // ($2,000.00)
println!("{}", hex(32768)); // 0x8000
println!("{}", hex(!0)); // 0xFFFFFFFFFFFFFFFF
println!("{}", yen(573765)); // 57万3765円
@niconii
niconii / nfmt.c
Created August 17, 2018 00:42
Forth number formatting, ported to C
#include <stdio.h>
#include <string.h>
#define NFMT_BUF_LEN 32
const char nfmt_set[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char nfmt_base;
char nfmt_buf[NFMT_BUF_LEN+1] = {0};
char* nfmt_ptr;
@niconii
niconii / cursor.rs
Created February 16, 2017 00:28
Testing text buffer cursors for CrayonForth
pub struct SingleCursor {
cursor: u16,
mask: u16
}
impl SingleCursor {
pub fn new(offset: u16, mask: u16) -> SingleCursor {
SingleCursor {
cursor: offset,
mask: mask
@niconii
niconii / movcpu.rs
Last active November 26, 2016 08:48
A little toy CPU with only a mov instruction
fn main() {
let mut ram = [0u16; 0x2000];
let mut rom = [0xffffu16; 0xc000];
{
let mut ptr = 0x0000;
let mut mov = |dst, src| {
rom[ptr+0] = src;
rom[ptr+1] = dst;
ptr += 2;
Name Stack effect Operation Operation after a b ? Operation after a 0? ASM
if ( -- ) if zero flag = 0 if a != b if a beq ...
=if ( -- ) if zero flag = 1 if a == b if !a bne ...
+if ( -- ) if negative flag = 0 if (i16)a - (i16)b >= 0 if (i16)a >= 0 bmi ...
-if ( -- ) if negative flag = 1 if (i16)a - (i16)b < 0 if (i16)a < 0 bpl ...
vif ( -- ) if overflow flag = 0 n/a n/a bvs ...
^if ( -- ) if overflow flag = 1 n/a n/a bvc ...
<if ( -- ) if carry flag = 0 if (u16)a < (u16)b if false bcs ...
&gt;if ( -- ) if carry flag = 1 if
; Goal: to turn controller button samples, incoming on the x register each
; frame, into three variables:
; - cur, which represents the current state of the controller buttons
; - prs, which represents the buttons pressed this frame
; - rls, which represents the buttons released this frame
;
; Example:
; x cur prs rls
; ................ ................ ................ <- initial values
; ...........1.... ...........1.... ...........1.... ................