1*47b43a1fSPaolo Bonzini /* 2*47b43a1fSPaolo Bonzini * GUSEMU32 - API 3*47b43a1fSPaolo Bonzini * 4*47b43a1fSPaolo Bonzini * Copyright (C) 2000-2007 Tibor "TS" Schütz 5*47b43a1fSPaolo Bonzini * 6*47b43a1fSPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 7*47b43a1fSPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 8*47b43a1fSPaolo Bonzini * in the Software without restriction, including without limitation the rights 9*47b43a1fSPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10*47b43a1fSPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 11*47b43a1fSPaolo Bonzini * furnished to do so, subject to the following conditions: 12*47b43a1fSPaolo Bonzini * 13*47b43a1fSPaolo Bonzini * The above copyright notice and this permission notice shall be included in 14*47b43a1fSPaolo Bonzini * all copies or substantial portions of the Software. 15*47b43a1fSPaolo Bonzini * 16*47b43a1fSPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*47b43a1fSPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*47b43a1fSPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19*47b43a1fSPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*47b43a1fSPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*47b43a1fSPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22*47b43a1fSPaolo Bonzini * THE SOFTWARE. 23*47b43a1fSPaolo Bonzini */ 24*47b43a1fSPaolo Bonzini 25*47b43a1fSPaolo Bonzini #ifndef GUSEMU_H 26*47b43a1fSPaolo Bonzini #define GUSEMU_H 27*47b43a1fSPaolo Bonzini 28*47b43a1fSPaolo Bonzini /* data types (need to be adjusted if neither a VC6 nor a C99 compatible compiler is used) */ 29*47b43a1fSPaolo Bonzini 30*47b43a1fSPaolo Bonzini #if defined _WIN32 && defined _MSC_VER /* doesn't support other win32 compilers yet, do it yourself... */ 31*47b43a1fSPaolo Bonzini typedef unsigned char GUSbyte; 32*47b43a1fSPaolo Bonzini typedef unsigned short GUSword; 33*47b43a1fSPaolo Bonzini typedef unsigned int GUSdword; 34*47b43a1fSPaolo Bonzini typedef signed char GUSchar; 35*47b43a1fSPaolo Bonzini typedef signed short GUSsample; 36*47b43a1fSPaolo Bonzini #else 37*47b43a1fSPaolo Bonzini #include <stdint.h> 38*47b43a1fSPaolo Bonzini typedef int8_t GUSchar; 39*47b43a1fSPaolo Bonzini typedef uint8_t GUSbyte; 40*47b43a1fSPaolo Bonzini typedef uint16_t GUSword; 41*47b43a1fSPaolo Bonzini typedef uint32_t GUSdword; 42*47b43a1fSPaolo Bonzini typedef int16_t GUSsample; 43*47b43a1fSPaolo Bonzini #endif 44*47b43a1fSPaolo Bonzini 45*47b43a1fSPaolo Bonzini typedef struct _GUSEmuState 46*47b43a1fSPaolo Bonzini { 47*47b43a1fSPaolo Bonzini GUSbyte *himemaddr; /* 1024*1024 bytes used for storing uploaded samples (+32 additional bytes for read padding) */ 48*47b43a1fSPaolo Bonzini GUSbyte *gusdatapos; /* (gusdataend-gusdata) bytes used for storing emulated GF1/mixer register states (32*32+4 bytes in initial GUSemu32 version) */ 49*47b43a1fSPaolo Bonzini uint32_t gusirq; 50*47b43a1fSPaolo Bonzini uint32_t gusdma; 51*47b43a1fSPaolo Bonzini unsigned int timer1fraction; 52*47b43a1fSPaolo Bonzini unsigned int timer2fraction; 53*47b43a1fSPaolo Bonzini void *opaque; 54*47b43a1fSPaolo Bonzini } GUSEmuState; 55*47b43a1fSPaolo Bonzini 56*47b43a1fSPaolo Bonzini /* ** Callback functions needed: */ 57*47b43a1fSPaolo Bonzini /* NMI is defined as hwirq=-1 (not supported (yet?)) */ 58*47b43a1fSPaolo Bonzini /* GUS_irqrequest returns the number of IRQs actually scheduled into the virtual machine */ 59*47b43a1fSPaolo Bonzini /* Level triggered IRQ simulations normally return 1 */ 60*47b43a1fSPaolo Bonzini /* Event triggered IRQ simulation can safely ignore GUS_irqclear calls */ 61*47b43a1fSPaolo Bonzini int GUS_irqrequest(GUSEmuState *state, int hwirq, int num);/* needed in both mixer and bus emulation functions. */ 62*47b43a1fSPaolo Bonzini void GUS_irqclear( GUSEmuState *state, int hwirq); /* used by gus_write() only - can be left empty for mixer functions */ 63*47b43a1fSPaolo Bonzini void GUS_dmarequest(GUSEmuState *state); /* used by gus_write() only - can be left empty for mixer functions */ 64*47b43a1fSPaolo Bonzini 65*47b43a1fSPaolo Bonzini /* ** ISA bus interface functions: */ 66*47b43a1fSPaolo Bonzini 67*47b43a1fSPaolo Bonzini /* Port I/O handlers */ 68*47b43a1fSPaolo Bonzini /* support the following ports: */ 69*47b43a1fSPaolo Bonzini /* 2x0,2x6,2x8...2xF,3x0...3x7; */ 70*47b43a1fSPaolo Bonzini /* optional: 388,389 (at least writes should be forwarded or some GUS detection algorithms will fail) */ 71*47b43a1fSPaolo Bonzini /* data is passed in host byte order */ 72*47b43a1fSPaolo Bonzini unsigned int gus_read( GUSEmuState *state, int port, int size); 73*47b43a1fSPaolo Bonzini void gus_write(GUSEmuState *state, int port, int size, unsigned int data); 74*47b43a1fSPaolo Bonzini /* size is given in bytes (1 for byte, 2 for word) */ 75*47b43a1fSPaolo Bonzini 76*47b43a1fSPaolo Bonzini /* DMA data transfer function */ 77*47b43a1fSPaolo Bonzini /* data pointed to is passed in native x86 order */ 78*47b43a1fSPaolo Bonzini void gus_dma_transferdata(GUSEmuState *state, char *dma_addr, unsigned int count, int TC); 79*47b43a1fSPaolo Bonzini /* Called back by GUS_start_DMA as soon as the emulated DMA controller is ready for a transfer to or from GUS */ 80*47b43a1fSPaolo Bonzini /* (might be immediately if the DMA controller was programmed first) */ 81*47b43a1fSPaolo Bonzini /* dma_addr is an already translated address directly pointing to the beginning of the memory block */ 82*47b43a1fSPaolo Bonzini /* do not forget to update DMA states after the call, including the DREQ and TC flags */ 83*47b43a1fSPaolo Bonzini /* it is possible to break down a single transfer into multiple ones, but take care that: */ 84*47b43a1fSPaolo Bonzini /* -dma_count is actually count-1 */ 85*47b43a1fSPaolo Bonzini /* -before and during a transfer, DREQ is set and TC cleared */ 86*47b43a1fSPaolo Bonzini /* -when calling gus_dma_transferdata(), TC is only set true for call transferring the last byte */ 87*47b43a1fSPaolo Bonzini /* -after the last transfer, DREQ is cleared and TC is set */ 88*47b43a1fSPaolo Bonzini 89*47b43a1fSPaolo Bonzini /* ** GF1 mixer emulation functions: */ 90*47b43a1fSPaolo Bonzini /* Usually, gus_irqgen should be called directly after gus_mixvoices if you can meet the recommended ranges. */ 91*47b43a1fSPaolo Bonzini /* If the interrupts are executed immediately (i.e., are synchronous), it may be useful to break this */ 92*47b43a1fSPaolo Bonzini /* down into a sequence of gus_mixvoice();gus_irqgen(); calls while mixing an audio block. */ 93*47b43a1fSPaolo Bonzini /* If the interrupts are asynchronous, it may be needed to use a separate thread mixing into a temporary */ 94*47b43a1fSPaolo Bonzini /* audio buffer in order to avoid quality loss caused by large numsamples and elapsed_time values. */ 95*47b43a1fSPaolo Bonzini 96*47b43a1fSPaolo Bonzini void gus_mixvoices(GUSEmuState *state, unsigned int playback_freq, unsigned int numsamples, GUSsample *bufferpos); 97*47b43a1fSPaolo Bonzini /* recommended range: 10 < numsamples < 100 */ 98*47b43a1fSPaolo Bonzini /* lower values may result in increased rounding error, higher values often cause audible timing delays */ 99*47b43a1fSPaolo Bonzini 100*47b43a1fSPaolo Bonzini void gus_irqgen(GUSEmuState *state, unsigned int elapsed_time); 101*47b43a1fSPaolo Bonzini /* recommended range: 80us < elapsed_time < max(1000us, numsamples/playback_freq) */ 102*47b43a1fSPaolo Bonzini /* lower values won´t provide any benefit at all, higher values can cause audible timing delays */ 103*47b43a1fSPaolo Bonzini /* note: masked timers are also calculated by this function, thus it might be needed even without any IRQs in use! */ 104*47b43a1fSPaolo Bonzini 105*47b43a1fSPaolo Bonzini #endif /* gusemu.h */ 106