-
Notifications
You must be signed in to change notification settings - Fork 4
/
cartridge.c
80 lines (66 loc) · 1.72 KB
/
cartridge.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "mmu.h"
#include "state.h"
#include "diag.h"
#define CARTRIDGESIZE 131072
#define CARTRIDGEBASE 0xfa0000
static BYTE *memory;
HANDLE_DIAGNOSTICS(cartridge)
static BYTE *real(LONG addr)
{
return memory+addr-CARTRIDGEBASE;
}
static BYTE cartridge_read_byte(LONG addr)
{
return *(real(addr));
}
static WORD cartridge_read_word(LONG addr)
{
return (cartridge_read_byte(addr)<<8)|cartridge_read_byte(addr+1);
}
static int cartridge_state_collect(struct mmu_state *state)
{
state->size = 0;
return STATE_VALID;
}
static void cartridge_state_restore(struct mmu_state *state)
{
}
void cartridge_init(char *filename)
{
struct mmu *cartridge;
FILE *fp;
int file_size = 0;
memory = xmalloc(sizeof(BYTE) * CARTRIDGESIZE);
if(!memory) {
return;
}
cartridge = mmu_create("CART", "Cartridge");
cartridge->start = CARTRIDGEBASE;
cartridge->size = CARTRIDGESIZE;
cartridge->read_byte = cartridge_read_byte;
cartridge->read_word = cartridge_read_word;
cartridge->state_collect = cartridge_state_collect;
cartridge->state_restore = cartridge_state_restore;
cartridge->diagnostics = cartridge_diagnostics;
mmu_register(cartridge);
fp = fopen(filename, "rb");
if(fp) {
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
rewind(fp);
if(file_size <= CARTRIDGESIZE) {
if(fread(memory, 1, file_size, fp) != file_size) {
ERROR("Unable to load %d bytes from %s", file_size, filename);
/* Making sure cartridge memory area doesn't start with boot sequence */
memory[0] = '\0';
}
} else {
ERROR("Cannot load file larger than cartridge memory area");
}
fclose(fp);
}
}