Debian Codegraph @

src/hardware/disney.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: disney.cpp,v 1.17 2009-05-14 17:04:37 qbix79 Exp $ */
20
21 #include <string.h>
22 #include "dosbox.h"
23 #include "inout.h"
24 #include "mixer.h"
25 #include "pic.h"
26 #include "setup.h"
27
28 #define DISNEY_BASE 0x0378
29
30 #define DISNEY_SIZE 128
31
32 typedef struct _dac_channel {
33 Bit8u buffer[DISNEY_SIZE]; // data buffer
34 Bitu used; // current data buffer level
35 double speedcheck_sum;
36 double speedcheck_last;
37 bool speedcheck_failed;
38 bool speedcheck_init;
39 } dac_channel;
40
41 static struct {
42 // parallel port stuff
43 Bit8u data;
44 Bit8u status;
45 Bit8u control;
46 // the D/A channels
47 dac_channel da[2];
48
49 Bitu last_used;
50 MixerObject * mo;
51 MixerChannel * chan;
52 bool stereo;
53 // which channel do we use for mono output?
54 // and the channel used for stereo
55 dac_channel* leader;
56
57 Bitu state;
58 Bitu interface_det;
59 Bitu interface_det_ext;
60 } disney;
61
62 #define DS_IDLE 0
63 #define DS_RUNNING 1
64 #define DS_FINISH 2
65 #define DS_ANALYZING 3
66
67 static void DISNEY_CallBack(Bitu len);
68
69 static void DISNEY_disable(Bitu) {
70 if(disney.mo) {
71 disney.chan->AddSilence();
72 disney.chan->Enable(false);
73 delete disney.mo;
74 }
75 disney.leader = 0;
76 disney.last_used = 0;
77 disney.mo = 0;
78 disney.state = DS_IDLE;
79 disney.interface_det = 0;
80 disney.interface_det_ext = 0;
81 disney.stereo = false;
82 }
83
84 static void DISNEY_enable(Bitu freq) {
85 if(freq < 500 || freq > 100000) {
86 // try again..
87 disney.state = DS_IDLE;
88 return;
89 } else {
90 #if 0
91 if(disney.stereo) LOG(LOG_MISC,LOG_NORMAL)("disney enable %d Hz, stereo",freq);
92 else LOG(LOG_MISC,LOG_NORMAL)("disney enable %d Hz, mono",freq);
93 #endif
94 disney.mo = new MixerObject();
95 disney.chan=disney.mo->Install(&DISNEY_CallBack,freq,"DISNEY");
96 disney.chan->Enable(true);
97 disney.state = DS_RUNNING;
98 }
99 }
100
101 static void DISNEY_analyze(Bitu channel){
102 switch(disney.state) {
103 case DS_RUNNING: // should not get here
104 break;
105 case DS_IDLE:
106 // initialize channel data
107 for(int i = 0; i < 2; i++) {
108 disney.da[i].used = 0;
109 disney.da[i].speedcheck_sum = 0;
110 disney.da[i].speedcheck_failed = false;
111 disney.da[i].speedcheck_init = false;
112 }
113 disney.da[channel].speedcheck_last = PIC_FullIndex();
114 disney.da[channel].speedcheck_init = true;
115
116 disney.state = DS_ANALYZING;
117 break;
118
119 case DS_FINISH:
120 {
121 // detect stereo: if we have about the same data amount in both channels
122 Bits st_diff = disney.da[0].used - disney.da[1].used;
123
124 // find leader channel (the one with higher rate) [this good for the stereo case?]
125 if(disney.da[0].used > disney.da[1].used) {
126 //disney.monochannel=0;
127 disney.leader = &disney.da[0];
128 } else {
129 //disney.monochannel=1;
130 disney.leader = &disney.da[1];
131 }
132
133 if((st_diff < 5) && (st_diff > -5)) disney.stereo = true;
134 else disney.stereo = false;
135
136 // calculate rate for both channels
137 Bitu ch_speed[2];
138
139 for(Bitu i = 0; i < 2; i++) {
140 ch_speed[i] = (Bitu)(1.0/((disney.da[i].speedcheck_sum/1000.0) /
141 (float)(((float)disney.da[i].used)-1.0))); // -1.75
142 }
143
144 // choose the larger value
145 DISNEY_enable(ch_speed[0] > ch_speed[1]?
146 ch_speed[0]:ch_speed[1]); // TODO
147 break;
148 }
149 case DS_ANALYZING:
150 {
151 double current = PIC_FullIndex();
152 dac_channel* cch = &disney.da[channel];
153
154 if(!cch->speedcheck_init) {
155 cch->speedcheck_init = true;
156 cch->speedcheck_last = current;
157 break;
158 }
159 cch->speedcheck_sum += current - cch->speedcheck_last;
160 //LOG_MSG("t=%f",current - cch->speedcheck_last);
161
162 // sanity checks (printer...)
163 if((current - cch-> speedcheck_last) < 0.01 ||
164 (current - cch-> speedcheck_last) > 2)
165 cch->speedcheck_failed = true;
166
167 // if both are failed we are back at start
168 if(disney.da[0].speedcheck_failed && disney.da[1].speedcheck_failed) {
169 disney.state=DS_IDLE;
170 break;
171 }
172
173 cch->speedcheck_last = current;
174
175 // analyze finish condition
176 if(disney.da[0].used > 30 || disney.da[1].used > 30)
177 disney.state = DS_FINISH;
178 break;
179 }
180 }
181 }
182
183 static void disney_write(Bitu port,Bitu val,Bitu iolen) {
184 //LOG_MSG("write disney time %f addr%x val %x",PIC_FullIndex(),port,val);
185 disney.last_used=PIC_Ticks;
186 switch (port-DISNEY_BASE) {
187 case 0: /* Data Port */
188 {
189 disney.data=val;
190 // if data is written here too often without using the stereo
191 // mechanism we use the simple DAC machanism.
192 if(disney.state != DS_RUNNING) {
193 disney.interface_det++;
194 if(disney.interface_det > 5)
195 DISNEY_analyze(0);
196 }
197 if(disney.interface_det > 5) {
198 if(disney.da[0].used < DISNEY_SIZE) {
199 disney.da[0].buffer[disney.da[0].used] = disney.data;
200 disney.da[0].used++;
201 } //else LOG_MSG("disney overflow 0");
202 }
203 break;
204 }
205 case 1: /* Status Port */
206 LOG(LOG_MISC,LOG_NORMAL)("DISNEY:Status write %x",val);
207 break;
208 case 2: /* Control Port */
209 if((disney.control & 0x2) && !(val & 0x2)) {
210 if(disney.state != DS_RUNNING) {
211 disney.interface_det = 0;
212 disney.interface_det_ext = 0;
213 DISNEY_analyze(1);
214 }
215
216 // stereo channel latch
217 if(disney.da[1].used < DISNEY_SIZE) {
218 disney.da[1].buffer[disney.da[1].used] = disney.data;
219 disney.da[1].used++;
220 } //else LOG_MSG("disney overflow 1");
221 }
222
223 if((disney.control & 0x1) && !(val & 0x1)) {
224 if(disney.state != DS_RUNNING) {
225 disney.interface_det = 0;
226 disney.interface_det_ext = 0;
227 DISNEY_analyze(0);
228 }
229 // stereo channel latch
230 if(disney.da[0].used < DISNEY_SIZE) {
231 disney.da[0].buffer[disney.da[0].used] = disney.data;
232 disney.da[0].used++;
233 } //else LOG_MSG("disney overflow 0");
234 }
235
236 if((disney.control & 0x8) && !(val & 0x8)) {
237 // emulate a device with 16-byte sound FIFO
238 if(disney.state != DS_RUNNING) {
239 disney.interface_det_ext++;
240 disney.interface_det = 0;
241 if(disney.interface_det_ext > 5) {
242 disney.leader = &disney.da[0];
243 DISNEY_enable(7000);
244 }
245 }
246 if(disney.interface_det_ext > 5) {
247 if(disney.da[0].used < DISNEY_SIZE) {
248 disney.da[0].buffer[disney.da[0].used] = disney.data;
249 disney.da[0].used++;
250 }
251 }
252 }
253
254 // LOG_WARN("DISNEY:Control write %x",val);
255 if (val&0x10) LOG(LOG_MISC,LOG_ERROR)("DISNEY:Parallel IRQ Enabled");
256 disney.control=val;
257 break;
258 }
259 }
260
261 static Bitu disney_read(Bitu port,Bitu iolen) {
262 Bitu retval;
263 switch (port-DISNEY_BASE) {
264 case 0: /* Data Port */
265 // LOG(LOG_MISC,LOG_NORMAL)("DISNEY:Read from data port");
266 return disney.data;
267 break;
268 case 1: /* Status Port */
269 // LOG(LOG_MISC,"DISNEY:Read from status port %X",disney.status);
270 retval = 0x07;//0x40; // Stereo-on-1 and (or) New-Stereo DACs present
271 if(disney.interface_det_ext > 5) {
272 if (disney.leader && disney.leader->used >= 16){
273 retval |= 0x40; // ack
274 retval &= ~0x4; // interrupt
275 }
276 }
277 if(!(disney.data&0x80)) retval |= 0x80; // pin 9 is wired to pin 11
278 return retval;
279 break;
280 case 2: /* Control Port */
281 LOG(LOG_MISC,LOG_NORMAL)("DISNEY:Read from control port");
282 return disney.control;
283 break;
284 }
285 return 0xff;
286 }
287
288 static void DISNEY_PlayStereo(Bitu len, Bit8u* l, Bit8u* r) {
289 static Bit8u stereodata[DISNEY_SIZE*2];
290 for(Bitu i = 0; i < len; i++) {
291 stereodata[i*2] = l[i];
292 stereodata[i*2+1] = r[i];
293 }
294 disney.chan->AddSamples_s8(len,stereodata);
295 }
296
297 static void DISNEY_CallBack(Bitu len) {
298 if (!len) return;
299
300 // get the smaller used
301 Bitu real_used;
302 if(disney.stereo) {
303 real_used = disney.da[0].used;
304 if(disney.da[1].used < real_used) real_used = disney.da[1].used;
305 } else
306 real_used = disney.leader->used;
307
308 if (real_used >= len) { // enough data for now
309 if(disney.stereo) DISNEY_PlayStereo(len, disney.da[0].buffer, disney.da[1].buffer);
310 else disney.chan->AddSamples_m8(len,disney.leader->buffer);
311
312 // put the rest back to start
313 for(int i = 0; i < 2; i++) {
314 // TODO for mono only one
315 memmove(disney.da[i].buffer,&disney.da[i].buffer[len],DISNEY_SIZE/*real_used*/-len);
316 disney.da[i].used -= len;
317 }
318 // TODO: len > DISNEY
319 } else { // not enough data
320 if(disney.stereo) {
321 Bit8u gapfiller0 = 128;
322 Bit8u gapfiller1 = 128;
323 if(real_used) {
324 gapfiller0 = disney.da[0].buffer[real_used-1];
325 gapfiller1 = disney.da[1].buffer[real_used-1];
326 };
327
328 memset(disney.da[0].buffer+real_used,
329 gapfiller0,len-real_used);
330 memset(disney.da[1].buffer+real_used,
331 gapfiller1,len-real_used);
332
333 DISNEY_PlayStereo(len, disney.da[0].buffer, disney.da[1].buffer);
334 len -= real_used;
335
336 } else { // mono
337 Bit8u gapfiller = 128; //Keep the middle
338 if(real_used) {
339 // fix for some stupid game; it outputs 0 at the end of the stream
340 // causing a click. So if we have at least two bytes availible in the
341 // buffer and the last one is a 0 then ignore that.
342 if(disney.leader->buffer[real_used-1]==0)
343 real_used--;
344 }
345 // do it this way because AddSilence sounds like a gnawing mouse
346 if(real_used)
347 gapfiller = disney.leader->buffer[real_used-1];
348 //LOG_MSG("gapfiller %x, fill len %d, realused %d",gapfiller,len-real_used,real_used);
349 memset(disney.leader->buffer+real_used, gapfiller, len-real_used);
350 disney.chan->AddSamples_m8(len, disney.leader->buffer);
351 }
352 disney.da[0].used =0;
353 disney.da[1].used =0;
354
355 //LOG_MSG("disney underflow %d",len - real_used);
356 }
357 if (disney.last_used+100<PIC_Ticks) {
358 // disable sound output
359 PIC_AddEvent(DISNEY_disable,0.0001f); // I think we shouldn't delete the
360 // mixer while we are inside it
361 }
362 }
363
364 class DISNEY: public Module_base {
365 private:
366 IO_ReadHandleObject ReadHandler;
367 IO_WriteHandleObject WriteHandler;
368 //MixerObject MixerChan;
369 public:
370 DISNEY(Section* configuration):Module_base(configuration) {
371 Section_prop * section=static_cast<Section_prop *>(configuration);
372 if(!section->Get_bool("disney")) return;
373
374 WriteHandler.Install(DISNEY_BASE,disney_write,IO_MB,3);
375 ReadHandler.Install(DISNEY_BASE,disney_read,IO_MB,3);
376
377 disney.status=0x84;
378 disney.control=0;
379 disney.last_used=0;
380
381 disney.mo=0;
382 DISNEY_disable(0);
383 }
384 ~DISNEY(){
385 DISNEY_disable(0);
386 }
387 };
388
389 static DISNEY* test;
390
391 static void DISNEY_ShutDown(Section* sec){
392 delete test;
393 }
394
395 void DISNEY_Init(Section* sec) {
396 test = new DISNEY(sec);
397 sec->AddDestroyFunction(&DISNEY_ShutDown,true);
398 }