aboutsummaryrefslogtreecommitdiff
path: root/pk/int.c
blob: 1424e73678c9365a524c627fb11014e400182bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "pk.h"
#include "pcr.h"


#include "softint.h"
#include "riscv-opc.h"
#include <stdint.h>

#define noisy 0


int emulate_int(trapframe_t* tf)
{
  if(noisy)
    printk("Int emulation at pc %lx, insn %x\n",tf->epc,(uint32_t)tf->insn);

  #define RS1 ((tf->insn >> 22) & 0x1F)
  #define RS2 ((tf->insn >> 17) & 0x1F)
  #define RD  ((tf->insn >> 27) & 0x1F)

//  #define XRS1 (tf->gpr[RS1])
//  #define XRS2 (tf->gpr[RS2])
  #define XRD  (tf->gpr[RD])

  unsigned long xrs1 = tf->gpr[RS1];
  unsigned long xrs2 = tf->gpr[RS2];

  #define IS_INSN(x) ((tf->insn & MASK_ ## x) == MATCH_ ## x)
   
  if(IS_INSN(DIV))
  {
    if(noisy)
      printk("emulating div\n");
    XRD = softint_divu(xrs1, xrs2);
  }
  else if(IS_INSN(DIVU))
  {
    if(noisy)
      printk("emulating divu\n");
    XRD = softint_divu( xrs1, xrs2);
  }
  else if(IS_INSN(MUL))
  {
    if(noisy)
      printk("emulating mul\n");
    XRD = softint_mul(xrs1, xrs2);
  }
  else if(IS_INSN(REM))
  {
    if(noisy)
      printk("emulating rem\n");
    XRD = softint_remu(xrs1, xrs2);
  }
  else if(IS_INSN(REMU))
  {
    if(noisy)
      printk("emulating remu\n");
    XRD = softint_remu(xrs1, xrs2);
  }
  else
    return -1;

  return 0;
}