It is currently Sat Dec 16, 2017 5:32 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: C++ WTF
PostPosted: Tue Jan 01, 2013 2:48 pm 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
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?

_________________
http://www.jamesturner.de/


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 3:05 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2983
Location: Tampere, Finland
"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.)

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: kkfos.aspekt.fi


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 4:05 pm 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
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.

_________________
http://www.jamesturner.de/


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 5:54 pm 
Offline

Joined: Fri Oct 26, 2012 5:27 pm
Posts: 76
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 5:55 pm 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 3969
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?

_________________
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 11:07 pm 
Offline

Joined: Fri Apr 29, 2011 9:44 pm
Posts: 267
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Tue Jan 01, 2013 11:16 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
Why not put a data breakpoint on the variable in question?


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Wed Jan 02, 2013 7:14 am 
Offline
NESICIDE developer
User avatar

Joined: Mon Oct 13, 2008 7:55 pm
Posts: 1058
Location: Minneapolis, MN
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].


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Wed Jan 02, 2013 10:33 am 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
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.

_________________
http://www.jamesturner.de/


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Wed Jan 02, 2013 11:15 am 
Offline

Joined: Mon Jun 27, 2011 4:14 am
Posts: 27
Location: Lurker Cave
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<>. :)


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Thu Jan 03, 2013 11:48 pm 
Offline

Joined: Thu Oct 05, 2006 6:29 am
Posts: 911
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Fri Jan 04, 2013 8:29 am 
Online

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19353
Location: NE Indiana, USA (NTSC)
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Fri Jan 04, 2013 10:26 am 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Fri Jan 04, 2013 10:44 am 
Offline

Joined: Mon Jun 27, 2011 4:14 am
Posts: 27
Location: Lurker Cave
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.


Top
 Profile  
 
 Post subject: Re: C++ WTF
PostPosted: Sat Jan 05, 2013 2:41 pm 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
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.

_________________
http://www.jamesturner.de/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 32 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group