// File: /workspace/github_cpp_makefile_star2_O1/gibsjose[P]VirtualMemorySimulation/Simulation.cpp
// Function: Simulation::Run()
void Simulation::Run(void) {
    //Read in the input file
    std::ifstream file(filepath);
    std::string line;
    std::vector<std::string> values;
    std::string name;
    std::string pageString;

    unsigned long lineNumber = 0;
    uint16_t frame;

    if(!file.is_open()) {
        std::cerr << "File \"" << filepath << "\" could not be opened. Check to see if it exists" << std::endl;
        exit(-1);
    }

    //Iterate over each line of the file
    while(std::getline(file, line)) {

        //Skip blank lines
        if(line.empty()) {
            continue;
        }

        values = StringUtilities::SplitString(line, ":\t");

        if(values.size() < 2) {
            std::cerr << "Error: Line " << lineNumber << ": \"" << line << "\" does not follow format of \"{NAME}:<TAB>{Page #}\"" << std::endl;
            exit(-1);
        }

        name = values.at(0);
        pageString = values.at(1);

        //New process: Add it to the map
        if(!processes.count(name)) {
            Process process(name);
            processes[name] = process;

            std::cout << std::endl << Color::Green("* [NEW PROCESS] ") << name << std::endl;
        }

        //Get the process object
        Process & process = processes.find(name)->second;
        uint16_t page = process.PageAccess(pageString);

        //Reference the process page in memory
        // This could result in one of three things:
        // 1. There is already a frame associated with the page number, and it is already in memory
        // 2. There is already a frame associated with the page number, and it is on the free frame list
        // 3. There is no frame associated with this page number --> Page fault: Assign it a page number and bring frame into
        //     memory
        memory.Reference(process, page);

        //Increment line number
        lineNumber++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq %rdi, %rbx
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x22b0
leaq 0x78(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x50(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xd8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x218(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x3138
leaq 0x20(%rbx), %r12
leaq 0x28(%rbx), %rax
movq %rax, 0xe8(%rsp)
addq $0x50, %rbx
movq $0x0, 0x60(%rsp)
leaq 0x68(%rsp), %r13
movq 0x1a0(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2310
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x30b4
movq 0x70(%rsp), %rdx
testq %rdx, %rdx
je 0x2ceb
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x68(%rsp), %rsi
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x4d20
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x108(%rsp), %rbp
movq %rbp, %rdi
leaq 0x535d(%rip), %rsi      # 0x80d2
leaq 0x5358(%rip), %rdx      # 0x80d4
callq 0x43a8
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
leaq 0x88(%rsp), %rsi
movq %rbp, %rdx
callq 0x352a
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps (%rsp), %xmm0
movq 0x10(%rsp), %rax
movups 0xf0(%rsp), %xmm1
movaps %xmm1, (%rsp)
movq 0x100(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movaps 0x20(%rsp), %xmm1
movaps %xmm0, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movups %xmm1, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x3608
movq %r15, %rdi
callq 0x3608
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2e20
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2e45
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21d0
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
cmpq $0x20, %rax
jbe 0x318c
leaq 0x40(%rsp), %rdi
callq 0x20f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
subq %rsi, %rdx
sarq $0x5, %rdx
cmpq $0x1, %rdx
jbe 0x31ff
addq $0x20, %rsi
leaq 0xc8(%rsp), %rdi
callq 0x20f0
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
callq 0x51da
cmpq 0xe8(%rsp), %rax
jne 0x3078
leaq 0x108(%rsp), %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0x6732
movq %r12, %rdi
movq %r15, %rsi
callq 0x3664
movq %rax, %rbp
movq %rax, %rdi
leaq 0x108(%rsp), %rsi
callq 0x20f0
leaq 0x118(%rsp), %rcx
movzwl 0x14(%rcx), %eax
movw %ax, 0x24(%rbp)
movl 0x10(%rcx), %eax
movl %eax, 0x20(%rbp)
leaq 0x108(%rsp), %rax
cmpq %rbp, %rax
je 0x2f12
leaq 0x28(%rbp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x461c
leaq 0x118(%rsp), %rcx
movb 0x54(%rcx), %al
movb %al, 0x64(%rbp)
movl 0x50(%rcx), %eax
movl %eax, 0x60(%rbp)
addq $0x68, %rbp
movq %rbp, %rdi
leaq 0x170(%rsp), %rsi
callq 0x4988
movq 0x9087(%rip), %rcx      # 0xbfc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x9069(%rip), %rdi      # 0xbfc8
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq %rax, %rbp
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
leaq 0x5184(%rip), %rsi      # 0x8115
leaq 0x518d(%rip), %rdx      # 0x8125
callq 0x43a8
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x36e0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rbp, %rdi
callq 0x2200
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rax, %rdi
callq 0x2200
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3014
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x3039
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x170(%rsp), %rdi
callq 0x3d68
leaq 0x130(%rsp), %rdi
callq 0x4cfa
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x3078
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
callq 0x560c
movq %rax, %rbp
addq $0x40, %rbp
movq %rbp, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x67de
movzwl %ax, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5654
incq 0x60(%rsp)
jmp 0x2ceb
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x30d9
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x30f5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rsp, %rdi
callq 0x3608
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x3119
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x1a0(%rsp), %rdi
callq 0x2080
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8eb1(%rip), %rdi      # 0xbff0
leaq 0x4f54(%rip), %rsi      # 0x809a
movl $0x6, %edx
callq 0x2200
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8e92(%rip), %rdi      # 0xbff0
callq 0x2200
movq %rax, %rbx
leaq 0x4f34(%rip), %rsi      # 0x80a1
movl $0x30, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20d0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2230
movq 0x8e5d(%rip), %rdi      # 0xbff0
leaq 0x4f3b(%rip), %rsi      # 0x80d5
movl $0xc, %edx
callq 0x2200
movq 0x8e45(%rip), %rdi      # 0xbff0
movq 0x60(%rsp), %rsi
callq 0x2130
movq %rax, %rbx
leaq 0x5140(%rip), %rsi      # 0x82ff
movl $0x3, %edx
movq %rax, %rdi
callq 0x2200
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq %rbx, %rdi
callq 0x2200
movq %rax, %rbx
leaq 0x4efa(%rip), %rsi      # 0x80e2
movl $0x32, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20d0
jmp 0x3182
leaq 0x4f72(%rip), %rdi      # 0x8178
movl $0x1, %esi
xorl %eax, %eax
callq 0x2280
jmp 0x32da
movq %rax, %rbx
jmp 0x329b
jmp 0x32c4
movq %rax, %rbx
jmp 0x3253
jmp 0x32da
movq %rax, %rbx
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x3253
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x32dd
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x32dd
jmp 0x32da
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x329b
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x32c7
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x32c7
jmp 0x32da
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x37c8
jmp 0x32dd
jmp 0x32da
jmp 0x32da
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x3302
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x331e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rsp, %rdi
callq 0x3608
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x3342
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x1a0(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x22f0
nop