// File: /workspace/github_cpp_makefile_star2_O2/MPSLab-ASU[P]DiRAC/./src/pe.cc
// Function: PE::determine_next_state()
void PE::determine_next_state()
{
  // Execution events to occur when PE is in execution mode.
  if(PE_State == execute)
  {
    counter_PE_exec += 1;

    // If execution of an RF pass finishes, change current state as next state.
    if (counter_PE_exec == total_cycles_RF_pass)
    {
      PE_State = PE_Next_State;
      if(PE_Next_State == execute)
      {
        update_trigger_states();
        update_reg_idx_Ops();
      }
      else
      {
        state_fetch = false;
      }

      counter_PE_exec = 0;
    }
    // If compute cycles are less than total cycles for an RF pass, stall!
    else if((counter_PE_exec == total_compute_cycles_RF_pass) && (counter_PE_exec < total_cycles_RF_pass))
    {
      state_fetch = false;
    }
  }
  // When PE is idle, check whether it needs to execute.
  else if((PE_Next_State == execute) && (counter_PE_exec == 0))
  {
    update_trigger_states();
    update_reg_idx_Ops();
  }
  // PE waiting for an execution pass to perform write-back.
  else if((PE_WB_next_pass == true) && (counter_PE_exec == 0))
  {
    update_trigger_states();
    update_reg_idx_Ops();
  }
  // PE does not execute on new data, just writeback to interconnect.
  // else if((sig_write_back_current_RF_Pass == true) && (counter_PE_exec == 0))
  // {
  //   // do nothing
  // }

  // Since Write-Back is triggered separately than PE-execution or
  // prefetching data via network, we need to monitor states for write-back
  // continuously and update the states upon the trigger.
  update_trigger_states_write_data();

  // Bits/Word for trigger:
  // i) PE execution (Bit0),
  // ii) Write back (Bit1),
  // iii) Zero-Initialize current RF pass (Bit2)
  // iii) reduction (Bit3).
  PE_Next_State   = (*trigger_from_controller_PE_exec == 1) ? execute : PE_Next_State;
  PE_WB_next_pass = (*trigger_from_controller_write_back == 1) ? true : PE_WB_next_pass;
  PE_zero_initialize_next_pass  = (*trigger_from_controller_zero_initialize == 1) ? true : PE_zero_initialize_next_pass;
  PE_spatial_reduce_next_pass   = (*trigger_from_controller_spatial_reduce == 1) ? true : PE_spatial_reduce_next_pass;
  sig_update_buffer_num_Op1_next_pass = (*trigger_from_controller_update_buf_num_Op1 == 1) ? true : sig_update_buffer_num_Op1_next_pass;
  sig_update_buffer_num_Op2_next_pass = (*trigger_from_controller_update_buf_num_Op2 == 1) ? true : sig_update_buffer_num_Op2_next_pass;
  sig_update_buffer_num_Op3_next_pass = (*trigger_from_controller_update_buf_num_Op3 == 1) ? true : sig_update_buffer_num_Op3_next_pass;
}

// Assembly:
pushq %rbx
movq %rdi, %rbx
cmpl $0x1, 0x8(%rdi)
jne 0x4122d6
movl 0x9c(%rbx), %eax
incl %eax
movw %ax, 0x9c(%rbx)
movq $0x6085e0, %rcx         # imm = 0x6085E0
cmpw (%rcx), %ax
jne 0x412313
movl 0xc(%rbx), %eax
movl %eax, 0x8(%rbx)
cmpl $0x1, %eax
jne 0x41232a
movq %rbx, %rdi
callq 0x41246c
movq %rbx, %rdi
callq 0x41252a
jmp 0x412331
cmpl $0x1, 0xc(%rbx)
sete %cl
movzwl 0x9c(%rbx), %eax
testw %ax, %ax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x412301
testw %ax, %ax
sete %al
andb 0xd8(%rbx), %al
cmpb $0x1, %al
jne 0x412339
movq %rbx, %rdi
callq 0x41246c
movq %rbx, %rdi
callq 0x41252a
jmp 0x412339
jae 0x412339
movq $0x6085e2, %rcx         # imm = 0x6085E2
cmpw (%rcx), %ax
jne 0x412339
movb $0x0, 0x80(%rbx)
jmp 0x412339
movb $0x0, 0x80(%rbx)
andw $0x0, 0x9c(%rbx)
movb 0xd8(%rbx), %cl
cmpb $0x1, %cl
jne 0x412358
movb $0x1, 0x138(%rbx)
movb $0x0, 0xd8(%rbx)
andl $0x0, 0x36(%rbx)
xorl %ecx, %ecx
movq 0xa0(%rbx), %rax
movq 0xa8(%rbx), %rdx
cmpb $0x0, (%rax)
pushq $0x1
popq %rax
movl %eax, %esi
jne 0x412373
movl 0xc(%rbx), %esi
movl %esi, 0xc(%rbx)
movq 0xb0(%rbx), %rsi
movzbl %cl, %ecx
cmpb $0x0, (%rsi)
cmovnel %eax, %ecx
movb %cl, 0xd8(%rbx)
movzbl 0xd9(%rbx), %ecx
cmpb $0x0, (%rdx)
cmovnel %eax, %ecx
movb %cl, 0xd9(%rbx)
movq 0xb8(%rbx), %rcx
movzbl 0xda(%rbx), %edx
cmpb $0x0, (%rcx)
cmovnel %eax, %edx
movb %dl, 0xda(%rbx)
movq 0xc0(%rbx), %rcx
cmpb $0x0, (%rcx)
movzbl 0xdb(%rbx), %ecx
cmovnel %eax, %ecx
movb %cl, 0xdb(%rbx)
movq 0xc8(%rbx), %rcx
cmpb $0x0, (%rcx)
movl %eax, %ecx
jne 0x4123e7
movl 0xdc(%rbx), %ecx
movb %cl, 0xdc(%rbx)
movq 0xd0(%rbx), %rcx
cmpb $0x0, (%rcx)
movzbl 0xdd(%rbx), %ecx
cmovnel %eax, %ecx
movb %cl, 0xdd(%rbx)
popq %rbx
retq
nop