-
Notifications
You must be signed in to change notification settings - Fork 0
/
MC146818.cpp
207 lines (185 loc) · 4.64 KB
/
MC146818.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// MC146818.cpp
// MIPS_Emulator
//
// Created by Matt on 5/12/16.
// Copyright © 2016 Matt. All rights reserved.
//
#include "MC146818.h"
// Constructor
MC146818::MC146818() {
// CMOS_RTC Addresses
//addresses.push_back(CMOS_BASE);
//addresses.push_back(CMOS_BASE+63);
addresses.push_back(CMOS_BASEPHYS);
addresses.push_back(CMOS_BASEPHYS+63);
cpu = nullptr;
cycleInterval = CLOCKSPEEDHZ;;
}
// Destructor
MC146818::~MC146818() {
}
// Called by MMU to initialize the device
void MC146818::initDevice() {
for (size_t i=0; i<64; i++) {
registers[i] = 0;
}
registers[0x1] = 0x1;
}
// Updates clock registers
void MC146818::updateClock() {
time(&utctime);
ptm = gmtime(&utctime);
// Seconds
registers[0] = ptm->tm_sec;
// Minutes
registers[2] = ptm->tm_min;
// Hours
registers[4] = ptm->tm_hour;
// Day of Week
registers[6] = ptm->tm_wday;
// Day of Month
registers[7] = ptm->tm_mday;
// Month
registers[8] = ptm->tm_mon;
// Year
registers[9] = ptm->tm_year;
}
// Called by MMU to get address mappings
std::vector<uint32_t> MC146818::getAddresses() {
return this->addresses;
}
// Called by MMU to read a byte
uint8_t MC146818::readByte(uint32_t address) {
//address &= 0x3F;
address -= CMOS_BASEPHYS;
if (address < 64) {
return registers[address];
}
else {
std::string msg = "Got invalid address in MC146818::readByte: ";
msg += std::to_string(address);
throw std::runtime_error(msg);
}
}
// Called by MMU to store a byte
void MC146818::storeByte(uint32_t address, uint8_t value) {
//address &= 0x3F;
address -= CMOS_BASEPHYS;
switch (address) {
// Seconds
case 0: {
registers[address] = value;
break;
}
// Seconds Alarm
case 1: {
registers[address] = value;
break;
}
// Minutes
case 2: {
registers[address] = value;
break;
}
// Minutes Alarm
case 3: {
registers[address] = value;
break;
}
// Hours
case 4: {
registers[address] = value;
break;
}
// Hours Alarm
case 5: {
registers[address] = value;
break;
}
// Day of Week
case 6: {
registers[address] = value;
break;
}
// Day of Month
case 7: {
registers[address] = value;
break;
}
// Month
case 8: {
registers[address] = value;
break;
}
// Year
case 9: {
registers[address] = value;
break;
}
// Register A
// UIP bit 8 is read only
case 10: {
registers[address] = (registers[address] & 0x80) | (value & 0x7F);
break;
}
// Register B
case 11: {
registers[address] = value;
break;
}
// Register C
case 12: {
registers[address] = value;
break;
}
// Register D
case 13: {
registers[address] = value;
break;
}
default: {
if ((address > 13) && (address < 64)) {
registers[address] = value;
}
else {
std::string msg = "Got invalid address in MC146818::storeByte: ";
msg += std::to_string(address);
throw std::runtime_error(msg);
}
}
}
}
// Used to get the interval between executions
uint32_t MC146818::getCycleInterval() {
return cycleInterval;
}
// Indicates to the CPU that the interval is enabled
bool MC146818::isEnabled() {
return true;
}
// Called by CPU when cycle interval is reached
void MC146818::onCycleExecute() {
// Check if we are in a time update period
if ((registers[0x1] & 0x80u) == 0) {
// Set UIP to 1 in Register A
registers[0x1] |= 0x80u;
// Set interval to match 248 milliseconds
cycleInterval = 1000*248*CLOCKSPEEDMHZ;
}
else {
// Update clock
updateClock();
// Set UIP to 0 in Register A
registers[0x1] &= ~(0x80u);
// Set interval to match 1 second
cycleInterval = CLOCKSPEEDHZ;
}
}
// Gets a pointer to the CPU so it can call sendInterrupt etc.
void MC146818::getCPUHandle(CPU* vcpu) {
if (vcpu == nullptr) {
throw std::runtime_error("Got nullptr in MC146818::getCPUHandle");
}
this->cpu = vcpu;
}