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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
// license:LGPL-2.1+
// copyright-holders:Michael Zapf
/****************************************************************************
TIPI adapter card
designed and realized by Matthew Splett (aka jedimatt42), Peter and others
The TIPI system (pronouced "tipee") is an expansion for the TI-99/4A and
other computers of this family. It consists of a Raspberry Pi computer
attached to an adapter card that fits into the PEB (Peripheral Expansion
Box) or to the console side connector.
The adapter card provides an interface between the TI system and the
Raspberry Pi, in particular for exchanging files. More than that, features
of the Raspberry like network connectivity, may be offered to the TI
system.
The general concept is that there is a set of memory-mapped ports between
the TI and the Raspberry Pi.
0x5ff9 (RCIN): PI Control Signal (input from RPi)
0x5ffb (RDIN): PI Data (input)
0x5ffd (TCOUT): TI Control Signal (output to RPi)
0x5fff (TDOUT): TI Data (output)
CRU map
-------
1x00: Activate the card and its ROM (x=0...f)
1x02: Send RESET to Raspberry Pi
Raspberry connection
--------------------
The good thing in terms of emulation is that the Raspberry Pi is completely
outside of the emulation. It allows for connecting via a WebSocket, which
is especially provided for emulators. In the real machine, the Raspberry Pi
gets the data from the adapter card.
It is recommended to run a virtual Raspberry Pi as a QEMU image. An image
is available on the TIPI website.
The connection is specified by -conn rpi.<hostname>[:<port>],
like "-conn rpi.localhost" or "-conn rpi.some.host.net"
This will create a file in the current directory of that name, which is
a side effect of the MAME image handling. The port default is 9901.
Mouse support
-------------
The websocket server accepts messages for mouse movements and button actions
from the mouse input of the connected emulator. However, in MAME, mouse
input is only available if there is a proper mouse device, like the busmouse
or the Mechatronics mouse. Hence, we cannot send any mouse input to the
Raspberry without one of these mice connected.
However, if a mouse is connected to the real or virtual Raspberry Pi,
the mouse operations can be polled from the Raspberry in the
intended way.
Logging
-------
Since we use a background thread for the websocket client, and logging
is not thread-safe, log output may look broken at some times.
Michael Zapf
March 20222
References
[1] TIPI description on Github: https://github.com/jedimatt42/tipi
*****************************************************************************/
#include "emu.h"
#include "tipi.h"
#define LOG_WARN (1U<<1)
#define LOG_EPROM (1U<<2)
#define LOG_CRU (1U<<3)
#define LOG_PORTS (1U<<4)
#define LOG_CONFIG (1U<<5)
#define LOG_RPI (1U<<6)
#define LOG_QUEUE (1U<<7)
#define LOG_PROT (1U<<8)
#define VERBOSE ( LOG_GENERAL | LOG_WARN | LOG_CONFIG )
#define RASPI "rpi"
#include "logmacro.h"
DEFINE_DEVICE_TYPE(TI99_TIPI, bus::ti99::peb::tipi_card_device, "ti99_tipi", "TIPI card")
DEFINE_DEVICE_TYPE(TI99_TIPI_RPI, bus::ti99::peb::tipi_attached_device, "ti99_tipi_atttached", "Connection to external device")
namespace bus::ti99::peb {
tipi_card_device::tipi_card_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
device_t(mconfig, TI99_TIPI, tag, owner, clock),
device_ti99_peribox_card_interface(mconfig, *this),
m_rpi(*this, RASPI),
m_address(0),
m_dsr(false),
m_portaccess(false),
m_waitinit(false),
m_rpiconn(false),
m_tc(0),
m_lasttc(0)
{
}
void tipi_card_device::setaddress_dbin(offs_t offset, int state)
{
if (machine().side_effects_disabled()) return;
m_address = offset;
m_dsr = false;
m_portaccess = false;
if (in_dsr_space(offset, true) && m_selected)
{
m_portaccess = ((offset & 0x1ff8)==0x1ff8);
m_dsr = !m_portaccess;
}
}
void tipi_card_device::readz(offs_t offset, uint8_t *value)
{
if (machine().side_effects_disabled())
{
debug_read(offset, value);
return;
}
if (m_dsr)
{
// Lines A0-A12 directly connected to the EPROM (chip pin order)
// Lines A13-A15 connected to PLD (currently not used)
int base = 0;
uint8_t* rom = &m_eprom[base | (m_address & 0x1fff)];
*value = *rom;
if (WORD_ALIGNED(m_address))
{
// Do logging by 16 bit words, as we typically have instructions in
// the eprom
uint16_t val = (*rom << 8) | (*(rom+1));
LOGMASKED(LOG_EPROM, "DSR: %04x -> %04x\n", m_address & 0xffff, val);
}
}
if (m_portaccess)
{
if ((m_address & 5)==1)
{
int val = 0;
if (m_address & 2)
{
val = m_indqueue.front();
m_indqueue.pop();
LOGMASKED(LOG_PORTS, "RDIN -> %02x\n", val);
}
else
{
val = m_rc;
LOGMASKED(LOG_PORTS, "RCIN -> %02x\n", val);
}
*value = val;
}
}
}
void tipi_card_device::write(offs_t offset, uint8_t data)
{
if (machine().side_effects_disabled())
{
debug_write(offset, data);
return;
}
if (m_dsr)
{
LOGMASKED(LOG_WARN, "Writing to DSR space: %04x <- %02x\n", m_address & 0xffff, data);
}
if (m_portaccess)
{
if (m_address & 1)
{
if (m_address & 2)
{
LOGMASKED(LOG_PORTS, "TDOUT <- %02x\n", data);
set_td(data);
}
else
{
LOGMASKED(LOG_PORTS, "TCOUT <- %02x\n", data);
set_tc(data);
}
}
}
}
void tipi_card_device::set_td(u8 data)
{
m_td = data;
}
void tipi_card_device::set_tc(u8 data)
{
if (m_tc != data)
{
m_tc = data;
m_lasttc = 0;
process_message();
}
}
/*
Debugger access.
*/
void tipi_card_device::debug_read(offs_t offset, uint8_t* value)
{
int base = 0;
if (in_dsr_space(offset, true) && m_selected)
{
if ((offset & 0x1ff8)!=0x1ff8) // not in mapping area
{
uint8_t* rom = &m_eprom[base | (offset & 0x1fff)];
*value = *rom;
}
}
}
/*
Debugger access.
*/
void tipi_card_device::debug_write(offs_t offset, uint8_t data)
{
// No write allowed
}
/*
CRU read access
*/
void tipi_card_device::crureadz(offs_t offset, uint8_t *value)
{
// No CRU read
}
/*
CRU write access.
*/
void tipi_card_device::cruwrite(offs_t offset, uint8_t data)
{
if ((offset & 0xff00) == m_cru_base)
{
int bit = (offset >> 1) & 0x1f;
switch (bit)
{
case 0: // card activation
LOGMASKED(LOG_CRU, "DSR %s\n", (data!=0)? "on" : "off");
m_selected = (data != 0);
break;
case 1:
LOGMASKED(LOG_CRU, "CRU Bit 1 %s\n", (data!=0)? "on" : "off");
if (data != 0) send("RESET");
break;
default:
LOGMASKED(LOG_CRU, "Unknown CRU Bit %d\n", bit);
break;
}
}
}
void tipi_card_device::send(const char* message)
{
if (m_rpiconn)
{
*m_send_stream << message;
m_wsclient->send(m_send_stream);
}
}
void tipi_card_device::send(u8* message, int len)
{
if (m_rpiconn)
{
for (int i=0; i < len; i++)
{
LOGMASKED(LOG_RPI, "Sending byte %02x\n", message[i]);
*m_send_stream << message[i];
}
m_wsclient->send(m_send_stream, nullptr, 130); // binary (10000010)
}
}
enum
{
TSWB = 0x02,
TSRB = 0x06,
TSRSET = 0xf1
};
/*
Messages from server:
"RD=xxx"
"RC=xxx"
processMessage occurs for each incoming message
and each time that TC has been changed
Write a message to RPi:
for i=-2..(n-1)
Write byte to TD
Write (02 | (i & 1)) to TC
Byte sequence: len1 len2 m0 m1 m2 m3 ... m(n-1), where n = len1<<8 | len2;
Message will be written to RPi when complete
Read a message from RPi:
n = message.length
n = n1<<8 | n2
Write 06 to TC
n1 = Read byte from TD
Write 07 to TC
n2 = Read byte from TD
for i=0..(n-1)
Write (06 | (i & 1)) to TC
Read byte from TD
*/
void tipi_card_device::process_message()
{
if (m_tc == TSRSET) // Reset command
{
LOGMASKED(LOG_PROT, "Reset\n");
m_rpimessage = nullptr;
m_msgindex = -2;
m_rc = m_tc;
m_lasttc = 0;
if (m_syncmode)
{
LOGMASKED(LOG_PROT, "Sending SYNC\n");
send("SYNC"); // only for SYNC mode
}
}
else
{
if (m_lasttc != m_tc)
{
if ((m_tc & 0xfe) == TSWB) // 02 and 03: write byte (LSB as strobe)
{
switch (m_msgindex)
{
case -2:
m_msglength = m_td << 8;
break;
case -1:
m_msglength |= m_td;
m_rpimessage = std::make_unique<u8[]>(m_msglength);
break;
default:
if (m_rpimessage != nullptr)
m_rpimessage[m_msgindex] = m_td;
}
m_msgindex++;
if (m_msgindex == m_msglength)
{
// Message is complete - transmit it
LOGMASKED(LOG_RPI, "Sending message, length %d\n", m_msglength);
send(m_rpimessage.get(), m_msglength);
m_rpimessage = nullptr;
}
else
m_lasttc = m_tc;
m_rc = m_tc; // Auto-acknowledge
}
else
{
if ((m_tc & 0xfe) == TSRB) // 06 and 07: read byte (LSB as strobe)
{
if (!m_indqueue.empty())
{
m_rc = m_tc; // Auto-acknowledge
m_lasttc = m_tc;
}
}
}
}
}
}
/*
Syntax for connecting is
-conn rpi.<host>[:<port>]
Default for port is 9901.
*/
TIMER_CALLBACK_MEMBER(tipi_card_device::open_websocket)
{
if (m_rpiconn)
{
// We are just interested in the file name
// Cannot call it "socket.xxx" because this will make sdlsocket open
// the connection
std::string sconn(m_rpi->filename());
int pos = sconn.find("rpi.");
if (pos != -1)
{
std::string shost(sconn.substr(pos + 4));
if (shost.find(":")==-1)
shost += ":9901";
shost += "/tipi";
LOG("Trying to connect to Raspberry Pi at %s\n", shost.c_str());
m_wsclient = std::make_unique<webpp::ws_client>(shost);
webpp::ws_client* wsc = m_wsclient.get();
wsc->on_open =
[this]()
{ websocket_opened(); };
wsc->on_message =
[this](std::shared_ptr<webpp::ws_client::Message> message)
{ websocket_incoming(message); };
wsc->on_close =
[this](int i, const std::string& msg)
{ websocket_closed(i, msg); };
wsc->on_error =
[this](const std::error_code& code)
{ websocket_error(code); };
m_send_stream = std::make_shared<webpp::ws_client::SendStream>();
asio::thread listener([this]()
{ m_wsclient->start(); });
}
else
fatalerror("Invalid TIPI connection address. Syntax: -conn rpi.<host>[:<port>]\n");
}
}
void tipi_card_device::websocket_opened()
{
LOG("Connection established\n");
m_connected = true;
m_rc = 0;
if (m_waitinit) m_slot->set_ready(ASSERT_LINE);
}
void tipi_card_device::websocket_incoming(std::shared_ptr<webpp::ws_client::Message> message)
{
switch (message->fin_rsv_opcode & 0x0f)
{
case 1:
// Text message is "RD=xxx" or "RC=xxx" with xxx=decimal number
LOGMASKED(LOG_RPI, "Got text message: %s\n", message->string());
if (message->string().find("RD=")==0)
{
m_indqueue.push(std::stoi(message->string().substr(3)));
}
if (message->string().find("RC=")==0)
{
m_rc = std::stoi(message->string().substr(3));
}
break;
case 2:
LOGMASKED(LOG_RPI, "Got binary message, length %d\n", message->size());
// Enqueue the incoming message
// as n1 n2 msg, where n1 and n2 are the length of the message,
// and msg is the byte sequence of the message.
{
u8 len1 = (message->size()>>8) & 0xff;
u8 len2 = message->size() & 0xff;
m_indqueue.push(len1);
LOGMASKED(LOG_QUEUE, "<- %02x (len1) [%d]\n", len1, m_indqueue.size());
m_indqueue.push(len2);
LOGMASKED(LOG_QUEUE, "<- %02x (len2) [%d]\n", len2, m_indqueue.size());
}
for (int i=0; i < message->size(); i++)
{
u8 msgbyte = (u8)message->get();
m_indqueue.push(msgbyte);
LOGMASKED(LOG_QUEUE, "<- %02x [%d]\n", msgbyte, m_indqueue.size());
}
process_message();
break;
default:
LOGMASKED(LOG_RPI, "Got unknown message\n");
break;
}
}
void tipi_card_device::websocket_error(const std::error_code& code)
{
if (code.value() == std::make_error_code(std::errc::connection_reset).value())
{
m_wsclient->stop();
if (m_attempts > 0)
{
m_attempts--;
LOGMASKED(LOG_WARN, "Error, connection reset. Retrying.\n");
m_restart_timer->adjust(attotime::from_msec(2500));
}
else
{
LOGMASKED(LOG_WARN, "Connection has been reset several times; giving up\n");
m_connected = false;
}
}
else
{
// End-of-file (2) occurs when the Pi is rebooted
LOGMASKED(LOG_WARN, "Got error: %s (%d)\n", code.message().c_str(), code.value());
// Reconnection is not implemented on the Pi side for emulations,
// so this may cause the config program to wait forever
// Needs to be documented
m_restart_timer->adjust(attotime::from_msec(20000));
m_attempts = 20;
}
}
void tipi_card_device::websocket_closed(int i, const std::string& msg)
{
LOG("Closing connection\n");
if (m_waitinit) m_slot->set_ready(CLEAR_LINE);
// Open it again after 3 secs
m_wsclient->stop();
m_restart_timer->adjust(attotime::from_msec(3000));
m_attempts = 5;
}
void tipi_card_device::device_start()
{
m_eprom = memregion(TI99_DSRROM)->base();
save_item(NAME(m_address));
save_item(NAME(m_dsr));
save_item(NAME(m_portaccess));
save_item(NAME(m_msgindex));
save_item(NAME(m_msglength));
save_item(NAME(m_attempts));
save_item(NAME(m_connected));
save_item(NAME(m_tc));
save_item(NAME(m_td));
save_item(NAME(m_rc));
save_item(NAME(m_lasttc));
m_restart_timer = timer_alloc(FUNC(tipi_card_device::open_websocket), this);
m_rpiconn = (m_rpi->exists());
}
void tipi_card_device::device_reset()
{
m_cru_base = (ioport("SW1")->read()) << 8;
m_waitinit = (ioport("WAIT")->read()!=0);
m_syncmode = (ioport("MODE")->read()!=0);
m_address = 0;
m_dsr = false;
m_portaccess = false;
if (!m_rpiconn) LOGMASKED(LOG_CONFIG, "No Raspberry Pi connected\n");
open_websocket(0);
m_connected = false;
m_lasttc = 0;
m_attempts = 5;
}
void tipi_card_device::device_stop()
{
// MZ: Without this I'm getting segfaults/list corruption
// when leaving the emulation
m_wsclient = NULL;
LOG("Stopping TIPI\n");
}
/*
The CRU address base for the card.
*/
INPUT_PORTS_START( tipi )
PORT_START("SW1")
PORT_DIPNAME(0x1f, 0x10, "CRU base")
PORT_DIPSETTING(0x10, "1000") // Default setting
PORT_DIPSETTING(0x11, "1100")
PORT_DIPSETTING(0x12, "1200")
PORT_DIPSETTING(0x13, "1300")
PORT_DIPSETTING(0x14, "1400")
PORT_DIPSETTING(0x15, "1500")
PORT_DIPSETTING(0x16, "1600")
PORT_DIPSETTING(0x17, "1700")
PORT_DIPSETTING(0x18, "1800")
PORT_DIPSETTING(0x19, "1900")
PORT_DIPSETTING(0x1a, "1a00")
PORT_DIPSETTING(0x1b, "1b00")
PORT_DIPSETTING(0x1c, "1c00")
PORT_DIPSETTING(0x1d, "1d00")
PORT_DIPSETTING(0x1e, "1e00")
PORT_DIPSETTING(0x1f, "1f00")
PORT_START("WAIT")
PORT_CONFNAME(0x01, 0x00, "Wait for initialization")
PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
PORT_CONFSETTING( 0x01, DEF_STR( On ) )
PORT_START("MODE")
PORT_CONFNAME(0x01, 0x00, "Transfer mode")
PORT_CONFSETTING( 0x00, "async" )
PORT_CONFSETTING( 0x01, "sync" )
INPUT_PORTS_END
void tipi_card_device::device_add_mconfig(machine_config &config)
{
TI99_TIPI_RPI(config, m_rpi, 0);
}
ROM_START( tipi )
ROM_REGION(0x8000, TI99_DSRROM, 0)
ROM_LOAD("tipidsr.u2", 0x0000, 0x8000, CRC(a54e65af) SHA1(f930e4b079a5b6b24dc20f262e9c599d2051b6be))
ROM_END
const tiny_rom_entry *tipi_card_device::device_rom_region() const
{
return ROM_NAME( tipi );
}
ioport_constructor tipi_card_device::device_input_ports() const
{
return INPUT_PORTS_NAME( tipi );
}
/****************************************************************************/
/* Connection to the external device, a Raspberry PI */
tipi_attached_device::tipi_attached_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
: device_t(mconfig, TI99_TIPI_RPI, tag, owner, clock),
device_image_interface(mconfig, *this)
{
}
/*
Initialize connection
*/
image_init_result tipi_attached_device::call_load()
{
return image_init_result::PASS; // OK
}
void tipi_attached_device::call_unload()
{
}
} // end namespace bus::ti99::peb
|