// File: /workspace/github_cpp_makefile_star2_O1/NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::add_wq(PACKET*)
int CACHE::add_wq(PACKET *packet)
{
    // check for duplicates in the write queue
    int index = WQ.check_queue(packet);
    if (index != -1) {

        WQ.MERGED++;
        WQ.ACCESS++;

        return index; // merged index
    }

    // sanity check
    if (WQ.occupancy >= WQ.SIZE)
        assert(0);

    // if there is no duplicate, add it to the write queue
    index = WQ.tail;
    if (WQ.entry[index].address != 0) {
        cerr << "[" << NAME << "_ERROR] " << __func__ << " is not empty index: " << index;
        cerr << " address: " << hex << WQ.entry[index].address;
        cerr << " full_addr: " << WQ.entry[index].full_addr << dec << endl;
        assert(0);
    }

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

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

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

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

    WQ.TO_CACHE++;
    WQ.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 0xc1c0
cmpl $-0x1, %eax
je 0x5eab
movl $0x7418, %ecx           # imm = 0x7418
jmp 0x5f61
movl 0x73dc(%rbx), %eax
cmpl 0x73c8(%rbx), %eax
jae 0x5f78
movslq 0x73d8(%rbx), %r15
movq 0x7440(%rbx), %rdi
imulq $0x380, %r15, %r12      # imm = 0x380
cmpq $0x0, 0x340(%rdi,%r12)
jne 0x5f97
addq %r12, %rdi
movl $0x380, %edx            # imm = 0x380
movq %r14, %rsi
callq 0x31c0
movq 0x7440(%rbx), %rax
movq 0x378(%rax,%r12), %rcx
movl 0x330(%r14), %edx
leaq 0x7afb2(%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)
movq 0x73d8(%rbx), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x73d8(%rbx)
movd %xmm0, %eax
xorl %ecx, %ecx
cmpl 0x73c8(%rbx), %eax
cmovbl %eax, %ecx
movl %ecx, 0x73d8(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7420, %ecx           # imm = 0x7420
incq (%rbx,%rcx)
incq 0x7408(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xe0e8(%rip), %rdi      # 0x14067
leaq 0xe0e3(%rip), %rsi      # 0x14069
leaq 0xe2bc(%rip), %rcx      # 0x14249
movl $0x430, %edx            # imm = 0x430
callq 0x3160
movq 0x14052(%rip), %r14     # 0x19ff0
leaq 0xe0eb(%rip), %rsi      # 0x14090
movq %r14, %rdi
callq 0x3250
leaq 0x7350(%rbx), %rsi
movq %rax, %rdi
callq 0x31e0
leaq 0xed23(%rip), %rsi      # 0x14ce6
movq %rax, %rdi
callq 0x3250
leaq 0xe29b(%rip), %rsi      # 0x1426d
movq %rax, %rdi
callq 0x3250
leaq 0xe252(%rip), %rsi      # 0x14233
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl %r15d, %esi
callq 0x33c0
leaq 0xe188(%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 0x7440(%rbx), %rcx
movq 0x340(%rcx,%r12), %rsi
movq %rax, %rdi
callq 0x3090
leaq 0xe158(%rip), %rsi      # 0x1418e
movq %r14, %rdi
callq 0x3250
movq 0x7440(%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 0xdfee(%rip), %rdi      # 0x14067
leaq 0xdfe9(%rip), %rsi      # 0x14069
leaq 0xe1c2(%rip), %rcx      # 0x14249
movl $0x438, %edx            # imm = 0x438
callq 0x3160
nop