Skip to content

Commit

Permalink
Started new Apu implementation, added System->Speed menu, various ref…
Browse files Browse the repository at this point in the history
…actorings
  • Loading branch information
ferris committed May 17, 2013
1 parent 28a067f commit 994b194
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 42 deletions.
22 changes: 22 additions & 0 deletions Chip8/Apu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Apu.h"

Apu::Apu()
{
mutex = Mutex::Create();
}

Apu::~Apu()
{
delete mutex;
}

void Apu::SetAudioDriver(IAudioDriver *audioDriver)
{
}

void Apu::SetBeeping(bool beeping)
{
mutex->Lock();
this->beeping = beeping;
mutex->Unlock();
}
24 changes: 24 additions & 0 deletions Chip8/Apu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __CHIP8_APU_H__
#define __CHIP8_APU_H__

#include "../Common.h"

#include "../IAudioDriver.h"

class Apu
{
public:
Apu();
~Apu();

void SetAudioDriver(IAudioDriver *audioDriver);

void SetBeeping(bool beeping);

private:
IAudioDriver *audioDriver;
Mutex *mutex;
bool beeping;
};

#endif
42 changes: 12 additions & 30 deletions Chip8/Chip8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Chip8::Chip8()
rom = 0;
romSize = 0;
stack = new Stack<unsigned short>(16);
gpu = new Gpu();
speed = 20;
Reset();
}
Expand All @@ -37,7 +36,6 @@ Chip8::~Chip8()
delete [] ram;
if (rom) delete rom;
delete stack;
delete gpu;
}

void Chip8::Reset()
Expand All @@ -50,8 +48,8 @@ void Chip8::Reset()
for (int i = 0; i < 4096; i++) ram[i] = random.GetNextInt(256);
for (int i = 0; i < 16 * 5; i++) ram[i] = charMem[i];
for (int i = 0; i < 16; i++) inputs[i] = false;
gpu->Clear();
//apu.SetBeeping(false);
gpu.Clear();
apu.SetBeeping(false);
running = false;
waitingForKeypress = false;
}
Expand All @@ -69,7 +67,7 @@ void Chip8::Update()
if (timerSound > 0)
{
timerSound--;
//apu.SetBeeping(timerSound > 0);
apu.SetBeeping(timerSound > 0);
}

int keyPressReg = 0;
Expand Down Expand Up @@ -110,7 +108,7 @@ void Chip8::Update()
else if(opcode == 0x00e0)
{
// CLS
gpu->Clear();
gpu.Clear();
}
else if (opcode == 0x00ee)
{
Expand Down Expand Up @@ -244,7 +242,7 @@ void Chip8::Update()

case 0xd:
// DRW Vx, Vy, nibble
regs[15] = gpu->DrawSprite(regs[x], regs[y], ram + iReg, n);
regs[15] = gpu.DrawSprite(regs[x], regs[y], ram + iReg, n);
break;

case 0xe:
Expand Down Expand Up @@ -340,8 +338,7 @@ void Chip8::Update()
running = false;
}

gpu->Update();
//apu.Update();
gpu.Update();
}

int Chip8::GetOutputWidth() const
Expand All @@ -356,7 +353,12 @@ int Chip8::GetOutputHeight() const

void Chip8::SetVideoDriver(IVideoDriver *videoDriver)
{
gpu->SetVideoDriver(videoDriver);
gpu.SetVideoDriver(videoDriver);
}

void Chip8::SetAudioDriver(IAudioDriver *audioDriver)
{
apu.SetAudioDriver(audioDriver);
}

void Chip8::LoadRom(const List<unsigned char>& input)
Expand Down Expand Up @@ -409,26 +411,6 @@ int Chip8::GetSpeed() const
return speed;
}

void Chip8::SetAudioEnabled(bool enabled)
{
//apu.SetEnabled(enabled);
}

bool Chip8::GetAudioEnabled() const
{
return false;//apu.GetEnabled();
}

void Chip8::SetAudioLatencyMs(int latencyMs)
{
//apu.SetLatencyMs(latencyMs);
}

int Chip8::GetAudioLatencyMs() const
{
return 0;//apu.GetLatencyMs();
}

void Chip8::invalidOpcode()
{
throw FSL_EXCEPTION("Invalid opcode");
Expand Down
22 changes: 11 additions & 11 deletions Chip8/Chip8.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@

#include "../Common.h"
#include "Gpu.h"
#include "Apu.h"

#include "../IAudioDriver.h"

class Chip8 : public IEmulator
{
public:
Chip8();
~Chip8();

void Reset();
void Update();
virtual void Reset();
virtual void Update();

int GetOutputWidth() const;
int GetOutputHeight() const;
virtual int GetOutputWidth() const;
virtual int GetOutputHeight() const;

void SetVideoDriver(IVideoDriver *videoDriver);
virtual void SetVideoDriver(IVideoDriver *videoDriver);
virtual void SetAudioDriver(IAudioDriver *audioDriver);

void LoadRom(const List<unsigned char>& input);
bool HasRom() const;
Expand All @@ -27,10 +31,6 @@ class Chip8 : public IEmulator

void SetSpeed(int speed);
int GetSpeed() const;
void SetAudioEnabled(bool enabled);
bool GetAudioEnabled() const;
void SetAudioLatencyMs(int latencyMs);
int GetAudioLatencyMs() const;

private:
static void invalidOpcode();
Expand All @@ -48,8 +48,8 @@ class Chip8 : public IEmulator
Stack<unsigned short> *stack;
bool inputs[16];

Gpu *gpu;
//Apu apu;
Gpu gpu;
Apu apu;

bool running;
bool waitingForKeypress;
Expand Down
1 change: 0 additions & 1 deletion GLVideoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void GLVideoDriver::SetOutput(int width, int height, const unsigned int *data)
wglMakeCurrent(dc, rc);

glViewport(0, 0, viewport->GetWidth(), viewport->GetHeight());
glClear(GL_COLOR_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
Expand Down
30 changes: 30 additions & 0 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,34 @@ int Main(const List<String>& arguments)
systemVideoMenu->AddChild(systemVideoZoomMenu);
systemMenu->AddChild(systemVideoMenu);

auto systemSpeedMenu = Menu::Create("Speed");
const int numSpeeds = 10;
const int speeds[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int speedIndex = 4;
List<MenuItem *> systemSpeedItems;
auto reflectSpeedIndex = [&]
{
auto speed = speeds[speedIndex];
chip8.SetSpeed(speed);
for (int i = 0; i < systemSpeedItems.Count(); i++) systemSpeedItems[i]->SetChecked(i == speedIndex);
};
for (int i = 0; i < numSpeeds; i++)
{
auto speed = speeds[i];
auto item = MenuItem::Create(String(speed) + " instruction" + (speed > 1 ? "s" : "") + "/frame");
item->Click += [&, i]
{
speedIndex = i;
reflectSpeedIndex();
};
systemSpeedMenu->AddChild(item);
systemSpeedItems.Add(item);
}
reflectSpeedIndex();
systemMenu->AddChild(systemSpeedMenu);

menu->AddChild(systemMenu);

auto helpMenu = Menu::Create("Help");
auto helpAbout = MenuItem::Create("About...");
helpAbout->Click += [&] { MessageWindow::Info(window, "Vip8 - A Chip-8 emulator"); };
Expand Down Expand Up @@ -157,6 +184,9 @@ int Main(const List<String>& arguments)
delete systemVideoZoomMenu;
for (int i = 0; i < systemVideoZoomItems.Count(); i++) delete systemVideoZoomItems[i];

delete systemSpeedMenu;
for (int i = 0; i < systemSpeedItems.Count(); i++) delete systemSpeedItems[i];

delete helpMenu;
delete helpAbout;
delete viewport;
Expand Down
2 changes: 2 additions & 0 deletions Vip8.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Chip8\Apu.cpp" />
<ClCompile Include="Chip8\Chip8.cpp" />
<ClCompile Include="Chip8\Gpu.cpp" />
<ClCompile Include="DirectSoundAudioDriver.cpp" />
Expand All @@ -86,6 +87,7 @@
<ClCompile Include="Main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Chip8\Apu.h" />
<ClInclude Include="Chip8\Chip8.h" />
<ClInclude Include="Chip8\Gpu.h" />
<ClInclude Include="Common.h" />
Expand Down
6 changes: 6 additions & 0 deletions Vip8.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<ClCompile Include="GLVideoDriver.cpp" />
<ClCompile Include="IAudioDriver.cpp" />
<ClCompile Include="DirectSoundAudioDriver.cpp" />
<ClCompile Include="Chip8\Apu.cpp">
<Filter>Chip8</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Chip8\Chip8.h">
Expand All @@ -28,5 +31,8 @@
<ClInclude Include="GLVideoDriver.h" />
<ClInclude Include="IAudioDriver.h" />
<ClInclude Include="DirectSoundAudioDriver.h" />
<ClInclude Include="Chip8\Apu.h">
<Filter>Chip8</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 994b194

Please sign in to comment.