Fixed compilation error
[bochs-mirror.git] / iodev / sb16.h
blobe3dde67541558bab280b1e848649fc0ec38bde6c
1 /////////////////////////////////////////////////////////////////////////
2 // $Id: sb16.h,v 1.31 2008/07/27 15:41:43 vruppert Exp $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2001 MandrakeSoft S.A.
6 //
7 // MandrakeSoft S.A.
8 // 43, rue d'Aboukir
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 // This file (SB16.H) written and donated by Josef Drexler
29 #ifndef BX_IODEV_SB16_H
30 #define BX_IODEV_SB16_H
32 #if BX_USE_SB16_SMF
33 # define BX_SB16_SMF static
34 # define BX_SB16_THIS theSB16Device->
35 # define BX_SB16_THISP (theSB16Device)
36 #else
37 # define BX_SB16_SMF
38 # define BX_SB16_THIS this->
39 # define BX_SB16_THISP (this)
40 #endif
42 #if BX_USE_SOUND_VIRTUAL
43 # define BX_SOUND_VIRTUAL virtual
44 # define BX_SOUND_OUTPUT_C_DEF bx_sound_output_c
45 #else
46 # define BX_SOUND_VIRTUAL
47 # define BX_SOUND_OUTPUT_C_DEF BX_SOUND_OUTPUT_C
48 #endif
50 // If the buffer commands are to be inlined:
51 #define BX_SB16_BUFINL BX_CPP_INLINE
52 // BX_CPP_INLINE is defined to the inline keyword for the C++ compiler.
54 // maximum number of patch translations
55 #define BX_SB16_PATCHTABLESIZE 1024
57 // the resources. Of these, IRQ and DMA's can be changed via a DSP command
58 #define BX_SB16_IO 0x220 // IO base address of DSP, mixer & FM part
59 #define BX_SB16_IOLEN 16 // number of addresses covered
60 #define BX_SB16_IOMPU 0x330 // IO base address of MPU402 part
61 #define BX_SB16_IOMPULEN 4 // number of addresses covered
62 #define BX_SB16_IOADLIB 0x388 // equivalent to 0x220..0x223 and 0x228..0x229
63 #define BX_SB16_IOADLIBLEN 4 // number of addresses covered
64 #define BX_SB16_IRQ theSB16Device->currentirq
65 #define BX_SB16_IRQMPU BX_SB16_IRQ // IRQ for the MPU401 part - same value
66 #define BX_SB16_DMAL theSB16Device->currentdma8
67 #define BX_SB16_DMAH theSB16Device->currentdma16
70 A few notes:
71 IRQ, DMA8bit and DMA16bit are for the DSP part. These
72 are changeable at runtime in mixer registers 0x80 and 0x81.
73 The defaults after a mixer initialization are IRQ 5, DMA8 1, no DMA16
75 Any of the address lengths can be zero to disable that particular
76 subdevice. Turning off the DSP still leaves FM music enabled on the
77 BX_SB16_IOADLIB ports, unless those are disabled as well.
79 BX_SB16_IOMPULEN should be 4 or 2. In the latter case, the emulator
80 is completely invisible, and runtime changes are not possible
82 BX_SB16_IOADLIBLEN should be 2 or 4. If 0, Ports 0x388.. don't
83 get used, but the OPL2 can still be accessed at 0x228..0x229.
84 If 2, the usual Adlib emulation is enabled. If 4, an OPL3 is
85 emulated at adresses 0x388..0x38b, or two separate OPL2's.
88 /* Definitions for the output functions */
89 #define BX_SOUND_OUTPUT_OK 0
90 #define BX_SOUND_OUTPUT_ERR 1
92 // this is the size of a DMA chunk sent to output
93 // it should not be too large to avoid lag, and not too
94 // small to avoid unnecessary overhead.
95 #define BX_SOUND_OUTPUT_WAVEPACKETSIZE 8192
97 #define BX_SB16_MIX_REG 0x100 // total number of mixer registers
99 // The array containing an instrument/bank remapping
100 struct bx_sb16_ins_map {
101 Bit8u oldbankmsb, oldbanklsb, oldprogch;
102 Bit8u newbankmsb, newbanklsb, newprogch;
105 // One operator of the FM emulation
106 #define BX_SB16_FM_NOP 36 // OPL3 has 36 operators
107 #define BX_SB16_FM_OPB 6 // one operator has 6 bytes
108 typedef Bit8u bx_sb16_fm_operator[BX_SB16_FM_OPB];
109 /* Explanation of the values:
110 (note [xx] is one bit for xx; [5 xx] is five bits for xx,
111 all bits listed MSB to LSB)
113 [0] = [Tremolo][Vibrato][Sustain][KSR][4 Frequency Multiply]
114 [1] = [2 Key Scale Level][6 Output Level for modulators, reserved for others]
115 [2] = [4 Attack Rate][4 Decay Rate]
116 [3] = [4 Sustain Level][4 Release Rate]
117 [4] = [2 reserved][Right][Left][3 Feedback Factor][SynthType]
118 [5] = [5 reserved][3 Waveform Select]
120 Frequency and Output Level are really properties of the channel,
121 so they get stored there. However, Output Level of the modulator
122 in FM synthesis varies the instrument.
124 [4] is only set for the first operator, and zeroed for the others.
126 All reserved bits are zeroed.
129 // One channel (1 to 4 operators)
130 #define BX_SB16_FM_NCH 18 // OPL3 has at most 18 channels
132 typedef struct {
133 int nop; // number of operators used: 0=disabled, 1=percussion, 2 or 4=melodic
134 int ncarr; // how many carriers this channel has (1..3)
135 int opnum[4]; // operator numbers
136 Bit16u freq; // frequency (in a special code)
137 Bit32u afreq; // actual frequency in milli-Hertz (10^-3 Hz)
138 Bit8u midichan; // assigned midi channel
139 bx_bool needprogch; // has the instrument changed
140 Bit8u midinote; // currently playing midi note
141 bx_bool midion; // is the note on
142 Bit16u midibend; // current value of the pitch bender
143 Bit8u outputlevel[4];// 6-bit output level attenuations
144 Bit8u midivol; // current midi volume (velocity)
145 } bx_sb16_fm_channel;
148 // This is the class for the input and
149 // output FIFO buffers of the SB16
151 class bx_sb16_buffer {
152 public:
154 BX_SB16_BUFINL bx_sb16_buffer(void);
155 BX_SB16_BUFINL ~bx_sb16_buffer();
156 BX_SB16_BUFINL void init(int bufferlen);
157 BX_SB16_BUFINL void reset();
159 /* These functions return 1 on success and 0 on error */
160 BX_SB16_BUFINL bx_bool put(Bit8u data); // write one byte in the buffer
161 BX_SB16_BUFINL bx_bool puts(char *data, ...); // write a formatted string to the buffer
162 BX_SB16_BUFINL bx_bool get(Bit8u *data); // read the next available byte
163 BX_SB16_BUFINL bx_bool getw(Bit16u *data); // get word, in order lo/hi
164 BX_SB16_BUFINL bx_bool getw1(Bit16u *data);// get word, in order hi/lo
165 BX_SB16_BUFINL bx_bool full(void); // is the buffer full?
166 BX_SB16_BUFINL bx_bool empty(void); // is it empty?
168 BX_SB16_BUFINL void flush(void); // empty the buffer
169 BX_SB16_BUFINL int bytes(void); // return number of bytes in the buffer
170 BX_SB16_BUFINL Bit8u peek(int ahead); // peek ahead number of bytes
172 /* These are for caching the command number */
173 BX_SB16_BUFINL void newcommand(Bit8u newcmd, int bytes); // start a new command with length bytes
174 BX_SB16_BUFINL Bit8u currentcommand(void); // return the current command
175 BX_SB16_BUFINL void clearcommand(void); // clear the command
176 BX_SB16_BUFINL bx_bool commanddone(void); // return if all bytes have arrived
177 BX_SB16_BUFINL bx_bool hascommand(void); // return if there is a pending command
178 BX_SB16_BUFINL int commandbytes(void); // return the length of the command
181 private:
182 Bit8u *buffer;
183 int head,tail,length;
184 Bit8u command;
185 bx_bool havecommand;
186 int bytesneeded;
190 // forward definition
191 class BX_SOUND_OUTPUT_C_DEF;
193 // The actual emulator class, emulating the sound blaster ports
194 class bx_sb16_c : public bx_devmodel_c {
195 public:
196 bx_sb16_c();
197 virtual ~bx_sb16_c();
198 virtual void init(void);
199 virtual void reset(unsigned type);
200 virtual void register_state(void);
201 virtual void after_restore_state(void);
203 /* Make writelog available to output functions */
204 BX_SB16_SMF void writelog(int loglev, const char *str, ...);
205 // return midimode and wavemode setting (for lowlevel output class)
206 int get_midimode() const {return midimode;}
207 int get_wavemode() const {return wavemode;}
208 // runtimer parameter handler
209 static Bit64s sb16_param_handler(bx_param_c *param, int set, Bit64s val);
211 private:
213 int midimode, wavemode, loglevel;
214 Bit32u dmatimer;
215 FILE *logfile, *midifile, *wavefile; // the output files or devices
216 BX_SOUND_OUTPUT_C_DEF *output;// the output class
217 int currentirq;
218 int currentdma8;
219 int currentdma16;
221 // the MPU 401 relevant variables
222 struct bx_sb16_mpu_struct {
223 bx_sb16_buffer datain, dataout, cmd, midicmd;
224 bx_bool uartmode, irqpending, forceuartmode, singlecommand;
226 int banklsb[BX_SB16_PATCHTABLESIZE];
227 int bankmsb[BX_SB16_PATCHTABLESIZE]; // current patch lists
228 int program[BX_SB16_PATCHTABLESIZE];
230 int timer_handle, current_timer; // no. of delta times passed
231 Bit32u last_delta_time; // timer value at last command
232 bx_bool outputinit;
233 } mpu401;
235 // the DSP variables
236 struct bx_sb16_dsp_struct {
237 bx_sb16_buffer datain, dataout;
238 Bit8u resetport; // last value written to the reset port
239 Bit8u speaker,prostereo; // properties of the sound input/output
240 bx_bool irqpending; // Is an IRQ pending (not ack'd)
241 bx_bool midiuartmode; // Is the DSP in MIDI UART mode
242 Bit8u testreg;
243 struct bx_sb16_dsp_dma_struct {
244 // Properties of the current DMA transfer:
245 // mode= 0: no transfer, 1: single-cycle transfer, 2: auto-init DMA
246 // bits= 8 or 16
247 // fifo= ?? Bit used in DMA command, no idea what it means...
248 // output= 0: input, 1: output
249 // bps= bytes per sample; =(dmabits/8)*(dmastereo+1)
250 // stereo= 0: mono, 1: stereo
251 // issigned= 0: unsigned data, 1: signed data
252 // highspeed= 0: normal mode, 1: highspeed mode (only SBPro)
253 // timer= so many us between data bytes
254 int mode, bits, bps, format, timer;
255 bx_bool fifo, output, stereo, issigned, highspeed;
256 Bit16u count; // bytes remaining in this transfer
257 Bit8u *chunk; // buffers up to BX_SOUND_OUTPUT_WAVEPACKETSIZE bytes
258 int chunkindex; // index into the buffer
259 int chunkcount; // for input: size of the recorded input
260 Bit16u timeconstant;
261 Bit16u blocklength, samplerate;
262 } dma;
263 int timer_handle; // handle for the DMA timer
264 bx_bool outputinit; // have the output functions been initialized
265 } dsp;
267 // the ASP/CSP registers
268 Bit8u csp_reg[256];
270 enum bx_sb16_fm_mode {single, adlib, dual, opl3, fminit};
272 // the variables common to all FM emulations
273 struct bx_sb16_opl_struct;
274 friend struct bx_sb16_opl_struct;
275 struct bx_sb16_opl_struct {
276 bx_sb16_fm_mode mode;
277 // modes: single: one OPL2 (OPL3 disabled),
278 // adlib: one OPL2 (no OPL3),
279 // dual: two seperate OPL2
280 // opl3: one OPL3 (enabled)
282 int timer_handle;
283 int timer_running;
284 Bit16u midichannels; // bitmask: unused midichannels
285 int drumchannel; // midi channel for percussion (10)
286 int index[2]; // index register for the two chips
287 int wsenable[2]; // wave form select enable
288 Bit16u timer[4]; // two timers on each chip
289 Bit16u timerinit[4]; // initial timer counts
290 int tmask[2]; // the timer masking byte for both chips
291 int tflag[2]; // shows if the timer overflow has occured
292 int percmode[2]; // percussion mode enabled
293 int cyhhnote[2]; // cymbal and high hat midi notes
294 int cyhhon[2]; // cymbal and high hat notes on
295 bx_sb16_fm_operator oper[BX_SB16_FM_NOP];
296 bx_sb16_fm_channel chan[BX_SB16_FM_NCH];
297 } opl;
299 struct bx_sb16_mixer_struct {
300 Bit8u regindex;
301 Bit8u reg[BX_SB16_MIX_REG];
302 } mixer;
304 struct bx_sb16_emul_struct {
305 bx_sb16_buffer datain, dataout;
306 bx_sb16_ins_map remaplist[256];
307 Bit16u remaps;
308 } emuldata;
310 /* DMA input and output, 8 and 16 bit */
311 BX_SB16_SMF void dma_write8(Bit8u *data_byte);
312 BX_SB16_SMF void dma_read8(Bit8u *data_byte);
313 BX_SB16_SMF void dma_write16(Bit16u *data_word);
314 BX_SB16_SMF void dma_read16(Bit16u *data_word);
316 /* the MPU 401 part of the emulator */
317 BX_SB16_SMF Bit32u mpu_status(); // read status port 3x1
318 BX_SB16_SMF void mpu_command(Bit32u value); // write command port 3x1
319 BX_SB16_SMF Bit32u mpu_dataread(); // read data port 3x0
320 BX_SB16_SMF void mpu_datawrite(Bit32u value); // write data port 3x0
321 BX_SB16_SMF void mpu_mididata(Bit32u value); // get a midi byte
322 static void mpu_timer (void *);
324 /* The DSP part */
325 BX_SB16_SMF void dsp_reset(Bit32u value); // write to reset port 2x6
326 BX_SB16_SMF Bit32u dsp_dataread(); // read from data port 2xa
327 BX_SB16_SMF void dsp_datawrite(Bit32u value); // write to data port 2xa
328 BX_SB16_SMF Bit32u dsp_bufferstatus(); // read buffer status 2xc
329 BX_SB16_SMF Bit32u dsp_status(); // read dsp status 2xe
330 BX_SB16_SMF void dsp_getsamplebyte(Bit8u value);
331 BX_SB16_SMF Bit8u dsp_putsamplebyte();
332 BX_SB16_SMF void dsp_sendwavepacket();
333 BX_SB16_SMF void dsp_getwavepacket();
334 BX_SB16_SMF Bit32u dsp_irq16ack(); // ack 16 bit IRQ 2xf
335 BX_SB16_SMF void dsp_dma(Bit8u command, Bit8u mode, Bit16u length, Bit8u comp);
336 // initiate a DMA transfer
337 BX_SB16_SMF void dsp_dmadone(); // stop a DMA transfer
338 BX_SB16_SMF void dsp_enabledma(); // enable the transfer
339 BX_SB16_SMF void dsp_disabledma(); // temporarily disable DMA
340 static void dsp_dmatimer (void *);
342 /* The mixer part */
343 BX_SB16_SMF Bit32u mixer_readdata(void);
344 BX_SB16_SMF void mixer_writedata(Bit32u value);
345 BX_SB16_SMF void mixer_writeregister(Bit32u value);
346 BX_SB16_SMF void set_irq_dma();
348 /* The emulator ports to change emulator properties */
349 BX_SB16_SMF Bit32u emul_read (void); // read emulator port
350 BX_SB16_SMF void emul_write(Bit32u value); // write emulator port
352 /* The FM emulation part */
353 BX_SB16_SMF void opl_entermode(bx_sb16_fm_mode newmode);
354 BX_SB16_SMF Bit32u opl_status(int chipid);
355 BX_SB16_SMF void opl_index(Bit32u value, int chipid);
356 BX_SB16_SMF void opl_data(Bit32u value, int chipid);
357 static void opl_timer(void *);
358 BX_SB16_SMF void opl_timerevent(void);
359 BX_SB16_SMF void opl_changeop(int channum, int opernum, int byte, int value);
360 BX_SB16_SMF void opl_settimermask(int value, int chipid);
361 BX_SB16_SMF void opl_set4opmode(int new4opmode);
362 BX_SB16_SMF void opl_setmodulation(int channel);
363 BX_SB16_SMF void opl_setpercussion(Bit8u value, int chipid);
364 BX_SB16_SMF void opl_setvolume(int channel, int opnum, int outlevel);
365 BX_SB16_SMF void opl_setfreq(int channel);
366 BX_SB16_SMF void opl_keyonoff(int channel, bx_bool onoff);
367 BX_SB16_SMF void opl_midichannelinit(int channel);
369 /* several high level sound handlers */
370 BX_SB16_SMF int currentdeltatime();
371 BX_SB16_SMF void processmidicommand(bx_bool force);
372 BX_SB16_SMF void midiremapprogram(int channel); // remap program change
373 BX_SB16_SMF int converttodeltatime(Bit32u deltatime, Bit8u value[4]);
374 BX_SB16_SMF void writemidicommand(int command, int length, Bit8u data[]);
375 BX_SB16_SMF void writedeltatime(Bit32u deltatime);
376 // write in delta time coding
378 BX_SB16_SMF void initmidifile(); // Write midi file header
379 BX_SB16_SMF void finishmidifile(); // write track length etc.
380 BX_SB16_SMF void initvocfile(); // Write voc file header
381 BX_SB16_SMF void writevocblock(int block, Bit32u headerlen, Bit8u header[],
382 Bit32u datalen, Bit8u data[]);
383 BX_SB16_SMF void finishvocfile(); // close voc file
385 /* The port IO multiplexer functions */
387 static Bit32u read_handler(void *this_ptr, Bit32u address, unsigned io_len);
388 static void write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len);
390 #if !BX_USE_SB16_SMF
391 Bit32u read(Bit32u address, unsigned io_len);
392 void write(Bit32u address, Bit32u value, unsigned io_len);
393 #endif
396 // The class with the output functions
397 class bx_sound_output_c : public logfunctions {
398 public:
400 /* These functions are the sound output functions, sending
401 the music/sound to the OS specific driver.
402 They are in a different file (sound.cc) because they are
403 non-portable, while everything in sb16.cc is portable */
405 bx_sound_output_c(bx_sb16_c *sb16);
406 BX_SOUND_VIRTUAL ~bx_sound_output_c();
408 BX_SOUND_VIRTUAL int waveready();
409 BX_SOUND_VIRTUAL int midiready();
411 BX_SOUND_VIRTUAL int openmidioutput(char *device);
412 BX_SOUND_VIRTUAL int sendmidicommand(int delta, int command, int length, Bit8u data[]);
413 BX_SOUND_VIRTUAL int closemidioutput();
415 BX_SOUND_VIRTUAL int openwaveoutput(char *device);
416 BX_SOUND_VIRTUAL int startwaveplayback(int frequency, int bits, int stereo, int format);
417 BX_SOUND_VIRTUAL int sendwavepacket(int length, Bit8u data[]);
418 BX_SOUND_VIRTUAL int stopwaveplayback();
419 BX_SOUND_VIRTUAL int closewaveoutput();
422 #define WRITELOG sb16->writelog
423 #define BOTHLOG(x) (x)
424 #ifndef BX_SOUNDLOW
425 #define MIDILOG(x) ((BX_SB16_THIS midimode>0?x:0x7f))
426 #define WAVELOG(x) ((BX_SB16_THIS wavemode>0?x:0x7f))
427 #else
428 #define MIDILOG(x) ((sb16->get_midimode()>0?x:0x7f))
429 #define WAVELOG(x) ((sb16->get_wavemode()>0?x:0x7f))
430 #endif
432 #endif