// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebx
leal -0x1(%rbx), %eax
cmpl 0xdf5d(%rip), %eax      # 0x19570
jb 0xb767
movq 0xdf58(%rip), %rdi      # 0x19578
testq %rdi, %rdi
je 0xb62a
callq 0x3250
andq $0x0, 0xdf46(%rip)      # 0x19578
movq 0xdf47(%rip), %rdi      # 0x19580
testq %rdi, %rdi
je 0xb643
callq 0x3250
andq $0x0, 0xdf35(%rip)      # 0x19580
andl $0x0, 0xdf1e(%rip)      # 0x19570
andl $0x0, 0xdf1b(%rip)      # 0x19574
testl %ebx, %ebx
je 0xb767
movl %ebx, %r12d
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x3040
movq %rax, %r14
movq %rax, 0xdefa(%rip)      # 0x19578
movq %r15, %rdi
callq 0x3040
movq %rax, 0xdef3(%rip)      # 0x19580
xorl %ecx, %ecx
cmpl $0x101, %ebx            # imm = 0x101
setae %cl
leal 0x8(,%rcx,8), %ecx
movl %ecx, 0xdecf(%rip)      # 0x19574
xorl %ecx, %ecx
cmpq %r12, %rcx
je 0xb6df
movl $0x8000, %edx           # imm = 0x8000
xorl %esi, %esi
testl %edx, %edx
je 0xb6d6
leal (,%rsi,4), %edi
testl %ecx, %edx
je 0xb6c9
orl $0x1, %edi
movl %edi, %esi
jmp 0xb6d2
andl $0x1, %esi
leal (%rdi,%rsi,2), %esi
xorl $0x2, %esi
shrl %edx
jmp 0xb6b3
movl %esi, (%r14,%rcx,4)
incq %rcx
jmp 0xb6a7
xorl %ecx, %ecx
cmpl $0x100, %ebx            # imm = 0x100
jbe 0xb739
cmpq %r12, %rcx
je 0xb761
movl $0x4000, %esi           # imm = 0x4000
xorl %edx, %edx
testl %esi, %esi
je 0xb709
xorl %edi, %edi
testl %ecx, %esi
setne %dil
leal (%rdi,%rdx,2), %edx
shrl $0x2, %esi
jmp 0xb6f5
movl %edx, %esi
movl (%r14,%rsi,4), %esi
movzwl %si, %edi
andl $0x7fff, %esi           # imm = 0x7FFF
movl %edx, %r8d
orl $0x80000000, %r8d       # imm = 0x80000000
cmpq %rsi, %rcx
cmovel %edx, %r8d
cmpq %rdi, %rcx
cmovel %edx, %r8d
movl %r8d, (%rax,%rcx,4)
incq %rcx
jmp 0xb6e9
cmpq %r12, %rcx
je 0xb761
movl $0x4000, %esi           # imm = 0x4000
xorl %edx, %edx
testl %esi, %esi
je 0xb759
xorl %edi, %edi
testl %ecx, %esi
setne %dil
leal (%rdi,%rdx,2), %edx
shrl $0x2, %esi
jmp 0xb745
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0xb739
movl %ebx, 0xde09(%rip)      # 0x19570
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nop