experimental load/save feature (only in debug mode for now)
[awish.git] / vm.txt
blobe0e471045348824325cc2f0d3c3498997310ad30
1 all numbers are int16_t
3 126 gvars
4 127 tvars
6 each op:
7   db opcode
8   db arg0
9   db arg1
10   db dvar
11 if argX is 255, then we have 'immediate' 2-byte value
14 opcode:
15   bits 6-7: # of operands (0-3)
16   bits 0-5: opcode (0-31)
18 operands:
19   255: immediate value, else:
20   bits 0-6: variable number
21   bit 7: if set, use global
23 'immediate' for storing: ignore
25 all jumps are absolute offsets (in bytes)
27 spriteid: bit 7 != 0: right (have any meaning only for banks 84 and 90)
29 opcodes:
30 ; opcodes 0-7
31   ; no args: op1=pop, op0=pop; push result
32   ; 1 op: op0=pop, op1=arg0; push result
33   ; op0, op1: op0 = op0 MATH op1
34   ; op0, op1, op2: op2 = op0 MATH op1
35   VM_ADD
36   VM_SUB
37   VM_MUL
38   VM_DIV
39   VM_MOD
40   VM_BOR
41   VM_XOR
42   VM_AND
44 ; opcodes 8-13
45   ; condition jump:
46   ; no args: pc=pop, vi1=pop, vi0=pop
47   ; 1 arg: pc=arg0, vi1=pop, vi0=pop
48   ; 2 args: pc=arg0, vi0=pop, vi1=arg0
49   ; 3 args: pc=arg0, vi0=arg1, vi1=arg1
50   VM_JEQ  ; ==
51   VM_JNE  ; !=
52   VM_JLS  ; <
53   VM_JLE  ; <=
54   VM_JGR  ; >
55   VM_JGE  ; >=
57 ; opcode 14
58   ; no args: pc=pop (ret, hehe)
59   ; 1-3 args: pc=arg0
60   VM_JMP
62 ; opcode 15
63   ; no args: pc=pop
64   ; 1-3 args: pc=arg0, arg1 and arg2 will be pushed on stack
65   VM_BSR   ; 'call'; use JMP w/o args to return (asm understands RET mnemonics)
67 ; opcode 16
68   VM_BRK  ; 'break' until the next frame
70 ; opcode 17
71   VM_END  ; stop thread and destroy object
73 ; opcode 18
74   ; no args: pc=pop
75   ; 1 arg: pc=arg0; push tid
76   ; 2 args: pc=arg0; [arg1]=tid (literals ignored)
77   VM_NEW  ; spawn thread, fatal error if can't spawn
79 ; opcode 19
80   ; no args: varid=pop, value=pop; varid<0: set global (note that you can't set global #0 this way)
81   ; 1 arg: value=pop, varid=arg0 (literals ignored)
82   ; 2 args: varid=arg0 (literals ignored), value=arg1
83   ; 3 args: varid=arg0 (literals ignored), value=arg1, destthreadid=arg2 -- set thread local var
84   VM_SET
86 ; opcode 20
87   ; no args: varid=pop, tid=pop, push result
88   ; 1 arg: tid=pop, varid=arg0, push result
89   ; 2 args: tid=arg0, varid=arg1, push result
90   ; 3 args: tid=arg0, varid=arg1, dest=arg2 (literals ignored)
91   VM_GET  ; get thread local var
93 ; opcodes 21-26
94   ; stack indicies: <0: from stack top
95   VM_PSH  ; 1-3 args; no args: DUP
96   VM_POP  ; no args: drop one item; numeric arg: drop*num, var: pop to var; can have 0-3 args
97   VM_SWP  ; 0 args: swap 2 top stack items; 1 arg: swap stack top and arg; 2 args: swap 'em
98   VM_PCK  ; 'pick', first arg: number (-1: top); 2nd arg: dest (push if no 2nd var)
99   VM_ROL  ; remove nth item from stack (and possibly place to var)/push to stack (no 2nd arg)
100   VM_DPT  ; get stack depth (optional arg: var to store; else: push)
102 ; VM_POP: one arg, and it's literal number, and number<0: alloc stack space, fill it with 0
104 ; opcodes 27-31
105   VM_TID  ; get current thread id (destarg or to stack)
106   VM_KIL  ; kill thread (arg: tid)
107   VM_SUS  ; suspend thread (arg: tid)
108   VM_RES  ; resume (arg: tid)
109   VM_STA  ; get thread state (arg1: tid; arg2: dvar; no: push); -1: invalid tid; 0: running; 1: suspended
111 ; opcodes 32-33
112   ; no args: addr=pop, push result
113   ; 1 arg: addr=arg0, push result
114   ; 2-3 args: addr=arg0, destvar=arg1
115   VM_RXC  ; read byte from code area
116   ; no args: byte=pop, addr=pop
117   ; 1 arg: byte=pop, addr=arg0
118   ; 2-3 args: addr=arg0, byte=arg1
119   VM_WXC  ; write byte to code area
121 ; opcode 34
122 ; no args: simple 'ret'
123 ; 1 arg: FATAL ERROR (was: # of locals to pop)
124 ; 2 args: # of locals to pop, # of arguments to pop
125 ; 3 args: # of locals to pop, # of arguments to pop, retvalue (taken before all pops, pushes after returning)
126   VM_RET
128 ; opcode 35
129   ; call 'primitive'
130   ; no args: arguments on stack, fid
131   ; 1 arg: fid
132   ; 2-3 args: fid=arg0
133   VM_RST
135 ; working with map: get/set foreground/background
136 ; opcodes 36-37
137 ; no args: y=pop, x=pop, push result
138 ; 1 arg: it's destvar
139 ; 2 args: x, y
140 ; 3 args: x, y, destvar
141   VM_MGF  ; get foreground tile
142   VM_MGB  ; get background tile
143 ; opcodes 38-39
144 ; no args: tile=pop, y=pop, x=pop
145 ; 1 arg: tile, y=pop, x=pop
146 ; 2 args: tile=pop, x, y
147 ; 3 args: x, y, tile
148   VM_MSF  ; set foreground tile
149   VM_MSB  ; set background tile
152 thread 0 is 'main' thread: title, professor, etc
155 var ref:
156   [vname] -- thread local, global or stack local
157   [inum]  -- thread local
158   [@inum] -- global
159   [.inum] -- stack index (<0: push order; >=0: from stack bottom)
161   [@retval] -- proc return value
163 include: file
164 [e]const: name = number
165 defgvar:
166 deftvar:
167 deflvar:
168 defetvar:
169 extern:
170 [e]proc: name
171   [args: ...]
172   [locals: ...]
173 endp: name
174 db: ...