Skip to content

Instantly share code, notes, and snippets.

@apage43
Created April 9, 2012 13:07
Show Gist options
  • Save apage43/2343276 to your computer and use it in GitHub Desktop.
Save apage43/2343276 to your computer and use it in GitHub Desktop.
DCPU16 Code-generation DSL
(ns cloxo.testapp
(use [cloxo.instr :only (add-label add-blob add-code save-app)]))
(def dprintstr
;; We only add to the global namespace when we need to
'[^{:label :dprintstr} ^:export
(SET B SP)
(SET SP A)
^{:label :loop}
(SET [+ :screen I] POP)
(ADD I 1)
(IFN PEEK 0)
(SET PC :loop)
(SET SP B)
(SET PC POP)])
(def appmain
'[(SET A :hello)
(JSR :dprintstr)
(SET A :thebest)
(JSR :dprintstr)
(SUB PC 1)])
; Shorthand for compose/partial
(def & comp)
(def p partial)
;; We can assemble code for any RAM location
;; and its exported labels will be available to other code
;; further up the chain :)
(def mkapp
(& (p add-code appmain 0x0)
(p add-code dprintstr 0x10)
(p add-label :screen 0x8000)
(p add-blob :hello (seq "Hello World! \0") 0x20)
(p add-blob :thebest (seq "You're the best!\0") 0x30)))
(save-app "/Users/apage43/out.dcpu16" (mkapp {}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment