fixed bug in rxc/wxc
[awish.git] / vm.txt
blob947c6f45887aafc8af944e191bb2bc3099407fcc
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
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   VM_PSH  ; 1-3 args; no args: DUP
95   VM_POP  ; no args: drop one item; numeric arg: drop*num, var: pop to var; can have 0-3 args
96   VM_SWP  ; 0 args: swap 2 top stack items; 1 arg: swap stack top and arg; 2 args: swap 'em
97   VM_PCK  ; 'pick', first arg: number (0: top); 2nd arg: dest (push if no 2nd var)
98   VM_ROL  ; remove nth item from stack (and possibly place to var)/push to stack (no 2nd arg)
99   VM_DPT  ; get stack depth (optional arg: var to store; else: push)
101 ; VM_POP: one arg, and it's literal number, and number<0: alloc stack space, fill it with 0
103 ; opcodes 27-31
104   VM_TID  ; get current thread id (destarg or to stack)
105   VM_KIL  ; kill thread (arg: tid)
106   VM_SUS  ; suspend thread (arg: tid)
107   VM_RES  ; resume (arg: tid)
108   VM_STA  ; get thread state (arg1: tid; arg2: dvar; no: push); -1: invalid tid; 0: running; 1: suspended
110 ; opcodes 32-33
111   ; no args: addr=pop, push result
112   ; 1 arg: addr=arg0, push result
113   ; 2-3 args: addr=arg0, destvar=arg1
114   VM_RXC  ; read byte from code area
115   ; no args: byte=pop, addr=pop
116   ; 1 arg: byte=pop, addr=arg0
117   ; 2-3 args: addr=arg0, byte=arg1
118   VM_WXC  ; write byte to code area
120 ; opcode 34
121 ; no args: simple 'ret'
122 ; 1 arg: FATAL ERROR (was: # of locals to pop)
123 ; 2 args: # of locals to pop, # of arguments to pop
124 ; 3 args: # of locals to pop, # of arguments to pop, retvalue (taken before all pops, pushes after returning)
125   VM_RET
127 ; opcode 35
128   ; call 'primitive'
129   ; no args: arguments on stack, fid
130   ; 1 arg: fid
131   ; 2-3 args: fid=arg0
132   VM_RST
134 ; working with map: get/set foreground/background
135 ; opcodes 36-37
136 ; no args: y=pop, x=pop, push result
137 ; 1 arg: it's destvar
138 ; 2 args: x, y
139 ; 3 args: x, y, destvar
140   VM_MGF  ; get foreground tile
141   VM_MGB  ; get background tile
142 ; opcodes 38-39
143 ; no args: tile=pop, y=pop, x=pop
144 ; 1 arg: tile, y=pop, x=pop
145 ; 2 args: tile=pop, x, y
146 ; 3 args: x, y, tile
147   VM_MSF  ; set foreground tile
148   VM_MSB  ; set background tile
151 thread 0 is 'main' thread: title, professor, etc
154 var ref:
155   [vname] -- thread local, global or stack local
156   [inum]  -- thread local
157   [@inum] -- global
158   [.inum] -- stack index (<0: push order; >=0: from stack bottom)
160   [@retval] -- proc return value
162 include: file
163 [e]const: name = number
164 defgvar:
165 deftvar:
166 deflvar:
167 defetvar:
168 extern:
169 [e]proc: name
170   [args: ...]
171   [locals: ...]
172 endp: name
173 db: ...