// File: /workspace/github_c_makefile_star2_O1/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
movq (%rsi), %rdx
xorl %ecx, %ecx
cmpl $0x0, (%rdx)
movl 0x8(%rdi), %r12d
setne %cl
movzwl 0x80888(%rsi,%rcx,2), %edi
movl %edi, %edx
andl $0x1f, %edx
imulq $0x8088, %rdx, %rsi     # imm = 0x8088
xorl %ebx, %ebx
cmpw $0x10, %dx
leaq 0x8(%r14,%rsi), %rsi
movl $0x0, %r15d
cmovbq %rsi, %r15
movzwl 0xc(%rax), %eax
testb $0x1, %r12b
movl $0xff, %esi
movl $0xff00, %r13d          # imm = 0xFF00
cmovel %esi, %r13d
leal -0x6000(%r12), %esi
cmpl $0x7fff, %esi           # imm = 0x7FFF
ja 0x3c02
cmpw $0xf, %dx
ja 0x3c9c
movl %r12d, %ecx
andl $0xfffe, %ecx           # imm = 0xFFFE
cmpl $0xdffe, %ecx           # imm = 0xDFFE
jne 0x3c52
shrl $0x8, %edi
movzwl 0x7ffe(%r15), %ebp
movzwl %ax, %edx
movl %ebp, %esi
callq 0x387e
xorl %ebp, %eax
andl %r13d, %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 0x38c8
jmp 0x3c9c
andl $-0x2, %r12d
leal -0x3e824(%r12), %edx
cmpl $0x6, %edx
ja 0x3c39
leaq 0x4612(%rip), %rsi      # 0x822c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leaq 0x80888(,%rcx,2), %rcx
xorl %edi, %eax
andl %r13d, %eax
xorl %edi, %eax
movw %ax, (%r14,%rcx)
jmp 0x3c9c
cmpl $0x3e830, %r12d         # imm = 0x3E830
je 0x3c9c
cmpl $0x3f1b8, %r12d         # imm = 0x3F1B8
je 0x3c9c
movl $0x1, %ebx
jmp 0x3c9c
shrl %r12d
movl %r12d, %ecx
movq %rcx, (%rsp)
addl $0xffffd000, %r12d      # imm = 0xFFFFD000
shrl $0x8, %edi
movzwl -0x6000(%r15,%rcx,2), %ebp
movzwl %ax, %edx
movl %ebp, %esi
callq 0x387e
xorl %ebp, %eax
andl %r13d, %eax
xorl %ebp, %eax
movq (%rsp), %rcx
movw %ax, -0x6000(%r15,%rcx,2)
movzwl %r12w, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3908
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc787(%rip), %rdi      # 0x1043b
leaq 0xc75f(%rip), %rsi      # 0x1041a
leaq 0xc75d(%rip), %rcx      # 0x1041f
movl $0x142, %edx            # imm = 0x142
callq 0x2120
leaq 0xc736(%rip), %rdi      # 0x10409
leaq 0xc740(%rip), %rsi      # 0x1041a
leaq 0xc73e(%rip), %rcx      # 0x1041f
movl $0x13f, %edx            # imm = 0x13F
callq 0x2120