147b43a1fSPaolo Bonzini /* 247b43a1fSPaolo Bonzini * GUSEMU32 - API 347b43a1fSPaolo Bonzini * 447b43a1fSPaolo Bonzini * Copyright (C) 2000-2007 Tibor "TS" Schütz 547b43a1fSPaolo Bonzini * 647b43a1fSPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 747b43a1fSPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 847b43a1fSPaolo Bonzini * in the Software without restriction, including without limitation the rights 947b43a1fSPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1047b43a1fSPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 1147b43a1fSPaolo Bonzini * furnished to do so, subject to the following conditions: 1247b43a1fSPaolo Bonzini * 1347b43a1fSPaolo Bonzini * The above copyright notice and this permission notice shall be included in 1447b43a1fSPaolo Bonzini * all copies or substantial portions of the Software. 1547b43a1fSPaolo Bonzini * 1647b43a1fSPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1747b43a1fSPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1847b43a1fSPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1947b43a1fSPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2047b43a1fSPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2147b43a1fSPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2247b43a1fSPaolo Bonzini * THE SOFTWARE. 2347b43a1fSPaolo Bonzini */ 2447b43a1fSPaolo Bonzini 2547b43a1fSPaolo Bonzini #ifndef GUSEMU_H 2647b43a1fSPaolo Bonzini #define GUSEMU_H 2747b43a1fSPaolo Bonzini 2847b43a1fSPaolo Bonzini /* data types (need to be adjusted if neither a VC6 nor a C99 compatible compiler is used) */ 2947b43a1fSPaolo Bonzini 3047b43a1fSPaolo Bonzini #if defined _WIN32 && defined _MSC_VER /* doesn't support other win32 compilers yet, do it yourself... */ 3147b43a1fSPaolo Bonzini typedef unsigned char GUSbyte; 3247b43a1fSPaolo Bonzini typedef unsigned short GUSword; 3347b43a1fSPaolo Bonzini typedef unsigned int GUSdword; 3447b43a1fSPaolo Bonzini typedef signed char GUSchar; 3547b43a1fSPaolo Bonzini typedef signed short GUSsample; 3647b43a1fSPaolo Bonzini #else 3747b43a1fSPaolo Bonzini typedef int8_t GUSchar; 3847b43a1fSPaolo Bonzini typedef uint8_t GUSbyte; 3947b43a1fSPaolo Bonzini typedef uint16_t GUSword; 4047b43a1fSPaolo Bonzini typedef uint32_t GUSdword; 4147b43a1fSPaolo Bonzini typedef int16_t GUSsample; 4247b43a1fSPaolo Bonzini #endif 4347b43a1fSPaolo Bonzini 4447b43a1fSPaolo Bonzini typedef struct _GUSEmuState 4547b43a1fSPaolo Bonzini { 4647b43a1fSPaolo Bonzini GUSbyte *himemaddr; /* 1024*1024 bytes used for storing uploaded samples (+32 additional bytes for read padding) */ 4747b43a1fSPaolo Bonzini GUSbyte *gusdatapos; /* (gusdataend-gusdata) bytes used for storing emulated GF1/mixer register states (32*32+4 bytes in initial GUSemu32 version) */ 4847b43a1fSPaolo Bonzini uint32_t gusirq; 4947b43a1fSPaolo Bonzini uint32_t gusdma; 5047b43a1fSPaolo Bonzini unsigned int timer1fraction; 5147b43a1fSPaolo Bonzini unsigned int timer2fraction; 5247b43a1fSPaolo Bonzini void *opaque; 5347b43a1fSPaolo Bonzini } GUSEmuState; 5447b43a1fSPaolo Bonzini 5547b43a1fSPaolo Bonzini /* ** Callback functions needed: */ 5647b43a1fSPaolo Bonzini /* NMI is defined as hwirq=-1 (not supported (yet?)) */ 5747b43a1fSPaolo Bonzini /* GUS_irqrequest returns the number of IRQs actually scheduled into the virtual machine */ 5847b43a1fSPaolo Bonzini /* Level triggered IRQ simulations normally return 1 */ 5947b43a1fSPaolo Bonzini /* Event triggered IRQ simulation can safely ignore GUS_irqclear calls */ 6047b43a1fSPaolo Bonzini int GUS_irqrequest(GUSEmuState *state, int hwirq, int num);/* needed in both mixer and bus emulation functions. */ 6147b43a1fSPaolo Bonzini void GUS_irqclear( GUSEmuState *state, int hwirq); /* used by gus_write() only - can be left empty for mixer functions */ 6247b43a1fSPaolo Bonzini void GUS_dmarequest(GUSEmuState *state); /* used by gus_write() only - can be left empty for mixer functions */ 6347b43a1fSPaolo Bonzini 6447b43a1fSPaolo Bonzini /* ** ISA bus interface functions: */ 6547b43a1fSPaolo Bonzini 6647b43a1fSPaolo Bonzini /* Port I/O handlers */ 6747b43a1fSPaolo Bonzini /* support the following ports: */ 6847b43a1fSPaolo Bonzini /* 2x0,2x6,2x8...2xF,3x0...3x7; */ 6947b43a1fSPaolo Bonzini /* optional: 388,389 (at least writes should be forwarded or some GUS detection algorithms will fail) */ 7047b43a1fSPaolo Bonzini /* data is passed in host byte order */ 7147b43a1fSPaolo Bonzini unsigned int gus_read( GUSEmuState *state, int port, int size); 7247b43a1fSPaolo Bonzini void gus_write(GUSEmuState *state, int port, int size, unsigned int data); 7347b43a1fSPaolo Bonzini /* size is given in bytes (1 for byte, 2 for word) */ 7447b43a1fSPaolo Bonzini 7547b43a1fSPaolo Bonzini /* DMA data transfer function */ 7647b43a1fSPaolo Bonzini /* data pointed to is passed in native x86 order */ 7747b43a1fSPaolo Bonzini void gus_dma_transferdata(GUSEmuState *state, char *dma_addr, unsigned int count, int TC); 7847b43a1fSPaolo Bonzini /* Called back by GUS_start_DMA as soon as the emulated DMA controller is ready for a transfer to or from GUS */ 7947b43a1fSPaolo Bonzini /* (might be immediately if the DMA controller was programmed first) */ 8047b43a1fSPaolo Bonzini /* dma_addr is an already translated address directly pointing to the beginning of the memory block */ 8147b43a1fSPaolo Bonzini /* do not forget to update DMA states after the call, including the DREQ and TC flags */ 8247b43a1fSPaolo Bonzini /* it is possible to break down a single transfer into multiple ones, but take care that: */ 8347b43a1fSPaolo Bonzini /* -dma_count is actually count-1 */ 8447b43a1fSPaolo Bonzini /* -before and during a transfer, DREQ is set and TC cleared */ 8547b43a1fSPaolo Bonzini /* -when calling gus_dma_transferdata(), TC is only set true for call transferring the last byte */ 8647b43a1fSPaolo Bonzini /* -after the last transfer, DREQ is cleared and TC is set */ 8747b43a1fSPaolo Bonzini 8847b43a1fSPaolo Bonzini /* ** GF1 mixer emulation functions: */ 8947b43a1fSPaolo Bonzini /* Usually, gus_irqgen should be called directly after gus_mixvoices if you can meet the recommended ranges. */ 9047b43a1fSPaolo Bonzini /* If the interrupts are executed immediately (i.e., are synchronous), it may be useful to break this */ 9147b43a1fSPaolo Bonzini /* down into a sequence of gus_mixvoice();gus_irqgen(); calls while mixing an audio block. */ 9247b43a1fSPaolo Bonzini /* If the interrupts are asynchronous, it may be needed to use a separate thread mixing into a temporary */ 9347b43a1fSPaolo Bonzini /* audio buffer in order to avoid quality loss caused by large numsamples and elapsed_time values. */ 9447b43a1fSPaolo Bonzini 9547b43a1fSPaolo Bonzini void gus_mixvoices(GUSEmuState *state, unsigned int playback_freq, unsigned int numsamples, GUSsample *bufferpos); 9647b43a1fSPaolo Bonzini /* recommended range: 10 < numsamples < 100 */ 9747b43a1fSPaolo Bonzini /* lower values may result in increased rounding error, higher values often cause audible timing delays */ 9847b43a1fSPaolo Bonzini 9947b43a1fSPaolo Bonzini void gus_irqgen(GUSEmuState *state, unsigned int elapsed_time); 10047b43a1fSPaolo Bonzini /* recommended range: 80us < elapsed_time < max(1000us, numsamples/playback_freq) */ 10147b43a1fSPaolo Bonzini /* lower values won´t provide any benefit at all, higher values can cause audible timing delays */ 10247b43a1fSPaolo Bonzini /* note: masked timers are also calculated by this function, thus it might be needed even without any IRQs in use! */ 10347b43a1fSPaolo Bonzini 104*175de524SMarkus Armbruster #endif /* GUSEMU_H */ 105