// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsFormatMFM.cpp
// Function: FmfmWriteBlockData(CapsFormatTrack*, unsigned int, CapsFormatBlock*)
UDWORD FmfmWriteBlockData(PCAPSFORMATTRACK pf, UDWORD state, PCAPSFORMATBLOCK pb)
{
	UWORD crc;

	// sector length
	int seclen=FmfmSectorLength(pb->sectorlen);

	// write sector ID
	// gap before first am
	state=FmfmWriteDataByte(pf, state, pb->gapavalue, pb->gapacnt);

	// 3 am a1/4489
	crc=~0;
	state=FmfmWriteMarkByte(pf, state, 0x4489, 3);
	crc=FmfmCrc(crc, 0xa1, 3);

	// fe
	state=FmfmWriteDataByte(pf, state, 0xfe, 1);
	crc=FmfmCrc(crc, 0xfe);

	// track#
	state=FmfmWriteDataByte(pf, state, pb->track, 1);
	crc=FmfmCrc(crc, pb->track);

	// side#
	state=FmfmWriteDataByte(pf, state, pb->side, 1);
	crc=FmfmCrc(crc, pb->side);

	// sector#
	state=FmfmWriteDataByte(pf, state, pb->sector, 1);
	crc=FmfmCrc(crc, pb->sector);

	// sector length
	state=FmfmWriteDataByte(pf, state, seclen, 1);
	crc=FmfmCrc(crc, seclen);

	// crc
	state=FmfmWriteDataByte(pf, state, crc>>8, 1);
	state=FmfmWriteDataByte(pf, state, crc, 1);

	// gap after first am
	state=FmfmWriteDataByte(pf, state, pb->gapbvalue, pb->gapbcnt);

	// write sector data
	// gap before second am
	state=FmfmWriteDataByte(pf, state, pb->gapcvalue, pb->gapccnt);

	// 3 am a1/4489
	crc=~0;
	state=FmfmWriteMarkByte(pf, state, 0x4489, 3);
	crc=FmfmCrc(crc, 0xa1, 3);

	// fb
	state=FmfmWriteDataByte(pf, state, 0xfb, 1);
	crc=FmfmCrc(crc, 0xfb);

	// data
	for (int i=0; i < pb->sectorlen; i++) {
		UDWORD data=pb->databuf ? pb->databuf[i] : pb->datavalue;
		state=FmfmWriteDataByte(pf, state, data, 1);
		crc=FmfmCrc(crc, data);
	}

	// crc
	state=FmfmWriteDataByte(pf, state, crc>>8, 1);
	state=FmfmWriteDataByte(pf, state, crc, 1);

	// gap after second am
	state=FmfmWriteDataByte(pf, state, pb->gapdvalue, pb->gapdcnt);

	return state;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rdi, %rbx
movl 0x30(%rdx), %eax
addl $-0x80, %eax
roll $0x19, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x7, %eax
ja 0x8913
movl %eax, %eax
leaq 0x98b0(%rip), %rcx      # 0x121c0
movl (%rcx,%rax,4), %ebp
movl (%r14), %ecx
movl 0x4(%r14), %edx
movq %rbx, %rdi
callq 0x8822
movq %rbx, (%rsp)
movq %rbx, %rdi
movl $0x4489, %edx           # imm = 0x4489
movl $0x3, %ecx
callq 0x8b88
movw $0xffff, %r12w          # imm = 0xFFFF
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
leaq 0x117a7(%rip), %r15     # 0x1a0f0
movzwl %r12w, %edx
shrl $0x8, %edx
xorl $0xa1, %edx
movzwl (%r15,%rdx,2), %ebx
shll $0x8, %r12d
xorl %ebx, %r12d
incl %ecx
jne 0x8949
movq (%rsp), %r13
movq %r13, %rdi
movl %eax, %esi
movl $0xfe, %edx
movl $0x1, %ecx
callq 0x8822
movzwl %r12w, %ecx
shrl $0x8, %ecx
xorl $0xfe, %ecx
movzwl (%r15,%rcx,2), %r12d
shll $0x8, %ebx
xorl %r12d, %ebx
movl 0x24(%r14), %edx
movq %r13, %rdi
movl %eax, %esi
movl $0x1, %ecx
callq 0x8822
movzbl 0x24(%r14), %ecx
movzbl %bh, %edx
xorl %ecx, %edx
movzwl (%r15,%rdx,2), %ebx
shll $0x8, %r12d
xorl %ebx, %r12d
movl 0x28(%r14), %edx
movq %r13, %rdi
movl %eax, %esi
movl $0x1, %ecx
callq 0x8822
movzbl 0x28(%r14), %ecx
movl %r12d, %edx
movzbl %dh, %edx
xorl %ecx, %edx
movzwl (%r15,%rdx,2), %r12d
shll $0x8, %ebx
xorl %r12d, %ebx
movl 0x2c(%r14), %edx
movq %r13, %rdi
movl %eax, %esi
movl $0x1, %ecx
callq 0x8822
movzbl 0x2c(%r14), %ecx
movzbl %bh, %edx
xorl %ecx, %edx
movzwl (%r15,%rdx,2), %ebx
shll $0x8, %r12d
xorl %ebx, %r12d
movq %r13, %rdi
movl %eax, %esi
movl %ebp, %edx
movl $0x1, %ecx
callq 0x8822
movzbl %bpl, %ecx
movl %r12d, %edx
movzbl %dh, %edx
xorl %ecx, %edx
shll $0x8, %ebx
xorw (%r15,%rdx,2), %bx
movzwl %bx, %ebp
movl %ebp, %edx
shrl $0x8, %edx
movq %r13, %rdi
movl %eax, %esi
movl $0x1, %ecx
callq 0x8822
movq %r13, %rdi
movl %eax, %esi
movl %ebp, %edx
movl $0x1, %ecx
callq 0x8822
movl 0x8(%r14), %ecx
movl 0xc(%r14), %edx
movq %r13, %rdi
movl %eax, %esi
callq 0x8822
movl 0x10(%r14), %ecx
movl 0x14(%r14), %edx
movq %r13, %rdi
movl %eax, %esi
callq 0x8822
movq %r13, %rdi
movl $0x4489, %edx           # imm = 0x4489
movl $0x3, %ecx
callq 0x8b88
movw $0xffff, %bx            # imm = 0xFFFF
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
movzwl %bx, %edx
shrl $0x8, %edx
xorl $0xa1, %edx
movzwl (%r15,%rdx,2), %r12d
shll $0x8, %ebx
xorl %r12d, %ebx
incl %ecx
jne 0x8a9f
movq (%rsp), %rdi
movl %eax, %esi
movl $0xfb, %edx
movl $0x1, %ecx
callq 0x8822
movzwl %bx, %ecx
shrl $0x8, %ecx
xorl $0xfb, %ecx
shll $0x8, %r12d
xorw (%r15,%rcx,2), %r12w
cmpl $0x0, 0x30(%r14)
jle 0x8b3b
xorl %r13d, %r13d
movq (%rsp), %rbx
movq 0x34(%r14), %rcx
testq %rcx, %rcx
je 0x8b02
movzbl (%rcx,%r13), %ebp
jmp 0x8b06
movl 0x3c(%r14), %ebp
movq %rbx, %rdi
movl %eax, %esi
movl %ebp, %edx
movl $0x1, %ecx
callq 0x8822
movzbl %bpl, %ecx
movzwl %r12w, %edx
shrl $0x8, %edx
xorl %ecx, %edx
shll $0x8, %r12d
xorw (%r15,%rdx,2), %r12w
incq %r13
movslq 0x30(%r14), %rcx
cmpq %rcx, %r13
jl 0x8af2
jmp 0x8b3f
movq (%rsp), %rbx
movzwl %r12w, %ebp
movl %ebp, %edx
shrl $0x8, %edx
movq %rbx, %rdi
movl %eax, %esi
movl $0x1, %ecx
callq 0x8822
movq %rbx, %rdi
movl %eax, %esi
movl %ebp, %edx
movl $0x1, %ecx
callq 0x8822
movl 0x18(%r14), %ecx
movl 0x1c(%r14), %edx
movq %rbx, %rdi
movl %eax, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8822