added some sounds
[awish.git] / src / vm.h
blob0db0cd939ed45f39668551db2ed77d1ef975b329
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #ifndef _VM_H_
16 #define _VM_H_
19 #define VM_STACK_SIZE (1024)
20 #define VM_VARS_SIZE (127)
22 #define VM_MAX_THREADS (512)
25 enum {
26 VM_ADD,
27 VM_SUB,
28 VM_MUL,
29 VM_DIV,
30 VM_MOD,
31 VM_BOR,
32 VM_XOR,
33 VM_AND,
35 VM_JEQ,
36 VM_JNE,
37 VM_JLT,
38 VM_JLE,
39 VM_JGT,
40 VM_JGE,
42 VM_JMP,
44 VM_BSR,
46 VM_BRK,
47 VM_END,
48 VM_NEW,
50 VM_SET,
51 VM_GET,
53 VM_PSH,
54 VM_POP,
55 VM_SWP,
56 VM_PCK,
57 VM_ROL,
58 VM_DPT,
60 VM_TID,
61 VM_KIL,
62 VM_SUS,
63 VM_RES,
64 VM_STA,
66 VM_RXC,
67 VM_WXC,
69 VM_RET,
71 VM_RST,
73 VM_MGF,
74 VM_MGB,
75 VM_MSF,
76 VM_MSB,
78 VM_LASTOP
82 enum {
83 LB_MIN_TYPE = 0,
84 LB_GVAR = 0,
85 LB_TVAR,
86 LB_SVAR,
87 LB_CODE,
88 LB_CONST,
89 LB_MAX_TYPE,
90 LB_MARK = -1
94 typedef struct VMLabelInfo {
95 struct VMLabelInfo *next;
96 char *name;
97 int type;
98 int value;
99 int pub; // public?
100 } VMLabelInfo;
103 extern int vmDebugTrace;
104 extern FILE *vmDebugOutput;
106 extern const char *vmOpNames[];
108 extern unsigned char vmCode[65536];
109 extern int vmCodeSize;
110 extern int vmGVars[VM_VARS_SIZE];
113 // <0: BRK; >0: END; 0: continue
114 typedef int (*VMRSTCB) (int tid, int opcode, int argc, int argv[], int *argp[]);
116 extern VMRSTCB vmRSTCB; // argv[0] is ALWAYS fid
119 typedef int (*VMMapGetCB) (int tid, int fg, int x, int y);
120 typedef void (*VMMapSetCB) (int tid, int fg, int x, int y, int tile);
122 extern VMMapGetCB vmMapGetCB;
123 extern VMMapSetCB vmMapSetCB;
126 // initialize VM, init main thread
127 extern int vmInitialize (void); // glovals, code and codesize must be set
128 extern void vmDeinitialize (void);
130 extern void vmExecuteAll (void);
132 // <0: BRK; >0: END; 0: ok
133 // maxinst<0: any number of instructions
134 extern int vmExecuteBSR (int tid, int pc, int maxinst);
136 // <0: BRK; >0: END
137 extern int vmExecuteOne (int tid);
139 extern int vmIsThreadAlive (int tid);
141 extern int vmNewThread (int pc); // returns thread id or -1
142 extern int vmKillThread (int tid);
143 extern int vmIsSuspendedThread (int tid);
144 extern int vmSuspendThread (int tid);
145 extern int vmResumeThread (int tid);
147 extern int vmGetTVar (int tid, int idx);
148 extern int vmSetTVar (int tid, int idx, int value);
149 extern int vmGetPC (int tid);
150 extern int vmSetPC (int tid, int pc);
152 extern int vmGetSP (int tid);
153 extern int vmSetSP (int tid, int value);
154 extern int vmGetStack (int tid, int idx); // <0: from stack top; >=0: from bottom (0: latest pushed item)
155 extern int vmSetStack (int tid, int idx, int value);
157 extern int vmPush (int tid, int value);
158 extern int vmPop (int tid);
160 extern int vmLoadArgs (int tid, int argc, int argv[], int *argp[], int aargc, int aargv[], int *aargp[]); // max 3 aargs
163 extern int vmLastThread (void);
166 // !0: error
167 extern int vmSaveState (FILE *fl);
169 // !0: error; VM is deinitialized on error
170 extern int vmLoadState (FILE *fl);
173 // return code size
174 //extern int vmLoadCodeFile (ResFile *resfile, int pc, int idx);
175 extern int vmLoadCodeFileFromDump (const void *data, int datasize, int pc);
178 extern void vmFreeLabels (void);
179 extern void vmFreeLabelsUntilMark (const char *name);
181 extern VMLabelInfo *vmLabelAddMark (const char *name);
182 extern VMLabelInfo *vmAddLabel (const char *name, int type, int value, int pub);
184 extern VMLabelInfo *vmFindLabel (const char *name);
186 extern int vmFindPC (const char *name);
187 extern int vmFindVarIndex (const char *name);
188 extern int vmFindGVarIndex (const char *name);
189 extern int vmFindTVarIndex (const char *name);
190 extern int vmFindSVarIndex (const char *name);
191 extern int vmFindConst (const char *name);
192 extern VMLabelInfo *vmFindMark (const char *name);
195 #endif