1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
//
// ai/controller.c: Audio interface controller.
//
// CEN64: Cycle-Accurate Nintendo 64 Emulator.
// Copyright (C) 2015, Tyler J. Stachecki.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#include "common.h"
#include "ai/context.h"
#include "ai/controller.h"
#include "bus/address.h"
#include "bus/controller.h"
#include "ri/controller.h"
#include "rsp/rsp.h"
#include "vr4300/interface.h"
#define NTSC_DAC_FREQ 48681812 // 48.681812MHz
#ifdef DEBUG_MMIO_REGISTER_ACCESS
const char *ai_register_mnemonics[NUM_AI_REGISTERS] = {
#define X(reg) #reg,
#include "ai/registers.md"
#undef X
};
#endif
static void ai_dma(struct ai_controller *ai);
static const uint8_t *byteswap_audio_buffer(const uint8_t *input,
uint8_t *output, uint32_t length);
// Advances the controller by one clock cycle.
void ai_cycle_(struct ai_controller *ai) {
// DMA engine is finishing up with one entry.
if (ai->fifo_count > 0) {
struct bus_controller *bus;
memcpy(&bus, ai, sizeof(bus));
signal_rcp_interrupt(bus->vr4300, MI_INTR_AI);
ai->fifo_ri ^= 0x1;
ai->regs[AI_STATUS_REG] &= ~0xC0000001;
ai->fifo_count--;
if (ai->fifo_count > 0) {
ai->regs[AI_STATUS_REG] = 0x40000000;
ai_dma(ai);
}
}
}
// Performs an (instantaneous) DMA.
void ai_dma(struct ai_controller *ai) {
struct bus_controller *bus;
// Shove things into the audio context, slide the window.
memcpy(&bus, ai, sizeof(bus));
if (ai->fifo[ai->fifo_ri].length > 0) {
unsigned freq = (double) NTSC_DAC_FREQ / (ai->regs[AI_DACRATE_REG] + 1);
unsigned samples = ai->fifo[ai->fifo_ri].length / 4;
// Need to raise an interrupt when the DMA engine
// is doing the last 8-byte data bus transfer...
//
// XXX: Should be > 2, I think, but don't want to
// risk breaking things and would need to verify.
ai->counter = (62500000.0 / freq) * (samples - 2);
// Shovel things into the audio context.
ALuint buffer;
ALint val;
alGetSourcei(ai->ctx.source, AL_BUFFERS_PROCESSED, &val);
// XXX: Most games pick one frequency and stick with it.
// Instead of paying garbage, try to dynamically switch
// the frequency of the buffers that OpenAL is using.
//
// This will result in pops and other unpleasant things
// when the frequency changes underneath us, but it still
// seems to sound better than what we had before.
if (ai->ctx.cur_frequency != freq) {
if (val == sizeof(ai->ctx.buffers) / sizeof(ai->ctx.buffers[0])) {
printf("OpenAL: Switching context buffer frequency to: %u\n", freq);
ai_switch_frequency(&ai->ctx, freq);
}
else
val = 0;
}
if (val) {
cen64_align(uint8_t buf[0x40000], 16);
uint32_t length = ai->fifo[ai->fifo_ri].length;
uint8_t *input = bus->ri->ram + ai->fifo[ai->fifo_ri].address;
const uint8_t *buf_ptr = byteswap_audio_buffer(input, buf, length);
if (ai->ctx.unqueued_buffers > 0) {
buffer = ai->ctx.buffers[sizeof(ai->ctx.buffers) /
sizeof(ai->ctx.buffers[0]) - ai->ctx.unqueued_buffers];
ai->ctx.unqueued_buffers--;
}
else
alSourceUnqueueBuffers(ai->ctx.source, 1, &buffer);
alBufferData(buffer, AL_FORMAT_STEREO16, buf_ptr, length, freq);
alSourceQueueBuffers(ai->ctx.source, 1, &buffer);
if (ai->ctx.unqueued_buffers == 1) {
alSourcePlay(ai->ctx.source);
}
else {
alGetSourcei(ai->ctx.source, AL_SOURCE_STATE, &val);
if (val != AL_PLAYING)
alSourcePlay(ai->ctx.source);
}
if (alGetError() != AL_NO_ERROR) {
fprintf(stderr, "OpenAL: Reporting an error while playing sources!\n");
fprintf(stderr, "Disabling it from this point forward; sorry!\n");
ai->no_output = true;
}
}
}
// If the length was zero, just interrupt now?
else {
signal_rcp_interrupt(bus->vr4300, MI_INTR_AI);
ai->fifo_ri ^= 0x1;
ai->regs[AI_STATUS_REG] &= ~0xC0000001;
ai->fifo_count--;
if (ai->fifo_count > 0) {
ai->regs[AI_STATUS_REG] |= 0x40000000;
ai->counter = 1;
}
}
}
// Initializes the AI.
int ai_init(struct ai_controller *ai,
struct bus_controller *bus, bool no_interface) {
ai->bus = bus;
ai->no_output = no_interface;
if (!no_interface) {
alGetError();
if (ai_context_create(&ai->ctx))
return 1;
}
return 0;
}
// Reads a word from the AI MMIO register space.
int read_ai_regs(void *opaque, uint32_t address, uint32_t *word) {
struct ai_controller *ai = (struct ai_controller *) opaque;
unsigned offset = address - AI_REGS_BASE_ADDRESS;
enum ai_register reg = (offset >> 2);
*word = ai->regs[reg];
debug_mmio_read(ai, ai_register_mnemonics[reg], *word);
if (reg == AI_LEN_REG) {
*word = 0;
if (ai->regs[AI_STATUS_REG] & 0x80000001)
*word = ai->regs[AI_LEN_REG];
else if (ai->regs[AI_STATUS_REG] & 0x40000000) {
// TODO
}
}
return 0;
}
// Writes a word to the AI MMIO register space.
int write_ai_regs(void *opaque, uint32_t address, uint32_t word, uint32_t dqm) {
struct ai_controller *ai = (struct ai_controller *) opaque;
unsigned offset = address - AI_REGS_BASE_ADDRESS;
enum ai_register reg = (offset >> 2);
debug_mmio_write(ai, ai_register_mnemonics[reg], word, dqm);
if (reg == AI_DRAM_ADDR_REG)
ai->regs[AI_DRAM_ADDR_REG] = word & 0xFFFFF8;
else if (reg == AI_LEN_REG) {
ai->regs[AI_LEN_REG] = word & 0x3FFF8;
if (ai->fifo_count == 2)
return 0;
// Fill the next FIFO entry in the DMA engine.
ai->fifo[ai->fifo_wi].address = ai->regs[AI_DRAM_ADDR_REG];
ai->fifo[ai->fifo_wi].length = ai->regs[AI_LEN_REG];
ai->fifo_wi ^= 0x1;
ai->fifo_count++;
if (ai->fifo_count == 2)
ai->regs[AI_STATUS_REG] |= 0x80000001U;
// If we're not DMA-ing already, start DMA engine.
if (!(ai->regs[AI_STATUS_REG] & 0x40000000U)) {
ai->regs[AI_STATUS_REG] = 0x40000000;
ai_dma(ai);
}
}
else if (reg == AI_STATUS_REG) {
struct bus_controller *bus;
memcpy(&bus, ai, sizeof(bus));
clear_rcp_interrupt(bus->vr4300, MI_INTR_AI);
}
else if (reg == AI_DACRATE_REG) {
ai->regs[AI_DACRATE_REG] = word & 0x3FFF;
ai->ctx.frequency = (double) NTSC_DAC_FREQ / (ai->regs[AI_DACRATE_REG] + 1);
}
else if (reg == AI_BITRATE_REG)
ai->regs[AI_BITRATE_REG] = word & 0xF;
else
ai->regs[reg] = word;
return 0;
}
const uint8_t *byteswap_audio_buffer(const uint8_t *input,
uint8_t *output, uint32_t length) {
uint32_t i = 0;
#ifdef __SSE2__
#ifdef __SSSE3__
static const uint32_t byteswap_key[] = {
0x02030001, 0x06070405, 0x0A0B0809, 0x0E0F0C0D};
const __m128i key = _mm_load_si128((__m128i*) byteswap_key);
#endif
const uint8_t *ret_buf;
uintptr_t input_ptr;
memcpy(&input_ptr, &input, sizeof(input_ptr));
// Align input buffer to 16 bytes (since transfers are 8 bytes).
if (input_ptr & 0x8) {
__m128i samples = _mm_loadl_epi64((__m128i*) input);
#ifdef __SSSE3__
samples = _mm_shuffle_epi8(samples, key);
#else
__m128i swapleft = _mm_slli_epi16(samples, 8);
__m128i swapright = _mm_srli_epi16(samples, 8);
samples = _mm_or_si128(swapleft, swapright);
#endif
_mm_storel_epi64((__m128i*) (output + sizeof(uint64_t)), samples);
output += sizeof(uint64_t);
i += sizeof(uint64_t);
}
ret_buf = output;
// Byteswap the majority of the buffer just our 16-byte algorithm.
while ((length - i) >= sizeof(__m128i)) {
__m128i samples = _mm_load_si128((__m128i*) (input + i));
#ifdef __SSSE3__
samples = _mm_shuffle_epi8(samples, key);
#else
__m128i swapleft = _mm_slli_epi16(samples, 8);
__m128i swapright = _mm_srli_epi16(samples, 8);
samples = _mm_or_si128(swapleft, swapright);
#endif
_mm_store_si128((__m128i*) (output + i), samples);
i += sizeof(samples);
}
// Finish last (8 byte) segment if one exists.
if (length != i) {
__m128i samples = _mm_loadl_epi64((__m128i*) (input + i));
#ifdef __SSSE3__
samples = _mm_shuffle_epi8(samples, key);
#else
__m128i swapleft = _mm_slli_epi16(samples, 8);
__m128i swapright = _mm_srli_epi16(samples, 8);
samples = _mm_or_si128(swapleft, swapright);
#endif
_mm_storel_epi64((__m128i*) (output + i), samples);
}
return ret_buf;
#else
#error "Unimplemented byteswap_audio_buffer!"
#endif
}
|