// File: /workspace/github_cpp_makefile_star2_O1/khedoros[P]khednes/cpu2.cpp
// Function: cpu::print_details(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void cpu::print_details(const std::string& filename) {
    int total=0;
    int count=0;

    for(int i=0;i<256;++i) {
        if(inst_counts[i]>0) {
            printf("Saw instruction #%02x  %d times.\n",i,inst_counts[i]);
            total+=inst_counts[i];
            count++;
        }
    }
    printf("Total of %d instructions invoked a total of %d times.\n",count,total);

    if(filename == "") return;
    ifstream rom(filename);
    bool print_insts = true;
    size_t filesize = 0;
    Vect<uint8_t> data;
    if(!rom.is_open()) {
        print_insts = false;
        cout<<"wtf couldn't open "<<filename<<" grrrr"<<endl;
    }
    else {
        cout<<"Opened "<<filename<<". ";
        rom.seekg(0,ios::end);
        filesize = rom.tellg() - streampos(16);
        cout<<"Going to read "<<dec<<filesize<<" bytes from it."<<endl;
        rom.seekg(16,ios::beg);
        if(filesize < 40 * 1024) {
            data.resize(filesize+PRG_PAGE_SIZE);
            rom.read(reinterpret_cast<char *>(&data[0]), filesize);
            rom.seekg(16,ios::beg);
            rom.read(reinterpret_cast<char *>(&data[PRG_PAGE_SIZE]), filesize);
        }
        else {
            data.resize(filesize);
            rom.read(reinterpret_cast<char *>(&data[0]), filesize);
        }
        rom.close();
        cout<<"Read the data. Closed the file."<<endl;
    }
    cout.flush();
    int addr_count = 0;
    for(int j=0;j<0x8000;++j) {
        if(addresses[16][j]) {
            if(print_insts) {
                printf("RAM:%04X (%04X)\t%s\n", j, addresses[16][j], util::inst_string(memory->read(j),memory->read(j+1),memory->read(j+2)).c_str());
            }
            else {
                printf("RAM:%04X (%04X)\n", j, addresses[16][j]);
            }
            addr_count++;
        }
    }
    for(int i=0;i<16;++i) {
        for(int j=0;j<PRG_PAGE_SIZE;++j) {
            if(addresses[i][j]) {
                if(print_insts) {
                    int base = i * PRG_PAGE_SIZE + j;
                    if(base < data.size()) {
                        printf("%02X:%04X (%04X)\t%s\n", i, j, addresses[i][j], (util::inst_string(data[base],data[base+1],data[base+2])).c_str());
                    }
                    else {
                        printf("%02X:%04X is outside the file.\n",i,j);
                    }
                }
                else {
                    printf("%02X:%04X (%04X)\n",i,j, addresses[i][j]);
                }
                addr_count++;
            }
        }
    }
    cout<<"The code visited "<<dec<<addr_count<<" addresses during execution."<<endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rsi, %r15
leaq 0x9d3f(%rip), %r12      # 0x152f0
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %rdi, 0x8(%rsp)
movl 0x20(%rdi,%r13,4), %edx
testl %edx, %edx
jle 0xb5df
movq %r12, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x4050
movq 0x8(%rsp), %rdi
addl 0x20(%rdi,%r13,4), %ebx
incl %ebp
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0xb5bd
leaq 0x9d20(%rip), %rdi      # 0x15312
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x4050
leaq 0x8b35(%rip), %rsi      # 0x14139
movq %r15, %rdi
callq 0x4120
testl %eax, %eax
je 0xba9f
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x4570
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x40e0
movb %al, 0x7(%rsp)
testb %al, %al
je 0xb78f
movq 0x10959(%rip), %rdi     # 0x1bfb0
leaq 0x9d05(%rip), %rsi      # 0x15363
movl $0x7, %edx
callq 0x4410
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x1093a(%rip), %rdi     # 0x1bfb0
callq 0x4410
leaq 0x9ce9(%rip), %rsi      # 0x1536b
movl $0x2, %edx
movq %rax, %rdi
callq 0x4410
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x4480
leaq 0x50(%rsp), %rdi
callq 0x45f0
movq %rax, %r12
movq 0x108fc(%rip), %r14     # 0x1bfb0
leaq 0x9cb3(%rip), %rsi      # 0x1536e
movl $0xe, %edx
movq %r14, %rdi
callq 0x4410
leaq -0x10(%r12), %r15
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r14,%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x4280
movq %rax, %r13
leaq 0x9c84(%rip), %rsi      # 0x1537d
movl $0xf, %edx
movq %rax, %rdi
callq 0x4410
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r13, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
leaq 0x50(%rsp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x4480
cmpq $0x9fff, %r15           # imm = 0x9FFF
ja 0xb7e7
addq $0x3ff0, %r12           # imm = 0x3FF0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0xa1ce
movq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movq %r15, %rdx
callq 0x4450
leaq 0x50(%rsp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x4480
movl $0x4000, %esi           # imm = 0x4000
addq 0x10(%rsp), %rsi
jmp 0xb7f9
movq 0x1081a(%rip), %rdi     # 0x1bfb0
leaq 0x9bac(%rip), %rsi      # 0x15349
movl $0x12, %edx
callq 0x4410
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x107fb(%rip), %rdi     # 0x1bfb0
callq 0x4410
movq %rax, %r15
leaq 0x9b98(%rip), %rsi      # 0x1535c
movl $0x6, %edx
movq %rax, %rdi
callq 0x4410
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4390
jmp 0xb83f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0xa1ce
movq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movq %r15, %rdx
callq 0x4450
leaq 0x50(%rsp), %rdi
callq 0x40c0
movq 0x10799(%rip), %r15     # 0x1bfb0
leaq 0x9b6f(%rip), %rsi      # 0x1538d
movl $0x1f, %edx
movq %r15, %rdi
callq 0x4410
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r15, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq 0x10757(%rip), %rdi     # 0x1bfb0
callq 0x4290
leaq 0x30(%rsp), %rbx
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x10b01(%rip), %rax     # 0x1c370
movq 0x180(%rax), %rax
movl (%rax,%r15,4), %r12d
testl %r12d, %r12d
je 0xb91a
cmpb $0x0, 0x7(%rsp)
je 0xb904
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movl %r15d, %esi
callq 0xa996
movl %eax, %r13d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
leal 0x1(%r15), %esi
callq 0xa996
movl %eax, %r14d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
leal 0x2(%r15), %esi
callq 0xa996
movq %rbx, %rdi
movl %r13d, %esi
movl %r14d, %edx
movl %eax, %ecx
callq 0xfffa
movq 0x30(%rsp), %rcx
leaq 0x9acc(%rip), %rdi      # 0x153ad
movl %r15d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x4050
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xb918
callq 0x4380
jmp 0xb918
leaq 0x9ab6(%rip), %rdi      # 0x153c1
movl %r15d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x4050
incl %ebp
incq %r15
cmpq $0x8000, %r15           # imm = 0x8000
jne 0xb868
xorl %ebx, %ebx
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r13
xorl %r15d, %r15d
movq 0x10a33(%rip), %rax     # 0x1c370
movq (%rax,%r13,8), %rax
movl (%rax,%r15,4), %r12d
testl %r12d, %r12d
je 0xb9ea
cmpb $0x0, 0x7(%rsp)
je 0xb9bb
leaq (%rbx,%r15), %rcx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
cmpq %rcx, %rdx
jbe 0xb9d4
addq %rbx, %rax
movzbl (%r15,%rax), %esi
movzbl 0x1(%r15,%rax), %edx
movzbl 0x2(%r15,%rax), %ecx
leaq 0x30(%rsp), %rdi
callq 0xfffa
movq 0x30(%rsp), %r8
leaq 0x9a3d(%rip), %rdi      # 0x153d2
movl %r14d, %esi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4050
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xb9e8
callq 0x4380
jmp 0xb9e8
leaq 0x9a45(%rip), %rdi      # 0x15407
movl %r14d, %esi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4050
jmp 0xb9e8
leaq 0x9a0c(%rip), %rdi      # 0x153e7
movl %r14d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x4050
incl %ebp
incq %r15
cmpq $0x4000, %r15           # imm = 0x4000
jne 0xb936
incq %r14
addq $0x4000, %rbx           # imm = 0x4000
cmpq $0x10, %r14
jne 0xb92f
movq 0x1059b(%rip), %rbx     # 0x1bfb0
leaq 0x99fd(%rip), %rsi      # 0x15419
movl $0x11, %edx
movq %rbx, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rbx,%rax)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x45e0
movq %rax, %rbx
leaq 0x99d9(%rip), %rsi      # 0x1542b
movl $0x1c, %edx
movq %rax, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xba95
callq 0x4380
leaq 0x50(%rsp), %rdi
callq 0x40f0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbab9
jmp 0xbab9
jmp 0xbab9
jmp 0xbab9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xbacb
callq 0x4380
leaq 0x50(%rsp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x4610
nop