// File: /workspace/github_c_makefile_star2_O2/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 0x2669
movq %rbx, %rdi
callq 0x25d9
movl 0x4(%rbx), %edi
andq $0x0, 0x8(%rsp)
callq 0x6c24
testl %eax, %eax
je 0x27f3
movl 0x4(%rbx), %edi
leaq 0x6(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x2160
cmpl $0x2, %eax
jne 0x2750
cmpb $0x0, 0x6(%rsp)
jne 0x2819
movzbl 0x7(%rsp), %edx
cmpl $0x7, %edx
jae 0x2819
movl 0x4(%rbx), %edi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x6c82
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 0x276d
cmpl $0x1, %edx
jne 0x27ff
movzwl %di, %edx
shll $0x8, %edx
movzwl %si, %esi
orl %edx, %esi
movl %ecx, 0x8(%r14)
movw %si, 0xc(%r14)
testb $0x1, %al
jne 0x27bb
movq %r14, %rdi
callq 0x24be
testl %eax, %eax
jne 0x27bb
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x4900(%rip), %rdi      # 0x7035
xorl %eax, %eax
callq *0x10c5b(%rip)          # 0x13398
leaq 0x8(%rsp), %rsi
movw $0x100, (%rsi)          # imm = 0x100
movb $0x3, 0x2(%rsi)
jmp 0x27e5
leaq 0x4a4b(%rip), %rdi      # 0x71a2
callq 0x2070
movl 0x4(%rbx), %edi
callq 0x2140
orl $-0x1, 0x4(%rbx)
jmp 0x27f3
movl %ecx, 0x8(%r14)
testb $0x1, %al
jne 0x27bb
movq %r14, %rdi
callq 0x248f
testl %eax, %eax
jne 0x27bb
movl 0x8(%r14), %esi
movzwl 0xc(%r14), %edx
leaq 0x48bc(%rip), %rdi      # 0x704d
xorl %eax, %eax
callq *0x10bff(%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
pushq $0x5
jmp 0x27ea
movq 0x10836(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
movl 0x8(%r14), %edx
leaq 0x48b3(%rip), %rsi      # 0x7083
xorl %eax, %eax
callq 0x2190
leaq 0x8(%rsp), %rsi
movw $0x100, (%rsi)          # imm = 0x100
movb $0x4, 0x2(%rsi)
movl 0x4(%rbx), %edi
pushq $0x3
popq %rdx
callq 0x6cc7
andl $0x0, (%rbx)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x107f2(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
leaq 0x4854(%rip), %rsi      # 0x7064
xorl %eax, %eax
callq 0x2190
jmp 0x27f0
movq 0x107d8(%rip), %rax     # 0x12ff8
movq (%rax), %rcx
leaq 0x47f4(%rip), %rdi      # 0x701e
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x22e0
xorl %edi, %edi
callq 0x22c0