// File: MPSLab-ASU[P]DiRAC/./src/controller.cc
// Function: controller::configure_interconnect()
void controller::configure_interconnect()
{
  for(uint8_t i=0; i < totalNetworks; i++)
  {
    configNetwork[i] = 1;
    // 4 is total elements in configArr packet
    uint32_t idxRow = cnt_data_distribution[i] / network_controller_config_params;
    uint16_t idxCol = cnt_data_distribution[i] % network_controller_config_params;
    if(cnt_data_distribution[i] >= nRows_PE_Array*network_controller_config_params)
      forwardDataToNetwork[i] = 1;
    else
      forwardDataToNetwork[i] = 0;

    rowTagDataToNetwork[i] = configArr[i][idxRow][idxCol].getRowTag();
    colTagDataToNetwork[i] = configArr[i][idxRow][idxCol].getColTag();
    dataOutToNetwork[i]    = configArr[i][idxRow][idxCol].getData();

    cnt_data_distribution[i]++;

    // all networks are configured simultanously
    // so controller does not need to sync-up or wait for a specific network
    if(cnt_data_distribution[i] == total_MC*network_controller_config_params)
    {
      cnt_data_distribution[i] = 0;
      acc_controller_state = exec_kernel;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movb $0x0, -0x9(%rbp)
movzbl -0x9(%rbp), %eax
movq $0x61c153, %rcx         # imm = 0x61C153
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jge 0x417889
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rcx
movzbl -0x9(%rbp), %edx
movb $0x1, (%rcx,%rdx)
movq 0x80(%rax), %rax
movzbl -0x9(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, -0x10(%rbp)
movq 0x80(%rax), %rax
movzbl -0x9(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movq -0x20(%rbp), %rax
movw %dx, %cx
movw %cx, -0x12(%rbp)
movq 0x80(%rax), %rax
movzbl -0x9(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movq $0x61e5ac, %rcx         # imm = 0x61E5AC
movzwl (%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jl 0x417762
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movzbl -0x9(%rbp), %ecx
movb $0x1, (%rax,%rcx)
jmp 0x417772
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movzbl -0x9(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movq $0x61e600, %rax         # imm = 0x61E600
movq (%rax), %rax
movzbl -0x9(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movzwl -0x12(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x418990
movb %al, %dl
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movzbl -0x9(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq $0x61e600, %rax         # imm = 0x61E600
movq (%rax), %rax
movzbl -0x9(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movzwl -0x12(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x4189a0
movb %al, %dl
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movzbl -0x9(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq $0x61e600, %rax         # imm = 0x61E600
movq (%rax), %rax
movzbl -0x9(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movzwl -0x12(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x4189c0
movw %ax, %si
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
movzbl -0x9(%rbp), %edx
movw %si, (%rcx,%rdx,2)
movq 0x80(%rax), %rcx
movzbl -0x9(%rbp), %edx
movw (%rcx,%rdx,2), %si
addw $0x1, %si
movw %si, (%rcx,%rdx,2)
movq 0x80(%rax), %rax
movzbl -0x9(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movq $0x61e610, %rcx         # imm = 0x61E610
movl (%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jne 0x41787a
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movzbl -0x9(%rbp), %ecx
movw $0x0, (%rax,%rcx,2)
movq $0x61e620, %rax         # imm = 0x61E620
movl $0x2, (%rax)
jmp 0x41787c
movb -0x9(%rbp), %al
addb $0x1, %al
movb %al, -0x9(%rbp)
jmp 0x4176c8
addq $0x20, %rsp
popq %rbp
retq
nop