// File: /workspace/github_c_makefile_star2_O0/aap[P]pdp11/tv.c
// Function: dati_tv
int
dati_tv(Bus *bus, void *dev)
{
	TV *tv = dev;
	word *creg;
	FBuffer *curbuf;

	creg = tv->ten11->cycle ? &tv->creg10 : &tv->creg11;
	curbuf = (*creg & BUFMASK) < NUMFBUFFERS ? &tv->buffers[*creg & BUFMASK] : nil;
	int waddr = bus->addr>>1;
	if(bus->addr >= TVLO && bus->addr < 0160000){
		if(curbuf == nil)
			return 0;
		/* In Buffer */
		if((bus->addr&~1) == CSA)
			bus->data = curbuf->csa;
		else{
			waddr -= TVLO>>1;
			bus->data = curbuf->fb[waddr];
		}
		return 0;
	}
	switch(bus->addr){
	case CREG:
		bus->data = *creg;
		return 0;
	case CREG+2:
		/* unused, read only as side effect */
		bus->data = 0;
		return 0;
	case KMS:
		bus->data = tv->kms&~037 | tv->kma_hi>>16 & 3;
		return 0;
	case KMA:
		bus->data = tv->kma_hi | tv->kma_lo;
		return 0;
	case VSW:
	case ASW:
		/* write only */
		bus->data = 0;
		return 0;
	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax)
je 0x43a0
movq -0x20(%rbp), %rax
addq $0x8088a, %rax          # imm = 0x8088A
movq %rax, -0x40(%rbp)
jmp 0x43ae
movq -0x20(%rbp), %rax
addq $0x80888, %rax          # imm = 0x80888
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzwl (%rax), %eax
andl $0x1f, %eax
cmpl $0x10, %eax
jge 0x43ea
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x28(%rbp), %rcx
movzwl (%rcx), %ecx
andl $0x1f, %ecx
movslq %ecx, %rcx
imulq $0x8088, %rcx, %rcx     # imm = 0x8088
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x43f2
xorl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x43f2
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
shrl %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x6000, 0x8(%rax)      # imm = 0x6000
jb 0x4484
movq -0x10(%rbp), %rax
cmpl $0xe000, 0x8(%rax)      # imm = 0xE000
jae 0x4484
cmpq $0x0, -0x30(%rbp)
jne 0x4433
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $-0x2, %eax
cmpl $0xdffe, %eax           # imm = 0xDFFE
jne 0x4459
movq -0x30(%rbp), %rax
movw 0x7ffe(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
jmp 0x4478
movl -0x34(%rbp), %eax
subl $0x3000, %eax           # imm = 0x3000
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movw (%rax,%rcx,2), %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x4c(%rbp)
subl $0x3e824, %eax          # imm = 0x3E824
je 0x44e2
jmp 0x4497
movl -0x4c(%rbp), %eax
subl $0x3e826, %eax          # imm = 0x3E826
je 0x44fd
jmp 0x44a3
movl -0x4c(%rbp), %eax
subl $0x3e828, %eax          # imm = 0x3E828
je 0x4510
jmp 0x44af
movl -0x4c(%rbp), %eax
subl $0x3e82a, %eax          # imm = 0x3E82A
je 0x4544
jmp 0x44bf
movl -0x4c(%rbp), %eax
subl $0x3e830, %eax          # imm = 0x3E830
je 0x456c
jmp 0x44cf
movl -0x4c(%rbp), %eax
subl $0x3f1b8, %eax          # imm = 0x3F1B8
je 0x456c
jmp 0x457f
movq -0x28(%rbp), %rax
movw (%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x10(%rbp), %rax
movw $0x0, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x20(%rbp), %rax
movzwl 0x808cc(%rax), %eax
andl $-0x20, %eax
movq -0x20(%rbp), %rcx
movl 0x808d0(%rcx), %ecx
shrl $0x10, %ecx
andl $0x3, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x20(%rbp), %rax
movl 0x808d0(%rax), %eax
movq -0x20(%rbp), %rcx
orl 0x808d4(%rcx), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movq -0x10(%rbp), %rax
movw $0x0, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4586
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax,%rax)