Pointer Question

Started by
5 comments, last by bower12345 20 years, 8 months ago
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
Mikehttp://cs.wpunj.edu/~bowersom
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.
WTF!? What exactly are you trying to do in that code?!
=================================Ignorance is the root of all evil
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.
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.
Mikehttp://cs.wpunj.edu/~bowersom
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);

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
Mikehttp://cs.wpunj.edu/~bowersom

This topic is closed to new replies.

Advertisement