2025-02-01 23:58:55 -07:00
|
|
|
#include <audio.h>
|
|
|
|
#include <math.h>
|
2025-02-02 20:36:32 -07:00
|
|
|
#include <windows.h>
|
2025-02-03 16:25:18 -07:00
|
|
|
#include <emu.h>
|
2025-02-01 23:58:55 -07:00
|
|
|
|
|
|
|
#include <portaudio.h>
|
2025-02-03 16:25:18 -07:00
|
|
|
#include <pa_ringbuffer.h>
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
#define SAMPLE_RATE 192000
|
2025-02-01 23:58:55 -07:00
|
|
|
#define FRAMES_PER_BUFFER 64
|
2025-02-03 16:25:18 -07:00
|
|
|
#define TIME_PER_SAMPLE 1.0f / (SAMPLE_RATE/1000.0f/1000.0f)
|
|
|
|
#define TIME_PER_AUDIO_TICK 1.0f / (1048576.0f/1000.0f/1000.0f)
|
|
|
|
#define LFSR_BASE_CLOCK 262144.0f
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
static audio_context ctx;
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
static float audio_time = 0;
|
|
|
|
static float lfsr_timer = 0;
|
|
|
|
static float lfsr_clock = LFSR_BASE_CLOCK;
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
const u8 square_sample_00[8] = {
|
|
|
|
0x0,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF
|
|
|
|
};
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
const u8 square_sample_01[8] = {
|
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF
|
|
|
|
};
|
2025-02-01 23:58:55 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
const u8 square_sample_10[8] = {
|
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF,
|
|
|
|
0xF
|
|
|
|
};
|
2025-02-02 20:36:32 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
const u8 square_sample_11[8] = {
|
|
|
|
0x0,
|
2025-02-02 20:36:32 -07:00
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0x0,
|
|
|
|
0x0,
|
2025-02-03 16:25:18 -07:00
|
|
|
0x0,
|
|
|
|
0xF,
|
|
|
|
0xF
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8 *square_sample[4] = {
|
|
|
|
square_sample_00,
|
|
|
|
square_sample_01,
|
|
|
|
square_sample_10,
|
|
|
|
square_sample_11
|
2025-02-02 20:36:32 -07:00
|
|
|
};
|
|
|
|
|
2025-02-01 23:58:55 -07:00
|
|
|
static int audio_callback(const void* input_uffer, void *output_buffer,
|
|
|
|
unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *time_info,
|
|
|
|
PaStreamCallbackFlags status_flags,
|
|
|
|
void *userData ) {
|
|
|
|
float *out = (float *)output_buffer;
|
2025-02-02 20:36:32 -07:00
|
|
|
float left = 0;
|
|
|
|
float right = 0;
|
2025-02-01 23:58:55 -07:00
|
|
|
for(int i = 0; i < framesPerBuffer; i++) {
|
2025-02-03 16:25:18 -07:00
|
|
|
audio_time += TIME_PER_SAMPLE;
|
|
|
|
for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) {
|
|
|
|
ctx.sq1_period_timer++;
|
|
|
|
ctx.sq2_period_timer++;
|
|
|
|
ctx.ch3_period_timer++;
|
|
|
|
|
|
|
|
if(ctx.sq1_period_timer >= 0x800) {
|
|
|
|
ctx.sq1_period_timer = ctx.sq1_period_reset;
|
|
|
|
ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
|
|
|
|
}
|
|
|
|
if(ctx.sq2_period_timer >= 0x800) {
|
|
|
|
ctx.sq2_period_timer = ctx.sq2_period_reset;
|
|
|
|
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
|
|
|
|
}
|
|
|
|
if(ctx.ch3_period_timer >= 0x800) {
|
|
|
|
ctx.ch3_period_timer = ctx.ch3_period_reset;
|
|
|
|
ctx.ch3_sample = (ctx.ch3_sample + 1) % 32;
|
|
|
|
if(ctx.ch3_sample & 0b1) {
|
2025-02-03 18:51:49 -07:00
|
|
|
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] & 0xF;
|
2025-02-03 16:25:18 -07:00
|
|
|
} else {
|
2025-02-03 18:51:49 -07:00
|
|
|
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] >> 4;
|
2025-02-03 16:25:18 -07:00
|
|
|
}
|
|
|
|
}
|
2025-02-02 22:56:07 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
lfsr_timer += TIME_PER_SAMPLE;
|
|
|
|
for(;lfsr_timer >= lfsr_clock;lfsr_timer -= lfsr_clock) {
|
|
|
|
if(lfsr_timer >= lfsr_clock && ctx.ch4_enable) {
|
|
|
|
lfsr_timer = 0;
|
2025-02-03 18:51:49 -07:00
|
|
|
u8 new = !((ctx.ch4_lfsr & 0b1) ^ ((ctx.ch4_lfsr >> 1) & 0b1)) & 0b1;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.ch4_lfsr |= (new << 15);
|
|
|
|
if(ctx.ch4_lfsr_width) {
|
|
|
|
ctx.ch4_lfsr |= (new << 7);
|
|
|
|
}
|
2025-02-03 18:51:49 -07:00
|
|
|
ctx.ch4_lfsr = ctx.ch4_lfsr >> 1;
|
2025-02-03 16:25:18 -07:00
|
|
|
//printf("lfsr: %02X, bit: %d\n", ctx.ch4_lfsr, new);
|
|
|
|
}
|
2025-02-02 22:56:07 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
if(ctx.audio_enabled){
|
|
|
|
if(ctx.sq1_enable) {
|
|
|
|
if(ctx.ch1_left) {
|
|
|
|
left += ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f);
|
|
|
|
}
|
|
|
|
if(ctx.ch1_right) {
|
|
|
|
right += ((float)ctx.sq1_volume/15.0f) * (((float)square_sample[ctx.sq1_duty][ctx.sq1_sample] - 7.5f)/7.5f);
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
|
|
|
if(ctx.sq2_enable) {
|
|
|
|
if(ctx.ch2_left) {
|
|
|
|
left += ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f);
|
|
|
|
}
|
|
|
|
if(ctx.ch2_right) {
|
|
|
|
right += ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f);
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
2025-02-03 18:51:49 -07:00
|
|
|
if(ctx.ch3_enable && ctx.ch3_volume != 00) {
|
|
|
|
u8 shift = 0;
|
|
|
|
if(ctx.ch3_volume == 0b10) {
|
|
|
|
shift = 1;
|
|
|
|
}
|
|
|
|
if(ctx.ch3_volume == 0b11) {
|
|
|
|
shift = 2;
|
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
if(ctx.ch3_left) {
|
2025-02-03 18:51:49 -07:00
|
|
|
left += (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
|
2025-02-03 16:25:18 -07:00
|
|
|
//printf("left: %d\n", ctx.ch3_volume);
|
|
|
|
}
|
|
|
|
if(ctx.ch3_right) {
|
2025-02-03 18:51:49 -07:00
|
|
|
right += (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
|
2025-02-03 16:25:18 -07:00
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
if(ctx.ch4_enable) {
|
|
|
|
if(ctx.ch4_left) {
|
|
|
|
left += ((float)ctx.ch4_volume/15.0f) * (ctx.ch4_lfsr & 0b1);
|
|
|
|
//printf("left: %d\n", ctx.ch3_volume);
|
|
|
|
}
|
|
|
|
if(ctx.ch4_right) {
|
|
|
|
right += ((float)ctx.ch4_volume/15.0f) * (ctx.ch4_lfsr & 0b1);
|
|
|
|
}
|
|
|
|
}
|
2025-02-01 23:58:55 -07:00
|
|
|
}
|
2025-02-03 22:59:24 -07:00
|
|
|
u8 left_vol = ctx.volume_left == 0 ? 1 : ctx.volume_left;
|
|
|
|
u8 right_vol = ctx.volume_right == 0 ? 1 : ctx.volume_right;
|
|
|
|
left *= (float)left_vol/7.0f;
|
|
|
|
right *= (float)right_vol/7.0f;
|
2025-02-03 16:25:18 -07:00
|
|
|
|
|
|
|
left /= 4;
|
|
|
|
right /= 4;
|
|
|
|
|
|
|
|
if(left > 1.0f) {
|
|
|
|
printf("Uh Oh! %02X\n", ctx.volume_left);
|
2025-02-02 22:56:07 -07:00
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
*out++ = left;
|
|
|
|
*out++ = right;
|
2025-02-01 23:58:55 -07:00
|
|
|
}
|
|
|
|
return paContinue;
|
|
|
|
}
|
2025-02-02 22:56:07 -07:00
|
|
|
static PaStream *stream;
|
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
void audio_sample();
|
|
|
|
|
2025-02-01 23:58:55 -07:00
|
|
|
void audio_init(){
|
|
|
|
PaStreamParameters output_parameters;
|
|
|
|
PaError err;
|
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.audio_enabled = false;
|
|
|
|
ctx.ch1_left = false;
|
|
|
|
ctx.ch1_right = false;
|
|
|
|
ctx.ch2_left = false;
|
|
|
|
ctx.ch2_right = false;
|
|
|
|
ctx.ch3_left = false;
|
|
|
|
ctx.ch3_right = false;
|
|
|
|
ctx.ch4_left = false;
|
|
|
|
ctx.ch4_right = false;
|
|
|
|
|
|
|
|
ctx.volume_left = 0x1;
|
|
|
|
ctx.volume_right = 0x1;
|
|
|
|
|
|
|
|
ctx.sq1_duty = 0x0;
|
|
|
|
ctx.sq1_volume = 0x0;
|
|
|
|
ctx.sq1_sample = 0x0;
|
|
|
|
ctx.sq1_period_reset = 0x0;
|
|
|
|
ctx.sq1_period_timer = 0x0;
|
|
|
|
ctx.sq1_enable = false;
|
|
|
|
ctx.sq1_len_enable = false;
|
|
|
|
ctx.sq1_sweep_pace = 0x0;
|
|
|
|
ctx.sq1_sweep_direction = false;
|
|
|
|
ctx.sq1_initial_len = 0x0;
|
|
|
|
ctx.sq1_len = 0x0;
|
|
|
|
ctx.sq1_initial_volume = 0x0;
|
|
|
|
ctx.sq1_env_direction = false;
|
|
|
|
ctx.sq1_env_pace = 0x0;
|
|
|
|
ctx.sq1_env_timer = 0x0;
|
|
|
|
ctx.sq1_sweep_step = 0x0;
|
|
|
|
|
|
|
|
ctx.sq2_duty = 0x0;
|
|
|
|
ctx.sq2_volume = 0x0;
|
|
|
|
ctx.sq2_sample = 0x0;
|
|
|
|
ctx.sq2_period_reset = 0x0;
|
|
|
|
ctx.sq2_period_timer = 0x0;
|
|
|
|
ctx.sq2_enable = false;
|
2025-02-02 22:56:07 -07:00
|
|
|
ctx.sq2_len_enable = false;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq2_initial_len = 0x0;
|
|
|
|
ctx.sq2_len = 0x0;
|
|
|
|
ctx.sq2_initial_volume = 0x0;
|
|
|
|
ctx.sq2_env_direction = false;
|
|
|
|
ctx.sq2_env_pace = 0x0;
|
|
|
|
ctx.sq2_env_timer = 0x0;
|
2025-02-01 23:58:55 -07:00
|
|
|
|
|
|
|
err = Pa_Initialize();
|
|
|
|
if (err != paNoError) goto error;
|
|
|
|
output_parameters.device = Pa_GetDefaultOutputDevice();
|
|
|
|
if(output_parameters.device == paNoDevice) {
|
|
|
|
fprintf(stderr, "No default audio device!\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
output_parameters.channelCount = 2;
|
2025-02-01 23:58:55 -07:00
|
|
|
output_parameters.sampleFormat = paFloat32;
|
|
|
|
output_parameters.suggestedLatency = Pa_GetDeviceInfo(output_parameters.device)->defaultLowOutputLatency;
|
|
|
|
output_parameters.hostApiSpecificStreamInfo = NULL;
|
|
|
|
|
|
|
|
err = Pa_OpenStream(&stream,
|
|
|
|
NULL,
|
|
|
|
&output_parameters,
|
|
|
|
SAMPLE_RATE,
|
2025-02-03 16:25:18 -07:00
|
|
|
paFramesPerBufferUnspecified,
|
|
|
|
paNoFlag,
|
2025-02-01 23:58:55 -07:00
|
|
|
audio_callback,
|
|
|
|
&ctx);
|
|
|
|
if(err != paNoError) goto error;
|
|
|
|
err = Pa_StartStream(stream);
|
|
|
|
if(err != paNoError) goto error;
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
Pa_Terminate();
|
|
|
|
fprintf(stderr, "portaudio stream error\n\tError Number: %d\n\tError Message: %s\n", err, Pa_GetErrorText(err));
|
2025-02-02 12:35:53 -07:00
|
|
|
}
|
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
void sq1_sweep() {
|
|
|
|
//frequency calculation
|
|
|
|
int step = (ctx.sq1_sweep_period >> ctx.sq1_sweep_step);
|
|
|
|
step = ctx.sq1_sweep_direction ? -step : step;
|
|
|
|
ctx.sq1_calc_period = ctx.sq1_sweep_period + step;
|
|
|
|
//overflow check
|
|
|
|
if(ctx.sq1_calc_period > 0x7FFF) {
|
|
|
|
ctx.sq1_enable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 12:35:53 -07:00
|
|
|
static int change = 1;
|
2025-02-02 20:36:32 -07:00
|
|
|
static u32 ticks = 0;
|
2025-02-02 12:35:53 -07:00
|
|
|
|
|
|
|
void audio_tick(){
|
2025-02-02 20:36:32 -07:00
|
|
|
u32 prev_ticks = ticks;
|
|
|
|
ticks++;
|
|
|
|
if(!(ticks & 0b1)) {
|
|
|
|
if(ctx.sq1_len_enable) {
|
|
|
|
ctx.sq1_len++;
|
|
|
|
if(ctx.sq1_len >= 64) {
|
|
|
|
ctx.sq1_enable = false;
|
|
|
|
}
|
2025-02-03 22:59:24 -07:00
|
|
|
printf("Len: %02X\n", ctx.sq1_len);
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.sq2_len_enable) {
|
|
|
|
ctx.sq2_len++;
|
|
|
|
if(ctx.sq2_len >= 64) {
|
|
|
|
ctx.sq2_enable = false;
|
|
|
|
}
|
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
|
|
|
if(ctx.ch3_len_enable) {
|
|
|
|
ctx.ch3_len++;
|
2025-02-03 22:59:24 -07:00
|
|
|
if(ctx.ch3_len >= 256) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.ch3_enable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.ch4_len_enable) {
|
|
|
|
ctx.ch4_len++;
|
|
|
|
if(ctx.ch4_len >= 64) {
|
|
|
|
ctx.ch4_enable = false;
|
|
|
|
}
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if((prev_ticks & (1 << 3)) && !(ticks & (1 << 3))) {
|
|
|
|
if(ctx.sq1_env_pace != 0){
|
|
|
|
ctx.sq1_env_timer++;
|
|
|
|
if(ctx.sq1_env_timer >= ctx.sq1_env_pace) {
|
|
|
|
ctx.sq1_env_timer = 0;
|
2025-02-03 16:25:18 -07:00
|
|
|
if((ctx.sq1_env_direction && ctx.sq1_volume != 15) || (!ctx.sq1_env_direction && ctx.sq1_volume != 0)){
|
|
|
|
ctx.sq1_volume += ctx.sq1_env_direction ? 1 : -1;
|
|
|
|
if(ctx.sq1_volume < 0)
|
|
|
|
ctx.sq1_volume = 0;
|
|
|
|
if(ctx.sq1_volume > 15)
|
|
|
|
ctx.sq1_volume = 15;
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.sq2_env_pace != 0){
|
|
|
|
ctx.sq2_env_timer++;
|
|
|
|
if(ctx.sq2_env_timer >= ctx.sq2_env_pace) {
|
|
|
|
ctx.sq2_env_timer = 0;
|
2025-02-03 16:25:18 -07:00
|
|
|
if((ctx.sq2_env_direction && ctx.sq2_volume != 15) || (!ctx.sq2_env_direction && ctx.sq2_volume != 0)){
|
|
|
|
ctx.sq2_volume += ctx.sq2_env_direction ? 1 : -1;
|
|
|
|
if(ctx.sq2_volume < 0)
|
|
|
|
ctx.sq2_volume = 0;
|
|
|
|
if(ctx.sq2_volume > 15)
|
|
|
|
ctx.sq2_volume = 15;
|
|
|
|
}
|
|
|
|
//printf("sq2 vol: %01X\n", ctx.sq2_volume);
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
if(ctx.ch4_env_pace != 0){
|
|
|
|
ctx.ch4_env_timer++;
|
|
|
|
if(ctx.ch4_env_timer >= ctx.ch4_env_pace) {
|
|
|
|
ctx.ch4_env_timer = 0;
|
|
|
|
if((ctx.ch4_env_direction && ctx.ch4_volume != 15) || (!ctx.ch4_env_direction && ctx.ch4_volume != 0)){
|
|
|
|
ctx.ch4_volume += ctx.ch4_env_direction ? 1 : -1;
|
|
|
|
if(ctx.ch4_volume < 0)
|
|
|
|
ctx.ch4_volume = 0;
|
|
|
|
if(ctx.ch4_volume > 15)
|
|
|
|
ctx.ch4_volume = 15;
|
|
|
|
}
|
|
|
|
//printf("sq2 vol: %01X\n", ctx.sq2_volume);
|
|
|
|
}
|
2025-02-02 22:56:07 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
|
|
|
if((prev_ticks & (1 << 2)) && !(ticks & (1 << 2))) {
|
|
|
|
ctx.sq1_sweep_timer++;
|
|
|
|
if(ctx.sq1_sweep_timer >= ctx.sq1_sweep_pace) {
|
|
|
|
ctx.sq1_sweep_timer = 0;
|
|
|
|
if(ctx.sq1_enable && ctx.sq1_sweep_pace){
|
|
|
|
sq1_sweep();
|
|
|
|
if(ctx.sq1_calc_period <= 0x7FFF && ctx.sq1_sweep_step != 0) {
|
|
|
|
ctx.sq1_sweep_period = ctx.sq1_calc_period;
|
|
|
|
ctx.sq1_period_reset = ctx.sq1_calc_period;
|
|
|
|
}
|
|
|
|
sq1_sweep();
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_square1() {
|
|
|
|
ctx.sq1_enable = true;
|
|
|
|
if(ctx.sq1_len >= 64) {
|
|
|
|
ctx.sq1_len = ctx.sq1_initial_len;
|
|
|
|
}
|
|
|
|
ctx.sq1_volume = ctx.sq1_initial_volume;
|
|
|
|
ctx.sq1_env_timer = 0;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq1_sweep_period = ctx.sq1_period_reset;
|
|
|
|
ctx.sq1_sweep_timer = 0;
|
|
|
|
ctx.sq1_env_direction = ctx.sq1_env_direction_buffer;
|
|
|
|
ctx.sq1_env_pace = ctx.sq1_env_pace_buffer;
|
|
|
|
ctx.sq1_sweep_enabled = (ctx.sq1_sweep_pace || ctx.sq1_sweep_step);
|
|
|
|
if(ctx.sq1_sweep_step) {
|
|
|
|
sq1_sweep();
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void enable_square2() {
|
2025-02-02 22:56:07 -07:00
|
|
|
ctx.sq2_enable = true;
|
2025-02-02 20:36:32 -07:00
|
|
|
if(ctx.sq2_len >= 64) {
|
|
|
|
ctx.sq2_len = ctx.sq2_initial_len;
|
|
|
|
}
|
|
|
|
ctx.sq2_volume = ctx.sq2_initial_volume;
|
|
|
|
ctx.sq2_env_timer = 0;
|
2025-02-02 22:56:07 -07:00
|
|
|
ctx.sq2_sample = 0;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq2_period_timer = ctx.sq2_period_reset;
|
|
|
|
ctx.sq2_env_direction = ctx.sq2_env_direction_buffer;
|
|
|
|
ctx.sq2_env_pace = ctx.sq2_env_pace_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_wave() {
|
|
|
|
ctx.ch3_enable = true;
|
2025-02-03 22:59:24 -07:00
|
|
|
if(ctx.ch3_len >= 256) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.ch3_len = ctx.ch3_initial_len;
|
|
|
|
}
|
|
|
|
ctx.ch3_sample = 0;
|
|
|
|
ctx.ch3_volume = ctx.ch3_initial_volume;
|
|
|
|
ctx.ch3_period_timer = ctx.ch3_period_reset;
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
|
2025-02-03 16:25:18 -07:00
|
|
|
void enable_noise() {
|
|
|
|
ctx.ch4_enable = true;
|
|
|
|
if(ctx.ch4_len >= 64) {
|
|
|
|
ctx.ch4_len = ctx.ch4_initial_len;
|
|
|
|
}
|
|
|
|
ctx.ch4_env_timer = 0;
|
|
|
|
ctx.ch4_env_direction = ctx.ch4_env_direction_buffer;
|
|
|
|
ctx.ch4_env_pace = ctx.ch4_env_pace_buffer;
|
|
|
|
ctx.ch4_volume = ctx.ch4_initial_volume;
|
|
|
|
ctx.ch4_lfsr = 0;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 audio_read(u16 address) {
|
2025-02-03 22:59:24 -07:00
|
|
|
printf("read at %02X\n", address);
|
|
|
|
if(address == 0xFF26) {
|
|
|
|
u8 value = (ctx.audio_enabled << 7) | (ctx.ch4_enable << 3) | (ctx.ch3_enable << 2) | (ctx.sq2_enable << 1) | (ctx.sq1_enable);
|
|
|
|
return value | 0b01110000;
|
|
|
|
}
|
|
|
|
if(address == 0xFF25) {
|
|
|
|
u8 value;
|
|
|
|
value |= ctx.ch4_left << 7;
|
|
|
|
value |= ctx.ch3_left << 6;
|
|
|
|
value |= ctx.ch2_left << 5;
|
|
|
|
value |= ctx.ch1_left << 4;
|
|
|
|
|
|
|
|
value |= ctx.ch4_right << 3;
|
|
|
|
value |= ctx.ch3_right << 2;
|
|
|
|
value |= ctx.ch2_right << 1;
|
|
|
|
value |= ctx.ch1_right;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF24) {
|
|
|
|
u8 value = (ctx.volume_left & 0b111) << 4;
|
|
|
|
value |= (ctx.volume_right & 0b111);
|
|
|
|
value |= (ctx.vin_left << 7);
|
|
|
|
value |= (ctx.vin_right << 3);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF10) {
|
|
|
|
u8 value = (ctx.sq1_sweep_pace & 0b111) << 4;
|
|
|
|
value |= (ctx.sq1_sweep_direction) << 3;
|
|
|
|
value |= ctx.sq1_sweep_step & 0b111;
|
|
|
|
return value | 0x80;
|
|
|
|
}
|
|
|
|
if(address == 0xFF11) {
|
|
|
|
u8 value = (ctx.sq1_duty & 0b11) << 6;
|
|
|
|
return value | 0b00111111;
|
|
|
|
}
|
|
|
|
if(address == 0xFF12) {
|
|
|
|
u8 value = (ctx.sq1_initial_volume & 0b1111) << 4;
|
|
|
|
value |= ctx.sq1_env_direction_buffer << 3;
|
|
|
|
value |= (ctx.sq1_env_pace_buffer & 0b111);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if(address == 0xFF13) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
if(address == 0xFF14) {
|
|
|
|
return (ctx.sq1_len_enable << 6) | 0b10111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(address == 0xFF16) {
|
|
|
|
u8 value = (ctx.sq2_duty & 0b11) << 6;
|
|
|
|
return value | 0b00111111;
|
|
|
|
}
|
|
|
|
if(address == 0xFF17) {
|
|
|
|
u8 value = (ctx.sq2_initial_volume & 0b1111) << 4;
|
|
|
|
value |= ctx.sq2_env_direction_buffer << 3;
|
|
|
|
value |= (ctx.sq2_env_pace_buffer & 0b111);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if(address == 0xFF18) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
if(address == 0xFF19) {
|
|
|
|
return (ctx.sq2_len_enable << 6) | 0b10111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1A) {
|
|
|
|
return (ctx.ch3_dac << 7) | 0b01111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1B) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1C) {
|
|
|
|
return ((ctx.ch3_initial_volume & 0b11) << 5) | 0b10011111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1D) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1E) {
|
|
|
|
return (ctx.ch3_len_enable << 6) | 0b10111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF20) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF21) {
|
|
|
|
u8 value = (ctx.ch4_initial_volume & 0b1111) << 4;
|
|
|
|
value |= ctx.ch4_env_direction_buffer << 3;
|
|
|
|
value |= (ctx.ch4_env_pace_buffer & 0b111);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF22) {
|
|
|
|
u8 value = (ctx.ch4_clock_shift & 0xF) << 4;
|
|
|
|
value |= ctx.ch4_lfsr_width << 3;
|
|
|
|
value |= (ctx.ch4_clock_divider & 0b111);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF23) {
|
|
|
|
return (ctx.ch4_len_enable << 6) | 0b10111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(BETWEEN(address, 0xFF30, 0xFF3F)) {
|
|
|
|
return ctx.wave_ram[address - 0xFF30];
|
|
|
|
}
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_buffers(){
|
|
|
|
ctx.ch1_left = 0;
|
|
|
|
ctx.ch1_right = 0;
|
|
|
|
ctx.ch2_left = 0;
|
|
|
|
ctx.ch2_right = 0;
|
|
|
|
ctx.ch3_left = 0;
|
|
|
|
ctx.ch3_right = 0;
|
|
|
|
ctx.ch4_left = 0;
|
|
|
|
ctx.ch4_right = 0;
|
|
|
|
ctx.volume_left = 0;
|
|
|
|
ctx.volume_right = 0;
|
|
|
|
ctx.vin_left = 0;
|
|
|
|
ctx.vin_right = 0;
|
|
|
|
ctx.sq1_duty = 0;
|
|
|
|
ctx.sq1_volume = 0;
|
|
|
|
ctx.sq1_sample = 0;
|
|
|
|
ctx.sq1_period_reset = 0;
|
|
|
|
ctx.sq1_period_timer = 0;
|
|
|
|
ctx.sq1_enable = 0;
|
|
|
|
ctx.sq1_len_enable = 0;
|
|
|
|
ctx.sq1_sweep_pace = 0;
|
|
|
|
ctx.sq1_sweep_direction = 0;
|
|
|
|
ctx.sq1_initial_len = 0;
|
|
|
|
ctx.sq1_len = 0;
|
|
|
|
ctx.sq1_initial_volume = 0;
|
|
|
|
ctx.sq1_env_direction = 0;
|
|
|
|
ctx.sq1_env_direction_buffer = 0;
|
|
|
|
ctx.sq1_env_pace = 0;
|
|
|
|
ctx.sq1_env_pace_buffer = 0;
|
|
|
|
ctx.sq1_env_timer = 0;
|
|
|
|
ctx.sq1_sweep_step = 0;
|
|
|
|
ctx.sq1_sweep_timer = 0;
|
|
|
|
ctx.sq1_sweep_enabled = 0;
|
|
|
|
ctx.sq1_sweep_period = 0;
|
|
|
|
ctx.sq1_calc_period = 0;
|
|
|
|
ctx.sq2_duty = 0;
|
|
|
|
ctx.sq2_volume = 0;
|
|
|
|
ctx.sq2_sample = 0;
|
|
|
|
ctx.sq2_period_reset = 0;
|
|
|
|
ctx.sq2_period_timer = 0;
|
|
|
|
ctx.sq2_enable = 0;
|
|
|
|
ctx.sq2_len_enable = 0;
|
|
|
|
ctx.sq2_initial_len = 0;
|
|
|
|
ctx.sq2_len = 0;
|
|
|
|
ctx.sq2_initial_volume = 0;
|
|
|
|
ctx.sq2_env_direction = 0;
|
|
|
|
ctx.sq2_env_direction_buffer = 0;
|
|
|
|
ctx.sq2_env_pace = 0;
|
|
|
|
ctx.sq2_env_pace_buffer = 0;
|
|
|
|
ctx.sq2_env_timer = 0;
|
|
|
|
ctx.ch3_enable = 0;
|
|
|
|
ctx.ch3_dac = 0;
|
|
|
|
ctx.ch3_initial_len = 0;
|
|
|
|
ctx.ch3_len = 0;
|
|
|
|
ctx.ch3_volume = 0;
|
|
|
|
ctx.ch3_initial_volume = 0;
|
|
|
|
ctx.ch3_period_timer = 0;
|
|
|
|
ctx.ch3_period_reset = 0;
|
|
|
|
ctx.ch3_len_enable = 0;
|
|
|
|
ctx.ch3_last_sample = 0;
|
|
|
|
ctx.ch3_sample = 0;
|
|
|
|
ctx.ch4_enable = 0;
|
|
|
|
ctx.ch4_initial_len = 0;
|
|
|
|
ctx.ch4_len = 0;
|
|
|
|
ctx.ch4_initial_volume = 0;
|
|
|
|
ctx.ch4_volume = 0;
|
|
|
|
ctx.ch4_env_pace = 0;
|
|
|
|
ctx.ch4_env_pace_buffer = 0;
|
|
|
|
ctx.ch4_env_timer = 0;
|
|
|
|
ctx.ch4_env_direction = 0;
|
|
|
|
ctx.ch4_env_direction_buffer = 0;
|
|
|
|
ctx.ch4_clock_shift = 0;
|
|
|
|
ctx.ch4_lfsr_width = 0;
|
|
|
|
ctx.ch4_clock_divider = 0;
|
|
|
|
ctx.ch4_len_enable = 0;
|
|
|
|
ctx.ch4_lfsr = 0;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void audio_write(u16 address, u8 value){
|
2025-02-03 22:59:24 -07:00
|
|
|
if(BETWEEN(address, 0xFF30, 0xFF3F)) {
|
|
|
|
ctx.wave_ram[address - 0xFF30] = value;
|
|
|
|
}
|
|
|
|
|
2025-02-02 20:36:32 -07:00
|
|
|
if(address == 0xFF26) {
|
|
|
|
ctx.audio_enabled = value & 0x80;
|
2025-02-03 22:59:24 -07:00
|
|
|
if(!ctx.audio_enabled) {
|
|
|
|
reset_buffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ctx.audio_enabled){
|
|
|
|
return;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
2025-02-03 22:59:24 -07:00
|
|
|
|
2025-02-02 20:36:32 -07:00
|
|
|
if(address == 0xFF25) {
|
|
|
|
ctx.ch4_left = value & 0b10000000;
|
|
|
|
ctx.ch3_left = value & 0b01000000;
|
|
|
|
ctx.ch2_left = value & 0b00100000;
|
|
|
|
ctx.ch1_left = value & 0b00010000;
|
|
|
|
|
|
|
|
ctx.ch4_right = value & 0b00001000;
|
|
|
|
ctx.ch3_right = value & 0b00000100;
|
|
|
|
ctx.ch2_right = value & 0b00000010;
|
|
|
|
ctx.ch1_right = value & 0b00000001;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF24) {
|
|
|
|
ctx.volume_left = (value >> 4) & 0b111;
|
2025-02-03 22:59:24 -07:00
|
|
|
//if(ctx.volume_left == 0) ctx.volume_left = 1;
|
2025-02-02 20:36:32 -07:00
|
|
|
ctx.volume_right = value & 0b111;
|
2025-02-03 22:59:24 -07:00
|
|
|
//if(ctx.volume_right == 0) ctx.volume_right = 1;
|
|
|
|
ctx.vin_left = value & 0x80;
|
|
|
|
ctx.vin_right = value & 0x8;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF10) {
|
|
|
|
ctx.sq1_sweep_pace = (value >> 4) & 0b111;
|
|
|
|
ctx.sq1_sweep_direction = value & 0x08;
|
|
|
|
ctx.sq1_sweep_step = value & 0b111;
|
|
|
|
}
|
|
|
|
if(address == 0xFF11) {
|
|
|
|
ctx.sq1_duty = value >> 6;
|
|
|
|
ctx.sq1_initial_len = value & 0x3F;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.sq1_len = ctx.sq1_initial_len;
|
|
|
|
printf("initial len: %02X\n", ctx.sq1_initial_len);
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF12) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq1_initial_volume = (value >> 4) & 0x0F;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.sq1_env_direction_buffer = (value & 0x8) == 0x8;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq1_env_pace_buffer = value & 0b111;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF13) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq1_period_reset = (ctx.sq1_period_reset & 0xF00) | value;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF14) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq1_period_reset = (ctx.sq1_period_reset & 0x0FF) | ((value & 0b111) << 8);
|
|
|
|
ctx.sq1_len_enable = (value & 0x40) == 0x40;
|
2025-02-02 20:36:32 -07:00
|
|
|
if(value & 0x80) {
|
|
|
|
enable_square1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(address == 0xFF16) {
|
|
|
|
ctx.sq2_duty = value >> 6;
|
|
|
|
ctx.sq2_initial_len = value & 0x3F;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.sq2_len = ctx.sq2_initial_len;
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF17) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq2_initial_volume = (value >> 4) & 0x0F;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.sq2_env_direction_buffer = (value & 0x8) == 0x8;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq2_env_pace_buffer = value & 0b111;
|
|
|
|
if(ctx.sq2_env_direction == 0 && ctx.sq2_initial_volume == 0) {
|
2025-02-02 22:56:07 -07:00
|
|
|
ctx.sq2_enable = false;
|
|
|
|
}
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF18) {
|
2025-02-03 16:25:18 -07:00
|
|
|
u16 prev_period = ctx.sq2_period_reset;
|
|
|
|
ctx.sq2_period_reset = (ctx.sq2_period_reset & 0xF00) | value;
|
|
|
|
//printf("period: %03X, old_period: %03X\n", ctx.sq2_period_reset, prev_period);
|
2025-02-02 20:36:32 -07:00
|
|
|
}
|
|
|
|
if(address == 0xFF19) {
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.sq2_period_reset = (ctx.sq2_period_reset & 0x0FF) | ((value & 0b111) << 8);
|
|
|
|
ctx.sq2_len_enable = (value & 0x40) == 0x40;
|
|
|
|
if((value & 0x80) == 0x80) {
|
2025-02-02 20:36:32 -07:00
|
|
|
enable_square2();
|
|
|
|
}
|
2025-02-02 12:35:53 -07:00
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
|
|
|
|
if(address == 0xFF1A) {
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.ch3_dac = value & 0x80;
|
|
|
|
if(!ctx.ch3_dac){
|
|
|
|
ctx.ch3_enable = false;
|
|
|
|
}
|
2025-02-03 16:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1B) {
|
|
|
|
ctx.ch3_initial_len = value;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.ch3_len = ctx.ch3_initial_len;
|
2025-02-03 16:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1C) {
|
|
|
|
ctx.ch3_initial_volume = (value >> 5) & 0b11;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1D) {
|
|
|
|
ctx.ch3_period_reset = (ctx.ch3_period_reset & 0xF00) | value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF1E) {
|
|
|
|
ctx.ch3_period_reset = (ctx.ch3_period_reset & 0x0FF) | ((value & 0b111) << 8);
|
|
|
|
ctx.ch3_len_enable = (value & 0x40) == 0x40;
|
|
|
|
if(value & 0x80) {
|
|
|
|
enable_wave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF20) {
|
|
|
|
ctx.ch4_initial_len = value & 0b111111;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.ch4_len = ctx.ch4_initial_len;
|
2025-02-03 16:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF21) {
|
|
|
|
ctx.ch4_initial_volume = (value >> 4) & 0x0F;
|
2025-02-03 22:59:24 -07:00
|
|
|
ctx.ch4_env_direction_buffer = (value & 0x8) == 0x8;
|
2025-02-03 16:25:18 -07:00
|
|
|
ctx.ch4_env_pace_buffer = value & 0b111;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF22) {
|
|
|
|
ctx.ch4_clock_shift = (value >> 4);
|
|
|
|
ctx.ch4_lfsr_width = (value & 0x08);
|
|
|
|
ctx.ch4_clock_divider = value & 0b111;
|
|
|
|
float div = (ctx.ch4_clock_divider == 0 ? 0.5f : ctx.ch4_clock_divider);
|
|
|
|
float lfsr_rate = (LFSR_BASE_CLOCK) / div * (1 << ctx.ch4_clock_shift);
|
|
|
|
lfsr_clock = 1.0f / (lfsr_rate/1000.0f/1000.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(address == 0xFF23) {
|
|
|
|
ctx.ch4_len_enable = (value & 0x40) == 0x40;
|
|
|
|
if((value & 0x80) == 0x80) {
|
|
|
|
enable_noise();
|
|
|
|
}
|
|
|
|
}
|
2025-02-01 23:58:55 -07:00
|
|
|
}
|