// File: /workspace/github_c_makefile_star2_O3/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
movl 0x4(%rsi), %edi
testl %edi, %edi
jns 0x2a12
movq %rbx, %rdi
callq 0x299b
movl 0x4(%rbx), %edi
movq $0x0, 0x8(%rsp)
callq 0x6fe4
testl %eax, %eax
je 0x2bcc
movl 0x4(%rbx), %edi
leaq 0x6(%rsp), %rsi
movl $0x2, %edx
callq 0x2160
cmpl $0x2, %eax
jne 0x2b10
cmpb $0x0, 0x6(%rsp)
jne 0x2bf2
movzbl 0x7(%rsp), %edx
cmpl $0x7, %edx
jae 0x2bf7
movl 0x4(%rbx), %edi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x7050
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 0x2b30
cmpl $0x1, %edx
jne 0x2bd8
movzwl %di, %edx
shll $0x8, %edx
movzwl %si, %esi
orl %edx, %esi
movl %ecx, 0x8(%r14)
movw %si, 0xc(%r14)
testb $0x1, %al
jne 0x2b8f
movq %r14, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x2b8f
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq *0x18(%r15)
testl %eax, %eax
jne 0x2aca
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x5540(%rip), %rdi      # 0x8035
xorl %eax, %eax
callq *0x1089b(%rip)          # 0x13398
leaq 0x8(%rsp), %rsi
movw $0x100, (%rsi)          # imm = 0x100
movb $0x3, 0x2(%rsi)
jmp 0x2bb9
leaq 0x568b(%rip), %rdi      # 0x81a2
callq 0x2070
movl 0x4(%rbx), %edi
callq 0x2140
movl $0xffffffff, 0x4(%rbx)  # imm = 0xFFFFFFFF
jmp 0x2bcc
movl %ecx, 0x8(%r14)
testb $0x1, %al
jne 0x2b8f
movq %r14, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x2b8f
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq *0x10(%r15)
testl %eax, %eax
jne 0x2b3b
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x54eb(%rip), %rdi      # 0x804d
xorl %eax, %eax
callq *0x1082e(%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 0x2bc1
movq 0x10462(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
movl 0x8(%r14), %edx
leaq 0x54df(%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 0x7099
movl $0x0, (%rbx)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x10419(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
leaq 0x547b(%rip), %rsi      # 0x8064
xorl %eax, %eax
callq 0x2190
jmp 0x2bc6
callq 0x2390
callq 0x23b4