// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rax
movl 0x8(%rdi), %r13d
movq (%rsi), %rdx
xorl %ecx, %ecx
cmpl $0x0, (%rdx)
setne %cl
movzwl 0x80888(%rsi,%rcx,2), %edi
movzwl 0xc(%rax), %eax
testb $0x1, %r13b
movl $0xff, %edx
movl $0xff00, %r12d          # imm = 0xFF00
cmovel %edx, %r12d
leal -0x6000(%r13), %edx
cmpl $0x7fff, %edx           # imm = 0x7FFF
ja 0x358a
movl %edi, %ecx
andl $0x1f, %ecx
xorl %ebx, %ebx
cmpw $0xf, %cx
ja 0x3623
imulq $0x8088, %rcx, %rcx     # imm = 0x8088
leaq (%r14,%rcx), %r15
addq $0x8, %r15
movl %r13d, %ecx
andl $0xfffe, %ecx           # imm = 0xFFFE
cmpl $0xdffe, %ecx           # imm = 0xDFFE
jne 0x35d9
shrl $0x8, %edi
movzwl 0x7ffe(%r15), %ebp
movzwl %ax, %edx
movl %ebp, %esi
callq 0x3217
xorl %ebp, %eax
andl %r12d, %eax
xorl %ebp, %eax
movw %ax, 0x7ffe(%r15)
shll $0x3, %eax
cwtl
shrl $0xf, %eax
movw %ax, 0x8000(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3261
jmp 0x3623
andl $-0x2, %r13d
xorl %ebx, %ebx
leal -0x3e824(%r13), %edx
cmpl $0x6, %edx
ja 0x35c2
leaq 0x3c89(%rip), %rsi      # 0x722c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leaq 0x80888(,%rcx,2), %rcx
xorl %edi, %eax
andl %r12d, %eax
xorl %edi, %eax
movw %ax, (%r14,%rcx)
jmp 0x3623
cmpl $0x3e830, %r13d         # imm = 0x3E830
je 0x3623
cmpl $0x3f1b8, %r13d         # imm = 0x3F1B8
je 0x3623
pushq $0x1
popq %rbx
jmp 0x3623
shrl %r13d
movl %r13d, %ecx
movq %rcx, (%rsp)
addl $0xffffd000, %r13d      # imm = 0xFFFFD000
shrl $0x8, %edi
movzwl -0x6000(%r15,%rcx,2), %ebp
movzwl %ax, %edx
movl %ebp, %esi
callq 0x3217
xorl %ebp, %eax
andl %r12d, %eax
xorl %ebp, %eax
movq (%rsp), %rcx
movw %ax, -0x6000(%r15,%rcx,2)
movzwl %r13w, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x329a
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbe00(%rip), %rdi      # 0xf43b
leaq 0xbdd8(%rip), %rsi      # 0xf41a
leaq 0xbdd6(%rip), %rcx      # 0xf41f
movl $0x142, %edx            # imm = 0x142
callq 0x2120
leaq 0xbdaf(%rip), %rdi      # 0xf409
leaq 0xbdb9(%rip), %rsi      # 0xf41a
leaq 0xbdb7(%rip), %rcx      # 0xf41f
movl $0x13f, %edx            # imm = 0x13F
callq 0x2120