// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movl %esi, %ebp
movq %rdi, %r14
movl 0x30(%rdx), %edi
callq 0x723f
movl %eax, %r12d
movl (%rbx), %ecx
movl 0x4(%rbx), %edx
movq %r14, %rdi
movl %ebp, %esi
callq 0x725b
pushq $0x3
popq %rbp
movq %r14, %rdi
movl $0x4489, %edx           # imm = 0x4489
movl %ebp, %ecx
callq 0x754d
movl %eax, %r13d
movl $0xffff, %edi           # imm = 0xFFFF
movl $0xa1, %esi
movl %ebp, %edx
callq 0x7596
movl %eax, %r15d
pushq $0x1
popq %rbp
movq %r14, %rdi
movl %r13d, %esi
movl $0xfe, %edx
movl %ebp, %ecx
callq 0x725b
movl %eax, %r13d
movzwl %r15w, %edi
movl $0xfe, %esi
movl %ebp, %edx
callq 0x7596
movl %eax, %r15d
movl 0x24(%rbx), %edx
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %ecx
callq 0x725b
movl %eax, %r13d
movl 0x24(%rbx), %esi
movzwl %r15w, %edi
movl %ebp, %edx
callq 0x7596
movl %eax, %r15d
movl 0x28(%rbx), %edx
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %ecx
callq 0x725b
movl %eax, %r13d
movl 0x28(%rbx), %esi
movzwl %r15w, %edi
movl %ebp, %edx
callq 0x7596
movl %eax, %r15d
movl 0x2c(%rbx), %edx
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %ecx
callq 0x725b
movl %eax, %r13d
movl 0x2c(%rbx), %esi
movzwl %r15w, %edi
movl %ebp, %edx
callq 0x7596
movl %eax, %r15d
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x725b
movl %eax, %r13d
movzwl %r15w, %edi
movl %r12d, %esi
movl %ebp, %edx
callq 0x7596
movzwl %ax, %r15d
movl %r15d, %edx
shrl $0x8, %edx
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %ecx
callq 0x725b
movq %r14, %rdi
movl %eax, %esi
movl %r15d, %edx
movl %ebp, %ecx
callq 0x725b
movl 0x8(%rbx), %ecx
movl 0xc(%rbx), %edx
movq %r14, %rdi
movl %eax, %esi
callq 0x725b
movl 0x10(%rbx), %ecx
movl 0x14(%rbx), %edx
movq %r14, %rdi
movl %eax, %esi
callq 0x725b
movq %r14, %rdi
movl $0x4489, %edx           # imm = 0x4489
pushq $0x3
popq %r12
movl %r12d, %ecx
callq 0x754d
movl %eax, %r15d
movl $0xffff, %edi           # imm = 0xFFFF
movl $0xa1, %esi
movl %r12d, %edx
callq 0x7596
movl %eax, %r12d
movq %r14, (%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl $0xfb, %edx
movl %ebp, %ecx
callq 0x725b
movl %eax, %r15d
movzwl %r12w, %edi
movl $0xfb, %esi
movl %ebp, %edx
callq 0x7596
xorl %r14d, %r14d
movq %rbx, %rbp
movslq 0x30(%rbx), %rcx
movzwl %ax, %r12d
cmpq %rcx, %r14
jge 0x7503
movq 0x34(%rbx), %rax
testq %rax, %rax
je 0x74d3
movzbl (%rax,%r14), %r13d
jmp 0x74d7
movl 0x3c(%rbx), %r13d
movq (%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
pushq $0x1
popq %rbx
movl %ebx, %ecx
callq 0x725b
movl %eax, %r15d
movl %r12d, %edi
movl %r13d, %esi
movl %ebx, %edx
callq 0x7596
incq %r14
movq %rbp, %rbx
jmp 0x74b6
movl %r12d, %edx
shrl $0x8, %edx
pushq $0x1
popq %r14
movq (%rsp), %rbp
movq %rbp, %rdi
movl %r15d, %esi
movl %r14d, %ecx
callq 0x725b
movq %rbp, %rdi
movl %eax, %esi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x725b
movl 0x18(%rbx), %ecx
movl 0x1c(%rbx), %edx
movq %rbp, %rdi
movl %eax, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x725b