-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMU.c
More file actions
69 lines (53 loc) · 1.63 KB
/
MMU.c
File metadata and controls
69 lines (53 loc) · 1.63 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
#include "MMU.h"
#include <stdio.h>
#include <stdlib.h>
uint8_t rom[0x7FFF];
uint8_t video_ram[0x1FFF];
uint8_t external_ram[0x1FFF]; // cartridge
uint8_t working_ram[0x1FFF];
uint8_t* working_ram_shadow; // Set this = working_ram!
uint8_t oam[0x00FF]; // Sprite attrib memory
uint8_t zeropage_ram[0x007F];
uint8_t* addressBus(uint16_t address) {
if (address <= 0x7FFF)
return &rom[address];
if (address >= 0x8000 && address <= 0x9FFF)
return &video_ram[address - 0x8000];
if (address >= 0xA000 && address <= 0xBFFF)
return &external_ram[address - 0xA000];
if (address >= 0xC000 && address <= 0xDFFF)
return &working_ram[address - 0xC000];
if (address >= 0xE000 && address <= 0xFDFF)
return &working_ram_shadow[address - 0xE000];
if (address >= 0xFE00 && address <= 0xFEFF)
return &oam[address - 0xFE00];
if (address >= 0xFF00 && address <= 0xFF7F){}
//io control handling
if (address >= 0xFF80 && address <= 0xFFFF)
return &zeropage_ram[address - 0xFF80];
fprintf(stderr, "adressBus received undefined address: 0x%X, exiting.\n", address);
exit(1);
}
uint8_t readByte(uint16_t address) {
return *addressBus(address);
}
uint16_t readShort(uint16_t address) {
// Z80 word size is 8 bits.
// Using short, from short integer, to denote 16 bit.
return readByte(address) | (readByte(address + 1) << 8);
}
void writeByte(uint16_t address, uint8_t byte) {
*addressBus(address) = byte;
}
void loadROM(char* path) {
FILE* f = fopen(path, "rb");
if (f == NULL) {
printf("Failed to load ROM. Exiting..\n");
exit(1);
}
fread(rom, sizeof(rom), 1, f);
}
void initMMU() {
working_ram_shadow = working_ram;
// TODO: run BIOS
}