Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / blockprinter.rs
Last active November 16, 2016 05:36
(WIP) Prints CrayonForth source code blocks
/*
crayonforth source code block format
each block is 256 dwords (1024 bytes), stored little-endian
31...24 23...16 15...8 7...0
____0ttt aaaaaabb bbbbcccc ccdddddd word
____0000 aaaaaabb bbbbcccc ccdddddd extension of previous word
__ux1ttt ________ hhhhhhhh llllllll number
____1111 ________ ________ ________ end of data
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
word = 2 bytes
0x10000 words (128 KB) of ram
addresses are words rather than bytes
0000-7fff generally contains forth dictionary and thus isn't usually swapped out
words are big endian
rom is split into 0x2000-word (16 KB) banks
maximum rom size is 0x8000 banks (512 MB)