// File: /workspace/github_c_makefile_star2_O3/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 0x3c20
cmpl $0x0, 0x3c(%rbx)
je 0x3479
leaq 0x40(%rbx), %r12
leaq 0xbc(%rbx), %rax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x1d90(%rip), %r15      # 0x5060
movq 0x10(%rsp), %rdi
callq 0x3c4a
testl %eax, %eax
je 0x32f9
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x88(%rbx)
movl 0x34(%rbx), %ecx
movl %ecx, %eax
andl $-0x11, %eax
jmp 0x330a
movl 0x34(%rbx), %ecx
movl %ecx, %eax
andl $-0x11, %eax
cmpl $0x0, 0x8c(%rbx)
je 0x3318
cmpl $0x0, 0x84(%rbx)
je 0x3318
orl $0x10, %ecx
movl %ecx, %eax
andl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
cmpl $0x0, 0xb0(%rbx)
je 0x3337
xorl %ecx, %ecx
cmpl $0x0, 0xa8(%rbx)
setne %cl
shll $0x8, %ecx
orl %ecx, %eax
andl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
cmpl $0x0, 0x9c(%rbx)
je 0x3356
xorl %ecx, %ecx
cmpl $0x0, 0x98(%rbx)
setne %cl
shll $0x7, %ecx
orl %ecx, %eax
movq 0x28(%rbx), %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r12)
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
andl $-0x6d, %eax
movl %eax, 0x34(%rbx)
movq (%rbp), %r14
testq %r14, %r14
je 0x33c7
movq 0x8(%r14), %rsi
movq %rbp, %rdi
callq *0x28(%r14)
movl %eax, %ecx
andl $-0x4, %ecx
cmpl $0x4, %ecx
jne 0x33c2
addl $-0x4, %eax
movq %rax, %rcx
shlq $0x4, %rcx
cmpq $0x0, (%r12,%rcx)
jne 0x33c2
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 0x337e
movl 0x3c(%rbx), %eax
cmpl $0x1, %eax
je 0x33da
cmpl $0x2, %eax
jne 0x33e9
cmpl $0x0, 0x34(%rbx)
je 0x33e9
movl $0x1, 0x3c(%rbx)
movq %rbx, %rdi
callq 0x1d2e
incl %r13d
cmpl $0x4e20, %r13d          # imm = 0x4E20
jne 0x346f
cmpl $0x0, 0xb4(%rbx)
jne 0x3430
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 0x3cdd
xorl %r13d, %r13d
testl %eax, %eax
je 0x346f
movl %r13d, 0x94(%rbx)
movb 0x7(%rsp), %al
movb %al, 0xa4(%rbx)
movl %r13d, 0x90(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x9c(%rbx)
cmpl $0x0, 0x3c(%rbx)
jne 0x32d0
movq %rbx, %rdi
callq 0x1905
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq