// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsFDCEmulator.cpp
// Function: FdcIndex(CapsFdc*, int)
void FdcIndex(PCAPSFDC pc, int drive)
{
	// must be valid drive
	if (drive < 0)
		return;

	// get selected drive
	PCAPSDRIVE pd=pc->drive+drive;

	// if drive empty, no index signal, stop
	if (!(pd->diskattr & CAPSDRIVE_DA_IN))
		return;

	// start index pulse
	pd->ipcnt=-1;

	// if track is changing, request new track data
	if ((pd->ttype & CTIT_FLAG_FLAKEY))
		pc->cbtrk(pc, drive);

	// update track parameters
	FdcUpdateTrack(pc, drive);

	// if drive not selected, index signal not routed, stop
	if (drive != pc->driveact)
		return;

	// set index signal
	pc->r_st0|=CAPSFDC_SR_IP_DRQ;

	// increment index pulse counter
	pc->indexcount++;

	// if index abort allowed and limit reached set end and disable abort
	if (pc->indexlimit>=0 && pc->indexcount>=pc->indexlimit) {
		pc->endrequest|=CAPSFDC_ER_COMEND;
		pc->indexlimit=-1;
	}

	// set spin-up flag if limit reached
	if (pc->spinupcnt>=pc->spinuplimit || ++pc->spinupcnt>=pc->spinuplimit)
		pc->r_st0|=CAPSFDC_SR_SU_RT;

	// if fdc is not busy turn motor off once idle limit reached, apparently SR spin-up is not reset
	if (!(pc->r_st0 & CAPSFDC_SR_BUSY)) {
		if (pc->idlecnt>=pc->idlelimit || ++pc->idlecnt>=pc->idlelimit) {
			pc->lineout&=~CAPSFDC_LO_MO;
			pd->diskattr&=~CAPSDRIVE_DA_MO;
			pc->r_st0&=~CAPSFDC_SR_MO;
			pc->spinupcnt=0;
		}
	}

	// set index pulse interrupt if enabled
	if (pc->lineout & CAPSFDC_LO_INTIP)
		FdcSetLine(pc, pc->lineout | CAPSFDC_LO_INTRQ);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0xb2ea
jmp 0xb4c0
movq -0x8(%rbp), %rax
movq 0x108(%rax), %rax
movslq -0xc(%rbp), %rcx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0xb31a
jmp 0xb4c0
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl 0x34(%rax), %eax
movabsq $0x80000000, %rcx       # imm = 0x80000000
andq %rcx, %rax
cmpq $0x0, %rax
je 0xb353
movq -0x8(%rbp), %rax
movq 0x120(%rax), %rax
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq *%rax
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0xb600
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xfc(%rcx), %eax
je 0xb373
jmp 0xb4c0
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x2, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl 0x94(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x94(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x98(%rax)
jl 0xb3d3
movq -0x8(%rbp), %rax
movl 0x94(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x98(%rcx), %eax
jl 0xb3d3
movq -0x8(%rbp), %rax
movl 0x8(%rax), %ecx
orq $0x1, %rcx
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rax
movl $0xffffffff, 0x98(%rax) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movl 0xa4(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0xa8(%rcx), %eax
jge 0xb408
movq -0x8(%rbp), %rcx
movl 0xa4(%rcx), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rcx)
movq -0x8(%rbp), %rcx
cmpl 0xa8(%rcx), %eax
jl 0xb416
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
orq $0x20, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0xb499
movq -0x8(%rbp), %rax
movl 0xac(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0xb0(%rcx), %eax
jge 0xb45c
movq -0x8(%rbp), %rcx
movl 0xac(%rcx), %eax
addl $0x1, %eax
movl %eax, 0xac(%rcx)
movq -0x8(%rbp), %rcx
cmpl 0xb0(%rcx), %eax
jl 0xb497
movq -0x8(%rbp), %rax
movl 0x24(%rax), %ecx
andq $-0x9, %rcx
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movl 0x20(%rax), %ecx
andq $-0x5, %rcx
movl %ecx, 0x20(%rax)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %ecx
andq $-0x81, %rcx
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0xa4(%rax)
jmp 0xb499
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
andq $0x20, %rax
cmpq $0x0, %rax
je 0xb4c0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
orq $0x2, %rax
movl %eax, %esi
callq 0xa860
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)