OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / audio / components.h
1 // components.h - description.  -*- C++ -*-
2
3 // Copyright (C) 1999, 2000 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
6
7 #ifndef COMPONENTS_H
8 #define COMPONENTS_H
9
10 #include <sidcomp.h>
11 #include <sidso.h>
12 #include <sidtypes.h>
13 #include <sidcomputil.h>
14 #include <sidattrutil.h>
15 #include <sidpinutil.h>
16 #include <sidbusutil.h>
17 #include <sidmiscutil.h>
18 #include <sidschedutil.h>
19
20 #include <deque>
21 #include <vector>
22 #include <string>
23 #include <algorithm>
24 #include <functional>
25
26 #include <cstdlib>
27
28 #include <unistd.h>
29 #include <fcntl.h>
30 #define BSD_COMP
31 #include <sys/ioctl.h>
32
33 #ifdef HAVE_SYS_AUDIOIO_H
34 #include <sys/audioio.h>
35 #endif
36 #ifdef HAVE_SYS_SOUNDCARD_H
37 #include <sys/soundcard.h>
38 #endif
39
40 #ifdef SOUND_CYGWIN
41 #include <windows.h>
42 #ifdef HAVE_MMSYSTEM_H
43 #include <mmsystem.h>
44 #endif
45 #ifdef HAVE_MMREG_H
46 #include <mmreg.h>
47 #endif
48 #endif
49
50
51 using std::deque;
52 using std::string;
53 using std::ostream;
54 using std::istream;
55 using std::vector;
56 using std::string;
57 using std::cerr;
58 using std::endl;
59 using std::ios;
60
61 using sid::component;
62 using sid::bus;
63 using sid::host_int_1;
64 using sid::host_int_2;
65 using sid::host_int_4;
66 using sid::big_int_1;
67 using sid::big_int_4;
68 using sid::component_library;
69 using sid::COMPONENT_LIBRARY_MAGIC;
70
71 using sidutil::fixed_attribute_map_component;
72 using sidutil::no_relation_component;
73 using sidutil::no_accessor_component;
74 using sidutil::fixed_pin_map_component;
75 using sidutil::fixed_bus_map_component;
76 using sidutil::no_bus_component;
77 using sidutil::make_attribute;
78 using sidutil::make_numeric_attribute;
79 using sidutil::parse_attribute;
80 using sidutil::callback_pin;
81 using sidutil::output_pin;
82 using sidutil::input_pin;
83 using sidutil::std_error_string;
84 using sidutil::callback_word_bus;
85
86
87 // ----------------------------------------------------------------------------
88
89
90
91 class basic_codec: public virtual component,
92                    protected fixed_attribute_map_component,
93                    protected no_relation_component,
94                    protected fixed_pin_map_component,
95                    protected no_accessor_component,
96                    protected fixed_bus_map_component
97 {
98 public:
99   basic_codec ();
100   ~basic_codec () {}
101
102 private:
103   friend class callback_word_bus<basic_codec,big_int_4>;
104   callback_word_bus<basic_codec,big_int_4> registers;
105
106   friend class callback_pin<basic_codec>;
107   callback_pin<basic_codec> rx_sample_pin;
108   output_pin tx_sample_pin;
109   output_pin tx_mode_pin, rx_mode_pin;
110   input_pin tx_pending_pin, rx_pending_pin;
111   output_pin config_set_pin;
112
113   host_int_4 tx_count;
114
115   host_int_4 rx_count;
116   string rx_buffer;
117   host_int_4 rx_offset;
118
119   host_int_4 config;
120
121 private:
122   bus::status reg_write (host_int_4 idx, big_int_4 mask, big_int_4 data);
123   bus::status reg_read (host_int_4 idx, big_int_4 mask, big_int_4& data);
124   void rx_sample_pin_handler (host_int_4);
125   
126   void update_txrx_mode_pins ();
127 };
128
129
130
131 // ----------------------------------------------------------------------------
132
133
134 struct audio_config
135 {
136   host_int_2 num_bits_per_sample;
137   enum encoding_t { ulaw, alaw, pcm } encoding;
138   host_int_2 num_channels;
139   host_int_2 sampling_frequency;
140
141   // Initialize to default
142   audio_config ();
143
144   // Encode/decode config state for sid::pin transmission
145   audio_config (host_int_4);
146   host_int_4 encode () const;
147 };
148
149
150 // ----------------------------------------------------------------------------
151
152
153 class generic_audio: public virtual component,
154                      protected fixed_attribute_map_component,
155                      protected no_relation_component,
156                      protected fixed_pin_map_component,
157                      protected no_accessor_component,
158                      protected no_bus_component
159 {
160 public:
161   generic_audio ();
162   ~generic_audio () {}
163
164 protected:
165   virtual bool begin_tx (const audio_config&) = 0;
166   virtual bool pending_tx_p ();
167   virtual void end_tx () = 0;
168   virtual bool begin_rx (const audio_config&) = 0;
169   virtual void end_rx () = 0;
170   virtual string poll_tx (const string&) = 0;
171   virtual string poll_rx () = 0;
172
173 private:
174   friend class callback_pin<generic_audio>;
175
176   callback_pin<generic_audio> tx_mode_pin;
177   callback_pin<generic_audio> tx_sample_pin;
178   bool tx_active_p;
179   string tx_buffer;
180   output_pin tx_pending_pin;
181
182   callback_pin<generic_audio> rx_mode_pin;
183   output_pin rx_sample_pin;
184   bool rx_active_p;
185   string rx_buffer;
186   output_pin rx_pending_pin;
187
188   audio_config config;
189   callback_pin<generic_audio> config_set_pin;
190
191   callback_pin<generic_audio> reset_pin;
192   callback_pin<generic_audio> poll_pin;
193
194   host_int_4 poll_count;
195   host_int_4 tx_samples_count;
196   host_int_4 tx_blocks_count;
197   host_int_4 rx_samples_count;
198   host_int_4 rx_blocks_count;
199
200   void tx_mode_pin_handler (host_int_4);
201   void tx_sample_pin_handler (host_int_4);
202   void rx_mode_pin_handler (host_int_4);
203   void config_set_pin_handler (host_int_4);
204   void reset_pin_handler (host_int_4);
205   void poll_pin_handler (host_int_4);
206
207   void update_txrx_pending_pins ();
208 };
209
210
211 // ----------------------------------------------------------------------------
212
213
214 class nop_audio: public generic_audio
215 {
216 public:
217   nop_audio () {}
218   ~nop_audio () {}
219
220 private:
221   // The inherited virtual functions 
222   bool begin_tx (const audio_config&) { return false; }
223   void end_tx () {}
224   bool begin_rx (const audio_config&) { return false; }
225   void end_rx () {}
226   string poll_tx (const string&) { return string(); }
227   string poll_rx () { return string(); }
228 };
229
230
231 // ----------------------------------------------------------------------------
232
233
234 class fd_audio: public generic_audio
235 {
236 public:
237   fd_audio ();
238   ~fd_audio ();
239
240 protected:
241   virtual bool set_audio_config (int, const audio_config&) = 0;
242
243 private:
244   string devaudio;
245   int tx_fd;
246   int rx_fd;
247
248   // The inherited virtual functions 
249   bool begin_tx (const audio_config&);
250   void end_tx ();
251   bool begin_rx (const audio_config&);
252   void end_rx ();
253   string poll_tx (const string&);
254   string poll_rx ();
255 };
256
257
258 class linux_audio: public fd_audio 
259
260   // XXX
261 public:
262   linux_audio () {}
263   ~linux_audio () {}
264
265 protected:
266   bool set_audio_config (int, const audio_config&) { return true; }
267 };
268
269 class solaris_audio: public fd_audio 
270 {
271 public:
272   solaris_audio () {}
273   ~solaris_audio () {}
274
275   // XXX
276   bool set_audio_config (int, const audio_config&) { return true; }
277 };
278
279
280 // ----------------------------------------------------------------------------
281
282
283 #if defined(SOUND_CYGWIN)
284
285 struct win32_audio_buf
286 {
287   WAVEHDR header;
288   HANDLE block_handle;
289   HPSTR block_addr;
290
291   win32_audio_buf (host_int_4 size);
292   virtual ~win32_audio_buf ();
293
294   bool done_p ();
295   string buffer ();
296 };
297
298
299 // Disposable, variable-size transmit buffer
300 struct win32_audio_tx_buf: public win32_audio_buf
301 {
302   HWAVEOUT device;
303
304   win32_audio_tx_buf (HWAVEOUT dev, const string& buf);
305   ~win32_audio_tx_buf ();
306 };
307
308
309 // Reusable, fixed-size receive buffer
310 struct win32_audio_rx_buf: public win32_audio_buf
311 {
312   HWAVEIN device;
313
314   win32_audio_rx_buf (HWAVEIN dev, host_int_4 size);
315   ~win32_audio_rx_buf ();
316
317 };
318
319
320
321
322 class cygwin_audio: public generic_audio
323 {
324 public:
325   cygwin_audio ();
326   ~cygwin_audio ();
327
328 private:
329   host_int_2 rx_buffer_size;
330   host_int_1 rx_buffer_count;
331
332   HWAVEOUT waveOut;
333   typedef deque<win32_audio_tx_buf*> tx_bufs_t;
334   tx_bufs_t tx_bufs;
335
336   HWAVEIN waveIn;
337   typedef deque<win32_audio_rx_buf*> rx_bufs_t;
338   rx_bufs_t rx_bufs;
339
340   // The inherited virtual functions 
341   bool begin_tx (const audio_config&);
342   bool pending_tx_p ();
343   void end_tx ();
344   bool begin_rx (const audio_config&);
345   void end_rx ();
346   string poll_tx (const string&);
347   string poll_rx ();
348 };
349
350 #endif // CYGWIN
351
352
353 // ----------------------------------------------------------------------------
354
355
356 #endif // COMPONENTS_H