-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppu_memory.cpp
More file actions
71 lines (58 loc) · 1.54 KB
/
Copy pathppu_memory.cpp
File metadata and controls
71 lines (58 loc) · 1.54 KB
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
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
using namespace std;
class PPUMemory {
public:
uint8_t pattern_tables[0x2000];
uint8_t name_tables[0x1000];
uint8_t palettes[0x20];
uint8_t spr_ram[0x100];
uint8_t oam[0x100];
// memory operations
uint8_t* get_pointer(uint16_t);
uint8_t* get_spr_pointer(uint8_t);
uint8_t read_oam(uint8_t);
void set_pattern_tables(uint8_t*);
void dma_write_oam(uint8_t*);
void write_oam(uint8_t, uint8_t);
uint8_t read(uint16_t);
void write(uint16_t, uint8_t);
};
uint8_t* PPUMemory::get_pointer(uint16_t addr) {
addr &= 0x3fff;
if (addr < 0x2000) {
return pattern_tables + addr;
} else if (addr < 0x3f00) {
return name_tables + ((addr - 0x2000) & 0xfff);
} else if (addr < 0x4000) {
if (addr & 0x13 == 0x10) {
addr -= 0x10;
}
return palettes + ((addr - 0x3f00) & 0x1f);
}
}
uint8_t PPUMemory::read(uint16_t ind) {
return *(get_pointer(ind));
}
void PPUMemory::write(uint16_t ind, uint8_t val) {
*(get_pointer(ind)) = val;
}
uint8_t* PPUMemory::get_spr_pointer(uint8_t addr) {
return spr_ram + addr;
}
void PPUMemory::set_pattern_tables(uint8_t* pt_pointer) {
memcpy(pattern_tables, pt_pointer, 0x2000);
}
void PPUMemory::dma_write_oam(uint8_t* values) {
for(int i = 0; i < 0x100; ++i) {
oam[i] = values[i];
}
}
uint8_t PPUMemory::read_oam(uint8_t ind) {
return oam[ind];
}
void PPUMemory::write_oam(uint8_t ind, uint8_t val) {
oam[ind] = val;
}