Skip to content

Commit

Permalink
APU modularised
Browse files Browse the repository at this point in the history
  • Loading branch information
v1bh475u committed Aug 15, 2024
1 parent 4e4bdd5 commit e077f31
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ build/
cmake-build-debug/
.idea/
src/.vscode/
src/dmg_boot.gb
src/dmg_boot.gb
tests/*
.vscode/
9 changes: 8 additions & 1 deletion src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ APU::APU()
enableVINRight = false;
volumeLeft = 0;
volumeRight = 0;

mMap = nullptr;
channel1 = new PulseChannel(CH1);
channel2 = new PulseChannel(CH2);
channel3 = new WaveChannel();
Expand Down Expand Up @@ -47,6 +47,7 @@ bool APU::init()
channel2->setFrameSequencer(frameSequencer);
channel3->setFrameSequencer(frameSequencer);
channel4->setFrameSequencer(frameSequencer);

return true;
}

Expand Down Expand Up @@ -170,6 +171,12 @@ Byte APU::readByte(Word address)

void APU::stepAPU(int cycles)
{
// Audio checker
Byte flag = mMap->getAudioWriteFlag();
Word address = 0;
if (flag)
address = mMap->getAudioWriteAddress();

sampleCounter += cycles;
frameSequencerCounter += cycles;

Expand Down
56 changes: 30 additions & 26 deletions src/audio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "types.h"
#include "mmap.h"
#include <stdio.h>
#include <SDL.h>

Expand All @@ -15,9 +16,9 @@ class PulseChannel
{
private:
Channel channel;
bool enabled;
bool dacEnabled;
int frameSequencer;
bool enabled;
bool dacEnabled;
int frameSequencer;

Byte sweepPeriod;
bool sweepNegate;
Expand All @@ -26,7 +27,7 @@ class PulseChannel
// NRx1
Byte waveDuty;
int lengthTimer;
int maxLengthTimer = 64;
int maxLengthTimer = 64;

Byte envelopeInitialVolume;
bool envelopeIncrease;
Expand All @@ -41,25 +42,24 @@ class PulseChannel
void test();
void writeByte(Word address, Byte value);
Byte readByte(Word address);
bool isEnabled();
void powerOff();
void run();
void set_NRx4(Byte value);
void setFrameSequencer(int frameSequencer);
void trigger();
bool isEnabled();
void powerOff();
void run();
void set_NRx4(Byte value);
void setFrameSequencer(int frameSequencer);
void trigger();
};

class WaveChannel
{
private:

Byte waveRAM[16];
bool dacEnabled;
bool enabled;

int lengthTimer;
int maxLengthTimer = 256;
int frameSequencer;
int frameSequencer;

Byte outputLevel;

Expand All @@ -73,22 +73,22 @@ class WaveChannel
void writeByte(Word address, Byte value);
Byte readByte(Word address);
void trigger();
bool isEnabled();
void powerOff();
void set_NRx4(Byte value);
void run();
void setFrameSequencer(int frameSequencer);
bool isEnabled();
void powerOff();
void set_NRx4(Byte value);
void run();
void setFrameSequencer(int frameSequencer);
};

class NoiseChannel
{
private:
bool enabled;
bool dacEnabled;
bool enabled;
bool dacEnabled;

int lengthTimer;
int maxLengthTimer = 64;
int frameSequencer;
int frameSequencer;

Byte envelopeInitialVolume;
bool envelopeIncrease;
Expand All @@ -112,11 +112,11 @@ class NoiseChannel
void writeByte(Word address, Byte value);
Byte readByte(Word address);
void trigger();
bool isEnabled();
void powerOff();
void set_NRx4(Byte value);
void run();
void setFrameSequencer(int frameSequencer);
bool isEnabled();
void powerOff();
void set_NRx4(Byte value);
void run();
void setFrameSequencer(int frameSequencer);
};

class APU
Expand Down Expand Up @@ -160,12 +160,16 @@ class APU
WaveChannel* channel3;
NoiseChannel* channel4;

// Memory Map
MemoryMap* mMap;

public:
APU();
void test();
bool init();
bool init();
void writeByte(Word address, Byte value);
Byte readByte(Word address);
void stepAPU(int cycles);
void clearRegisters();
void setMemoryMap(MemoryMap* map) { mMap = map; }
};
Loading

0 comments on commit e077f31

Please sign in to comment.