// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/tu58drive.c
// Function: tu58_server
void* tu58_server(void* none) {
	uint8_t flag = TUF_NULL;
	uint8_t last = TUF_NULL;
	UNUSED(none);

	// some init
	reinit(); // empty serial line buffers
	tu58_doinit = !opt_nosync; // start sending init flags?

	tu58_offline_request = 0;
	tu58_offline = 0;

	// say hello
	info("emulator %sstarted", tu58_runonce++ ? "re" : "");

	// loop forever ... almost
	for (;;) {

		if (tu58_offline_request && !tu58_offline) {
			// if requested, go offline after inactivity timeout

			if (tu58_serial.rx_lasttime_ms + opt_offlinetimeout_sec * 1000 < now_ms()
					&& tu58_serial.tx_lasttime_ms + opt_offlinetimeout_sec * 1000 < now_ms()) {
				tu58_offline = 1;
				if (opt_verbose)
					info("TU58 now offline");
			}
		} else if (!tu58_offline_request && tu58_offline) {
			tu58_offline = 0;
			if (opt_verbose)
				info("TU58 now online");
		}
		// if offline, on read/write/seek a "no cartridge" is sent

		// loop while no characters are available
		if (serial_devrxavail(&tu58_serial) == 0) {
			// delays and printout only if not VAX
			if (!opt_vax) {
				// send INITs if still required
				if (tu58_doinit) {
					if (opt_debug)
						fprintf(ferr, ".");
					serial_devtxput(&tu58_serial, TUF_INIT);
					serial_devtxflush(&tu58_serial);
					tu58_serial.tx_lasttime_ms = 0; // does not count as traffic
					delay_ms(75);
				}
				delay_ms(25);
				continue; // loop again
			}
		} else
			tu58_doinit = 0; // quit sending init flags

#ifdef ORG
		// loop while no characters are available
		while (serial_devrxavail() == 0) {
			// delays and printout only if not VAX
			if (!opt_vax) {
				// send INITs if still required
				if (tu58_doinit) {
					if (opt_debug) fprintf(ferr, ".");
					serial_devtxput(TUF_INIT);
					serial_devtxflush();
					delay_ms(75);
				}
				delay_ms(25);
			}
		}
		tu58_doinit = 0; // quit sending init flags
#endif

		// process received characters
		last = flag;
		flag = serial_devrxget(&tu58_serial);
		if (opt_debug)
			info("flag=0x%02X last=0x%02X", flag, last);

		switch (flag) {

		case TUF_CTRL:
			// control packet - process
			command(flag);
			break;

		case TUF_INIT:
			// init flag
			if (opt_debug)
				info("<INIT> seen");
			if (last == TUF_INIT) {
				// two in a row is special
				if (!opt_vax)
					delay_ms(tudelay[opt_timing].init); // no delay for VAX
				serial_devtxput(&tu58_serial, TUF_CONT); // send 'continue'
				serial_devtxflush(&tu58_serial); // send immediate
				flag = -1; // undefined
				if (opt_debug)
					info("<INIT><INIT> seen, sending <CONT>");
			}
			break;

		case TUF_BOOT:
			// special boot sequence
			if (opt_debug)
				info("<BOOT> seen");
			bootio();
			break;

		case TUF_NULL:
			// ignore nulls (which are BREAKs)
			if (opt_debug)
				info("<NULL> seen");
			break;

		case TUF_CONT:
			// continue restarts output
			if (opt_debug)
				info("<CONT> seen, starting output");
			serial_devtxstart(&tu58_serial);
			break;

		case TUF_XOFF:
			// send disable flag stops output
			if (opt_debug)
				info("<XOFF> seen, stopping output");
			serial_devtxstop(&tu58_serial);
			break;

		case TUF_DATA:
			// data packet - should never see one here
			error("protocol error - data flag out of sequence");
			reinit();
			break;

		default:
			// whoops, protocol error
			error("unknown packet flag 0x%02X (%c)", flag,
			isprint(flag) ? flag : '.');
			break;

		} // switch (flag)

	} // for (;;)

	return (void*) 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
movb $0x0, -0xa(%rbp)
callq 0x7fe0
leaq 0x1c160(%rip), %rax     # 0x23d70
cmpl $0x0, (%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movb %al, 0x1d54e(%rip)      # 0x25171
movl $0x0, 0x1d593(%rip)     # 0x251c0
movl $0x0, 0x1d58d(%rip)     # 0x251c4
movb 0x1d535(%rip), %al      # 0x25172
movb %al, %cl
addb $0x1, %cl
movb %cl, 0x1d52a(%rip)      # 0x25172
movzbl %al, %ecx
leaq 0x14a72(%rip), %rsi     # 0x1c6c4
leaq 0x13b9d(%rip), %rax     # 0x1b7f6
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x13b7c(%rip), %rdi     # 0x1b7e3
movb $0x0, %al
callq 0xcf60
movl 0x1d54c(%rip), %eax     # 0x251c0
cmpl $0x0, %eax
je 0x7d16
movl 0x1d541(%rip), %eax     # 0x251c4
cmpl $0x0, %eax
jne 0x7d16
movq 0x1d545(%rip), %rax     # 0x251d8
leaq 0x1b672(%rip), %rcx     # 0x2330c
imull $0x3e8, (%rcx), %ecx    # imm = 0x3E8
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movb $0x0, %al
callq 0xd2c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
cmpq %rcx, %rax
jae 0x7d14
movq 0x1d51c(%rip), %rax     # 0x251e0
leaq 0x1b641(%rip), %rcx     # 0x2330c
imull $0x3e8, (%rcx), %ecx    # imm = 0x3E8
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movb $0x0, %al
callq 0xd2c0
movq %rax, %rcx
movq -0x20(%rbp), %rax
cmpq %rcx, %rax
jae 0x7d14
movl $0x1, 0x1d4cc(%rip)     # 0x251c4
leaq 0x1c065(%rip), %rax     # 0x23d64
cmpl $0x0, (%rax)
je 0x7d12
leaq 0x13aee(%rip), %rdi     # 0x1b7f9
movb $0x0, %al
callq 0xcf60
jmp 0x7d14
jmp 0x7d54
movl 0x1d4a4(%rip), %eax     # 0x251c0
cmpl $0x0, %eax
jne 0x7d52
movl 0x1d49d(%rip), %eax     # 0x251c4
cmpl $0x0, %eax
je 0x7d52
movl $0x0, 0x1d48e(%rip)     # 0x251c4
leaq 0x1c027(%rip), %rax     # 0x23d64
cmpl $0x0, (%rax)
je 0x7d50
leaq 0x13ac1(%rip), %rdi     # 0x1b80a
movb $0x0, %al
callq 0xcf60
jmp 0x7d52
jmp 0x7d54
leaq 0x1d46d(%rip), %rdi     # 0x251c8
callq 0xa7a0
cmpl $0x0, %eax
jne 0x7de1
leaq 0x1c00c(%rip), %rax     # 0x23d78
cmpl $0x0, (%rax)
jne 0x7ddf
cmpb $0x0, 0x1d3f9(%rip)     # 0x25171
je 0x7dd0
leaq 0x1bff3(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7d9e
leaq 0x1d7a3(%rip), %rax     # 0x25530
movq (%rax), %rdi
leaq 0x1324b(%rip), %rsi     # 0x1afe2
movb $0x0, %al
callq 0x3300
leaq 0x1d423(%rip), %rdi     # 0x251c8
movl $0x4, %esi
callq 0xa9b0
leaq 0x1d412(%rip), %rdi     # 0x251c8
callq 0xa8a0
movq $0x0, 0x1d41a(%rip) # 0x251e0
movl $0x4b, %edi
callq 0xd2a0
movl $0x19, %edi
callq 0xd2a0
jmp 0x7c6e
jmp 0x7de8
movb $0x0, 0x1d389(%rip)     # 0x25171
movb -0x9(%rbp), %al
movb %al, -0xa(%rbp)
leaq 0x1d3d3(%rip), %rdi     # 0x251c8
callq 0xa940
movb %al, -0x9(%rbp)
leaq 0x1bf70(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7e1f
movzbl -0x9(%rbp), %esi
movzbl -0xa(%rbp), %edx
leaq 0x13a02(%rip), %rdi     # 0x1b81a
movb $0x0, %al
callq 0xcf60
movzbl -0x9(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x13, %rax
ja 0x7f90
movq -0x28(%rbp), %rax
leaq 0x138b8(%rip), %rcx     # 0x1b6f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl -0x9(%rbp), %edi
callq 0x8050
jmp 0x7fdb
leaq 0x1bf1a(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7e6d
leaq 0x139cc(%rip), %rdi     # 0x1b832
movb $0x0, %al
callq 0xcf60
movzbl -0xa(%rbp), %eax
cmpl $0x4, %eax
jne 0x7ee0
leaq 0x1befb(%rip), %rax     # 0x23d78
cmpl $0x0, (%rax)
jne 0x7ea3
leaq 0x1bedf(%rip), %rax     # 0x23d68
movslq (%rax), %rcx
leaq 0x1b73d(%rip), %rax     # 0x235d0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movzwl 0x2(%rax), %edi
callq 0xd2a0
leaq 0x1d31e(%rip), %rdi     # 0x251c8
movl $0x10, %esi
callq 0xa9b0
leaq 0x1d30d(%rip), %rdi     # 0x251c8
callq 0xa8a0
movb $-0x1, -0x9(%rbp)
leaq 0x1bea9(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7ede
leaq 0x13967(%rip), %rdi     # 0x1b83e
movb $0x0, %al
callq 0xcf60
jmp 0x7ee0
jmp 0x7fdb
leaq 0x1be88(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7eff
leaq 0x13968(%rip), %rdi     # 0x1b860
movb $0x0, %al
callq 0xcf60
callq 0x8530
jmp 0x7fdb
leaq 0x1be64(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7f23
leaq 0x13950(%rip), %rdi     # 0x1b86c
movb $0x0, %al
callq 0xcf60
jmp 0x7fdb
leaq 0x1be45(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7f42
leaq 0x1393d(%rip), %rdi     # 0x1b878
movb $0x0, %al
callq 0xcf60
leaq 0x1d27f(%rip), %rdi     # 0x251c8
callq 0xa6a0
jmp 0x7fdb
leaq 0x1be1a(%rip), %rax     # 0x23d74
cmpl $0x0, (%rax)
je 0x7f6d
leaq 0x1392f(%rip), %rdi     # 0x1b895
movb $0x0, %al
callq 0xcf60
leaq 0x1d254(%rip), %rdi     # 0x251c8
callq 0xa680
jmp 0x7fdb
leaq 0x13930(%rip), %rdi     # 0x1b8b2
movb $0x0, %al
callq 0xce70
callq 0x7fe0
jmp 0x7fdb
movzbl -0x9(%rbp), %eax
movl %eax, -0x2c(%rbp)
callq 0x35c0
movq (%rax), %rax
movzbl -0x9(%rbp), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x7fbd
movzbl -0x9(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x7fc7
movl $0x2e, %eax
movl %eax, -0x30(%rbp)
jmp 0x7fc7
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
leaq 0x13909(%rip), %rdi     # 0x1b8dd
movb $0x0, %al
callq 0xce70
jmp 0x7c6e