// File: OpenFodder[P]SOS_Unpacker/capsimg/Codec/DiskEncoding.cpp
// Function: CDiskEncoding::InitMFM(unsigned int)
void CDiskEncoding::InitMFM(uint32_t mfmsize)
{
	// cancel if correct tables are available
	if (mfmsize && mfminit>=mfmsize)
		return;

	// clear tables
	delete [] mfmcode;
	mfmcode=NULL;
	delete [] mfmdecode;
	mfmdecode=NULL;

	mfminit=0;
	mfmcodebit=0;

	// stop if empty tables
	if (!mfmsize)
		return;

	// create tables
	mfmcode=new uint32_t[mfmsize];
	mfmdecode=new uint32_t[mfmsize];

	// code only ever uses 8 or 16 bit indexing
	mfmcodebit=(mfmsize > 0x100) ? 16 : 8;

	uint32_t sval;

	// create MFM word code table (last code bit assumed 0)
	for (sval=0x0000; sval < mfmsize; sval++) {
		uint32_t code=0;

		// all bits
		for (uint32_t bit=0x8000; bit; bit>>=1) {
			code<<=2;
			// bit 0 encoded as 00, if last bit is 1
			// bit 0 encoded as 10, if last bit is 0
			// bit 1 encoded as 01
			if (sval & bit)
				code|=1;
			else
				if (!(code & 4))
					code|=2;
		}

		// store encoding
		mfmcode[sval]=code;
	}

	// create MFM decode table, possible errors marked for analyser only
	if (mfmsize > 0x100) {
		for (sval=0x0000; sval < mfmsize; sval++) {
			uint32_t code=0;

			// all bits
			for (uint32_t bit=0x4000; bit; bit>>=2) {
				code<<=1;
				if (sval & bit)
					code|=1;
			}

			// store decoding, mark coding erros
			uint32_t cd=mfmcode[code]&0xffff;
			if (cd!=sval && (cd&0x7fff)!=sval)
				code|=DF_31;
			mfmdecode[sval]=code;
		}
	} else {
		for (sval=0x0000; sval < mfmsize; sval++) {
			uint32_t code=0;

			// all bits
			for (uint32_t bit=0x4000; bit; bit>>=2) {
				code<<=1;
				if (sval & bit)
					code|=1;
			}

			// store decoding
			mfmdecode[sval]=code;
		}
	}

	mfminit=mfmsize;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x4(%rbp)
cmpl $0x0, -0x4(%rbp)
je 0x16091
movl 0x13529(%rip), %eax     # 0x295b0
cmpl -0x4(%rbp), %eax
jb 0x16091
jmp 0x16317
movq 0x13520(%rip), %rax     # 0x295b8
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x160ab
movq -0x30(%rbp), %rdi
callq 0x42e0
movq $0x0, 0x13502(%rip) # 0x295b8
movq 0x13503(%rip), %rax     # 0x295c0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x160d0
movq -0x38(%rbp), %rdi
callq 0x42e0
movq $0x0, 0x134e5(%rip) # 0x295c0
movl $0x0, 0x134cb(%rip)     # 0x295b0
movl $0x0, 0x134c5(%rip)     # 0x295b4
cmpl $0x0, -0x4(%rbp)
jne 0x160fa
jmp 0x16317
movl -0x4(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4040
movq %rax, 0x13493(%rip)     # 0x295b8
movl -0x4(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4040
movq %rax, 0x13470(%rip)     # 0x295c0
movl -0x4(%rbp), %edx
movl $0x8, %eax
movl $0x10, %ecx
cmpl $0x100, %edx            # imm = 0x100
cmoval %ecx, %eax
movl %eax, 0x13448(%rip)     # 0x295b4
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x161eb
movl $0x0, -0xc(%rbp)
movl $0x8000, -0x10(%rbp)    # imm = 0x8000
cmpl $0x0, -0x10(%rbp)
je 0x161d0
movl -0xc(%rbp), %eax
shll $0x2, %eax
movl %eax, -0xc(%rbp)
movl -0x8(%rbp), %eax
andl -0x10(%rbp), %eax
cmpl $0x0, %eax
je 0x161ae
movl -0xc(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x161c4
movl -0xc(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x161c2
movl -0xc(%rbp), %eax
orl $0x2, %eax
movl %eax, -0xc(%rbp)
jmp 0x161c4
jmp 0x161c6
movl -0x10(%rbp), %eax
shrl %eax
movl %eax, -0x10(%rbp)
jmp 0x16189
movl -0xc(%rbp), %edx
movq 0x133de(%rip), %rax     # 0x295b8
movl -0x8(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x16173
cmpl $0x100, -0x4(%rbp)      # imm = 0x100
jbe 0x162a5
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x162a3
movl $0x0, -0x14(%rbp)
movl $0x4000, -0x18(%rbp)    # imm = 0x4000
cmpl $0x0, -0x18(%rbp)
je 0x16248
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
andl -0x18(%rbp), %eax
cmpl $0x0, %eax
je 0x1623b
movl -0x14(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1623d
movl -0x18(%rbp), %eax
shrl $0x2, %eax
movl %eax, -0x18(%rbp)
jmp 0x16219
movq 0x13369(%rip), %rax     # 0x295b8
movl -0x14(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8(%rbp), %eax
je 0x16285
movl -0x1c(%rbp), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
cmpl -0x8(%rbp), %eax
je 0x16285
movl -0x14(%rbp), %eax
movabsq $0x80000000, %rcx       # imm = 0x80000000
orq %rcx, %rax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edx
movq 0x13331(%rip), %rax     # 0x295c0
movl -0x8(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x161ff
jmp 0x1630e
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x1630c
movl $0x0, -0x20(%rbp)
movl $0x4000, -0x24(%rbp)    # imm = 0x4000
cmpl $0x0, -0x24(%rbp)
je 0x162f1
movl -0x20(%rbp), %eax
shll %eax
movl %eax, -0x20(%rbp)
movl -0x8(%rbp), %eax
andl -0x24(%rbp), %eax
cmpl $0x0, %eax
je 0x162e4
movl -0x20(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x162e6
movl -0x24(%rbp), %eax
shrl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x162c2
movl -0x20(%rbp), %edx
movq 0x132c5(%rip), %rax     # 0x295c0
movl -0x8(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x162ac
jmp 0x1630e
movl -0x4(%rbp), %eax
movl %eax, 0x13299(%rip)     # 0x295b0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)