// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %r14
leaq 0x9312(%rip), %r12      # 0x13280
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq $0x100, %r13            # imm = 0x100
je 0x9fa0
movl 0x20(%r14,%r13,4), %edx
testl %edx, %edx
jle 0x9f9b
movq %r12, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x4040
addl 0x20(%r14,%r13,4), %ebp
incl %ebx
incq %r13
jmp 0x9f75
leaq 0x92fb(%rip), %rdi      # 0x132a2
movl %ebx, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x4040
leaq 0x8180(%rip), %rsi      # 0x12139
movq %r15, %rdi
callq 0xb46d
testb %al, %al
jne 0xa380
leaq 0x50(%rsp), %rdi
pushq $0x8
popq %rdx
movq %r15, %rsi
callq 0x4590
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x40d0
movb %al, 0xb(%rsp)
testb %al, %al
je 0xa0f3
movq 0x10fa9(%rip), %rdi     # 0x1afb0
leaq 0x92e5(%rip), %rsi      # 0x132f3
callq 0x4390
movq %rax, %rdi
movq %r15, %rsi
callq 0x4330
leaq 0x92d6(%rip), %rsi      # 0x132fb
movq %rax, %rdi
callq 0x4390
leaq 0x50(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x4460
leaq 0x50(%rsp), %rdi
callq 0x4620
movq %rax, %r13
movq 0x10f60(%rip), %r15     # 0x1afb0
leaq 0x92a7(%rip), %rsi      # 0x132fe
movq %r15, %rdi
callq 0x4390
leaq -0x10(%r13), %r12
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r12, %rsi
callq 0x4270
leaq 0x9283(%rip), %rsi      # 0x1330d
movq %rax, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
leaq 0x50(%rsp), %rdi
pushq $0x10
popq %rsi
xorl %edx, %edx
callq 0x4460
cmpq $0x9fff, %r12           # imm = 0x9FFF
ja 0xa11d
addq $0x3ff0, %r13           # imm = 0x3FF0
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x8f98
movq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movq %r12, %rdx
callq 0x4420
leaq 0x50(%rsp), %rdi
pushq $0x10
popq %rsi
xorl %edx, %edx
callq 0x4460
movl $0x4000, %esi           # imm = 0x4000
addq 0x10(%rsp), %rsi
jmp 0xa12f
movq 0x10eb6(%rip), %rdi     # 0x1afb0
leaq 0x91d8(%rip), %rsi      # 0x132d9
callq 0x4390
movq %rax, %rdi
movq %r15, %rsi
callq 0x4330
movq %rax, %r15
leaq 0x91d1(%rip), %rsi      # 0x132ec
jmp 0xa14d
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8f98
movq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdi
movq %r12, %rdx
callq 0x4420
leaq 0x50(%rsp), %rdi
callq 0x40b0
leaq 0x91d0(%rip), %rsi      # 0x1331d
movq %r15, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movq 0x10e4c(%rip), %rdi     # 0x1afb0
callq 0x4280
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq $0x8000, %r12           # imm = 0x8000
je 0xa226
movq 0x111fe(%rip), %rax     # 0x1b380
movq 0x180(%rax), %rax
movl (%rax,%r12,4), %r13d
testl %r13d, %r13d
je 0xa21e
cmpb $0x0, 0xb(%rsp)
je 0xa208
movq %r14, %rbx
movq 0x10(%r14), %rdi
movl %r12d, %esi
callq 0x96dc
movl %eax, %r15d
movq 0x10(%rbx), %rdi
leal 0x1(%r12), %esi
callq 0x96dc
movl %eax, %r14d
movq 0x10(%rbx), %rdi
leal 0x2(%r12), %esi
callq 0x96dc
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
movl %eax, %ecx
callq 0xe49e
movq 0x30(%rsp), %rcx
leaq 0x9151(%rip), %rdi      # 0x1333d
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x4040
leaq 0x30(%rsp), %rdi
callq 0x41b0
movq %rbx, %r14
jmp 0xa21c
leaq 0x9142(%rip), %rdi      # 0x13351
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x4040
incl %ebp
incq %r12
jmp 0xa16e
leaq 0x916a(%rip), %r13      # 0x13397
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0xa323
imulq $0x18, %rbx, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
cmpq $0x4000, %r12           # imm = 0x4000
je 0xa314
movq 0x11124(%rip), %rax     # 0x1b380
movq 0x28(%rsp), %rcx
movq (%rax,%rcx), %rax
movl (%rax,%r12,4), %r14d
testl %r14d, %r14d
je 0xa30c
cmpb $0x0, 0xb(%rsp)
je 0xa2e3
leaq (%r15,%r12), %rcx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
cmpq %rcx, %rdx
jbe 0xa2f7
movl %ebp, 0xc(%rsp)
movq %r13, %rbp
addq %r15, %rax
movzbl (%r12,%rax), %esi
movzbl 0x1(%r12,%rax), %edx
movzbl 0x2(%r12,%rax), %ecx
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0xe49e
movq 0x30(%rsp), %r8
leaq 0x909f(%rip), %rdi      # 0x13362
movl %ebx, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x4040
movq %r13, %rdi
callq 0x41b0
movq %rbp, %r13
movl 0xc(%rsp), %ebp
jmp 0xa30a
movq %r13, %rdi
movl %ebx, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x4040
jmp 0xa30a
leaq 0x9079(%rip), %rdi      # 0x13377
movl %ebx, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x4040
incl %ebp
incq %r12
jmp 0xa248
incq %rbx
addq $0x4000, %r15           # imm = 0x4000
jmp 0xa232
movq 0x10c86(%rip), %rdi     # 0x1afb0
leaq 0x9078(%rip), %rsi      # 0x133a9
callq 0x4390
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4610
leaq 0x905f(%rip), %rsi      # 0x133bb
movq %rax, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
leaq 0x10(%rsp), %rdi
callq 0x9000
leaq 0x50(%rsp), %rdi
callq 0x40e0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa39a
jmp 0xa39a
jmp 0xa39a
jmp 0xa39a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9000
leaq 0x50(%rsp), %rdi
callq 0x40e0
movq %rbx, %rdi
callq 0x4640
nop