// File: /workspace/github_cpp_makefile_star2_O1/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 0x416235
movl 0x9c(%rbx), %eax
incl %eax
movw %ax, 0x9c(%rbx)
movq $0x60b5e0, %rcx         # imm = 0x60B5E0
cmpw (%rcx), %ax
jne 0x4162e4
movl 0xc(%rbx), %eax
movl %eax, 0x8(%rbx)
cmpl $0x1, %eax
jne 0x4162fb
movq %rbx, %rdi
callq 0x41644a
movzbl 0xde(%rbx), %eax
movzwl 0xf8(%rbx,%rax,2), %eax
movw %ax, 0x26(%rbx)
movzbl 0xdf(%rbx), %eax
movzwl 0xfc(%rbx,%rax,2), %eax
movw %ax, 0x28(%rbx)
movzbl 0xe0(%rbx), %eax
movzwl 0x100(%rbx,%rax,2), %eax
movw %ax, 0x2a(%rbx)
movw %ax, 0x2c(%rbx)
movzbl 0xe1(%rbx), %eax
movzwl 0xf8(%rbx,%rax,2), %eax
movw %ax, 0x2e(%rbx)
movzbl 0xe2(%rbx), %eax
movzwl 0xfc(%rbx,%rax,2), %eax
movw %ax, 0x30(%rbx)
movzbl 0xe3(%rbx), %eax
movzwl 0x100(%rbx,%rax,2), %eax
movw %ax, 0x32(%rbx)
jmp 0x416302
cmpl $0x1, 0xc(%rbx)
sete %cl
movzwl 0x9c(%rbx), %eax
testw %ax, %ax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x416264
testw %ax, %ax
sete %al
andb 0xd8(%rbx), %al
cmpb $0x1, %al
jne 0x41630b
movq %rbx, %rdi
callq 0x41644a
movzbl 0xde(%rbx), %eax
movzwl 0xf8(%rbx,%rax,2), %eax
movw %ax, 0x26(%rbx)
movzbl 0xdf(%rbx), %eax
movzwl 0xfc(%rbx,%rax,2), %eax
movw %ax, 0x28(%rbx)
movzbl 0xe0(%rbx), %eax
movzwl 0x100(%rbx,%rax,2), %eax
movw %ax, 0x2a(%rbx)
movw %ax, 0x2c(%rbx)
movzbl 0xe1(%rbx), %eax
movzwl 0xf8(%rbx,%rax,2), %eax
movw %ax, 0x2e(%rbx)
movzbl 0xe2(%rbx), %eax
movzwl 0xfc(%rbx,%rax,2), %eax
movw %ax, 0x30(%rbx)
movzbl 0xe3(%rbx), %eax
movzwl 0x100(%rbx,%rax,2), %eax
movw %ax, 0x32(%rbx)
jmp 0x41630b
jae 0x41630b
movq $0x60b5e2, %rcx         # imm = 0x60B5E2
cmpw (%rcx), %ax
jne 0x41630b
movb $0x0, 0x80(%rbx)
jmp 0x41630b
movb $0x0, 0x80(%rbx)
movw $0x0, 0x9c(%rbx)
cmpb $0x1, 0xd8(%rbx)
jne 0x416329
movb $0x1, 0x138(%rbx)
movb $0x0, 0xd8(%rbx)
movl $0x0, 0x36(%rbx)
movq 0xa0(%rbx), %rax
cmpb $0x0, (%rax)
movl $0x1, %eax
movl $0x1, %edx
jne 0x416342
movl 0xc(%rbx), %edx
movq 0xa8(%rbx), %rcx
movl %edx, 0xc(%rbx)
movq 0xb0(%rbx), %rdx
cmpb $0x0, (%rdx)
movl $0x1, %edx
jne 0x416363
movl 0xd8(%rbx), %edx
movb %dl, 0xd8(%rbx)
movzbl 0xd9(%rbx), %edx
cmpb $0x0, (%rcx)
cmovnel %eax, %edx
movb %dl, 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 $0x1, %ecx
jne 0x4163c7
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