Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicolette Verlinden niconii

View GitHub Profile
@niconii
niconii / list.fs
Last active Jan 12, 2019
Simple linked list implementation in Forth
View list.fs
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 Sep 10, 2018
Example SNES main loop
View snes_main_loop.asm
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 Sep 9, 2018
Example for clearing SNES WRAM using DMA
View clear_wram.asm
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 Aug 17, 2018
Forth number formatting, ported to Rust
View nfmt.rs
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 Aug 17, 2018
Forth number formatting, ported to C
View nfmt.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 Feb 16, 2017
Testing text buffer cursors for CrayonForth
View cursor.rs
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 Nov 16, 2016
(WIP) Prints CrayonForth source code blocks
View blockprinter.rs
/*
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
View crayonforth_branches.md
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 ...
>if ( -- ) if carry flag = 1 if
View esvm.txt
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)
View asm_grammar
ident:
/[A-Za-z_.][A-Za-z0-9_.]*/
expr:
ident |
expr '+' expr |
etc.
nl:
'\' | '\r' | '\n' | EOF
You can’t perform that action at this time.