// File: /workspace/github_c_makefile_star2_O1/aap[P]pdp11/tv.c
// Function: dato_tv
int
dato_tv(Bus *bus, void *dev)
{
	TV *tv = dev;
	int waddr = bus->addr>>1;
	word w, d;
	word *creg;
	FBuffer *curbuf;

	creg = tv->ten11->cycle ? &tv->creg10 : &tv->creg11;
	curbuf = (*creg & BUFMASK) < NUMFBUFFERS ? &tv->buffers[*creg & BUFMASK] : nil;
	d = bus->data;
	if(bus->addr >= TVLO && bus->addr < 0160000){
		if(curbuf == nil)
			return 0;
		/* In Buffer */
		if(bus->addr == CSA){
			curbuf->csa = alu(*creg>>8, curbuf->csa, d);
			curbuf->mask = curbuf->csa&010000 ? ~0 : 0;
			sendbuf(tv, curbuf);
		}else{
			waddr -= TVLO>>1;
			w = curbuf->fb[waddr];
			curbuf->fb[waddr] = alu(*creg>>8, w, d);
			sendupdate(tv, curbuf, waddr);
		}
		return 0;
	}
	switch(bus->addr){
	case CREG:
//printf("writing CREG %06o\n", d);
		*creg = d;
		return 0;
	case CREG+2:
		/* unused, written only as side effect */
		return 0;
	case KMS:
		tv->kms = d & ~037;
		oc_output ((tv->kms >> 8) & 017);
		SETMASK(tv->kma_hi, (d&3)<<16, 3<<16);
		return 0;
	case KMA:
		tv->kma_lo = d & 0174;
		tv->kma_hi = d & ~0177;
		SETMASK(tv->kma_hi, d&~0177, ~0177);
		return 0;
	case VSW:{
		int i, o, s;
		i = d&017;	
		o = d>>8 & 037;
		s = d>>13 & 07;
		if(s < 2)
			tv->vswsect[s][o] = i;
		updatevsw(tv);
		sendfb(tv, o);
		}
		return 0;
	case ASW:
		// TODO, but we don't support audio anyway
		return 0;
	}
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rax
movq (%rsi), %rdx
xorl %ecx, %ecx
cmpl $0x0, (%rdx)
movl 0x8(%rdi), %r13d
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, %r12d
cmovbq %rsi, %r12
movzwl 0xc(%rax), %eax
movzwl %ax, %r15d
leal -0x6000(%r13), %esi
cmpl $0x7fff, %esi           # imm = 0x7FFF
ja 0x3690
cmpw $0xf, %dx
ja 0x3830
cmpl $0xdffe, %r13d          # imm = 0xDFFE
jne 0x36e2
shrl $0x8, %edi
movzwl 0x7ffe(%r12), %esi
movl %r15d, %edx
callq 0x387e
movw %ax, 0x7ffe(%r12)
leal (,%rax,8), %eax
cwtl
shrl $0xf, %eax
movw %ax, 0x8000(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x38c8
jmp 0x3830
leal -0x3e824(%r13), %edx
cmpl $0x6, %edx
ja 0x36be
leaq 0x4b2d(%rip), %rsi      # 0x81d0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leaq 0x80888(,%rcx,2), %rcx
movw %ax, (%r14,%rcx)
jmp 0x3830
cmpl $0x3e830, %r13d         # imm = 0x3E830
je 0x37f4
cmpl $0x3f1b8, %r13d         # imm = 0x3F1B8
je 0x3830
movl $0x1, %ebx
jmp 0x3830
shrl %r13d
movl %r13d, %ebp
addl $0xffffd000, %r13d      # imm = 0xFFFFD000
movzwl -0x6000(%r12,%rbp,2), %esi
shrl $0x8, %edi
movl %r15d, %edx
callq 0x387e
movw %ax, -0x6000(%r12,%rbp,2)
movzwl %r13w, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x3908
jmp 0x3830
movl %r15d, %eax
andl $0x7c, %eax
movl %eax, 0x808d4(%r14)
andl $-0x80, %r15d
jmp 0x37eb
andl $0xffe0, %eax           # imm = 0xFFE0
movw %ax, 0x808cc(%r14)
movl %r15d, %r13d
shrl $0x8, %r13d
movl %r13d, %eax
andb $0xf, %al
movb %al, 0xf(%rsp)
andl $0xf, %r13d
cmpl %r13d, 0xfc0a(%rip)     # 0x13368
je 0x37cd
cmpl $-0x1, 0xfc05(%rip)     # 0x1336c
jne 0x37b8
leaq 0xccf6(%rip), %rdi      # 0x10466
callq 0x2030
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x37b2
movq %rax, %r12
leaq 0x10(%rsp), %rdi
callq 0x2150
cmpl $-0x1, %eax
je 0x37b2
callq 0x2330
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x37b2
testl %ebp, %ebp
je 0x3841
movl 0x10(%rsp), %edi
callq 0x2140
movl 0x14(%rsp), %ebp
movl %ebp, 0xfbb4(%rip)      # 0x1336c
movl 0xfbae(%rip), %edi      # 0x1336c
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
movl %r13d, 0xfb94(%rip)     # 0x13368
movl $0xfffcffff, %eax       # imm = 0xFFFCFFFF
andl 0x808d0(%r14), %eax
andl $0x3, %r15d
shll $0x10, %r15d
orl %eax, %r15d
movl %r15d, 0x808d0(%r14)
jmp 0x3830
movl %r15d, %r12d
shrl $0x8, %r12d
andl $0x1f, %r12d
cmpl $0x3fff, %r15d          # imm = 0x3FFF
ja 0x381d
andb $0xf, %al
shrl $0x8, %r15d
andl $-0x20, %r15d
addq %r14, %r15
movb %al, 0x8088c(%r12,%r15)
movq %r14, %rdi
callq 0x3a48
movq %r14, %rdi
movl %r12d, %esi
callq 0x3ab4
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x2140
movl 0x10(%rsp), %edi
xorl %esi, %esi
callq 0x20e0
movl 0x10(%rsp), %edi
callq 0x2140
movl 0x14(%rsp), %edi
callq 0x2140
movq %r12, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2300
movl $0x1, %edi
callq 0x22c0