// File: /workspace/github_cpp_makefile_star2_O3/MPSLab-ASU[P]DiRAC/./src/controller.cc
// Function: controller::write_back_controller(unsigned short)
void controller::write_back_controller(uint16_t current_RF_pass)
{
  assert(PE_writeback_RF_incomplete == 1 && "Communication of data from PEs to controller is incomplete.");
  bool writeback_completed = true;

  for(uint8_t i=0; i < outNetworks; i++) {

    network_write_op_comm_completed[i] = 1;

    if(write_data_RF_pass[current_RF_pass][i])
    {
      // schedule writeback of operand to PE
      if(cnt_writeback_via_network_RF_pass[i] < RF_pass_network_write_operand[current_RF_pass][i].size())
      {
        uint16_t burst_size_op = RF_pass_network_write_operand_burst_width_elements[current_RF_pass][i][cnt_writeback_via_network_RF_pass[i]];
        network_write_op_comm_completed[i] = 0; // communication incomplete yet.
        if(idx_write_network_packet[i] < burst_size_op) {
          if(is_data_collected[i]) {
            uint8_t operand = RF_pass_network_write_operand[current_RF_pass][i][cnt_writeback_via_network_RF_pass[i]];
            uint8_t buffer_number_comp = spm_buffer_number_op_comp_write[operand];
            uint16_t offset_data_collect = config_data_collect_write_back[i][idx_write_network_packet[i]];
            uint32_t offset_SPM = RF_pass_network_write_operand_offset_base_addr_SPM[current_RF_pass][i][cnt_writeback_via_network_RF_pass[i]];
            uint32_t address_SPM = ((operand_start_addr_SPM_buffer[buffer_number_comp][operand] + offset_SPM) / sizeof(data_type)) + offset_data_collect;
            scratchpad_mem->write(address_SPM, collected_data[i]);
            idx_write_network_packet[i] += 1;
          }
        }
        else {
          cnt_writeback_via_network_RF_pass[i] += 1;
          idx_write_network_packet[i] = 0;
        }
      }
    }
    writeback_completed &= (network_write_op_comm_completed[i] == 1);
  }

  counter_RF_pass_writeback += 1;

  if ((counter_RF_pass_writeback >= total_cycles_RF_pass) && (writeback_completed)) {
    PE_writeback_RF_incomplete = false;
    for(uint8_t i=0; i < inNetworks; i++)
      cnt_writeback_via_network_RF_pass[i] = 0;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpb $0x1, 0xcf(%rdi)
jne 0x410ce2
movq %rdi, %rbx
movq $0x60b5e4, %r8          # imm = 0x60B5E4
cmpb $0x0, (%r8)
je 0x410c84
movzwl %si, %r15d
movq 0x108(%rbx), %rax
movb $0x1, %bpl
xorl %r12d, %r12d
movq $0x60b6b0, %r13         # imm = 0x60B6B0
movq $0x60b6b8, %r9          # imm = 0x60B6B8
movq $0x60b6c8, %r11         # imm = 0x60B6C8
xorl %r14d, %r14d
movb $0x1, (%rax,%r14)
movq (%r13), %rcx
movq (%rcx,%r15,8), %rcx
cmpb $0x1, (%rcx,%r14)
jne 0x410c63
movq 0xe0(%rbx), %rcx
movzbl (%rcx,%r14), %esi
movq (%r9), %rdx
movq (%rdx,%r15,8), %rdi
movq (%rdi,%r12), %rdx
movq 0x8(%rdi,%r12), %rdi
subq %rdx, %rdi
cmpq %rsi, %rdi
jbe 0x410c63
movq (%r11), %rdi
movq (%rdi,%r15,8), %rdi
movq (%rdi,%r12), %rdi
movzwl (%rdi,%rsi,4), %esi
movb $0x0, (%rax,%r14)
movq 0xf0(%rbx), %rax
movzwl (%rax,%r14,2), %eax
cmpl %eax, %esi
jbe 0x410c51
movq 0x110(%rbx), %rsi
cmpb $0x1, (%rsi,%r14)
jne 0x410c63
movzbl (%rcx,%r14), %ecx
movzbl (%rdx,%rcx), %edx
movq $0x60b730, %rsi         # imm = 0x60B730
movq (%rsi), %rsi
movzbl (%rsi,%rdx), %esi
movq $0x60b608, %rdi         # imm = 0x60B608
movq (%rdi), %rdi
movq (%rdi,%r14,8), %rdi
movzwl (%rdi,%rax,2), %eax
movq $0x60b6d0, %rdi         # imm = 0x60B6D0
movq (%rdi), %rdi
movq (%rdi,%r15,8), %rdi
movq (%rdi,%r12), %rdi
movq $0x60b640, %r8          # imm = 0x60B640
movq (%r8), %r8
movq (%r8,%rsi,8), %rsi
movl (%rsi,%rdx,4), %esi
addl (%rdi,%rcx,4), %esi
shrl %esi
addl %eax, %esi
movq 0xb0(%rbx), %rdi
movq 0x118(%rbx), %rax
movzwl (%rax,%r14,2), %edx
callq 0x4174fe
movq $0x60b6c8, %r11         # imm = 0x60B6C8
movq $0x60b6b8, %r9          # imm = 0x60B6B8
movq $0x60b5e4, %r8          # imm = 0x60B5E4
movq 0xf0(%rbx), %rax
incw (%rax,%r14,2)
jmp 0x410c63
incb (%rcx,%r14)
movq 0xf0(%rbx), %rax
movw $0x0, (%rax,%r14,2)
movq 0x108(%rbx), %rax
andb (%rax,%r14), %bpl
incq %r14
movzbl (%r8), %ecx
addq $0x18, %r12
cmpq %rcx, %r14
jb 0x410b49
jmp 0x410c87
movb $0x1, %bpl
movzwl 0xd2(%rbx), %eax
incl %eax
movw %ax, 0xd2(%rbx)
movq $0x60b5e0, %rcx         # imm = 0x60B5E0
cmpw (%rcx), %ax
jb 0x410cd3
testb %bpl, %bpl
je 0x410cd3
movb $0x0, 0xcf(%rbx)
movq $0x609151, %rax         # imm = 0x609151
cmpb $0x0, (%rax)
je 0x410cd3
xorl %ecx, %ecx
movq 0xe0(%rbx), %rdx
movb $0x0, (%rdx,%rcx)
incq %rcx
movzbl (%rax), %edx
cmpq %rdx, %rcx
jb 0x410cbd
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x185078(%rip), %rdi    # 0x595d61
leaq 0x184e7a(%rip), %rsi    # 0x595b6a
leaq 0x1850cb(%rip), %rcx    # 0x595dc2
movl $0x26b, %edx            # imm = 0x26B
callq 0x4d8100
nop