Debian Codegraph @

src/hardware/tandy_sound.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2018 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 /*
20 Based of sn76496.c of the M.A.M.E. project
21 */
22
23 #include "dosbox.h"
24 #include "inout.h"
25 #include "mixer.h"
26 #include "mem.h"
27 #include "setup.h"
28 #include "pic.h"
29 #include "dma.h"
30 #include "hardware.h"
31 #include <cstring>
32 #include <math.h>
33 #include "mame/emu.h"
34 #include "mame/sn76496.h"
35
36
37 #define SOUND_CLOCK (14318180 / 4)
38
39 #define TDAC_DMA_BUFSIZE 1024
40
41 static struct {
42 MixerChannel * chan;
43 bool enabled;
44 Bitu last_write;
45 struct {
46 MixerChannel * chan;
47 bool enabled;
48 struct {
49 Bitu base;
50 Bit8u irq,dma;
51 } hw;
52 struct {
53 Bitu rate;
54 Bit8u buf[TDAC_DMA_BUFSIZE];
55 Bit8u last_sample;
56 DmaChannel * chan;
57 bool transfer_done;
58 } dma;
59 Bit8u mode,control;
60 Bit16u frequency;
61 Bit8u amplitude;
62 bool irq_activated;
63 } dac;
64 } tandy;
65
66 static sn76496_device device_sn76496(machine_config(), 0, 0, SOUND_CLOCK );
67 static ncr8496_device device_ncr8496(machine_config(), 0, 0, SOUND_CLOCK);
68
69 static sn76496_base_device* activeDevice = &device_ncr8496;
70 #define device (*activeDevice)
71
72 static void SN76496Write(Bitu /*port*/,Bitu data,Bitu /*iolen*/) {
73 tandy.last_write=PIC_Ticks;
74 if (!tandy.enabled) {
75 tandy.chan->Enable(true);
76 tandy.enabled=true;
77 }
78 device.write(data);
79
80 // LOG_MSG("3voice write %X at time %7.3f",data,PIC_FullIndex());
81 }
82
83 static void SN76496Update(Bitu length) {
84 //Disable the channel if it's been quiet for a while
85 if ((tandy.last_write+5000)<PIC_Ticks) {
86 tandy.enabled=false;
87 tandy.chan->Enable(false);
88 return;
89 }
90 const Bitu MAX_SAMPLES = 2048;
91 if (length > MAX_SAMPLES)
92 return;
93 Bit16s buffer[MAX_SAMPLES];
94 Bit16s* outputs = buffer;
95
96 device_sound_interface::sound_stream stream;
97 static_cast<device_sound_interface&>(device).sound_stream_update(stream, 0, &outputs, length);
98 tandy.chan->AddSamples_m16(length, buffer);
99 }
100
101 bool TS_Get_Address(Bitu& tsaddr, Bitu& tsirq, Bitu& tsdma) {
102 tsaddr=0;
103 tsirq =0;
104 tsdma =0;
105 if (tandy.dac.enabled) {
106 tsaddr=tandy.dac.hw.base;
107 tsirq =tandy.dac.hw.irq;
108 tsdma =tandy.dac.hw.dma;
109 return true;
110 }
111 return false;
112 }
113
114
115 static void TandyDAC_DMA_CallBack(DmaChannel * /*chan*/, DMAEvent event) {
116 if (event == DMA_REACHED_TC) {
117 tandy.dac.dma.transfer_done=true;
118 PIC_ActivateIRQ(tandy.dac.hw.irq);
119 }
120 }
121
122 static void TandyDACModeChanged(void) {
123 switch (tandy.dac.mode&3) {
124 case 0:
125 // joystick mode
126 break;
127 case 1:
128 break;
129 case 2:
130 // recording
131 break;
132 case 3:
133 // playback
134 tandy.dac.chan->FillUp();
135 if (tandy.dac.frequency!=0) {
136 float freq=3579545.0f/((float)tandy.dac.frequency);
137 tandy.dac.chan->SetFreq((Bitu)freq);
138 float vol=((float)tandy.dac.amplitude)/7.0f;
139 tandy.dac.chan->SetVolume(vol,vol);
140 if ((tandy.dac.mode&0x0c)==0x0c) {
141 tandy.dac.dma.transfer_done=false;
142 tandy.dac.dma.chan=GetDMAChannel(tandy.dac.hw.dma);
143 if (tandy.dac.dma.chan) {
144 tandy.dac.dma.chan->Register_Callback(TandyDAC_DMA_CallBack);
145 tandy.dac.chan->Enable(true);
146 // LOG_MSG("Tandy DAC: playback started with freqency %f, volume %f",freq,vol);
147 }
148 }
149 }
150 break;
151 }
152 }
153
154 static void TandyDACDMAEnabled(void) {
155 TandyDACModeChanged();
156 }
157
158 static void TandyDACDMADisabled(void) {
159 }
160
161 static void TandyDACWrite(Bitu port,Bitu data,Bitu /*iolen*/) {
162 switch (port) {
163 case 0xc4: {
164 Bitu oldmode = tandy.dac.mode;
165 tandy.dac.mode = (Bit8u)(data&0xff);
166 if ((data&3)!=(oldmode&3)) {
167 TandyDACModeChanged();
168 }
169 if (((data&0x0c)==0x0c) && ((oldmode&0x0c)!=0x0c)) {
170 TandyDACDMAEnabled();
171 } else if (((data&0x0c)!=0x0c) && ((oldmode&0x0c)==0x0c)) {
172 TandyDACDMADisabled();
173 }
174 }
175 break;
176 case 0xc5:
177 switch (tandy.dac.mode&3) {
178 case 0:
179 // joystick mode
180 break;
181 case 1:
182 tandy.dac.control = (Bit8u)(data&0xff);
183 break;
184 case 2:
185 break;
186 case 3:
187 // direct output
188 break;
189 }
190 break;
191 case 0xc6:
192 tandy.dac.frequency = (tandy.dac.frequency & 0xf00) | (Bit8u)(data & 0xff);
193 switch (tandy.dac.mode&3) {
194 case 0:
195 // joystick mode
196 break;
197 case 1:
198 case 2:
199 case 3:
200 TandyDACModeChanged();
201 break;
202 }
203 break;
204 case 0xc7:
205 tandy.dac.frequency = (tandy.dac.frequency & 0x00ff) | (((Bit8u)(data & 0xf)) << 8);
206 tandy.dac.amplitude = (Bit8u)(data>>5);
207 switch (tandy.dac.mode&3) {
208 case 0:
209 // joystick mode
210 break;
211 case 1:
212 case 2:
213 case 3:
214 TandyDACModeChanged();
215 break;
216 }
217 break;
218 }
219 }
220
221 static Bitu TandyDACRead(Bitu port,Bitu /*iolen*/) {
222 switch (port) {
223 case 0xc4:
224 return (tandy.dac.mode&0x77) | (tandy.dac.irq_activated ? 0x08 : 0x00);
225 case 0xc6:
226 return (Bit8u)(tandy.dac.frequency&0xff);
227 case 0xc7:
228 return (Bit8u)(((tandy.dac.frequency>>8)&0xf) | (tandy.dac.amplitude<<5));
229 }
230 LOG_MSG("Tandy DAC: Read from unknown %X",port);
231 return 0xff;
232 }
233
234 static void TandyDACGenerateDMASound(Bitu length) {
235 if (length) {
236 Bitu read=tandy.dac.dma.chan->Read(length,tandy.dac.dma.buf);
237 tandy.dac.chan->AddSamples_m8(read,tandy.dac.dma.buf);
238 if (read < length) {
239 if (read>0) tandy.dac.dma.last_sample=tandy.dac.dma.buf[read-1];
240 for (Bitu ct=read; ct < length; ct++) {
241 tandy.dac.chan->AddSamples_m8(1,&tandy.dac.dma.last_sample);
242 }
243 }
244 }
245 }
246
247 static void TandyDACUpdate(Bitu length) {
248 if (tandy.dac.enabled && ((tandy.dac.mode&0x0c)==0x0c)) {
249 if (!tandy.dac.dma.transfer_done) {
250 Bitu len = length;
251 TandyDACGenerateDMASound(len);
252 } else {
253 for (Bitu ct=0; ct < length; ct++) {
254 tandy.dac.chan->AddSamples_m8(1,&tandy.dac.dma.last_sample);
255 }
256 }
257 } else {
258 tandy.dac.chan->AddSilence();
259 }
260 }
261
262
263 class TANDYSOUND: public Module_base {
264 private:
265 IO_WriteHandleObject WriteHandler[4];
266 IO_ReadHandleObject ReadHandler[4];
267 MixerObject MixerChan;
268 MixerObject MixerChanDAC;
269 public:
270 TANDYSOUND(Section* configuration):Module_base(configuration){
271 Section_prop * section=static_cast<Section_prop *>(configuration);
272
273 bool enable_hw_tandy_dac=true;
274 Bitu sbport, sbirq, sbdma;
275 if (SB_Get_Address(sbport, sbirq, sbdma)) {
276 enable_hw_tandy_dac=false;
277 }
278
279 //Select the correct tandy chip implementation
280 if (machine == MCH_PCJR) activeDevice = &device_sn76496;
281 else activeDevice = &device_ncr8496;
282
283 //0.74 has the ncr8496 for all machines.
284 activeDevice = &device_ncr8496;
285
286 real_writeb(0x40,0xd4,0x00);
287 if (IS_TANDY_ARCH) {
288 /* enable tandy sound if tandy=true/auto */
289 if ((strcmp(section->Get_string("tandy"),"true")!=0) &&
290 (strcmp(section->Get_string("tandy"),"on")!=0) &&
291 (strcmp(section->Get_string("tandy"),"auto")!=0)) return;
292 } else {
293 /* only enable tandy sound if tandy=true */
294 if ((strcmp(section->Get_string("tandy"),"true")!=0) &&
295 (strcmp(section->Get_string("tandy"),"on")!=0)) return;
296
297 /* ports from second DMA controller conflict with tandy ports */
298 CloseSecondDMAController();
299
300 if (enable_hw_tandy_dac) {
301 WriteHandler[2].Install(0x1e0,SN76496Write,IO_MB,2);
302 WriteHandler[3].Install(0x1e4,TandyDACWrite,IO_MB,4);
303 // ReadHandler[3].Install(0x1e4,TandyDACRead,IO_MB,4);
304 }
305 }
306
307
308 Bit32u sample_rate = section->Get_int("tandyrate");
309 tandy.chan=MixerChan.Install(&SN76496Update,sample_rate,"TANDY");
310
311 WriteHandler[0].Install(0xc0,SN76496Write,IO_MB,2);
312
313 if (enable_hw_tandy_dac) {
314 // enable low-level Tandy DAC emulation
315 WriteHandler[1].Install(0xc4,TandyDACWrite,IO_MB,4);
316 ReadHandler[1].Install(0xc4,TandyDACRead,IO_MB,4);
317
318 tandy.dac.enabled=true;
319 tandy.dac.chan=MixerChanDAC.Install(&TandyDACUpdate,sample_rate,"TANDYDAC");
320
321 tandy.dac.hw.base=0xc4;
322 tandy.dac.hw.irq =7;
323 tandy.dac.hw.dma =1;
324 } else {
325 tandy.dac.enabled=false;
326 tandy.dac.hw.base=0;
327 tandy.dac.hw.irq =0;
328 tandy.dac.hw.dma =0;
329 }
330
331 tandy.dac.control=0;
332 tandy.dac.mode =0;
333 tandy.dac.irq_activated=false;
334 tandy.dac.frequency=0;
335 tandy.dac.amplitude=0;
336 tandy.dac.dma.last_sample=0;
337
338
339 tandy.enabled=false;
340 real_writeb(0x40,0xd4,0xff); /* BIOS Tandy DAC initialization value */
341
342 ((device_t&)device).device_start();
343 device.convert_samplerate(sample_rate);
344
345 }
346 ~TANDYSOUND(){ }
347 };
348
349
350
351 static TANDYSOUND* test;
352
353 void TANDYSOUND_ShutDown(Section* /*sec*/) {
354 delete test;
355 }
356
357 void TANDYSOUND_Init(Section* sec) {
358 test = new TANDYSOUND(sec);
359 sec->AddDestroyFunction(&TANDYSOUND_ShutDown,true);
360 }