// File: /workspace/github_c_makefile_star2_O1/aap[P]pdp11/kd11b.c
// Function: run
void
run(KD11B *cpu)
{
	int n;
	char c;
	cpu->state = STATE_RUNNING;
	initclock(&cpu->clock, CLOCKFREQ);
	n = 0;
	while(cpu->state != STATE_HALTED){
		if(handleclock(&cpu->clock)){
			cpu->lc_clock = 1;
			cpu->lc_int = 1;
		}

		cpu->traps &= ~TRAP_CLK;
		if(cpu->lc_int && cpu->lc_int_enab)
			cpu->traps |= TRAP_CLK;

		cpu->traps &= ~TRAP_TX;
		if(cpu->xmit_int && cpu->xmit_int_enab)
			cpu->traps |= TRAP_TX;

		cpu->traps &= ~TRAP_RX;
		if(cpu->rcd_int && cpu->rcd_int_enab)
			cpu->traps |= TRAP_RX;

		svc(cpu, cpu->bus);

		if(cpu->state == STATE_RUNNING ||
		   cpu->state == STATE_WAITING && cpu->traps){
			cpu->state = STATE_RUNNING;
			step(cpu);
		}

		// Don't handle IO all the time
		n++;
//		if(n != 20)
		if(n != 20000)
			continue;
		n = 0;

		/* transmit */
		if(!cpu->xmit_tbmt){
			uint8 c = cpu->xmit_b & 0177;
			write(cpu->tty.fd, &c, 1);
#ifdef AUTODIAG
	extern int diagpassed;
	if(c == '\a'){
		diagpassed = 1;
		return;
	}
#endif
			cpu->xmit_tbmt = 1;
			cpu->xmit_int = 1;
		}

		/* receive */
		if(ttyinput(&cpu->tty, &c)){
			cpu->rcd_busy = 1;
			cpu->rcd_rdr_enab = 0;
			cpu->rcd_b = c;
			cpu->rcd_da = 1;
			cpu->rcd_busy = 0;
			cpu->rcd_int = 1;
		}
	}

	printstate(cpu);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x1, 0x3c(%rdi)
addq $0xc0, %rdi
movl $0xfe502a, %esi         # imm = 0xFE502A
movq %rdi, 0x10(%rsp)
callq 0x3d08
cmpl $0x0, 0x3c(%rbx)
je 0x3565
leaq 0x40(%rbx), %r12
leaq 0xbc(%rbx), %rax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x1f39(%rip), %r15      # 0x52e0
movq 0x10(%rsp), %rdi
callq 0x3d32
testl %eax, %eax
je 0x33c6
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x88(%rbx)
movl 0x34(%rbx), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movl %ecx, 0x34(%rbx)
cmpl $0x0, 0x8c(%rbx)
je 0x33e9
cmpl $0x0, 0x84(%rbx)
je 0x33e9
orl $0x10, %eax
movl %eax, 0x34(%rbx)
movl 0x34(%rbx), %eax
movl %eax, %ecx
andl $0xfffffeff, %ecx       # imm = 0xFFFFFEFF
movl %ecx, 0x34(%rbx)
cmpl $0x0, 0xb0(%rbx)
je 0x3411
cmpl $0x0, 0xa8(%rbx)
je 0x3411
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x34(%rbx)
movl 0x34(%rbx), %eax
movl %eax, %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movl %ecx, 0x34(%rbx)
cmpl $0x0, 0x9c(%rbx)
je 0x3439
cmpl $0x0, 0x98(%rbx)
je 0x3439
orl $0x80, %eax
movl %eax, 0x34(%rbx)
movq 0x28(%rbx), %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r12)
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
andb $-0x6d, 0x34(%rbx)
movq (%rbp), %r14
testq %r14, %r14
je 0x34a8
movq 0x8(%r14), %rsi
movq %rbp, %rdi
callq *0x28(%r14)
movl %eax, %ecx
andl $-0x4, %ecx
cmpl $0x4, %ecx
jne 0x34a3
addl $-0x4, %eax
movq %rax, %rcx
shlq $0x4, %rcx
cmpq $0x0, (%r12,%rcx)
jne 0x34a3
addq %r12, %rcx
movq 0x30(%r14), %rdx
movq %rdx, (%rcx)
movq 0x8(%r14), %rdx
movq %rdx, 0x8(%rcx)
movl (%r15,%rax,4), %eax
orl %eax, 0x34(%rbx)
movq (%r14), %r14
jmp 0x345f
movl 0x3c(%rbx), %eax
cmpl $0x1, %eax
je 0x34bb
cmpl $0x2, %eax
jne 0x34ca
cmpl $0x0, 0x34(%rbx)
je 0x34ca
movl $0x1, 0x3c(%rbx)
movq %rbx, %rdi
callq 0x1e54
incl %r13d
cmpl $0x4e20, %r13d          # imm = 0x4E20
jne 0x355b
cmpl $0x0, 0xb4(%rbx)
jne 0x3515
movb 0xb8(%rbx), %al
andb $0x7f, %al
movb %al, 0x6(%rsp)
movl 0xbc(%rbx), %edi
movl $0x1, %edx
leaq 0x6(%rsp), %rsi
callq 0x1080
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xb0(%rbx)
movq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rsi
callq 0x3dc5
xorl %r13d, %r13d
testl %eax, %eax
je 0x355b
movq $0x1, 0x90(%rbx)
movb 0x7(%rsp), %al
movb %al, 0xa4(%rbx)
movl $0x0, 0x90(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x9c(%rbx)
cmpl $0x0, 0x3c(%rbx)
jne 0x33a7
movq %rbx, %rdi
callq 0x1a19
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq