nesdev.com
http://forums.nesdev.com/

C++ WTF
http://forums.nesdev.com/viewtopic.php?f=3&t=9660
Page 1 of 3

Author:  WedNESday [ Tue Jan 01, 2013 2:48 pm ]
Post subject:  C++ WTF

On creation of a mapper 3 game...

Code:
case 3:
   VideoRAM = 0
   break;


While fetching either VRAM or VRAM data...

Code:
if (VideoRAM)
   Data = VRAM[....];
else
   Data = VROM[....];


After the working Arkanoid title screen (which means VROM has been used so far) just as you go to play my emulator crashes with an error. Upon debugging it says that it tried to access VRAM. Nowhere in the program is VideoRAM being set to 1 by mistake and if I try to manually trap the illegal access with...

Code:
if (VideoRAM)
{
   MessageBox(NULL, "WARNING", "", 0);
   Data = VRAM[....];
}
else
   Data = VROM[....];


...nothing happens because VideoRAM never equals 1 but it still goes on to crash straight away on the next line.

I have deleted my PCH once already and that helped but it came back after a few tries. Whats more it only seems to happen about 80% of the time. Just WTF is going on?

Author:  thefox [ Tue Jan 01, 2013 3:05 pm ]
Post subject:  Re: C++ WTF

"Happens 80% of the time" usually means some kind of concurrency problem. It's impossible to say based on the information that you gave. Because you talked about PCH files, I assume you use Visual Studio, so why don't you debug from the point where the crash happened? (Look at the disassembly if you have to.)

Author:  WedNESday [ Tue Jan 01, 2013 4:05 pm ]
Post subject:  Re: C++ WTF

I can't debug any further from the point where it crashes as the Ignore button is greyed out.

It has nothing to do with the PCH as regardless to whether I delete that or not the problem still persists.

Author:  natt [ Tue Jan 01, 2013 5:54 pm ]
Post subject:  Re: C++ WTF

WedNESday wrote:
I have deleted my PCH once already and that helped but it came back after a few tries.


I highly doubt it has anything to do with PCH files, but if you think it does, just turn them off.

Author:  Dwedit [ Tue Jan 01, 2013 5:55 pm ]
Post subject:  Re: C++ WTF

Sounds like an access violation? I've seen some programs put in SEH handlers (structured exception handlers) to allow execution to continue despite an access violation.

But anyway, are you sure it's not just uninitialized or corrupted memory?

Author:  Zelex [ Tue Jan 01, 2013 11:07 pm ]
Post subject:  Re: C++ WTF

thefox wrote:
"Happens 80% of the time" usually means some kind of concurrency problem. It's impossible to say based on the information that you gave. Because you talked about PCH files, I assume you use Visual Studio, so why don't you debug from the point where the crash happened? (Look at the disassembly if you have to.)


That or also could be memory corruption.

Author:  rainwarrior [ Tue Jan 01, 2013 11:16 pm ]
Post subject:  Re: C++ WTF

Why not put a data breakpoint on the variable in question?

Author:  cpow [ Wed Jan 02, 2013 7:14 am ]
Post subject:  Re: C++ WTF

WedNESday wrote:
Code:
case 3:
   VideoRAM = 0
   break;


While fetching either VRAM or VRAM data...

Code:
if (VideoRAM)
   Data = VRAM[....];
else
   Data = VROM[....];


There's no C++ here. :?: It's things like this that give C++ a bad name. :roll:

WedNESday wrote:
I have deleted my PCH once already and that helped but it came back after a few tries. Whats more it only seems to happen about 80% of the time. Just WTF is going on?


Check your CHR bank switching code. Make sure you're never selecting VROM banks other than 0 or 1. I'm assuming you mean Arkanoid (U).nes. When I run that it is CNROM and has 2 CHR banks. Make sure that no matter what is written to $8000 - $FFFF you only use the number of bits necessary to select between the actual number of banks present in the image.

I looked at my CNROM implementation and I don't mask any bits so that implies that the correct values are written to $8000 - $FFFF at least by Arkanoid (U).nes [which works for me].

Author:  WedNESday [ Wed Jan 02, 2013 10:33 am ]
Post subject:  Re: C++ WTF

1. What's wrong with my C++? :o

2. The problem seems to have fixed itself now since I have mended the PPU renderer. Although I must admit that this is not the first time that I have had code self modify.

Author:  Jsolo [ Wed Jan 02, 2013 11:15 am ]
Post subject:  Re: C++ WTF

Code does not modify itself unless explicitly told to. You are probably a victim of the common buffer overflow (bufferus overflowis).
C++ provides many abstractions to prevent buffer overflows, for instance std::vector<> or std::array<>. :)

Author:  mic_ [ Thu Jan 03, 2013 11:48 pm ]
Post subject:  Re: C++ WTF

For issues like these you might want to make use of utilities like Valgrind or Electric-Fence in order to find potential error sources faster.

Quote:
Upon debugging it says that it tried to access VRAM.

So it actually halted execution and entered the VS debugger? In that case you should be able to see what value VideoRAM has when execution halts. Does it look valid (you seem to be writing only 0 and 1 to it, so then it should never contain anything else)? If it doesn't you might be having problems with buffer overflows as Jsolo suggested.
Another potential error source would be concurrency, though I have no idea how you've structured your emulator, and it's impossible to say from the snippets of code pasted here. If you've got dependencies to a global variable in more than one thread and at least one of those threads is modifying the variable you'll typically want to make those pieces of code mutually exclusive from one another.

Quote:
Nowhere in the program is VideoRAM being set to 1 by mistake

At which points do you set it to 1 on purpose though? Might that have happened sometime earlier during execution, and now you're looking at an old value of VideoRAM while VRAM has been freed?

Quote:
are you sure it's not just uninitialized ... memory?

Only local variables (i.e. those allocated on the stack) are truly uninitialized in C++. A global or local static variable should contain a zero value if not explictly initialized.

Author:  tepples [ Fri Jan 04, 2013 8:29 am ]
Post subject:  Re: C++ WTF

I was under the impression that the use of global and static variables was "considered harmful" nowadays. Global variables make it harder to turn an object into one that supports multiple instances (such as the parallel emulators that power nemulator's Wii Menu-style game chooser). Global variables also interfere with multithreading, especially now that computers have multiple cores, and cores in low-cost or low-power CPUs such as Atom and PPE have simultaneous multithreading to hide the negative effects of in-order execution.

Author:  koitsu [ Fri Jan 04, 2013 10:26 am ]
Post subject:  Re: C++ WTF

tepples wrote:
I was under the impression that the use of global and static variables was "considered harmful" nowadays. ... Global variables also interfere with multithreading, especially now that computers have multiple cores, and cores in low-cost or low-power CPUs such as Atom and PPE have simultaneous multithreading to hide the negative effects of in-order execution.

Utter nonsense.

Author:  Jsolo [ Fri Jan 04, 2013 10:44 am ]
Post subject:  Re: C++ WTF

tepples wrote:
I was under the impression that the use of global and static variables was "considered harmful" nowadays.

Global variables have their place, as do macros and all other language features. There are situations where using global variables is perfectly fine.

tepples wrote:
Global variables also interfere with multithreading.

C++11 fixed this by introducing thread_local.

Author:  WedNESday [ Sat Jan 05, 2013 2:41 pm ]
Post subject:  Re: C++ WTF

Just a quick update for you guys. As I have fixed elements of broken mapping/mirroring/timing etc. the whole thing has gotten a lot better. I think that maybe before the PPU was getting into a loop that it couldn't get out of or something.

Page 1 of 3 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/