gzdoom-gles/libraries/game-music-emu/gme/Gb_Apu.cpp
Christoph Oelckers 040cb17370 - moved third party library projects to a subfolder to reduce size of the root.
# Conflicts:
#	CMakeLists.txt
#	libraries/glslang/OGLCompilersDLL/CMakeLists.txt
#	libraries/glslang/OGLCompilersDLL/InitializeDll.cpp
#	libraries/glslang/OGLCompilersDLL/InitializeDll.h
#	libraries/glslang/glslang/CMakeLists.txt
#	libraries/glslang/glslang/GenericCodeGen/CodeGen.cpp
#	libraries/glslang/glslang/GenericCodeGen/Link.cpp
#	libraries/glslang/glslang/Include/BaseTypes.h
#	libraries/glslang/glslang/Include/Common.h
#	libraries/glslang/glslang/Include/ConstantUnion.h
#	libraries/glslang/glslang/Include/InfoSink.h
#	libraries/glslang/glslang/Include/InitializeGlobals.h
#	libraries/glslang/glslang/Include/PoolAlloc.h
#	libraries/glslang/glslang/Include/ResourceLimits.h
#	libraries/glslang/glslang/Include/ShHandle.h
#	libraries/glslang/glslang/Include/Types.h
#	libraries/glslang/glslang/Include/arrays.h
#	libraries/glslang/glslang/Include/intermediate.h
#	libraries/glslang/glslang/Include/revision.h
#	libraries/glslang/glslang/Include/revision.template
#	libraries/glslang/glslang/MachineIndependent/Constant.cpp
#	libraries/glslang/glslang/MachineIndependent/InfoSink.cpp
#	libraries/glslang/glslang/MachineIndependent/Initialize.cpp
#	libraries/glslang/glslang/MachineIndependent/Initialize.h
#	libraries/glslang/glslang/MachineIndependent/IntermTraverse.cpp
#	libraries/glslang/glslang/MachineIndependent/Intermediate.cpp
#	libraries/glslang/glslang/MachineIndependent/LiveTraverser.h
#	libraries/glslang/glslang/MachineIndependent/ParseContextBase.cpp
#	libraries/glslang/glslang/MachineIndependent/ParseHelper.cpp
#	libraries/glslang/glslang/MachineIndependent/ParseHelper.h
#	libraries/glslang/glslang/MachineIndependent/PoolAlloc.cpp
#	libraries/glslang/glslang/MachineIndependent/RemoveTree.cpp
#	libraries/glslang/glslang/MachineIndependent/RemoveTree.h
#	libraries/glslang/glslang/MachineIndependent/Scan.cpp
#	libraries/glslang/glslang/MachineIndependent/Scan.h
#	libraries/glslang/glslang/MachineIndependent/ScanContext.h
#	libraries/glslang/glslang/MachineIndependent/ShaderLang.cpp
#	libraries/glslang/glslang/MachineIndependent/SymbolTable.cpp
#	libraries/glslang/glslang/MachineIndependent/SymbolTable.h
#	libraries/glslang/glslang/MachineIndependent/Versions.cpp
#	libraries/glslang/glslang/MachineIndependent/Versions.h
#	libraries/glslang/glslang/MachineIndependent/attribute.cpp
#	libraries/glslang/glslang/MachineIndependent/attribute.h
#	libraries/glslang/glslang/MachineIndependent/gl_types.h
#	libraries/glslang/glslang/MachineIndependent/glslang.y
#	libraries/glslang/glslang/MachineIndependent/glslang_tab.cpp
#	libraries/glslang/glslang/MachineIndependent/glslang_tab.cpp.h
#	libraries/glslang/glslang/MachineIndependent/intermOut.cpp
#	libraries/glslang/glslang/MachineIndependent/iomapper.cpp
#	libraries/glslang/glslang/MachineIndependent/iomapper.h
#	libraries/glslang/glslang/MachineIndependent/limits.cpp
#	libraries/glslang/glslang/MachineIndependent/linkValidate.cpp
#	libraries/glslang/glslang/MachineIndependent/localintermediate.h
#	libraries/glslang/glslang/MachineIndependent/parseConst.cpp
#	libraries/glslang/glslang/MachineIndependent/parseVersions.h
#	libraries/glslang/glslang/MachineIndependent/pch.cpp
#	libraries/glslang/glslang/MachineIndependent/pch.h
#	libraries/glslang/glslang/MachineIndependent/preprocessor/Pp.cpp
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpAtom.cpp
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpContext.cpp
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpContext.h
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpScanner.cpp
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpTokens.cpp
#	libraries/glslang/glslang/MachineIndependent/preprocessor/PpTokens.h
#	libraries/glslang/glslang/MachineIndependent/propagateNoContraction.cpp
#	libraries/glslang/glslang/MachineIndependent/propagateNoContraction.h
#	libraries/glslang/glslang/MachineIndependent/reflection.cpp
#	libraries/glslang/glslang/MachineIndependent/reflection.h
#	libraries/glslang/glslang/OSDependent/Unix/CMakeLists.txt
#	libraries/glslang/glslang/OSDependent/Unix/ossource.cpp
#	libraries/glslang/glslang/OSDependent/Windows/CMakeLists.txt
#	libraries/glslang/glslang/OSDependent/Windows/main.cpp
#	libraries/glslang/glslang/OSDependent/Windows/ossource.cpp
#	libraries/glslang/glslang/OSDependent/osinclude.h
#	libraries/glslang/glslang/Public/ShaderLang.h
#	libraries/glslang/glslang/updateGrammar
#	libraries/glslang/spirv/CMakeLists.txt
#	libraries/glslang/spirv/GLSL.ext.AMD.h
#	libraries/glslang/spirv/GLSL.ext.EXT.h
#	libraries/glslang/spirv/GLSL.ext.KHR.h
#	libraries/glslang/spirv/GLSL.ext.NV.h
#	libraries/glslang/spirv/GLSL.std.450.h
#	libraries/glslang/spirv/GlslangToSpv.cpp
#	libraries/glslang/spirv/GlslangToSpv.h
#	libraries/glslang/spirv/InReadableOrder.cpp
#	libraries/glslang/spirv/Logger.cpp
#	libraries/glslang/spirv/Logger.h
#	libraries/glslang/spirv/SPVRemapper.cpp
#	libraries/glslang/spirv/SPVRemapper.h
#	libraries/glslang/spirv/SpvBuilder.cpp
#	libraries/glslang/spirv/SpvBuilder.h
#	libraries/glslang/spirv/SpvPostProcess.cpp
#	libraries/glslang/spirv/SpvTools.cpp
#	libraries/glslang/spirv/SpvTools.h
#	libraries/glslang/spirv/bitutils.h
#	libraries/glslang/spirv/disassemble.cpp
#	libraries/glslang/spirv/disassemble.h
#	libraries/glslang/spirv/doc.cpp
#	libraries/glslang/spirv/doc.h
#	libraries/glslang/spirv/hex_float.h
#	libraries/glslang/spirv/spirv.hpp
#	libraries/glslang/spirv/spvIR.h
#	src/CMakeLists.txt
2020-01-04 21:30:08 +01:00

306 lines
7.3 KiB
C++

// Gb_Snd_Emu 0.1.5. http://www.slack.net/~ant/
#include "Gb_Apu.h"
#include <string.h>
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include "blargg_source.h"
unsigned const vol_reg = 0xFF24;
unsigned const status_reg = 0xFF26;
Gb_Apu::Gb_Apu()
{
square1.synth = &square_synth;
square2.synth = &square_synth;
wave.synth = &other_synth;
noise.synth = &other_synth;
oscs [0] = &square1;
oscs [1] = &square2;
oscs [2] = &wave;
oscs [3] = &noise;
for ( int i = 0; i < osc_count; i++ )
{
Gb_Osc& osc = *oscs [i];
osc.regs = &regs [i * 5];
osc.output = 0;
osc.outputs [0] = 0;
osc.outputs [1] = 0;
osc.outputs [2] = 0;
osc.outputs [3] = 0;
}
set_tempo( 1.0 );
volume( 1.0 );
reset();
}
void Gb_Apu::treble_eq( const blip_eq_t& eq )
{
square_synth.treble_eq( eq );
other_synth.treble_eq( eq );
}
void Gb_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right )
{
require( (unsigned) index < osc_count );
require( (center && left && right) || (!center && !left && !right) );
Gb_Osc& osc = *oscs [index];
osc.outputs [1] = right;
osc.outputs [2] = left;
osc.outputs [3] = center;
osc.output = osc.outputs [osc.output_select];
}
void Gb_Apu::output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right )
{
for ( int i = 0; i < osc_count; i++ )
osc_output( i, center, left, right );
}
void Gb_Apu::update_volume()
{
// TODO: doesn't handle differing left/right global volume (support would
// require modification to all oscillator code)
int data = regs [vol_reg - start_addr];
double vol = (max( data & 7, data >> 4 & 7 ) + 1) * volume_unit;
square_synth.volume( vol );
other_synth.volume( vol );
}
static unsigned char const powerup_regs [0x20] = {
0x80,0x3F,0x00,0xFF,0xBF, // square 1
0xFF,0x3F,0x00,0xFF,0xBF, // square 2
0x7F,0xFF,0x9F,0xFF,0xBF, // wave
0xFF,0xFF,0x00,0x00,0xBF, // noise
0x00, // left/right enables
0x77, // master volume
0x80, // power
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
void Gb_Apu::set_tempo( double t )
{
frame_period = 4194304 / 256; // 256 Hz
if ( t != 1.0 )
frame_period = blip_time_t (frame_period / t);
}
void Gb_Apu::reset()
{
next_frame_time = 0;
last_time = 0;
frame_count = 0;
square1.reset();
square2.reset();
wave.reset();
noise.reset();
noise.bits = 1;
wave.wave_pos = 0;
// avoid click at beginning
regs [vol_reg - start_addr] = 0x77;
update_volume();
regs [status_reg - start_addr] = 0x01; // force power
write_register( 0, status_reg, 0x00 );
static unsigned char const initial_wave [] = {
0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C, // wave table
0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA
};
memcpy( wave.wave, initial_wave, sizeof initial_wave );
}
void Gb_Apu::run_until( blip_time_t end_time )
{
require( end_time >= last_time ); // end_time must not be before previous time
if ( end_time == last_time )
return;
while ( true )
{
blip_time_t time = next_frame_time;
if ( time > end_time )
time = end_time;
// run oscillators
for ( int i = 0; i < osc_count; ++i )
{
Gb_Osc& osc = *oscs [i];
if ( osc.output )
{
osc.output->set_modified(); // TODO: misses optimization opportunities?
int playing = false;
if ( osc.enabled && osc.volume &&
(!(osc.regs [4] & osc.len_enabled_mask) || osc.length) )
playing = -1;
switch ( i )
{
case 0: square1.run( last_time, time, playing ); break;
case 1: square2.run( last_time, time, playing ); break;
case 2: wave .run( last_time, time, playing ); break;
case 3: noise .run( last_time, time, playing ); break;
}
}
}
last_time = time;
if ( time == end_time )
break;
next_frame_time += frame_period;
// 256 Hz actions
square1.clock_length();
square2.clock_length();
wave.clock_length();
noise.clock_length();
frame_count = (frame_count + 1) & 3;
if ( frame_count == 0 )
{
// 64 Hz actions
square1.clock_envelope();
square2.clock_envelope();
noise.clock_envelope();
}
if ( frame_count & 1 )
square1.clock_sweep(); // 128 Hz action
}
}
void Gb_Apu::end_frame( blip_time_t end_time )
{
if ( end_time > last_time )
run_until( end_time );
assert( next_frame_time >= end_time );
next_frame_time -= end_time;
assert( last_time >= end_time );
last_time -= end_time;
}
void Gb_Apu::write_register( blip_time_t time, unsigned addr, int data )
{
require( (unsigned) data < 0x100 );
int reg = addr - start_addr;
if ( (unsigned) reg >= register_count )
return;
run_until( time );
int old_reg = regs [reg];
regs [reg] = data;
if ( addr < vol_reg )
{
write_osc( reg / 5, reg, data );
}
else if ( addr == vol_reg && data != old_reg ) // global volume
{
// return all oscs to 0
for ( int i = 0; i < osc_count; i++ )
{
Gb_Osc& osc = *oscs [i];
int amp = osc.last_amp;
osc.last_amp = 0;
if ( amp && osc.enabled && osc.output )
other_synth.offset( time, -amp, osc.output );
}
if ( wave.outputs [3] )
other_synth.offset( time, 30, wave.outputs [3] );
update_volume();
if ( wave.outputs [3] )
other_synth.offset( time, -30, wave.outputs [3] );
// oscs will update with new amplitude when next run
}
else if ( addr == 0xFF25 || addr == status_reg )
{
int mask = (regs [status_reg - start_addr] & 0x80) ? ~0 : 0;
int flags = regs [0xFF25 - start_addr] & mask;
// left/right assignments
for ( int i = 0; i < osc_count; i++ )
{
Gb_Osc& osc = *oscs [i];
osc.enabled &= mask;
int bits = flags >> i;
Blip_Buffer* old_output = osc.output;
osc.output_select = (bits >> 3 & 2) | (bits & 1);
osc.output = osc.outputs [osc.output_select];
if ( osc.output != old_output )
{
int amp = osc.last_amp;
osc.last_amp = 0;
if ( amp && old_output )
other_synth.offset( time, -amp, old_output );
}
}
if ( addr == status_reg && data != old_reg )
{
if ( !(data & 0x80) )
{
for ( unsigned i = 0; i < sizeof powerup_regs; i++ )
{
if ( i != status_reg - start_addr )
write_register( time, i + start_addr, powerup_regs [i] );
}
}
else
{
//debug_printf( "APU powered on\n" );
}
}
}
else if ( addr >= 0xFF30 )
{
int index = (addr & 0x0F) * 2;
wave.wave [index] = data >> 4;
wave.wave [index + 1] = data & 0x0F;
}
}
int Gb_Apu::read_register( blip_time_t time, unsigned addr )
{
run_until( time );
int index = addr - start_addr;
require( (unsigned) index < register_count );
int data = regs [index];
if ( addr == status_reg )
{
data = (data & 0x80) | 0x70;
for ( int i = 0; i < osc_count; i++ )
{
const Gb_Osc& osc = *oscs [i];
if ( osc.enabled && (osc.length || !(osc.regs [4] & osc.len_enabled_mask)) )
data |= 1 << i;
}
}
return data;
}