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/>.
19 #define VM_STACK_SIZE (1024)
20 #define VM_VARS_SIZE (127)
22 #define VM_MAX_THREADS (512)
94 typedef struct VMLabelInfo
{
95 struct VMLabelInfo
*next
;
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
];
112 extern int vmMaxGVar
;
113 extern int vmMaxTVar
;
116 // <0: BRK; >0: END; 0: continue
117 typedef int (*VMRSTCB
) (int tid
, int opcode
, int argc
, int argv
[], int *argp
[]);
119 extern VMRSTCB vmRSTCB
; // argv[0] is ALWAYS fid
122 typedef int (*VMMapGetCB
) (int tid
, int fg
, int x
, int y
);
123 typedef void (*VMMapSetCB
) (int tid
, int fg
, int x
, int y
, int tile
);
125 extern VMMapGetCB vmMapGetCB
;
126 extern VMMapSetCB vmMapSetCB
;
129 // initialize VM, init main thread
130 extern int vmInitialize (void); // glovals, code and codesize must be set
131 extern void vmDeinitialize (void);
133 extern void vmExecuteAll (void);
135 // <0: BRK; >0: END; 0: ok
136 // maxinst<0: any number of instructions
137 extern int vmExecuteBSR (int tid
, int pc
, int maxinst
);
140 extern int vmExecuteOne (int tid
);
142 extern int vmIsThreadAlive (int tid
);
144 extern int vmNewThread (int pc
); // returns thread id or -1
145 extern int vmKillThread (int tid
);
146 extern int vmIsSuspendedThread (int tid
);
147 extern int vmSuspendThread (int tid
);
148 extern int vmResumeThread (int tid
);
150 extern int vmGetTVar (int tid
, int idx
);
151 extern int vmSetTVar (int tid
, int idx
, int value
);
152 extern int vmGetPC (int tid
);
153 extern int vmSetPC (int tid
, int pc
);
155 extern int vmGetSP (int tid
);
156 extern int vmSetSP (int tid
, int value
);
157 extern int vmGetStack (int tid
, int idx
); // <0: from stack top; >=0: from bottom (0: latest pushed item)
158 extern int vmSetStack (int tid
, int idx
, int value
);
160 extern int vmPush (int tid
, int value
);
161 extern int vmPop (int tid
);
163 extern int vmLoadArgs (int tid
, int argc
, int argv
[], int *argp
[], int aargc
, int aargv
[], int *aargp
[]); // max 3 aargs
166 extern int vmLastThread (void);
170 extern int vmSaveState (FILE *fl
);
172 // !0: error; VM is deinitialized on error
173 extern int vmLoadState (FILE *fl
);
177 extern int vmLoadCodeFileFromDump (const void *data
, int datasize
, int pc
, int gvfirst
, int tvfirst
, int *gvmax
, int *tvmax
);
180 extern void vmFreeLabels (void);
181 extern void vmFreeLabelsUntilMark (const char *name
);
183 extern VMLabelInfo
*vmLabelAddMark (const char *name
);
184 extern VMLabelInfo
*vmAddLabel (const char *name
, int type
, int value
, int pub
);
186 extern VMLabelInfo
*vmFindLabel (const char *name
);
188 extern int vmFindPC (const char *name
);
189 extern int vmFindVarIndex (const char *name
);
190 extern int vmFindGVarIndex (const char *name
);
191 extern int vmFindTVarIndex (const char *name
);
192 extern int vmFindSVarIndex (const char *name
);
193 extern int vmFindConst (const char *name
);
194 extern VMLabelInfo
*vmFindMark (const char *name
);