// File: /workspace/github_c_makefile_star2_O0/aap[P]pdp11/tv.c
// Function: datob_tv
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;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
shrl %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax)
je 0x40a0
movq -0x20(%rbp), %rax
addq $0x8088a, %rax          # imm = 0x8088A
movq %rax, -0x48(%rbp)
jmp 0x40ae
movq -0x20(%rbp), %rax
addq $0x80888, %rax          # imm = 0x80888
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzwl (%rax), %eax
andl $0x1f, %eax
cmpl $0x10, %eax
jge 0x40ea
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x38(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1f, %ecx
movslq %ecx, %rcx
imulq $0x8088, %rcx, %rcx     # imm = 0x8088
addq %rcx, %rax
movq %rax, -0x50(%rbp)
jmp 0x40f2
xorl %eax, %eax
movq %rax, -0x50(%rbp)
jmp 0x40f2
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
andl $0x1, %edx
movl $0xff, %eax
movl $0xffffff00, %ecx       # imm = 0xFFFFFF00
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, -0x2a(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x6000, 0x8(%rax)      # imm = 0x6000
jb 0x427c
movq -0x10(%rbp), %rax
cmpl $0xe000, 0x8(%rax)      # imm = 0xE000
jae 0x427c
cmpq $0x0, -0x40(%rbp)
jne 0x4159
movl $0x0, -0x4(%rbp)
jmp 0x4358
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $-0x2, %eax
cmpl $0xdffe, %eax           # imm = 0xDFFE
jne 0x41fc
movq -0x38(%rbp), %rax
movzwl (%rax), %edi
sarl $0x8, %edi
movq -0x40(%rbp), %rax
movw 0x7ffe(%rax), %ax
movzwl %ax, %esi
movzwl -0x28(%rbp), %edx
callq 0x3a30
movw %ax, -0x26(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x7ffe(%rax), %eax
movzwl -0x2a(%rbp), %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movzwl -0x26(%rbp), %ecx
movzwl -0x2a(%rbp), %edx
andl %edx, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x7ffe(%rax)
movq -0x40(%rbp), %rax
movzwl 0x7ffe(%rax), %edx
andl $0x1000, %edx           # imm = 0x1000
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x8000(%rax)
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3b80
jmp 0x4270
movl -0x24(%rbp), %eax
subl $0x3000, %eax           # imm = 0x3000
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movzwl (%rax), %edi
sarl $0x8, %edi
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movzwl %ax, %esi
movzwl -0x28(%rbp), %edx
callq 0x3a30
movw %ax, -0x26(%rbp)
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
movzwl -0x2a(%rbp), %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movzwl -0x26(%rbp), %ecx
movzwl -0x2a(%rbp), %edx
andl %edx, %ecx
orl %ecx, %eax
movw %ax, %dx
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x24(%rbp), %eax
movzwl %ax, %edx
callq 0x3bf0
movl $0x0, -0x4(%rbp)
jmp 0x4358
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $-0x2, %eax
movl %eax, -0x54(%rbp)
subl $0x3e824, %eax          # imm = 0x3E824
je 0x42d2
jmp 0x4292
movl -0x54(%rbp), %eax
subl $0x3e826, %eax          # imm = 0x3E826
je 0x4301
jmp 0x429e
movl -0x54(%rbp), %eax
subl $0x3e828, %eax          # imm = 0x3E828
je 0x430a
jmp 0x42aa
movl -0x54(%rbp), %eax
subl $0x3e82a, %eax          # imm = 0x3E82A
je 0x4329
jmp 0x42b6
movl -0x54(%rbp), %eax
subl $0x3e830, %eax          # imm = 0x3E830
je 0x4348
jmp 0x42c6
movl -0x54(%rbp), %eax
subl $0x3f1b8, %eax          # imm = 0x3F1B8
je 0x4348
jmp 0x4351
movq -0x38(%rbp), %rax
movzwl (%rax), %eax
movzwl -0x2a(%rbp), %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movzwl -0x28(%rbp), %ecx
movzwl -0x2a(%rbp), %edx
andl %edx, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4358
movl $0x0, -0x4(%rbp)
jmp 0x4358
leaq 0x6f7f(%rip), %rdi      # 0xb290
leaq 0x6f89(%rip), %rsi      # 0xb2a1
movl $0x13f, %edx            # imm = 0x13F
leaq 0x6f82(%rip), %rcx      # 0xb2a6
callq 0x2120
leaq 0x6f92(%rip), %rdi      # 0xb2c2
leaq 0x6f6a(%rip), %rsi      # 0xb2a1
movl $0x142, %edx            # imm = 0x142
leaq 0x6f63(%rip), %rcx      # 0xb2a6
callq 0x2120
movl $0x0, -0x4(%rbp)
jmp 0x4358
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)