// File: Ziang95[P]Tomasulo_simulator/memory.cpp
// Function: memory::load(LSQEntry)
bool memory::load(LSQEntry entry)
{
    ROBEntry *R = CPU_ROB->get_entry(entry.rob_i);
    int found = -1;
    bool no_blocking_SD = true;
    memCell ret;
    for (int i = Srear; is_prev_index(Sfront, i, Sfront, Srear);)
    {
        i = (--i)%Q_LEN;
        if (is_prev_index(Stor_Q[i].rob_i, entry.rob_i, CPU_ROB->get_front(), CPU_ROB->get_rear()))
        {
            if (Stor_Q[i].addr == -1)
            {
                no_blocking_SD = false;
                break;
            }
            else
            {
                if (Stor_Q[i].addr == entry.addr && found == -1)
                {
                    if (Stor_Q[i].ready)
                        found = i;
                    else
                    {
                        no_blocking_SD = false;
                        break;
                    }
                }
            }
        }
    }
    if (no_blocking_SD)
    {
        R->output.mem = sys_clk.get_prog_cyc();
        if (found >= 0)
        {
            msg_log("Forward store found, forwarding, Addr="+to_string(Stor_Q[found].addr), 3);
            ret = Stor_Q[found].val;
        }
        else
        {
            msg_log("Begin LD, ROB = " + to_string(entry.rob_i), 3);
            for (int i = 0; ; i++)
            {
                if (squash_p.flag)
                {
                    squash_p.flag = false;
                    if (is_prev_index(brcUnit.squash_ROB_i(), entry.rob_i, squash_p.R_f, squash_p.R_r))
                    {
                        at_falling_edge(mem_next_vdd);
                        return true;
                    }
                }
                if (i == CPU_cfg->ld_str->mem_time - 1)
                {
                    ret = buf[entry.addr];
                    break;
                }
                at_falling_edge(mem_next_vdd);
                at_rising_edge(mem_next_vdd);
                msg_log("Loading value from Addr="+to_string(entry.addr), 3);
            }
        }
        fCDB.enQ(&ret, entry.rob_i);
        at_falling_edge(mem_next_vdd);
        Lfront = (++Lfront)%Q_LEN;
        msg_log("Value loaded ROB = " + to_string(entry.rob_i), 2);
    }
    else
    {
        msg_log("Previous SD not ready, caller ROB = " + to_string(Load_Q[Lfront].rob_i), 3);
        at_falling_edge(mem_next_vdd);
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
leaq 0x10(%rbp), %rax
movq %rax, -0x190(%rbp)
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x188(%rbp)
leaq 0x14bd4(%rip), %rcx     # 0x2f460
movq (%rcx), %rdi
movl (%rax), %esi
callq 0xe4d0
movq %rax, %rcx
movq -0x188(%rbp), %rax
movq %rcx, -0x18(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movb $0x1, -0x1d(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x188(%rbp), %rax
movl 0x14(%rax), %edi
movl -0x28(%rbp), %esi
movl 0x14(%rax), %edx
movl 0x18(%rax), %ecx
callq 0x1aec0
testb $0x1, %al
jne 0x1a8d6
jmp 0x1a9e8
movq -0x188(%rbp), %rcx
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
movl $0x100, %esi            # imm = 0x100
cltd
idivl %esi
movq -0x190(%rbp), %rax
movl %edx, -0x28(%rbp)
addq $0x1888, %rcx           # imm = 0x1888
movslq -0x28(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl %ecx, -0x19c(%rbp)
movl (%rax), %eax
movl %eax, -0x198(%rbp)
leaq 0x14b3f(%rip), %rax     # 0x2f460
movq (%rax), %rdi
callq 0xe5d0
movl %eax, -0x194(%rbp)
leaq 0x14b2a(%rip), %rax     # 0x2f460
movq (%rax), %rdi
callq 0xe5e0
movl -0x19c(%rbp), %edi
movl -0x198(%rbp), %esi
movl -0x194(%rbp), %edx
movl %eax, %ecx
callq 0x1aec0
testb $0x1, %al
jne 0x1a960
jmp 0x1a9e3
movq -0x188(%rbp), %rax
addq $0x1888, %rax           # imm = 0x1888
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x10(%rax)
jne 0x1a984
movb $0x0, -0x1d(%rbp)
jmp 0x1a9e8
movq -0x190(%rbp), %rcx
movq -0x188(%rbp), %rax
addq $0x1888, %rax           # imm = 0x1888
movslq -0x28(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl 0x10(%rax), %eax
cmpl 0x10(%rcx), %eax
jne 0x1a9df
cmpl $-0x1, -0x1c(%rbp)
jne 0x1a9df
movq -0x188(%rbp), %rax
addq $0x1888, %rax           # imm = 0x1888
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x14(%rax)
je 0x1a9d7
movl -0x28(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x1a9dd
movb $0x0, -0x1d(%rbp)
jmp 0x1a9e8
jmp 0x1a9df
jmp 0x1a9e1
jmp 0x1a9e3
jmp 0x1a8b5
testb $0x1, -0x1d(%rbp)
je 0x1adde
leaq 0x1539f(%rip), %rdi     # 0x2fd98
callq 0x116c0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x58(%rax)
cmpl $0x0, -0x1c(%rbp)
jl 0x1aac6
movq -0x188(%rbp), %rax
movslq -0x1c(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x1898(%rax,%rcx,8), %esi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x8410
movq -0x1a8(%rbp), %rdx
leaq 0x7d11(%rip), %rsi      # 0x22756
leaq -0x48(%rbp), %rdi
callq 0x83c0
jmp 0x1aa50
leaq -0x48(%rbp), %rdi
movl $0x3, %esi
callq 0x12060
jmp 0x1aa60
leaq -0x48(%rbp), %rdi
callq 0x4240
leaq -0x68(%rbp), %rdi
callq 0x4240
movq -0x188(%rbp), %rax
addq $0x1888, %rax           # imm = 0x1888
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x1ace5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1aab8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4240
leaq -0x68(%rbp), %rdi
callq 0x4240
jmp 0x1aea9
movq -0x190(%rbp), %rax
movl (%rax), %esi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x8410
movq -0x1b0(%rbp), %rdx
leaq 0x7c8d(%rip), %rsi      # 0x2277d
leaq -0x98(%rbp), %rdi
callq 0x83c0
jmp 0x1aafe
leaq -0x98(%rbp), %rdi
movl $0x3, %esi
callq 0x12060
jmp 0x1ab11
leaq -0x98(%rbp), %rdi
callq 0x4240
leaq -0xb8(%rbp), %rdi
callq 0x4240
movl $0x0, -0xbc(%rbp)
movq -0x188(%rbp), %rax
testb $0x1, 0x28(%rax)
je 0x1abd3
movq -0x188(%rbp), %rax
movb $0x0, 0x28(%rax)
leaq 0x14932(%rip), %rdi     # 0x2f488
callq 0x105f0
movq -0x190(%rbp), %rcx
movl %eax, %edi
movq -0x188(%rbp), %rax
movl (%rcx), %esi
movl 0x1c(%rax), %edx
movl 0x20(%rax), %ecx
callq 0x1aec0
testb $0x1, %al
jne 0x1ab7e
jmp 0x1abd1
movq -0x188(%rbp), %rdi
addq $0x80, %rdi
callq 0x121a0
movb $0x1, -0x1(%rbp)
jmp 0x1ae9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1abc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4240
leaq -0xb8(%rbp), %rdi
callq 0x4240
jmp 0x1aea9
jmp 0x1abd3
movl -0xbc(%rbp), %eax
leaq 0x14870(%rip), %rcx     # 0x2f450
movq (%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1ac11
movq -0x190(%rbp), %rcx
movq -0x188(%rbp), %rax
movq (%rax), %rax
movslq 0x10(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
jmp 0x1ace3
movq -0x188(%rbp), %rdi
subq $-0x80, %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x121a0
movq -0x1c0(%rbp), %rdi
callq 0x12130
movq -0x190(%rbp), %rax
movl 0x10(%rax), %esi
leaq -0x100(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x8410
movq -0x1b8(%rbp), %rdx
leaq 0x7b2f(%rip), %rsi      # 0x2278e
leaq -0xe0(%rbp), %rdi
callq 0x83c0
jmp 0x1ac6d
leaq -0xe0(%rbp), %rdi
movl $0x3, %esi
callq 0x12060
jmp 0x1ac80
leaq -0xe0(%rbp), %rdi
callq 0x4240
leaq -0x100(%rbp), %rdi
callq 0x4240
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x1ab33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1acd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x4240
leaq -0x100(%rbp), %rdi
callq 0x4240
jmp 0x1aea9
jmp 0x1ace5
movq -0x190(%rbp), %rax
movl (%rax), %edx
leaq 0x1481b(%rip), %rdi     # 0x2f510
leaq -0x24(%rbp), %rsi
callq 0x4de0
movq -0x188(%rbp), %rdi
subq $-0x80, %rdi
callq 0x121a0
movq -0x188(%rbp), %rcx
movq -0x190(%rbp), %rax
movl 0xc(%rcx), %edi
movl %edi, %edx
incl %edx
movl %edx, 0xc(%rcx)
movl %edi, %esi
addl $0x100, %esi            # imm = 0x100
testl %edx, %edx
cmovsl %esi, %edx
andl $0xffffff00, %edx       # imm = 0xFFFFFF00
negl %edx
movl %edx, %esi
movl %edi, %edx
leal 0x1(%rdx,%rsi), %edx
movl %edx, 0xc(%rcx)
movl (%rax), %esi
leaq -0x140(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x8410
movq -0x1c8(%rbp), %rdx
leaq 0x7a3e(%rip), %rsi      # 0x227a7
leaq -0x120(%rbp), %rdi
callq 0x83c0
jmp 0x1ad77
leaq -0x120(%rbp), %rdi
movl $0x2, %esi
callq 0x12060
jmp 0x1ad8a
leaq -0x120(%rbp), %rdi
callq 0x4240
leaq -0x140(%rbp), %rdi
callq 0x4240
jmp 0x1ae97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1adcd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4240
leaq -0x140(%rbp), %rdi
callq 0x4240
jmp 0x1aea9
movq -0x188(%rbp), %rax
movslq 0xc(%rax), %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x88(%rax,%rcx,8), %esi
leaq -0x180(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x8410
movq -0x1d0(%rbp), %rdx
leaq 0x79a6(%rip), %rsi      # 0x227bb
leaq -0x160(%rbp), %rdi
callq 0x83c0
jmp 0x1ae23
leaq -0x160(%rbp), %rdi
movl $0x3, %esi
callq 0x12060
jmp 0x1ae36
leaq -0x160(%rbp), %rdi
callq 0x4240
leaq -0x180(%rbp), %rdi
callq 0x4240
movq -0x188(%rbp), %rdi
addq $0x80, %rdi
callq 0x121a0
jmp 0x1ae97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1ae89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x4240
leaq -0x180(%rbp), %rdi
callq 0x4240
jmp 0x1aea9
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)