// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::add_pq(PACKET*)
int CACHE::add_pq(PACKET *packet)
{
    // check for the latest wirtebacks in the write queue
    int wq_index = WQ.check_queue(packet);
    if (wq_index != -1) {
        
        // check fill level
        if (packet->fill_level < fill_level) {

            packet->data = WQ.entry[wq_index].data;
            if (packet->instruction) 
                upper_level_icache[packet->cpu]->return_data(packet);
            else // data
                upper_level_dcache[packet->cpu]->return_data(packet);
        }

        HIT[packet->type]++;
        ACCESS[packet->type]++;

        WQ.FORWARD++;
        PQ.ACCESS++;

        return -1;
    }

    // check for duplicates in the PQ
    int index = PQ.check_queue(packet);
    if (index != -1) {
        if (packet->fill_level < PQ.entry[index].fill_level)
            PQ.entry[index].fill_level = packet->fill_level;

        PQ.MERGED++;
        PQ.ACCESS++;

        return index; // merged index
    }

    // check occupancy
    if (PQ.occupancy == PQ_SIZE) {
        PQ.FULL++;

        DP ( if (warmup_complete[packet->cpu]) {
        cout << "[" << NAME << "] cannot process add_pq since it is full" << endl; });
        return -2; // cannot handle this request
    }

    // if there is no duplicate, add it to PQ
    index = PQ.tail;

#ifdef SANITY_CHECK
    if (PQ.entry[index].address != 0) {
        cerr << "[" << NAME << "_ERROR] " << __func__ << " is not empty index: " << index;
        cerr << " address: " << hex << PQ.entry[index].address;
        cerr << " full_addr: " << PQ.entry[index].full_addr << dec << endl;
        assert(0);
    }
#endif

    memcpy(&PQ.entry[index], packet, sizeof(PACKET));

    // ADD LATENCY
    if (PQ.entry[index].event_cycle < current_core_cycle[packet->cpu])
        PQ.entry[index].event_cycle = current_core_cycle[packet->cpu] + LATENCY;
    else
        PQ.entry[index].event_cycle += LATENCY;

    PQ.occupancy++;
    PQ.tail++;
    if (PQ.tail >= PQ.SIZE)
        PQ.tail = 0;

    DP ( if (warmup_complete[PQ.entry[index].cpu]) {
    cout << "[" << NAME << "_PQ] " <<  __func__ << " instr_id: " << PQ.entry[index].instr_id << " address: " << hex << PQ.entry[index].address;
    cout << " full_addr: " << PQ.entry[index].full_addr << dec;
    cout << " type: " << +PQ.entry[index].type << " head: " << PQ.head << " tail: " << PQ.tail << " occupancy: " << PQ.occupancy;
    cout << " event: " << PQ.entry[index].event_cycle << " current: " << current_core_cycle[PQ.entry[index].cpu] << endl; });

    if (packet->address == 0)
        assert(0);

    PQ.TO_CACHE++;
    PQ.ACCESS++;

    return -1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
addq $0x73a8, %rdi           # imm = 0x73A8
callq 0xc248
cmpl $-0x1, %eax
je 0x639d
movl 0x8(%r14), %ecx
cmpl 0x7398(%rbx), %ecx
jge 0x6362
movq 0x7440(%rbx), %rcx
cltq
imulq $0x380, %rax, %rax      # imm = 0x380
movq 0x360(%rcx,%rax), %rax
movq %rax, 0x360(%r14)
xorl %eax, %eax
cmpb $0x0, (%r14)
sete %al
movl 0x330(%r14), %ecx
leaq (%rbx,%rax,8), %rax
movq 0x8(%rax,%rcx,8), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x18(%rax)
movzbl 0x32f(%r14), %eax
incq 0x72c8(%rbx,%rax,8)
incq 0x72a8(%rbx,%rax,8)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7410, %ecx           # imm = 0x7410
incq (%rbx,%rcx)
movl $0xad48, %ecx           # imm = 0xAD48
incq (%rbx,%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xace8(%rbx), %rdi
movq %r14, %rsi
callq 0xc248
cmpl $-0x1, %eax
je 0x63dc
movl 0x8(%r14), %edx
movq 0xad80(%rbx), %rsi
movslq %eax, %rcx
imulq $0x380, %rcx, %rdi      # imm = 0x380
movl $0xad58, %ecx           # imm = 0xAD58
cmpl 0x8(%rsi,%rdi), %edx
jge 0x6384
addq %rdi, %rsi
addq $0x8, %rsi
movl %edx, (%rsi)
jmp 0x6384
movl 0xad1c(%rbx), %eax
cmpl 0x7384(%rbx), %eax
jne 0x63f6
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl $0xad78, %ecx           # imm = 0xAD78
jmp 0x638d
movslq 0xad18(%rbx), %r15
movq 0xad80(%rbx), %rdi
imulq $0x380, %r15, %r12      # imm = 0x380
cmpq $0x0, 0x340(%rdi,%r12)
jne 0x649f
addq %r12, %rdi
movl $0x380, %edx            # imm = 0x380
movq %r14, %rsi
callq 0x31c0
movq 0xad80(%rbx), %rax
movq 0x378(%rax,%r12), %rcx
movl 0x330(%r14), %edx
leaq 0x7aa79(%rip), %rsi     # 0x80ec0
movq (%rsi,%rdx,8), %rdx
movl 0x738c(%rbx), %esi
cmpq %rdx, %rcx
cmovaq %rcx, %rdx
addq %rsi, %rdx
movq %rdx, 0x378(%rax,%r12)
incl 0xad1c(%rbx)
movl 0xad18(%rbx), %eax
incl %eax
xorl %ecx, %ecx
cmpl 0xad08(%rbx), %eax
cmovbl %eax, %ecx
movl %ecx, 0xad18(%rbx)
cmpq $0x0, 0x340(%r14)
je 0x6599
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xad60, %ecx           # imm = 0xAD60
jmp 0x6384
movq 0x13b4a(%rip), %r14     # 0x19ff0
leaq 0xdbe3(%rip), %rsi      # 0x14090
movq %r14, %rdi
callq 0x3250
leaq 0x7350(%rbx), %rsi
movq %rax, %rdi
callq 0x31e0
leaq 0xe84b(%rip), %rsi      # 0x14d16
movq %rax, %rdi
callq 0x3250
leaq 0xdd9a(%rip), %rsi      # 0x14274
movq %rax, %rdi
callq 0x3250
leaq 0xdd4a(%rip), %rsi      # 0x14233
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl %r15d, %esi
callq 0x33c0
leaq 0xdc80(%rip), %rsi      # 0x14183
movq %r14, %rdi
callq 0x3250
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0xad80(%rbx), %rcx
movq 0x340(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x3090
leaq 0xdc50(%rip), %rsi      # 0x1418e
movq %r14, %rdi
callq 0x3250
movq 0xad80(%rbx), %rcx
movq 0x348(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x3090
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
callq 0x3110
leaq 0xdae6(%rip), %rdi      # 0x14067
leaq 0xdae1(%rip), %rsi      # 0x14069
leaq 0xdcec(%rip), %rcx      # 0x1427b
movl $0x4c9, %edx            # imm = 0x4C9
callq 0x3160
leaq 0xdac7(%rip), %rdi      # 0x14067
leaq 0xdac2(%rip), %rsi      # 0x14069
leaq 0xdccd(%rip), %rcx      # 0x1427b
movl $0x4e1, %edx            # imm = 0x4E1
callq 0x3160