// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jge 0x28ab
movq -0x20(%rbp), %rdi
callq 0x27f0
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2130
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
callq 0xa550
cmpl $0x0, %eax
jne 0x28d8
movl $0x0, -0x4(%rbp)
jmp 0x2b66
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x2a(%rbp), %rsi
movl $0x2, %edx
callq 0x2160
movl %eax, -0x30(%rbp)
cmpl $0x2, -0x30(%rbp)
je 0x2927
leaq 0x8761(%rip), %rdi      # 0xb05e
movb $0x0, %al
callq 0x20f0
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x2140
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0x2b66
movzbl -0x2a(%rbp), %eax
cmpl $0x0, %eax
je 0x294f
movq 0xb6c1(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x8739(%rip), %rsi      # 0xb07a
movb $0x0, %al
callq 0x2190
xorl %edi, %edi
callq 0x22c0
movzbl -0x29(%rbp), %eax
cmpl $0x6, %eax
jle 0x2977
movq 0xb699(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x8711(%rip), %rsi      # 0xb07a
movb $0x0, %al
callq 0x2190
xorl %edi, %edi
callq 0x22c0
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
movzbl -0x29(%rbp), %edx
leaq -0x28(%rbp), %rsi
callq 0xa650
movw -0x26(%rbp), %ax
rolw $0x8, %ax
movzwl %ax, %eax
movzbl -0x27(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movzbl -0x23(%rbp), %eax
movzbl -0x24(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, -0x36(%rbp)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movzbl -0x28(%rbp), %eax
movl %eax, -0x3c(%rbp)
subl $0x1, %eax
je 0x29dc
jmp 0x29cb
movl -0x3c(%rbp), %eax
subl $0x2, %eax
je 0x2a5c
jmp 0x2ae6
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movw -0x36(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movl -0x34(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2a02
jmp 0x2b15
movq -0x10(%rbp), %rdi
callq 0x2530
cmpl $0x0, %eax
je 0x2a15
jmp 0x2b15
movq 0xbb1c(%rip), %rcx      # 0xe538
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %edx
leaq 0x865f(%rip), %rdi      # 0xb091
movb $0x0, %al
callq *%rcx
movb $0x0, -0x28(%rbp)
movb $0x1, -0x27(%rbp)
movb $0x3, -0x26(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x28(%rbp), %rsi
movl $0x3, %edx
callq 0xa6c0
jmp 0x2b02
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x34(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2a76
jmp 0x2b15
movq -0x10(%rbp), %rdi
callq 0x24c0
cmpl $0x0, %eax
je 0x2a89
jmp 0x2b15
movq 0xbaa8(%rip), %rcx      # 0xe538
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %edx
leaq 0x8603(%rip), %rdi      # 0xb0a9
movb $0x0, %al
callq *%rcx
movb $0x0, -0x28(%rbp)
movb $0x3, -0x27(%rbp)
movb $0x3, -0x26(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %eax
sarl $0x8, %eax
movb %al, -0x25(%rbp)
movq -0x10(%rbp), %rax
movw 0xc(%rax), %ax
movb %al, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x28(%rbp), %rsi
movl $0x5, %edx
callq 0xa6c0
jmp 0x2b02
movq 0xb50b(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movzbl -0x28(%rbp), %edx
leaq 0x85c5(%rip), %rsi      # 0xb0c0
movb $0x0, %al
callq 0x2190
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x2b66
movq 0xb4dc(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x85b2(%rip), %rsi      # 0xb0df
movb $0x0, %al
callq 0x2190
movb $0x0, -0x28(%rbp)
movb $0x1, -0x27(%rbp)
movb $0x4, -0x26(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x28(%rbp), %rsi
movl $0x3, %edx
callq 0xa6c0
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop