// File: NishadSaraf[P]Championship-Simulator/src/dram_controller.cc
// Function: MEMORY_CONTROLLER::reset_remain_requests(PACKET_QUEUE*)
void MEMORY_CONTROLLER::reset_remain_requests(PACKET_QUEUE *queue)
{
    for (uint32_t i=0; i<queue->SIZE; i++) {
        if (queue->entry[i].scheduled) {

            uint64_t op_addr = queue->entry[i].address;
            uint32_t op_cpu = queue->entry[i].cpu,
                     op_channel = dram_get_channel(op_addr), 
                     op_rank = dram_get_rank(op_addr), 
                     op_bank = dram_get_bank(op_addr), 
                     op_row = dram_get_row(op_addr);

#ifdef DEBUG_PRINT
            //uint32_t op_column = dram_get_column(op_addr);
#endif

            // update open row
            if ((bank_request[op_channel][op_rank][op_bank].cycle_available - tCAS) <= current_core_cycle[op_cpu])
                bank_request[op_channel][op_rank][op_bank].open_row = op_row;
            else
                bank_request[op_channel][op_rank][op_bank].open_row = UINT32_MAX;

            // this bank is ready for another DRAM request
            bank_request[op_channel][op_rank][op_bank].request_index = -1;
            bank_request[op_channel][op_rank][op_bank].row_buffer_hit = 0;
            bank_request[op_channel][op_rank][op_bank].working = 0;
            bank_request[op_channel][op_rank][op_bank].cycle_available = current_core_cycle[op_cpu];
            if (bank_request[op_channel][op_rank][op_bank].is_write) {
                scheduled_writes--;
                bank_request[op_channel][op_rank][op_bank].is_write = 0;
            }
            else if (bank_request[op_channel][op_rank][op_bank].is_read) {
                scheduled_reads--;
                bank_request[op_channel][op_rank][op_bank].is_read = 0;
            }

            queue->entry[i].scheduled = 0;
            queue->entry[i].event_cycle = current_core_cycle[op_cpu];

            DP ( if (warmup_complete[op_cpu]) {
            cout << queue->NAME << " instr_id: " << queue->entry[i].instr_id << " swrites: " << scheduled_writes << " sreads: " << scheduled_reads << endl; });

        }
    }
    
    update_schedule_cycle(&RQ);
    update_schedule_cycle(&WQ);
    update_process_cycle(&RQ);
    update_process_cycle(&WQ);

#ifdef SANITY_CHECK
    if (queue->is_WQ) {
        if (scheduled_writes != 0)
            assert(0);
    }
    else {
        if (scheduled_reads != 0)
            assert(0);
    }
#endif
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jae 0x133ca
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movl 0x34(%rsp), %ecx
imulq $0x380, %rcx, %rcx      # imm = 0x380
addq %rcx, %rax
cmpb $0x0, 0x2(%rax)
je 0x133b8
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movl 0x34(%rsp), %ecx
imulq $0x380, %rcx, %rcx      # imm = 0x380
addq %rcx, %rax
movq 0x340(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movl 0x34(%rsp), %ecx
imulq $0x380, %rcx, %rcx      # imm = 0x380
addq %rcx, %rax
movl 0x330(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rsi
callq 0x13490
movq 0x8(%rsp), %rdi
movl %eax, 0x20(%rsp)
movq 0x28(%rsp), %rsi
callq 0x134a0
movq 0x8(%rsp), %rdi
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rsi
callq 0x134d0
movq 0x8(%rsp), %rdi
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rsi
callq 0x13500
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x14(%rsp)
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl 0x18123(%rip), %ecx     # 0x2b21c
subq %rcx, %rax
movl 0x24(%rsp), %ecx
movl %ecx, %edx
leaq 0x7cdb7(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
ja 0x1314a
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %edx
imulq $0xa00, %rdx, %rdx      # imm = 0xA00
addq %rdx, %rax
movl 0x1c(%rsp), %edx
imulq $0x140, %rdx, %rdx      # imm = 0x140
addq %rdx, %rax
movl 0x18(%rsp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
jmp 0x13183
movq 0x8(%rsp), %rax
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
movq %rax, %rcx
addq $0x7858, %rcx           # imm = 0x7858
movl 0x20(%rsp), %edx
imulq $0xa00, %rdx, %rdx      # imm = 0xA00
addq %rdx, %rcx
movl 0x1c(%rsp), %edx
imulq $0x140, %rdx, %rdx      # imm = 0x140
addq %rdx, %rcx
movl 0x18(%rsp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl $0xffffffff, 0x24(%rcx) # imm = 0xFFFFFFFF
movq %rax, %rcx
addq $0x7858, %rcx           # imm = 0x7858
movl 0x20(%rsp), %edx
imulq $0xa00, %rdx, %rdx      # imm = 0xA00
addq %rdx, %rcx
movl 0x1c(%rsp), %edx
imulq $0x140, %rdx, %rdx      # imm = 0x140
addq %rdx, %rcx
movl 0x18(%rsp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movb $0x0, 0x1e(%rcx)
movq %rax, %rcx
addq $0x7858, %rcx           # imm = 0x7858
movl 0x20(%rsp), %edx
imulq $0xa00, %rdx, %rdx      # imm = 0xA00
addq %rdx, %rcx
movl 0x1c(%rsp), %edx
imulq $0x140, %rdx, %rdx      # imm = 0x140
addq %rdx, %rcx
movl 0x18(%rsp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movb $0x0, 0x1c(%rcx)
movl 0x24(%rsp), %ecx
movl %ecx, %edx
leaq 0x7cc89(%rip), %rcx     # 0x8fec0
movq (%rcx,%rdx,8), %rdx
movq %rax, %rcx
addq $0x7858, %rcx           # imm = 0x7858
movl 0x20(%rsp), %esi
imulq $0xa00, %rsi, %rsi      # imm = 0xA00
addq %rsi, %rcx
movl 0x1c(%rsp), %esi
imulq $0x140, %rsi, %rsi      # imm = 0x140
addq %rsi, %rcx
movl 0x18(%rsp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movq %rdx, (%rcx)
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x20(%rax)
je 0x132e9
movq 0x8(%rsp), %rax
movl 0x784c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x784c(%rax)
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x20(%rax)
jmp 0x13368
movq 0x8(%rsp), %rax
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x21(%rax)
je 0x13366
movq 0x8(%rsp), %rax
movl 0x7848(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x7848(%rax)
addq $0x7858, %rax           # imm = 0x7858
movl 0x20(%rsp), %ecx
imulq $0xa00, %rcx, %rcx      # imm = 0xA00
addq %rcx, %rax
movl 0x1c(%rsp), %ecx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x21(%rax)
jmp 0x13368
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movl 0x34(%rsp), %ecx
imulq $0x380, %rcx, %rcx      # imm = 0x380
addq %rcx, %rax
movb $0x0, 0x2(%rax)
movl 0x24(%rsp), %eax
movl %eax, %ecx
leaq 0x7cb2d(%rip), %rax     # 0x8fec0
movq (%rax,%rcx,8), %rcx
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movl 0x34(%rsp), %edx
imulq $0x380, %rdx, %rdx      # imm = 0x380
addq %rdx, %rax
movq %rcx, 0x378(%rax)
jmp 0x133ba
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x12ff0
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
addq $0x9ef8, %rsi           # imm = 0x9EF8
callq 0x13530
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
addq $0x8258, %rsi           # imm = 0x8258
callq 0x13530
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
addq $0x9ef8, %rsi           # imm = 0x9EF8
callq 0x13650
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
addq $0x8258, %rsi           # imm = 0x8258
callq 0x13650
movq 0x38(%rsp), %rax
cmpb $0x0, 0x25(%rax)
je 0x13454
movq 0x8(%rsp), %rax
cmpl $0x0, 0x784c(%rax)
je 0x13452
leaq 0xfbca(%rip), %rdi      # 0x23004
leaq 0x10620(%rip), %rsi     # 0x23a61
movl $0x3c, %edx
leaq 0x1062b(%rip), %rcx     # 0x23a78
callq 0x3160
jmp 0x13483
movq 0x8(%rsp), %rax
cmpl $0x0, 0x7848(%rax)
je 0x13481
leaq 0xfb9b(%rip), %rdi      # 0x23004
leaq 0x105f1(%rip), %rsi     # 0x23a61
movl $0x40, %edx
leaq 0x105fc(%rip), %rcx     # 0x23a78
callq 0x3160
jmp 0x13483
addq $0x48, %rsp
retq
nopl (%rax,%rax)