aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_schasm/main.scm
blob: cdaa3dd1688cb190f808cefcd09e785f3b6ada4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(load "core.scm")
(load "macros.scm")

;; load asm modules
(load "bootstrap.scm")

;; you can just write a raw word
(emit-word #x1234)

;; compile instructions
(%ld SP ZR (u16 0))

;; set labels
(def-label 'some-data
  (emit-word #xF800)
  (emit-word #xF801))

;; set the current emit address
(at-addr #x20)
(def-label 'main
  (%add R1 R2)
  (%add R3 (u16 #xFE)))

;; abs jump
(%ld PC (label 'main))

;; rel jump
(%add PC (i16 10))

;; jump forward to a currently undefined label
(%ld PC (label 'forward))
(def-label 'forward
  (%hlt))

;; store string data in memory
(def-label 'text-data
  (%packed-string "hölynpöly"))

(at-addr #x50)

;; branch based on an ALU flag
(%br flag-carry (label 'branch-true) #:asserted #t)
(def-label 'branch-false
  (emit-word 1))
(def-label 'branch-true
  (emit-word 2))

;; use macros like %if-else, %when, %while for convenience
(let ((x #x5678))
  (%if-else (R1 == R2)
	    (emit-word #x1234)
	    (emit-word x)))

(%when (R1 <= (u16 #xFF))
       (emit-word #xbeef))

;; procedures
(%proc (println *str)

       (%ld R3 (param *str)) ;; => evaluates to R0
       (%svar x 1)   ;; allocate a word on the stack
       (%svar y 1)   ;; allocate another word on the stack
       (%ld R2 (u16 (soffset y)))  ;; => evaluates to frame offset 1

       ;; %proc epilogue fees the %svars
       )

;; procedure calls
(%ld R1 (u16 10))
(%while (R1 > (u16 0))
	(%call println (label 'text-data))
	(%sub R1 (u16 1)))

;; compile to a 128KB image file
(write-image-to "out.bin")