// File: /workspace/github_cpp_makefile_star2_O2/MPSLab-ASU[P]DiRAC/./src/controller.cc
// Function: controller::inc_SPM_pass()
void controller::inc_SPM_pass()
{
  // Move to next SPM pass only if PEs are done processing previous data
  // i.e., execution in all RF passes of an SPM pass.
  if ((current_RF_pass == RF_passes + WB_delay_RF_pass) || (current_SPM_pass == 0) || (current_SPM_pass == SPM_passes + 1))
  {
    current_SPM_pass += 1;
    current_operand_prefetch = 0;
    current_operand_writeback = 0;
    current_RF_pass = 0;

    for(uint8_t i=0; i < total_operands; i++)
    {
      bool update_idx_buffer_comm = false;
      bool update_idx_buffer_comp = false;

      // We need to update buffer idx responsible for communication in two cases:
      // 1) DMA is going to prefetch some data in the upcoming pass.
      // 2) DMA will write-back in the upcoming pass.
      // In either cases, need to communicate data between SPM and DRAM
      // via different buffer.
      if(current_SPM_pass < SPM_passes)
        if(read_operand_SPM_pass[current_SPM_pass][i])
          update_idx_buffer_comm = true;

      if((current_SPM_pass < SPM_passes + WB_delay_SPM_pass) && write_operand_SPM_pass[current_SPM_pass][i])
        update_idx_buffer_comm = true;

      if(update_idx_buffer_comm)
      {
        spm_buffer_number_op_prefetch[i] = (spm_buffer_number_op_prefetch[i] + 1) % 2;
        spm_buffer_number_op_writeback[i] = (spm_buffer_number_op_writeback[i] + 1) % 2;
      }

      // We need to update buffer idx responsible for computation in two cases:
      // 1) DMA prefetched some data in previous pass.
      //    Need to compute from different buffer.
      // 2) DMA will write-back in the upcoming pass.
      //    PE-array should write output in the different scratchpad buffer.

      if(current_SPM_pass > 0 && current_SPM_pass < SPM_passes + 1)
      {
        if(read_operand_SPM_pass[current_SPM_pass-1][i])
        {
          update_idx_buffer_comp = true;
        }
      }

      if((current_SPM_pass < SPM_passes + WB_delay_SPM_pass) && write_operand_SPM_pass[current_SPM_pass][i])
        update_idx_buffer_comp = true;

      if(update_idx_buffer_comp)
      {
        spm_buffer_number_op_comp_read[i] = (spm_buffer_number_op_comp_read[i] + 1) % 2;
        spm_buffer_number_op_comp_write[i] = (spm_buffer_number_op_comp_write[i] + 1) % 2;
      }
    }

  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq $0x608738, %rcx         # imm = 0x608738
movzwl (%rcx), %eax
movq $0x608552, %rdx         # imm = 0x608552
movzwl (%rdx), %edx
addl $0x2, %edx
cmpl %eax, %edx
sete %sil
movq $0x60873a, %rax         # imm = 0x60873A
movzwl (%rax), %edx
movzwl %dx, %edx
testw %dx, %dx
sete %r8b
orb %sil, %r8b
jne 0x40c9ae
movq $0x608554, %rsi         # imm = 0x608554
movzwl (%rsi), %esi
incl %esi
cmpl %edx, %esi
jne 0x40caf7
incl %edx
movw %dx, (%rax)
andw $0x0, 0xca(%rdi)
andw $0x0, (%rcx)
xorl %ecx, %ecx
movq $0x608556, %rdx         # imm = 0x608556
movq $0x608554, %rsi         # imm = 0x608554
movq $0x608648, %rdi         # imm = 0x608648
movq $0x608668, %r8          # imm = 0x608668
movl $0x1, %r9d
movq $0x608720, %r11         # imm = 0x608720
movq $0x608728, %rbx         # imm = 0x608728
movq $0x608730, %r14         # imm = 0x608730
movzbl (%rdx), %r15d
cmpq %r15, %rcx
jae 0x40caf7
movzwl (%rax), %r15d
movzwl %r15w, %r13d
movzwl (%rsi), %ebp
cmpw %bp, %r13w
jae 0x40ca23
movq (%rdi), %r12
movq (%r12,%r13,8), %r12
movb (%r12,%rcx), %r12b
jmp 0x40ca26
xorl %r12d, %r12d
movzwl %bp, %r10d
addl $0x2, %r10d
cmpl %r13d, %r10d
jbe 0x40ca4b
movq (%r8), %r10
movq (%r10,%r13,8), %r10
cmpb $0x0, (%r10,%rcx)
movl %r9d, %r13d
jne 0x40ca48
movzbl %r12b, %r13d
movl %r13d, %r12d
testb %r12b, %r12b
je 0x40ca82
movq $0x608718, %r10         # imm = 0x608718
movq (%r10), %r10
movb (%r10,%rcx), %bpl
notb %bpl
andb $0x1, %bpl
movb %bpl, (%r10,%rcx)
movq (%r11), %r10
movb (%r10,%rcx), %bpl
notb %bpl
andb $0x1, %bpl
movb %bpl, (%r10,%rcx)
movzwl (%rax), %r15d
movzwl (%rsi), %ebp
movzwl %r15w, %r12d
leal -0x1(%r15), %r10d
cmpw %bp, %r10w
jae 0x40caa2
movq (%rdi), %r10
leal -0x1(%r12), %r15d
movq (%r10,%r15,8), %r10
movb (%r10,%rcx), %r15b
jmp 0x40caa5
xorl %r15d, %r15d
movzwl %bp, %r10d
addl $0x2, %r10d
cmpl %r12d, %r10d
jbe 0x40cac6
movq (%r8), %r10
movq (%r10,%r12,8), %r10
movzbl %r15b, %r15d
cmpb $0x0, (%r10,%rcx)
cmovnel %r9d, %r15d
testb %r15b, %r15b
je 0x40caef
movq (%rbx), %r10
movb (%r10,%rcx), %bpl
notb %bpl
andb $0x1, %bpl
movb %bpl, (%r10,%rcx)
movq (%r14), %r10
movb (%r10,%rcx), %bpl
notb %bpl
andb $0x1, %bpl
movb %bpl, (%r10,%rcx)
incq %rcx
jmp 0x40c9f8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq