diff --git a/Changelog b/Changelog index 5117b243e3bcb16605b3e25949075dded798259a..73b85e5dd678b394625560393c9db458c65aedbc 100644 --- a/Changelog +++ b/Changelog @@ -26,6 +26,7 @@ version 0.6.1: - BIOS floppy fix for NT4 (Mike Nordell, Derek Fawcus, Volker Ruppert) - Floppy fixes for NT4 and NT5 (Mike Nordell) - NT4 IDE fixes (Ben Pfaf, Mike Nordell) + - SDL Audio support and SB16 fixes (malc) version 0.6.0: diff --git a/hw/sb16.c b/hw/sb16.c index 260718cdc00b130ffb9aa5ade943c35d2a234543..f84cf8d4a2cec38301d3b2b53615e1bcddbc27c7 100644 --- a/hw/sb16.c +++ b/hw/sb16.c @@ -26,12 +26,10 @@ #define MIN(a, b) ((a)>(b)?(b):(a)) #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0]))) -#define log(...) do { \ - fprintf (stderr, "sb16: " __VA_ARGS__); \ - fputc ('\n', stderr); \ -} while (0) +#define dolog(...) fprintf (stderr, "sb16: " __VA_ARGS__); /* #define DEBUG_SB16 */ + #ifdef DEBUG_SB16 #define lwarn(...) fprintf (stderr, "sb16: " __VA_ARGS__) #define linfo(...) fprintf (stderr, "sb16: " __VA_ARGS__) @@ -47,7 +45,10 @@ #define IO_WRITE_PROTO(name) \ void name (void *opaque, uint32_t nport, uint32_t val) -static const char e3[] = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992."; +static const char e3[] = + "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992\0" + "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1994-1997"; + /* "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1994."; */ static struct { int ver_lo; @@ -80,10 +81,13 @@ typedef struct SB16State { int v2x6; - uint8_t in_data[10]; - uint8_t out_data[50]; + uint8_t in2_data[10]; + uint8_t out_data[1024]; int left_till_irq; + uint64_t nzero; + uint8_t last_read_byte; + uint8_t test_reg; /* mixer state */ int mixer_nreg; @@ -95,7 +99,7 @@ static struct SB16State dsp; static void log_dsp (SB16State *dsp) { - linfo ("%c:%c:%d:%c:dmabuf=%d:pos=%d:freq=%d:timeconst=%d:speaker=%d\n", + ldebug ("%c:%c:%d:%c:dmabuf=%d:pos=%d:freq=%d:timeconst=%d:speaker=%d\n", dsp->fmt_stereo ? 'S' : 'M', dsp->fmt_signed ? 'S' : 'U', dsp->fmt_bits, @@ -191,6 +195,7 @@ static void dma_cmd (uint8_t cmd, uint8_t d0, int dma_len) mix_block = ((dsp.freq * align) / 100) & ~(align - 1); } + if (dsp.freq) AUD_reset (dsp.freq, 1 << dsp.fmt_stereo, fmt); control (1); dsp.speaker = 1; @@ -202,9 +207,17 @@ static inline void dsp_out_data(SB16State *dsp, int val) dsp->out_data[dsp->out_data_len++] = val; } +static inline uint8_t dsp_get_data(SB16State *dsp) +{ + if (dsp->in_index) + return dsp->in2_data[--dsp->in_index]; + else + return 0; +} + static void command (SB16State *dsp, uint8_t cmd) { - linfo ("%#x\n", cmd); + linfo ("command: %#x\n", cmd); if (cmd > 0xaf && cmd < 0xd0) { if (cmd & 8) @@ -215,7 +228,7 @@ static void command (SB16State *dsp, uint8_t cmd) case 12: break; default: - log("%#x wrong bits", cmd); + dolog ("command: %#x wrong bits specification\n", cmd); goto error; } dsp->needed_bytes = 3; @@ -223,23 +236,25 @@ static void command (SB16State *dsp, uint8_t cmd) else { switch (cmd) { case 0x00: - case 0x03: case 0xe7: /* IMS uses those when probing for sound devices */ return; + case 0x03: case 0x04: - dsp->needed_bytes = 1; - break; + dsp_out_data (dsp, 0); + return; case 0x05: + dsp->needed_bytes = 2; + break; + case 0x0e: dsp->needed_bytes = 2; break; case 0x0f: dsp->needed_bytes = 1; - dsp_out_data (dsp, 0); break; case 0x10: @@ -272,6 +287,8 @@ static void command (SB16State *dsp, uint8_t cmd) dsp->needed_bytes = 2; break; + case 0x45: + dsp_out_data (dsp, 0xaa); case 0x47: /* Continue Auto-Initialize DMA 16bit */ break; @@ -346,19 +363,39 @@ static void command (SB16State *dsp, uint8_t cmd) case 0xe3: { int i; - for (i = sizeof (e3) - 1; i >= 0; --i) + for (i = sizeof (e3) - 1; i >= 0; i--) dsp_out_data (dsp, e3[i]); return; } + case 0xe4: /* write test reg */ + dsp->needed_bytes = 1; + break; + + case 0xe8: /* read test reg */ + dsp_out_data (dsp, dsp->test_reg); + break; + case 0xf2: - dsp_out_data(dsp, 0xaa); + dsp_out_data (dsp, 0xaa); dsp->mixer_regs[0x82] |= dsp->mixer_regs[0x80]; pic_set_irq (sb.irq, 1); return; + case 0xf9: + dsp->needed_bytes = 1; + break; + + case 0xfa: + dsp_out_data (dsp, 0); + break; + + case 0xfc: /* FIXME */ + dsp_out_data (dsp, 0); + break; + default: - log("%#x is unknown", cmd); + dolog ("unrecognized command %#x\n", cmd); goto error; } } @@ -371,15 +408,14 @@ static void command (SB16State *dsp, uint8_t cmd) static void complete (SB16State *dsp) { + int d0, d1, d2; linfo ("complete command %#x, in_index %d, needed_bytes %d\n", dsp->cmd, dsp->in_index, dsp->needed_bytes); if (dsp->cmd > 0xaf && dsp->cmd < 0xd0) { - int d0, d1, d2; - - d0 = dsp->in_data[0]; - d1 = dsp->in_data[1]; - d2 = dsp->in_data[2]; + d2 = dsp_get_data (dsp); + d1 = dsp_get_data (dsp); + d0 = dsp_get_data (dsp); ldebug ("d0 = %d, d1 = %d, d2 = %d\n", d0, d1, d2); @@ -387,23 +423,29 @@ static void complete (SB16State *dsp) } else { switch (dsp->cmd) { - case 0x05: case 0x04: - case 0x0e: + case 0x10: + dsp_get_data (dsp); + break; + case 0x0f: + d0 = dsp_get_data (dsp); + dsp_out_data (dsp, 0xf8); break; - case 0x10: + case 0x05: + case 0x0e: + dsp_get_data (dsp); + dsp_get_data (dsp); break; case 0x14: { - int d0, d1; int save_left; int save_pos; - d0 = dsp->in_data[0]; - d1 = dsp->in_data[1]; + d1 = dsp_get_data (dsp); + d0 = dsp_get_data (dsp); save_left = dsp->left_till_irq; save_pos = dsp->dma_pos; @@ -417,35 +459,60 @@ static void complete (SB16State *dsp) } case 0x40: - dsp->time_const = dsp->in_data[0]; + dsp->time_const = dsp_get_data (dsp); linfo ("set time const %d\n", dsp->time_const); break; case 0x41: case 0x42: - dsp->freq = dsp->in_data[1] + (dsp->in_data[0] << 8); - linfo ("set freq %#x, %#x = %d\n", - dsp->in_data[1], dsp->in_data[0], dsp->freq); + d1 = dsp_get_data (dsp); + d0 = dsp_get_data (dsp); + + dsp->freq = d1 + (d0 << 8); + linfo ("set freq %#x, %#x = %d\n", d1, d0, dsp->freq); break; case 0x48: - dsp->dma_buffer_size = dsp->in_data[1] + (dsp->in_data[0] << 8); + d1 = dsp_get_data (dsp); + d0 = dsp_get_data (dsp); + dsp->dma_buffer_size = d1 + (d0 << 8); linfo ("set dma len %#x, %#x = %d\n", - dsp->in_data[1], dsp->in_data[0], dsp->dma_buffer_size); + d1, d0, dsp->dma_buffer_size); break; case 0xe0: + d0 = dsp_get_data (dsp); dsp->out_data_len = 0; - linfo ("data = %#x\n", dsp->in_data[0]); - dsp_out_data(dsp, dsp->in_data[0] ^ 0xff); + linfo ("data = %#x\n", d0); + dsp_out_data (dsp, d0 ^ 0xff); + break; + + case 0xe4: + dsp->test_reg = dsp_get_data (dsp); + break; + + + case 0xf9: + d0 = dsp_get_data (dsp); + ldebug ("f9 <- %#x\n", d0); + switch (d0) { + case 0x0e: dsp_out_data (dsp, 0xff); break; + case 0x0f: dsp_out_data (dsp, 0x07); break; + case 0xf9: dsp_out_data (dsp, 0x00); break; + case 0x37: + dsp_out_data (dsp, 0x38); break; + default: + dsp_out_data (dsp, 0); + } break; default: - log ("unrecognized command %#x", dsp->cmd); + dolog ("complete: unrecognized command %#x\n", dsp->cmd); return; } } + dsp->needed_bytes = 0; dsp->cmd = -1; return; } @@ -457,7 +524,7 @@ static IO_WRITE_PROTO (dsp_write) iport = nport - sb.port; - ldebug ("write %#x %#x\n", nport, iport); + ldebug ("dsp_write %#x <- %#x\n", nport, val); switch (iport) { case 0x6: control (0); @@ -465,6 +532,14 @@ static IO_WRITE_PROTO (dsp_write) dsp->v2x6 = 0; else if ((1 == val) && (0 == dsp->v2x6)) { dsp->v2x6 = 1; + dsp->dma_pos = 0; + dsp->dma_auto = 0; + dsp->in_index = 0; + dsp->out_data_len = 0; + dsp->left_till_irq = 0; + dsp->speaker = 0; + dsp->needed_bytes = 0; + pic_set_irq (sb.irq, 0); dsp_out_data(dsp, 0xaa); } else @@ -479,18 +554,22 @@ static IO_WRITE_PROTO (dsp_write) } } else { - dsp->in_data[dsp->in_index++] = val; + if (dsp->in_index == sizeof (dsp->in2_data)) { + dolog ("in data overrun\n"); + } + else { + dsp->in2_data[dsp->in_index++] = val; if (dsp->in_index == dsp->needed_bytes) { dsp->needed_bytes = 0; - dsp->in_index = 0; complete (dsp); log_dsp (dsp); } } + } break; default: - log ("(nport=%#x, val=%#x)", nport, val); + dolog ("dsp_write (nport=%#x, val=%#x)\n", nport, val); break; } } @@ -503,31 +582,36 @@ static IO_READ_PROTO (dsp_read) iport = nport - sb.port; switch (iport) { - case 0x6: /* reset */ - return 0; + control (0); + retval = 0; + dsp->speaker = 0; + break; case 0xa: /* read data */ if (dsp->out_data_len) { retval = dsp->out_data[--dsp->out_data_len]; + dsp->last_read_byte = retval; } else { - log("empty output buffer"); - goto error; + retval = dsp->last_read_byte; + dolog ("empty output buffer\n"); + /* goto error; */ } break; - case 0xc: /* 0 can write */ + case 0xc: /* 0xxxxxxx can write */ retval = 0; + if (dsp->out_data_len == sizeof (dsp->out_data)) retval |= 0x80; break; case 0xd: /* timer interrupt clear */ - log("timer interrupt clear"); + dolog ("timer interrupt clear\n"); goto error; case 0xe: /* data available status | irq 8 ack */ /* XXX drop pic irq line here? */ - ldebug ("8 ack\n"); - retval = (0 == dsp->out_data_len) ? 0 : 0x80; + /* ldebug ("8 ack\n"); */ + retval = dsp->out_data_len ? 0x80 : 0; dsp->mixer_regs[0x82] &= ~dsp->mixer_regs[0x80]; pic_set_irq (sb.irq, 0); break; @@ -544,15 +628,30 @@ static IO_READ_PROTO (dsp_read) goto error; } - if ((0xc != iport) && (0xe != iport)) { - ldebug ("nport=%#x iport %#x = %#x\n", + if (0xe == iport) { + if (0 == retval) { + if (!dsp->nzero) { + ldebug ("dsp_read (nport=%#x iport %#x) = %#x, %lld\n", + nport, iport, retval, dsp->nzero); + } + dsp->nzero += 1; + } + else { + ldebug ("dsp_read (nport=%#x iport %#x) = %#x, %lld\n", + nport, iport, retval, dsp->nzero); + dsp->nzero = 0; + } + } + else { + ldebug ("dsp_read (nport=%#x iport %#x) = %#x\n", nport, iport, retval); } return retval; error: - return 0; + printf ("dsp_read error %#x\n", nport); + return 0xff; } static IO_WRITE_PROTO(mixer_write_indexb) @@ -564,12 +663,100 @@ static IO_WRITE_PROTO(mixer_write_indexb) static IO_WRITE_PROTO(mixer_write_datab) { SB16State *dsp = opaque; + int i; - if (dsp->mixer_nreg > 0x83) + linfo ("mixer [%#x] <- %#x\n", dsp->mixer_nreg, val); + switch (dsp->mixer_nreg) { + case 0x00: + /* Bochs */ + dsp->mixer_regs[0x04] = 0xcc; + dsp->mixer_regs[0x0a] = 0x00; + dsp->mixer_regs[0x22] = 0xcc; + dsp->mixer_regs[0x26] = 0xcc; + dsp->mixer_regs[0x28] = 0x00; + dsp->mixer_regs[0x2e] = 0x00; + dsp->mixer_regs[0x3c] = 0x1f; + dsp->mixer_regs[0x3d] = 0x15; + dsp->mixer_regs[0x3e] = 0x0b; + + for (i = 0x30; i <= 0x35; i++) + dsp->mixer_regs[i] = 0xc0; + + for (i = 0x36; i <= 0x3b; i++) + dsp->mixer_regs[i] = 0x00; + + for (i = 0x3f; i <= 0x43; i++) + dsp->mixer_regs[i] = 0x00; + + for (i = 0x44; i <= 0x47; i++) + dsp->mixer_regs[i] = 0x80; + + for (i = 0x30; i < 0x48; i++) { + dsp->mixer_regs[i] = 0x20; + } + break; + + case 0x04: + case 0x0a: + case 0x22: + case 0x26: + case 0x28: + case 0x2e: + case 0x30: + case 0x31: + case 0x32: + case 0x33: + case 0x34: + case 0x35: + case 0x36: + case 0x37: + case 0x38: + case 0x39: + case 0x3a: + case 0x3b: + case 0x3c: + case 0x3d: + case 0x3e: + case 0x3f: + case 0x40: + case 0x41: + case 0x42: + case 0x43: + case 0x44: + case 0x45: + case 0x46: + case 0x47: + case 0x80: + case 0x81: + break; + default: return; + } dsp->mixer_regs[dsp->mixer_nreg] = val; } +static IO_WRITE_PROTO(mpu_write) +{ + linfo ("mpu: %#x\n", val); +} + +static IO_WRITE_PROTO(adlib_write) +{ + linfo ("adlib: %#x\n", val); +} + +static IO_READ_PROTO(mpu_read) +{ + linfo ("mpu read: %#x\n", nport); + return 0x80; +} + +static IO_READ_PROTO(adlib_read) +{ + linfo ("adlib read: %#x\n", nport); + return 0; +} + static IO_WRITE_PROTO(mixer_write_indexw) { mixer_write_indexb (opaque, nport, val & 0xff); @@ -579,6 +766,7 @@ static IO_WRITE_PROTO(mixer_write_indexw) static IO_READ_PROTO(mixer_read) { SB16State *dsp = opaque; + linfo ("mixer [%#x] -> %#x\n", dsp->mixer_nreg, dsp->mixer_regs[dsp->mixer_nreg]); return dsp->mixer_regs[dsp->mixer_nreg]; } @@ -637,6 +825,8 @@ static int SB_read_DMA (void *opaque, target_ulong addr, int size) return 0; if (dsp->left_till_irq < 0) { + ldebug ("left_till_irq < 0, %d, pos %d \n", + dsp->left_till_irq, dsp->dma_buffer_size); dsp->left_till_irq += dsp->dma_buffer_size; return dsp->dma_pos; } @@ -644,6 +834,7 @@ static int SB_read_DMA (void *opaque, target_ulong addr, int size) free = AUD_get_free (); if ((free <= 0) || (0 == size)) { + ldebug ("returning, since free = %d and size = %d\n", free, size); return dsp->dma_pos; } @@ -656,8 +847,11 @@ static int SB_read_DMA (void *opaque, target_ulong addr, int size) till = dsp->left_till_irq; +#ifdef DEBUG_SB16_MOST ldebug ("addr:%#010x free:%d till:%d size:%d\n", addr, free, till, size); +#endif + if (till <= copy) { if (0 == dsp->dma_auto) { copy = till; @@ -671,7 +865,8 @@ static int SB_read_DMA (void *opaque, target_ulong addr, int size) if (dsp->left_till_irq <= 0) { dsp->mixer_regs[0x82] |= dsp->mixer_regs[0x80]; if (0 == noirq) { - ldebug ("request irq\n"); + ldebug ("request irq pos %d, left %d\n", + dsp->dma_pos, dsp->left_till_irq); pic_set_irq(sb.irq, 1); } @@ -680,9 +875,11 @@ static int SB_read_DMA (void *opaque, target_ulong addr, int size) } } +#ifdef DEBUG_SB16_MOST ldebug ("pos %5d free %5d size %5d till % 5d copy %5d dma size %5d\n", dsp->dma_pos, free, size, dsp->left_till_irq, copy, dsp->dma_buffer_size); +#endif if (dsp->left_till_irq <= 0) { dsp->left_till_irq += dsp->dma_buffer_size; @@ -703,7 +900,7 @@ static int magic_of_irq (int irq) case 10: return 8; default: - log ("bad irq %d", irq); + dolog ("bad irq %d\n", irq); return 2; } } @@ -721,12 +918,42 @@ static int irq_of_magic (int magic) case 8: return 10; default: - log ("bad irq magic %d", magic); + dolog ("bad irq magic %d\n", magic); return 2; } } #endif +#ifdef SB16_TRAP_ALL +static IO_READ_PROTO (trap_read) +{ + switch (nport) { + case 0x220: + return 0; + case 0x226: + case 0x22a: + case 0x22c: + case 0x22d: + case 0x22e: + case 0x22f: + return dsp_read (opaque, nport); + } + linfo ("trap_read: %#x\n", nport); + return 0xff; +} + +static IO_WRITE_PROTO (trap_write) +{ + switch (nport) { + case 0x226: + case 0x22c: + dsp_write (opaque, nport, val); + return; + } + linfo ("trap_write: %#x = %#x\n", nport, val); +} +#endif + void SB16_init (void) { SB16State *s = &dsp; @@ -736,13 +963,23 @@ void SB16_init (void) memset(s->mixer_regs, 0xff, sizeof(s->mixer_regs)); + s->mixer_regs[0x00] = 0; s->mixer_regs[0x0e] = ~0; s->mixer_regs[0x80] = magic_of_irq (sb.irq); - s->mixer_regs[0x81] = 0x20 | (sb.dma << 1); - - for (i = 0x30; i < 0x48; i++) { - s->mixer_regs[i] = 0x20; + s->mixer_regs[0x81] = 0x80 | 0x10 | (sb.dma << 1); + s->mixer_regs[0x82] = 0; + s->mixer_regs[0xfd] = 16; /* bochs */ + s->mixer_regs[0xfe] = 6; /* bochs */ + mixer_write_indexw (s, 0x224, 0); + +#ifdef SB16_TRAP_ALL + for (i = 0; i < 0x100; i++) { + if (i != 4 && i != 5) { + register_ioport_write (sb.port + i, 1, 1, trap_write, s); + register_ioport_read (sb.port + i, 1, 1, trap_read, s); + } } +#else for (i = 0; i < LENOFA (dsp_write_ports); i++) { register_ioport_write (sb.port + dsp_write_ports[i], 1, 1, dsp_write, s); @@ -751,12 +988,20 @@ void SB16_init (void) for (i = 0; i < LENOFA (dsp_read_ports); i++) { register_ioport_read (sb.port + dsp_read_ports[i], 1, 1, dsp_read, s); } +#endif register_ioport_write (sb.port + 0x4, 1, 1, mixer_write_indexb, s); register_ioport_write (sb.port + 0x4, 1, 2, mixer_write_indexw, s); register_ioport_read (sb.port + 0x5, 1, 1, mixer_read, s); register_ioport_write (sb.port + 0x5, 1, 1, mixer_write_datab, s); + for (i = 0; 4 < 4; i++) { + register_ioport_read (0x330 + i, 1, 1, mpu_read, s); + register_ioport_write (0x330 + i, 1, 1, mpu_write, s); + register_ioport_read (0x388 + i, 1, 1, adlib_read, s); + register_ioport_write (0x388 + i, 1, 1, adlib_write, s); + } + DMA_register_channel (sb.hdma, SB_read_DMA, s); DMA_register_channel (sb.dma, SB_read_DMA, s); } diff --git a/oss.c b/oss.c index 40c9462cf39bce520102e11579549c8b418f35f2..91eb47e49105419d095fd1b6f6a97da8ef231613 100644 --- a/oss.c +++ b/oss.c @@ -23,22 +23,446 @@ */ #include "vl.h" -#if !defined(_WIN32) && !defined(__APPLE__) -#include +#include +#include +#include +#include + +/* TODO: Graceful error handling */ + +#if defined(_WIN32) +#define USE_SDL_AUDIO +#endif + +#define MIN(a, b) ((a)>(b)?(b):(a)) +#define MAX(a, b) ((a)<(b)?(b):(a)) + +#define DEREF(x) (void)x +#define dolog(...) fprintf (stderr, "audio: " __VA_ARGS__) +#define ERRFail(...) do { \ + int _errno = errno; \ + fprintf (stderr, "audio: " __VA_ARGS__); \ + fprintf (stderr, "\nsystem error: %s\n", strerror (_errno)); \ + abort (); \ +} while (0) +#define Fail(...) do { \ + fprintf (stderr, "audio: " __VA_ARGS__); \ + fprintf (stderr, "\n"); \ + abort (); \ +} while (0) + +#ifdef DEBUG_AUDIO +#define lwarn(...) fprintf (stderr, "audio: " __VA_ARGS__) +#define linfo(...) fprintf (stderr, "audio: " __VA_ARGS__) +#define ldebug(...) fprintf (stderr, "audio: " __VA_ARGS__) +#else +#define lwarn(...) +#define linfo(...) +#define ldebug(...) +#endif + +static int get_conf_val (const char *key, int defval) +{ + int val = defval; + char *strval; + + strval = getenv (key); + if (strval) { + val = atoi (strval); + } + + return val; +} + +static void copy_no_conversion (void *dst, void *src, int size) +{ + memcpy (dst, src, size); +} + +static void copy_u16_to_s16 (void *dst, void *src, int size) +{ + int i; + uint16_t *out, *in; + + out = dst; + in = src; + + for (i = 0; i < size / 2; i++) { + out[i] = in[i] + 0x8000; + } +} + +#ifdef USE_SDL_AUDIO +#include +#include + +static struct { + int samples; +} conf = { + .samples = 4096 +}; + +typedef struct AudioState { + int freq; + int bits16; + int nchannels; + int rpos; + int wpos; + volatile int live; + volatile int exit; + int bytes_per_second; + Uint8 *buf; + int bufsize; + int leftover; + uint64_t old_ticks; + SDL_AudioSpec spec; + SDL_mutex *mutex; + SDL_sem *sem; + void (*copy_fn)(void *, void *, int); +} AudioState; + +static AudioState sdl_audio; + +void AUD_run (void) +{ +} + +static void own (AudioState *s) +{ + /* SDL_LockAudio (); */ + if (SDL_mutexP (s->mutex)) + dolog ("SDL_mutexP: %s\n", SDL_GetError ()); +} + +static void disown (AudioState *s) +{ + /* SDL_UnlockAudio (); */ + if (SDL_mutexV (s->mutex)) + dolog ("SDL_mutexV: %s\n", SDL_GetError ()); +} + +static void sem_wait (AudioState *s) +{ + if (SDL_SemWait (s->sem)) + dolog ("SDL_SemWait: %s\n", SDL_GetError ()); +} + +static void sem_post (AudioState *s) +{ + if (SDL_SemPost (s->sem)) + dolog ("SDL_SemPost: %s\n", SDL_GetError ()); +} + +static void audio_callback (void *data, Uint8 *stream, int len) +{ + int to_mix; + AudioState *s = data; + + if (s->exit) return; + while (len) { + sem_wait (s); + if (s->exit) return; + own (s); + to_mix = MIN (len, s->live); + len -= to_mix; + /* printf ("to_mix=%d len=%d live=%d\n", to_mix, len, s->live); */ + while (to_mix) { + int chunk = MIN (to_mix, s->bufsize - s->rpos); + /* SDL_MixAudio (stream, buf, chunk, SDL_MIX_MAXVOLUME); */ + memcpy (stream, s->buf + s->rpos, chunk); + + s->rpos += chunk; + s->live -= chunk; + + stream += chunk; + to_mix -= chunk; + + if (s->rpos == s->bufsize) s->rpos = 0; + } + disown (s); + } +} + +static void sem_zero (AudioState *s) +{ + int res; + + do { + res = SDL_SemTryWait (s->sem); + if (res < 0) { + dolog ("SDL_SemTryWait: %s\n", SDL_GetError ()); + return; + } + } while (res != SDL_MUTEX_TIMEDOUT); +} + +static void do_open (AudioState *s) +{ + int status; + SDL_AudioSpec obtained; + + SDL_PauseAudio (1); + if (s->buf) { + s->exit = 1; + sem_post (s); + SDL_CloseAudio (); + s->exit = 0; + qemu_free (s->buf); + s->buf = NULL; + sem_zero (s); + } + + s->bytes_per_second = (s->spec.freq << (s->spec.channels >> 1)) << s->bits16; + s->spec.samples = conf.samples; + s->spec.userdata = s; + s->spec.callback = audio_callback; + + status = SDL_OpenAudio (&s->spec, &obtained); + if (status < 0) { + dolog ("SDL_OpenAudio: %s\n", SDL_GetError ()); + goto exit; + } + + if (obtained.freq != s->spec.freq || + obtained.channels != s->spec.channels || + obtained.format != s->spec.format) { + dolog ("Audio spec mismatch requested obtained\n" + "freq %5d %5d\n" + "channels %5d %5d\n" + "fmt %5d %5d\n", + s->spec.freq, obtained.freq, + s->spec.channels, obtained.channels, + s->spec.format, obtained.format + ); + } + + s->bufsize = obtained.size; + s->buf = qemu_mallocz (s->bufsize); + if (!s->buf) { + dolog ("qemu_mallocz(%d)\n", s->bufsize); + goto exit; + } + SDL_PauseAudio (0); + +exit: + s->rpos = 0; + s->wpos = 0; + s->live = 0; +} + +int AUD_write (void *in_buf, int size) +{ + AudioState *s = &sdl_audio; + int to_copy, temp; + uint8_t *in, *out; + + own (s); + to_copy = MIN (s->bufsize - s->live, size); + + temp = to_copy; + + in = in_buf; + out = s->buf; + + while (temp) { + int copy; + + copy = MIN (temp, s->bufsize - s->wpos); + s->copy_fn (out + s->wpos, in, copy); + + s->wpos += copy; + if (s->wpos == s->bufsize) { + s->wpos = 0; + } + + temp -= copy; + in += copy; + s->live += copy; + } + + disown (s); + sem_post (s); + return to_copy; +} + +static void maybe_open (AudioState *s, int req_freq, int req_nchannels, + audfmt_e req_fmt, int force_open) +{ + int sdl_fmt, bits16; + + switch (req_fmt) { + case AUD_FMT_U8: + bits16 = 0; + sdl_fmt = AUDIO_U8; + s->copy_fn = copy_no_conversion; + break; + + case AUD_FMT_S8: + fprintf (stderr, "audio: can not play 8bit signed\n"); + return; + + case AUD_FMT_S16: + bits16 = 1; + sdl_fmt = AUDIO_S16; + s->copy_fn = copy_no_conversion; + break; + + case AUD_FMT_U16: + bits16 = 1; + sdl_fmt = AUDIO_S16; + s->copy_fn = copy_u16_to_s16; + break; + + default: + abort (); + } + + if (force_open + || (NULL == s->buf) + || (sdl_fmt != s->spec.format) + || (req_nchannels != s->spec.channels) + || (req_freq != s->spec.freq) + || (bits16 != s->bits16)) { + + s->spec.format = sdl_fmt; + s->spec.channels = req_nchannels; + s->spec.freq = req_freq; + s->bits16 = bits16; + do_open (s); + } +} + +void AUD_reset (int req_freq, int req_nchannels, audfmt_e req_fmt) +{ + AudioState *s = &sdl_audio; + own (s); + maybe_open (s, req_freq, req_nchannels, req_fmt, 0); + disown (s); +} + +void AUD_open (int req_freq, int req_nchannels, audfmt_e req_fmt) +{ + AudioState *s = &sdl_audio; + own (s); + maybe_open (s, req_freq, req_nchannels, req_fmt, 1); + disown (s); +} + +void AUD_adjust_estimate (int leftover) +{ + AudioState *s = &sdl_audio; + own (s); + s->leftover = leftover; + disown (s); +} + +int AUD_get_free (void) +{ + int free, elapsed; + uint64_t ticks, delta; + uint64_t ua_elapsed; + uint64_t al_elapsed; + AudioState *s = &sdl_audio; + + own (s); + free = s->bufsize - s->live; + + if (0 == free) { + disown (s); + return 0; + } + + elapsed = free; + ticks = qemu_get_clock(rt_clock); + delta = ticks - s->old_ticks; + s->old_ticks = ticks; + + ua_elapsed = (delta * s->bytes_per_second) / 1000; + al_elapsed = ua_elapsed & ~3ULL; + + ldebug ("tid elapsed %llu bytes\n", ua_elapsed); + + if (al_elapsed > (uint64_t) INT_MAX) + elapsed = INT_MAX; + else + elapsed = al_elapsed; + + elapsed += s->leftover; + disown (s); + + if (elapsed > free) { + lwarn ("audio can not keep up elapsed %d free %d\n", elapsed, free); + return free; + } + else { + return elapsed; + } +} + +int AUD_get_live (void) +{ + int live; + AudioState *s = &sdl_audio; + + own (s); + live = s->live; + disown (s); + return live; +} + +int AUD_get_buffer_size (void) +{ + int bufsize; + AudioState *s = &sdl_audio; + + own (s); + bufsize = s->bufsize; + disown (s); + return bufsize; +} + +#define QC_SDL_NSAMPLES "QEMU_SDL_NSAMPLES" + +static void cleanup (void) +{ + AudioState *s = &sdl_audio; + own (s); + s->exit = 1; + sem_post (s); + disown (s); +} + +void AUD_init (void) +{ + AudioState *s = &sdl_audio; + + atexit (cleanup); + SDL_InitSubSystem (SDL_INIT_AUDIO); + s->mutex = SDL_CreateMutex (); + if (!s->mutex) { + dolog ("SDL_CreateMutex: %s\n", SDL_GetError ()); + return; + } + + s->sem = SDL_CreateSemaphore (0); + if (!s->sem) { + dolog ("SDL_CreateSemaphore: %s\n", SDL_GetError ()); + return; + } + + conf.samples = get_conf_val (QC_SDL_NSAMPLES, conf.samples); +} + +#elif !defined(_WIN32) && !defined(__APPLE__) + #include #include -#include #include -#include -#include -#include -#include #include #include #include #include - /* http://www.df.lth.se/~john_e/gems/gem002d.html */ /* http://www.multi-platforms.com/Tips/PopCount.htm */ static inline uint32_t popcount (uint32_t u) @@ -56,34 +480,6 @@ static inline uint32_t lsbindex (uint32_t u) return popcount ((u&-u)-1); } -#define MIN(a, b) ((a)>(b)?(b):(a)) -#define MAX(a, b) ((a)<(b)?(b):(a)) - -#define DEREF(x) (void)x -#define log(...) fprintf (stderr, "oss: " __VA_ARGS__) -#define ERRFail(...) do { \ - int _errno = errno; \ - fprintf (stderr, "oss: " __VA_ARGS__); \ - fprintf (stderr, "system error: %s\n", strerror (_errno)); \ - abort (); \ -} while (0) -#define Fail(...) do { \ - fprintf (stderr, "oss: " __VA_ARGS__); \ - fprintf (stderr, "\n"); \ - abort (); \ -} while (0) - -#ifdef DEBUG_OSS -#define lwarn(...) fprintf (stderr, "oss: " __VA_ARGS__) -#define linfo(...) fprintf (stderr, "oss: " __VA_ARGS__) -#define ldebug(...) fprintf (stderr, "oss: " __VA_ARGS__) -#else -#define lwarn(...) -#define linfo(...) -#define ldebug(...) -#endif - - #define IOCTL(args) do { \ int ret = ioctl args; \ if (-1 == ret) { \ @@ -92,7 +488,7 @@ static inline uint32_t lsbindex (uint32_t u) ldebug ("ioctl " #args " = %d\n", ret); \ } while (0) -static struct { +typedef struct AudioState { int fd; int freq; int bits16; @@ -111,7 +507,9 @@ static struct { int leftover; uint64_t old_ticks; void (*copy_fn)(void *, void *, int); -} oss = { .fd = -1 }; +} AudioState; + +static AudioState oss_audio = { .fd = -1 }; static struct { int try_mmap; @@ -125,25 +523,7 @@ static struct { static enum {DONT, DSP, TID} est = DONT; -static void copy_no_conversion (void *dst, void *src, int size) -{ - memcpy (dst, src, size); -} - -static void copy_u16_to_s16 (void *dst, void *src, int size) -{ - int i; - uint16_t *out, *in; - - out = dst; - in = src; - - for (i = 0; i < size / 2; i++) { - out[i] = in[i] + 0x8000; - } -} - -static void pab (struct audio_buf_info *abinfo) +static void pab (AudioState *s, struct audio_buf_info *abinfo) { DEREF (abinfo); @@ -153,118 +533,122 @@ static void pab (struct audio_buf_info *abinfo) abinfo->fragstotal, abinfo->fragsize, abinfo->bytes, - rpos, wpos, live); + s->rpos, s->wpos, s->live); } -static void do_open () +static void do_open (AudioState *s) { int mmmmssss; audio_buf_info abinfo; int fmt, freq, nchannels; - if (oss.buf) { - if (-1 == munmap (oss.buf, oss.bufsize)) { + if (s->buf) { + if (s->is_mapped) { + if (-1 == munmap (s->buf, s->bufsize)) { ERRFail ("failed to unmap audio buffer %p %d", - oss.buf, oss.bufsize); + s->buf, s->bufsize); + } } - oss.buf = NULL; + else { + qemu_free (s->buf); + } + s->buf = NULL; } - if (-1 != oss.fd) - close (oss.fd); + if (-1 != s->fd) + close (s->fd); - oss.fd = open ("/dev/dsp", O_RDWR | O_NONBLOCK); - if (-1 == oss.fd) { + s->fd = open ("/dev/dsp", O_RDWR | O_NONBLOCK); + if (-1 == s->fd) { ERRFail ("can not open /dev/dsp"); } - fmt = oss.oss_fmt; - freq = oss.freq; - nchannels = oss.nchannels; + fmt = s->oss_fmt; + freq = s->freq; + nchannels = s->nchannels; - IOCTL ((oss.fd, SNDCTL_DSP_RESET, 1)); - IOCTL ((oss.fd, SNDCTL_DSP_SAMPLESIZE, &fmt)); - IOCTL ((oss.fd, SNDCTL_DSP_CHANNELS, &nchannels)); - IOCTL ((oss.fd, SNDCTL_DSP_SPEED, &freq)); - IOCTL ((oss.fd, SNDCTL_DSP_NONBLOCK)); + IOCTL ((s->fd, SNDCTL_DSP_RESET, 1)); + IOCTL ((s->fd, SNDCTL_DSP_SAMPLESIZE, &fmt)); + IOCTL ((s->fd, SNDCTL_DSP_CHANNELS, &nchannels)); + IOCTL ((s->fd, SNDCTL_DSP_SPEED, &freq)); + IOCTL ((s->fd, SNDCTL_DSP_NONBLOCK)); mmmmssss = (conf.nfrags << 16) | conf.fragsize; - IOCTL ((oss.fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)); + IOCTL ((s->fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)); - if ((oss.oss_fmt != fmt) - || (oss.nchannels != nchannels) - || (oss.freq != freq)) { + if ((s->oss_fmt != fmt) + || (s->nchannels != nchannels) + || (s->freq != freq)) { Fail ("failed to set audio parameters\n" "parameter | requested value | obtained value\n" "format | %10d | %10d\n" "channels | %10d | %10d\n" "frequency | %10d | %10d\n", - oss.oss_fmt, fmt, - oss.nchannels, nchannels, - oss.freq, freq); + s->oss_fmt, fmt, + s->nchannels, nchannels, + s->freq, freq); } - IOCTL ((oss.fd, SNDCTL_DSP_GETOSPACE, &abinfo)); + IOCTL ((s->fd, SNDCTL_DSP_GETOSPACE, &abinfo)); - oss.nfrags = abinfo.fragstotal; - oss.fragsize = abinfo.fragsize; - oss.bufsize = oss.nfrags * oss.fragsize; - oss.old_optr = 0; + s->nfrags = abinfo.fragstotal; + s->fragsize = abinfo.fragsize; + s->bufsize = s->nfrags * s->fragsize; + s->old_optr = 0; - oss.bytes_per_second = (freq << (nchannels >> 1)) << oss.bits16; + s->bytes_per_second = (freq << (nchannels >> 1)) << s->bits16; - linfo ("bytes per second %d\n", oss.bytes_per_second); + linfo ("bytes per second %d\n", s->bytes_per_second); linfo ("fragments %d, fragstotal %d, fragsize %d, bytes %d, bufsize %d\n", abinfo.fragments, abinfo.fragstotal, abinfo.fragsize, abinfo.bytes, - oss.bufsize); + s->bufsize); - oss.buf = MAP_FAILED; - oss.is_mapped = 0; + s->buf = MAP_FAILED; + s->is_mapped = 0; if (conf.try_mmap) { - oss.buf = mmap (NULL, oss.bufsize, PROT_WRITE, MAP_SHARED, oss.fd, 0); - if (MAP_FAILED == oss.buf) { + s->buf = mmap (NULL, s->bufsize, PROT_WRITE, MAP_SHARED, s->fd, 0); + if (MAP_FAILED == s->buf) { int err; err = errno; - log ("failed to mmap audio, size %d, fd %d\n" + dolog ("failed to mmap audio, size %d, fd %d\n" "syserr: %s\n", - oss.bufsize, oss.fd, strerror (err)); + s->bufsize, s->fd, strerror (err)); } else { est = TID; - oss.is_mapped = 1; + s->is_mapped = 1; } } - if (MAP_FAILED == oss.buf) { + if (MAP_FAILED == s->buf) { est = TID; - oss.buf = mmap (NULL, oss.bufsize, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - if (MAP_FAILED == oss.buf) { - ERRFail ("mmap audio buf, size %d", oss.bufsize); + s->buf = qemu_mallocz (s->bufsize); + if (!s->buf) { + ERRFail ("audio buf malloc failed, size %d", s->bufsize); } } - oss.rpos = 0; - oss.wpos = 0; - oss.live = 0; + s->rpos = 0; + s->wpos = 0; + s->live = 0; - if (oss.is_mapped) { + if (s->is_mapped) { int trig; trig = 0; - IOCTL ((oss.fd, SNDCTL_DSP_SETTRIGGER, &trig)); + IOCTL ((s->fd, SNDCTL_DSP_SETTRIGGER, &trig)); trig = PCM_ENABLE_OUTPUT; - IOCTL ((oss.fd, SNDCTL_DSP_SETTRIGGER, &trig)); + IOCTL ((s->fd, SNDCTL_DSP_SETTRIGGER, &trig)); } } -static void maybe_open (int req_freq, int req_nchannels, +static void maybe_open (AudioState *s, int req_freq, int req_nchannels, audfmt_e req_fmt, int force_open) { int oss_fmt, bits16; @@ -273,7 +657,7 @@ static void maybe_open (int req_freq, int req_nchannels, case AUD_FMT_U8: bits16 = 0; oss_fmt = AFMT_U8; - oss.copy_fn = copy_no_conversion; + s->copy_fn = copy_no_conversion; break; case AUD_FMT_S8: @@ -282,13 +666,13 @@ static void maybe_open (int req_freq, int req_nchannels, case AUD_FMT_S16: bits16 = 1; oss_fmt = AFMT_S16_LE; - oss.copy_fn = copy_no_conversion; + s->copy_fn = copy_no_conversion; break; case AUD_FMT_U16: bits16 = 1; oss_fmt = AFMT_S16_LE; - oss.copy_fn = copy_u16_to_s16; + s->copy_fn = copy_u16_to_s16; break; default: @@ -296,55 +680,58 @@ static void maybe_open (int req_freq, int req_nchannels, } if (force_open - || (-1 == oss.fd) - || (oss_fmt != oss.oss_fmt) - || (req_nchannels != oss.nchannels) - || (req_freq != oss.freq) - || (bits16 != oss.bits16)) { - oss.oss_fmt = oss_fmt; - oss.nchannels = req_nchannels; - oss.freq = req_freq; - oss.bits16 = bits16; - do_open (); + || (-1 == s->fd) + || (oss_fmt != s->oss_fmt) + || (req_nchannels != s->nchannels) + || (req_freq != s->freq) + || (bits16 != s->bits16)) { + s->oss_fmt = oss_fmt; + s->nchannels = req_nchannels; + s->freq = req_freq; + s->bits16 = bits16; + do_open (s); } } void AUD_reset (int req_freq, int req_nchannels, audfmt_e req_fmt) { - maybe_open (req_freq, req_nchannels, req_fmt, 0); + AudioState *s = &oss_audio; + maybe_open (s, req_freq, req_nchannels, req_fmt, 0); } void AUD_open (int req_freq, int req_nchannels, audfmt_e req_fmt) { - maybe_open (req_freq, req_nchannels, req_fmt, 1); + AudioState *s = &oss_audio; + maybe_open (s, req_freq, req_nchannels, req_fmt, 1); } int AUD_write (void *in_buf, int size) { + AudioState *s = &oss_audio; int to_copy, temp; uint8_t *in, *out; - to_copy = MIN (oss.bufsize - oss.live, size); + to_copy = MIN (s->bufsize - s->live, size); temp = to_copy; in = in_buf; - out = oss.buf; + out = s->buf; while (temp) { int copy; - copy = MIN (temp, oss.bufsize - oss.wpos); - oss.copy_fn (out + oss.wpos, in, copy); + copy = MIN (temp, s->bufsize - s->wpos); + s->copy_fn (out + s->wpos, in, copy); - oss.wpos += copy; - if (oss.wpos == oss.bufsize) { - oss.wpos = 0; + s->wpos += copy; + if (s->wpos == s->bufsize) { + s->wpos = 0; } temp -= copy; in += copy; - oss.live += copy; + s->live += copy; } return to_copy; @@ -355,15 +742,16 @@ void AUD_run (void) int res; int bytes; struct audio_buf_info abinfo; + AudioState *s = &oss_audio; - if (0 == oss.live) + if (0 == s->live) return; - if (oss.is_mapped) { + if (s->is_mapped) { count_info info; - res = ioctl (oss.fd, SNDCTL_DSP_GETOPTR, &info); - if (-1 == res) { + res = ioctl (s->fd, SNDCTL_DSP_GETOPTR, &info); + if (res < 0) { int err; err = errno; @@ -371,29 +759,30 @@ void AUD_run (void) return; } - if (info.ptr > oss.old_optr) { - bytes = info.ptr - oss.old_optr; + if (info.ptr > s->old_optr) { + bytes = info.ptr - s->old_optr; } else { - bytes = oss.bufsize + info.ptr - oss.old_optr; + bytes = s->bufsize + info.ptr - s->old_optr; } - oss.old_optr = info.ptr; - oss.live -= bytes; + s->old_optr = info.ptr; + s->live -= bytes; return; } - res = ioctl (oss.fd, SNDCTL_DSP_GETOSPACE, &abinfo); + res = ioctl (s->fd, SNDCTL_DSP_GETOSPACE, &abinfo); - if (-1 == res) { + if (res < 0) { int err; err = errno; lwarn ("SNDCTL_DSP_GETOSPACE failed with %s\n", strerror (err)); + return; } bytes = abinfo.bytes; - bytes = MIN (oss.live, bytes); + bytes = MIN (s->live, bytes); #if 0 bytes = (bytes / fragsize) * fragsize; #endif @@ -401,9 +790,9 @@ void AUD_run (void) while (bytes) { int left, play, written; - left = oss.bufsize - oss.rpos; + left = s->bufsize - s->rpos; play = MIN (left, bytes); - written = write (oss.fd, (uint8_t *)oss.buf + oss.rpos, play); + written = write (s->fd, (uint8_t *)s->buf + s->rpos, play); if (-1 == written) { if (EAGAIN == errno || EINTR == errno) { @@ -415,12 +804,12 @@ void AUD_run (void) } play = written; - oss.live -= play; - oss.rpos += play; + s->live -= play; + s->rpos += play; bytes -= play; - if (oss.rpos == oss.bufsize) { - oss.rpos = 0; + if (s->rpos == s->bufsize) { + s->rpos = 0; } } } @@ -429,8 +818,9 @@ static int get_dsp_bytes (void) { int res; struct count_info info; + AudioState *s = &oss_audio; - res = ioctl (oss.fd, SNDCTL_DSP_GETOPTR, &info); + res = ioctl (s->fd, SNDCTL_DSP_GETOPTR, &info); if (-1 == res) { int err; @@ -446,16 +836,18 @@ static int get_dsp_bytes (void) void AUD_adjust_estimate (int leftover) { - oss.leftover = leftover; + AudioState *s = &oss_audio; + s->leftover = leftover; } int AUD_get_free (void) { int free, elapsed; + AudioState *s = &oss_audio; - free = oss.bufsize - oss.live; + free = s->bufsize - s->live; - if (0 == free) + if (free <= 0) return 0; elapsed = free; @@ -485,10 +877,10 @@ int AUD_get_free (void) uint64_t al_elapsed; ticks = qemu_get_clock(rt_clock); - delta = ticks - oss.old_ticks; - oss.old_ticks = ticks; + delta = ticks - s->old_ticks; + s->old_ticks = ticks; - ua_elapsed = (delta * oss.bytes_per_second) / 1000; + ua_elapsed = (delta * s->bytes_per_second) / 1000; al_elapsed = ua_elapsed & ~3ULL; ldebug ("tid elapsed %llu bytes\n", ua_elapsed); @@ -498,7 +890,7 @@ int AUD_get_free (void) else elapsed = al_elapsed; - elapsed += oss.leftover; + elapsed += s->leftover; } } @@ -513,31 +905,20 @@ int AUD_get_free (void) int AUD_get_live (void) { - return oss.live; + AudioState *s = &oss_audio; + return s->live; } int AUD_get_buffer_size (void) { - return oss.bufsize; + AudioState *s = &oss_audio; + return s->bufsize; } #define QC_OSS_FRAGSIZE "QEMU_OSS_FRAGSIZE" #define QC_OSS_NFRAGS "QEMU_OSS_NFRAGS" #define QC_OSS_MMAP "QEMU_OSS_MMAP" -static int get_conf_val (const char *key, int defval) -{ - int val = defval; - char *strval; - - strval = getenv (key); - if (strval) { - val = atoi (strval); - } - - return val; -} - void AUD_init (void) { int fsp;