// File: /workspace/github_c_makefile_star2_O0/aap[P]pdp11/kd11b.c
// Function: dato
int
dato(KD11B *cpu, int b)
{
trace("dato %06o %06o %d\n", cpu->ba, cpu->bus->data, b);
	cpu->bus->pause = 0;

	/* allow odd addresses for bytes and registers */
	int alodd = b || cpu->ba >= 0177700 && cpu->ba < 0177720;

	if(!alodd && cpu->ba&1)
		goto be;

	/* internal registers */
	if((cpu->ba&0177400) == 0177400){
		if((cpu->ba&0360) == 0300){
			/* no idea if byte access even makes sense here */
			cpu->r[cpu->ba&017] = cpu->bus->data;
			goto ok;
		}
		switch(cpu->ba&0377){
		/* Line clock */
		case 0146:
			cpu->lc_int_enab = cpu->bus->data>>6 & 1;
			if((cpu->bus->data & 0200) == 0){
				cpu->lc_clock = 0;
				cpu->lc_int = 0;
			}
			goto ok;
		/* Receive */
		case 0160:
			// TODO: RDR ENAB
			cpu->rcd_rdr_enab = cpu->bus->data & 1;
			if(!cpu->rcd_int_enab && cpu->bus->data&0100 && cpu->rcd_da)
				cpu->rcd_int = 1;
			cpu->rcd_int_enab = cpu->bus->data>>6 & 1;
			goto ok;
		case 0162:
			/* read only */
			goto ok;
		/* Transmit */
		case 0164:
			// TODO: MAINT
			cpu->xmit_maint = cpu->bus->data>>2 & 1;
			if(!cpu->xmit_int_enab && cpu->bus->data&0100 && cpu->xmit_tbmt)
				cpu->xmit_int = 1;
			cpu->xmit_int_enab = cpu->bus->data>>6 & 1;
			goto ok;
		case 0166:
			cpu->xmit_b = cpu->bus->data;
			cpu->xmit_tbmt = 0;
			cpu->xmit_int = 0;
			goto ok;
		case 0170: case 0171:
			/* can't write switches */
			goto ok;
		case 0376: case 0377:
			/* writes 0 for the odd byte.
			   I think this is correct. */
			cpu->psw = cpu->bus->data;
			goto ok;

		/* respond but don't do anything */
		case 0147:
		case 0161:
		case 0163:
		case 0165:
		case 0167:
			goto ok;
		}
	}

	if(b){
		cpu->bus->addr = ubxt(cpu->ba);
		if(datob_bus(cpu->bus))
			goto be;
	}else{
		cpu->bus->addr = ubxt(cpu->ba)&~1;
		if(dato_bus(cpu->bus))
			goto be;
	}
ok:
	cpu->be = 0;
	return 0;
be:
	cpu->be++;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl $0x0, 0x10(%rax)
movb $0x1, %al
cmpl $0x0, -0x14(%rbp)
movb %al, -0x19(%rbp)
jne 0x22b7
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %ecx
xorl %eax, %eax
cmpl $0xffc0, %ecx           # imm = 0xFFC0
movb %al, -0x1a(%rbp)
jl 0x22b1
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %eax
cmpl $0xffd0, %eax           # imm = 0xFFD0
setl %al
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x22dd
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x22dd
jmp 0x260a
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
cmpl $0xff00, %eax           # imm = 0xFF00
jne 0x258b
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %eax
andl $0xf0, %eax
cmpl $0xc0, %eax
jne 0x2330
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw 0xc(%rax), %dx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x22(%rcx), %ecx
andl $0xf, %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x25f6
movq -0x10(%rbp), %rax
movzbl 0x22(%rax), %eax
movl %eax, -0x20(%rbp)
subl $0x66, %eax
je 0x23eb
jmp 0x2346
movl -0x20(%rbp), %eax
subl $0x67, %eax
je 0x2587
jmp 0x2354
movl -0x20(%rbp), %eax
subl $0x70, %eax
je 0x243e
jmp 0x2362
movl -0x20(%rbp), %eax
subl $0x71, %eax
je 0x2587
jmp 0x2370
movl -0x20(%rbp), %eax
subl $0x72, %eax
je 0x24b4
jmp 0x237e
movl -0x20(%rbp), %eax
subl $0x73, %eax
je 0x2587
jmp 0x238c
movl -0x20(%rbp), %eax
subl $0x74, %eax
je 0x24b9
jmp 0x239a
movl -0x20(%rbp), %eax
subl $0x75, %eax
je 0x2587
jmp 0x23a8
movl -0x20(%rbp), %eax
subl $0x76, %eax
je 0x2532
jmp 0x23b6
movl -0x20(%rbp), %eax
subl $0x77, %eax
je 0x2587
jmp 0x23c4
movl -0x20(%rbp), %eax
addl $-0x78, %eax
subl $0x2, %eax
jb 0x256b
jmp 0x23d5
movl -0x20(%rbp), %eax
addl $0xffffff02, %eax       # imm = 0xFFFFFF02
subl $0x2, %eax
jb 0x2570
jmp 0x2589
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %ecx
sarl $0x6, %ecx
andl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x84(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x2439
movq -0x10(%rbp), %rax
movl $0x0, 0x88(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8c(%rax)
jmp 0x25f6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %ecx
andl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x94(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x98(%rax)
jne 0x2493
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x2493
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
je 0x2493
movq -0x10(%rbp), %rax
movl $0x1, 0x9c(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %ecx
sarl $0x6, %ecx
andl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x98(%rax)
jmp 0x25f6
jmp 0x25f6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %ecx
sarl $0x2, %ecx
andl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xac(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
jne 0x2511
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x2511
movq -0x10(%rbp), %rax
cmpl $0x0, 0xb4(%rax)
je 0x2511
movq -0x10(%rbp), %rax
movl $0x1, 0xb0(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0xc(%rax), %ecx
sarl $0x6, %ecx
andl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xa8(%rax)
jmp 0x25f6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw 0xc(%rax), %ax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xb8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xb4(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xb0(%rax)
jmp 0x25f6
jmp 0x25f6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw 0xc(%rax), %ax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x30(%rax)
jmp 0x25f6
jmp 0x25f6
jmp 0x258b
cmpl $0x0, -0x14(%rbp)
je 0x25c1
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %edi
callq 0x2220
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x1460
cmpl $0x0, %eax
je 0x25bf
jmp 0x260a
jmp 0x25f4
movq -0x10(%rbp), %rax
movzwl 0x22(%rax), %edi
callq 0x2220
movl %eax, %ecx
andl $-0x2, %ecx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x13f0
cmpl $0x0, %eax
je 0x25f2
jmp 0x260a
jmp 0x25f4
jmp 0x25f6
movq -0x10(%rbp), %rax
movl $0x0, 0x38(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x261e
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)