-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.cpp
109 lines (89 loc) · 2.21 KB
/
mem.cpp
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
#include <iostream>
#include <stdio.h>
#include "mem.h"
// NOTE : the way 8 and 16 bits access were implemented naturally prevent misaligned accesses
// TODO : write an exeption handler for memory violations (and other unsupported events ?)
// - dump memory address, PC value, instruction details, register status
// - and exit... or just display a nice blue screen and reboot ?
Mem::Mem() {
ram16 = (int16_t*)*(&ram8);
ram32 = (int32_t*)*(&ram8);
}
Mem::~Mem() {
// nothing to dealocate
}
uint32_t Mem::getRamStartAddress() {
return ramStartAddress;
}
void Mem::setRamStartAddress(uint32_t address) {
ramStartAddress = address;
}
int8_t Mem::get8(uint32_t address) {
if (address == GETCHAR)
return (int8_t)getchar();
address -= ramStartAddress;
if (address < RAMSIZE)
return ram8[address];
else {
std::cerr << "\n\nIllegal Memory Read\n\n";
return 0;
}
}
int16_t Mem::get16(uint32_t address) {
if (address == GETCHAR)
return (int16_t)getchar();
address -= ramStartAddress;
if (address < RAMSIZE)
return ram16[address >> 1];
else {
std::cerr << "\n\nIllegal Memory Read\n\n";
return 0;
}
}
int32_t Mem::get32(uint32_t address) {
if (address == GETCHAR)
return (int32_t)getchar();
address -= ramStartAddress;
if (address < RAMSIZE)
return ram32[address >> 2];
else {
std::cerr << "\n\nIllegal Memory Read\n\n";
return 0;
}
}
void Mem::set8(uint32_t address, int8_t value) {
if (address == PUTCHAR) {
putchar((char)value);
}
else {
address -= ramStartAddress;
if (address < RAMSIZE)
ram8[address] = value;
else
std::cerr << "\n\nIllegal Memory Write\n\n";
}
}
void Mem::set16(uint32_t address, int16_t value) {
if (address == PUTCHAR) {
putchar((char)value);
}
else {
address -= ramStartAddress;
if (address < RAMSIZE)
ram16[address >> 1] = value;
else
std::cerr << "\n\nIllegal Memory Write\n\n";
}
}
void Mem::set32(uint32_t address, int32_t value) {
if (address == PUTCHAR) {
putchar((char)value);
}
else {
address -= ramStartAddress;
if (address < RAMSIZE)
ram32[address >> 2] = value;
else
std::cerr << "\n\nIllegal Memory Write\n\n";
}
}