PancakeNESEmu
A homebrewed NES Emulator written in C
Loading...
Searching...
No Matches
reader.c File Reference
#include "rom_reader/reader.h"
#include "macros.h"
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for reader.c:

Go to the source code of this file.

Functions

romopen_rom (char *filepath)
 A function that open the NES rom at a specified path.
 
void display_rom (rom *r)
 Procedure that displays the content of the rom in hexadecimal.
 
void free_rom (rom *r)
 Frees the rom struct.
 

Function Documentation

◆ display_rom()

void display_rom ( rom * r)

Procedure that displays the content of the rom in hexadecimal.

Parameters
[in]rThe ROM to display

Definition at line 59 of file reader.c.

59 {
60 if (r == NULL) {
61 printf("No rom found. \n");
62 return;
63 }
64 int char_displayed = 0;
65 for (size_t i = 0; i < r->buff_size; ++i) {
66 printf("%02x ", r->buff[i]);
67 ++char_displayed;
68 if (char_displayed == 16) {
69 printf("\n");
70 char_displayed = 0;
71 }
72 }
73}
size_t buff_size
The size of the buffer.
Definition reader.h:10
rom_buffer buff
The buffer that contains all the ROM's data.
Definition reader.h:9

References rom::buff, and rom::buff_size.

Referenced by main().

Here is the caller graph for this function:

◆ free_rom()

void free_rom ( rom * r)

Frees the rom struct.

Parameters
[in]rThe rom to free

Definition at line 75 of file reader.c.

75 {
76 free(r->buff);
77 free(r);
78}

References rom::buff.

Referenced by main().

Here is the caller graph for this function:

◆ open_rom()

rom * open_rom ( char * filepath)

A function that open the NES rom at a specified path.

Parameters
[in]filepathThe path to the rom
Returns
A pointer to the buffer that contains the NES rom

Definition at line 7 of file reader.c.

7 {
8 FILE* file;
9 size_t fsize;
10
11 file = fopen(filepath, "rb");
12 if (file == NULL) {
13 perror("Error opening the file !\n");
14 exit(EXIT_FAILURE);
15 }
16
17 fseek(file, 0, SEEK_END);
18 fsize = ftell(file);
19 fseek(file, 0, SEEK_SET);
20
21 rom_buffer buff = (rom_buffer)malloc(fsize);
22 if (!buff) {
23 perror("Memory allocation failed !\n");
24 fclose(file);
25 exit(EXIT_FAILURE);
26 }
27
28 size_t result = fread(buff, 1, fsize, file);
29 printf("\nRead: %lu\n", result);
30 if (result != fsize) {
31 perror("Reading error !\n");
32 free(buff);
33 fclose(file);
34 exit(EXIT_FAILURE);
35 }
36
37 fclose(file);
38
39 unsigned int file_id = (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | (buff[3]);
40 if (file_id != NES_SIGNATURE) {
41 printf("Signature is not matching ! %i != %i\n", file_id, NES_SIGNATURE);
42 printf("Only NES ROMs are accepted by this emulator !\n\n");
43 free(buff);
44 exit(EXIT_FAILURE);
45 }
46
47 rom* r = (rom*)malloc(sizeof(rom));
48 if (r == NULL) {
49 perror("Allocation failure !\n");
50 free(buff);
51 exit(EXIT_FAILURE);
52 }
53 r->buff = buff;
54 r->buff_size = fsize;
55
56 return r;
57}
#define NES_SIGNATURE
Definition macros.h:10
#define EXIT_FAILURE
Definition macros.h:6
unsigned char * rom_buffer
Definition reader.h:6
Definition reader.h:8

References rom::buff, rom::buff_size, EXIT_FAILURE, and NES_SIGNATURE.

Referenced by main().

Here is the caller graph for this function: