// File: /workspace/github_c_makefile_star2_O2/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)
leaq 0xc0(%rdi), %r14
movl $0xfe502a, %esi         # imm = 0xFE502A
movq %r14, %rdi
callq 0x38dc
leaq 0x40(%rbx), %r13
leaq 0xbc(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x3c(%rbx)
je 0x318c
movq %r14, %rdi
callq 0x3906
testl %eax, %eax
je 0x2ffd
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x88(%rbx)
movl 0x34(%rbx), %eax
movl %eax, %ecx
andl $-0x11, %ecx
jmp 0x300e
movl 0x34(%rbx), %eax
movl %eax, %ecx
andl $-0x11, %ecx
cmpl $0x0, 0x8c(%rbx)
je 0x301c
cmpl $0x0, 0x84(%rbx)
je 0x301c
orl $0x10, %eax
movl %eax, %ecx
andl $0xfffffeff, %ecx       # imm = 0xFFFFFEFF
cmpl $0x0, 0xb0(%rbx)
je 0x303c
xorl %eax, %eax
cmpl $0x0, 0xa8(%rbx)
setne %al
shll $0x8, %eax
orl %eax, %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
cmpl $0x0, 0x9c(%rbx)
je 0x305c
xorl %eax, %eax
cmpl $0x0, 0x98(%rbx)
setne %al
shll $0x7, %eax
orl %eax, %ecx
movq 0x28(%rbx), %rbp
pushq $0x40
popq %rax
cmpq $0x80, %rax
je 0x3078
xorps %xmm0, %xmm0
movups %xmm0, (%rbx,%rax)
addq $0x10, %rax
jmp 0x3063
andl $-0x6d, %ecx
movq %rbp, %r12
movl %ecx, 0x34(%rbx)
movq (%r12), %r12
testq %r12, %r12
je 0x30d6
movq 0x8(%r12), %rsi
movq %rbp, %rdi
callq *0x28(%r12)
movl %eax, %ecx
andl $-0x4, %ecx
cmpl $0x4, %ecx
jne 0x3081
addl $-0x4, %eax
movq %rax, %rcx
shlq $0x4, %rcx
cmpq $0x0, (%r13,%rcx)
jne 0x3081
addq %r13, %rcx
movq 0x30(%r12), %rdx
movq %rdx, (%rcx)
movq 0x8(%r12), %rdx
movq %rdx, 0x8(%rcx)
movl 0x34(%rbx), %ecx
leaq 0xf9f(%rip), %rdx       # 0x4070
orl (%rdx,%rax,4), %ecx
jmp 0x307e
movl 0x3c(%rbx), %eax
cmpl $0x1, %eax
je 0x30e9
cmpl $0x2, %eax
jne 0x30f8
cmpl $0x0, 0x34(%rbx)
je 0x30f8
movl $0x1, 0x3c(%rbx)
movq %rbx, %rdi
callq 0x1c54
incl %r15d
cmpl $0x4e20, %r15d          # imm = 0x4E20
jne 0x2fcc
cmpl $0x0, 0xb4(%rbx)
jne 0x3141
movb 0xb8(%rbx), %al
andb $0x7f, %al
movb %al, 0xe(%rsp)
movl 0xbc(%rbx), %edi
leaq 0xe(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x1080
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xb0(%rbx)
movq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rsi
callq 0x3995
movl $0x0, %r15d
testl %eax, %eax
je 0x2fcc
andl $0x0, 0x94(%rbx)
movb 0xf(%rsp), %al
movb %al, 0xa4(%rbx)
andl $0x0, 0x90(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x9c(%rbx)
jmp 0x2fc9
movq %rbx, %rdi
callq 0x184d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop