-
Notifications
You must be signed in to change notification settings - Fork 14
/
ExecuteI.fs
446 lines (402 loc) · 16.7 KB
/
ExecuteI.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
module ISA.RISCV.Execute.I
open ISA.RISCV.Arch
open ISA.RISCV.Decode.I
open ISA.RISCV.MachineState
open ISA.RISCV.Utils.Bits
//=================================================
// LUI - Load Upper immediate
let execLUI (rd : Register) (imm20 : InstrField) (mstate : MachineState) =
let mstate = mstate.setRegister rd (int64 imm20)
mstate.incPC
//=================================================
// AUIPC - Add Upper immediate PC
let execAUIPC (rd : Register) (imm20 : InstrField) (mstate : MachineState) =
let mstate = mstate.setRegister rd ((int64 imm20) + mstate.PC)
mstate.incPC
//=================================================
// JALR - Jump Relative immediately
let execJALR (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let newPC = ((mstate.getRegister rs1) + (int64 imm12)) &&& (~~~1L)
if newPC % 4L <> 0L then
mstate.setRunState (Trap JumpAddress)
else if newPC = mstate.PC then
mstate.setRunState Stopped
else
let newPCstate = mstate.incPC
let mstate = mstate.setRegister rd newPCstate.PC
mstate.setPC newPC
//=================================================
// JAL - Jump immediately
let execJAL (rd : Register) (imm20 : InstrField) (mstate : MachineState) =
let newPC = mstate.PC + int64 imm20
if newPC % 4L <> 0L then
mstate.setRunState (Trap JumpAddress)
else if newPC = mstate.PC then
mstate.setRunState Stopped
else
let newPCstate = mstate.incPC
let mstate = mstate.setRegister rd newPCstate.PC
mstate.setPC newPC
// Basic branch flow
let branch (branchCheck : MachineInt -> MachineInt -> bool) (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
let x1 = mstate.getRegister rs1
let x2 = mstate.getRegister rs2
let newPC = mstate.PC + int64 imm12
if newPC % 4L <> 0L then
mstate.setRunState (Trap BreakAddress)
else if newPC = mstate.PC then
mstate.setRunState Stopped
else
if branchCheck x1 x2 then
mstate.setPC newPC
else
mstate.incPC
//=================================================
// BEQ - Branch if Equal
let execBEQ (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
branch (=) rs1 rs2 imm12 mstate
//=================================================
// BNE - Branch if Not Equal
let execBNE (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
branch (<>) rs1 rs2 imm12 mstate
//=================================================
// BLT - Branch if Less Then
let execBLT (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
branch (<) rs1 rs2 imm12 mstate
//=================================================
// BGE - Branch if Greater or Equal
let execBGE (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
branch (>=) rs1 rs2 imm12 mstate
//=================================================
// BLTU - Branch if Less Then (Unsigned)
let execBLTU (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
let x1 = mstate.getRegister rs1
let x2 = mstate.getRegister rs2
let branchCheck =
match mstate.Arch.archBits with
| RV32 -> uint32 x1 < uint32 x2
| _ -> uint64 x1 < uint64 x2
let newPC = mstate.PC + int64 imm12
if newPC % 4L <> 0L then
mstate.setRunState (Trap BreakAddress)
else if newPC = mstate.PC then
mstate.setRunState Stopped
else
if branchCheck then
mstate.setPC newPC
else
mstate.incPC
//=================================================
// BGEU - Branch If Greater or Equal (Unsigned)
let execBGEU (rs1 : Register) (rs2: Register) (imm12 : InstrField) (mstate : MachineState) =
let x1 = mstate.getRegister rs1
let x2 = mstate.getRegister rs2
let branchCheck =
match mstate.Arch.archBits with
| RV32 -> uint32 x1 >= uint32 x2
| _ -> uint64 x1 >= uint64 x2
let newPC = mstate.PC + int64 imm12
if newPC % 4L <> 0L then
mstate.setRunState (Trap BreakAddress)
else if newPC = mstate.PC then
mstate.setRunState Stopped
else
if branchCheck then
mstate.setPC newPC
else
mstate.incPC
//=================================================
// LB - Load Byte from Memory
let execLB (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let memResult = loadByte mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// LH - Load Half-word (2 bytes) from Memory
let execLH (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let memResult = loadHalfWord mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// LW - Load Word (4 bytes) from Memory
let execLW (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let memResult = loadWord mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let mstate = mstate.setRegister rd (int64 memResult.Value)
mstate.incPC
//=================================================
// LBU - Load Byte Unsigned from Memory
let execLBU (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let memResult = loadByte mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let memVal = uint8 memResult.Value
let mstate = mstate.setRegister rd (int64 memVal)
mstate.incPC
//=================================================
// LHU - Load Half-word (2 bytes) Unsigned from Memory
let execLHU (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let memResult = loadHalfWord mstate.Memory addr
if memResult.IsNone then
mstate.setRunState (Trap (MemAddress addr))
else
let memVal = uint16 memResult.Value
let mstate = mstate.setRegister rd (int64 memVal)
mstate.incPC
//=================================================
// SB - Store Byte to Memory
let execSB (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let rs2Val = mstate.getRegister rs2
let mstate = mstate.storeMemoryByte addr rs2Val
mstate.incPC
//=================================================
// SH - Store 2 Bytes (Hald word) to Memory
let execSH (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let rs2Val = mstate.getRegister rs2
let mstate = mstate.storeMemoryHalfWord addr rs2Val
mstate.incPC
//=================================================
// SW - Store 4 Bytes (Word) to Memory
let execSW (rs1 : Register) (rs2 : Register) (imm12 : InstrField) (mstate : MachineState) =
let addr = (mstate.getRegister rs1) + int64 imm12
let rs2Val = mstate.getRegister rs2
let mstate = mstate.storeMemoryWord addr rs2Val
mstate.incPC
//=================================================
// ADDI - Add immediate
let execADDI (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) + int64 imm12
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SLTI - Set to 1 if Less Then Immediate
let execSLTI (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal = if (mstate.getRegister rs1) < int64 imm12 then 1L else 0L
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SLTIU - Set to 1 if Less Then Unsign Immediate
let execSLTIU (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal =
match mstate.Arch.archBits with
| RV32 -> if uint32(mstate.getRegister rs1) < uint32 imm12 then 1L else 0L
| _ -> if uint64(mstate.getRegister rs1) < uint64 imm12 then 1L else 0L
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// XORI - Xor immediately
let execXORI (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) ^^^ int64 imm12
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// ORI - Or immediately
let execORI (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) ||| int64 imm12
let mstate = mstate.setRegister rd (int64 rdVal)
mstate.incPC
//=================================================
// SLLI - Shift Left Logical Immediate
let execSLLI (rd : Register) (rs1 : Register) (shamt : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) <<< int32 shamt
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SRLI - Shift Right Logical Immediate
let execSRLI (rd : Register) (rs1 : Register) (shamt : InstrField) (mstate : MachineState) =
let rdVal =
match mstate.Arch.archBits with
| RV32 -> int64(uint32(mstate.getRegister rs1) >>> int32 shamt)
| _ -> int64(uint64(mstate.getRegister rs1) >>> int32 shamt)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SRAI - Shift Right Arithmetic Immediate
let execSRAI (rd : Register) (rs1 : Register) (shamt : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) >>> int32 shamt
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// ANDI - And immediately
let execANDI (rd : Register) (rs1 : Register) (imm12 : InstrField) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) &&& int64 imm12
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// ADD - Add operation
let execADD (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) + (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SUB - Sub operation
let execSUB (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) - (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SLL - Shift Logical Left
let execSLL (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) <<< int32(mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SLT - Set 1 if Less Then
let execSLT (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = if mstate.getRegister rs1 < mstate.getRegister rs2 then 1L else 0L
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SLTU - Set to 1 if Less Then Unsign Immediate
let execSLTU (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal =
match mstate.Arch.archBits with
| RV32 -> if uint32(mstate.getRegister rs1) < uint32(mstate.getRegister rs2) then 1L else 0L
| _ -> if uint64(mstate.getRegister rs1) < uint64(mstate.getRegister rs2) then 1L else 0L
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// XOR - Xor operation
let execXOR (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) ^^^ (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SRL - Shift Right Logical
let execSRL (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal =
match mstate.Arch.archBits with
| RV32 -> int64(uint32(mstate.getRegister rs1) >>> int32(mstate.getRegister rs2))
| _ -> int64(uint64(mstate.getRegister rs1) >>> int32(mstate.getRegister rs2))
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// SRA - Shift Right Arithmetic
let execSRA (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) >>> int32 (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// OR - Or operation
let execOR (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) ||| (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// AND - And operation
let execAND (rd : Register) (rs1 : Register) (rs2 : Register) (mstate : MachineState) =
let rdVal = (mstate.getRegister rs1) &&& (mstate.getRegister rs2)
let mstate = mstate.setRegister rd rdVal
mstate.incPC
//=================================================
// Fence - Fence operation (no operations)
let execFENCE (mstate : MachineState) =
mstate.incPC
//=================================================
// ECALL - ECALL operation
let execECALL (mstate : MachineState) =
mstate.setRunState (Trap ECall)
//=================================================
// execEBREAK - EBREAK operation
let execEBREAK (mstate : MachineState) =
mstate.setRunState (Trap EBreak)
// Execute I-instructions
let Execute (instr : InstructionI) (mstate : MachineState) =
match instr with
| LUI i ->
execLUI i.rd i.imm20 mstate
| AUIPC i ->
execAUIPC i.rd i.imm20 mstate
| JALR i ->
execJALR i.rd i.rs1 i.imm12 mstate
| JAL i ->
execJAL i.rd i.imm20 mstate
| BEQ i ->
execBEQ i.rs1 i.rs2 i.imm12 mstate
| BNE i ->
execBNE i.rs1 i.rs2 i.imm12 mstate
| BLT i ->
execBLT i.rs1 i.rs2 i.imm12 mstate
| BGE i ->
execBGE i.rs1 i.rs2 i.imm12 mstate
| BLTU i ->
execBLTU i.rs1 i.rs2 i.imm12 mstate
| BGEU i ->
execBGEU i.rs1 i.rs2 i.imm12 mstate
| LB i ->
execLB i.rd i.rs1 i.imm12 mstate
| LH i ->
execLH i.rd i.rs1 i.imm12 mstate
| LW i ->
execLW i.rd i.rs1 i.imm12 mstate
| LBU i ->
execLBU i.rd i.rs1 i.imm12 mstate
| LHU i ->
execLHU i.rd i.rs1 i.imm12 mstate
| SB i ->
execSB i.rs1 i.rs2 i.imm12 mstate
| SH i ->
execSH i.rs1 i.rs2 i.imm12 mstate
| SW i ->
execSW i.rs1 i.rs2 i.imm12 mstate
| ADDI i ->
execADDI i.rd i.rs1 i.imm12 mstate
| SLTI i ->
execSLTI i.rd i.rs1 i.imm12 mstate
| SLTIU i ->
execSLTIU i.rd i.rs1 i.imm12 mstate
| XORI i ->
execXORI i.rd i.rs1 i.imm12 mstate
| ORI i ->
execORI i.rd i.rs1 i.imm12 mstate
| ANDI i ->
execANDI i.rd i.rs1 i.imm12 mstate
| SLLI i ->
execSLLI i.rd i.rs1 i.shamt mstate
| SRLI i ->
execSRLI i.rd i.rs1 i.shamt mstate
| SRAI i ->
execSRAI i.rd i.rs1 i.shamt mstate
| ADD i ->
execADD i.rd i.rs1 i.rs2 mstate
| SUB i ->
execSUB i.rd i.rs1 i.rs2 mstate
| SLL i ->
execSLL i.rd i.rs1 i.rs2 mstate
| SLT i ->
execSLT i.rd i.rs1 i.rs2 mstate
| SLTU i ->
execSLTU i.rd i.rs1 i.rs2 mstate
| XOR i ->
execXOR i.rd i.rs1 i.rs2 mstate
| SRL i ->
execSRL i.rd i.rs1 i.rs2 mstate
| SRA i ->
execSRA i.rd i.rs1 i.rs2 mstate
| OR i ->
execOR i.rd i.rs1 i.rs2 mstate
| AND i ->
execAND i.rd i.rs1 i.rs2 mstate
| FENCE _ ->
execFENCE mstate
| ECALL ->
execECALL mstate
| EBREAK ->
execEBREAK mstate
| _ -> mstate.setRunState (Trap InstructionExecute)