// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsFDCEmulator.cpp
// Function: FdcComT1_Step(CapsFdc*)
void FdcComT1_Step(PCAPSFDC pc)
{
	// seek or restore
	if (pc->r_command < 0x20) {
		// skip loop if destination track reached
		if (pc->r_track == pc->r_data) {
			pc->runstate+=2;
			return;
		}

		// if destination>track step-in, else step-out
		if (pc->r_data > pc->r_track)
			pc->lineout|=CAPSFDC_LO_DIRC;
		else
			pc->lineout&=~CAPSFDC_LO_DIRC;
	}

	// step direction, 0: out
	int dir=pc->lineout & CAPSFDC_LO_DIRC;

	// if seek, restore or step with update requested
	if (pc->r_command<0x20 || (pc->r_command & DF_4)) {
		// update track register
		if (dir) {
			// step-in, 255 wraps to 1 (due to add with carry)
			if (pc->r_track == 0xff)
				pc->r_track=0x01;
			else
				pc->r_track++;
		} else {
			// step-out, 0 wraps to 0xfe (due to subtract with carry)
			if (!pc->r_track)
				pc->r_track=0xfe;
			else
				pc->r_track--;
		}
	}

	// get track# if valid drive or -1
	int track=pc->driveprc ? pc->driveprc->track : -1;

	// if on track0 and step-out, track=0, set tr0, skip loop
	if (!track && !dir) {
		pc->r_track=0;
		pc->r_st0|=CAPSFDC_SR_TR0_LD;
		pc->runstate+=2;
		return;
	}

	// step pulse if drive is valid
	if (track >= 0) {
		if (dir) {
			// step-in, hard stop head on last valid track
			if (track >= pc->driveprc->maxtrack)
				track=pc->driveprc->maxtrack;
			else
				track++;

			// clear tr0, since we can't be on track0 anymore
			pc->r_st0&=~CAPSFDC_SR_TR0_LD;
		} else {
			// step-out, hard stop head on track0
			if (track > 0)
				track--;

			// set tr0 if on track0
			if (!track)
				pc->r_st0|=CAPSFDC_SR_TR0_LD;
		}

		// set new track#
		pc->driveprc->track=track;
	}

	// set step delay
	pc->clockcnt=pc->clockstep[pc->r_command & 3];

	// set loop
	pc->runstate++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x20, 0x3c(%rax)
jae 0x8de4
movq -0x8(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jne 0x8db4
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x8f7b
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jbe 0x8dd4
movq -0x8(%rbp), %rax
movl 0x24(%rax), %ecx
orq $0x10, %rcx
movl %ecx, 0x24(%rax)
jmp 0x8de2
movq -0x8(%rbp), %rax
movl 0x24(%rax), %ecx
andq $-0x11, %rcx
movl %ecx, 0x24(%rax)
jmp 0x8de4
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
andq $0x10, %rax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x20, 0x3c(%rax)
jb 0x8e0d
movq -0x8(%rbp), %rax
movl 0x3c(%rax), %eax
andq $0x10, %rax
cmpq $0x0, %rax
je 0x8e64
cmpl $0x0, -0xc(%rbp)
je 0x8e3c
movq -0x8(%rbp), %rax
cmpl $0xff, 0x40(%rax)
jne 0x8e2d
movq -0x8(%rbp), %rax
movl $0x1, 0x40(%rax)
jmp 0x8e3a
movq -0x8(%rbp), %rax
movl 0x40(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x40(%rax)
jmp 0x8e62
movq -0x8(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x8e53
movq -0x8(%rbp), %rax
movl $0xfe, 0x40(%rax)
jmp 0x8e60
movq -0x8(%rbp), %rax
movl 0x40(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x40(%rax)
jmp 0x8e62
jmp 0x8e64
movq -0x8(%rbp), %rax
cmpq $0x0, 0x100(%rax)
je 0x8e85
movq -0x8(%rbp), %rax
movq 0x100(%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x8e8f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x14(%rbp)
jmp 0x8e8f
movl -0x14(%rbp), %eax
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x8ecc
cmpl $0x0, -0xc(%rbp)
jne 0x8ecc
movq -0x8(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x4, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x8f7b
cmpl $0x0, -0x10(%rbp)
jl 0x8f4d
cmpl $0x0, -0xc(%rbp)
je 0x8f17
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x100(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jl 0x8efe
movq -0x8(%rbp), %rax
movq 0x100(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x10(%rbp)
jmp 0x8f07
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
andq $-0x5, %rcx
movl %ecx, 0x30(%rax)
jmp 0x8f3c
cmpl $0x0, -0x10(%rbp)
jle 0x8f26
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x8f3a
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x4, %rcx
movl %ecx, 0x30(%rax)
jmp 0x8f3c
movl -0x10(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x100(%rax), %rax
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x3c(%rcx), %ecx
andl $0x3, %ecx
movl %ecx, %ecx
movl 0xc8(%rax,%rcx,4), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xb4(%rax)
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
popq %rbp
retq
nopl (%rax)