forked from raybellis/usim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usim.cpp
169 lines (136 loc) · 3.11 KB
/
usim.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
//
//
// usim.cpp
//
// (C) R.P.Bellis 1994
//
//
#include <cstdlib>
#include <cstdio>
#include "usim.h"
//----------------------------------------------------------------------------
// Generic processor run state routines
//----------------------------------------------------------------------------
void USim::run()
{
halted = false;
while (!halted) {
tick();
}
}
void USim::tick()
{
// assume one cycle happens every time
++cycles;
// update all active devices
for (auto& d : dev_active) {
d.device->tick(cycles);
}
// reset the cycle counter
cycles = 0;
}
void USim::reset()
{
// reset all active devices
for (auto& d : dev_active) {
d.device->reset();
}
}
void USim::halt()
{
halted = true;
}
void USim::invalid(const char *msg)
{
fprintf(stderr, "error: %s [PC:$%04X IR:$%04X]\n", msg, pc, ir);
this->abort();
}
Byte USim::fetch()
{
return read(pc++);
}
//----------------------------------------------------------------------------
// Device handling
//----------------------------------------------------------------------------
void USim::attach(const ActiveDevice::shared_ptr& dev)
{
dev_active.push_back({ dev });
}
void USim::attach(const MappedDevice::shared_ptr& dev, Word base, Word mask, rank<0>)
{
dev_mapped.push_back({ dev, base, mask });
}
void USim::attach(const ActiveMappedDevice::shared_ptr& dev, Word base, Word mask, rank<1>)
{
dev_active.push_back({ dev });
dev_mapped.push_back({ dev, base, mask });
}
//----------------------------------------------------------------------------
// Mapped Device IO
//----------------------------------------------------------------------------
// Single byte read
Byte USim::read(Word offset)
{
++cycles;
for (auto& d : dev_mapped) {
if ((offset & d.mask) == d.base) {
return d.device->read(offset - d.base);
}
}
return 0xff;
}
// Single byte write
void USim::write(Word offset, Byte val)
{
++cycles;
for (auto& d : dev_mapped) {
if ((offset & d.mask) == d.base) {
d.device->write(offset - d.base, val);
break;
}
}
}
//----------------------------------------------------------------------------
// Word memory access routines for big-endian (Motorola type)
//----------------------------------------------------------------------------
Word USimMotorola::fetch_word()
{
Word tmp;
tmp = fetch() << 8;
tmp |= fetch();
return tmp;
}
Word USimMotorola::read_word(Word offset)
{
Word tmp;
tmp = read(offset++) << 8;
tmp |= read(offset);
return tmp;
}
void USimMotorola::write_word(Word offset, Word val)
{
write(offset++, (Byte)(val >> 8));
write(offset, (Byte)val);
}
//----------------------------------------------------------------------------
// Word memory access routines for little-endian (Intel type)
//----------------------------------------------------------------------------
Word USimIntel::fetch_word()
{
Word tmp;
tmp = fetch();
tmp |= fetch() << 8;
return tmp;
}
Word USimIntel::read_word(Word offset)
{
Word tmp;
tmp = read(offset++);
tmp |= (read(offset) << 8);
return tmp;
}
void USimIntel::write_word(Word offset, Word val)
{
write(offset++, (Byte)val);
write(offset, (Byte)(val >> 8));
}