| 1 |
// license:BSD-3-Clause |
| 2 |
// copyright-holders:Hiromitsu Shioya, Olivier Galibert |
| 3 |
/*********************************************************/ |
| 4 |
/* SEGA 16ch 8bit PCM */ |
| 5 |
/*********************************************************/ |
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "segapcm.h" |
| 10 |
|
| 11 |
//------------------------------------------------- |
| 12 |
// segapcm_device - constructor |
| 13 |
//------------------------------------------------- |
| 14 |
|
| 15 |
segapcm_device::segapcm_device() |
| 16 |
: m_bankshift(12) |
| 17 |
, m_bankmask(0x70) |
| 18 |
{ |
| 19 |
memset(m_muted,0,16*sizeof(bool)); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
//------------------------------------------------- |
| 24 |
// device_start - device-specific startup |
| 25 |
//------------------------------------------------- |
| 26 |
|
| 27 |
void segapcm_device::device_start() |
| 28 |
{ |
| 29 |
memset(m_ram,255,0x800); |
| 30 |
memset(m_low,0,16); |
| 31 |
memset(lastOut,0,16*2*sizeof(short)); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
//------------------------------------------------- |
| 36 |
// sound_stream_update - handle a stream update |
| 37 |
//------------------------------------------------- |
| 38 |
|
| 39 |
void segapcm_device::sound_stream_update(int* outputs) |
| 40 |
{ |
| 41 |
/* clear the buffers */ |
| 42 |
outputs[0]=0; |
| 43 |
outputs[1]=0; |
| 44 |
|
| 45 |
// reg function |
| 46 |
// ------------------------------------------------ |
| 47 |
// 0x00 ? |
| 48 |
// 0x01 ? |
| 49 |
// 0x02 volume left |
| 50 |
// 0x03 volume right |
| 51 |
// 0x04 loop address (08-15) |
| 52 |
// 0x05 loop address (16-23) |
| 53 |
// 0x06 end address |
| 54 |
// 0x07 address delta |
| 55 |
// 0x80 ? |
| 56 |
// 0x81 ? |
| 57 |
// 0x82 ? |
| 58 |
// 0x83 ? |
| 59 |
// 0x84 current address (08-15), 00-07 is internal? |
| 60 |
// 0x85 current address (16-23) |
| 61 |
// 0x86 bit 0: channel disable? |
| 62 |
// bit 1: loop disable |
| 63 |
// other bits: bank |
| 64 |
// 0x87 ? |
| 65 |
|
| 66 |
/* loop over channels */ |
| 67 |
for (int ch = 0; ch < 16; ch++) |
| 68 |
{ |
| 69 |
uint8_t *regs = &m_ram[8*ch]; |
| 70 |
|
| 71 |
/* only process active channels */ |
| 72 |
if (!(regs[0x86]&1)) |
| 73 |
{ |
| 74 |
int offset = (regs[0x86] & m_bankmask) << m_bankshift; |
| 75 |
uint32_t addr = (regs[0x85] << 16) | (regs[0x84] << 8) | m_low[ch]; |
| 76 |
uint32_t loop = (regs[0x05] << 16) | (regs[0x04] << 8); |
| 77 |
uint8_t end = regs[6] + 1; |
| 78 |
|
| 79 |
int8_t v; |
| 80 |
bool fetch=true; |
| 81 |
|
| 82 |
/* handle looping if we've hit the end */ |
| 83 |
if ((addr >> 16) == end) |
| 84 |
{ |
| 85 |
if (regs[0x86] & 2) |
| 86 |
{ |
| 87 |
regs[0x86] |= 1; |
| 88 |
fetch=false; |
| 89 |
} |
| 90 |
else addr = loop; |
| 91 |
} |
| 92 |
|
| 93 |
/* fetch the sample */ |
| 94 |
if (fetch) { |
| 95 |
v = read_byte(offset + (addr >> 8)) - 0x80; |
| 96 |
|
| 97 |
/* apply panning and advance */ |
| 98 |
if (m_muted[ch]) { |
| 99 |
lastOut[ch][0]=0; |
| 100 |
lastOut[ch][1]=0; |
| 101 |
} else { |
| 102 |
lastOut[ch][0]=v * (regs[2] & 0x7f); |
| 103 |
lastOut[ch][1]=v * (regs[3] & 0x7f); |
| 104 |
} |
| 105 |
outputs[0]+=lastOut[ch][0]; |
| 106 |
outputs[1]+=lastOut[ch][1]; |
| 107 |
addr = (addr + regs[7]) & 0xffffff; |
| 108 |
} else { |
| 109 |
lastOut[ch][0]=0; |
| 110 |
lastOut[ch][1]=0; |
| 111 |
} |
| 112 |
|
| 113 |
/* store back the updated address */ |
| 114 |
regs[0x84] = addr >> 8; |
| 115 |
regs[0x85] = addr >> 16; |
| 116 |
m_low[ch] = regs[0x86] & 1 ? 0 : addr; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
void segapcm_device::write(unsigned int offset, uint8_t data) |
| 123 |
{ |
| 124 |
m_ram[offset & 0x07ff] = data; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
uint8_t segapcm_device::read(unsigned int offset) |
| 129 |
{ |
| 130 |
return m_ram[offset & 0x07ff]; |
| 131 |
} |
| 132 |
|
| 133 |
uint8_t* segapcm_device::get_ram() { |
| 134 |
return m_ram; |
| 135 |
} |
| 136 |
|
| 137 |
unsigned int segapcm_device::get_addr(int ch) { |
| 138 |
uint8_t *regs = &m_ram[8*ch]; |
| 139 |
int offset = (regs[0x86] & m_bankmask) << m_bankshift; |
| 140 |
uint32_t addr = (regs[0x85] << 8) | (regs[0x84]) | offset; |
| 141 |
return addr; |
| 142 |
} |
| 143 |
|
| 144 |
bool segapcm_device::is_playing(int ch) { |
| 145 |
uint8_t *regs = &m_ram[8*ch]; |
| 146 |
return !(regs[0x86]&1); |
| 147 |
} |
| 148 |
|
| 149 |
void segapcm_device::mute(int ch, bool doMute) { |
| 150 |
m_muted[ch&15]=doMute; |
| 151 |
} |