*** empty log message ***
[chuck-blob.git] / v1 / chuck_vm.h
blob7436a91052eac389358e6848fe75aee44be94240
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_vm.h
27 // desc: ...
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_VM_H__
34 #define __CHUCK_VM_H__
36 #include "chuck_oo.h"
37 #include "chuck_ugen.h"
38 #include "chuck_dl.h"
39 #include <string>
40 #include <map>
41 using namespace std;
46 //-----------------------------------------------------------------------------
47 // vm defines
48 //-----------------------------------------------------------------------------
49 #define CVM_MEM_STACK_SIZE (0x1 << 16)
50 #define CVM_REG_STACK_SIZE (0x1 << 16)
53 // forward references
54 class Chuck_Instr;
55 class Chuck_VM;
56 class Chuck_VM_Func;
57 class Chuck_VM_FTable;
58 struct Chuck_Msg;
59 class BBQ;
60 class CBuffer;
61 class Digitalio;
66 //-----------------------------------------------------------------------------
67 // name: class Chuck_VM_Stack
68 // desc: ...
69 //-----------------------------------------------------------------------------
70 class Chuck_VM_Stack
72 //-----------------------------------------------------------------------------
73 // functions
74 //-----------------------------------------------------------------------------
75 public:
76 Chuck_VM_Stack();
77 ~Chuck_VM_Stack();
79 public:
80 t_CKBOOL initialize( t_CKUINT size );
81 t_CKBOOL shutdown();
83 //-----------------------------------------------------------------------------
84 // data
85 //-----------------------------------------------------------------------------
86 public: // machine
87 t_CKBYTE * stack;
88 t_CKBYTE * sp;
89 t_CKBYTE * sp_max;
91 public: // linked list
92 Chuck_VM_Stack * prev;
93 Chuck_VM_Stack * next;
95 public: // state
96 t_CKBOOL m_is_init;
102 //-----------------------------------------------------------------------------
103 // name: class Chuck_VM_Code
104 // desc: ...
105 //-----------------------------------------------------------------------------
106 class Chuck_VM_Code
108 public:
109 Chuck_VM_Code();
110 ~Chuck_VM_Code();
112 public:
113 Chuck_Instr ** instr;
114 t_CKUINT num_instr;
116 string name;
117 t_CKUINT stack_depth;
123 //-----------------------------------------------------------------------------
124 // name: class Chuck_VM_Shred
125 // desc: ...
126 //-----------------------------------------------------------------------------
127 class Chuck_VM_Shred
129 //-----------------------------------------------------------------------------
130 // functions
131 //-----------------------------------------------------------------------------
132 public:
133 Chuck_VM_Shred( );
134 ~Chuck_VM_Shred( );
136 t_CKBOOL initialize( Chuck_VM_Code * c,
137 t_CKUINT mem_st_size = CVM_MEM_STACK_SIZE,
138 t_CKUINT reg_st_size = CVM_REG_STACK_SIZE );
139 t_CKBOOL shutdown();
140 t_CKBOOL run( Chuck_VM * vm );
141 t_CKBOOL add( Chuck_UGen * ugen );
142 t_CKBOOL remove( Chuck_UGen * ugen );
144 //-----------------------------------------------------------------------------
145 // data
146 //-----------------------------------------------------------------------------
147 public: // machine components
148 // stacks
149 Chuck_VM_Stack * mem;
150 Chuck_VM_Stack * reg;
152 // code
153 Chuck_VM_Code * code;
154 Chuck_Instr ** instr;
155 Chuck_VM_Shred * parent;
156 map<t_CKUINT, Chuck_VM_Shred *> children;
157 t_CKUINT pc;
159 // time
160 t_CKTIME now;
161 t_CKTIME start;
163 public:
164 t_CKTIME wake_time;
165 t_CKUINT next_pc;
166 t_CKBOOL is_done;
167 t_CKBOOL is_running;
168 map<Chuck_UGen *, Chuck_UGen *> m_ugen_map;
170 public: // id
171 t_CKUINT id;
172 string name;
174 public:
175 Chuck_VM_Shred * prev;
176 Chuck_VM_Shred * next;
182 //-----------------------------------------------------------------------------
183 // name: class Chuck_VM_Shreduler
184 // desc: ...
185 //-----------------------------------------------------------------------------
186 class Chuck_VM_Shreduler
188 //-----------------------------------------------------------------------------
189 // functions
190 //-----------------------------------------------------------------------------
191 public:
192 Chuck_VM_Shreduler();
193 ~Chuck_VM_Shreduler();
195 public:
196 t_CKBOOL initialize();
197 t_CKBOOL shutdown();
199 public: // shreduling
200 t_CKBOOL shredule( Chuck_VM_Shred * shred );
201 t_CKBOOL shredule( Chuck_VM_Shred * shred, t_CKTIME wake_time );
202 Chuck_VM_Shred * get( );
203 void advance( );
205 public: // high-level shred interface
206 t_CKBOOL remove( Chuck_VM_Shred * shred );
207 t_CKBOOL replace( Chuck_VM_Shred * out, Chuck_VM_Shred * in );
208 Chuck_VM_Shred * lookup( t_CKUINT id );
209 void status( );
211 //-----------------------------------------------------------------------------
212 // data
213 //-----------------------------------------------------------------------------
214 public:
215 // time and audio
216 t_CKTIME now_system;
217 BBQ * bbq;
219 // shreds
220 Chuck_VM_Shred * shred_list;
222 // ugen
223 Chuck_UGen * m_dac;
224 Chuck_UGen * m_adc;
225 Chuck_UGen * m_bunghole;
226 t_CKUINT m_num_dac_channels;
227 t_CKUINT m_num_adc_channels;
233 //-----------------------------------------------------------------------------
234 // name: class Chuck_VM
235 // desc: ...
236 //-----------------------------------------------------------------------------
237 class Chuck_VM
239 //-----------------------------------------------------------------------------
240 // functions
241 //-----------------------------------------------------------------------------
242 public:
243 Chuck_VM();
244 ~Chuck_VM();
246 public: // init
247 t_CKBOOL initialize( t_CKBOOL enable_audio = TRUE, t_CKBOOL halt = TRUE,
248 t_CKUINT srate = 44100,
249 t_CKUINT buffer_size = 512, t_CKUINT num_buffers = 4,
250 t_CKUINT dac = 0, t_CKUINT adc = 0,
251 t_CKINT priority = 0x7fffffff );
252 t_CKBOOL shutdown();
254 public: // shreds
255 Chuck_VM_Shred * spork( Chuck_VM_Code * code, Chuck_VM_Shred * parent );
256 Chuck_VM_Shred * fork( Chuck_VM_Code * code );
257 Chuck_VM_Shreduler * shreduler() const;
258 t_CKUINT next_id( );
260 public: // audio
261 BBQ * bbq() const;
262 t_CKUINT srate() const;
264 public: // running the machine
265 t_CKBOOL run( );
266 t_CKBOOL pause( );
267 t_CKBOOL stop( );
269 public: // invoke functions
270 t_CKBOOL invoke_static( Chuck_VM_Shred * shred );
272 public: // garbage collection
273 void gc();
274 void gc( t_CKUINT amount );
276 public: // msg
277 t_CKBOOL queue_msg( Chuck_Msg * msg, int num_msg );
278 t_CKUINT process_msg( Chuck_Msg * msg );
279 Chuck_Msg * get_reply( );
281 public: // static/dynamic function table
282 Chuck_DLL * dll_load( const char * path, const char * id = NULL );
283 t_CKBOOL dll_unload( Chuck_DLL * & dll );
284 void dll_clear( );
285 void set_env( void * env ) { m_env = env; }
286 void * get_env( ) { return m_env; }
288 public: // get error
289 const char * last_error() const
290 { return m_last_error.c_str(); }
292 //-----------------------------------------------------------------------------
293 // data
294 //-----------------------------------------------------------------------------
295 public:
296 // ugen
297 Chuck_UGen * m_adc;
298 Chuck_UGen * m_dac;
299 Chuck_UGen * m_bunghole;
300 t_CKUINT m_num_adc_channels;
301 t_CKUINT m_num_dac_channels;
303 t_CKBOOL m_halt;
304 t_CKBOOL m_audio;
306 protected:
307 Chuck_VM_Shred * spork( Chuck_VM_Shred * shred );
308 t_CKBOOL free( Chuck_VM_Shred * shred, t_CKBOOL cascade,
309 t_CKBOOL dec = TRUE );
311 protected:
312 t_CKBOOL m_init;
313 t_CKBOOL m_running;
314 string m_last_error;
316 // shred
317 Chuck_VM_Shred * m_shreds;
318 t_CKUINT m_num_shreds;
319 t_CKUINT m_shred_id;
320 Chuck_VM_Shreduler * m_shreduler;
322 // audio
323 BBQ * m_bbq;
325 // function table
326 Chuck_VM_FTable * m_func_table;
327 t_CKUINT m_num_func;
329 // message queue
330 CBuffer * m_msg_buffer;
331 CBuffer * m_reply_buffer;
333 // type information
334 void * m_env;
335 map<string, Chuck_DLL *> m_dlls;
337 public:
338 // priority
339 static t_CKBOOL set_priority( t_CKINT priority, Chuck_VM * vm );
340 static t_CKINT our_priority;
346 //-----------------------------------------------------------------------------
347 // name: enum Chuck_Msg_Type
348 // desc: ...
349 //-----------------------------------------------------------------------------
350 enum Chuck_Msg_Type
352 MSG_ADD = 1,
353 MSG_REMOVE,
354 MSG_REMOVEALL,
355 MSG_REPLACE,
356 MSG_STATUS,
357 MSG_PAUSE,
358 MSG_KILL,
359 MSG_TIME,
360 MSG_DONE
366 // callback function prototype
367 typedef void (* ck_msg_func)( const Chuck_Msg * msg );
368 //-----------------------------------------------------------------------------
369 // name: struct Chuck_Msg
370 // desc: ...
371 //-----------------------------------------------------------------------------
372 struct Chuck_Msg
374 t_CKUINT type;
375 t_CKUINT param;
376 Chuck_VM_Code * code;
377 Chuck_VM_Shred * shred;
378 t_CKTIME when;
380 void * user;
381 ck_msg_func reply;
382 t_CKUINT replyA;
383 t_CKUINT replyB;
384 void * replyC;
386 Chuck_Msg() { memset( this, 0, sizeof(*this) ); }
392 #endif