Skip to content

Commit

Permalink
Sound Update: Changes to Chip8.cpp and included lib files.
Browse files Browse the repository at this point in the history
This Chip8 emulator now produces a beeping noise. The noise made can be changed by swapping out the "beep.wav" file for another file of that type. The file must be named "beep.wav" in order for the emulator to properly pick it up.

The next planned addition is for SCHIP functionality. This will most likely require a detection method to determine what type of ROM is being loaded, and thus, what methods should be used to communicate with SFML.
  • Loading branch information
deathscreton committed May 12, 2020
1 parent a2f613a commit 3627750
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Chip8 Emulator.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)lib\debug\SFML;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-window-s-d.lib;sfml-system-s-d.lib;sfml-graphics-s-d.lib;opengl32.lib;winmm.lib;gdi32.lib;freetype.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>sfml-audio-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;sfml-graphics-s-d.lib;opengl32.lib;winmm.lib;gdi32.lib;freetype.lib;openal32.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;legacy_stdio_definitions.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>LinkVerbose</ShowProgress>
</Link>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -126,7 +126,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)lib\release\SFML;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-graphics-s.lib;sfml-system-s.lib;sfml-window-s.lib;opengl32.lib;winmm.lib;gdi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>sfml-audio-s.lib;sfml-graphics-s.lib;sfml-system-s.lib;sfml-window-s.lib;opengl32.lib;winmm.lib;gdi32.lib;openal32.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;legacy_stdio_definitions.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
4 changes: 4 additions & 0 deletions include/Chip8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Chip8
///////////////////////

bool drawFlag; //Determines if something was written to the display buffer and needs to be pushed.
bool playSound; //Determines if the beep sound is ready to play.

char unsigned gfx[64][32]{0,0}; //Screen buffer, 2048 pixels total. Switched to a 2D array, last buffer used 1D array with a 64 multiple offset for the y value.
char unsigned key[16]{0}; //Key buffer that stores the state of a pressed key; the value does not matter. Anything other than 0 indicates a pressed state.
Expand Down Expand Up @@ -73,6 +74,9 @@ class Chip8
//Checks to make sure the opcode being generated is a legal instruction.
bool isValidOp(const char &nibble);

//Checks to see if a sound needs to be played. If so, returns true.
bool isSoundReady();

//Function responsible for returning the offset value based on the child opcode function requested by the loNibble.
enum childFuncOffset getOffset();

Expand Down
33 changes: 30 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

#include "SFML\Graphics.hpp"
#include "SFML\Window\Keyboard.hpp"
#include "SFML\Audio.hpp"

#include "Chip8.hpp"

//Chunk of code responsible for resolving "unresolved external symbol __iob_func linker" errors.
//Had to include legacy_stdio_definitions.lib as well. Will debug this later.
#define stdin (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))

FILE _iob[] = { *stdin, *stdout, *stderr };
extern "C" FILE * __cdecl __iob_func(void) { return _iob; }
//

//MACROS//

#define SCALE_FACTOR 12.5f //ratio between resolutions 64x32 and 800x600
Expand All @@ -16,7 +27,9 @@
//OBJECT INITIALIZATION//

Chip8 chip8; //Creates emulator object and initializes class state using constructor
sf::Event event;
sf::Event event; //Creates event object to contain event types necessary for interactivity.
sf::SoundBuffer beepBuffer; //Creates sound buffer to hold beep.wav file.
sf::Sound beepSound; //Creates sound object to control buffer playback.
sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Chip 8 Emulator"); //Create and declare Window object for rendering.
sf::RectangleShape chip8SpriteRect(sf::Vector2f(SCALE_FACTOR, SCALE_FACTOR)); //Create RectangleShape object with a size of 12.5f, which is also the scale factor.

Expand Down Expand Up @@ -172,10 +185,10 @@ void pushBuffer() //Fills the SFML window buffer with the gfx buffer from the ch
}
}

void emulationLoop()
void emulationLoop() //Main emulation loop responsible for running cycles, checking events, playing sounds, etc.
{
mainWindow.setFramerateLimit(60);

while (mainWindow.isOpen())
{
while (mainWindow.pollEvent(event))
Expand Down Expand Up @@ -212,6 +225,12 @@ void emulationLoop()

chip8.drawFlag = false;
}

if (chip8.playSound)
{
beepSound.play();
chip8.playSound = false;
}
}
}

Expand All @@ -221,6 +240,14 @@ int main(int argc, char* argv[])
{
if (chip8.loadROM())
{
if (!beepBuffer.loadFromFile("beep.wav"))
{
std::cerr << "Error loading sound file. Please check that the file name is beep.wav and is located next to the executable. Continuing execution..." << std::endl;
}
else
{
beepSound.setBuffer(beepBuffer);
}
emulationLoop();
}
else
Expand Down
21 changes: 18 additions & 3 deletions src/Chip8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Chip8::Chip8()
for (int x = 0; x < 16; x++)
V[x] = 0;

playSound = false;
drawFlag = true;
}

Expand Down Expand Up @@ -189,12 +190,27 @@ void Chip8::emulateCycle()
--delayTimer;
if (soundTimer > 0)
{
if (soundTimer == 1)
std::cout << "BEEP MOFO" << std::endl;
if (isSoundReady())
{
playSound = true;
}
--soundTimer;
}
}

//Checks to see if a sound needs to be played. If so, returns true.
bool Chip8::isSoundReady()
{
if (soundTimer == 1)
{
return true;
}
else
{
return false;
}
}

//Find the initial opcode function using the hiNibble. If isValidOP returns true, the nibble is passed to parentFuncTable to initiate the function. If false, it hands off the program to the catch-all function XXXX.
void Chip8::findFunc()
{
Expand Down Expand Up @@ -507,7 +523,6 @@ void Chip8::ADDXY()
pc += 2;
}


//8xy5: Compares VX to VY. If larger, set VF flag to 1, else set to 0, then subtract VY from VX, store the results in VX.
void Chip8::SUBXY()
{
Expand Down

0 comments on commit 3627750

Please sign in to comment.