Haskell library for writing assemblers/compilers/disassemblers for the DCPU-16 architecture.
An instruction set data type forms the core. Around it, utilities are built:
- assembly parser with nice error messages, and ability to handle inconsistent implementations
- consistent pretty-printer, for smoothing out inconsistent styles and implementations
- machine code encoding/decoding
- simple optimizer (so far, only optimizes short literals, although it works on labels too)
- stuff for writing interpreters/debuggers
Most of these are accessible via a command line tool:
amtal@yggdrasil:~/code/0x10c$ ./0x10c --help
0x10c 0.0.0, amtal <[email protected]>
0x10c [OPTIONS] <FILE>
Mode of operation:
-p --prettyprint Assembly -> consistently formatted assembly
-a --assemble Assembly -> machine code
-d --disassemble Machine code -> assembly
Optimization:
--no-optimize Disable short literal optimization
General:
-o --output=<FILE> Write to file instead of stdout
--uppercase Parse uppercase symbols (but never mixed case)
--smooth-brackets Parse (a) instead of [a] for indirect mode
-? --help Display help message
-V --version Print version information
Documentation and source at https://github.com/amtal/0x10c or on Hackage.
amtal@yggdrasil:~/code/0x10c$ cat > fib.asm
; Fibonacci filler from reddit
SET A, 1
SET B, 1
SET PEEK, 1
:loop ADD A, B ; A is now 2, B is still 1
SET PUSH, A
SET A, B
SET B, PEEK
IFG SP, 10 ; 10 because that's how much space
; this program takes
SET PC, loop
amtal@yggdrasil:~/code/0x10c$ ./0x10c -p fib.asm --uppercase
; Fibonacci filler from reddit
set a, 0x1
set b, 0x1
set peek, 0x1
:loop add a, b ; A is now 2, B is still 1
set push, a
set a, b
set b, peek
ifg sp, 0xa ; 10 because that's how much space
; this program takes
set pc, loop
amtal@yggdrasil:~/code/0x10c$
More advanced tools can be written using the libraries.
Trifecta parse error messages, with colour:
*DCPU16.Assembler.Parser> parseFile "test.masm"
test.masm:2:19: error: expected: "0", "[",
"a", "b", "c", "i", "j", "o", "pc", "peek",
"pop", "push", "sp", "x", "y", "z", digit,
letter or digit
set #a, 0x30 ; 7c01 0030
^
Nothing
*DCPU16.Assembler.Parser> parseFile "lower.masm"
lower.masm:14:29: error: label "lop" not defined
set pc, lop ; 7dc1 000d [*]
^
Nothing
*DCPU16.Assembler.Parser>