// File: ksherlock[P]wdc-utils/zrdz_disassembler.cpp
// Function: zrdz_disassembler::zrdz_disassembler(std::vector<section, std::allocator<section>>&&, std::vector<symbol, std::allocator<symbol>>&&)
zrdz_disassembler::zrdz_disassembler(std::vector<section> &&sections, std::vector<symbol> &&symbols) : 
	disassembler(wdc), _symbols(std::move(symbols))
{

	// do not sort _symbols ... order matters for lookup by entry number.
#if 0
	// sort by section / address.
	std::sort(_symbols.begin(), _symbols.end(), [](const symbol &a, const symbol &b){
		if (a.section < b.section) return true;
		if (a.section == b.section && a.offset < b.offset) return true;
		return false;
	});
#endif

	for (auto &s : sections) {
		if (s.number >= _sections.size()) _sections.resize(s.number + 1);
		_sections[s.number].valid = true;
		_sections[s.number].pc = s.org;
		_sections[s.number] = std::move(s);
	}

	for (auto &s : _symbols) {

		if (s.type == S_UND) continue;
		if (s.flags & SF_VAR) continue;

		if (s.section >= _sections.size()) {
			warnx("invalid section %d for symbol %s", s.section, s.name.c_str());
			continue;
		}
		auto &e = _sections[s.section];
		if (!e.valid) {
			warnx("invalid section %d for symbol %s", s.section, s.name.c_str());
			continue;
		}
		if (s.type == equ_type) continue;
		e.symbols.emplace_back(s);
	}

	// sort labels...
	for (auto &e : _sections) {
		if (e.symbols.empty()) continue;
		std::sort(e.symbols.begin(), e.symbols.end(), [](const symbol &a, const symbol &b) {
			return a.offset > b.offset;
		});
	}

	if (_sections.size() < 5) _sections.resize(5);
	_sections[SECT_PAGE0].name = "page0";
	_sections[SECT_CODE].name = "code";
	_sections[SECT_KDATA].name = "kdata";
	_sections[SECT_DATA].name = "data";
	_sections[SECT_UDATA].name = "udata";

#if 0
	// now place equates at the end...
	for (auto &s : _symbols) {
		if (s.type != equ_type) continue;
		auto &sec = _sections[s.section];
		sec.symbols.emplace_back(s);
	}	
#endif

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
movl $0x1, %esi
callq 0x159f0
movq -0xb0(%rbp), %rdi
leaq 0x13656(%rip), %rax     # 0x24d40
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x38, %rdi
movq %rdi, -0xc0(%rbp)
movq -0x18(%rbp), %rsi
callq 0x15a60
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movq %rdi, -0xb8(%rbp)
callq 0x15a90
movq -0xb0(%rbp), %rax
movl $0xffffffff, 0x68(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9bb0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x99a0
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15ab0
testb $0x1, %al
jne 0x11762
jmp 0x11882
leaq -0x28(%rbp), %rdi
callq 0xa0b0
movq -0xb0(%rbp), %rdi
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
movq %rax, -0xc8(%rbp)
addq $0x50, %rdi
callq 0x15af0
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jb 0x117f2
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x15b20
jmp 0x117bb
jmp 0x117f2
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x15e30
movq -0xc0(%rbp), %rdi
callq 0x7e00
movq -0xb0(%rbp), %rdi
callq 0xcb50
jmp 0x11b8d
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
movl %eax, %esi
callq 0x15bb0
movq -0xb0(%rbp), %rdi
movb $0x1, 0x2d(%rax)
movq -0x38(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0xd4(%rbp)
addq $0x50, %rdi
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
movl %eax, %esi
callq 0x15bb0
movl -0xd4(%rbp), %ecx
movq -0xb0(%rbp), %rdi
movl %ecx, 0x48(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
addq $0x50, %rdi
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
movl %eax, %esi
callq 0x15bb0
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x15bd0
leaq -0x28(%rbp), %rdi
callq 0x15c10
jmp 0x1174c
movq -0xb0(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0xa6a0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0xa490
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x15c30
testb $0x1, %al
jne 0x118c1
jmp 0x119db
leaq -0x58(%rbp), %rdi
callq 0xab10
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x0, %eax
jne 0x118e0
jmp 0x119cd
movq -0x68(%rbp), %rax
movzbl 0x21(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x118f5
jmp 0x119cd
movq -0xb0(%rbp), %rdi
movq -0x68(%rbp), %rax
movzbl 0x22(%rax), %eax
movq %rax, -0xe0(%rbp)
addq $0x50, %rdi
callq 0x15af0
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jb 0x11951
movq -0x68(%rbp), %rdi
movzbl 0x22(%rdi), %eax
movl %eax, -0xe4(%rbp)
callq 0x30f0
movl -0xe4(%rbp), %esi
movq %rax, %rdx
leaq 0xa342(%rip), %rdi      # 0x1bc88
xorl %eax, %eax
callq 0x32a0
jmp 0x1194f
jmp 0x119cd
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movq -0x68(%rbp), %rax
movzbl 0x22(%rax), %eax
movl %eax, %esi
callq 0x15bb0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
testb $0x1, 0x2d(%rax)
jne 0x119a7
movq -0x68(%rbp), %rdi
movzbl 0x22(%rdi), %eax
movl %eax, -0xe8(%rbp)
callq 0x30f0
movl -0xe8(%rbp), %esi
movq %rax, %rdx
leaq 0xa2ec(%rip), %rdi      # 0x1bc88
xorl %eax, %eax
callq 0x32a0
jmp 0x119a5
jmp 0x119cd
movq -0x68(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x91, %eax
jne 0x119b8
jmp 0x119cd
movq -0x70(%rbp), %rdi
addq $0x30, %rdi
movq -0x68(%rbp), %rsi
callq 0x15c70
jmp 0x119cb
jmp 0x119cd
leaq -0x58(%rbp), %rdi
callq 0x15cf0
jmp 0x118ab
movq -0xb0(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x15d10
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x15d40
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x15d70
testb $0x1, %al
jne 0x11a1d
jmp 0x11a98
leaq -0x80(%rbp), %rdi
callq 0x15db0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
addq $0x30, %rdi
callq 0x15dd0
testb $0x1, %al
jne 0x11a43
jmp 0x11a45
jmp 0x11a8a
movq -0x90(%rbp), %rdi
addq $0x30, %rdi
callq 0xa6a0
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
addq $0x30, %rdi
callq 0xa490
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x11ba0
jmp 0x11a88
jmp 0x11a8a
leaq -0x80(%rbp), %rdi
callq 0x15e10
jmp 0x11a07
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
callq 0x15af0
cmpq $0x5, %rax
jae 0x11ac7
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movl $0x5, %esi
callq 0x15b20
jmp 0x11ac5
jmp 0x11ac7
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x15bb0
movq %rax, %rdi
leaq 0xa1c4(%rip), %rsi      # 0x1bca9
callq 0x3350
jmp 0x11aec
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movl $0x1, %esi
callq 0x15bb0
movq %rax, %rdi
leaq 0xa1a4(%rip), %rsi      # 0x1bcaf
callq 0x3350
jmp 0x11b12
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movl $0x2, %esi
callq 0x15bb0
movq %rax, %rdi
leaq 0xa183(%rip), %rsi      # 0x1bcb4
callq 0x3350
jmp 0x11b38
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movl $0x3, %esi
callq 0x15bb0
movq %rax, %rdi
leaq 0xa15e(%rip), %rsi      # 0x1bcb5
callq 0x3350
jmp 0x11b5e
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
movl $0x4, %esi
callq 0x15bb0
movq %rax, %rdi
leaq 0xa13d(%rip), %rsi      # 0x1bcba
callq 0x3350
jmp 0x11b84
addq $0xf0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)