-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmsufrend.c
3308 lines (2972 loc) · 105 KB
/
msufrend.c
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------
**
** Written by Mark Riordan, and placed in the public domain. June 2004
** Version 2 done in February 2005.
**
** Version 3 done in August 2022 by Kevin Jordan. The Frend2 functionality
** was integrated into the DtCyber executable instead of running as a
** separate process.
**
** Name: msufrend.c
**
** Description:
** Perform emulation of Michigan State University's FREND
** interactive front-end. The real FREND ran on an Interdata 7/32,
** but we are emulating only the functionality of FREND and not
** the 7/32 instruction set.
**
** This module emulates the functions of the 6000 Channel Adapter (6CA),
** a custom piece of hardware designed at MSU which allows a 6000 PP to
** do DMA to the 7/32. An array of bytes implements the 7/32's memory.
** Like the original 6CA, DtCyber reads and writes FREND memory without
** disturbing frend3, and issues an interrupt request when it wants
** frend3 to process commands written into the memory.
**
** On the deadstart tape Ken Hunter gave to Mark Riordan 5 June 2004,
** FREND is Equipment Status Table (EST) entry 70 octal, equip 0, unit 0,
** channel 2, DST 24.
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#if defined(_WIN32)
#include <windows.h>
#else
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#endif
#include "const.h"
#include "proto.h"
#include "types.h"
#include "msufrend_lmbi.h"
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** Function codes.
*/
#define FcFEFSEL 02400 /* SELECT 6000 CHANNEL ADAPTER */
#define FcFEFDES 02410 /* DESELECT 6000 CHANNEL ADAPTER */
#define FcFEFST 00000 /* READ 6CA STATUS */
#define FcFEFSAU 01000 /* SET ADDRESS (UPPER) */
#define FcFEFSAM 01400 /* SET ADDRESS (MIDDLE) */
#define FcFEFHL 03000 /* HALT-LOAD THE 7/32 */
#define FcFEFINT 03400 /* INTERRUPT THE 7/32 */
#define FcFEFLP 06000 /* LOAD INTERFACE MEMORY */
#define FcFEFRM 04400 /* READ */
#define FcFEFWM0 07000 /* WRITE MODE 0 */
#define FcFEFWM 07400 /* WRITE MODE 1 */
#define FcFEFRSM 05000 /* READ AND SET */
#define FcFEFCI 00400 /* CLEAR INITIALIZED STATUS BIT */
/*
** Commands from 1FP to FREND.
*/
#define FC_ITOOK 1
#define FC_HI80 2
#define FC_HI240 3
#define FC_CPOP 4
#define FC_CPGON 5
/*
** FREND 6000 Channel Adapter bits, for function FcFEFST
*/
#define FCA_STATUS_INITIALIZED 04000
#define FCA_STATUS_NON_EXIST_MEM 02000
#define FCA_STATUS_LAST_BYTE_NO_ERR 00000
#define FCA_STATUS_LAST_BYTE_PAR_ERR 00400
#define FCA_STATUS_LAST_BYTE_MEM_MAL 01000
#define FCA_STATUS_LAST_BYTE_NON_EXIST 01400
#define FCA_STATUS_MODE_WHEN_ERROR 00200
#define FCA_STATUS_READ_WHEN_ERROR 00100
#define FCA_STATUS_WRITE_WHEN_ERROR 00040
#define FCA_STATUS_HALT_LOADING 00020
#define FCA_STATUS_INT_PENDING 00010
#define DEFAULT_MAX_CONNECTIONS 8
#define DEFAULT_TCP_PORT 6500
#define IoTurnsPerPoll 4
#define MIN_FREE_PORT_BUFFERS 2
/*
** It appears that the Cyber never tries to access memory beyond this.
*/
#define MAX_FREND_BYTES 0xc0000
/* hard-coded port numbers for initial implementation. */
#define FPORTCONSOLE 4 /* must be greater than PTN.MAX */
#define RESERVED_PORTS 4
#define FIRSTUSERPORT (RESERVED_PORTS+1)
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/* Interdata 7/32 types */
#define ByteAddr u32
#define FrendAddr u32
#define FullWord u32
#define HalfWord u16
/* Compute FWA of socket entry given port number */
#define portNumToFwa(portNum) (activeFrend->fwaPORT + (portNum - 1) * LE_PORT)
#define sockNumToFwa(sockNum) (activeFrend->fwaSOCK + (sockNum - 1) * LE_SOCK)
#if !defined(_WIN32)
#define SOCKET int
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef enum telnetState
{
TELST_NORMAL, TELST_GOT_IAC, TELST_GOT_WILL_OR_SIMILAR
} TelnetState;
#define TELCODE_IAC 0xff
#define TELCODE_DONT 0xfe
#define TELCODE_DO 0xfd
#define TELCODE_WONT 0xfc
#define TELCODE_WILL 0xfb
#define TELCODE_OPT_ECHO 0x01
#define TELCODE_OPT_SUPPRESS_GO_AHEAD 0x03
typedef struct pendingBuffer
{
u8 buf[L_LINE + 16]; /* Waiting chars */
int first; /* Index of first char still pending */
int charsLeft; /* # of chars remaining in buffer */
} PendingBuffer;
typedef struct portContext
{
int id;
struct frendContext *frend; /* pointer to supporting FREND context */
bool active; /* TRUE if port is connected and active */
SOCKET fd; /* TCP socket descriptor */
TelnetState telnetState; /* telnet state */
bool EOLL; /* TRUE if last line ended in end of line */
PendingBuffer pbuf; /* chars pending output. Normally, this is 0 */
/* except when assembling bytes to be sent. */
/* If non-zero, then we shouldn't send any */
/* more lines until this buffer is sent. */
} PortContext;
typedef struct frendContext
{
struct frendContext *next;
int listenPort;
SOCKET listenFd;
int portCount;
bool doesTelnet; /* TRUE if Telnet protocol enabled */
int ioTurns;
u8 channelNo;
u8 eqNo;
u8 unitNo;
PortContext *ports; /* one per supported terminal */
ByteAddr addr; /* Next byte (not halfword) address to read or write.
* However, when this is set via the 6CA, the bottom
* bit is cleared, because the memory interface between
* FREND and the Cyber specifies addresses halfword addresses */
bool nextIsSecond; /* true if the next byte of I/O is the second in a sequence.
* This is used for READ-AND-SET, which transfers 2 bytes
* but which is not supposed to change the address register. */
u8 mem[MAX_FREND_BYTES]; /* Contents of FREND memory, in bytes.
* The 7/32 stored in most-significant-byte-first format */
FrendAddr fwaMISC;
FrendAddr fwaFPCOM;
FrendAddr fwaBF80;
FrendAddr fwaBF240;
FrendAddr fwaBFREL;
FrendAddr fwaBANM;
FrendAddr fwaLOGM;
FrendAddr fwaSOCK;
FrendAddr fwaDVSK;
FrendAddr fwaPORT;
FrendAddr fwaPTBUF;
FrendAddr fwaMALC;
FrendAddr fwaALLOC;
FrendAddr fwaBuffers80;
FrendAddr fwaBuffers240;
} FrendContext;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static void msufrendCheckIo(void);
static FcStatus msufrendFunc(PpWord funcCode);
static void msufrendIo(void);
static void msufrendActivate(void);
static void msufrendDisconnect(void);
#if DEBUG
static char *msufrendFunc2String(PpWord funcCode);
#endif
static void handleInterruptFromHost(void);
static HalfWord initCircList(FrendAddr fwaList, HalfWord nslots);
static void initLmbi(void);
static void initPortBufs(void);
static void setHalfWord(FrendAddr addr, HalfWord half);
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
static FrendContext *activeFrend = NULL;
static FrendContext *firstFrend = NULL;
static FrendContext *lastFrend = NULL;
#if DEBUG
static FILE *msufrendLog = NULL;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise the MSUFREND device.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** params optional device parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void msufrendInit(u8 eqNo, u8 unitNo, u8 channelNo, char *params)
{
#if defined(_WIN32)
u_long blockEnable = 1;
#endif
char connType[16];
DevSlot *dp;
FrendAddr fwaThisSock;
FrendAddr fwaListSock;
u8 i;
bool isTelnet;
int listenPort;
int numParam;
int portCount;
PortContext *pp;
#if DEBUG
if (msufrendLog == NULL)
{
msufrendLog = fopen("msufrendlog.txt", "wt");
}
#endif
dp = channelAttach(channelNo, eqNo, DtMSUFrend);
dp->activate = msufrendActivate;
dp->disconnect = msufrendDisconnect;
dp->func = msufrendFunc;
dp->io = msufrendIo;
dp->selectedUnit = unitNo;
if (dp->context[0] != NULL)
{
fputs("(msufrend) Only one unit is possible per equipment\n", stderr);
exit(1);
}
if (params == NULL)
{
params = "";
}
numParam = sscanf(params, "%d,%d,%s", &listenPort, &portCount, connType);
if (numParam < 1)
{
listenPort = DEFAULT_TCP_PORT;
portCount = DEFAULT_MAX_CONNECTIONS;
isTelnet = TRUE;
}
else if (numParam < 2)
{
portCount = DEFAULT_MAX_CONNECTIONS;
isTelnet = TRUE;
}
else if (strcasecmp(connType, "telnet") == 0)
{
isTelnet = TRUE;
}
else if (strcasecmp(connType, "raw") == 0)
{
isTelnet = FALSE;
}
else
{
fprintf(stderr, "(msufrend) Invalid connection type: %s, not one of 'telnet' or 'raw'.\n", connType);
exit(1);
}
if ((listenPort < 1) || (listenPort > 65535))
{
fprintf(stderr, "(msufrend) Invalid TCP port number: %d\n", listenPort);
exit(1);
}
if (portCount < 1)
{
fprintf(stderr, "(msufrend) Invalid port count: %d\n", portCount);
exit(1);
}
activeFrend = (FrendContext *)calloc(1, sizeof(FrendContext));
if (activeFrend == NULL)
{
fputs("(msufrend) Failed to allocate context block\n", stderr);
exit(1);
}
dp->context[0] = activeFrend;
if (firstFrend == NULL)
{
firstFrend = activeFrend;
}
else
{
lastFrend->next = activeFrend;
}
lastFrend = activeFrend;
activeFrend->listenPort = listenPort;
activeFrend->portCount = portCount + RESERVED_PORTS;
activeFrend->doesTelnet = isTelnet;
activeFrend->channelNo = channelNo;
activeFrend->eqNo = eqNo;
activeFrend->unitNo = unitNo;
activeFrend->ioTurns = IoTurnsPerPoll - 1;
activeFrend->ports = (PortContext *)calloc(activeFrend->portCount, sizeof(PortContext));
if (activeFrend->ports == NULL)
{
fputs("(msufrend) Failed to allocate port context blocks\n", stderr);
exit(1);
}
initLmbi();
/*
** Initialise port context blocks.
*/
for (i = 0, pp = activeFrend->ports; i < activeFrend->portCount; i++, pp++)
{
pp->id = i + 1;
pp->frend = activeFrend;
pp->active = FALSE;
pp->EOLL = FALSE;
fwaThisSock = portNumToFwa(pp->id);
fwaListSock = fwaThisSock + W_SKOTCL;
/* Initialize the circular list, which is actually part of the socket entry. */
initCircList(fwaListSock, L_SKOCL);
setHalfWord(fwaThisSock + H_SKNUM, pp->id);
}
initPortBufs();
setHalfWord(H_INICMP, 1); /* Initialization complete */
/*
** Create socket, bind to specified port, and begin listening for connections
*/
activeFrend->listenFd = netCreateListener(activeFrend->listenPort);
#if defined(_WIN32)
if (activeFrend->listenFd == INVALID_SOCKET)
#else
if (activeFrend->listenFd == -1)
#endif
{
fprintf(stderr, "(msufrend) Can't create socket on port %d\n", activeFrend->listenPort);
exit(1);
}
/*
** Print a friendly message.
*/
printf("(msufrend) initialised on channel %o equipment %o, ports %d, TCP port %d\n",
channelNo, eqNo, portCount, activeFrend->listenPort);
}
/*--------------------------------------------------------------------------
** Purpose: Show mux status (operator interface).
**
** Parameters: Name Description.
**
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void msufrendShowStatus()
{
FrendContext *fp;
int i;
char outBuf[200];
PortContext *pp;
for (fp = firstFrend; fp != NULL; fp = fp->next)
{
if (fp->listenFd > 0)
{
sprintf(outBuf, " > %-8s C%02o E%02o ", "MSUFrEnd", fp->channelNo, fp->eqNo);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(fp->listenFd), "", "frend", "listening");
opDisplay(outBuf);
for (i = 0, pp = fp->ports; i < fp->portCount; i++, pp++)
{
if (pp->active && pp->fd > 0)
{
sprintf(outBuf, "(msu ) P%02o ", pp->id);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(pp->fd), netGetPeerTcpAddress(pp->fd), "frend", "connected");
opDisplay(outBuf);
}
}
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on MSU FREND.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus msufrendFunc(PpWord funcCode)
{
u8 data = funcCode & 0xff;
activeFrend = (FrendContext *)activeDevice->context[0];
/* Mask off top 4 bits of the PP word. This contains the function code,
* more or less. In some cases, the bottom 8 bits contain data related
* to the function--typically address bits to set.
*/
activeDevice->fcode = funcCode & 07400;
#if DEBUG
fprintf(msufrendLog, "\n%010u PP:%02o CH:%02o P:%04o f:%04o T:%s",
traceSequenceNo, activePpu->id, activeDevice->channel->id,
activePpu->regP, funcCode, msufrendFunc2String(activeDevice->fcode));
#endif
switch (activeDevice->fcode)
{
case FcFEFSEL: /* SELECT 6000 CHANNEL ADAPTER */
case FcFEFDES: /* DESELECT 6000 CHANNEL ADAPTER */
/* DESELECT is a special case--must recheck original parameter. */
if (FcFEFDES == funcCode)
{
activeDevice->fcode = funcCode;
return FcProcessed;
}
/* Technically, this is not correct, because SELECT (2400) and
* DESELECT (2410) have the same top 4 bits.
* But I don't think it matters. */
break;
case FcFEFST:
break;
case FcFEFSAU:
/* SET ADDRESS (UPPER) - Set upper 3 bits of 19-bit address */
activeFrend->addr = (activeFrend->addr & 0x1fffe) | ((data & 7) << 17);
#if DEBUG
fprintf(msufrendLog, " data:%02x", data);
#endif
return FcProcessed;
case FcFEFSAM:
/* SET ADDRESS (MIDDLE) - Set middle byte of address, bits 2**8 thru 2**15 */
activeFrend->addr = (activeFrend->addr & 0xfe01ff) | (((u32)data) << 9);
#if DEBUG
fprintf(msufrendLog, " data:%02x", data);
#endif
return FcProcessed;
case FcFEFHL:
/* Halt-Load the 7/32 */
return FcProcessed;
case FcFEFINT: /* Interrupt the 7/32 */
handleInterruptFromHost();
return FcProcessed;
case FcFEFLP: /* LOAD INTERFACE MEMORY */
/* Prepare to accept 8-bit bytes, to be written in "a 16-byte memory"
* starting at location 0. */
activeFrend->addr = 0;
return FcProcessed;
case FcFEFRM:
/* READ - I think the data byte is the lower 8 bits of the address. */
activeFrend->addr = (activeFrend->addr & 0x1fffe00) | ((u32)(data << 1));
#if DEBUG
fprintf(msufrendLog, " data:%02x addr:%05x <-", data, activeFrend->addr);
#endif
break;
case FcFEFWM0:
/* WRITE MODE 0 - One PP word considered as 2 6-bit bytes, written to a 16-bit FE word. */
activeFrend->addr = (activeFrend->addr & 0x1fffe00) | ((u32)(data << 1));
#if DEBUG
fprintf(msufrendLog, " data:%02x addr:%05x ->", data, activeFrend->addr);
#endif
break;
case FcFEFWM:
/* WRITE MODE 1 - Two consecutive PP words (8 in 12) written to a 16-bit FE word. */
/* First PP word goes to upper 8 bits of FE word, confusingly called bits 0-7. */
activeFrend->addr = (activeFrend->addr & 0x1fffe00) | ((u32)(data << 1));
#if DEBUG
fprintf(msufrendLog, " data:%02x addr:%05x ->", data, activeFrend->addr);
#endif
break;
case FcFEFRSM:
/* READ AND SET - test and set on single 16-bit location. */
/* Mode always forced to 1 so exactly 2 bytes of data will be sent to PPU. */
/* After second byte, channel is empty until transfer terminated */
/* by the PPU with a DCN. Address register is not changed, says */
/* the 6CA hardware doc. */
/* Apparently the top bit is set. */
activeFrend->addr = (activeFrend->addr & 0x1fffe00) | ((u32)(data << 1));
activeFrend->nextIsSecond = FALSE;
#if DEBUG
fprintf(msufrendLog, " data:%02x addr:%05x <-", data, activeFrend->addr);
#endif
break;
case FcFEFCI:
/* CLEAR INITIALIZED STATUS BIT */
return FcProcessed;
}
return FcAccepted;
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on MSU FREND.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
** The rules seem to be:
** If last function was read, if activeChannel->full is true, do nothing,
** else if there's data in the device,
** set activeChannel->data to the next 12-bit data
** and set activeChannel->full = TRUE;
** else (no data in device)
** activeChannel->active = FALSE;
** If last function was write, if activeChannel->full is false, do nothing,
** else take activeChannel->data and send it to the device,
** and set activeChannel->full = FALSE;
**------------------------------------------------------------------------*/
static void msufrendIo(void)
{
activeFrend = (FrendContext *)activeDevice->context[0];
msufrendCheckIo();
switch (activeDevice->fcode)
{
case FcFEFSEL:
/* I don't know what to do on an I/O after a SELECT */
activeChannel->full = TRUE;
activeChannel->active = TRUE;
break;
case FcFEFST:
/* READ 6CA STATUS */
if (!activeChannel->full)
{
activeChannel->data = FCA_STATUS_INITIALIZED | FCA_STATUS_LAST_BYTE_NO_ERR;
activeChannel->full = TRUE;
#if DEBUG
fprintf(msufrendLog, " data:%04o", activeChannel->data);
#endif
}
break;
case FcFEFRM:
/* READ */
if (!activeChannel->full)
{
activeChannel->data = activeFrend->mem[activeFrend->addr++];
activeChannel->full = TRUE;
#if DEBUG
fprintf(msufrendLog, " %04o", activeChannel->data);
#endif
}
break;
case FcFEFRSM:
/* READ AND SET */
if (!activeChannel->full)
{
/* Return either the top or bottom byte of the address--but do */
/* not change the address register. */
if (activeFrend->nextIsSecond)
{
activeChannel->data = activeFrend->mem[activeFrend->addr + 1];
activeFrend->nextIsSecond = FALSE; /* Probably unnecessary. */
}
else
{
activeChannel->data = activeFrend->mem[activeFrend->addr];
/* Set top bit of word. */
activeFrend->mem[activeFrend->addr] |= 0x80;
activeFrend->nextIsSecond = TRUE;
}
activeChannel->full = TRUE;
#if DEBUG
fprintf(msufrendLog, " %04o", activeChannel->data);
#endif
}
break;
case FcFEFWM0:
/* WRITE MODE 0 - One PP word considered as 2 6-bit bytes, written to a 16-bit FE word. */
if (activeChannel->full)
{
/*
** Output data.
*/
activeFrend->mem[activeFrend->addr++] = (u8)activeChannel->data >> 8;
activeFrend->mem[activeFrend->addr++] = (u8)activeChannel->data & 0xff;
activeChannel->full = FALSE;
#if DEBUG
fprintf(msufrendLog, " %04o", activeChannel->data);
#endif
}
break;
case FcFEFWM:
/* WRITE MODE 1 - Two consecutive PP words (8 in 12) written to a 16-bit FE word. */
/* First PP word goes to upper 8 bits of FE word, confusingly called bits 0-7. */
if (activeChannel->full)
{
/*
** Output data.
*/
activeFrend->mem[activeFrend->addr++] = (u8)activeChannel->data;
activeChannel->full = FALSE;
#if DEBUG
fprintf(msufrendLog, " %04o", activeChannel->data);
#endif
}
break;
}
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void msufrendActivate(void)
{
activeChannel->active = TRUE;
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void msufrendDisconnect(void)
{
activeChannel->active = FALSE;
}
/*==========================================================================
* Reimplementation of FREND, an interactive front-end to SCOPE/Hustler,
* written at Michigan State University in the late 1970's.
*
* Mark Riordan 23 June 2004 (original implementation)
* Kevin Jordan 20 August 2022 (integrated within DtCyber executable)
*
* This version is integrated within the DtCyber executable. DtCyber has
* direct access to FREND memory, just as in the original 6000 Channel Adapter
* and FREND.
*
* It really helps to have listings of the original FREND and 1FP
* when you are following this code. See http://60bits.net
* As I wrote this code, I made it more and more like FREND.
* Originally, I tried to take shortcuts and implement a simpler
* structure--but that's the hard way if you want to eventually
* get most things working. Now even the routine names are
* largely borrowed from FREND.
*
* Original frend2 source is maintained via CVS (Concurrent Versions System)
* at:
*
* http://sourceforge.net/projects/frend2
*
* Symbol names are mostly derived from FREND, with '_' substituted
* for camel case.
*
* In comments, FWA means "First Word Address". It means the
* address of the first byte of a structure.
*
* This software is subject to the following, which is believed to be
* a verbatim copy of the well-known MIT license.
*
*----- Beginning of License ---------------------------------
* Copyright (c) 2005, Mark Riordan
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* ----- End of License ---------------------------------------
*/
static char *frendVersion = "63.01";
static char *author = "Mark Riordan 4513 Gregg Rd Madison, WI 53705";
/*===== Function prototypes =============================== */
static void taskSENDCP(HalfWord portNum, u8 MsgCode);
static void taskSKOTCL(PortContext *pp, FrendAddr fwaMySocket);
#define ALIGN_FULLWORD(addr) (0xfffffffc & (3 + addr))
/* Sets a 1-bit flag in a halfword */
/* EntryBaseAddr is the FWA of this table entry (NOT the FWA of the whole table). */
/* Name is the name of the flag. We obtain both offset and bit position from this. */
#define SETHFLAG(EntryBaseAddr, Name) \
setHalfWord(EntryBaseAddr + H_ ## Name, (HalfWord)(getHalfWord(EntryBaseAddr + H_ ## Name) | (1 << (15 - J_ ## Name))));
/* Clears a 1-bit flag in a halfword */
/* EntryBaseAddr is the FWA of this table entry (NOT the FWA of the whole table). */
/* Name is the name of the flag. We obtain both offset and bit position from this. */
#define CLEARHFLAG(EntryBaseAddr, Name) \
setHalfWord(EntryBaseAddr + H_ ## Name, (HalfWord)(getHalfWord(EntryBaseAddr + H_ ## Name) & (0xffff - (1 << (15 - J_ ## Name)))));
/* Returns 0 or 1, depending on whether a bit flag is set. */
#define HFLAGISSET(EntryBaseAddr, Name) \
((getHalfWord(EntryBaseAddr + H_ ## Name) & (1 << (15 - J_ ## Name))) ? 1 : 0)
/*--- function getLastOSError ----------------------
* Return the last error on a system call.
* Intended as a platform-agnostic wrapper to GetLastError() or whatever.
*/
static int getLastOSError()
{
#if defined(_WIN32)
return GetLastError();
#else
return errno;
#endif
}
/*--- function getLastSocketError ---------------------
* Socket-specific version of getLastOSError().
*/
static int getLastSocketError()
{
#if defined(_WIN32)
return WSAGetLastError();
#else
return errno;
#endif
}
/*--- function addrFrendTo1FP ----------------------
* Convert an address from FREND to 1FP format.
* This means dividing by 2, and ORing in the magic value
* intended to catch hardware errors.
*/
static FrendAddr addrFrendTo1FP(FrendAddr addr)
{
if (addr)
{
return (addr >> 1) | (F_PTIN << 24);
}
else
{
return 0;
}
}
/*--- function addr1FPToFrend ----------------------
* Convert an address from 1FP format to FREND format.
* This means multiplying by 2.
* To play it safe, we also get rid of the F_PTIN bits.
*/
static FrendAddr addr1FPToFrend(FrendAddr addr)
{
return (addr & 0xffffff) << 1; /* clear magic F_PTIN bits */
}
static void setFullWord(FrendAddr addr, FullWord word)
{
activeFrend->mem[addr] = (u8)((word >> 24) & 0xff);
activeFrend->mem[1 + addr] = (u8)((word >> 16) & 0xff);
activeFrend->mem[2 + addr] = (u8)((word >> 8) & 0xff);
activeFrend->mem[3 + addr] = (u8)(word & 0xff);
}
static FullWord getFullWord(FrendAddr addr)
{
return (activeFrend->mem[addr ] << 24)
| (activeFrend->mem[1 + addr] << 16)
| (activeFrend->mem[2 + addr] << 8)
| activeFrend->mem[3 + addr];
}
static void setHalfWord(FrendAddr addr, HalfWord half)
{
activeFrend->mem[addr] = (u8)((half >> 8) & 0xff);
activeFrend->mem[1 + addr] = (u8)(half & 0xff);
}
static HalfWord getHalfWord(FrendAddr addr)
{
return (activeFrend->mem[addr] << 8) | (activeFrend->mem[1 + addr]);
}
static void setByte(FrendAddr addr, u8 byte)
{
activeFrend->mem[addr] = byte;
}
static u8 getByte(FrendAddr addr)
{
return activeFrend->mem[addr];
}
/*--- function writeToOperTerm ----------------------
* Write a character to the operator terminal, possibly
* also logging it to a file.
*/
static void writeToOperTerm(u8 ch)
{
char buf[2];
buf[0] = ch;
buf[1] = '\0';
opDisplay(buf);
}
/*--- function sendToFSock ----------------------
* Send a buffer of bytes to a socket.
* We special-case the console, which is not connected via TCP.
* Exit: Returns # of bytes sent, or -1 if error.
* (The error is usually EWOULDBLOCK--not really an error.)
* The # may be less than nOutBytes because socket is non-blocking.
*/
static int sendToFSock(PortContext *pp, u8 bufout[], int nOutBytes)
{
int bytesSent;
if (FPORTCONSOLE == pp->id)
{
int j;
for (j = 0; j < nOutBytes; j++)
{
writeToOperTerm(bufout[j]);
}
return nOutBytes;
}
else
{
if (pp->active)
{
/*//! We should handle the case where not all bytes are sent.
* That would require queuing (fairly simple) and to really
* do it right, the ability to stop accepting lines from the
* host until the output queue is empty. */
bytesSent = send(pp->fd, bufout, nOutBytes, 0);
#if DEBUG
if (bytesSent != nOutBytes)
{
fprintf(msufrendLog, "\nsendToFSock: sent %d of %d bytes", bytesSent, nOutBytes);
}
#endif
return bytesSent;
}
else
{
#if DEBUG
fprintf(msufrendLog, "\nsendToFSock on port %d is not active", pp->id);
#endif
return -1;
}
}
}
/*===== Circular List functions. See lmbi.h for description. =====*/
/*--- function initCircList ----------------------
* Initialize a 7/32-style circular list.
* Entry: fwa is the addess of the list.
* nslots is the number of slots in the list.
* Exit: Returns # of bytes in circular list
*/
static HalfWord initCircList(FrendAddr fwaList, HalfWord nslots)
{
HalfWord totbytes = H_CIRCLIST_HEADER_BYTES + (nslots * CIRCLIST_SLOT_SIZE_BYTES);
memset(activeFrend->mem + fwaList, 0, totbytes);
setHalfWord(fwaList + H_CIRCLIST_N_SLOTS_TOT, nslots);
return totbytes;
}
/*--- function getListUsedEntries ----------------------
* Exit: Returns # of used entries in the list.
*/
static HalfWord getListUsedEntries(FrendAddr fwaList)
{
return getHalfWord(fwaList + H_CIRCLIST_N_USED);
}
/*--- function getListUsedEntries ----------------------
* Exit: Returns total # of entries (used and free) in the list.
*/
static HalfWord getListTotalEntries(FrendAddr fwaList)
{
return getHalfWord(fwaList + H_CIRCLIST_N_SLOTS_TOT);
}