-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerial.cpp
More file actions
735 lines (662 loc) · 19.6 KB
/
Serial.cpp
File metadata and controls
735 lines (662 loc) · 19.6 KB
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/* ES40 emulator.
* Copyright (C) 2007-2008 by the ES40 Emulator Project
*
* WWW : http://sourceforge.net/projects/es40
* E-mail : camiel@camicom.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains the code for the emulated Serial Port devices.
*
* $Id: Serial.cpp,v 1.39 2008/03/02 09:42:52 iamcamiel Exp $
*
* X-1.39 Camiel Vanderhoeven 02-MAR-2008
* Natural way to specify large numeric values ("10M") in the config file.
*
* X-1.38 Brian Wheeler 29-FEB-2008
* Restart serial port connection if lost.
*
* X-1.37 Brian Wheeler 27-FEB-2008
* Avoid compiler warnings.
*
* X-1.36 Camiel Vanderhoeven 06-JAN-2008
* Proper interrupt handling.
*
* X-1.35 Camiel Vanderhoeven 02-JAN-2008
* Cleanup.
*
* X-1.34 Camiel Vanderhoeven 30-DEC-2007
* Print file id on initialization.
*
* X-1.33 Camiel Vanderhoeven 28-DEC-2007
* Throw exceptions rather than just exiting when errors occur.
*
* X-1.32 Camiel Vanderhoeven 28-DEC-2007
* Keep the compiler happy.
*
* X-1.31 Camiel Vanderhoeven 17-DEC-2007
* SaveState file format 2.1
*
* X-1.30 Camiel Vanderhoeven 16-DEC-2007
* Added menu option to load state.
*
* X-1.29 Brian Wheeler 10-DEC-2007
* Better exec handling.
*
* X-1.28 Camiel Vanderhoeven 10-DEC-2007
* Use configurator.
*
* X-1.27 Camiel Vanderhoeven 10-NOV-2007
* Add possibility to save system state when not in debug mode.
*
* X-1.26 Camiel Vanderhoeven 09-NOV-2007
* Drop LF when received; OpenVMS expects to receive a CR only on its
* console. This allows entering the password during the OpenVMS 8.3
* installation procedure.
*
* X-1.25 Camiel Vanderhoeven 17-APR-2007
* Allow a telnet client program to be in a directory containing
* spaces on Windows ("c:\program files\putty\putty.exe")
*
* X-1.24 Camiel Vanderhoeven 17-APR-2007
* Only include process.h on Windows.
*
* X-1.23 Camiel Vanderhoeven 16-APR-2007
* Never start a telnet client when running as lockstep slave, because
* we want to receive a connection from the master instead.
*
* X-1.22 Camiel Vanderhoeven 16-APR-2007
* Added possibility to start a Telnet client automatically.
*
* X-1.21 Camiel Vanderhoeven 31-MAR-2007
* Added old changelog comments.
*
* X-1.20 Camiel Vanderhoeven 26-MAR-2007
* Unintentional CVS commit / version number increase.
*
* X-1.19 Camiel Vanderhoeven 27-FEB-2007
* a) Moved tons of defines to telnet.h
* b) When this emulator is the lockstep-master, connect to the slave's
* serial port.
*
* X-1.18 Camiel Vanderhoeven 27-FEB-2007
* Define socklen_t as unsigned int on OpenVMS.
*
* X-1.17 Camiel Vanderhoeven 27-FEB-2007
* Add support for OpenVMS.
*
* X-1.16 Camiel Vanderhoeven 20-FEB-2007
* Use small menu to determine what to do when a <BREAK> is received.
*
* X-1.15 Camiel Vanderhoeven 16-FEB-2007
* Directly use the winsock functions, don't use the CTelnet class
* any more. windows and Linux code are more alike now.
*
* X-1.14 Brian Wheeler 13-FEB-2007
* Formatting.
*
* X-1.13 Camiel Vanderhoeven 12-FEB-2007
* Added comments.
*
* X-1.12 Camiel Vanderhoeven 9-FEB-2007
* Added comments.
*
* X-1.11 Camiel Vanderhoeven 9-FEB-2007
* Enable eating of first characters (needed for now for WINDOWS).
*
* X-1.10 Brian Wheeler 7-FEB-2007
* Disable eating of first characters. Treat Telnet commands properly
* for Linux.
*
* X-1.9 Camiel Vanderhoeven 7-FEB-2007
* Calls to trace_dev now use the TRC_DEVx macro's.
*
* X-1.8 Camiel Vanderhoeven 3-FEB-2007
* a) Restructure Linux/Windows code mixing to make more sense.
* b) Eat first incoming characters (so we don't burden the SRM with
* weird incoming characters.
*
* X-1.7 Camiel Vanderhoeven 3-FEB-2007
* No longer start PuTTy. We might just want to do something wild like
* connecting from a different machine!
*
* X-1.6 Brian Wheeler 3-FEB-2007
* Formatting.
*
* X-1.5 Brian Wheeler 3-FEB-2007
* Get the Telnet port number from the configuration file.
*
* X-1.4 Brian Wheeler 3-FEB-2007
* Add support for Linux.
*
* X-1.3 Brian Wheeler 3-FEB-2007
* 64-bit literals made compatible with Linux/GCC/glibc.
*
* X-1.2 Brian Wheeler 3-FEB-2007
* Includes are now case-correct (necessary on Linux)
*
* X-1.1 Camiel Vanderhoeven 19-JAN-2007
* Initial version in CVS.
*
* \author Camiel Vanderhoeven (camiel@camicom.com / http://www.camicom.com)
**/
#include "StdAfx.h"
#include "Serial.h"
#include "System.h"
#include "AliM1543C.h"
#include "lockstep.h"
#define RECV_TICKS 10
int iCounter = 0;
#define FIFO_SIZE 1024
//#define DEBUG_SERIAL 1
/**
* Constructor.
**/
CSerial::CSerial(CConfigurator * cfg, CSystem * c, u16 number) : CSystemComponent(cfg,c)
{
listenPort = (int)myCfg->get_num_value("port",8000+number);
char s[1000];
char * nargv = s;
int i = 0;
c->RegisterMemory (this, 0, X64(00000801fc0003f8) - (0x100*number), 8);
// Start Telnet server
c->RegisterClock(this, true);
#if defined(_WIN32)
// Windows Sockets only work after calling WSAStartup.
WSADATA wsa;
WSAStartup(0x0101, &wsa);
#endif // defined (_WIN32)
struct sockaddr_in Address;
socklen_t nAddressSize=sizeof(struct sockaddr_in);
listenSocket = (int)socket(AF_INET,SOCK_STREAM,0);
if (listenSocket == INVALID_SOCKET)
{
printf("Could not open socket to listen on!\n");
}
Address.sin_addr.s_addr=INADDR_ANY;
Address.sin_port=htons((u16)(listenPort));
Address.sin_family=AF_INET;
int optval = 1;
setsockopt(listenSocket,SOL_SOCKET,SO_REUSEADDR, (char*)&optval,sizeof(optval));
bind(listenSocket,(struct sockaddr *)&Address,sizeof(Address));
listen(listenSocket, 1);
printf("%s: Waiting for connection on port %d.\n",devid_string,listenPort);
WaitForConnection();
#if defined(IDB) && defined(LS_MASTER)
struct sockaddr_in dest_addr;
int result = -1;
throughSocket = socket(AF_INET, SOCK_STREAM, 0);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons((u16)(base + number));
dest_addr.sin_addr.s_addr = inet_addr(ls_IP);
printf("%s: Waiting to initiate remote connection to %s.\n",devid_string,ls_IP);
while (result == -1)
{
result = connect(throughSocket, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
}
#endif
state.iNumber = number;
state.rcvW = 0;
state.rcvR = 0;
state.bLCR = 0x00;
state.bLSR = 0x60; // THRE, TSRE
state.bMSR = 0x30; // CTS, DSR
state.bIIR = 0x01; // no interrupt
state.irq_active = false;
printf("%s: $Id: Serial.cpp,v 1.39 2008/03/02 09:42:52 iamcamiel Exp $\n",devid_string);
}
/**
* Destructor.
**/
CSerial::~CSerial()
{
}
u64 CSerial::ReadMem(int index, u64 address, int dsize)
{
u8 d;
switch (address)
{
case 0: // data buffer
if (state.bLCR & 0x80)
{
return state.bBRB_LSB;
}
else
{
if (state.rcvR != state.rcvW)
{
state.bRDR = state.rcvBuffer[state.rcvR];
state.rcvR++;
if (state.rcvR == FIFO_SIZE)
state.rcvR = 0;
TRC_DEV4("Read character %02x (%c) on serial port %d\n",state.bRDR,printable(state.bRDR),state.iNumber);
#if defined(DEBUG_SERIAL)
printf("Read character %02x (%c) on serial port %d\n",state.bRDR,printable(state.bRDR),state.iNumber);
#endif
}
else
{
TRC_DEV2("Read past FIFO on serial port %d\n",state.iNumber);
#if defined(DEBUG_SERIAL)
printf("Read past FIFO on serial port %d\n",state.iNumber);
#endif
}
return state.bRDR;
}
case 1:
if (state.bLCR & 0x80)
{
return state.bBRB_MSB;
}
else
{
return state.bIER;
}
case 2: //interrupt cause
d = state.bIIR;
state.bIIR = 0x01;
return d;
case 3:
return state.bLCR;
case 4:
return state.bMCR;
case 5: //serialization state
if (state.rcvR != state.rcvW)
state.bLSR = 0x61; // THRE, TSRE, RxRD
else
state.bLSR = 0x60; // THRE, TSRE
return state.bLSR;
case 6:
return state.bMSR;
default:
return state.bSPR;
}
}
void CSerial::WriteMem(int index, u64 address, int dsize, u64 data)
{
u8 d;
char s[5];
d = (u8)data;
switch (address)
{
case 0:
if (state.bLCR & 0x80) // divisor latch access bit set
{
// LSB of divisor latch
state.bBRB_LSB = d;
}
else
{
// Transmit Hold Register
sprintf(s,"%c",d);
write(s);
TRC_DEV4("Write character %02x (%c) on serial port %d\n",d,printable(d),state.iNumber);
#if defined(DEBUG_SERIAL)
printf("Write character %02x (%c) on serial port %d\n",d,printable(d),state.iNumber);
#endif
eval_interrupts();
}
break;
case 1:
if (state.bLCR & 0x80) // divisor latch access bit set
{
// MSB of divisor latch
state.bBRB_MSB = d;
}
else
{
// Interrupt Enable Register
state.bIER = d;
eval_interrupts();
}
break;
case 2:
state.bFCR = d;
break;
case 3:
state.bLCR = d;
break;
case 4:
state.bMCR = d;
break;
default:
state.bSPR = d;
}
}
void CSerial::eval_interrupts()
{
state.bIIR = 0x01; // no interrupt
if ((state.bIER & 0x01) && (state.rcvR != state.rcvW))
state.bIIR = 0x04;
else if (state.bIER & 0x2) //transmitter buffer empty enabled?
state.bIIR = 0x02; //transmitter buffer empty
else
state.bIIR = 0x01; // no interrupt
if (state.bIIR>0x01)
{
if (!state.irq_active)
theAli->pic_interrupt(0, 4 - state.iNumber);
}
else
{
if (state.irq_active)
theAli->pic_deassert(0, 4 - state.iNumber);
}
}
void CSerial::write(char *s)
{
int val = send(connectSocket,s,(int)strlen(s)+1,0);
}
void CSerial::receive(const char* data)
{
char * x;
x = (char *) data;
while (*x)
{
state.rcvBuffer[state.rcvW++] = *x;
if (state.rcvW == FIFO_SIZE)
state.rcvW = 0;
x++;
eval_interrupts();
}
}
int CSerial::DoClock()
{
fd_set readset;
unsigned char buffer[FIFO_SIZE+1];
unsigned char cbuffer[FIFO_SIZE+1]; // cooked buffer
unsigned char *b, *c;
ssize_t size;
struct timeval tv;
state.serial_cycles++;
if(state.serial_cycles >= RECV_TICKS)
{
FD_ZERO(&readset);
FD_SET(connectSocket,&readset);
tv.tv_sec=0;
tv.tv_usec=0;
if(select(connectSocket+1,&readset,NULL,NULL,&tv) > 0)
{
#if defined(_WIN32)
// Windows Sockets has no direct equivalent of BSD's read
size = recv(connectSocket,(char*)buffer,FIFO_SIZE, 0);
#else
size = read(connectSocket,&buffer,FIFO_SIZE);
#endif
if(size == 0) {
printf("%%SRL-W-DISCONNECT: Write socket closed on other end for serial port %d.\n",state.iNumber);
printf("-SRL-I-WAITFOR: Waiting for a new connection on port %d.\n",listenPort);
WaitForConnection();
return 0;
}
buffer[size+1]=0; // force null termination.
b=buffer;
c=cbuffer;
while((ssize_t)(b - buffer) < size)
{
if (*b == 0x0a)
{
b++; // skip LF
continue;
}
if(*b == IAC)
{
if(*(b+1) == IAC)
{ // escaped IAC.
b++;
}
else if(*(b+1) >= WILL)
{ // will/won't/do/don't
b+=3; // skip this byte, and following two. (telnet escape)
continue;
}
else if(*(b+1) == SB)
{ // skip until IAC SE
b+=2; // now we're at start of subnegotiation.
while(*b!=IAC && *(b+1)!=SE) b++;
b+=2;
continue;
}
else if(*(b+1) == BREAK)
{ // break (== halt button?)
b+=2;
write("\r\n<BREAK> received. What do you want to do?\r\n");
write(" 0. Continue\r\n");
#if defined(IDB)
write(" 1. End run\r\n");
#else
write(" 1. Exit emulator gracefully\r\n");
write(" 2. Abort emulator (no changes saved)\r\n");
write(" 3. Save state to autosave.axp and continue\r\n");
write(" 4. Load state from autosave.axp and continue\r\n");
#endif
for (;;)
{
FD_ZERO(&readset);
FD_SET(connectSocket,&readset);
tv.tv_sec = 60;
tv.tv_usec = 0;
if (select(connectSocket+1,&readset,NULL,NULL,&tv) <= 0)
{
write("%SRL-I-TIMEOUT: no timely answer received. Continuing emulation.\r\n");
return 0;
}
#if defined(_WIN32)
size = recv(connectSocket,(char*)buffer,FIFO_SIZE, 0);
#else
size = read(connectSocket,&buffer,FIFO_SIZE);
#endif
switch (buffer[0])
{
case '0':
write("%SRL-I-CONTINUE: continuing emulation.\r\n");
return 0;
case '1':
write("%SRL-I-EXIT: exiting emulation gracefully.\r\n");
return 1;
case '2':
write("%SRL-I-ABORT: aborting emulation.\r\n");
return -1;
case '3':
write("%SRL-I-SAVESTATE: Saving state to autosave.axp.\r\n");
cSystem->SaveState("autosave.axp");
write("%SRL-I-CONTINUE: continuing emulation.\r\n");
return 0;
case '4':
write("%SRL-I-LOADSTATE: Loading state from autosave.axp.\r\n");
cSystem->RestoreState("autosave.axp");
write("%SRL-I-CONTINUE: continuing emulation.\r\n");
return 0;
}
write("%SRL-W-INVALID: Not a valid answer.\r\n");
}
}
else if(*(b+1) == AYT)
{ // are you there?
}
else
{ // misc single byte command.
b+=2;
continue;
}
}
*c=*b;
c++; b++;
}
*c=0; // null terminate it.
this->receive((const char*)&cbuffer);
}
state.serial_cycles=0;
}
eval_interrupts();
return 0;
}
static u32 srl_magic1 = 0x5A15A15A;
static u32 srl_magic2 = 0x1A51A51A;
/**
* Save state to a Virtual Machine State file.
**/
int CSerial::SaveState(FILE *f)
{
long ss = sizeof(state);
fwrite(&srl_magic1,sizeof(u32),1,f);
fwrite(&ss,sizeof(long),1,f);
fwrite(&state,sizeof(state),1,f);
fwrite(&srl_magic2,sizeof(u32),1,f);
printf("%s: %d bytes saved.\n",devid_string,(int)ss);
return 0;
}
/**
* Restore state from a Virtual Machine State file.
**/
int CSerial::RestoreState(FILE *f)
{
long ss;
u32 m1;
u32 m2;
size_t r;
r = fread(&m1,sizeof(u32),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (m1 != srl_magic1)
{
printf("%s: MAGIC 1 does not match!\n",devid_string);
return -1;
}
fread(&ss,sizeof(long),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (ss != sizeof(state))
{
printf("%s: STRUCT SIZE does not match!\n",devid_string);
return -1;
}
fread(&state,sizeof(state),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
r = fread(&m2,sizeof(u32),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (m2 != srl_magic2)
{
printf("%s: MAGIC 1 does not match!\n",devid_string);
return -1;
}
printf("%s: %d bytes restored.\n",devid_string,(int)ss);
return 0;
}
void CSerial::WaitForConnection()
{
struct sockaddr_in Address;
socklen_t nAddressSize=sizeof(struct sockaddr_in);
char *telnet_options="%c%c%c";
char buffer[8];
char s[1000];
char * nargv = s;
int i = 0;
#if !defined(LS_SLAVE)
char s2[200];
char * argv[20];
strncpy(s, myCfg->get_text_value("action",""),999);
s[999] = '\0';
//printf("%s: Specified : %s\n",devid_string,s);
if (strcmp(s,""))
{
// spawn external program (telnet client)...
while (*nargv)
{
argv[i] = nargv;
if (nargv[0] == '\"')
nargv = strchr(nargv+1,'\"');
if (nargv)
nargv = strchr(nargv,' ');
if (!nargv)
break;
*nargv++ = '\0';
i++;
argv[i] = NULL;
}
argv[i+1] = NULL;
strcpy(s2,argv[0]);
nargv = s2;
if (nargv[0] == '\"')
{
nargv++;
*(strchr(nargv,'\"')) = '\0';
}
//printf("%s: Starting %s\n", devid_string,nargv);
#if defined(_WIN32)
_spawnvp(_P_NOWAIT, nargv, argv);
#else
pid_t child;
int status;
if (!(child=fork())){
execvp(argv[0], argv);
printf("Exec of '%s' failed.\n",argv[0]);
throw((int)1);
} else {
sleep(1); // give it a chance to start up.
waitpid(child,&status,WNOHANG); // reap it, if needed.
if(kill(child,0) < 0) { // uh oh, no kiddo.
printf("%%SRL-F-EXEC: Exec of '%s' has failed.\n",argv[0]);
throw((int)1);
}
}
#endif
}
#endif
Address.sin_addr.s_addr=INADDR_ANY;
Address.sin_port=htons((u16)listenPort);
Address.sin_family=AF_INET;
// Wait until we have a connection
connectSocket = INVALID_SOCKET;
while (connectSocket == INVALID_SOCKET)
{
connectSocket = (int)accept(listenSocket,(struct sockaddr*)&Address,&nAddressSize);
}
state.serial_cycles = 0;
// Send some control characters to the telnet client to handle
// character-at-a-time mode.
sprintf(buffer,telnet_options,IAC,DO,TELOPT_ECHO);
this->write(buffer);
sprintf(buffer,telnet_options,IAC,DO,TELOPT_NAWS);
write(buffer);
sprintf(buffer,telnet_options,IAC,DO,TELOPT_LFLOW);
this->write(buffer);
sprintf(buffer,telnet_options,IAC,WILL,TELOPT_ECHO);
this->write(buffer);
sprintf(buffer,telnet_options,IAC,WILL,TELOPT_SGA);
this->write(buffer);
sprintf(s,"This is serial port #%d on AlphaSim\r\n",state.iNumber);
this->write(s);
}