// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsFDCEmulator.cpp
// Function: FdcComT1_VerifyLoop(CapsFdc*)
void FdcComT1_VerifyLoop(PCAPSFDC pc)
{
	// update data access
	FdcUpdateData(pc);

	// get access hook, crc
	FDCREAD ph=FdcGetReadAccess(pc);

	// clock cycles to complete
	UDWORD cst=pc->clockact;

	// process requested cycles
	while (!pc->endrequest && pc->clockact<pc->clockreq) {
		// read byte when ready
		if (!ph(pc))
			continue;

		// check ID
		switch (pc->dataphase) {
			case 0:
				// AM detected, read returns on dsr ready
				pc->amisigmask=CAPSFDC_AI_DSRREADY;
				pc->dataphase++;
				break;

			case 1:
				//if not fc...ff, restart
				if (pc->dsr < 0xfc) {
					FdcResetAm(pc);
					continue;
				}

				pc->dataphase++;
				break;

			case 2:
				//if not the track, restart
				if (pc->dsr != pc->r_track) {
					FdcResetAm(pc);
					continue;
				}

				pc->dataphase++;
				break;

			// skip rest ofID
			case 3:
			case 4:
			case 5:
			case 6:
				pc->dataphase++;
				break;

			case 7:
				// if crc error, set crc flag, restart
				if (pc->crc & 0xffff) {
					FdcResetAm(pc);
					pc->r_st0|=CAPSFDC_SR_CRCERR;
					continue;
				}

				// clear crc flag, stop
				pc->r_st0&=~CAPSFDC_SR_CRCERR;

				// run cycles used on unselected drives
				FdcComIdleOther(pc, pc->clockact-cst);

				// end
				pc->endrequest|=CAPSFDC_ER_COMEND;
				return;

			default:
				NODEFAULT;
		}
	}

	// end requested, set seek error
	if (pc->endrequest & CAPSFDC_ER_COMEND)
		pc->r_st0|=CAPSFDC_SR_RNF;

	// run cycles on unselected drives
	FdcComIdleOther(pc, pc->clockact-cst);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0xb000
movq -0x8(%rbp), %rdi
callq 0xb130
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x8(%rcx)
movb %al, -0x15(%rbp)
jne 0x917f
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
setb %al
movb %al, -0x15(%rbp)
movb -0x15(%rbp), %al
testb $0x1, %al
jne 0x918b
jmp 0x92d1
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdi
callq *%rax
cmpl $0x0, %eax
jne 0x919c
jmp 0x915c
movq -0x8(%rbp), %rax
movl 0x8c(%rax), %eax
movq %rax, -0x20(%rbp)
subq $0x7, %rax
ja 0x92ca
movq -0x20(%rbp), %rax
leaq 0x14ebd(%rip), %rcx     # 0x1e07c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movl $0x100, 0x64(%rax)      # imm = 0x100
movq -0x8(%rbp), %rax
movl 0x8c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8c(%rax)
jmp 0x92cc
movq -0x8(%rbp), %rax
cmpl $0xfc, 0x78(%rax)
jae 0x9208
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xb1a0
jmp 0x915c
movq -0x8(%rbp), %rax
movl 0x8c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8c(%rax)
jmp 0x92cc
movq -0x8(%rbp), %rax
movl 0x78(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x40(%rcx), %eax
je 0x9240
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xb1a0
jmp 0x915c
movq -0x8(%rbp), %rax
movl 0x8c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8c(%rax)
jmp 0x92cc
movq -0x8(%rbp), %rax
movl 0x8c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8c(%rax)
jmp 0x92cc
movq -0x8(%rbp), %rax
movl 0x54(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cmpl $0x0, %eax
je 0x9299
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xb1a0
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x8, %rcx
movl %ecx, 0x30(%rax)
jmp 0x915c
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
andq $-0x9, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %esi
subl -0x14(%rbp), %esi
callq 0xb200
movq -0x8(%rbp), %rax
movl 0x8(%rax), %ecx
orq $0x1, %rcx
movl %ecx, 0x8(%rax)
jmp 0x9303
jmp 0x92cc
jmp 0x915c
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x92f0
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x10, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %esi
subl -0x14(%rbp), %esi
callq 0xb200
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)