// File: /workspace/github_cpp_makefile_star2_O2/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 0x40d415
movq %rdi, %rbx
movzwl %si, %r14d
movb $0x1, %bpl
xorl %r15d, %r15d
movq $0x6085e4, %r12         # imm = 0x6085E4
movq $0x6086b0, %r8          # imm = 0x6086B0
movq $0x6086b8, %r9          # imm = 0x6086B8
movq $0x6086c8, %r11         # imm = 0x6086C8
xorl %r13d, %r13d
movzbl (%r12), %eax
cmpq %rax, %r13
jae 0x40d3bc
movq 0x108(%rbx), %rdx
movb $0x1, (%rdx,%r13)
movq (%r8), %rax
movq (%rax,%r14,8), %rax
cmpb $0x1, (%rax,%r13)
jne 0x40d3a5
movq 0xe0(%rbx), %rax
movzbl (%rax,%r13), %esi
movq (%r9), %rcx
movq (%rcx,%r14,8), %rdi
movq (%rdi,%r15), %rcx
movq 0x8(%rdi,%r15), %rdi
subq %rcx, %rdi
cmpq %rsi, %rdi
jbe 0x40d3a5
movq (%r11), %rdi
movq (%rdi,%r14,8), %rdi
movq (%rdi,%r15), %rdi
movzwl (%rdi,%rsi,4), %esi
movb $0x0, (%rdx,%r13)
movq 0xf0(%rbx), %rdx
movzwl (%rdx,%r13,2), %edx
cmpl %edx, %esi
jbe 0x40d394
movq 0x110(%rbx), %rsi
cmpb $0x1, (%rsi,%r13)
jne 0x40d3a5
movzbl (%rax,%r13), %eax
movzbl (%rcx,%rax), %ecx
movq $0x608730, %rsi         # imm = 0x608730
movq (%rsi), %rsi
movzbl (%rsi,%rcx), %esi
movq $0x608608, %rdi         # imm = 0x608608
movq (%rdi), %rdi
movq (%rdi,%r13,8), %rdi
movzwl (%rdi,%rdx,2), %edx
movq $0x6086d0, %rdi         # imm = 0x6086D0
movq (%rdi), %rdi
movq (%rdi,%r14,8), %rdi
movq (%rdi,%r15), %rdi
movq $0x608640, %r8          # imm = 0x608640
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,%r13,2), %edx
callq 0x41296e
movq $0x6086c8, %r11         # imm = 0x6086C8
movq $0x6086b8, %r9          # imm = 0x6086B8
movq $0x6086b0, %r8          # imm = 0x6086B0
movq 0xf0(%rbx), %rax
incw (%rax,%r13,2)
jmp 0x40d3a5
incb (%rax,%r13)
movq 0xf0(%rbx), %rax
andw $0x0, (%rax,%r13,2)
movq 0x108(%rbx), %rax
andb (%rax,%r13), %bpl
incq %r13
addq $0x18, %r15
jmp 0x40d278
movzwl 0xd2(%rbx), %eax
incl %eax
movw %ax, 0xd2(%rbx)
movq $0x6085e0, %rcx         # imm = 0x6085E0
cmpw (%rcx), %ax
setae %al
testb %bpl, %al
je 0x40d406
movb $0x0, 0xcf(%rbx)
xorl %eax, %eax
movq $0x606151, %rcx         # imm = 0x606151
movzbl (%rcx), %edx
cmpq %rdx, %rax
jae 0x40d406
movq 0xe0(%rbx), %rdx
movb $0x0, (%rdx,%rax)
incq %rax
jmp 0x40d3ee
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x184945(%rip), %rdi    # 0x591d61
leaq 0x184747(%rip), %rsi    # 0x591b6a
leaq 0x184998(%rip), %rcx    # 0x591dc2
movl $0x26b, %edx            # imm = 0x26B
callq 0x4d36d0