// File: /workspace/github_c_makefile_star2_O1/aap[P]pdp11/u_kb11a.c
// Function: update_state
void
update_state(KB11A *kb)
{
	// TODO: tv

	switch(kb->c.rbr.kmx){
	case 0:
		kb->k0mx = 1;
		kb->k1mx = sxt(kb->sv);
		break;
	case 1:
		kb->k0mx = 2;	// TODO: correct?
		kb->k1mx = kb->tv;
		break;
	case 2:
		kb->k0mx = kb->srccon;
		if(kb->c.sr & 0200) kb->k0mx |= 0177400;
		kb->k1mx = kb->c.br & 0377;
		break;
	case 3:
		kb->k0mx = kb->destcon;
		if(kb->c.br & 0200) kb->k0mx |= 0177400;
		kb->k1mx = sxt(kb->c.br) & 0173377;
		break;
	default: assert(0);
	}
	kb->k1mx &= ~1;

	switch(kb->c.rbr.amx){
	case 0: kb->amx = kb->c.dr; break;
	case 1: kb->amx = kb->c.pcb; break;
	case 2: kb->amx = kb->c.sr; break;
	case 3: kb->amx = kb->c.br; break;
	default: assert(0);
	}

	switch(kb->c.rbr.bmx){
	case 0: kb->bmx = kb->k0mx; break;
	case 1: kb->bmx = kb->k1mx; break;
	case 2: kb->bmx = kb->c.sr; break;
	case 3: kb->bmx = kb->c.br; break;
	default: assert(0);
	}

	switch(kb->c.rbr.bax){
	case 0: kb->bamx = kb->c.dr; break;
	case 1: kb->bamx = kb->c.pcb; break;
	case 2: kb->bamx = kb->c.sr; break;
	case 3: kb->bamx = 0; break;	// TODO: EALU
	default: assert(0);
	}

	int shf = kb->c.rbr.shf;
	switch(kb->c.rbr.alu){
	case 0: kb->alu = ~kb->amx&M16; break;
	case 1: kb->alu = kb->bmx; break;
	case 2: kb->alu = kb->amx; break;
	case 3: kb->alu = kb->amx + kb->bmx; break;
	case 4: kb->alu = kb->amx + (kb->amx&(~kb->bmx&M16)); break;
	case 5: kb->alu = kb->amx + kb->amx; break;
	case 6: kb->alu = kb->amx + (~kb->bmx&M16) + 1; break;
	case 7:
		// change shf
		assert(0 && "yikes, not implemented yet");
	default: assert(0);
	}

	switch(shf){
	case 0: kb->shfr = kb->alu<<8 | kb->alu>>8; break;
	case 1: kb->shfr = kb->c.pcb; break;
	case 2: kb->shfr = kb->alu; break;
	// TODO: this is wrong
	case 3: kb->shfr = kb->alu>>1;
assert(0); break;
	default: assert(0);
	}

	switch(kb->c.rbr.ibs){
	case 0: kb->intbus = 0; break;
	case 1: kb->intbus = kb->sw; break;
	case 2: kb->intbus = 0; break;
	case 3: kb->intbus = kb->c.ps; break;
	default: assert(0);
	}

	int wantunibus = kb->c.rbr.bsd==INTR_PAUSE;	// TODO: DDC STOP?
	int wantintbus = kb->c.rbr.ibs&1;	// TODO: SW, KT11, FP
	kb->sel_int = !wantunibus && wantintbus;
	// we don't have fast mem
	kb->sel_mem = !wantunibus && !wantintbus && 0;

	switch(kb->c.rbr.brx<<2 | kb->sel_int<<1 | kb->sel_mem){
	case 0: case 1: case 2: case 3: case 7:
		kb->brmx = kb->shfr;
		break;
	case 4: kb->brmx = kb->unibusd; break;
	case 5: kb->brmx = 0; break;	// membus, we don't have it
	case 6: kb->brmx = kb->intbus; break;
	default: assert(0);
	}
}

// Assembly:
pushq %rax
movl 0x70(%rdi), %eax
cmpq $0x3, %rax
ja 0x29ff
leaq 0x1969(%rip), %rcx      # 0x4064
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movw $0x1, 0x20c(%rdi)
movl 0x250(%rdi), %eax
movl %eax, %ecx
orl $0xffffff00, %ecx       # imm = 0xFFFFFF00
testb %al, %al
movzbl %al, %eax
cmovsl %ecx, %eax
jmp 0x2793
movzbl 0x20a(%rdi), %eax
movw %ax, 0x20c(%rdi)
testb $-0x80, 0x6(%rdi)
je 0x2748
movzwl %ax, %eax
orl $0xff00, %eax           # imm = 0xFF00
movw %ax, 0x20c(%rdi)
movzbl (%rdi), %eax
jmp 0x2793
movzbl 0x209(%rdi), %eax
movw %ax, 0x20c(%rdi)
movzwl (%rdi), %edx
movzbl %dl, %ecx
testb %dl, %dl
jns 0x2774
movzwl %ax, %eax
orl $0xff00, %eax           # imm = 0xFF00
movw %ax, 0x20c(%rdi)
movsbl %cl, %eax
movzwl %cx, %ecx
andl $0xfffff600, %eax       # imm = 0xFFFFF600
orl %ecx, %eax
jmp 0x2793
movw $0x2, 0x20c(%rdi)
movzbl 0x20b(%rdi), %eax
andl $0xfffe, %eax           # imm = 0xFFFE
movw %ax, 0x20e(%rdi)
movl 0x68(%rdi), %ecx
cmpq $0x3, %rcx
ja 0x2a1e
leaq 0x18c1(%rip), %rdx      # 0x4074
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
movq %rdi, %rcx
jmpq *%rsi
leaq 0x8(%rdi), %rcx
jmp 0x27cf
leaq 0x4(%rdi), %rcx
jmp 0x27cf
leaq 0x6(%rdi), %rcx
movzwl (%rcx), %ecx
movw %cx, 0x210(%rdi)
movl 0x6c(%rdi), %ecx
cmpq $0x3, %rcx
ja 0x2a3d
leaq 0x1897(%rip), %rdx      # 0x4084
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzwl 0x20c(%rdi), %eax
jmp 0x2808
movzwl 0x6(%rdi), %eax
jmp 0x2808
movzwl (%rdi), %eax
movw %ax, 0x212(%rdi)
movl 0x50(%rdi), %eax
cmpq $0x3, %rax
ja 0x2a5c
leaq 0x1871(%rip), %rcx      # 0x4094
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl 0x8(%rdi), %eax
jmp 0x2840
movzwl 0x6(%rdi), %eax
jmp 0x2840
xorl %eax, %eax
jmp 0x2840
movzwl 0x4(%rdi), %eax
movw %ax, 0x214(%rdi)
movl 0x74(%rdi), %ecx
cmpq $0x7, %rcx
ja 0x2ad8
movl 0x3c(%rdi), %eax
leaq 0x1846(%rip), %rdx      # 0x40a4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzwl 0x210(%rdi), %ecx
xorl $0xffff, %ecx           # imm = 0xFFFF
jmp 0x28d1
movzwl 0x210(%rdi), %edx
movzwl 0x212(%rdi), %ecx
notl %ecx
andl %edx, %ecx
jmp 0x28a1
movzwl 0x210(%rdi), %ecx
jmp 0x28d1
movzwl 0x210(%rdi), %edx
movzwl 0x212(%rdi), %ecx
addl %edx, %ecx
jmp 0x28d1
movzwl 0x212(%rdi), %ecx
jmp 0x28d1
movzwl 0x210(%rdi), %ecx
addl %ecx, %ecx
jmp 0x28d1
movzwl 0x210(%rdi), %ecx
movzwl 0x212(%rdi), %edx
xorl $0xffff, %edx           # imm = 0xFFFF
addl %edx, %ecx
incl %ecx
movl %ecx, 0x21c(%rdi)
cmpl $0x3, %eax
ja 0x29e0
movl %eax, %eax
leaq 0x17db(%rip), %rcx      # 0x40c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x21c(%rdi), %eax
movl %eax, %ecx
shll $0x8, %ecx
shrl $0x8, %eax
orl %ecx, %eax
jmp 0x2911
movzwl 0x21c(%rdi), %eax
jmp 0x2911
movzwl 0x4(%rdi), %eax
movw %ax, 0x216(%rdi)
movl 0x54(%rdi), %eax
cmpq $0x3, %rax
ja 0x2a7b
leaq 0x17a8(%rip), %rcx      # 0x40d4
movslq (%rcx,%rax,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
xorl %ecx, %ecx
jmp 0x2949
movzwl 0x254(%rdi), %ecx
jmp 0x2949
movzwl 0x98(%rdi), %ecx
movw %cx, 0x24e(%rdi)
movl 0x1c(%rdi), %ecx
cmpl $0x1, 0x4c(%rdi)
setne %dl
andb %al, %dl
movzbl %dl, %eax
movl %eax, 0x240(%rdi)
movl $0x0, 0x244(%rdi)
addl %eax, %eax
leal (%rax,%rcx,4), %eax
cmpl $0x6, %eax
ja 0x2a9a
leaq 0x1760(%rip), %rcx      # 0x40e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x216(%rdi), %rax
jmp 0x29a6
leaq 0x24c(%rdi), %rax
jmp 0x29a6
leaq 0x24e(%rdi), %rax
movzwl (%rax), %eax
movw %ax, 0x218(%rdi)
popq %rax
retq
movl 0x21c(%rdi), %eax
shrl %eax
movw %ax, 0x216(%rdi)
leaq 0x1a94(%rip), %rdi      # 0x445c
leaq 0x1f00(%rip), %rsi      # 0x48cf
leaq 0x1f03(%rip), %rcx      # 0x48d9
movl $0x208, %edx            # imm = 0x208
callq 0x2050
leaq 0x1a75(%rip), %rdi      # 0x445c
leaq 0x1ee1(%rip), %rsi      # 0x48cf
leaq 0x1ee4(%rip), %rcx      # 0x48d9
movl $0x209, %edx            # imm = 0x209
callq 0x2050
leaq 0x1a56(%rip), %rdi      # 0x445c
leaq 0x1ec2(%rip), %rsi      # 0x48cf
leaq 0x1ec5(%rip), %rcx      # 0x48d9
movl $0x1d7, %edx            # imm = 0x1D7
callq 0x2050
leaq 0x1a37(%rip), %rdi      # 0x445c
leaq 0x1ea3(%rip), %rsi      # 0x48cf
leaq 0x1ea6(%rip), %rcx      # 0x48d9
movl $0x1e0, %edx            # imm = 0x1E0
callq 0x2050
leaq 0x1a18(%rip), %rdi      # 0x445c
leaq 0x1e84(%rip), %rsi      # 0x48cf
leaq 0x1e87(%rip), %rcx      # 0x48d9
movl $0x1e8, %edx            # imm = 0x1E8
callq 0x2050
leaq 0x19f9(%rip), %rdi      # 0x445c
leaq 0x1e65(%rip), %rsi      # 0x48cf
leaq 0x1e68(%rip), %rcx      # 0x48d9
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x2050
leaq 0x19da(%rip), %rdi      # 0x445c
leaq 0x1e46(%rip), %rsi      # 0x48cf
leaq 0x1e49(%rip), %rcx      # 0x48d9
movl $0x211, %edx            # imm = 0x211
callq 0x2050
leaq 0x19bb(%rip), %rdi      # 0x445c
leaq 0x1e27(%rip), %rsi      # 0x48cf
leaq 0x1e2a(%rip), %rcx      # 0x48d9
movl $0x221, %edx            # imm = 0x221
callq 0x2050
leaq 0x1e34(%rip), %rdi      # 0x48f4
leaq 0x1e08(%rip), %rsi      # 0x48cf
leaq 0x1e0b(%rip), %rcx      # 0x48d9
movl $0x1fe, %edx            # imm = 0x1FE
callq 0x2050
leaq 0x197d(%rip), %rdi      # 0x445c
leaq 0x1de9(%rip), %rsi      # 0x48cf
leaq 0x1dec(%rip), %rcx      # 0x48d9
movl $0x1ff, %edx            # imm = 0x1FF
callq 0x2050