// File: /workspace/github_cpp_makefile_star2_O3/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 0xde99(%rip), %eax      # 0x1b570
jb 0xd840
movq 0xde94(%rip), %rdi      # 0x1b578
testq %rdi, %rdi
je 0xd6ee
callq 0x4280
movq $0x0, 0xde7f(%rip) # 0x1b578
movq 0xde80(%rip), %rdi      # 0x1b580
testq %rdi, %rdi
je 0xd70a
callq 0x4280
xorl %eax, %eax
movq $0x0, 0xde69(%rip) # 0x1b580
movl %eax, 0xde53(%rip)      # 0x1b570
movl %eax, 0xde51(%rip)      # 0x1b574
testl %ebx, %ebx
je 0xd840
movl %ebx, %r12d
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x4040
movq %rax, %r14
movq %rax, 0xde30(%rip)      # 0x1b578
movq %r15, %rdi
callq 0x4040
movq %rax, 0xde29(%rip)      # 0x1b580
xorl %ecx, %ecx
cmpl $0x101, %ebx            # imm = 0x101
setae %cl
leal 0x8(,%rcx,8), %ecx
movl %ecx, 0xde05(%rip)      # 0x1b574
xorl %ecx, %ecx
movl $0x8000, %edx           # imm = 0x8000
xorl %esi, %esi
leal (,%rsi,4), %edi
testl %ecx, %edx
je 0xd78a
orl $0x1, %edi
movl %edi, %esi
jmp 0xd793
andl $0x1, %esi
leal (%rdi,%rsi,2), %esi
xorl $0x2, %esi
movl %edx, %edi
shrl %edi
cmpl $0x2, %edx
movl %edi, %edx
jae 0xd778
movl %esi, (%r14,%rcx,4)
incq %rcx
cmpq %r12, %rcx
jne 0xd771
xorl %ecx, %ecx
cmpl $0x100, %ebx            # imm = 0x100
jbe 0xd80c
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 0xd7c0
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 0xd7b4
jmp 0xd83a
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 0xd818
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %r12, %rcx
jne 0xd80c
movl %ebx, 0xdd30(%rip)      # 0x1b570
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq