~groovestomp/gsnes

gsnes/cart.c -rw-r--r-- 7.1 KiB
9de07668 — GrooveStomp Foreground rendering works 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/******************************************************************************
  GrooveStomp's NES Emulator
  Copyright (c) 2019 Aaron Oman (GrooveStomp)

  File: cart.c
  Created: 2019-11-03
  Updated: 2019-12-02
  Author: Aaron Oman
  Notice: GNU AGPLv3 License

  Based off of: One Lone Coder NES Emulator Copyright (C) 2019 Javidx9
  This program comes with ABSOLUTELY NO WARRANTY.
  This is free software, and you are welcome to redistribute it under certain
  conditions; See LICENSE for details.
 ******************************************************************************/
//! \file cart.c
#include <stdint.h>
#include <stdio.h> // fopen
#include <stdbool.h> // bool
#include <stdlib.h> // calloc, free

#include "cart.h"
#include "util.h"
#include "mapper.h"
#include "mapper000.h"

struct cart {
        uint8_t mapperId;
        uint8_t prgBanks;
        uint8_t chrBanks;
        bool isImageValid;
        uint8_t *prgMem;
        uint8_t *chrMem;
        void *mapper;
        mapper_init_fn mapperInit;
        mapper_deinit_fn mapperDeinit;
        mapper_reset_fn mapperReset;
        map_cpu_read_fn mapCpuRead;
        map_cpu_write_fn mapCpuWrite;
        map_ppu_read_fn mapPpuRead;
        map_ppu_write_fn mapPpuWrite;

        enum mirror mirror;
};

struct header {
        char name[4];
        uint8_t prgRomChunks;
        uint8_t chrRomChunks;
        uint8_t mapper1;
        uint8_t mapper2;
        uint8_t prgRamSize;
        uint8_t tvSystem1;
        uint8_t tvSystem2;
        char unused[5];
};

struct cart *CartInit(char *filename) {
        struct cart *cart = (struct cart *)calloc(1, sizeof(struct cart));
        if (NULL == cart) {
                return NULL;
        }

        cart->isImageValid = false;
        cart->mapperId = 0;
        cart->prgBanks = 0;
        cart->chrBanks = 0;
        cart->mirror = MIRROR_HORIZONTAL;

        FILE *f = fopen(filename, "rb");
        if (NULL == f) {
                free(cart);
                return NULL;
        }

        struct header header;
        size_t objs_read = fread(&header, sizeof(struct header), 1, f);
        if (objs_read != 1) {
                fclose(f);
                free(cart);
                return NULL;
        }

        // For this mapper, the next 512 bytes are trainer info; which we ignore.
        if (header.mapper1 & 0x04) {
                fseek(f, 512, SEEK_CUR);
        }

        // Determine mapper ID
        cart->mapperId = ((header.mapper2 >> 4) << 4) | (header.mapper1 >> 4);
        cart->mirror = (header.mapper1 & 0x01) ? MIRROR_VERTICAL : MIRROR_HORIZONTAL;

        // "Discover" file format.
        uint8_t file_type = 1;

        if (0 == file_type) {
                // TODO unhandled file_type
        }

        if (1 == file_type) {
                cart->prgBanks = header.prgRomChunks;
                cart->prgMem = (uint8_t *)calloc(cart->prgBanks, KB_AS_B(16));
                if (NULL == cart->prgMem) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't allocate memory for prgMem - set appropriate error
                }

                objs_read = fread(cart->prgMem, KB_AS_B(16), cart->prgBanks, f);
                if (objs_read < cart->prgBanks) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }

                cart->chrBanks = header.chrRomChunks;
                int numBanks = (0 == cart->chrBanks) ? 1 : cart->chrBanks;

                cart->chrMem = (uint8_t *)calloc(numBanks, KB_AS_B(8));
                if (NULL == cart->chrMem) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }

                objs_read = fread(cart->chrMem, KB_AS_B(8), numBanks, f);
                if (objs_read < numBanks) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }
        }

        if (2 == file_type) {
                // TODO unhandled file_type
        }

        switch(cart->mapperId) {
                case 0: {
                        cart->mapperInit = Mapper000_Init;
                        cart->mapperDeinit = Mapper000_Deinit;
                        cart->mapperReset = Mapper000_Reset;
                        cart->mapCpuRead = Mapper000_MapCpuRead;
                        cart->mapCpuWrite = Mapper000_MapCpuWrite;
                        cart->mapPpuRead = Mapper000_MapPpuRead;
                        cart->mapPpuWrite = Mapper000_MapPpuWrite;

                        cart->mapper = cart->mapperInit(cart->prgBanks, cart->chrBanks);
                        if (NULL == cart->mapper) {
                                fclose(f);
                                free(cart);
                                return NULL;
                                // TODO Couldn't initialize mapper - set appropriate error
                        }
                        break;
                }
        }

        cart->isImageValid = true;
        fclose(f);

        return cart;
}

void CartDeinit(struct cart *cart) {
        if (NULL == cart)
                return;

        if (NULL != cart->chrMem)
                free(cart->chrMem);

        if (NULL != cart->prgMem)
                free(cart->prgMem);

        if (NULL != cart->mapper && NULL != cart->mapperDeinit)
                cart->mapperDeinit(cart->mapper);

        free(cart);
}

bool CartCpuRead(struct cart *cart, uint16_t addr, uint8_t *data) {
        uint32_t mappedAddr = 0;
        if (cart->mapCpuRead(cart->mapper, addr, &mappedAddr)) {
                *data = cart->prgMem[mappedAddr];
                return true;
        }

        return false;
}

bool CartCpuWrite(struct cart *cart, uint16_t addr, uint8_t data) {
        uint32_t mappedAddr = 0;
        if (cart->mapCpuWrite(cart->mapper, addr, &mappedAddr)) {
                cart->prgMem[mappedAddr] = data;
                return true;
        }

        return false;
}

bool CartPpuRead(struct cart *cart, uint16_t addr, uint8_t *data) {
        uint32_t mappedAddr = 0;
        if (cart->mapPpuRead(cart->mapper, addr, &mappedAddr)) {
                *data = cart->chrMem[mappedAddr];
                return true;
        }

        return false;
}

bool CartPpuWrite(struct cart *cart, uint16_t addr, uint8_t data) {
        uint32_t mappedAddr = 0;
        if (cart->mapPpuWrite(cart->mapper, addr, &mappedAddr)) {
                cart->chrMem[mappedAddr] = data;
                return true;
        }

        return false;
}

enum mirror CartMirroring(struct cart *cart) {
        return cart->mirror;
}

bool CartIsImageValid(struct cart *cart) {
        return cart->isImageValid;
}

void CartReset(struct cart *cart) {
        if (NULL != cart->mapper && NULL != cart->mapperReset)
                cart->mapperReset(cart->mapper);
}