-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcpu_m.c
97 lines (85 loc) · 1.94 KB
/
cpu_m.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "cpu.h"
#include "cpu_m.h"
#include "debug.h"
static inline int32_t mul32(int32_t a, int32_t b)
{
return (int32_t)(a * b);
}
static inline uint32_t mulh32(int32_t a, int32_t b)
{
return ((int64_t)a * (int64_t)b) >> 32;
}
static inline uint32_t mulhsu32(int32_t a, uint32_t b)
{
return ((int64_t)a * (int64_t)b) >> 32;
}
static inline uint32_t mulhu32(uint32_t a, uint32_t b)
{
return ((int64_t)a * (int64_t)b) >> 32;
}
static inline int32_t div32(int32_t a, int32_t b)
{
if (b == 0) {
return -1;
} else if (a == ((int32_t)1 << (XLEN - 1)) && b == -1) {
return a;
} else {
return a / b;
}
}
static inline uint32_t divu32(uint32_t a, uint32_t b)
{
if (b == 0) {
return -1;
} else {
return a / b;
}
}
static inline int32_t rem32(int32_t a, int32_t b)
{
if (b == 0) {
return a;
} else if (a == ((int32_t)1 << (XLEN - 1)) && b == -1) {
return 0;
} else {
return a % b;
}
}
static inline uint32_t remu32(uint32_t a, uint32_t b)
{
if (b == 0) {
return a;
} else {
return a % b;
}
}
#define M_OP(NAME, OP) { \
PRINT_DEBUG(NAME" x%d,x%d,x%d\n", GET_RD(*instruction), GET_RS1(*instruction), GET_RS2(*instruction)); \
uint32_t a = get_rs1_value(state, instruction); \
uint32_t b = get_rs2_value(state, instruction); \
set_rd_value(state, instruction, OP(a, b)); \
}
void mul(State* state, word* instruction) {
M_OP("mul", mul32);
}
void mulh(State* state, word* instruction) {
M_OP("mulh", mulh32);
}
void mulhu(State* state, word* instruction) {
M_OP("mulhu", mulhu32);
}
void mulhsu(State* state, word* instruction) {
M_OP("mulhsu", mulhsu32);
}
void rem(State* state, word* instruction) {
M_OP("rem", rem32);
}
void remu(State* state, word* instruction) {
M_OP("remu", remu32);
}
void op_div(State* state, word* instruction) {
M_OP("div", div32);
}
void divu(State* state, word* instruction) {
M_OP("divu", divu32);
}