-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
422 lines (400 loc) · 9.71 KB
/
main.go
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
// This is a small simulator of the risc-v processor.
// It is going to simulate the same way as the venus simulator by kvakil
// (https://github.com/kvakil/venus).
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/tabwriter"
)
// readBinary reads binary file in little endian format and returns the content
// in slice of instructions. If there is an error, it will be of type ErrUnexpectedEOF.
func readBinary(name string) (instructions []uint32, err error) {
b, err := ioutil.ReadFile(name)
if err != nil {
return nil, fmt.Errorf("could not open file: %v", err)
}
var data uint32
buf := bytes.NewReader(b)
for {
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
if err != io.EOF {
return nil, fmt.Errorf("could not decode binary data: %v", err)
}
break
}
instructions = append(instructions, data)
}
return instructions, nil
}
// writeBinary writes registers out in binary format to named file.
// If there is an error, it is either a file creation or binary writing failure.
func writeBinary(name string, reg []uint32) error {
f, err := os.Create(name)
if err != nil {
return fmt.Errorf("could not create file: %v", err)
}
defer f.Close()
err = binary.Write(f, binary.LittleEndian, reg)
if err != nil {
return fmt.Errorf("could not write content in binary: %v", err)
}
return nil
}
const header = "PC\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\tx%v\t\n"
const body = "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t\n"
func conv(a []uint32) []interface{} {
s := make([]interface{}, len(a))
for i, v := range a {
s[i] = v
}
return s
}
// gen generates a slice from 0 to 31.
func gen() []interface{} {
v := make([]interface{}, 32)
for i := 0; i < 32; i++ {
v[i] = i
}
return v
}
// sext sign extend a imm value.
func sext(imm uint32) uint32 {
if imm>>20 == 1 {
imm = imm | 0xfff00000
} else if imm>>11 == 1 {
imm = imm | 0xfffff000
}
return imm
}
// execute decode and executes the instruction and store the results into the
// registers. It will return whether a branch instruction is taken with an
// offset.
func execute(pc, instr uint32, reg []uint32, mem []byte) (offset int, branching, exit bool) {
opcode := instr & 0x7f
switch opcode {
case 0x3:
rd := (instr >> 7) & 0x1f
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f
imm := sext((instr >> 20))
sp := reg[rs1]
switch funct3 {
case 0: // LB
reg[rd] = uint32(int8(mem[sp+imm]))
case 1: // LH
res := uint32(0)
for i := 0; i < 2; i++ {
res = res + uint32(int16(mem[sp+imm+uint32(i)])<<uint(8*i))
}
reg[rd] = res
case 2: // LW
res := uint32(0)
for i := 0; i < 4; i++ {
res = res + uint32(int32(mem[sp+imm+uint32(i)])<<uint(8*i))
}
reg[rd] = res
case 4: // LBU
reg[rd] = uint32(mem[sp+imm])
case 5: // LHU
res := uint32(0)
for i := 0; i < 2; i++ {
res = res + uint32(uint16(mem[sp+imm+uint32(i)])<<uint(8*i))
}
reg[rd] = res
}
case 0x13:
rd := (instr >> 7) & 0x1f
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f
imm := sext((instr >> 20))
switch funct3 {
case 0: // Addi
reg[rd] = reg[rs1] + imm
case 1: // Shift Left Logical Intermediate
shamt := imm & 0x3f
rest := (imm >> 6)
if rest == 0 {
reg[rd] = reg[rs1] << shamt
}
case 2: // SLTI
trs1 := int32(reg[rs1])
timm := int32(imm)
if trs1 < timm {
reg[rd] = 1
} else {
reg[rd] = 0
}
case 3: // SLTIU
if reg[rs1] < imm {
reg[rd] = 1
} else {
reg[rd] = 0
}
case 4: // XOR Intermediate
reg[rd] = reg[rs1] ^ imm
case 5: // Shift Right Intermediate
shamt := imm & 0x3f
rest := (imm >> 6)
if rest == 0 { // Logical
reg[rd] = reg[rs1] >> shamt
} else { // Arithmetic
reg[rd] = uint32(int32(reg[rs1]) >> shamt)
}
case 6: // OR Intermediate
reg[rd] = reg[rs1] | imm
case 7: // AND Intermediate
reg[rd] = reg[rs1] & imm
}
case 0x17: // AUIPC
rd := (instr >> 7) & 0x1f
imm := (instr >> 12) << 12
reg[rd] = pc + imm
case 0x23:
imm1 := (instr >> 7) & 0x1f
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f // base
rs2 := (instr >> 20) & 0x1f // src
imm2 := (instr >> 25)
imm := sext(imm2<<5 + imm1)
sp := reg[rs1]
switch funct3 {
case 0: // SB
mem[sp+imm] = byte(reg[rs2] & 0xff)
case 1: // SH
for i := 0; i < 2; i++ {
mem[sp+imm+uint32(i)] = byte((uint16(reg[rs2]) >> uint(8*i)) & 0xff)
}
case 2: // SW
for i := 0; i < 4; i++ {
mem[sp+imm+uint32(i)] = byte((uint32(reg[rs2]) >> uint(8*i)) & 0xff)
}
}
case 0x33:
rd := (instr >> 7) & 0x1f
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f
rs2 := (instr >> 20) & 0x1f
funct7 := (instr >> 25)
switch funct3 {
case 0:
switch funct7 {
case 0: // Add
reg[rd] = reg[rs1] + reg[rs2]
case 1: // Mul
reg[rd] = reg[rs1] * reg[rs2]
case 32: // Sub
reg[rd] = reg[rs1] - reg[rs2]
}
case 1:
switch funct7 {
case 0: // Shift Left Logical
reg[rd] = reg[rs1] << reg[rs2]
case 1: // Mulh
res := int64(int32(reg[rs1])) * int64(int32(reg[rs2]))
res = res >> 32
reg[rd] = uint32(res)
}
case 2:
switch funct7 {
case 0: // SLT
trs1 := int32(reg[rs1])
trs2 := int32(reg[rs2])
if trs1 < trs2 {
reg[rd] = 1
} else {
reg[rd] = 0
}
case 1: // Mulhsu
res := int64(int32(reg[rs1])) * int64(reg[rs2])
res = res >> 32
reg[rd] = uint32(res)
}
case 3:
switch funct7 {
case 0: // SLTU
if reg[rs1] < reg[rs2] {
reg[rd] = 1
} else {
reg[rd] = 0
}
case 1: // Mulhu
res := uint64(reg[rs1]) * uint64(reg[rs2])
res = res >> 32
reg[rd] = uint32(res)
}
case 4:
switch funct7 {
case 0: // XOR
reg[rd] = reg[rs1] ^ reg[rs2]
case 1: // Div
if int32(reg[rs2]) == 0 {
reg[rd] = ^uint32(0)
} else {
reg[rd] = uint32(int32(reg[rs1]) / int32(reg[rs2]))
}
}
case 5: // Shift Right
switch funct7 {
case 0: // Logical
reg[rd] = reg[rs1] >> reg[rs2]
case 1: // Divu
// TODO: ask TA about unsigned division by zero.
if reg[rs2] == 0 {
reg[rd] = reg[rs1]
} else {
reg[rd] = reg[rs1] / reg[rs2]
}
case 32: // Arithmetic
reg[rd] = uint32(int32(reg[rs1]) >> reg[rs2])
}
case 6:
switch funct7 {
case 0: // OR
reg[rd] = reg[rs1] | reg[rs2]
case 1: // Rem
if reg[rs2] == 0 {
reg[rd] = uint32(int32(reg[rs1]))
} else {
reg[rd] = uint32(int32(reg[rs1]) % int32(reg[rs2]))
}
}
case 7:
switch funct7 {
case 0: // AND
reg[rd] = reg[rs1] & reg[rs2]
case 1: // Remu
if reg[rs2] == 0 {
reg[rd] = reg[rs1]
} else {
reg[rd] = reg[rs1] % reg[rs2]
}
}
}
case 0x37: // LUI
rd := (instr >> 7) & 0x1f
imm := (instr >> 12) << 12
reg[rd] = imm
case 0x63: // Branching
imm1 := (instr >> 7) & 0x1 // imm 11
imm2 := (instr >> 8) & 0xf // imm 1 - 4
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f
rs2 := (instr >> 20) & 0x1f
imm3 := (instr >> 25) & 0x3f // imm 5 - 10
imm4 := (instr >> 31) // imm 12
imm := imm4<<11 + imm1<<10 + imm3<<4 + imm2
if imm4 == 1 {
offset = -2 * int(imm^0xfff+1)
} else {
offset = 2 * int(imm)
}
switch funct3 {
case 0: // BEQ
branching = reg[rs1] == reg[rs2]
case 1: // BNE
branching = reg[rs1] != reg[rs2]
case 4: // BLT
branching = int32(reg[rs1]) < int32(reg[rs2])
case 5: // BGE
branching = int32(reg[rs1]) >= int32(reg[rs2])
case 6: // BLTU
branching = reg[rs1] < reg[rs2]
case 7: // BGEU
branching = reg[rs1] >= reg[rs2]
}
case 0x67: // JALR
rd := (instr >> 7) & 0x1f
funct3 := (instr >> 12) & 0x7
rs1 := (instr >> 15) & 0x1f
imm := sext((instr >> 20))
if funct3 == 0 {
branching = true
reg[rd] = pc + 1
offset = int(reg[rs1]+imm) & 0xfffffffe
}
case 0x6f: // JAL
rd := (instr >> 7) & 0x1f
imm1 := (instr >> 12) & 0xff // imm 12 - 19
imm2 := (instr >> 20) & 0x1 // imm 11
imm3 := (instr >> 21) & 0x3ff // imm 1 - 10
imm4 := (instr >> 31) // imm 20
imm := sext((imm4<<20 + imm1<<12 + imm2<<11 + imm3<<1))
branching = true
reg[rd] = pc + 1
offset = int(reg[rd] + imm)
case 0x73: // Ecall
fmt.Println(conv(reg)...)
exit = true
default:
fmt.Printf("Opcode %d not yet implemented\n", opcode)
}
reg[0] = 0
return offset, branching, exit
}
func usage() {
fmt.Println(`Usage: caeriscv [-debug] <binary file>`)
flag.PrintDefaults()
}
func main() {
debug := flag.Bool("debug", false, "enable debug information")
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 || !strings.HasSuffix(args[0], ".bin") {
usage()
os.Exit(1)
}
reg := make([]uint32, 32)
mem := make([]byte, 4096)
reg[2] = uint32(len(mem))
prog, err := readBinary(args[0])
if err != nil {
panic(err)
}
w := new(tabwriter.Writer)
if *debug {
w.Init(os.Stdout, 0, 0, 2, ' ', tabwriter.AlignRight)
fmt.Fprintln(w, "Welcome to Go RISC-V simulator")
fmt.Fprintf(w, "Running program: %s\n", filepath.Base(args[0]))
fmt.Fprintln(w, "Instructions:")
for i, instr := range prog {
fmt.Fprintf(w, "%d: %v\n", i, instr)
}
fmt.Fprintln(w)
fmt.Fprintf(w, header, gen()...)
}
pc := uint32(0)
for {
instr := prog[pc]
offset, branching, exit := execute(pc, instr, reg, mem)
if *debug {
fmt.Fprintf(w, "%v\t", pc)
fmt.Fprintf(w, body, conv(reg)...)
}
if exit {
break
}
if branching {
pc = pc + uint32((offset / 4))
continue
}
pc++
if pc >= uint32(len(prog)) {
break
}
}
w.Flush()
err = writeBinary("out.res", reg)
if err != nil {
panic(err)
}
}