// File: mdpkh[P]retro6k/src/main.cpp
// Function: LogRead(unsigned short, unsigned char)
void LogRead(uint16_t address, uint8_t value)
{
	if (suppresslogging && ((bool)(syspflags[address >> 8] & pageflags::PF_IO)
		|| ((syspflags[address >> 8] & pageflags::PF_TMASK) == pageflags::PF_TFLOATING)))
		return;
	if (!partialinst)
		goto logmementry;
	if (debuglogexpectargs == -1)
	{
		const char nargsbyopcode[256] = {
			/*      0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F */
			/* 0 */ 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* 0 */
			/* 1 */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* 1 */
			/* 2 */ 2, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* 2 */
			/* 3 */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* 3 */
			/* 4 */ 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* 4 */
			/* 5 */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* 5 */
			/* 6 */ 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* 6 */
			/* 7 */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* 7 */
			/* 8 */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* 8 */
			/* 9 */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* 9 */
			/* A */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* A */
			/* B */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* B */
			/* C */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* C */
			/* D */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2, /* D */
			/* E */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 2, 2, 2, 2, /* E */
			/* F */ 1, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 2, 2, 2, 2, 2  /* F */
		};
		if (partialinst->entrytype != dletype::LT_PARTIALINST)
			goto logmementry;
		partialinst->instentry.opc = value;
		debuglogexpectargs = nargsbyopcode[value];
		partialinst->instentry.nargs = 0;
		if (!debuglogexpectargs)
		{
			partialinst->entrytype = dletype::LT_INST;
			if (breakpreexecute)
				DoDebugger();
			if (breakafterrts && partialinst->instentry.opc == 0x60)
			{
				breakafterrts = false;
				breakpreexecute = true;
			}
			partialinst = nullptr;
		}
	}
	else if (debuglogexpectargs)
	{
		if (partialinst->entrytype != dletype::LT_PARTIALINST)
			goto logmementry;
		partialinst->instentry.arg[partialinst->instentry.nargs] = value;
		++(partialinst->instentry.nargs);
		--debuglogexpectargs;
		if (!debuglogexpectargs)
		{
			partialinst->entrytype = dletype::LT_INST;
			partialinst = nullptr;
			if (breakpreexecute)
				DoDebugger();
		}
	}
	else
	{
	logmementry:
		if (suppresslogging && (bool)(syspflags[address >> 8] & pageflags::PF_DONTLOG))
			return;
		dlogentry* newentry = ExtendLog(dletype::LT_READ);
		newentry->mementry.address = address;
		newentry->mementry.value = value;
		newentry->mementry.memtype = syspflags[address >> 8];
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movb %sil, %al
movw %di, %cx
movw %cx, -0x2(%rbp)
movb %al, -0x3(%rbp)
testb $0x1, 0x1e092(%rip)     # 0x2d7c1
je 0xf77c
movq 0x1de30(%rip), %rax     # 0x2d568
movzwl -0x2(%rbp), %ecx
sarl $0x8, %ecx
movslq %ecx, %rcx
movl $0x8, %esi
movsbl (%rax,%rcx), %edi
callq 0x7990
cmpb $0x0, %al
jne 0xf777
movq 0x1de0d(%rip), %rax     # 0x2d568
movzwl -0x2(%rbp), %ecx
sarl $0x8, %ecx
movslq %ecx, %rcx
movl $0x3, %esi
movsbl (%rax,%rcx), %edi
callq 0x7990
cmpb $0x0, %al
jne 0xf77c
jmp 0xf949
cmpq $0x0, 0x81e1bc(%rip)    # 0x82d940
jne 0xf78b
jmp 0xf8ce
cmpl $-0x1, 0x81e1a2(%rip)   # 0x82d934
jne 0xf848
leaq -0x110(%rbp), %rdi
leaq 0x10f5a(%rip), %rsi     # 0x20700
movl $0x100, %edx            # imm = 0x100
callq 0x73c0
movq 0x81e189(%rip), %rax    # 0x82d940
cmpb $0x6, (%rax)
je 0xf7c1
jmp 0xf8ce
movb -0x3(%rbp), %cl
movq 0x81e175(%rip), %rax    # 0x82d940
movb %cl, 0x4(%rax)
movzbl -0x3(%rbp), %eax
movsbl -0x110(%rbp,%rax), %eax
movl %eax, 0x81e154(%rip)    # 0x82d934
movq 0x81e159(%rip), %rax    # 0x82d940
movb $0x0, 0x5(%rax)
cmpl $0x0, 0x81e142(%rip)    # 0x82d934
jne 0xf843
movq 0x81e145(%rip), %rax    # 0x82d940
movb $0x7, (%rax)
testb $0x1, 0x81e143(%rip)    # 0x82d948
je 0xf811
movl $0x1, %edi
callq 0x91f0
testb $0x1, 0x81e131(%rip)    # 0x82d949
je 0xf838
movq 0x81e11f(%rip), %rax    # 0x82d940
movzbl 0x4(%rax), %eax
cmpl $0x60, %eax
jne 0xf838
movb $0x0, 0x81e118(%rip)    # 0x82d949
movb $0x1, 0x81e110(%rip)    # 0x82d948
movq $0x0, 0x81e0fd(%rip) # 0x82d940
jmp 0xf949
cmpl $0x0, 0x81e0e5(%rip)    # 0x82d934
je 0xf8cc
movq 0x81e0e8(%rip), %rax    # 0x82d940
cmpb $0x6, (%rax)
je 0xf85f
jmp 0xf8ce
movb -0x3(%rbp), %dl
movq 0x81e0d7(%rip), %rax    # 0x82d940
movq 0x81e0d0(%rip), %rcx    # 0x82d940
movzbl 0x5(%rcx), %ecx
movb %dl, 0x6(%rax,%rcx)
movq 0x81e0c1(%rip), %rax    # 0x82d940
movb 0x5(%rax), %cl
addb $0x1, %cl
movb %cl, 0x5(%rax)
movl 0x81e0a6(%rip), %eax    # 0x82d934
addl $-0x1, %eax
movl %eax, 0x81e09d(%rip)    # 0x82d934
cmpl $0x0, 0x81e096(%rip)    # 0x82d934
jne 0xf8ca
movq 0x81e099(%rip), %rax    # 0x82d940
movb $0x7, (%rax)
movq $0x0, 0x81e08b(%rip) # 0x82d940
testb $0x1, 0x81e08c(%rip)    # 0x82d948
je 0xf8c8
movl $0x1, %edi
callq 0x91f0
jmp 0xf8ca
jmp 0xf947
jmp 0xf8ce
testb $0x1, 0x1deec(%rip)     # 0x2d7c1
je 0xf8fc
movq 0x1dc8a(%rip), %rax     # 0x2d568
movzwl -0x2(%rbp), %ecx
sarl $0x8, %ecx
movslq %ecx, %rcx
movl $0xffffff80, %esi       # imm = 0xFFFFFF80
movsbl (%rax,%rcx), %edi
callq 0x7990
cmpb $0x0, %al
je 0xf8fc
jmp 0xf949
movl $0x2, %edi
callq 0xd3d0
movq %rax, -0x118(%rbp)
movw -0x2(%rbp), %cx
movq -0x118(%rbp), %rax
movw %cx, 0x4(%rax)
movb -0x3(%rbp), %cl
movq -0x118(%rbp), %rax
movb %cl, 0x6(%rax)
movq 0x1dc38(%rip), %rax     # 0x2d568
movzwl -0x2(%rbp), %ecx
sarl $0x8, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq -0x118(%rbp), %rax
movb %cl, 0x7(%rax)
jmp 0xf949
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)