-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8mem.c
112 lines (94 loc) · 2.4 KB
/
chip8mem.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
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
#include "chip8mem.h"
#include "eeprom.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "rom.h"
enum {
ROM = 0,
RAM = 1,
} MemType;
#define SLAVE_ADDRESS 1
#define CH8_PAGE_SIZE 64
#define CH8_MEM_SIZE 0x1000;
#define CACHE_NUM_PAGES 12
#define CH8_BUFFER_SIZE (CH8_PAGE_SIZE * CACHE_NUM_PAGES)
#define DEBUG_ON
byte g_Cache [CH8_BUFFER_SIZE];
byte * g_CachePtr;
uint16 g_CacheOffset = 0;
byte init;
void Ch8MemSet(const byte value, int size, uint16 address)
{
int i = 0;
uint16 cAddress = address;
uint16 cSize = size;
uint16 cOffset = 0;
byte v [CH8_PAGE_SIZE];
memset(&v, value, CH8_PAGE_SIZE);
for (i = 0; i < size; i++)
{
EEPROMReadBytes(&v[0], 1, SLAVE_ADDRESS, cAddress);
cAddress++;
cSize--;
cOffset++;
}
}
void FillCache(uint16 address)
{
int i = 0;
uint16 cAddress = address - (address % CH8_PAGE_SIZE);
uint16 cSize = CH8_BUFFER_SIZE;
uint16 cOffset = 0;
g_CacheOffset = cAddress;
for (i = 0; i < CH8_BUFFER_SIZE; i++)
{
EEPROMReadBytes(g_CachePtr + cOffset, 1, SLAVE_ADDRESS, cAddress);
cAddress++;
cSize--;
cOffset++;
}
}
void Ch8MemInit()
{
/* Initialize 4KB of blank data on the EEPROM */
g_CacheOffset = 0;
g_CachePtr = &g_Cache[0];
init = 0;
Ch8MemSet(0, 0x1000, 0x0);
}
byte Ch8MemReadByte(uint16 address)
{
if (!init || address < g_CacheOffset || address >= (g_CacheOffset + CH8_BUFFER_SIZE))
{
init = 1;
FillCache(address);
}
return g_Cache[address - g_CacheOffset];
}
void Ch8MemWriteByte(uint16 address, byte value)
{
EEPROMWriteBytes(&value, 1, SLAVE_ADDRESS, address);
if (address >= g_CacheOffset && address < (g_CacheOffset + CH8_BUFFER_SIZE))
{
g_Cache[address - g_CacheOffset] = value;
}
}
void Ch8LoadRom(const Chip8Rom * chip8rom, const uint16 dstAddress)
{
int i = 0;
uint16 cAddress = dstAddress;
uint16 cOffset = 0;
uint16 cSize = chip8rom->size;
byte page [CH8_PAGE_SIZE];
DEBUG_WRITE("ROM: %s", chip8rom->name);
for (i = 0; i < chip8rom->size; i++)
{
EEPROMReadBytes( &page[0], 1, 0, chip8rom->offset + cOffset);
EEPROMWriteBytes(&page[0], 1, 1, cAddress);
cAddress++;
cOffset++;
cSize--;
}
DEBUG_WRITE("Done");
}