Not reading 0x20!

Hello all

I'm reading a binary file and some of the value are incorrect. Please help!

Here's the relevant code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>

#include "CPU.h"

using namespace std;

void clear()
{
    for(int i=0;i<30;i++)
        cout << endl;
}

int main()
{
    ifstream file;

    CPU test;

    file.open("nestest.nes", ios::binary | ios::in);

    if(file.is_open())
        cout << "Good\n";

    char in;
    uint8_t bin;
    uint16_t oi = 0xC000;

    for(int i=0;i<0x10;i++) //get rid of header for now
        file >> bin;

    while(!file.eof() && in != 'y')
    {
        file >> bin;
        cout << "bin " << hex << (int)bin << endl;
        test.Write8(oi, bin);
        oi += 1;
        cin >> in;
        clear();
    }

    test.PrintMem(0xC000, 0xC06B);
    while(in!='y')
    {
        test.Debug();
        test.Execute();
        test.Debug();
        cin >> in;
        clear();
    }



   // for(int i=0;i<4;i++)
     //   file.read(&header[i], sizeof(char));

  //  cout << header[0] << header[1] << header[2] << header[3] << endl;


    return 1;
}

Hex dump
4C F5 C5 60 78 D8 A2 FF 9A AD 02 20 10 FB AD 02 20 10 FB A9

Program output
4c f5 c5 60 78 d8 a2 ff 9a ad 02 10 fb ad 02 10 fb a9
Last edited on
NES EMULATION!!!!!!!

I was an NES emu author in a previous life. I'm always excited to see other people start work on an emu of their own. Please feel free to post questions about emulation on here... I freaking love this stuff.


That said... the problem is that the >> operator is designed for extracting text... and as part of that design, it throws away whitespace. 0x20 is a space character, so it is getting discarded.

So line 34:
 
file >> bin;


Should be replaced with:
 
file.read(&bin, sizeof(char));


The read() function does not do any text processing -- it just gives you whatever raw data is in the file. Unlike the >> operator.



EDIT:

Also -- great choice starting with nestest.nes. Get your CPU working first or else nothing will work. One nasty CPU bug can destroy the whole emu.
Last edited on
Ahh thank you! It's working fine now. It's small quirks like that, that make me realize how much more I need to learn. Thank you!
Topic archived. No new replies allowed.