-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtv.c
875 lines (792 loc) · 16.4 KB
/
tv.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
#include "11.h"
#include "tv.h"
#include "util.h"
#include <sys/poll.h>
#include <pthread.h>
/* maximum:
* 256 framebuffers
* 64 keyboards
* 16 video switch inputs (buffers) per section
* 32 video switch outputs (displays)
*/
enum {
WIDTH = 576,
HEIGHT = 454
};
enum {
TVLO = 060000,
TVHI = TVLO + WIDTH*HEIGHT/8,
CSA = 0157776,
CREG = 0764044,
KMS = 0764050,
KMA = 0764052,
VSW = 0764060,
ASW = 0770670,
};
enum {
ALU_SETC = 0,
ALU_NOR,
ALU_ANDC,
ALU_SETZ,
ALU_NAND,
ALU_COMP,
ALU_XOR,
ALU_ANDCSD,
ALU_ORCSD,
ALU_EQV,
ALU_SAME,
ALU_AND,
ALU_ORSCD,
ALU_SETO,
ALU_IOR,
ALU_SET
};
static pthread_mutex_t lock;
static pthread_mutex_t kblock;
static pthread_cond_t kbcond;
#define GUARD pthread_mutex_lock(&lock)
#define UNGUARD pthread_mutex_unlock(&lock)
static word nullfb[16*1024 - 1];
/* map from input switch to buffer number
* This is the config at tech square that the TV program expects */
static int dpymap[NUMSECTIONS][NUMINPUTS] = {
/* first section */
{ -1, /* null input */
0, 1, 2, 3, 4, 5, 6,
7, 014, 015, 016, 017, -1, -1, -1 },
/* first section */
{ -1, /* null input */
010, 011, 012, 013, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1 }
};
/* List of video switch output and a keyboard */
static struct {
int dpy;
int kbd;
} dpykbdlist[NUMOUTPUTS] = {
{ 0, 0, }, // 809 FAHLMAN, HOLLOWAY, KNIGHT
{ 01, 014 }, // 820 MINSKY
{ 02, 021 }, // 824 RICH, DEKLEER
{ 03, 06 }, // 808 FREILING, ULLMAN
{ 04, 010 }, // 817 JABARI
{ 05, 022 }, // 825 Sjoberg
{ 06, 04 }, // 813 HEWITT
{ 07, 05 }, // 814 SUSSMAN
{ 010, 013 }, // 819 GOLDSTEIN
{ 011, 017 }, // 821A MARR
{ 012, 035 }, // 914 COHEN, GOSPER, ETC.
{ 013, 034 }, // 913 BAISLEY, GREENBLATT
{ 014, 033 }, // 334 EDWARDS, LEBEL
{ 015, 030 }, // 925 MOON'S REFRIGERATOR
{ 016, 031 }, // 902 TAENZER, MASON
{ 017, 032 }, // 919 Very Small Data losers
{ 020, 03 }, // 812 YVONNE
{ 021, 036 }, // 912 9TH FLOOR LOUNGE
{ 022, 037 }, // 907 CHESS, LISP MACHINES
{ 023, 01 }, // 810 LAVIN, KUIPERS, MILLER
{ 024, 02 }, // 919 Very Small Data Bases NORTH (FAR END)
{ -1, -1, },
{ 026, 026 }, // 826 Fredkin
{ -1, -1, },
{ 030, 046 }, // 3rd Floor #6
{ 031, 031 }, // 815 Horn
{ -1, -1, },
{ 033, 045 }, // 3rd Floor #5
{ 034, 044 }, // 3rd Floor #4
{ 035, 043 }, // 3rd Floor #3
{ 036, 042 }, // 3rd Floor #2
{ 037, 041 }, // 3rd Floor #1
};
void sendfb(TV *tv, int osw);
void sendbuf(TV *tv, FBuffer *buffer);
void sendupdate(TV *tv, FBuffer *buffer, uint16 addr);
void
vswinfo(TV *tv)
{
int i, j;
int osw;
printf("Video switch:\n");
for(i = 0; i < NUMOUTPUTS; i++)
printf("%o|%o -> %o\n", tv->vswsect[0][i], tv->vswsect[1][i], i);
printf("frame buffer output:\n");
for(i = 0; i < NUMFBUFFERS; i++)
for(j = 0; j < tv->buffers[i].nosw; j++){
osw = tv->buffers[i].osw[j];
printf("%o %o: %o %o\n", i, j, osw, tv->omap[osw]);
}
}
static void
updatevsw(TV *tv)
{
int i, j, n;
for(i = 0; i < NUMFBUFFERS; i++){
n = 0;
for(j = 0; j < NUMOUTPUTS; j++)
if(dpymap[0][tv->vswsect[0][j]] == i ||
dpymap[1][tv->vswsect[1][j]] == i)
tv->buffers[i].osw[n++] = j;
tv->buffers[i].nosw = n;
}
}
static word
alu(int op, word a, word b)
{
switch(op){
case ALU_SETC: a = ~b; break;
case ALU_NOR: a = ~(a|b); break;
case ALU_ANDC: a = a&~b; break;
case ALU_SETZ: a = 0; break;
case ALU_NAND: a = ~(a&b); break;
case ALU_COMP: a = ~a; break;
case ALU_XOR: a ^= b; break;
case ALU_ANDCSD: a = ~a&b; break;
case ALU_ORCSD: a = a|~b; break;
case ALU_EQV: a = ~(a^b); break;
case ALU_SAME: break;
case ALU_AND: a &= b; break;
case ALU_ORSCD: a = ~a|b; break;
case ALU_SETO: a = ~0; break;
case ALU_IOR: a |= b; break;
case ALU_SET: a = b; break;
default: a = 0; /* can't happen */
}
return a;
}
static int
oc_child(void)
{
int fd[2];
pid_t pid;
const char *prog = getenv("TV11KMS");
if(prog == NULL)
return -1;
if(pipe(fd) == -1)
return -1;
pid = fork();
if(pid == -1)
return -1;
else if(pid == 0) {
close(0);
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
execlp(prog, prog, NULL);
exit(1);
}
close(fd[0]);
return fd[1];
}
static void
oc_output(unsigned char bits)
{
static int old = -1;
static int fd = -1;
if(bits != old) {
if(fd == -1)
fd = oc_child();
(void)write(fd, &bits, 1); /* error? lol yolo */
}
old = bits;
}
int
dato_tv(Bus *bus, void *dev)
{
TV *tv = dev;
int waddr = bus->addr>>1;
word w, d;
word *creg;
FBuffer *curbuf;
creg = tv->ten11->cycle ? &tv->creg10 : &tv->creg11;
curbuf = (*creg & BUFMASK) < NUMFBUFFERS ? &tv->buffers[*creg & BUFMASK] : nil;
d = bus->data;
if(bus->addr >= TVLO && bus->addr < 0160000){
if(curbuf == nil)
return 0;
/* In Buffer */
if(bus->addr == CSA){
curbuf->csa = alu(*creg>>8, curbuf->csa, d);
curbuf->mask = curbuf->csa&010000 ? ~0 : 0;
sendbuf(tv, curbuf);
}else{
waddr -= TVLO>>1;
w = curbuf->fb[waddr];
curbuf->fb[waddr] = alu(*creg>>8, w, d);
sendupdate(tv, curbuf, waddr);
}
return 0;
}
switch(bus->addr){
case CREG:
//printf("writing CREG %06o\n", d);
*creg = d;
return 0;
case CREG+2:
/* unused, written only as side effect */
return 0;
case KMS:
tv->kms = d & ~037;
oc_output ((tv->kms >> 8) & 017);
SETMASK(tv->kma_hi, (d&3)<<16, 3<<16);
return 0;
case KMA:
tv->kma_lo = d & 0174;
tv->kma_hi = d & ~0177;
SETMASK(tv->kma_hi, d&~0177, ~0177);
return 0;
case VSW:{
int i, o, s;
i = d&017;
o = d>>8 & 037;
s = d>>13 & 07;
if(s < 2)
tv->vswsect[s][o] = i;
updatevsw(tv);
sendfb(tv, o);
}
return 0;
case ASW:
// TODO, but we don't support audio anyway
return 0;
}
return 1;
}
int
datob_tv(Bus *bus, void *dev)
{
TV *tv = dev;
int waddr = bus->addr>>1;
word w, d, m;
word *creg;
FBuffer *curbuf;
creg = tv->ten11->cycle ? &tv->creg10 : &tv->creg11;
curbuf = ((*creg & BUFMASK) < NUMFBUFFERS) ? &tv->buffers[*creg & BUFMASK] : nil;
d = bus->data;
m = bus->addr&1 ? ~0377 : 0377;
if(bus->addr >= TVLO && bus->addr < 0160000){
if(curbuf == nil)
return 0;
/* In Buffer */
if((bus->addr&~1) == CSA){
w = alu(*creg>>8, curbuf->csa, d);
SETMASK(curbuf->csa, w, m);
curbuf->mask = curbuf->csa&010000 ? ~0 : 0;
sendbuf(tv, curbuf);
}else{
waddr -= TVLO>>1;
w = alu(*creg>>8, curbuf->fb[waddr], d);
SETMASK(curbuf->fb[waddr], w, m);
sendupdate(tv, curbuf, waddr);
}
return 0;
}
switch(bus->addr&~1){
case CREG:
//printf("writing CREGb %06o\n", d);
SETMASK(*creg, d, m);
return 0;
case CREG+2:
/* unused, written only as side effect */
return 0;
case KMS:
/* Don't know if this is allowed,
* so catch it if it happens. */
assert(0 && "KMS write");
return 0;
case KMA:
assert(0 && "KMA write");
return 0;
case VSW:
case ASW:
// don't allow byte writes to this
// doesn't make that much sense
return 0;
}
return 1;
}
int
dati_tv(Bus *bus, void *dev)
{
TV *tv = dev;
word *creg;
FBuffer *curbuf;
creg = tv->ten11->cycle ? &tv->creg10 : &tv->creg11;
curbuf = (*creg & BUFMASK) < NUMFBUFFERS ? &tv->buffers[*creg & BUFMASK] : nil;
int waddr = bus->addr>>1;
if(bus->addr >= TVLO && bus->addr < 0160000){
if(curbuf == nil)
return 0;
/* In Buffer */
if((bus->addr&~1) == CSA)
bus->data = curbuf->csa;
else{
waddr -= TVLO>>1;
bus->data = curbuf->fb[waddr];
}
return 0;
}
switch(bus->addr){
case CREG:
bus->data = *creg;
return 0;
case CREG+2:
/* unused, read only as side effect */
bus->data = 0;
return 0;
case KMS:
bus->data = tv->kms&~037 | tv->kma_hi>>16 & 3;
return 0;
case KMA:
bus->data = tv->kma_hi | tv->kma_lo;
return 0;
case VSW:
case ASW:
/* write only */
bus->data = 0;
return 0;
}
return 1;
}
void
reset_tv(void *dev)
{
TV *tv = dev;
memset(tv->buffers, 0, sizeof(tv->buffers));
tv->creg11 = 0;
tv->creg10 = 0;
memset(tv->vswsect, 0, sizeof(tv->vswsect));
tv->kms = 0;
tv->kma_hi = 0;
tv->kma_lo = 0;
tv->km_haskey = 0;
tv->km_kbd = 0;
tv->km_key = 0;
updatevsw(tv);
}
void
inittv(TV *tv)
{
TVcon *con;
int i;
for(con = tv->cons; con < &tv->cons[NUMCONNECTIONS]; con++){
con->fd = -1;
con->dpy = -1;
con->kbd = -1;
}
for(i = 0; i < NUMOUTPUTS; i++)
tv->omap[i] = -1;
pthread_mutex_init(&lock, nil);
pthread_mutex_init(&kblock, nil);
pthread_cond_init(&kbcond, nil);
}
/*
* The network TV connections
*/
enum {
/* TV to 11 */
MSG_KEYDN = 0,
MSG_GETFB,
/* 11 to TV */
MSG_FB,
MSG_WD,
MSG_CLOSE,
};
word
b2w(uint8 *b)
{
return b[0] | b[1]<<8;
}
void
w2b(uint8 *b, word w)
{
b[0] = w;
b[1] = w>>8;
}
void
msgheader(uint8 *b, uint8 type, uint16 length)
{
w2b(b, length);
b[2] = type;
}
uint8 largebuf[64*1024];
void
dumpbuf(uint8 *b, int n)
{
while(n--)
printf("%o ", *b++);
printf("\n");
}
static void
packfb(TV *tv, uint8 *dst, int osw, int x, int y, int w, int h)
{
int stride;
int n1, n2;
word *src1, *src2;
word bw1, bw2;
stride = WIDTH/16;
/* We mix the outputs of both sections for the final output.
* This feature does not seem to be used by the TV system
* but it's theoretically capable of doing something like this.
* External video input is not supported here of course. */
n1 = dpymap[0][tv->vswsect[0][osw]];
n2 = dpymap[1][tv->vswsect[1][osw]];
//printf("inbuf: %d %d\n", n1, n2);
src1 = n1 < 0 ? nullfb : &tv->buffers[n1].fb[stride*y + x];
src2 = n2 < 0 ? nullfb : &tv->buffers[n2].fb[stride*y + x];
bw1 = n1 < 0 ? 0 : tv->buffers[n1].mask;
bw2 = n2 < 0 ? 0 : tv->buffers[n2].mask;
for(y = 0; y < h; y++){
for(x = 0; x < w; x++){
/* We mix with an OR */
w2b(dst, src1[x]^bw1 | src2[x]^bw2);
dst += 2;
}
src1 += stride;
src2 += stride;
}
}
void
sendfb(TV *tv, int osw)
{
TVcon *con = &tv->cons[tv->omap[osw]];
if(tv->omap[osw] < 0)
return;
uint8 *b = largebuf;
msgheader(b, MSG_FB, 1+8+WIDTH*HEIGHT/8);
b += 3;
w2b(b, 0);
w2b(b+2, 0);
w2b(b+4, WIDTH/16);
w2b(b+6, HEIGHT);
b += 8;
packfb(tv, b, con->dpy, 0, 0, WIDTH/16, HEIGHT);
writen(con->fd, largebuf, 3+8+WIDTH*HEIGHT/8);
}
void
sendbuf(TV *tv, FBuffer *buffer)
{
int i;
for(i = 0; i < buffer->nosw; i++)
if(buffer->osw[i] >= 0)
sendfb(tv, buffer->osw[i]);
}
/* Send a single word update to all displays */
void
sendupdate(TV *tv, FBuffer *buffer, uint16 addr)
{
uint8 buf[7];
int i;
int osw;
int n1, n2;
word w1, w2;
word bw1, bw2;
/* Don't tell screen to update anything not visible */
if(addr >= (TVHI-TVLO)/2)
return;
msgheader(buf, MSG_WD, 5);
w2b(buf+3, addr);
// printf("sending update of %o: %o\n", addr, tv->curbuf->fb[addr]);
/* Again do the mixing thing for no reason */
for(i = 0; i < buffer->nosw; i++){
osw = buffer->osw[i];
if(osw < 0 || tv->omap[osw] < 0)
continue;
n1 = dpymap[0][tv->vswsect[0][osw]];
n2 = dpymap[1][tv->vswsect[1][osw]];
w1 = n1 < 0 ? 0 : tv->buffers[n1].fb[addr];
w2 = n2 < 0 ? 0 : tv->buffers[n2].fb[addr];
bw1 = n1 < 0 ? 0 : tv->buffers[n1].mask;
bw2 = n2 < 0 ? 0 : tv->buffers[n2].mask;
w2b(buf+5, w1^bw1 | w2^bw2);
writen(tv->cons[tv->omap[osw]].fd, buf, 7);
}
}
/* Tell the other side what we have allocated for them.
* Not terribly important but they may want to know. */
void
setdpykbd(int fd, int dpy, int kbd)
{
uint8 buf[2];
buf[0] = dpy;
buf[1] = kbd;
writen(fd, buf, 2);
}
/* Get first free connection or -1 */
static int
alloccon(TV *tv)
{
int i;
for(i = 0; i < NUMCONNECTIONS; i++)
if(tv->cons[i].fd < 0 && dpykbdlist[i].dpy >= 0)
return i;
return -1;
}
/* NB: this should be GUARDed by the caller */
static void
closecon(TV *tv, TVcon *con)
{
printf("disconnect display %o\n", con->dpy);
tv->omap[con->dpy] = -1;
close(con->fd);
con->fd = -1;
con->dpy = -1;
con->kbd = -1;
}
void
closetv(TV *tv)
{
uint8 buf[3];
int i;
for(i = 0; i < NUMCONNECTIONS; i++)
if(tv->cons[i].fd >= 0){
msgheader(buf, MSG_CLOSE, 1);
writen(tv->cons[i].fd, buf, 3);
/* wait for close */
read(tv->cons[i].fd, buf, 1);
/* this will cause the handletv thread
* to close the connection...if it makes
* it that far */
}
}
void
accepttv(int fd, void *arg)
{
TV *tv = arg;
int c;
TVcon *con;
c = alloccon(tv);
if(c < 0){
/* Nothing free */
setdpykbd(fd, -1, -1);
close(fd);
return;
}
con = &tv->cons[c];
GUARD;
con->dpy = dpykbdlist[c].dpy;
con->kbd = dpykbdlist[c].kbd;
con->fd = fd;
nodelay(con->fd);
tv->omap[con->dpy] = c;
UNGUARD;
setdpykbd(con->fd, con->dpy, con->kbd);
printf("connected display %o\n", con->dpy);
}
int
svc_tv(Bus *bus, void *dev)
{
TV *tv = dev;
word key, kbd;
int ch;
ch = (tv->kms&0100040) == 0100040 ? 5 : 0;
if(!tv->km_haskey)
return ch;
pthread_mutex_lock(&kblock);
tv->km_haskey = 0;
key = tv->km_key;
kbd = tv->km_kbd;
pthread_cond_signal(&kbcond);
pthread_mutex_unlock(&kblock);
bus->addr = tv->kma_hi | tv->kma_lo;
bus->data = key;
dato_bus(bus);
tv->kma_lo = tv->kma_lo+2 & 0177;
bus->addr = tv->kma_hi | tv->kma_lo;
bus->data = (kbd&077) << 8;
dato_bus(bus);
tv->kma_lo = tv->kma_lo+2 & 0177;
tv->kms &= ~0200; /* DMA done */
tv->kms |= 0100000; /* interrupt bit */
ch = (tv->kms&0100040) == 0100040 ? 5 : 0;
return ch;
}
int
bg_tv(void *dev)
{
TV *tv = dev;
tv->kms &= ~0100000;
return 0340;
}
/* Send key to emu thread to write to memory.
* This BLOCKS if the emu thread doesn't consume keys! */
static void
sendkey(TV *tv, word kbd, word key)
{
if((tv->kms & 0100) == 0) /* write to mem */
return;
/* wait until we can send a key */
pthread_mutex_lock(&kblock);
tv->kms |= 0200;
while(tv->km_haskey != 0)
pthread_cond_wait(&kbcond, &kblock);
tv->km_haskey = 1;
tv->km_key = key;
tv->km_kbd = kbd;
pthread_mutex_unlock(&kblock);
}
/* This is executed within GUARDs */
static void
handlemsg(TV *tv, TVcon *con)
{
uint16 len;
uint8 *b;
uint8 type;
int x, y, w, h;
if(readn(con->fd, &len, 2)){
err:
closecon(tv, con);
return;
}
len = b2w((uint8*)&len);
b = largebuf;
if(readn(con->fd, b, len))
goto err;
type = *b++;
switch(type){
case MSG_KEYDN:
sendkey(tv, con->kbd, b2w(b));
break;
case MSG_GETFB:
x = b2w(b);
y = b2w(b+2);
w = b2w(b+4);
h = b2w(b+6);
printf("getfb: %d %d %d %d\n", x, y, w, h);
x /= 16;
w = (w+15) / 16;
b = largebuf;
msgheader(b, MSG_FB, 1+8+w*h*2);
b += 3;
w2b(b, x);
w2b(b+2, y);
w2b(b+4, w);
w2b(b+6, h);
b += 8;
packfb(tv, b, con->dpy, x, y, w, h);
writen(con->fd, largebuf, 3+8+w*h*2);
break;
default:
fprintf(stderr, "unknown msg type %d\n", type);
}
}
static void*
handletv_thread(void *arg)
{
TV *tv = arg;
int i;
int nfds;
int n;
struct pollfd fds[NUMCONNECTIONS];
int conmap[NUMCONNECTIONS];
TVcon *con;
for(;;){
nfds = 0;
for(i = 0; i < NUMCONNECTIONS; i++){
if(tv->cons[i].fd < 0)
continue;
fds[nfds].fd = tv->cons[i].fd;
fds[nfds].events = POLLIN;
conmap[nfds] = i;
nfds++;
}
if(nfds == 0){
sleep_ms (200);
continue;
}
/* We need a timeout here so poll can see
* when we open a new connection */
n = poll(fds, nfds, 200);
if(n < 0){
perror("error: poll");
/* Is this the correct thing to do? */
return nil;
}
/* timeout */
if(n == 0)
continue;
for(i = 0; i < nfds; i++){
if(fds[i].revents == 0)
continue;
con = &tv->cons[conmap[i]];
if(fds[i].revents != POLLIN){
/* When does this happen? */
GUARD;
closecon(tv, con);
UNGUARD;
continue;
}
GUARD;
/* Check whether it's still the connection
* we thought we're using. */
/* TODO: make this check more sound */
if(fds[i].fd == con->fd){
/* All good, talk to the display */
// printf("handling con %d\n", conmap[i]);
handlemsg(tv, con);
}
UNGUARD;
}
}
return nil;
}
void
handletvs(TV *tv)
{
pthread_t th;
pthread_create(&th, nil, handletv_thread, tv);
}
struct Serveargs {
TV *tv;
int port;
};
static void*
servetv_thread(void *arg)
{
struct Serveargs *sa = arg;
serve(sa->port, accepttv, sa->tv);
free(sa);
return nil;
}
void
servetv(TV *tv, int port)
{
pthread_t th;
struct Serveargs *sa;
sa = malloc(sizeof(struct Serveargs));
sa->tv = tv;
sa->port = port;
pthread_create(&th, nil, servetv_thread, sa);
}
#if 0
void
tvtest(TV *tv, Bus *bus)
{
bus->addr = VSW;
bus->data = WD(0 | 0, 1);
dato_bus(bus);
bus->data = WD(0 | 1, 2);
dato_bus(bus);
bus->data = WD(0 | 2, 3);
dato_bus(bus);
bus->data = WD(0 | 3, 4);
dato_bus(bus);
vswinfo(tv);
bus->addr = KMA;
bus->data = 01200;
dato_bus(bus);
bus->addr = KMS;
bus->data = 0140;
dato_bus(bus);
bus->addr = KMS;
dati_bus(bus);
printf("kms read: %o\n", bus->data);
bus->addr = KMA;
dati_bus(bus);
printf("kma read: %o\n", bus->data);
bus->addr = CREG;
bus->data = WD(ALU_SET, 0);
dato_bus(bus);
bus->addr = 0060000 + 0037730;
bus->data = ~0;
dato_bus(bus);
}
#endif