Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bower12345

Pointer Question

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Guys, I''m trying to use pointers to access 4 specific areas of an one array at a time. I''m doing this to be able to switch where I''m pointing at very quickly instead of copying memory. But, I can''t seem to get this to work...here is the code:
fread(Prg_Rom_Dump, 0x4000, Prg_ROM_Pages, RomImage);
if(Prg_ROM_Pages == 1)
{
	PrgPtr3 = Prg_Rom_Dump;
	PrgPtr4 = Prg_Rom_Dump + 0x2000;
}
else
{
	PrgPtr1 = Prg_Rom_Dump;
	PrgPtr2 = Prg_Rom_Dump + 0x2000;
	PrgPtr3 = Prg_Rom_Dump + 0x4000;
	PrgPtr4 = Prg_Rom_Dump + 0x6000;
}
CPU.PC = *(PrgPtr4 + 0x1FFC) | (*(PrgPtr4 + 0x1FFD) << 8);

I seem to be having a problem somewhere in here. The file opens correctly, and everything is read into the array correctly with fread. I''m looking to find my initial PC value from the PrgPtr4 pointer, but its not working. It compiles correctly, but can anyone see if I''m messing up somewhere? Thanks, Mike

Share this post


Link to post
Share on other sites
Advertisement
In what way is it not working? Are you sure you''ve got the byte order correct? I presume from the way you''re using PrgPtr4 that it is unsigned char (otherwise don''t forget that sizeing issues mean you can''t do those simple pointer arithmetics). Other than that I don''t think theres enough info for a sensible answer.

Share this post


Link to post
Share on other sites
I guess this is for emulating a processor, right?
If the byte order is little-endian (as for Intel CPUs e.g.), shouldn''t

CPU.PC = *(PrgPtr4 + 0x1FFC) | (*(PrgPtr4 + 0x1FFD) << 8);


be the same as:

int adress = *(PrgPtr4 + 0x1FFC);
(given 2-byte-integers)?

Anyway, you should try to split up the last line into several, saving every byte for itself.
Perhaps like this:

char lb = *(PrgPtr4 + 0x1FFC);
short int lowbyte = lb;
char hb = *(PrgPtr4 + 0x1FFD);
short int highbyte = hb;

CPU.PC = lowbyte | (highbyte << 8);


Then debugging the program and looking at these two variables should lead to the problem.
Sorry the code is such a mess, but as JuNC already mentioned there is probably a sizing issue. Thus, shifting the one byte could easily ''create'' all-zeroes.

Share this post


Link to post
Share on other sites
Hey,

What I am doing is emulating a 6502 processor for NES. Basically, I am trying to optimize code swapping for memory mappers by using pointers. Because of course its much more efficient to use pointers than copying memory. The pointers are pointing to an array declared as: BYTE Prg_Rom_Dump[1045786];

The Program counter is a 16 bit value. Does this mean I need my pointers to be of the type WORD? Even though they are pointing to a BYTE array? I seem to be having trouble getting the initial value of the program counter from the reset vector. I do know the the correct values are in PrgPtr4 + 0x1FFC and PrgPtr4 + 0x1FFD. But for some reason I can''t get them using a pointer. 0x1FFD is the high byte and 0x1FFC is the low byte in case you were wondering.

Share this post


Link to post
Share on other sites
Yeah, Ledins probably hit the nail on the head:

CPU.PC = *(PrgPtr4 + 0x1FFC) | (*(PrgPtr4 + 0x1FFD) << 8);

In this line *(PrgPtr4 + 0x1FFD) has type BYTE (probably unsigned char) and shifting it left by 8 will essentially zero out the byte (although it depends somewhat on the generated instructions exactly what will happen). You need to cast these to unsigned short before the bitops:

CPU.PC = ((unsigned short)*(PrgPtr4 + 0x1FFC)) | ((unsigned short)*(PrgPtr4 + 0x1FFD) << 8);

Share this post


Link to post
Share on other sites
Thanks folks. That fixed it up. I didn''t realize the whole size thing mattered with pointers also. I thought the pointer just had to be the same type as the value it was pointing to. My CPU core is parsing code like crazy now. Thanks again!

Regards,
Mike

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!