// File: /workspace/github_cpp_makefile_star2_O1/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      # 0x1b570
jb 0xd77c
movq 0xdf58(%rip), %rdi      # 0x1b578
testq %rdi, %rdi
je 0xd62a
callq 0x4280
movq $0x0, 0xdf43(%rip) # 0x1b578
movq 0xdf44(%rip), %rdi      # 0x1b580
testq %rdi, %rdi
je 0xd646
callq 0x4280
xorl %eax, %eax
movq $0x0, 0xdf2d(%rip) # 0x1b580
movl %eax, 0xdf17(%rip)      # 0x1b570
movl %eax, 0xdf15(%rip)      # 0x1b574
testl %ebx, %ebx
je 0xd77c
movl %ebx, %r12d
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x4040
movq %rax, %r14
movq %rax, 0xdef4(%rip)      # 0x1b578
movq %r15, %rdi
callq 0x4040
movq %rax, 0xdeed(%rip)      # 0x1b580
xorl %ecx, %ecx
cmpl $0x101, %ebx            # imm = 0x101
setae %cl
leal 0x8(,%rcx,8), %ecx
movl %ecx, 0xdec9(%rip)      # 0x1b574
xorl %ecx, %ecx
movl $0x8000, %edx           # imm = 0x8000
xorl %esi, %esi
leal (,%rsi,4), %edi
testl %ecx, %edx
je 0xd6c6
orl $0x1, %edi
movl %edi, %esi
jmp 0xd6cf
andl $0x1, %esi
leal (%rdi,%rsi,2), %esi
xorl $0x2, %esi
movl %edx, %edi
shrl %edi
cmpl $0x2, %edx
movl %edi, %edx
jae 0xd6b4
movl %esi, (%r14,%rcx,4)
incq %rcx
cmpq %r12, %rcx
jne 0xd6ad
xorl %ecx, %ecx
cmpl $0x100, %ebx            # imm = 0x100
jbe 0xd748
movl $0x4000, %esi           # imm = 0x4000
xorl %edx, %edx
movl $0x4000, %edi           # imm = 0x4000
xorl %r8d, %r8d
testl %ecx, %esi
setne %r8b
leal (%r8,%rdx,2), %edx
shrl $0x2, %edi
cmpl $0x4, %esi
movl %edi, %esi
jae 0xd6fc
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
cmpq %r12, %rcx
jne 0xd6f0
jmp 0xd776
movl $0x4000, %esi           # imm = 0x4000
xorl %edx, %edx
movl $0x4000, %edi           # imm = 0x4000
xorl %r8d, %r8d
testl %ecx, %esi
setne %r8b
leal (%r8,%rdx,2), %edx
shrl $0x2, %edi
cmpl $0x4, %esi
movl %edi, %esi
jae 0xd754
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %r12, %rcx
jne 0xd748
movl %ebx, 0xddf4(%rip)      # 0x1b570
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq