// File: /workspace/github_cpp_makefile_star2_O1/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 0x410c4c
movq %rdi, %rbx
movq $0x60b5e4, %r8          # imm = 0x60B5E4
cmpb $0x0, (%r8)
je 0x410bee
movzwl %si, %r15d
movb $0x1, %bpl
xorl %r12d, %r12d
movq $0x60b6b0, %r13         # imm = 0x60B6B0
movq $0x60b6b8, %r9          # imm = 0x60B6B8
movq $0x60b6c8, %r11         # imm = 0x60B6C8
xorl %r14d, %r14d
movq 0x108(%rbx), %rdx
movb $0x1, (%rdx,%r14)
movq (%r13), %rax
movq (%rax,%r15,8), %rax
cmpb $0x1, (%rax,%r14)
jne 0x410bcd
movq 0xe0(%rbx), %rax
movzbl (%rax,%r14), %esi
movq (%r9), %rcx
movq (%rcx,%r15,8), %rdi
movq (%rdi,%r12), %rcx
movq 0x8(%rdi,%r12), %rdi
subq %rcx, %rdi
cmpq %rsi, %rdi
jbe 0x410bcd
movq (%r11), %rdi
movq (%rdi,%r15,8), %rdi
movq (%rdi,%r12), %rdi
movzwl (%rdi,%rsi,4), %esi
movb $0x0, (%rdx,%r14)
movq 0xf0(%rbx), %rdx
movzwl (%rdx,%r14,2), %edx
cmpl %edx, %esi
jbe 0x410bbb
movq 0x110(%rbx), %rsi
cmpb $0x1, (%rsi,%r14)
jne 0x410bcd
movzbl (%rax,%r14), %eax
movzbl (%rcx,%rax), %ecx
movq $0x60b730, %rsi         # imm = 0x60B730
movq (%rsi), %rsi
movzbl (%rsi,%rcx), %esi
movq $0x60b608, %rdi         # imm = 0x60B608
movq (%rdi), %rdi
movq (%rdi,%r14,8), %rdi
movzwl (%rdi,%rdx,2), %edx
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,%rcx,4), %esi
addl (%rdi,%rax,4), %esi
shrl %esi
addl %edx, %esi
movq 0xb0(%rbx), %rdi
movq 0x118(%rbx), %rax
movzwl (%rax,%r14,2), %edx
callq 0x4169ba
movq $0x60b6c8, %r11         # imm = 0x60B6C8
movq $0x60b6b8, %r9          # imm = 0x60B6B8
movq $0x60b5e4, %r8          # imm = 0x60B5E4
movq 0xf0(%rbx), %rax
incw (%rax,%r14,2)
jmp 0x410bcd
incb (%rax,%r14)
movq 0xf0(%rbx), %rax
movw $0x0, (%rax,%r14,2)
movq 0x108(%rbx), %rax
andb (%rax,%r14), %bpl
incq %r14
movzbl (%r8), %eax
addq $0x18, %r12
cmpq %rax, %r14
jb 0x410aac
jmp 0x410bf1
movb $0x1, %bpl
movzwl 0xd2(%rbx), %eax
incl %eax
movw %ax, 0xd2(%rbx)
movq $0x60b5e0, %rcx         # imm = 0x60B5E0
cmpw (%rcx), %ax
jb 0x410c3d
testb %bpl, %bpl
je 0x410c3d
movb $0x0, 0xcf(%rbx)
movq $0x609151, %rax         # imm = 0x609151
cmpb $0x0, (%rax)
je 0x410c3d
xorl %ecx, %ecx
movq 0xe0(%rbx), %rdx
movb $0x0, (%rdx,%rcx)
incq %rcx
movzbl (%rax), %edx
cmpq %rdx, %rcx
jb 0x410c27
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18510e(%rip), %rdi    # 0x595d61
leaq 0x184f10(%rip), %rsi    # 0x595b6a
leaq 0x185161(%rip), %rcx    # 0x595dc2
movl $0x26b, %edx            # imm = 0x26B
callq 0x4d75c0
nop