// File: /workspace/github_c_makefile_star2_O2/hamsternz[P]emulate-risc-v/riscv.c
// Function: op_unified
static int op_unified(void) {
  uint32_t op1, op2, res, csr_res; 
  uint32_t pc_next_i, pc_cond_jump, pc_rel_jump, pc_indirect; 

  /*******************************************************
   * Build local variables based on global state 
   ******************************************************/
  /* Options for next PC value */
  pc_next_i    = pc + 4;
  pc_cond_jump = pc + broffset;
  pc_rel_jump  = pc + jmpoffset;
  pc_indirect  = (regs[rs1] + imm12) & (~1);

  /* Operands */
  op1 = regs[rs1];
  op2 = op->op2_immediate ? imm12 : regs[rs2];

  /* Find the results */
  switch(op->alu_mode) {
    case ALU_ADD:    res = op1 + op2;                                               break;
    case ALU_SUB:    res = op1 - op2;                                               break;
    case ALU_SLL:    res = op1 << (op2 & 0x1f);                                     break;
    case ALU_XOR:    res = op1 ^ op2;                                               break;
    case ALU_SRL:    res = (uint32_t)op1 >> (op2 & 0x1f);                           break;
    case ALU_SRA:    res =  (int32_t)op1 >> (op2 & 0x1f);                           break;
    case ALU_OR:     res = op1 | op2;                                               break;
    case ALU_AND:    res = op1 & op2;                                               break;
  
    case ALU_SEQ:    res = (op1 == op2)                     ? 1 : 0;                break;
    case ALU_SLT:    res = ((int32_t)  op1 <  (int32_t)op2) ? 1 : 0;                break;
    case ALU_SLTU:   res = ((uint32_t) op1 < (uint32_t)op2) ? 1 : 0;                break;

    case ALU_MUL:    res = ((uint64_t)op1 * (uint64_t)op2);                         break;
    case ALU_MULH:   res = ( (int64_t)op1 *  (int64_t)op2) >> 32;                   break;
    case ALU_MULHSU: res = ( (int64_t)op1 * (uint64_t)op2) >> 32;                   break;
    case ALU_MULHU:  res = ((uint64_t)op1 * (uint64_t)op2) >> 32;                   break;
    case ALU_DIV:    res = (op2 == 0) ? 0xFFFFFFFF : (int32_t)op1 / (int32_t)op2;   break;
    case ALU_DIVU:   res = (op2 == 0) ? 0xFFFFFFFF : (uint32_t)op1 / (uint32_t)op2; break;
    case ALU_REM:    res = (op2 == 0) ? 0xFFFFFFFF : (int32_t)op1 % (int32_t)op2;   break;
    case ALU_REMU:   res = (op2 == 0) ? 0xFFFFFFFF : (uint32_t)op1 % (uint32_t)op2; break;

    // Maybe seperate
    case ALU_NEXT_I: res = pc_next_i;                                             break;
    case ALU_PC_U20: res = pc + upper20;                                          break;
    case ALU_U20:    res = upper20;                                               break;
    case ALU_CSR:    res = csr[csrid];                                            break;
    default:         res = 0;                                                     break; 
  }

  switch(op->csr_mode) {
    case CSR_RW:  csr_res = regs[rs1];               break;
    case CSR_RS:  csr_res = csr[csrid] | regs[rs1];  break;
    case CSR_RC:  csr_res = csr[csrid] & ~regs[rs1]; break;
    case CSR_RWI: csr_res = uimm;                    break;
    case CSR_RSI: csr_res = csr[csrid] | uimm;       break;
    case CSR_RCI: csr_res = csr[csrid] & ~uimm;      break;
    default:      csr_res = 0;                       break;
  }

  if(op->csr_mode != CSR_NOP) { 
    char buffer[100];
    sprintf(buffer,"CSR 0x%03x accessed",csrid);
    display_log(buffer);
  }

  /* And now do the write */
  if(op->memory_mode == MEM_STORE) {
    if(memory_write_full()) {
      stalled = 1;
    } else {
      uint32_t addr;
      int unaligned = 0;
      addr = regs[rs1]+imm12wr;
      stalled = 0;

      switch(addr & 3) {
        case 1:
          if(op->memory_mask  == 0xFFFFFFFF) {
            unaligned = 1;
          }
          break;
        case 2:
          if(op->memory_mask  == 0xFFFFFFFF) {
            unaligned = 1;
          }
          break;
        case 3:
          if(op->memory_mask  != 0xFF) {
            unaligned = 1;
          }
          break;
      }
      if(unaligned) {
        char buffer[100];
        sprintf(buffer,"Unaligned write at %08x %08x",addr, op->memory_mask);
        display_log(buffer);
      }

      if(!memory_write_request(addr, op->memory_mask, regs[rs2])) {
        return 0;
      }
    }
  }
  
  /* do we need to do a load? */
  if(op->memory_mode == MEM_LOAD) {
    if(rd != 0) {
      if(!read_dispatched) {
        uint32_t addr;
        int unaligned = 0;
        addr = regs[rs1]+imm12;
        stalled = 1;

        switch(addr & 3) {
          case 1:
            if(op->memory_mask  == 0xFFFFFFFF) {
              unaligned = 1;
            }
            break;
          case 2:
            if(op->memory_mask  == 0xFFFFFFFF) {
              unaligned = 1;
            }
            break;
          case 3:
            if(op->memory_mask  != 0xFF) {
              unaligned = 1;
            }
            break;
        }
        if(unaligned) {
          char buffer[100];
          sprintf(buffer,"Unaligned read at %08x %08x",addr, op->memory_mask);
          display_log(buffer);
        }

        if(memory_read_request(regs[rs1]+imm12)) {
          read_dispatched = 1;
        } 
        /* Unable to queue request -  will retry */
      } else {
        /* To get here we are stalled waiting for data */
        if(!memory_read_data_empty())  {
          stalled = 0;
          res = memory_read_data() & op->memory_mask;
          /* Sign extend */
          res |= (res & op->load_sign_check) ? ~op->memory_mask : 0;
        }
      }
    }
  }

  /******************************************************************
   * All assignments from local to global state variables below here
   *****************************************************************/

  /* Store the results? */
  if(!stalled) {
    if(op->store_result && rd != 0)
      regs[rd] = res;

    /* Any CSR updates? */
    switch(op->csr_mode) {
      case CSR_RW:  if(rs1 != 0) csr[csrid] = csr_res;  break;
      case CSR_RS:  if(rs1 != 0) csr[csrid] = csr_res;  break;
      case CSR_RC:  if(rs1 != 0) csr[csrid] = csr_res;  break;
      case CSR_RWI: csr[csrid] = csr_res;               break;
      case CSR_RSI: csr[csrid] = csr_res;               break;
      case CSR_RCI: csr[csrid] = csr_res;               break;
      default:                                          break;
    }

    /* Which instruction next? */
    switch(op->pc_mode) {
      case PC_NEXT_I:        pc = pc_next_i;                      break;
      case PC_COND_JUMP:     pc = res ? pc_cond_jump : pc_next_i; break;
      case PC_COND_JUMP_INV: pc = res ? pc_next_i : pc_cond_jump; break;
      case PC_REL_JUMP:      pc = pc_rel_jump;                    break;
      case PC_INDIRECT:      pc = pc_indirect;                    break;
      default:                                                    break;
    }
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl 0x992d(%rip), %eax      # 0xca00
movzbl 0xd9ea(%rip), %edx      # 0x10ac4
leaq 0xd93f(%rip), %rdi      # 0x10a20
movl 0xd9e1(%rip), %ecx      # 0x10ac8
movq 0xd9ba(%rip), %rsi      # 0x10aa8
cmpb $0x0, 0x10(%rsi)
movl %ecx, 0xc(%rsp)
jne 0x3102
movzbl 0xd9cd(%rip), %ecx      # 0x10acc
movl (%rdi,%rcx,4), %ecx
movl (%rdi,%rdx,4), %ebx
leal 0x4(%rax), %ebp
movl 0xd9ad(%rip), %r12d     # 0x10abc
addl %eax, %r12d
movl 0xd9a7(%rip), %r13d     # 0x10ac0
addl %eax, %r13d
movzbl 0x11(%rsi), %edx
cmpq $0x16, %rdx
movl %ebp, 0x8(%rsp)
ja 0x3559
pushq $-0x1
popq %rdi
leaq 0x3f74(%rip), %r8       # 0x70ac
movslq (%r8,%rdx,4), %r9
addq %r8, %r9
movl %ecx, %edx
jmpq *%r9
imulq %rbx, %rdx
shrq $0x20, %rdx
movl %edx, %ebp
jmp 0x31a7
imull %ebx, %ecx
jmp 0x31a5
andl %ebx, %ecx
jmp 0x31a5
xorl %ebp, %ebp
cmpl %ecx, %ebx
sete %bpl
jmp 0x31a7
addl 0xd94b(%rip), %eax      # 0x10ab4
movl %eax, %ebp
jmp 0x31a7
movl %ebx, %ebp
sarl %cl, %ebp
jmp 0x31a7
movl %ebx, %ebp
subl %ecx, %ebp
jmp 0x31a7
movl %ebx, %ebp
shll %cl, %ebp
jmp 0x31a7
xorl %ebx, %ecx
jmp 0x31a5
addl %ebx, %ecx
jmp 0x31a5
xorl %ebp, %ebp
cmpl %ecx, %ebx
setl %bpl
jmp 0x31a7
movl 0xd91d(%rip), %ebp      # 0x10ab4
jmp 0x31a7
xorl %ebp, %ebp
cmpl %ecx, %ebx
setb %bpl
jmp 0x31a7
orl %ebx, %ecx
movl %ecx, %ebp
movzbl 0x14(%rsi), %eax
leal -0x1(%rax), %ecx
cmpl $0x5, %ecx
ja 0x322e
leaq 0x3f4e(%rip), %rax      # 0x7108
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
movl %ebx, %r15d
jmpq *%rcx
movl 0xd904(%rip), %eax      # 0x10ad0
leaq 0x983d(%rip), %rcx      # 0xca10
movl (%rcx,%rax,4), %r15d
orl %ebx, %r15d
jmp 0x3235
movl 0xd8ee(%rip), %eax      # 0x10ad0
leaq 0x9827(%rip), %rcx      # 0xca10
movl 0xd8e4(%rip), %r15d     # 0x10ad4
orl (%rcx,%rax,4), %r15d
jmp 0x3235
movl 0xd8d4(%rip), %eax      # 0x10ad0
leaq 0x980d(%rip), %rcx      # 0xca10
movl %ebx, %r15d
jmp 0x3225
movl 0xd8c5(%rip), %r15d     # 0x10ad4
jmp 0x3235
movl 0xd8b9(%rip), %eax      # 0x10ad0
leaq 0x97f2(%rip), %rcx      # 0xca10
movl 0xd8af(%rip), %r15d     # 0x10ad4
notl %r15d
andl (%rcx,%rax,4), %r15d
jmp 0x3235
xorl %r15d, %r15d
testb %al, %al
je 0x3260
movl 0xd895(%rip), %edx      # 0x10ad0
leaq 0x46b8(%rip), %rsi      # 0x78fa
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x21f0
movq %r14, %rdi
callq 0x4b7e
movq 0xd848(%rip), %rsi      # 0x10aa8
cmpb $0x2, 0x15(%rsi)
jne 0x3324
callq 0x5ec4
testl %eax, %eax
je 0x327f
movb $0x1, 0xd83e(%rip)      # 0x10ab8
jmp 0x3324
movzbl 0xd83e(%rip), %eax      # 0x10ac4
movl 0xd84b(%rip), %r14d     # 0x10ad8
leaq 0xd78c(%rip), %rdx      # 0x10a20
addl (%rdx,%rax,4), %r14d
movb $0x0, 0xd819(%rip)      # 0x10ab8
movl %r14d, %eax
andl $0x3, %eax
leaq 0x3e74(%rip), %rcx      # 0x7120
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xd7ec(%rip), %rax      # 0x10aa8
cmpl $-0x1, 0x18(%rax)
jne 0x3300
pushq $-0x1
popq %rcx
jmp 0x32d9
movq 0xd7da(%rip), %rax      # 0x10aa8
movl 0x18(%rax), %ecx
cmpl $0xff, %ecx
je 0x3300
leaq 0x462e(%rip), %rsi      # 0x790e
leaq 0x10(%rsp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x21f0
leaq 0x10(%rsp), %rdi
callq 0x4b7e
leaq 0xd720(%rip), %rdx      # 0x10a20
movq 0xd7a1(%rip), %rax      # 0x10aa8
movl 0x18(%rax), %esi
movzbl 0xd7bb(%rip), %eax      # 0x10acc
movl (%rdx,%rax,4), %edx
movl %r14d, %edi
callq 0x5ed1
testl %eax, %eax
je 0x33bf
movq 0xd77d(%rip), %rax      # 0x10aa8
cmpb $0x1, 0x15(%rax)
jne 0x3418
cmpb $0x0, 0xd774(%rip)      # 0x10ab0
je 0x3418
cmpb $0x0, 0xd793(%rip)      # 0x10adc
je 0x3381
callq 0x5e16
testl %eax, %eax
jne 0x3418
movb $0x0, 0xd759(%rip)      # 0x10ab8
callq 0x5e30
movq 0xd73d(%rip), %rcx      # 0x10aa8
movl 0x18(%rcx), %ebp
andl %ebp, %eax
movl 0x1c(%rcx), %ecx
andl %eax, %ecx
notl %ebp
cmovel %ecx, %ebp
orl %eax, %ebp
jmp 0x3418
movzbl 0xd73c(%rip), %ecx      # 0x10ac4
movl 0xd73a(%rip), %edx      # 0x10ac8
leaq 0xd68b(%rip), %rsi      # 0x10a20
addl (%rsi,%rcx,4), %edx
movb $0x1, 0xd719(%rip)      # 0x10ab8
movl %edx, %ecx
andl $0x3, %ecx
leaq 0x3d85(%rip), %rsi      # 0x7130
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
cmpl $-0x1, 0x18(%rax)
jne 0x3406
pushq $-0x1
popq %rcx
jmp 0x33d1
xorl %eax, %eax
jmp 0x34dc
movl 0x18(%rax), %ecx
cmpl $0xff, %ecx
je 0x3406
leaq 0x4553(%rip), %rsi      # 0x792b
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x21f0
movq %r14, %rdi
callq 0x4b7e
movzbl 0xd6ce(%rip), %eax      # 0x10ac4
movl 0xd6cc(%rip), %edx      # 0x10ac8
leaq 0xd61d(%rip), %rcx      # 0x10a20
addl (%rcx,%rax,4), %edx
movl %edx, %edi
callq 0x5d98
testl %eax, %eax
je 0x3418
movb $0x1, 0xd6c4(%rip)      # 0x10adc
pushq $0x1
popq %rax
cmpb $0x0, 0xd696(%rip)      # 0x10ab8
jne 0x34dc
movq 0xd679(%rip), %rcx      # 0x10aa8
cmpb $0x0, 0x12(%rcx)
setne %sil
movb 0xd673(%rip), %dl       # 0x10ab0
testb %dl, %dl
setne %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x3459
movzbl %dl, %edx
leaq 0xd5ca(%rip), %rsi      # 0x10a20
movl %ebp, (%rsi,%rdx,4)
movzbl 0x14(%rcx), %edx
decl %edx
cmpl $0x5, %edx
ja 0x348e
leaq 0x3cd5(%rip), %rsi      # 0x7140
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpb $0x0, 0xd649(%rip)      # 0x10ac4
je 0x348e
movl 0xd64d(%rip), %edx      # 0x10ad0
leaq 0x9586(%rip), %rsi      # 0xca10
movl %r15d, (%rsi,%rdx,4)
movzbl 0x13(%rcx), %ecx
cmpq $0x4, %rcx
ja 0x34dc
leaq 0x3cb9(%rip), %rdx      # 0x7158
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl 0x8(%rsp), %r13d
jmp 0x34d5
movl 0xc(%rsp), %r13d
addl %ebx, %r13d
andl $-0x2, %r13d
jmp 0x34d5
testl %ebp, %ebp
cmovel 0x8(%rsp), %r12d
movl %r12d, %r13d
jmp 0x34d5
testl %ebp, %ebp
movl 0x8(%rsp), %r13d
cmovel %r12d, %r13d
movl %r13d, 0x9524(%rip)     # 0xca00
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %ebp
shrl %cl, %ebp
jmp 0x31a7
movl %edi, %ebp
testl %ecx, %ecx
je 0x31a7
movl %ebx, %eax
cltd
idivl %ecx
jmp 0x3515
movl %edi, %ebp
testl %ecx, %ecx
je 0x31a7
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movl %edx, %ebp
jmp 0x31a7
movl %edi, %ebp
testl %ecx, %ecx
je 0x31a7
movl %ebx, %eax
cltd
idivl %ecx
jmp 0x3552
movl 0xd59d(%rip), %eax      # 0x10ad0
leaq 0x94d6(%rip), %rcx      # 0xca10
movl (%rcx,%rax,4), %ebp
jmp 0x31a7
movl %edi, %ebp
testl %ecx, %ecx
je 0x31a7
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ebp
jmp 0x31a7
xorl %ebp, %ebp
jmp 0x31a7