// File: rockytriton[P]dreamsys_emu_gb/src/main.cpp
// Function: main
int main(int argc, char **argv) {
    cout << "Starting main.." << endl;

    std::memset(ram, 0, 0xFFFF);

    dsemu::cart::load((const char *)argv[1]);

    std::memcpy(ram, dsemu::cart::g_romData, 0x8000);

    ui::init();

    std::thread t(dsemu::run);

    int prevFrame = 0;

    while(true) {
        sleepMs(1);
        ui::handleEvents();

        if (prevFrame != ppu::currentFrame) {
            ui::update();
        }

        prevFrame = ppu::currentFrame;
    }

/*
    ram[0] = 0x01;
    ram[1] = 0xC0;
    ram[2] = 0xDE;
    ram[3] = 0x3E;
    ram[4] = 0x55;
    ram[5] = 0x02;
    ram[6] = 0x3E;
    ram[7] = 0x99;
    ram[8] = 0x02;
    ram[9] = 0xFF;
*/
    //cout << endl << "BEFORE RUN, C0DE = " << std::hex << std::setfill('0') << std::setw(2) << (int)ram[0xC0DE] << endl << endl;

    

    //cout << endl << "AFTER RUN, C0DE  = " << std::hex << std::setfill('0') << std::setw(2) << (int)ram[0xC0DE] << endl << endl;


/*

    for (int i=0xC0DE; i<0xCFFF; i++) {
        if ((i % 64) == 0) {
            cout << endl;
            cout << std::hex << std::setfill('0') << std::setw(4) << i << " - ";
        }

        cout << std::hex << std::setfill('0') << std::setw(2) << (int)ram[i] << " ";
    }

    Register reg;
    reg.hi = 0xC0;
    reg.lo = 0xDE;

    ushort *p = (ushort *)&reg;

    regAF.lo = 1;

    cout << "Reg: " << endl 
         << "Hi: " << std::hex << (int)reg.hi << " Lo:" << (int)reg.lo << " = " << (*p) << endl;
*/
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x16553(%rip), %rdi     # 0x2efd0
leaq 0xb6f5(%rip), %rsi      # 0x24179
callq 0x32c0
movq %rax, %rdi
movq 0x1651d(%rip), %rsi     # 0x2efb0
callq 0x3310
leaq 0x26bb1(%rip), %rdi     # 0x3f650
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x3180
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x60(%rbp)
callq 0x34c0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x18bc0
jmp 0x18ad7
leaq -0x30(%rbp), %rdi
callq 0x13a30
jmp 0x18ae2
leaq -0x30(%rbp), %rdi
callq 0x3518
leaq -0x31(%rbp), %rdi
callq 0x3330
leaq 0x1687d(%rip), %rax     # 0x2f378
movq (%rax), %rsi
leaq 0x26b4b(%rip), %rdi     # 0x3f650
movl $0x8000, %edx           # imm = 0x8000
callq 0x3240
callq 0x22f10
leaq -0x50(%rbp), %rdi
leaq -0x277f(%rip), %rsi     # 0x163a0
callq 0x18c70
movl $0x0, -0x54(%rbp)
jmp 0x18b2d
movl $0x1, %edi
callq 0x163e0
jmp 0x18b39
callq 0x23430
jmp 0x18b40
movl -0x54(%rbp), %eax
leaq 0x3bbce(%rip), %rcx     # 0x54718
cmpl (%rcx), %eax
je 0x18b9c
callq 0x23310
jmp 0x18b55
jmp 0x18b9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x18b7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3518
leaq -0x31(%rbp), %rdi
callq 0x3330
jmp 0x18baa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x18d30
jmp 0x18baa
leaq 0x3bb75(%rip), %rax     # 0x54718
movl (%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x18b2b
movq -0x40(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)
nopl (%rax)