// File: NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::check_hit(PACKET*)
int CACHE::check_hit(PACKET *packet)
{
    uint32_t set = get_set(packet->address);
    int match_way = -1;

    if (NUM_SET < set) {
        cerr << "[" << NAME << "_ERROR] " << __func__ << " invalid set index: " << set << " NUM_SET: " << NUM_SET;
        cerr << " address: " << hex << packet->address << " full_addr: " << packet->full_addr << dec;
        cerr << " event: " << packet->event_cycle << endl;
        assert(0);
    }

    // hit
    for (uint32_t way=0; way<NUM_WAY; way++) {
        if (block[set][way].valid && (block[set][way].tag == packet->address)) {

            match_way = way;

            DP ( if (warmup_complete[packet->cpu]) {
            cout << "[" << NAME << "] " << __func__ << " instr_id: " << packet->instr_id << " type: " << +packet->type << hex << " addr: " << packet->address;
            cout << " full_addr: " << packet->full_addr << " tag: " << block[set][way].tag << " data: " << block[set][way].data << dec;
            cout << " set: " << set << " way: " << way << " lru: " << block[set][way].lru;
            cout << " event: " << packet->event_cycle << " cycle: " << current_core_cycle[cpu] << endl; });

            break;
        }
    }

    return match_way;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, (%rsp)
movq 0x18(%rsp), %rax
movq 0x340(%rax), %rsi
callq 0x49b0
movl %eax, %ecx
movq (%rsp), %rax
movl %ecx, 0x14(%rsp)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl 0x7370(%rax), %eax
cmpl 0x14(%rsp), %eax
jae 0x62d7
movq 0x22e4f(%rip), %rdi     # 0x28ff0
leaq 0x1ce85(%rip), %rsi     # 0x2302d
callq 0x3220
movq (%rsp), %rsi
movq %rax, %rdi
addq $0x7350, %rsi           # imm = 0x7350
callq 0x31c0
movq %rax, %rdi
leaq 0x1da1b(%rip), %rsi     # 0x23be5
callq 0x3220
movq %rax, %rdi
leaq 0x1cf1d(%rip), %rsi     # 0x230f6
callq 0x3220
movq %rax, %rdi
leaq 0x1cf18(%rip), %rsi     # 0x23100
callq 0x3220
movq %rax, %rdi
movl 0x14(%rsp), %esi
callq 0x32c0
movq %rax, %rdi
leaq 0x1cf12(%rip), %rsi     # 0x23115
callq 0x3220
movq %rax, %rdi
movq (%rsp), %rax
movl 0x7370(%rax), %esi
callq 0x32c0
movq 0x22dcf(%rip), %rdi     # 0x28ff0
leaq 0x1cef8(%rip), %rsi     # 0x23120
callq 0x3220
movq %rax, %rdi
leaq 0x3679(%rip), %rsi      # 0x98b0
callq 0x30d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x340(%rax), %rsi
callq 0x3080
movq %rax, %rdi
leaq 0x1ced1(%rip), %rsi     # 0x2312b
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x348(%rax), %rsi
callq 0x3080
movq %rax, %rdi
leaq 0x3653(%rip), %rsi      # 0x98d0
callq 0x30d0
movq 0x22d67(%rip), %rdi     # 0x28ff0
leaq 0x1cea8(%rip), %rsi     # 0x23138
callq 0x3220
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x378(%rax), %rsi
callq 0x3080
movq %rax, %rdi
movq 0x22d05(%rip), %rsi     # 0x28fb8
callq 0x3250
leaq 0x1cd45(%rip), %rdi     # 0x23004
leaq 0x1cd40(%rip), %rsi     # 0x23006
movl $0x36b, %edx            # imm = 0x36B
leaq 0x1ce6f(%rip), %rcx     # 0x23141
callq 0x3160
movl $0x0, 0xc(%rsp)
movq (%rsp), %rcx
movl 0xc(%rsp), %eax
cmpl 0x7374(%rcx), %eax
jae 0x635b
movq (%rsp), %rax
movq 0x7390(%rax), %rax
movl 0x14(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0xc(%rsp), %ecx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, (%rax)
je 0x634c
movq (%rsp), %rax
movq 0x7390(%rax), %rax
movl 0x14(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0xc(%rsp), %ecx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rax), %rax
movq 0x18(%rsp), %rcx
cmpq 0x340(%rcx), %rax
jne 0x634c
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x635b
jmp 0x634e
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x62df
movl 0x10(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)