-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dc11.cpp
322 lines (243 loc) · 7.69 KB
/
dc11.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#if defined(ESP32)
#include <Arduino.h>
#endif
#if IS_POSIX
#include <errno.h>
#include <termios.h>
#include <thread>
#endif
#include <cstring>
#include <unistd.h>
#include "bus.h"
#include "cpu.h"
#include "dc11.h"
#include "log.h"
#include "utils.h"
#define ESP32_UART UART_NUM_1
// this line is reserved for a serial port
constexpr const int serial_line = 3;
const char *const dc11_register_names[] { "RCSR", "RBUF", "TSCR", "TBUF" };
dc11::dc11(bus *const b, const std::vector<comm *> & comm_interfaces):
b(b),
comm_interfaces(comm_interfaces)
{
connected.resize(4); // FIXME keep same size as comm_interfaces
}
dc11::~dc11()
{
DOLOG(debug, false, "DC11 closing");
stop_flag = true;
if (th) {
th->join();
delete th;
}
for(auto & c : comm_interfaces) {
DOLOG(debug, false, "Stopping %s", c->get_identifier().c_str());
delete c;
}
}
void dc11::show_state(console *const cnsl) const
{
for(size_t i=0; i<comm_interfaces.size(); i++) {
cnsl->put_string_lf(format("* LINE %zu", i + 1));
#if 0 // TODO
if (i == serial_line) {
cnsl->put_string_lf(format(" TTY thread running: %s", serial_thread_running ? "true": "false" ));
cnsl->put_string_lf(format(" TTY enabled: %s", serial_enabled ? "true": "false" ));
}
else {
if (pfds[dc11_n_lines + i].fd != INVALID_SOCKET)
cnsl->put_string_lf(" Connected to: " + get_endpoint_name(pfds[dc11_n_lines + i].fd));
}
#endif
std::unique_lock<std::mutex> lck(input_lock[i]);
cnsl->put_string_lf(format(" Characters in buffer: %zu", recv_buffers[i].size()));
cnsl->put_string_lf(format(" RX interrupt enabled: %s", is_rx_interrupt_enabled(i) ? "true": "false" ));
cnsl->put_string_lf(format(" TX interrupt enabled: %s", is_tx_interrupt_enabled(i) ? "true": "false" ));
}
}
bool dc11::begin()
{
th = new std::thread(std::ref(*this));
return true;
}
void dc11::test_port(const size_t nr, const std::string & txt) const
{
DOLOG(info, false, "DC11 test line %zu", nr);
comm_interfaces.at(nr)->send_data(reinterpret_cast<const uint8_t *>(txt.c_str()), txt.size());
}
void dc11::test_ports(const std::string & txt) const
{
for(size_t i=0; i<comm_interfaces.size(); i++)
test_port(i, txt);
}
void dc11::trigger_interrupt(const int line_nr, const bool is_tx)
{
TRACE("DC11: interrupt for line %d, %s", line_nr, is_tx ? "TX" : "RX");
b->getCpu()->queue_interrupt(5, 0300 + line_nr * 010 + 4 * is_tx);
}
void dc11::operator()()
{
set_thread_name("kek:DC11");
DOLOG(info, true, "DC11 thread started");
while(!stop_flag) {
myusleep(10000); // TODO replace polling
for(size_t line_nr=0; line_nr<comm_interfaces.size(); line_nr++) {
std::unique_lock<std::mutex> lck(input_lock[line_nr]);
// (dis-)connected?
bool is_connected = comm_interfaces.at(line_nr)->is_connected();
if (is_connected != connected[line_nr]) {
DOLOG(debug, false, "DC11 line %d state changed to %d", line_nr, is_connected);
#if defined(ESP32)
Serial.printf("DC11 line %d state changed to %d\r\n", line_nr, is_connected);
#endif
connected[line_nr] = is_connected;
if (is_connected)
registers[line_nr * 4 + 0] |= 0160000; // "ERROR", RING INDICATOR, CARRIER TRANSITION
else
registers[line_nr * 4 + 0] |= 0120000; // "ERROR", CARRIER TRANSITION
if (is_rx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, false);
}
// receive data
bool have_data = false;
while(comm_interfaces.at(line_nr)->has_data()) {
uint8_t buffer = comm_interfaces.at(line_nr)->get_byte();
recv_buffers[line_nr].push_back(char(buffer));
have_data = true;
}
if (have_data) {
registers[line_nr * 4 + 0] |= 128; // DONE: bit 7
if (is_rx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, false);
}
}
}
DOLOG(info, true, "DC11 thread terminating");
}
void dc11::reset()
{
}
bool dc11::is_rx_interrupt_enabled(const int line_nr) const
{
return !!(registers[line_nr * 4 + 0] & 64);
}
bool dc11::is_tx_interrupt_enabled(const int line_nr) const
{
return !!(registers[line_nr * 4 + 2] & 64);
}
uint8_t dc11::read_byte(const uint16_t addr)
{
uint16_t v = read_word(addr & ~1);
if (addr & 1)
return v >> 8;
return v;
}
uint16_t dc11::read_word(const uint16_t addr)
{
int reg = (addr - DC11_BASE) / 2;
int line_nr = reg / 4;
int sub_reg = reg & 3;
std::unique_lock<std::mutex> lck(input_lock[line_nr]);
uint16_t vtemp = registers[reg];
if (sub_reg == 0) { // receive status
// emulate DTR, CTS & READY
registers[line_nr * 4 + 0] &= ~1; // DTR: bit 0 [RCSR]
registers[line_nr * 4 + 0] &= ~4; // CD : bit 2
if (comm_interfaces.at(line_nr)->is_connected()) {
registers[line_nr * 4 + 0] |= 1;
registers[line_nr * 4 + 0] |= 4;
}
vtemp = registers[line_nr * 4 + 0];
// clear error(s)
registers[line_nr * 4 + 0] &= ~0160000;
}
else if (sub_reg == 1) { // read data register
TRACE("DC11: %zu characters in buffer for line %d", recv_buffers[line_nr].size(), line_nr);
// get oldest byte in buffer
if (recv_buffers[line_nr].empty() == false) {
vtemp = *recv_buffers[line_nr].begin();
// parity check
registers[line_nr * 4 + 0] &= ~(1 << 5);
registers[line_nr * 4 + 0] |= parity(vtemp) << 5;
recv_buffers[line_nr].erase(recv_buffers[line_nr].begin());
// still data in buffer? generate interrupt
if (recv_buffers[line_nr].empty() == false) {
registers[line_nr * 4 + 0] |= 128; // DONE: bit 7
if (is_rx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, false);
}
}
}
else if (sub_reg == 2) { // transmit status
registers[line_nr * 4 + 2] &= ~2; // CTS: bit 1 [TSCR]
registers[line_nr * 4 + 2] &= ~128; // READY: bit 7
if (comm_interfaces.at(line_nr)->is_connected()) {
registers[line_nr * 4 + 2] |= 2;
registers[line_nr * 4 + 2] |= 128;
}
vtemp = registers[line_nr * 4 + 2];
}
TRACE("DC11: read register %06o (\"%s\", %d line %d): %06o", addr, dc11_register_names[sub_reg], sub_reg, line_nr, vtemp);
return vtemp;
}
// FIXME locking
void dc11::write_byte(const uint16_t addr, const uint8_t v)
{
uint16_t vtemp = registers[(addr - DC11_BASE) / 2];
if (addr & 1) {
vtemp &= ~0xff00;
vtemp |= v << 8;
}
else {
vtemp &= ~0x00ff;
vtemp |= v;
}
write_word(addr, vtemp);
}
void dc11::write_word(const uint16_t addr, const uint16_t v)
{
int reg = (addr - DC11_BASE) / 2;
int line_nr = reg / 4;
int sub_reg = reg & 3;
std::unique_lock<std::mutex> lck(input_lock[line_nr]);
TRACE("DC11: write register %06o (\"%s\", %d line_nr %d) to %06o", addr, dc11_register_names[sub_reg], sub_reg, line_nr, v);
if (sub_reg == 3) { // transmit buffer
char c = v & 127; // strip parity
if (c <= 32 || c >= 127)
TRACE("DC11: transmit [%d] on line %d", c, line_nr);
else
TRACE("DC11: transmit %c on line %d", c, line_nr);
comm_interfaces.at(line_nr)->send_data(reinterpret_cast<const uint8_t *>(&c), 1);
if (is_tx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, true);
}
registers[reg] = v;
}
JsonDocument dc11::serialize() const
{
JsonDocument j;
JsonDocument j_interfaces;
JsonArray j_interfaces_work = j_interfaces.to<JsonArray>();
for(auto & c: comm_interfaces)
j_interfaces_work.add(c->serialize());
j["interfaces"] = j_interfaces;
for(int regnr=0; regnr<4; regnr++)
j[format("register-%d", regnr)] = registers[regnr];
return j;
}
dc11 *dc11::deserialize(const JsonVariantConst j, bus *const b)
{
std::vector<comm *> interfaces;
JsonArrayConst j_interfaces = j["interfaces"];
for(auto v: j_interfaces)
interfaces.push_back(comm::deserialize(v, b));
dc11 *r = new dc11(b, interfaces);
r->begin();
for(int regnr=0; regnr<4; regnr++)
r->registers[regnr] = j[format("register-%d", regnr)];
return r;
}