// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl $0x1, 0x3c(%rax)
movq -0x8(%rbp), %rdi
addq $0xc0, %rdi
movl $0xfe502a, %esi         # imm = 0xFE502A
callq 0x5e40
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
je 0x4c0e
movq -0x8(%rbp), %rdi
addq $0xc0, %rdi
callq 0x5e90
cmpl $0x0, %eax
je 0x4a42
movq -0x8(%rbp), %rax
movl $0x1, 0x88(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x8c(%rax)
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
andl $-0x11, %ecx
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8c(%rax)
je 0x4a76
movq -0x8(%rbp), %rax
cmpl $0x0, 0x84(%rax)
je 0x4a76
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
orl $0x10, %ecx
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
andl $0xfffffeff, %ecx       # imm = 0xFFFFFEFF
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0xb0(%rax)
je 0x4ab0
movq -0x8(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
je 0x4ab0
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
orl $0x100, %ecx            # imm = 0x100
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x9c(%rax)
je 0x4aea
movq -0x8(%rbp), %rax
cmpl $0x0, 0x98(%rax)
je 0x4aea
movq -0x8(%rbp), %rax
movl 0x34(%rax), %ecx
orl $0x80, %ecx
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x4c20
movq -0x8(%rbp), %rax
cmpl $0x1, 0x3c(%rax)
je 0x4b19
movq -0x8(%rbp), %rax
cmpl $0x2, 0x3c(%rax)
jne 0x4b2d
movq -0x8(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x4b2d
movq -0x8(%rbp), %rax
movl $0x1, 0x3c(%rax)
movq -0x8(%rbp), %rdi
callq 0x2630
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
cmpl $0x4e20, -0xc(%rbp)     # imm = 0x4E20
je 0x4b44
jmp 0x4a03
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0xb4(%rax)
jne 0x4b9d
movq -0x8(%rbp), %rax
movzbl 0xb8(%rax), %eax
andl $0x7f, %eax
movb %al, -0xe(%rbp)
movq -0x8(%rbp), %rax
movl 0xbc(%rax), %edi
leaq -0xe(%rbp), %rsi
movl $0x1, %edx
callq 0x1070
movq -0x8(%rbp), %rax
movl $0x1, 0xb4(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0xb0(%rax)
movq -0x8(%rbp), %rdi
addq $0xbc, %rdi
leaq -0xd(%rbp), %rsi
callq 0x5fb0
cmpl $0x0, %eax
je 0x4c09
movq -0x8(%rbp), %rax
movl $0x1, 0x90(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x94(%rax)
movb -0xd(%rbp), %cl
movq -0x8(%rbp), %rax
movb %cl, 0xa4(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0xa0(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x90(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x9c(%rax)
jmp 0x4a03
movq -0x8(%rbp), %rdi
callq 0x1c60
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax)