// File: /workspace/github_cpp_makefile_star2_O1/saumya-prakash[P]AssemblerEmulator/Emulator.cpp
// Function: Emulator::loader()
bool Emulator::loader()     // no size checking has been done now -> just simple loading
{    
    ifstream fi(obj_filename, ifstream::binary | ifstream::in);

    if(!fi) // ERROR in opening file
    {
        return false;
    }

    char waste;

    char aux[8] = "\0";
    for(unsigned i=0; i<8; ++i)
    {
        fi.read(aux+i, 1);
    }

    for(unsigned i=0; i<8; ++i)
    {
        if(aux[i]!=format_code[i])
        {
            cout<<i<<" ";
            cout<<"Unknown object file format"<<endl;
            return false;
        }
    }

    fi.read(&waste, 1);
    if(waste!='\n')
    {
        cout<<"Unknown onject file format"<<endl;
        return false; 
    }


    text_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<text_size; ++i)     // load text segment
    {
        mempory_space[i] = get_int(fi);
    }

    fi.read(&waste, 1);     // dump newline character
    fi.read(&waste, 1);     // dump newline character
    
    data_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<data_size; ++i)
    {
        mempory_space[i+data_lower] = get_int(fi);
    }


    PC = 0x00000000;     // set PC to 0x0
    A = 0x00000000;
    B = 0x00000000;
    data_upper = data_lower + data_size - 1;    // denotes upper boundary of data loaded
    SP = 0xff00;    // set SP to ?? NOT CONFIRMED


    fi.close();     // close the object file

    return true;    // loading successful
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rdi, %rbx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0xc, %edx
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x38(%rsp,%rax)
jne 0x46b2
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x18(%rsp), %r14
leaq (%rsp,%r15), %rsi
addq $0x10, %rsi
movl $0x1, %edx
movq %r14, %rdi
callq 0x21d0
incq %r15
cmpq $0x8, %r15
jne 0x44b7
xorl %esi, %esi
leaq 0x2e1e(%rip), %rax      # 0x72fc
movb 0x10(%rsp,%rsi), %cl
cmpb (%rsi,%rax), %cl
jne 0x4604
incq %rsi
cmpq $0x8, %rsi
jne 0x44de
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x21d0
cmpb $0xa, 0xf(%rsp)
jne 0x466c
leaq 0x18(%rsp), %rsi
callq 0x46ea
movl %eax, 0x48(%rbx)
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x21d0
cmpl $0x0, 0x48(%rbx)
je 0x455d
xorl %r15d, %r15d
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x46ea
movq 0x30(%rbx), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
movl 0x48(%rbx), %eax
cmpq %rax, %r15
jb 0x4542
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x21d0
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x21d0
leaq 0x18(%rsp), %rsi
callq 0x46ea
movl %eax, 0x4c(%rbx)
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq 0x21d0
movl 0x4c(%rbx), %eax
testl %eax, %eax
je 0x45e0
movl $0x400, %r15d           # imm = 0x400
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x46ea
movq 0x30(%rbx), %rcx
movl %r15d, %edx
movl %eax, (%rcx,%rdx,4)
movl 0x4c(%rbx), %eax
leaq 0x1(%r15), %rcx
addq $-0x3ff, %r15           # imm = 0xFC01
cmpq %rax, %r15
movq %rcx, %r15
jb 0x45b8
addl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x38(%rbx)
movaps 0x2cb1(%rip), %xmm0     # 0x72a0
movups %xmm0, 0x20(%rbx)
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x2060
jmp 0x46b4
movq 0x59c5(%rip), %rdi      # 0x9fd0
callq 0x2110
leaq 0x2ee4(%rip), %rsi      # 0x74fb
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
movq 0x59a5(%rip), %rbx      # 0x9fd0
leaq 0x2e02(%rip), %rsi      # 0x7434
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x5973(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
jmp 0x46b2
movq 0x595d(%rip), %rbx      # 0x9fd0
leaq 0x2dd5(%rip), %rsi      # 0x744f
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x592b(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x2070
movl %ebx, %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
jmp 0x46d5
jmp 0x46d5
jmp 0x46d5
jmp 0x46d5
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2270