git @ Cat's Eye Technologies Tamsin / master doc / 6502-sketch.tamsin
master

Tree @master (Download .tar.gz)

6502-sketch.tamsin @masterraw · history · blame

# Copyright (c) 2014-2026, Chris Pressey, Cat's Eye Technologies.
# This file is distributed under a 2-clause BSD license, see LICENSES/ dir.
# SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Tamsin

# a sketch of what a Tamsin program to simulate a subset of the 6502
# might look like.

# note that the 6502 memory is in the IMPLICIT BUFFER.

sim6502 = instr(0,0,0) using $:byte.

instr(A,X,Y) =
      "\xA9" & any → A               & instr(A,X,Y)    # LDA #
    | "\xC8" & inc(Y) → Y            & instr(A,X,Y)    # INY
    | "\x8A" & A ← X                 & instr(A,X,Y)    # TAX
    | "\x4C" & word → W & $:seek(W)  & instr(A,X,Y)    # JMP
    | etc.

word =
   any → Lo & any → Hi & return $:add($:ord(Lo), $:mul($:ord(Hi), 256)).

etc.


# That's the recursive version; compiling it to C currently would not be
# nice to the stack.  Here's an iterative version:


sim6502 =
    A  0 & X  0 & Y  0 &
    !{instr(A,X,Y)  state(A,X,Y)} using $:byte.

instr(A,X,Y) =
       "\xA9" & any  A               & return! state(A,X,Y)    # LDA #
    !| "\xC8" & inc(Y)  Y            & return! state(A,X,Y)    # INY
    !| "\x8A" & A  X                 & return! state(A,X,Y)    # TAX
    !| "\x4C" & word  W & $:seek(W)  & return! state(A,X,Y)    # JMP
    !| "\x00"                         & return! halted          # BRK
    !| etc.


# this uses ! (non-backtracking) and return! (immediate return from production)
# (not sure about either of these...)