-
Notifications
You must be signed in to change notification settings - Fork 14
/
ExecuteA64.fs
210 lines (186 loc) · 7.87 KB
/
ExecuteA64.fs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module ISA.RISCV.Execute.A64
open ISA.RISCV.Arch
open ISA.RISCV.Decode.A64
open ISA.RISCV.MachineState
open ISA.RISCV.Utils.Bits
//=================================================
// LR.D - Load-Reserved Double Word operation
// This acts just like a lw in this implementation (no need for sync)
// (except there's no immediate)
let execLR_D (rd : Register) (rs1 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// SC.D - Store Conditional Double Word
// This acts just like a sd in this implementation, but it will
// always set the check register to 0 (indicating load success)
let execSC_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let resMemOp = mstate.getRegister rs2
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd 0L
mstate.incPC
//=================================================
// AMOSWAP.D - AMO Swap Double word
let execAMOSWAP_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp = rs2Val
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOADD.D - AMO Add Double Word
let execAMOADD_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp = (int64 memResult.Value) + rs2Val
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOXOR.D - AMO Xor Double Word
let execAMOXOR_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp = (int64 memResult.Value) ^^^ rs2Val
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOAND.D - AMO And Double Word
let execAMOAND_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp = (int64 memResult.Value) &&& rs2Val
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOOR.D - AMO Or Double Word
let execAMOOR_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp = (int64 memResult.Value) ||| rs2Val
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOMIN.D - AMO Min Double Word
let execAMOMIN_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp =
if (int64 memResult.Value) > rs2Val then
rs2Val
else
int64 memResult.Value
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOMAX.D - AMO Max Double Word
let execAMOMAX_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp =
if (int64 memResult.Value) < rs2Val then
rs2Val
else
int64 memResult.Value
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOMINU.D - AMO Unsigned Min Double Word
let execAMOMINU_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp =
if (uint64 memResult.Value) > (uint64 rs2Val) then
rs2Val
else
int64 memResult.Value
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// AMOMAXU.D - AMO Unsigned Max Double Word
let execAMOMAXU_D (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let addr = mstate.getRegister rs1
let rs2Val = mstate.getRegister rs2
let memResult = loadDouble mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let resMemOp =
if (uint64 memResult.Value) < (uint64 rs2Val) then
rs2Val
else
int64 memResult.Value
let mstate = mstate.storeMemoryDoubleWord addr resMemOp
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
// Execute A64-instructions
let Execute (instr : InstructionA64) (mstate : MachineState) =
match instr with
| LR_D i ->
execLR_D i.rd i.rs1 mstate
| SC_D i ->
execSC_D i.rd i.rs1 i.rs2 mstate
| AMOSWAP_D i ->
execAMOSWAP_D i.rd i.rs1 i.rs2 mstate
| AMOADD_D i ->
execAMOADD_D i.rd i.rs1 i.rs2 mstate
| AMOXOR_D i ->
execAMOXOR_D i.rd i.rs1 i.rs2 mstate
| AMOAND_D i ->
execAMOAND_D i.rd i.rs1 i.rs2 mstate
| AMOOR_D i ->
execAMOOR_D i.rd i.rs1 i.rs2 mstate
| AMOMIN_D i ->
execAMOMIN_D i.rd i.rs1 i.rs2 mstate
| AMOMAX_D i ->
execAMOMAX_D i.rd i.rs1 i.rs2 mstate
| AMOMINU_D i ->
execAMOMINU_D i.rd i.rs1 i.rs2 mstate
| AMOMAXU_D i ->
execAMOMAXU_D i.rd i.rs1 i.rs2 mstate
| _ -> mstate.setRunState (Trap InstructionExecute)