// File: /workspace/github_cpp_makefile_star2_O1/NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::handle_writeback()
void CACHE::handle_writeback()
{
    // handle write
    uint32_t writeback_cpu = WQ.entry[WQ.head].cpu;
    if (writeback_cpu == NUM_CPUS)
        return;

    // handle the oldest entry
    if ((WQ.entry[WQ.head].event_cycle <= current_core_cycle[writeback_cpu]) && (WQ.occupancy > 0)) {
        int index = WQ.head;

        // access cache
        uint32_t set = get_set(WQ.entry[index].address);
        int way = check_hit(&WQ.entry[index]);
        
        if (way >= 0) { // writeback hit (or RFO hit for L1D)

            if (cache_type == IS_LLC) {
                llc_update_replacement_state(writeback_cpu, set, way, block[set][way].full_addr, WQ.entry[index].ip, 0, WQ.entry[index].type, 1);

            }
            else
                update_replacement_state(writeback_cpu, set, way, block[set][way].full_addr, WQ.entry[index].ip, 0, WQ.entry[index].type, 1);

            // COLLECT STATS
            sim_hit[writeback_cpu][WQ.entry[index].type]++;
            sim_access[writeback_cpu][WQ.entry[index].type]++;

            // mark dirty
            block[set][way].dirty = 1;

            if (cache_type == IS_ITLB)
                WQ.entry[index].instruction_pa = block[set][way].data;
            else if (cache_type == IS_DTLB)
                WQ.entry[index].data_pa = block[set][way].data;
            else if (cache_type == IS_STLB)
                WQ.entry[index].data = block[set][way].data;

            // check fill level
            if (WQ.entry[index].fill_level < fill_level) {

                if (WQ.entry[index].instruction) 
                    upper_level_icache[writeback_cpu]->return_data(&WQ.entry[index]);
                else // data
                    upper_level_dcache[writeback_cpu]->return_data(&WQ.entry[index]);
            }

            HIT[WQ.entry[index].type]++;
            ACCESS[WQ.entry[index].type]++;

            // remove this entry from WQ
            WQ.remove_queue(&WQ.entry[index]);
        }
        else { // writeback miss (or RFO miss for L1D)
            
            DP ( if (warmup_complete[writeback_cpu]) {
            cout << "[" << NAME << "] " << __func__ << " type: " << +WQ.entry[index].type << " miss";
            cout << " instr_id: " << WQ.entry[index].instr_id << " address: " << hex << WQ.entry[index].address;
            cout << " full_addr: " << WQ.entry[index].full_addr << dec;
            cout << " cycle: " << WQ.entry[index].event_cycle << endl; });

            if (cache_type == IS_L1D) { // RFO miss

                // check mshr
                uint8_t miss_handled = 1;
                int mshr_index = check_mshr(&WQ.entry[index]);

                if ((mshr_index == -1) && (MSHR.occupancy < MSHR_SIZE)) { // this is a new miss

                    // add it to mshr (RFO miss)
                    add_mshr(&WQ.entry[index]);

                    // add it to the next level's read queue
                    //if (lower_level) // L1D always has a lower level cache
                        lower_level->add_rq(&WQ.entry[index]);
                }
                else {
                    if ((mshr_index == -1) && (MSHR.occupancy == MSHR_SIZE)) { // not enough MSHR resource
                        
                        // cannot handle miss request until one of MSHRs is available
                        miss_handled = 0;
                        STALL[WQ.entry[index].type]++;
                    }
                    else if (mshr_index != -1) { // already in-flight miss

                        // update fill_level
                        if (WQ.entry[index].fill_level < MSHR.entry[mshr_index].fill_level)
                            MSHR.entry[mshr_index].fill_level = WQ.entry[index].fill_level;

                        // update request
                        if (MSHR.entry[mshr_index].type == PREFETCH) {
                            uint8_t  prior_returned = MSHR.entry[mshr_index].returned;
                            uint64_t prior_event_cycle = MSHR.entry[mshr_index].event_cycle;
                            memcpy(&MSHR.entry[mshr_index], &WQ.entry[index], sizeof(PACKET));

                            // in case request is already returned, we should keep event_cycle and retunred variables
                            MSHR.entry[mshr_index].returned = prior_returned;
                            MSHR.entry[mshr_index].event_cycle = prior_event_cycle;
                        }

                        MSHR_MERGED[WQ.entry[index].type]++;

                        DP ( if (warmup_complete[writeback_cpu]) {
                        cout << "[" << NAME << "] " << __func__ << " mshr merged";
                        cout << " instr_id: " << WQ.entry[index].instr_id << " prior_id: " << MSHR.entry[mshr_index].instr_id; 
                        cout << " address: " << hex << WQ.entry[index].address;
                        cout << " full_addr: " << WQ.entry[index].full_addr << dec;
                        cout << " cycle: " << WQ.entry[index].event_cycle << endl; });
                    }
                    else { // WE SHOULD NOT REACH HERE
                        cerr << "[" << NAME << "] MSHR errors" << endl;
                        assert(0);
                    }
                }

                if (miss_handled) {

                    MISS[WQ.entry[index].type]++;
                    ACCESS[WQ.entry[index].type]++;

                    // remove this entry from WQ
                    WQ.remove_queue(&WQ.entry[index]);
                }

            }
            else {
                // find victim
                uint32_t set = get_set(WQ.entry[index].address), way;
                if (cache_type == IS_LLC) {
                    way = llc_find_victim(writeback_cpu, WQ.entry[index].instr_id, set, block[set], WQ.entry[index].ip, WQ.entry[index].full_addr, WQ.entry[index].type);
                }
                else
                    way = find_victim(writeback_cpu, WQ.entry[index].instr_id, set, block[set], WQ.entry[index].ip, WQ.entry[index].full_addr, WQ.entry[index].type);

#ifdef LLC_BYPASS
                if ((cache_type == IS_LLC) && (way == LLC_WAY)) {
                    cerr << "LLC bypassing for writebacks is not allowed!" << endl;
                    assert(0);
                }
#endif

                uint8_t  do_fill = 1;

                // is this dirty?
                if (block[set][way].dirty) {

                    // check if the lower level WQ has enough room to keep this writeback request
                    if (lower_level) { 
                        if (lower_level->get_occupancy(2) == lower_level->get_size(2)) {

                            // lower level WQ is full, cannot replace this victim
                            do_fill = 0;
                            lower_level->increment_WQ_FULL();
                            STALL[WQ.entry[index].type]++;

                            DP ( if (warmup_complete[writeback_cpu]) {
                            cout << "[" << NAME << "] " << __func__ << "do_fill: " << +do_fill;
                            cout << " lower level wq is full!" << " fill_addr: " << hex << WQ.entry[index].address;
                            cout << " victim_addr: " << block[set][way].tag << dec << endl; });
                        }
                        else { 
                            PACKET writeback_packet;

                            writeback_packet.fill_level = fill_level << 1;
                            writeback_packet.cpu = writeback_cpu;
                            writeback_packet.address = block[set][way].address;
                            writeback_packet.full_addr = block[set][way].full_addr;
                            writeback_packet.data = block[set][way].data;
                            writeback_packet.instr_id = WQ.entry[index].instr_id;
                            writeback_packet.ip = 0;
                            writeback_packet.type = WRITEBACK;
                            writeback_packet.event_cycle = current_core_cycle[writeback_cpu];

                            lower_level->add_wq(&writeback_packet);
                        }
                    }
#ifdef SANITY_CHECK
                    else {
                        // sanity check
                        if (cache_type != IS_STLB)
                            assert(0);
                    }
#endif
                }

                if (do_fill) {
                    // update prefetcher
                    if (cache_type == IS_L1D)
                        l1d_prefetcher_cache_fill(WQ.entry[index].full_addr, set, way, 0, block[set][way].full_addr);
                    else if (cache_type == IS_L2C)
                        l2c_prefetcher_cache_fill(WQ.entry[index].full_addr, set, way, 0, block[set][way].full_addr);

                    // update replacement policy
                    if (cache_type == IS_LLC) {
                        llc_update_replacement_state(writeback_cpu, set, way, WQ.entry[index].full_addr, WQ.entry[index].ip, block[set][way].full_addr, WQ.entry[index].type, 0);

                    }
                    else
                        update_replacement_state(writeback_cpu, set, way, WQ.entry[index].full_addr, WQ.entry[index].ip, block[set][way].full_addr, WQ.entry[index].type, 0);

                    // COLLECT STATS
                    sim_miss[writeback_cpu][WQ.entry[index].type]++;
                    sim_access[writeback_cpu][WQ.entry[index].type]++;

                    fill_cache(set, way, &WQ.entry[index]);

                    // mark dirty
                    block[set][way].dirty = 1; 

                    // check fill level
                    if (WQ.entry[index].fill_level < fill_level) {

                        if (WQ.entry[index].instruction) 
                            upper_level_icache[writeback_cpu]->return_data(&WQ.entry[index]);
                        else // data
                            upper_level_dcache[writeback_cpu]->return_data(&WQ.entry[index]);
                    }

                    MISS[WQ.entry[index].type]++;
                    ACCESS[WQ.entry[index].type]++;

                    // remove this entry from WQ
                    WQ.remove_queue(&WQ.entry[index]);
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq 0x7440(%rdi), %rax
movl 0x73d4(%rdi), %ecx
imulq $0x380, %rcx, %rdx      # imm = 0x380
movl 0x330(%rax,%rdx), %r15d
cmpq $0x1, %r15
jne 0x41cd
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rax, %rdx
movq 0x378(%rdx), %rdx
leaq 0x7cce2(%rip), %r13     # 0x80ec0
cmpq (%r13,%r15,8), %rdx
ja 0x41bb
movq %rdi, %rbx
cmpl $0x0, 0x73dc(%rdi)
je 0x41bb
leaq 0x73a8(%rbx), %rdx
movq %rdx, 0x18(%rsp)
movslq %ecx, %rcx
imulq $0x380, %rcx, %r14      # imm = 0x380
movl 0x340(%rax,%r14), %r12d
movl 0x7370(%rbx), %edi
callq 0x9196
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %eax, %ecx
shll %cl, %ebp
movq 0x7440(%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x495c
movb 0x73a4(%rbx), %cl
testl %eax, %eax
js 0x429a
notl %ebp
andl %r12d, %ebp
movl %eax, %r12d
leaq (%r12,%r12,4), %rdx
shlq $0x4, %rdx
movl %ebp, %r13d
movq 0x7390(%rbx), %rsi
movq 0x7440(%rbx), %rdi
movq (%rsi,%r13,8), %rsi
movq 0x20(%rsi,%rdx), %r8
movq 0x370(%rdi,%r14), %r9
movzbl 0x32f(%rdi,%r14), %r10d
subq $0x8, %rsp
cmpb $0x6, %cl
jne 0x42f6
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %eax, %ecx
pushq $0x1
pushq %r10
pushq $0x0
callq 0x1385c
jmp 0x430b
movq 0x7440(%rbx), %rax
cmpb $0x4, %cl
jne 0x4384
leaq (%rax,%r14), %rsi
movl 0x7388(%rbx), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rdx, %rdx
je 0x44f9
movq 0x340(%rsi), %rdi
movl $0x340, %r8d            # imm = 0x340
addq 0xca20(%rbx), %r8
xorl %r9d, %r9d
cmpq %rdi, (%r8)
je 0x44f6
incq %r9
addq $0x380, %r8             # imm = 0x380
cmpq %r9, %rdx
jne 0x42d9
jmp 0x44f9
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %eax, %ecx
pushq $0x1
pushq %r10
pushq $0x0
callq 0x137be
addq $0x20, %rsp
movq 0x7440(%rbx), %rsi
movzbl 0x32f(%rsi,%r14), %eax
movq %r15, %rcx
shlq $0x5, %rcx
addq %rbx, %rcx
incq 0x10308(%rcx,%rax,8)
incq 0x102e8(%rcx,%rax,8)
movq 0x7390(%rbx), %rax
movq (%rax,%r13,8), %rax
leaq (%r12,%r12,4), %rcx
shlq $0x4, %rcx
movb $0x1, 0x2(%rax,%rcx)
movzbl 0x73a4(%rbx), %edx
cmpl $0x2, %edx
je 0x4406
cmpl $0x1, %edx
je 0x43f7
testl %edx, %edx
jne 0x4413
movq 0x30(%rax,%rcx), %rax
movq %rax, 0x350(%rsi,%r14)
jmp 0x4413
movl 0x340(%rax,%r14), %ebp
movl 0x7370(%rbx), %edi
callq 0x9196
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %eax, %ecx
shll %cl, %r12d
notl %r12d
andl %ebp, %r12d
cmpb $0x6, 0x73a4(%rbx)
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r14), %rax
movq 0x368(%rcx,%r14), %rdx
movq 0x7390(%rbx), %rsi
movq (%rsi,%r12,8), %r8
movq 0x370(%rcx,%r14), %r9
movzbl 0x32f(%rcx,%r14), %r10d
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %ecx
jne 0x4457
pushq %r10
pushq %rax
callq 0x13856
jmp 0x445f
movq 0x30(%rax,%rcx), %rax
movq %rax, 0x358(%rsi,%r14)
jmp 0x4413
movq 0x30(%rax,%rcx), %rax
movq %rax, 0x360(%rsi,%r14)
movl 0x8(%rsi,%r14), %eax
cmpl 0x7398(%rbx), %eax
jge 0x443a
addq %r14, %rsi
xorl %eax, %eax
cmpb $0x0, (%rsi)
sete %al
leaq (%rbx,%rax,8), %rax
movq 0x8(%rax,%r15,8), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x7440(%rbx), %rax
movzbl 0x32f(%rax,%r14), %ecx
incq 0x72c8(%rbx,%rcx,8)
jmp 0x45bd
pushq %r10
pushq %rax
callq 0x136b8
addq $0x10, %rsp
movl %eax, %ebp
movb 0x73a4(%rbx), %al
cmpb $0x6, %al
setne %cl
cmpl $0x10, %ebp
setne %dl
orb %cl, %dl
je 0x4922
movq 0x7390(%rbx), %rcx
movl %r12d, %edx
movq %rdx, 0x10(%rsp)
movq (%rcx,%rdx,8), %rcx
movl %ebp, %edx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
cmpb $0x0, 0x2(%rcx,%rdx)
movq %rdx, 0x8(%rsp)
je 0x4703
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x45e4
movl %ebp, 0x24(%rsp)
movq (%rdi), %rax
movl $0x2, %esi
callq *0x30(%rax)
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x38(%rax)
cmpl %eax, %ebp
jne 0x460b
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x7440(%rbx), %rax
jmp 0x48a8
movl %r9d, %ecx
cmpl $-0x1, %ecx
je 0x457c
movl 0x8(%rax,%r14), %eax
movq 0xca20(%rbx), %rdi
movslq %ecx, %rcx
imulq $0x380, %rcx, %r15      # imm = 0x380
cmpl 0x8(%rdi,%r15), %eax
jge 0x4525
leaq (%rdi,%r15), %rcx
addq $0x8, %rcx
movl %eax, (%rcx)
cmpb $0x2, 0x32f(%rdi,%r15)
jne 0x4562
addq %r15, %rdi
movb 0x32c(%rdi), %bpl
movq 0x378(%rdi), %r12
movl $0x380, %edx            # imm = 0x380
callq 0x31c0
movq 0xca20(%rbx), %rax
movb %bpl, 0x32c(%rax,%r15)
movq %r12, 0x378(%rax,%r15)
movq 0x7440(%rbx), %rax
movzbl 0x32f(%rax,%r14), %eax
incq 0x7308(%rbx,%rax,8)
jmp 0x45a5
movl 0xc9bc(%rbx), %edi
cmpl %edx, %edi
jae 0x48a4
movq %rbx, %rdi
callq 0x4b3e
movq 0x18(%rbx), %rdi
movq 0x7440(%rbx), %rsi
addq %r14, %rsi
movq (%rdi), %rax
callq *(%rax)
movq 0x7440(%rbx), %rax
movzbl 0x32f(%rax,%r14), %ecx
incq 0x72e8(%rbx,%rcx,8)
incq 0x72a8(%rbx,%rcx,8)
leaq (%rax,%r14), %rsi
movq 0x18(%rsp), %rdi
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc3e6
cmpb $0x2, %al
je 0x4703
leaq 0xfa74(%rip), %rdi      # 0x14067
leaq 0xfa6f(%rip), %rsi      # 0x14069
leaq 0xfa9f(%rip), %rcx      # 0x140a0
movl $0x15f, %edx            # imm = 0x15F
callq 0x3160
xorl %eax, %eax
leaq 0x40(%rsp), %rbp
movl %eax, -0x15(%rbp)
movl %eax, -0x18(%rbp)
movl $0xffff00, 0x314(%rbp)  # imm = 0xFFFF00
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, -0x10(%rbp)
movl $0x314, %edx            # imm = 0x314
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3130
movl $0x1, 0x318(%rbp)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x31c(%rbp)
movdqu %xmm0, 0x32c(%rbp)
xorl %eax, %eax
movl %eax, 0x33c(%rbp)
movdqu %xmm0, 0x348(%rbp)
movq %rax, 0x358(%rbp)
xorl %edx, %edx
movq $-0x1, 0x360(%rbp)
movl 0x7398(%rbx), %eax
addl %eax, %eax
movl %eax, -0x10(%rbp)
movl %r15d, 0x318(%rbp)
movq 0x7390(%rbx), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rsp), %rcx
movups 0x18(%rax,%rcx), %xmm0
movups %xmm0, 0x328(%rbp)
movq 0x30(%rax,%rcx), %rax
movq %rax, 0x348(%rbp)
movq 0x7440(%rbx), %rax
movq 0x368(%rax,%r14), %rax
movq %rax, 0x350(%rbp)
movq %rdx, 0x358(%rbp)
movb $0x3, 0x317(%rbp)
movq (%r13,%r15,8), %rax
movq %rax, 0x360(%rbp)
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
leaq 0x28(%rsp), %rsi
callq *0x8(%rax)
movq 0x8(%rsp), %rdx
movl 0x24(%rsp), %ebp
movzbl 0x73a4(%rbx), %eax
cmpl $0x5, %eax
je 0x474a
cmpl $0x4, %eax
movq 0x10(%rsp), %r13
jne 0x4783
movq 0x7390(%rbx), %rax
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r14), %rsi
movq (%rax,%r13,8), %rax
movq 0x20(%rax,%rdx), %r9
movq %rbx, %rdi
movl %r12d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x1399c
jmp 0x477e
movq 0x7390(%rbx), %rax
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r14), %rsi
movq 0x10(%rsp), %r13
movq (%rax,%r13,8), %rax
movq 0x20(%rax,%rdx), %r9
movq %rbx, %rdi
movl %r12d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x13994
movq 0x8(%rsp), %rdx
cmpb $0x6, 0x73a4(%rbx)
movq 0x7440(%rbx), %rcx
movq 0x348(%rcx,%r14), %r8
movq 0x370(%rcx,%r14), %r9
movq 0x7390(%rbx), %rax
movq (%rax,%r13,8), %rax
movq 0x20(%rax,%rdx), %rax
movzbl 0x32f(%rcx,%r14), %r10d
jne 0x47d7
subq $0x8, %rsp
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %ebp, %ecx
pushq $0x0
pushq %r10
pushq %rax
callq 0x1385c
jmp 0x47f0
subq $0x8, %rsp
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %ebp, %ecx
pushq $0x0
pushq %r10
pushq %rax
callq 0x137be
addq $0x20, %rsp
movq 0x7440(%rbx), %rcx
movzbl 0x32f(%rcx,%r14), %eax
movq %r15, %rdx
shlq $0x5, %rdx
addq %rbx, %rdx
incq 0x10328(%rdx,%rax,8)
addq %r14, %rcx
incq 0x102e8(%rdx,%rax,8)
movq %rbx, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x4044
movq 0x7390(%rbx), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rsp), %rcx
movb $0x1, 0x2(%rax,%rcx)
movq 0x7440(%rbx), %rsi
movl 0x8(%rsi,%r14), %eax
cmpl 0x7398(%rbx), %eax
jge 0x4871
addq %r14, %rsi
xorl %eax, %eax
cmpb $0x0, (%rsi)
sete %al
leaq (%rbx,%rax,8), %rax
movq 0x8(%rax,%r15,8), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x7440(%rbx), %rax
movzbl 0x32f(%rax,%r14), %ecx
incq 0x72e8(%rbx,%rcx,8)
leaq (%rax,%r14), %rsi
incq 0x72a8(%rbx,%rcx,8)
movq 0x18(%rsp), %rdi
callq 0xc3e6
jmp 0x41bb
cmpl %edx, %edi
jne 0x48be
movzbl 0x32f(%rax,%r14), %eax
incq 0x7328(%rbx,%rax,8)
jmp 0x41bb
cmpl $-0x1, %ecx
jne 0x44fe
movq 0x15722(%rip), %rdi     # 0x19ff0
leaq 0xf7bb(%rip), %rsi      # 0x14090
callq 0x3250
addq $0x7350, %rbx           # imm = 0x7350
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31e0
leaq 0xf79f(%rip), %rsi      # 0x14092
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3110
leaq 0xf75d(%rip), %rdi      # 0x14067
leaq 0xf758(%rip), %rsi      # 0x14069
leaq 0xf788(%rip), %rcx      # 0x140a0
movl $0x11a, %edx            # imm = 0x11A
callq 0x3160
movq 0x156c7(%rip), %rdi     # 0x19ff0
leaq 0xf78f(%rip), %rsi      # 0x140bf
callq 0x3250
movq %rax, %rdi
callq 0x3110
leaq 0xf723(%rip), %rdi      # 0x14067
leaq 0xf71e(%rip), %rsi      # 0x14069
leaq 0xf74e(%rip), %rcx      # 0x140a0
movl $0x134, %edx            # imm = 0x134
callq 0x3160