diff options
Diffstat (limited to 'hw/sb16.c')
-rw-r--r-- | hw/sb16.c | 365 |
1 files changed, 305 insertions, 60 deletions
@@ -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); } |