Skip to content

Commit

Permalink
Merge pull request #424 from ogamespec/main
Browse files Browse the repository at this point in the history
IIR
  • Loading branch information
ogamespec authored May 24, 2023
2 parents a50fe3d + 6321ea3 commit 8d6c737
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Breaknes/Breaknes/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void loadROMDumpToolStripMenuItem_Click(object sender, EventArgs e)
var rom_name = Path.GetFileNameWithoutExtension(filename);
vid_out = new(OnRenderField, settings.DumpVideo, settings.DumpVideoDir, rom_name);
vid_out.SetOutputPictureBox(pictureBox1);
snd_out = new(Handle, settings.DumpAudio, settings.DumpAudioDir, rom_name);
snd_out = new(Handle, settings.DumpAudio, settings.DumpAudioDir, rom_name, settings.IIR, settings.CutoffFrequency);
board.Paused = debuggers.Count != 0;

foreach (var inst in debuggers)
Expand Down
12 changes: 12 additions & 0 deletions Breaknes/Breaknes/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static BreaknesSettings SetDefaultSettings()
settings.DumpVideo = false;
settings.DumpVideoDir = "";
settings.AllocConsole = false;
settings.IIR = true;
settings.CutoffFrequency = 12000;

SaveSettings(settings);

Expand Down Expand Up @@ -122,6 +124,16 @@ public class BreaknesSettings
[Description("Turn on the debug console. Requires a restart.")]
[DefaultValue(false)]
public bool AllocConsole { get; set; }

[XmlElement]
[Category("Sound Features")]
[Description("Implementation of a simple IIR filter, in addition to decimation")]
public bool IIR { get; set; }

[XmlElement]
[Category("Sound Features")]
[Description("Frequency for IIR filter (try 12000 for example)")]
public int CutoffFrequency { get; set; }
}

// https://stackoverflow.com/questions/24503462/how-to-show-drop-down-control-in-property-grid
Expand Down
29 changes: 27 additions & 2 deletions Breaknes/Breaknes/SoundProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ public class AudioRender
private string dump_audio_dir = "";
private string dump_rom_name;

public AudioRender(System.IntPtr handle, bool dump, string dump_dir, string rom_name)
private SimpleIIR filter = new SimpleIIR();
private bool iir = true;

public AudioRender(System.IntPtr handle, bool dump, string dump_dir, string rom_name, bool use_iir, int cutoff_freq)
{
dump_audio = dump;
dump_audio_dir = dump_dir;
dump_rom_name = rom_name;

audio_backend = new DSound(handle);

BreaksCore.GetApuSignalFeatures(out aux_features);

iir = use_iir;
if (iir)
{
filter.Reconfigure(aux_features.SampleRate, cutoff_freq);
}

// SRC
Redecimate();
}
Expand All @@ -43,10 +54,24 @@ public AudioRender(System.IntPtr handle, bool dump, string dump_dir, string rom_
/// </summary>
public void FeedSample()
{
if (DecimateCounter >= DecimateEach)
if (iir)
{
float sample;
BreaksCore.SampleAudioSignal(out sample);
filter.Input(sample);
}

if (DecimateCounter >= DecimateEach)
{
float sample;
if (iir)
{
sample = filter.Output();
}
else
{
BreaksCore.SampleAudioSignal(out sample);
}
SampleBuf.Add(sample);
DecimateCounter = 0;

Expand Down
2 changes: 1 addition & 1 deletion BreaksAPU/APUPlayer/FormAbout.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions BreaksAPU/APUPlayer/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public partial class FormMain : Form
private bool auxdump_loaded = false;
private bool Paused = true; // atomic
private bool Dma = false; // atomic
private bool InitRequired = false; // atmoic

// Stats
private long timeStamp;
Expand All @@ -34,11 +33,13 @@ public partial class FormMain : Form
private BreaksCore.AudioSignalFeatures aux_features;
private int DecimateEach = 1;
private int DecimateCounter = 0;
private long AclkToPlay = 0;

private float[] aux_dump = Array.Empty<float>();
private long aux_dump_pointer = 0;

private SimpleIIR filter = new SimpleIIR();
private bool iir = true;

public FormMain()
{
InitializeComponent();
Expand Down Expand Up @@ -193,6 +194,14 @@ private void InitBoardForRegDump(string regdump_filename)

UpdateMemLayout();

// Filter
iir = settings.IIR;
if (iir)
{
BreaksCore.GetApuSignalFeatures(out aux_features);
filter.Reconfigure(aux_features.SampleRate, settings.CutoffFrequency);
}

// SRC
Redecimate();

Expand Down Expand Up @@ -297,6 +306,13 @@ private void UpdateSignalPlot()
/// </summary>
private void FeedSample()
{
if (iir)
{
float sample;
BreaksCore.SampleAudioSignal(out sample);
filter.Input(sample);
}

if (DecimateCounter >= DecimateEach)
{
float sample;
Expand All @@ -318,7 +334,14 @@ private void FeedSample()
}
else
{
BreaksCore.SampleAudioSignal(out sample);
if (iir)
{
sample = filter.Output();
}
else
{
BreaksCore.SampleAudioSignal(out sample);
}
}

SampleBuf.Add(sample);
Expand Down
12 changes: 12 additions & 0 deletions BreaksAPU/APUPlayer/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static APUPlayerSettings SetDefaultSettings()
settings.DC = 0.0f;
settings.Visual2A03Mapping = false;
settings.AllocConsole = false;
settings.IIR = true;
settings.CutoffFrequency = 12000;

SaveSettings(settings);

Expand Down Expand Up @@ -137,6 +139,16 @@ public class APUPlayerSettings
[Category("Debug")]
[Description("Open the console for debug output.")]
public bool AllocConsole { get; set; }

[XmlElement]
[Category("Host Features")]
[Description("Implementation of a simple IIR filter, in addition to decimation")]
public bool IIR { get; set; }

[XmlElement]
[Category("Host Features")]
[Description("Frequency for IIR filter (try 12000 for example)")]
public int CutoffFrequency { get; set; }
}

// https://stackoverflow.com/questions/24503462/how-to-show-drop-down-control-in-property-grid
Expand Down
20 changes: 15 additions & 5 deletions BreaksAPU/FastAPU/clkgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ namespace FastAPU
{
CLKGen::CLKGen(FastAPU* parent)
{

apu = parent;
}

CLKGen::~CLKGen()
{

}

void CLKGen::sim()
{

sim_ACLK();
sim_SoftCLK_Mode();
sim_SoftCLK_PLA();
sim_SoftCLK_Control();
sim_SoftCLK_LFSR();
}

int CLKGen::GetINTFF()
{

return int_ff;
}

void CLKGen::sim_ACLK()
Expand All @@ -31,7 +34,14 @@ namespace FastAPU

void CLKGen::sim_SoftCLK_Mode()
{

if (apu->RegOps.W4017 && !apu->wire.ACLK1) {
reg_mode = apu->GetDBBit(7);
}
n_mode = !reg_mode;
if (apu->wire.ACLK1) {
md_latch = n_mode;
}
mode = !md_latch;
}

void CLKGen::sim_SoftCLK_Control()
Expand Down
33 changes: 31 additions & 2 deletions BreaksAPU/FastAPU/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,50 @@

#include "pch.h"

using namespace BaseLogic;

namespace FastAPU
{
CoreBinding::CoreBinding(FastAPU* parent)
{

apu = parent;
}

CoreBinding::~CoreBinding()
{

}

void CoreBinding::sim()
{
sim_DividerBeforeCore();

// The 6502 core uses TriState from BaseLogicLib, so you have to convert the values

if (apu->wire.PHI0 != apu->PrevPHI_Core)
{
TriState inputs[(size_t)M6502Core::InputPad::Max]{};
TriState outputs[(size_t)M6502Core::OutputPad::Max];

auto IRQINT = (!apu->wire.n_IRQ || apu->wire.INT);

inputs[(size_t)M6502Core::InputPad::n_NMI] = apu->wire.n_NMI ? TriState::One : TriState::Zero;
inputs[(size_t)M6502Core::InputPad::n_IRQ] = IRQINT ? TriState::Zero : TriState::One;
inputs[(size_t)M6502Core::InputPad::n_RES] = apu->wire.RES ? TriState::Zero : TriState::One;
inputs[(size_t)M6502Core::InputPad::PHI0] = apu->wire.PHI0 ? TriState::One : TriState::Zero;
inputs[(size_t)M6502Core::InputPad::RDY] = (apu->wire.RDY && apu->wire.RDY2) ? TriState::One : TriState::Zero;
inputs[(size_t)M6502Core::InputPad::SO] = TriState::One;

apu->core->sim(inputs, outputs, &apu->CPU_Addr, &apu->DB);

apu->wire.PHI1 = outputs[(size_t)M6502Core::OutputPad::PHI1] == TriState::One ? 1 : 0;
apu->wire.PHI2 = outputs[(size_t)M6502Core::OutputPad::PHI2] == TriState::One ? 1 : 0;
apu->wire.RnW = outputs[(size_t)M6502Core::OutputPad::RnW] == TriState::One ? 1 : 0;
apu->wire.SYNC = outputs[(size_t)M6502Core::OutputPad::SYNC] == TriState::One ? 1 : 0;

apu->PrevPHI_Core = apu->wire.PHI0;
}

sim_DividerAfterCore();
}

void CoreBinding::sim_DividerBeforeCore()
Expand Down
6 changes: 3 additions & 3 deletions BreaksAPU/FastAPU/dpcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ namespace FastAPU

int DpcmChan::get_CTRL1()
{

return 0;
}

int DpcmChan::get_CTRL2()
{

return 0;
}

int DpcmChan::get_DMC1()
{

return 0;
}
}
2 changes: 1 addition & 1 deletion BreaksAPU/FastAPU/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ namespace FastAPU

int EnvelopeUnit::get_LC()
{

return 0;
}
}
27 changes: 27 additions & 0 deletions Common/SharpTools/IIR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Thanks to @zeta0134

public class SimpleIIR
{
float prev_output = 0;
float delta = 0;
float alpha;

public void Reconfigure (float SampleRate, float cutoff_freq)
{
delta = 1.0f / SampleRate;
float time_const = (float)(1.0 / (2.0 * Math.PI * cutoff_freq));
alpha = delta / (time_const + delta);
Console.WriteLine("IIR Reconfigured delta: {0}, alpha: {0}", delta, alpha);
}

public void Input (float sample)
{
prev_output = Output();
delta = sample - prev_output;
}

public float Output ()
{
return prev_output + alpha * delta;
}
}

0 comments on commit 8d6c737

Please sign in to comment.