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
Pointer Question
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:
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.
I guess this is for emulating a processor, right?
If the byte order is little-endian (as for Intel CPUs e.g.), shouldn''t
be the same as:
Anyway, you should try to split up the last line into several, saving every byte for itself.
Perhaps like this:
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.
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.
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.
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);
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);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement