Shifting Data In An Array


goobers

Member
Joined
Jan 22, 2007
Messages
344
Location
The UK
Website
Visit site
Hey, I'm currently working on a little Chip8 emulator and I'm reading the contents of a file into an array.

Problem is that Chip8 programs all start at memory address 0x200. Basically, how could I shift all the data in the array 200 bytes?

The code I'm using at the moment is as below:

CODE

char * memory; // Set up an array for the memory
unsigned char V[16];

FILE * pFile;
long fSize;
size_t result;
char * pFileName;
pFileName = "test.bin";

pFile = fopen ( pFileName, "rb");
if (pFile==NULL) {fprintf(stderr, "Error opening file"); return 0;}
else
{
fseek (pFile, 0, SEEK_END);
fSize = ftell (pFile);
rewind (pFile);

// Size the memory buffer to the size of the input file
memory = (char*) malloc ((sizeof(char)*fSize)+200);
if (memory == NULL) {fprintf(stderr, "Error creating memory buffer"); return 0;}

// Copy the file into the memory buffer
result = fread (memory,1, fSize,pFile);
if (result != fSize) {fprintf(stderr, "Error reading file"); return 0;}
}
 
yeah, just point to 200b into the array :) If you're new to the language, it might be hard to work on an emu first, but good show in challenging yourself :)

ie: memory+200 is one way (since you malloc'd it), or you could do &(memory[200]), that sort of thing.

jeff
 
Cheers- I find that I learn best when I push myself to do something quite challenging :)

Is there a way to tell fRead to copy the data starting at address 200? cos at the moment it obviously just copies it to the beginning:

CODE

result = fread (memory,1, fSize,pFile);



Thanks for your help guys, maybe when I'm done, I'll release the source and write a tutorial for the Wiki?
 
Back
Top