// File: /workspace/github_cpp_makefile_star2_O2/MPSLab-ASU[P]DiRAC/./src/controller.cc
// Function: controller::write_back_DRAM(unsigned short)
void controller::write_back_DRAM(uint16_t current_SPM_pass)
{
  assert(current_operand_writeback < total_operands && "invalid operand for write back!");
  if(write_operand_SPM_pass[current_SPM_pass][current_operand_writeback])
  {
    // schedule write-back of operand from SPM to DRAM
    if(current_DMAC_invocation_operand_writeback < write_operand_offset_base_addr_SPM[current_SPM_pass][current_operand_writeback].size())
    {
      uint8_t buffer_number_comm = spm_buffer_number_op_writeback[current_operand_writeback];
      uint32_t offset_SPM = write_operand_offset_base_addr_SPM[current_SPM_pass][current_operand_writeback][current_DMAC_invocation_operand_writeback];
      uint32_t offset_DRAM = write_operand_offset_base_addr_DRAM[current_SPM_pass][current_operand_writeback][current_DMAC_invocation_operand_writeback];
      uint32_t burst_size = write_operand_burst_width_bytes_SPM_pass[current_SPM_pass][current_operand_writeback][current_DMAC_invocation_operand_writeback];
      assert(burst_size > 0 && "burst size for DMAC request must be greater than zero.");
      data_type *address_operand_DRAM = tensor_operands[current_operand_writeback] + (offset_DRAM / sizeof(data_type));
      data_type *address_operand_SPM = scratchpad_mem->get_ptr() + ((operand_start_addr_SPM_buffer[buffer_number_comm][current_operand_writeback] + offset_SPM) / sizeof(data_type));
      bus_req_DMA = true;
      data_mov_SPM_DRAM_in_prog = true;
      DMAC->write_back<data_type>(address_operand_SPM, address_operand_DRAM, burst_size);
      current_DMAC_invocation_operand_writeback += 1;
    }
    else {
      current_DMAC_invocation_operand_writeback = 0;
      current_operand_writeback += 1;
    }
  }
  else
    current_operand_writeback += 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movb 0xcb(%rdi), %cl
movq $0x608556, %rax         # imm = 0x608556
cmpb (%rax), %cl
jae 0x40c91b
movq %rdi, %rbx
addq $0xcb, %rdi
movzbl %cl, %eax
movq $0x608668, %rdx         # imm = 0x608668
movq (%rdx), %r8
movzwl %si, %edx
movq (%r8,%rdx,8), %rsi
cmpb $0x1, (%rsi,%rax)
jne 0x40c903
leaq 0xcd(%rbx), %r12
movzbl 0xcd(%rbx), %esi
movq $0x608670, %r8          # imm = 0x608670
movq (%r8), %r8
movq (%r8,%rdx,8), %r10
imulq $0x18, %rax, %r9
movq (%r10,%r9), %r8
movq 0x8(%r10,%r9), %r10
subq %r8, %r10
sarq $0x2, %r10
cmpq %rsi, %r10
jbe 0x40c8fe
movq $0x608678, %rcx         # imm = 0x608678
movq (%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movq (%rcx,%r9), %rcx
movl (%rcx,%rsi,4), %ebp
testl %ebp, %ebp
je 0x40c93a
movq $0x608680, %rcx         # imm = 0x608680
movq (%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movq (%rcx,%r9), %rcx
shll $0x2, %esi
movl (%rcx,%rsi), %r14d
movl (%r8,%rsi), %r15d
movq $0x608720, %rcx         # imm = 0x608720
movq (%rcx), %rcx
movzbl (%rcx,%rax), %r13d
movq $0x608560, %rcx         # imm = 0x608560
movq (%rcx), %rcx
andl $-0x2, %r14d
addq (%rcx,%rax,8), %r14
movq 0xb0(%rbx), %rdi
callq 0x412978
movq $0x608640, %rcx         # imm = 0x608640
movq (%rcx), %rcx
movq (%rcx,%r13,8), %rcx
movzbl 0xcb(%rbx), %edx
addl (%rcx,%rdx,4), %r15d
andl $-0x2, %r15d
addq %rax, %r15
movb $0x1, %al
movb %al, 0xb8(%rbx)
movb %al, 0xc9(%rbx)
movq 0xa8(%rbx), %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x40d7ca
movb 0xcd(%rbx), %cl
jmp 0x40c906
movb $0x0, (%r12)
movq %rdi, %r12
incb %cl
movb %cl, (%r12)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1852d2(%rip), %rdi    # 0x591bf4
leaq 0x185241(%rip), %rsi    # 0x591b6a
leaq 0x185314(%rip), %rcx    # 0x591c44
movl $0x180, %edx            # imm = 0x180
callq 0x4d36d0
leaq 0x185268(%rip), %rdi    # 0x591ba9
leaq 0x185222(%rip), %rsi    # 0x591b6a
leaq 0x1852f5(%rip), %rcx    # 0x591c44
movl $0x18a, %edx            # imm = 0x18A
callq 0x4d36d0
nop