First things first - I actually never read file as binary in C++ (although, of course, I used C++ o/i/fstream a lot of times) and I'm also new to emulation stuff.
Recently I got into this tutorial and coded my very first emulator (which is Chip 8 emulator):
http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/
But when I ran Pong on my emulator all I saw were a couple of "O"s at the top of the console window (right now I'm just printing Os and white spaces in the console, as I just want to check out if my emulator works). Clearly there was something very wrong with my code, so I downloaded and opened source code included in the above tutorial to check for any errors I might've done, but found none. I compared each function from sources with my own code and couldn't find anything wrong with my code.
Therefore I thought that there must be some kind of error ongoing when the program loads a file as binary. Although I did included some simple error checking in the function responsible for loading a file and none of this error checkings ever showed me an error. First I wrote a simple printf in emulateCycle function:
printf("%X\n", opcode);
And then in main function I replaced infinite for loop with 8-step for loop, so my app would just show me first 8 opcodes. And then I opened pong2.ch8 file (which is the file I'm trying to load) in hex editor, so I could compare these opcodes with "original" ones. You can see what I got in readingerror.jpg. Only the first opcode matches its respective opcode in pong2.ch8 hex code. (File size is actually ok here, it should be 294 as it is).
I checked loadprogram function (which loads a binary file) for any errors, but there were none. So I decided to check if actually everything from pong2.ch8 is read and written to buffer and my emulator memory correctly. After writing contents of pong2.ch8 into buffer (which is of type char *) I did a printf to view contents of buffer variable. File readingerror2.jpg shows the results - first three nibbles are ok, but 4th one misses a zero before C! And this error continues to appear later.
I'm not sure if this is actually the issue and if this is the cause of Pong not working correctly on my emulator, but it seems to me there's something really wrong with loading a file as binary. I'm attaching loadprogram function here and you can see my whole code in main.txt. I searched Internet, but I cannot find what I'm doing wrong in this loadprogram function. I also compiled my project on VS2010 and Devcpp, but results are the same.
bool loadprogram(const char * filename) {
fstream pfile;
pfile.open(filename, ios::in | ios::binary);
if (pfile == NULL) {
fputs("File error", stderr);
return false;
}
pfile.seekg(0, ios::end);
long bufferSize = pfile.tellg();
pfile.seekg(0, ios::beg);
printf("File size: %d\n", (int)bufferSize);
char * buffer = (char*)malloc(sizeof(char) * bufferSize);
if (buffer == NULL) {
fputs("Memory error", stderr);
return false;
}
pfile.read(buffer, bufferSize);
if ((4096-512) > bufferSize) {
for (int i = 0; i<bufferSize; i++) {
memory[i+512] = buffer[i];
printf("%X\n", buffer[i]);
}
}
else
printf("Error: ROM too big for memory.\n");
pfile.close();
free(buffer);
return true;
}
Guy in the tutorial actually coded this function in C, but I rewrote it in C++ to see if it would help me resolve this issue.