// File: /workspace/github_c_makefile_star2_O0/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
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 0x3700
movq -0x20(%rbp), %rax
addq $0x8088a, %rax          # imm = 0x8088A
movq %rax, -0x50(%rbp)
jmp 0x370e
movq -0x20(%rbp), %rax
addq $0x80888, %rax          # imm = 0x80888
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
andl $0x1f, %eax
cmpl $0x10, %eax
jge 0x374a
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x30(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1f, %ecx
movslq %ecx, %rcx
imulq $0x8088, %rcx, %rcx     # imm = 0x8088
addq %rcx, %rax
movq %rax, -0x58(%rbp)
jmp 0x3752
xorl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x3752
movq -0x58(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x6000, 0x8(%rax)      # imm = 0x6000
jb 0x3875
movq -0x10(%rbp), %rax
cmpl $0xe000, 0x8(%rax)      # imm = 0xE000
jae 0x3875
cmpq $0x0, -0x38(%rbp)
jne 0x379b
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movq -0x10(%rbp), %rax
cmpl $0xdffe, 0x8(%rax)      # imm = 0xDFFE
jne 0x3812
movq -0x30(%rbp), %rax
movzwl (%rax), %edi
sarl $0x8, %edi
movq -0x38(%rbp), %rax
movw 0x7ffe(%rax), %ax
movzwl %ax, %esi
movzwl -0x28(%rbp), %edx
callq 0x3a30
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x7ffe(%rax)
movq -0x38(%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 -0x38(%rbp), %rax
movw %cx, 0x8000(%rax)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x3b80
jmp 0x3869
movl -0x24(%rbp), %eax
subl $0x3000, %eax           # imm = 0x3000
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x26(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %edi
sarl $0x8, %edi
movw -0x26(%rbp), %ax
movzwl %ax, %esi
movzwl -0x28(%rbp), %edx
callq 0x3a30
movw %ax, %dx
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x24(%rbp), %eax
movzwl %ax, %edx
callq 0x3bf0
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x5c(%rbp)
subl $0x3e824, %eax          # imm = 0x3E824
je 0x38d3
jmp 0x3888
movl -0x5c(%rbp), %eax
subl $0x3e826, %eax          # imm = 0x3E826
je 0x38ea
jmp 0x3894
movl -0x5c(%rbp), %eax
subl $0x3e828, %eax          # imm = 0x3E828
je 0x38f6
jmp 0x38a0
movl -0x5c(%rbp), %eax
subl $0x3e82a, %eax          # imm = 0x3E82A
je 0x395b
jmp 0x38b0
movl -0x5c(%rbp), %eax
subl $0x3e830, %eax          # imm = 0x3E830
je 0x39a9
jmp 0x38c0
movl -0x5c(%rbp), %eax
subl $0x3f1b8, %eax          # imm = 0x3F1B8
je 0x3a12
jmp 0x3a1b
movw -0x28(%rbp), %cx
movq -0x30(%rbp), %rax
movw %cx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movzwl -0x28(%rbp), %eax
andl $-0x20, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x808cc(%rax)
movq -0x20(%rbp), %rax
movzwl 0x808cc(%rax), %eax
sarl $0x8, %eax
andl $0xf, %eax
movzbl %al, %edi
callq 0x3e00
movq -0x20(%rbp), %rax
movl 0x808d0(%rax), %ecx
andl $0xfffcffff, %ecx       # imm = 0xFFFCFFFF
movzwl -0x28(%rbp), %eax
andl $0x3, %eax
shll $0x10, %eax
andl $0x30000, %eax          # imm = 0x30000
orl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x808d0(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movzwl -0x28(%rbp), %ecx
andl $0x7c, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x808d4(%rax)
movzwl -0x28(%rbp), %ecx
andl $-0x80, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x808d0(%rax)
movq -0x20(%rbp), %rax
movl 0x808d0(%rax), %ecx
andl $0x7f, %ecx
movzwl -0x28(%rbp), %eax
andl $-0x80, %eax
andl $-0x80, %eax
orl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x808d0(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movzwl -0x28(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x3c(%rbp)
movzwl -0x28(%rbp), %eax
sarl $0x8, %eax
andl $0x1f, %eax
movl %eax, -0x40(%rbp)
movzwl -0x28(%rbp), %eax
sarl $0xd, %eax
andl $0x7, %eax
movl %eax, -0x44(%rbp)
cmpl $0x2, -0x44(%rbp)
jge 0x39f4
movl -0x3c(%rbp), %eax
movb %al, %dl
movq -0x20(%rbp), %rax
addq $0x8088c, %rax          # imm = 0x8088C
movslq -0x44(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x40(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rdi
callq 0x3e60
movq -0x20(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x3f50
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movl $0x0, -0x4(%rbp)
jmp 0x3a22
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)