-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIoCardKeyboard.cpp
220 lines (179 loc) · 4.8 KB
/
IoCardKeyboard.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
208
209
210
211
212
213
214
215
216
217
218
219
220
// This code emulates the keyboard controller.
#include "Cpu2200.h"
#include "IoCardKeyboard.h"
#include "Scheduler.h"
#include "Ui.h"
#include "system2200.h"
#define NOISY 0 // turn on some debugging messages
#ifdef _MSC_VER
#pragma warning( disable: 4127 ) // conditional expression is constant
#endif
// instance constructor
IoCardKeyboard::IoCardKeyboard(std::shared_ptr<Scheduler> scheduler,
std::shared_ptr<Cpu2200> cpu,
int base_addr, int card_slot) :
m_scheduler(scheduler),
m_cpu(cpu),
m_base_addr(base_addr),
m_slot(card_slot)
{
if (m_slot >= 0) {
reset(true);
system2200::registerKb(
m_base_addr, 0,
std::bind(&IoCardKeyboard::receiveKeystroke, this, std::placeholders::_1)
);
}
}
// instance destructor
IoCardKeyboard::~IoCardKeyboard()
{
if (m_slot >= 0) {
reset(true); // turns off handshakes in progress
system2200::unregisterKb(m_base_addr, 0);
}
}
std::string
IoCardKeyboard::getDescription() const
{
return "Keyboard Controller";
}
std::string
IoCardKeyboard::getName() const
{
return "6367";
}
// return a list of the various base addresses a card can map to.
// the default comes first.
std::vector<int>
IoCardKeyboard::getBaseAddresses() const
{
std::vector<int> v { 0x001, 0x002, 0x003, 0x004 };
return v;
}
// return the list of addresses that this specific card responds to
std::vector<int>
IoCardKeyboard::getAddresses() const
{
std::vector<int> v;
v.push_back(m_base_addr);
return v;
}
void
IoCardKeyboard::reset(bool /*hard_reset*/) noexcept
{
m_tmr_script = nullptr;
// reset card state
m_selected = false;
m_key_ready = false; // no pending keys
m_cpb = true; // CPU busy, presumably
}
void
IoCardKeyboard::select()
{
if (NOISY) {
UI_info("keyboard ABS");
}
m_selected = true;
checkKeyReady(); // doesn't seem to matter if it is here or not
}
void
IoCardKeyboard::deselect()
{
if (NOISY) {
UI_info("keyboard -ABS");
}
m_selected = false;
m_cpb = true;
}
void
IoCardKeyboard::strobeOBS(int val)
{
if (NOISY) {
UI_warn("unexpected keyboard OBS: Output of byte 0x%02x", val);
}
}
void
IoCardKeyboard::strobeCBS(int /*val*/) noexcept
{
#if 0
int val8 = val & 0xFF;
// unexpected -- the real hardware ignores this byte
if (NOISY) {
UI_warn("unexpected keyboard CBS: Output of byte 0x%02x", val8);
}
#endif
}
// change of CPU Busy state
void
IoCardKeyboard::setCpuBusy(bool busy)
{
if (NOISY) {
UI_info("keyboard CPB%c", busy ? '+' : '-');
}
// it appears that except for reset, ucode only ever clears it,
// and of course the IBS sets it back.
m_cpb = busy;
checkKeyReady();
}
// ================== keyboard specific public functions =================
void
IoCardKeyboard::receiveKeystroke(int keycode)
{
assert((keycode >= 0));
if (keycode == KEYCODE_RESET) {
// warm reset
system2200::reset(false);
} else if (keycode == KEYCODE_HALT) {
// halt/step
m_key_ready = false;
m_cpu->halt();
} else {
m_key_code = keycode;
m_key_ready = true;
}
checkKeyReady();
}
// =================== private functions ===================
void
IoCardKeyboard::tcbScript()
{
if (m_selected) {
assert(!m_cpb);
if (m_key_ready) {
m_cpu->ioCardCbIbs(m_key_code);
m_key_ready = false;
}
m_cpu->setDevRdy(m_key_ready);
}
m_tmr_script = nullptr;
}
// this function should be safe to call any time; it internally makes
// sure not to change any state when it isn't safe to do so. if we are
// in script_mode, it will first fill the pending keystroke slot with
// the next character from the script. next, if the keyboard is selected
// and a keystroke is pending, it will schedule a callback in a few uS.
// that callback is responsible for completing the handshake to deliver
// the keystroke. the reason we wait instead of doing it immediately is
// that empirically such a delay is required otherwise the ucode may
// drop characters.
void
IoCardKeyboard::checkKeyReady()
{
if (!m_key_ready) {
system2200::pollScriptInput(m_base_addr, 0);
}
if (m_selected) {
if (m_key_ready && !m_cpb) {
// we can't return IBS right away -- apparently there
// must be some delay otherwise the handshake breaks
if (m_tmr_script == nullptr) {
m_tmr_script = m_scheduler->createTimer(
TIMER_US(50), // 30 is OK, 20 is too little
[&](){ tcbScript(); });
}
}
m_cpu->setDevRdy(m_key_ready);
}
}
// vim: ts=8:et:sw=4:smarttab