// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/main.c
// Function: run_emulator
void run_emulator(void) {
	// a sanity check for blocksize definition
	if (TU58_BLOCKSIZE % TU_DATA_LEN != 0)
		fatal("illegal BLOCKSIZE (%d) / TU_DATA_LEN (%d) ratio", TU58_BLOCKSIZE,
		TU_DATA_LEN);

	// say hello
	info("TU58 emulation start");
#ifdef DEVICEDIALOG
	info("0-7 device dialog, R restart, S toggle send init, V toggle verbose, D toggle debug, Q quit");
#else
	info("R restart, S toggle send init, V toggle verbose, D toggle debug, Q quit");
#endif

	// run the emulator
	if (pthread_create(&th_run, NULL, tu58_server, NULL))
		error("unable to create emulation thread");

	// run the monitor
	if (pthread_create(&th_monitor, NULL, tu58_monitor, NULL))
		error("unable to create monitor thread");

	// loop on user input
	for (;;) {
		uint8_t c;

		// get char from stdin (if available)
		if ((c = toupper(conget())) > 0) {
#ifdef DEVICEDIALOG
			if (c >= '0' && c <= '7') {
				image_t *img = NULL;
				int unit = c - '0';
				// number of open device?
				if (IMAGE_UNIT_VALID(unit))
				img = tu58image_get(unit);
				if (img && img->open)
				device_dialog(img);
			}
#endif
			if (c == 'V') {
				// toggle verbosity
				opt_verbose ^= 1;
				opt_debug = 0;
				info("verbosity set to %s; debug %s", opt_verbose ? "ON" : "OFF",
						opt_debug ? "ON" : "OFF");
			} else if (c == 'D') {
				// toggle debug
				opt_verbose = 1;
				opt_debug ^= 1;
				info("verbosity set to %s; debug %s", opt_verbose ? "ON" : "OFF",
						opt_debug ? "ON" : "OFF");
			} else if (c == 'S') {
				// toggle sending init string
				tu58_doinit = (tu58_doinit + 1) % 2;
				if (opt_debug)
					fprintf(ferr, "\n");
				info("send of <INIT> %sabled", tu58_doinit ? "en" : "dis");
			} else if (c == 'R') {
				// kill and restart the emulator
				if (pthread_cancel(th_run))
					error("unable to cancel emulation thread");
				if (pthread_join(th_run, NULL))
					error("unable to join on emulation thread");
				if (pthread_create(&th_run, NULL, tu58_server, NULL))
					error("unable to restart emulation thread");
			} else if (c == 'Q') {
				// kill the emulator and exit
				if (pthread_cancel(th_monitor))
					error("unable to cancel monitor thread");
				if (pthread_cancel(th_run))
					error("unable to cancel emulation thread");
				break;
			}
		}

		// wait a bit
		delay_ms(25);

	} // for (;;)

	// wait for emulator to finish
	if (pthread_join(th_run, NULL))
		error("unable to join on emulation thread");

	// all done
	info("TU58 emulation end");
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
leaq 0x157b8(%rip), %rdi     # 0x190e7
movb $0x0, %al
callq 0xcf60
leaq 0x157bf(%rip), %rdi     # 0x190fc
movb $0x0, %al
callq 0xcf60
leaq 0x2160d(%rip), %rdi     # 0x24f58
xorl %eax, %eax
movl %eax, %ecx
leaq 0x429a(%rip), %rdx      # 0x7bf0
movq %rcx, %rsi
callq 0x3430
cmpl $0x0, %eax
je 0x3971
leaq 0x157da(%rip), %rdi     # 0x19144
movb $0x0, %al
callq 0xce70
leaq 0x215e8(%rip), %rdi     # 0x24f60
xorl %eax, %eax
movl %eax, %ecx
leaq 0x4cdd(%rip), %rdx      # 0x8660
movq %rcx, %rsi
callq 0x3430
cmpl $0x0, %eax
je 0x399e
leaq 0x157cf(%rip), %rdi     # 0x19166
movb $0x0, %al
callq 0xce70
jmp 0x39a0
callq 0xb090
movl %eax, %edi
callq 0x30e0
movb %al, -0x1(%rbp)
movzbl %al, %eax
cmpl $0x0, %eax
jle 0x3bda
movzbl -0x1(%rbp), %eax
cmpl $0x56, %eax
jne 0x3a26
movl 0x2039a(%rip), %eax     # 0x23d64
xorl $0x1, %eax
movl %eax, 0x20391(%rip)     # 0x23d64
movl $0x0, 0x20397(%rip)     # 0x23d74
movl 0x20381(%rip), %ecx     # 0x23d64
leaq 0x157ba(%rip), %rsi     # 0x191a4
leaq 0x19b9c(%rip), %rax     # 0x1d58d
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movl 0x20376(%rip), %ecx     # 0x23d74
leaq 0x1579f(%rip), %rdx     # 0x191a4
leaq 0x19b81(%rip), %rax     # 0x1d58d
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq 0x1576c(%rip), %rdi     # 0x19186
movb $0x0, %al
callq 0xcf60
jmp 0x3bd8
movzbl -0x1(%rbp), %eax
cmpl $0x44, %eax
jne 0x3a91
movl $0x1, 0x2032b(%rip)     # 0x23d64
movl 0x20335(%rip), %eax     # 0x23d74
xorl $0x1, %eax
movl %eax, 0x2032c(%rip)     # 0x23d74
movl 0x20316(%rip), %ecx     # 0x23d64
leaq 0x1574f(%rip), %rsi     # 0x191a4
leaq 0x19b31(%rip), %rax     # 0x1d58d
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movl 0x2030b(%rip), %ecx     # 0x23d74
leaq 0x15734(%rip), %rdx     # 0x191a4
leaq 0x19b16(%rip), %rax     # 0x1d58d
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq 0x15701(%rip), %rdi     # 0x19186
movb $0x0, %al
callq 0xcf60
jmp 0x3bd6
movzbl -0x1(%rbp), %eax
cmpl $0x53, %eax
jne 0x3b0d
leaq 0x216d0(%rip), %rax     # 0x25171
movzbl (%rax), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movb %dl, %cl
leaq 0x216b9(%rip), %rax     # 0x25171
movb %cl, (%rax)
cmpl $0x0, 0x202b3(%rip)     # 0x23d74
je 0x3adb
leaq 0x21a66(%rip), %rax     # 0x25530
movq (%rax), %rdi
leaq 0x18bef(%rip), %rsi     # 0x1c6c3
movb $0x0, %al
callq 0x3300
leaq 0x2168f(%rip), %rax     # 0x25171
movzbl (%rax), %ecx
leaq 0x156d3(%rip), %rsi     # 0x191bf
leaq 0x18848(%rip), %rax     # 0x1c33b
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x156a7(%rip), %rdi     # 0x191a8
movb $0x0, %al
callq 0xcf60
jmp 0x3bd4
movzbl -0x1(%rbp), %eax
cmpl $0x52, %eax
jne 0x3b87
movq 0x2143b(%rip), %rdi     # 0x24f58
callq 0x3570
cmpl $0x0, %eax
je 0x3b35
leaq 0x15695(%rip), %rdi     # 0x191c3
movb $0x0, %al
callq 0xce70
movq 0x2141c(%rip), %rdi     # 0x24f58
xorl %eax, %eax
movl %eax, %esi
callq 0x3550
cmpl $0x0, %eax
je 0x3b58
leaq 0x15694(%rip), %rdi     # 0x191e5
movb $0x0, %al
callq 0xce70
leaq 0x213f9(%rip), %rdi     # 0x24f58
xorl %eax, %eax
movl %eax, %ecx
leaq 0x4086(%rip), %rdx      # 0x7bf0
movq %rcx, %rsi
callq 0x3430
cmpl $0x0, %eax
je 0x3b85
leaq 0x1568a(%rip), %rdi     # 0x19208
movb $0x0, %al
callq 0xce70
jmp 0x3bd2
movzbl -0x1(%rbp), %eax
cmpl $0x51, %eax
jne 0x3bd0
movq 0x213c9(%rip), %rdi     # 0x24f60
callq 0x3570
cmpl $0x0, %eax
je 0x3baf
leaq 0x15683(%rip), %rdi     # 0x1922b
movb $0x0, %al
callq 0xce70
movq 0x213a2(%rip), %rdi     # 0x24f58
callq 0x3570
cmpl $0x0, %eax
je 0x3bce
leaq 0x155fc(%rip), %rdi     # 0x191c3
movb $0x0, %al
callq 0xce70
jmp 0x3be9
jmp 0x3bd2
jmp 0x3bd4
jmp 0x3bd6
jmp 0x3bd8
jmp 0x3bda
movl $0x19, %edi
callq 0xd2a0
jmp 0x39a0
movq 0x21368(%rip), %rdi     # 0x24f58
xorl %eax, %eax
movl %eax, %esi
callq 0x3550
cmpl $0x0, %eax
je 0x3c0c
leaq 0x155e0(%rip), %rdi     # 0x191e5
movb $0x0, %al
callq 0xce70
leaq 0x15638(%rip), %rdi     # 0x1924b
movb $0x0, %al
callq 0xcf60
addq $0x10, %rsp
popq %rbp
retq