The first problem I want to solve is that I want to read in a binary file, find the value of a particular memory address, and output the value in hexidecimal. Let's say I want to read in the values from 0xFF7D to 0xFF80.
Code: Select all
#include <stdio.h>
#include <stdlib.h> // required for the function malloc
int main ( int argc, char *argv[] ){
char *buffer;
int *romsize;
int i;
unsigned long file_length;
int mem_address1 = 0xFF7D;
int mem_address2 = 0xFF7E;
int mem_address3 = 0xFF7F;
int mem_address4 = 0xFF80;
FILE *rom_image;
if ( argc != 2 ){
// print out error if there are not the right amount of arguments
printf( "usage: %s filename", argv[0] );
}
else {
// argv[1] is a filename to open
rom_image = fopen( argv[1], "rb" );
// if the file does not exist, it throws an error
if ( rom_image == 0 ){
printf( "Could not open file\n" );
}
else {
printf( "successfully opened file\n" );
// find the size of the binary file
fseek(rom_image, 0, SEEK_END);
file_length = ftell(rom_image);
fseek(rom_image, 0, SEEK_SET);
// creates a pointer that is the same size as the file
buffer=malloc(file_length+1);
if (!buffer) {
fprintf (stderr, "Memory error!");
fclose(rom_image);
}
// read the file into the buffer
char a = fread(buffer, file_length, 1, rom_image);
// file read into the buffer, can close
fclose( rom_image );
// print out the desired byte
printf("%X ", buffer[mem_address1]);
printf("\n");
printf("%X ", buffer[mem_address2]);
printf("\n");
printf("%X ", buffer[mem_address3]);
printf("\n");
printf("%X ", buffer[mem_address4]);
printf("\n");
// clear memory
free(buffer);
}
}
}
In my test game (Super Mario World), I should get the following output:
Code: Select all
BD
53
FF
99
Code: Select all
FFFFFFBD
53
FFFFFFFF
FFFFFF99