// File: /workspace/github_c_makefile_star2_O1/aap[P]pdp11/tv11.c
// Function: svc_ten11
int
svc_ten11(Bus *bus, void *dev)
{
	Ten11 *ten11 = dev;
	uint8 buf[8], len[2];
	int n;
	uint32 a;
	word d;

	if(ten11->fd < 0)
		reconnect(ten11);

	memset(buf, 0, sizeof(buf));
	if(!hasinput(ten11->fd))
		return 0;
	n = read(ten11->fd, len, 2);
	if(n != 2){
		printf("fd closed, reconnecting...\n");
		close(ten11->fd);
		ten11->fd = -1;
		return 0;
	}
	if(len[0] != 0){
		fprintf(stderr, "unibus botch, exiting\n");
		exit(0);
	}
	if(len[1] > 6){
		fprintf(stderr, "unibus botch, exiting\n");
		exit(0);
	}
	readn(ten11->fd, buf, len[1]);

	a = buf[3] | buf[2]<<8 | buf[1]<<16;
	d = buf[5] | buf[4]<<8;

	ten11->cycle = 1;
	switch(buf[0]){
	case 1:		/* write */
		bus->addr = a;
		bus->data = d;
		if(a&1) goto be;
		if(dato_bus(bus)) goto be;
		debug("TEN11 write: %06o %06o\n", bus->addr, bus->data);
		buf[0] = 0;
		buf[1] = 1;
		buf[2] = 3;
		writen(ten11->fd, buf, 3);
		break;
	case 2:		/* read */
		bus->addr = a;
		if(a&1) goto be;
		if(dati_bus(bus)) goto be;
		debug("TEN11 read: %06o %06o\n", bus->addr, bus->data);
		buf[0] = 0;
		buf[1] = 3;
		buf[2] = 3;
		buf[3] = bus->data>>8;
		buf[4] = bus->data;
		writen(ten11->fd, buf, 5);
		break;
	default:
		fprintf(stderr, "unknown ten11 message type %d\n", buf[0]);
		break;
	}
	ten11->cycle = 0;
	return 0;
be:
fprintf(stderr, "TEN11 bus error %06o\n", bus->addr);
	buf[0] = 0;
	buf[1] = 1;
	buf[2] = 4;
	writen(ten11->fd, buf, 3);
	ten11->cycle = 0;
	return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r14
cmpl $0x0, 0x4(%rsi)
jns 0x2b6e
movq %rbx, %rdi
callq 0x2afb
movq $0x0, 0x8(%rsp)
movl 0x4(%rbx), %edi
callq 0x70e8
testl %eax, %eax
je 0x2d39
movl 0x4(%rbx), %edi
leaq 0x6(%rsp), %rsi
movl $0x2, %edx
callq 0x2160
cmpl $0x2, %eax
jne 0x2c78
cmpb $0x0, 0x6(%rsp)
jne 0x2d5f
movzbl 0x7(%rsp), %edx
cmpl $0x7, %edx
jae 0x2d64
movl 0x4(%rbx), %edi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x7154
movzbl 0x3(%r15), %eax
movzbl 0x2(%r15), %edx
shll $0x8, %edx
movzbl 0x1(%r15), %ecx
shll $0x10, %ecx
orl %edx, %ecx
orl %eax, %ecx
movzbl 0x5(%r15), %esi
movzbl 0x4(%r15), %edi
movl $0x1, (%rbx)
movzbl (%r15), %edx
cmpl $0x2, %edx
je 0x2c98
cmpl $0x1, %edx
jne 0x2d45
movzwl %di, %edx
shll $0x8, %edx
movzwl %si, %esi
orl %edx, %esi
movl %ecx, 0x8(%r14)
movw %si, 0xc(%r14)
testb $0x1, %al
jne 0x2cfc
movq %r14, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x2cfc
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq *0x18(%r15)
testl %eax, %eax
jne 0x2c29
testq %r15, %r15
je 0x2cfc
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x53d8(%rip), %rdi      # 0x8035
xorl %eax, %eax
callq *0x10733(%rip)          # 0x13398
leaq 0x8(%rsp), %rsi
movw $0x100, (%rsi)          # imm = 0x100
movb $0x3, 0x2(%rsi)
jmp 0x2d26
leaq 0x5523(%rip), %rdi      # 0x81a2
callq 0x2070
movl 0x4(%rbx), %edi
callq 0x2140
movl $0xffffffff, 0x4(%rbx)  # imm = 0xFFFFFFFF
jmp 0x2d39
movl %ecx, 0x8(%r14)
testb $0x1, %al
jne 0x2cfc
movq %r14, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x2cfc
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq *0x10(%r15)
testl %eax, %eax
jne 0x2ca3
testq %r15, %r15
je 0x2cfc
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x537e(%rip), %rdi      # 0x804d
xorl %eax, %eax
callq *0x106c1(%rip)          # 0x13398
leaq 0x8(%rsp), %rsi
movw $0x300, (%rsi)          # imm = 0x300
movb $0x3, 0x2(%rsi)
movzwl 0xc(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x3(%rsi)
movl 0x4(%rbx), %edi
movl $0x5, %edx
jmp 0x2d2e
movq 0x102f5(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
movl 0x8(%r14), %edx
leaq 0x5372(%rip), %rsi      # 0x8083
xorl %eax, %eax
callq 0x2190
leaq 0x8(%rsp), %rsi
movw $0x100, (%rsi)          # imm = 0x100
movb $0x4, 0x2(%rsi)
movl 0x4(%rbx), %edi
movl $0x3, %edx
callq 0x719d
movl $0x0, (%rbx)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x102ac(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
leaq 0x530e(%rip), %rsi      # 0x8064
xorl %eax, %eax
callq 0x2190
jmp 0x2d33
callq 0x2390
callq 0x23b4