// File: /workspace/github_c_makefile_star2_O3/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
movl 0x8(%rdi), %r13d
movq (%rsi), %rax
xorl %ecx, %ecx
cmpl $0x0, (%rax)
setne %cl
movzwl 0xc(%rdi), %eax
movzwl %ax, %r15d
leal -0x6000(%r13), %edx
cmpl $0x7fff, %edx           # imm = 0x7FFF
ja 0x34e7
movzwl 0x80888(%r14,%rcx,2), %edi
movl %edi, %eax
andl $0x1f, %eax
xorl %ebx, %ebx
cmpw $0xf, %ax
ja 0x3687
imulq $0x8088, %rax, %rax     # imm = 0x8088
leaq (%r14,%rax), %r12
addq $0x8, %r12
cmpl $0xdffe, %r13d          # imm = 0xDFFE
jne 0x353b
shrl $0x8, %edi
movzwl 0x7ffe(%r12), %esi
movl %r15d, %edx
callq 0x36d5
movw %ax, 0x7ffe(%r12)
leal (,%rax,8), %eax
cwtl
shrl $0xf, %eax
movw %ax, 0x8000(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x371f
jmp 0x3687
xorl %ebx, %ebx
leal -0x3e824(%r13), %edx
cmpl $0x6, %edx
ja 0x3517
leaq 0x4cd4(%rip), %rsi      # 0x81d0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leaq 0x80888(,%rcx,2), %rcx
movw %ax, (%r14,%rcx)
jmp 0x3687
cmpl $0x3e830, %r13d         # imm = 0x3E830
je 0x364b
cmpl $0x3f1b8, %r13d         # imm = 0x3F1B8
je 0x3687
movl $0x1, %ebx
jmp 0x3687
shrl %r13d
movl %r13d, %ebp
addl $0xffffd000, %r13d      # imm = 0xFFFFD000
movzwl -0x6000(%r12,%rbp,2), %esi
shrl $0x8, %edi
movl %r15d, %edx
callq 0x36d5
movw %ax, -0x6000(%r12,%rbp,2)
movzwl %r13w, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x3762
jmp 0x3687
movl %r15d, %eax
andl $0x7c, %eax
movl %eax, 0x808d4(%r14)
andl $-0x80, %r15d
jmp 0x3642
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, 0xfdb1(%rip)     # 0x13368
je 0x3624
movl 0xfdad(%rip), %ebp      # 0x1336c
cmpl $-0x1, %ebp
jne 0x3613
leaq 0xce9b(%rip), %rdi      # 0x10466
callq 0x2030
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x360d
movq %rax, %r12
leaq 0x10(%rsp), %rdi
callq 0x2150
cmpl $-0x1, %eax
je 0x360d
callq 0x2330
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x360d
testl %ebp, %ebp
je 0x3698
movl 0x10(%rsp), %edi
callq 0x2140
movl 0x14(%rsp), %ebp
movl %ebp, 0xfd59(%rip)      # 0x1336c
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
movl %ebp, %edi
callq 0x20b0
movl %r13d, 0xfd3d(%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 0x3687
movl %r15d, %r12d
shrl $0x8, %r12d
andl $0x1f, %r12d
cmpl $0x3fff, %r15d          # imm = 0x3FFF
ja 0x3674
andb $0xf, %al
shrl $0x8, %r15d
andl $-0x20, %r15d
addq %r14, %r15
movb %al, 0x8088c(%r12,%r15)
movq %r14, %rdi
callq 0x38a5
movq %r14, %rdi
movl %r12d, %esi
callq 0x3911
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