// File: /workspace/github_cpp_makefile_star2_O1/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsFDCEmulator.cpp
// Function: FdcComReadDMap(CapsFdc*)
int FdcComReadDMap(PCAPSFDC pc)
{
	// clear dsr signals
	pc->aminfo&=~(CAPSFDC_AI_DSRREADY|CAPSFDC_AI_DSRAM|CAPSFDC_AI_DSRMA1);

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

	// all cycles possibly advanced this time
	UDWORD dist=pd->idistance+(pc->clockreq-pc->clockact);

	// bit complete
	UDWORD sum=pd->timebuf[pd->tracklen-1];
	UDWORD base=pc->datacycle;
	PUDWORD time=pd->timebuf+(pc->datalock>>3);
	UDWORD diff=*time-base;
	int hi=(pc->datalock&7)+1;

	// process until broken
	while (true) {
		// bit is complete at this disk cycle
		UDWORD nextcyc=UDWORD(((UQUAD)(base+(hi*diff)/8)*pd->clockrev)/sum);

		// if not enough time to read bit, return
		if (dist < nextcyc) {
			pd->idistance=dist;
			pc->clockact=pc->clockreq;
			return 0;
		}

		// read bit into shifter
		FdcShiftBit(pc);

		// next bit
		pc->datalock++;

		// next bit timing
		if (hi == 8) {
			hi=1;
			base=*time++;
			pc->datacycle=base;
			diff=*time-base;
		} else
			hi++;

		// if index reached
		if (nextcyc >= pd->clockrev) {
			// adjust to index position
			dist-=pd->clockrev;
			pc->clockact+=pd->clockrev-pd->idistance;
			pd->idistance=0;
			pc->datalock=0;
			base=0;
			pc->datacycle=0;
			time=pd->timebuf;
			diff=*time;
			hi=1;

			// process disk index
			FdcIndex(pc, pc->driveact);

			// we read a complete byte exactly at index, set flag
			if (pc->aminfo & pc->amisigmask)
				return 1;

			// stop on end
			if (pc->endrequest)
				return 0;
		}

		// if a byte is complete, break and signal new byte
		if (pc->aminfo & pc->amisigmask) {
			int dst=pd->idistance;
			pd->idistance=nextcyc;
			pc->clockact+=nextcyc-dst;
			return 1;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
andb $-0x8, 0x61(%rdi)
movq 0x100(%rdi), %r8
movl 0x10(%rdi), %edi
addl 0x24(%r8), %edi
movl 0x80(%rbx), %r12d
subl 0xc(%rbx), %edi
movq 0x40(%r8), %rax
movl 0x48(%r8), %ecx
decl %ecx
movl (%rax,%rcx,4), %esi
movl 0x88(%rbx), %r15d
movl %r12d, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x18(%rsp)
movl (%rax), %ebp
subl %r15d, %ebp
andl $0x7, %r12d
incl %r12d
movq %rsi, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movl %eax, %r9d
movl %r12d, %ecx
imull %ebp, %ecx
shrl $0x3, %ecx
addl %r15d, %ecx
movl 0x28(%r8), %eax
imulq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r14
cmpl %r14d, %edi
jae 0x825f
movl %edi, 0x24(%r8)
movl 0x10(%rbx), %ecx
xorl %eax, %eax
jmp 0x82df
movl %r9d, 0x24(%rsp)
movl %ebp, %r13d
movl %edi, %ebp
movq %rbx, %rdi
callq 0x8398
incl 0x80(%rbx)
cmpl $0x8, %r12d
je 0x8282
incl %r12d
jmp 0x82a8
movq 0x18(%rsp), %rax
movl (%rax), %ecx
movl %ecx, 0x88(%rbx)
movl 0x4(%rax), %r13d
addq $0x4, %rax
movl %ecx, %r15d
subl %ecx, %r13d
movl $0x1, %r12d
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %r8
movl 0x28(%r8), %eax
cmpl %r14d, %eax
movl %ebp, %edi
jbe 0x82f1
movq 0x10(%rsp), %rsi
movl %r13d, %ebp
movl 0x64(%rbx), %eax
testl %eax, 0x60(%rbx)
je 0x8351
movl %r14d, %ecx
subl 0x24(%r8), %ecx
movl %r14d, 0x24(%r8)
addl 0xc(%rbx), %ecx
movl $0x1, %eax
movl %ecx, 0xc(%rbx)
xorl %ecx, %ecx
testb %cl, %cl
jne 0x822d
jmp 0x8389
subl %eax, %edi
movl %edi, 0x4(%rsp)
subl 0x24(%r8), %eax
addl %eax, 0xc(%rbx)
xorl %r15d, %r15d
movl %r15d, 0x24(%r8)
movl %r15d, 0x80(%rbx)
movl %r15d, 0x88(%rbx)
movq 0x40(%r8), %rax
movq %rax, 0x18(%rsp)
movl (%rax), %ebp
movl 0xfc(%rbx), %esi
movq %rbx, %rdi
callq 0x7be5
movl 0x64(%rbx), %eax
testl %eax, 0x60(%rbx)
je 0x8359
movl $0x1, %r12d
xorl %ecx, %ecx
movl $0x1, %eax
movq 0x10(%rsp), %rsi
movl 0x4(%rsp), %edi
movq 0x8(%rsp), %r8
jmp 0x82e4
movb $0x1, %cl
movl 0x24(%rsp), %eax
jmp 0x82e4
movl $0x1, %r12d
xorl %r15d, %r15d
cmpl $0x0, 0x8(%rbx)
movl $0x0, %ecx
movl $0x0, %eax
movq 0x10(%rsp), %rsi
movl 0x4(%rsp), %edi
movq 0x8(%rsp), %r8
jne 0x82e4
jmp 0x82c0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq