example/aica: G2 AICA DMA

This commit is contained in:
Zack Buhman 2025-05-15 18:21:34 -05:00
parent 49203a0b0b
commit 0b3cf2ead5
5 changed files with 179 additions and 48 deletions

View File

@ -35,10 +35,10 @@ void main()
wait(); aica_sound.common.afsel_mslc_mobuf = aica::afsel_mslc_mobuf::MSLC(0);
serial::string("mrwinh: ");
wait_read();
wait();
serial::integer<uint8_t>(aica_sound.common.MRWINH());
while (1) {
wait_read();
wait();
serial::string("sgc: ");
serial::integer<uint8_t>(aica_sound.common.SGC(), ' ');
serial::string("; ca: ");

View File

@ -9,7 +9,8 @@ example/aica/aica.elf: $(START_OBJ) $(AICA_OBJ)
AICA_XM_OBJ = \
example/aica/aica_xm.o \
sh7091/serial.o \
example/arm/xm.bin.o
example/arm/xm.bin.o \
example/arm/channel.bin.o
example/aica/aica_xm.elf: LDSCRIPT = $(LIB)/main.lds
example/aica/aica_xm.elf: $(START_OBJ) $(AICA_XM_OBJ)

View File

@ -3,9 +3,16 @@
#include "systembus.hpp"
#include "systembus_bits.hpp"
#include "aica/aica.hpp"
#include "sh7091/sh7091.hpp"
#include "sh7091/sh7091_bits.hpp"
#include "assert.h"
#include "example/arm/xm.bin.h"
extern void * _binary_start __asm("_binary_example_arm_channel_bin_start");
extern void * _binary_size __asm("_binary_example_arm_channel_bin_size");
void wait()
{
uint32_t ffst = system.FFST;
@ -16,35 +23,97 @@ void wait()
};
}
constexpr uint32_t dma_address_mask = 0x1fffffe0;
void g2_aica_dma(uint32_t g2_address, uint32_t system_address, int length)
{
using namespace dmac;
length = (length + 31) & (~31);
// is DMAOR needed?
sh7091.DMAC.DMAOR = dmaor::ddt::on_demand_data_transfer_mode /* on-demand data transfer mode */
| dmaor::pr::ch2_ch0_ch1_ch3 /* priority mode; CH2 > CH0 > CH1 > CH3 */
| dmaor::dme::operation_enabled_on_all_channels; /* DMAC master enable */
g2_if.G2APRO = 0x4659007f; // disable protection
g2_if.ADSTAG = dma_address_mask & g2_address; // G2 address
g2_if.ADSTAR = dma_address_mask & system_address; // system memory address
g2_if.ADLEN = length;
g2_if.ADDIR = 0; // from root bus to G2 device
g2_if.ADTSEL = 0; // CPU controlled trigger
g2_if.ADEN = 1; // enable G2-DMA
g2_if.ADST = 1; // start G2-DMA
}
void g2_aica_dma_wait_complete()
{
// wait for maple DMA completion
while ((system.ISTNRM & istnrm::end_of_dma_aica_dma) == 0);
system.ISTNRM = istnrm::end_of_dma_aica_dma;
}
uint8_t __attribute__((aligned(32))) zero[32768] = {0};
void main()
{
serial::init(0);
const uint32_t * binary = reinterpret_cast<uint32_t *>(&_binary_example_arm_xm_bin_start);
const uint32_t binary_size = reinterpret_cast<uint32_t>(&_binary_example_arm_xm_bin_size);
const int start = reinterpret_cast<int>(&_binary_start);
const int size = reinterpret_cast<int>(&_binary_size);
wait(); aica_sound.common.vreg_armrst = aica::vreg_armrst::ARMRST(1);
wait(); aica_sound.common.dmea0_mrwinh = aica::dmea0_mrwinh::MRWINH(0b0111);
for (uint32_t i = 0; i < binary_size / 4; i++) {
// copy
while (aica_wave_memory[i] != binary[i]) {
wait();
aica_wave_memory[i] = binary[i];
system.ISTNRM = istnrm::end_of_dma_aica_dma;
g2_aica_dma((uint32_t)&aica_sound, (int)zero, 32768);
g2_aica_dma_wait_complete();
assert(g2_if.ADST == 0);
g2_aica_dma((uint32_t)aica_wave_memory, start, size);
g2_aica_dma_wait_complete();
assert(g2_if.ADST == 0);
for (int i = 0; i < size / 4; i++) {
wait();
assert(aica_wave_memory[i] == ((uint32_t*)start)[i]);
}
wait(); aica_sound.common.dmea0_mrwinh = aica::dmea0_mrwinh::MRWINH(0b0001);
wait(); aica_sound.channel[0].KYONB(1);
wait(); aica_sound.channel[0].LPCTL(1);
wait(); aica_sound.channel[0].PCMS(0);
wait(); aica_sound.channel[0].LSA(0);
wait(); aica_sound.channel[0].LEA(44100);
wait(); aica_sound.channel[0].D2R(0x0);
wait(); aica_sound.channel[0].D1R(0x0);
wait(); aica_sound.channel[0].RR(0x0);
wait(); aica_sound.channel[0].AR(0x1f);
wait(); aica_sound.channel[0].OCT(0);
wait(); aica_sound.channel[0].FNS(0);
wait(); aica_sound.channel[0].DISDL(0xf);
wait(); aica_sound.channel[0].DIPAN(0x0);
wait(); aica_sound.channel[0].Q(0b00100);
wait(); aica_sound.channel[0].TL(0);
wait(); aica_sound.channel[0].LPOFF(1);
wait(); aica_sound.common.mono_mem8mb_dac18b_ver_mvol =
aica::mono_mem8mb_dac18b_ver_mvol::MONO(0) // enable panpots
| aica::mono_mem8mb_dac18b_ver_mvol::MEM8MB(0) // 16Mbit SDRAM
| aica::mono_mem8mb_dac18b_ver_mvol::DAC18B(0) // 16-bit DAC
| aica::mono_mem8mb_dac18b_ver_mvol::MVOL(0xf) // 15/15 volume
;
wait(); aica_sound.channel[0].SA(44100 * 2);
wait(); aica_sound.channel[0].KYONEX(1);
while (1) {
for (int i = 0; i < 10000000; i++) {
asm volatile ("nop");
}
}
wait(); aica_sound.common.dmea0_mrwinh = aica::dmea0_mrwinh::MRWINH(0b0001);
wait(); aica_sound.common.vreg_armrst = aica::vreg_armrst::ARMRST(0);
wait(); aica_sound.common.afsel_mslc_mobuf = aica::afsel_mslc_mobuf::MSLC(0);
serial::string("mrwinh: ");
wait();
serial::integer<uint8_t>(aica_sound.common.MRWINH());
int last_dram = -1;
while (1) {
wait();
int read = aica_wave_memory[0];
if (read != last_dram) {
serial::integer<uint32_t>(read);
}
last_dram = read;
};
}

View File

@ -2,7 +2,7 @@ MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
DIR := $(dir $(MAKEFILE_PATH))
LIB ?= .
OPT ?= -O1
OPT ?= -Og
GENERATED ?=
AARCH = -march=armv4 -mlittle-endian

View File

@ -11,9 +11,12 @@ struct xm_state {
xm_header_t * header;
xm_pattern_header_t * pattern_header[max_patterns];
xm_instrument_header_t * instrument_header[max_instruments];
xm_sample_header_t * sample_header; // array
xm_sample_header_t * sample_header[max_instruments]; // array
int sample_data_address[max_instruments];
};
uint8_t __attribute__((aligned(2))) sample_data[512 * 1024];
static xm_state xm = {0};
int s16(void * buf)
@ -30,20 +33,6 @@ int s32(void * buf)
return v;
}
int xm_samples_init(int buf, int offset, int number_of_samples)
{
xm_sample_header_t * sample_header[number_of_samples];
for (int i = 0; i < number_of_samples; i++) {
sample_header[i] = (xm_sample_header_t *)(buf + offset);
offset += (sizeof (xm_sample_header_t));
}
for (int i = 0; i < number_of_samples; i++) {
offset += s32(&sample_header[i]->sample_length);
}
return offset;
}
void print(int i)
{
for (int i = 0; i < 100000; i++) {
@ -52,6 +41,66 @@ void print(int i)
dram[0] = i;
}
int unpack_sample(int buf, int offset, int sample_ix, xm_sample_header_t * sample_header, int sample_data_ix)
{
int size = s32(&sample_header->sample_length);
if (sample_header->type & (1 << 4)) { // 16-bit samples
int num_samples = size / 2;
int old = 0;
volatile int16_t * out = (volatile int16_t *)(&sample_data[sample_data_ix]);
print((int)out);
int16_t * in = (int16_t *)(buf + offset);
for (int i = 0; i < num_samples; i++) {
old += s16(&in[i]);
while (((uint16_t)out[i]) != ((uint16_t)old)) {
out[i] = old;
print((uint16_t)old);
print((uint16_t)out[i]);
}
}
} else { // 8-bit
int num_samples = size;
int old = 0;
volatile int8_t * out = (volatile int8_t *)(&sample_data[sample_data_ix]);
int8_t * in = (int8_t *)(buf + offset);
for (int i = 0; i < num_samples; i++) {
old += in[i];
out[i] = old;
}
}
if (size & 1) {
size += 1;
}
return size;
}
static int sample_data_ix;
int xm_samples_init(int buf, int offset, int instrument_ix, int number_of_samples)
{
xm_sample_header_t * sample_header[number_of_samples];
xm.sample_header[instrument_ix] = (xm_sample_header_t *)(buf + offset);
for (int i = 0; i < number_of_samples; i++) {
sample_header[i] = (xm_sample_header_t *)(buf + offset);
offset += (sizeof (xm_sample_header_t));
}
if (number_of_samples > 0) {
print(instrument_ix);
print(sample_data_ix);
}
for (int i = 0; i < number_of_samples; i++) {
if (s32(&sample_header[i]->sample_length) > 0) {
xm.sample_data_address[instrument_ix] = (int)(&sample_data[sample_data_ix]);
sample_data_ix += unpack_sample(buf, offset, instrument_ix, sample_header[i], sample_data_ix);
}
offset += s32(&sample_header[i]->sample_length);
}
return offset;
}
void xm_init()
{
int buf = (int)(&_binary_xm_milkypack01_xm_start);
@ -74,6 +123,8 @@ void xm_init()
print(offset);
}
print(0xaaaaaaaa);
sample_data_ix = 0;
int number_of_instruments = s16(&xm.header->number_of_instruments);
for (int i = 0; i < number_of_instruments; i++) {
xm_instrument_header_t * instrument_header = (xm_instrument_header_t *)(buf + offset);
@ -82,7 +133,7 @@ void xm_init()
offset += s32(&instrument_header->instrument_size);
int number_of_samples = s16(&instrument_header->number_of_samples);
offset = xm_samples_init(buf, offset, number_of_samples);
offset = xm_samples_init(buf, offset, i, number_of_samples);
}
print(0x11223344);
@ -112,7 +163,7 @@ void main()
aica_sound.channel[0].RR(0x0);
aica_sound.channel[0].AR(0x1f);
aica_sound.channel[0].OCT(0);
aica_sound.channel[0].OCT(-1);
aica_sound.channel[0].FNS(0);
aica_sound.channel[0].DISDL(0xf);
aica_sound.channel[0].DIPAN(0x0);
@ -128,17 +179,26 @@ void main()
| aica::mono_mem8mb_dac18b_ver_mvol::MVOL(0xf) // 15/15 volume
;
uint32_t segment = 0;
constexpr uint32_t timer_a_interrupt = (1 << 6);
aica_sound.common.scire = timer_a_interrupt;
bool started = 0;
xm_init();
print(xm.sample_data_address[0]);
aica_sound.channel[0].SA(xm.sample_data_address[0]);
int lsa = xm.sample_header[0]->sample_loop_start / 2;
int lea = xm.sample_header[0]->sample_loop_length / 2;
print(lsa);
print(lea);
aica_sound.channel[0].LSA(lsa);
aica_sound.channel[0].LEA(lsa + lea);
aica_sound.channel[0].KYONB(1);
aica_sound.channel[0].KYONEX(1);
while (1) {
/*
if (!started || (aica_sound.common.SCIPD() & timer_a_interrupt)) {
//aica_sound.channel[0].SA(next_sa);
aica_sound.channel[0].SA();
aica_sound.common.tactl_tima =
aica::tactl_tima::TACTL(0) // increment once every 128 samples
| aica::tactl_tima::TIMA(256 - 128) // interrupt after 128 counts
@ -148,5 +208,6 @@ void main()
aica_sound.common.scire = timer_a_interrupt;
}
*/
}
}