-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (98 loc) · 2.3 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
package main
import (
"debug/elf"
"flag"
"fmt"
"os"
)
var dbg bool
type RV struct {
cpu *CPU
// tohost is an special address which shows a message from program to the host.
// For now, tohost is used to terminate the execution of riscv-tests program.
// https://riscv.org/wp-content/uploads/2015/01/riscv-testing-frameworks-bootcamp-jan2015.pdf
tohost uint64
}
func (r *RV) Start() error {
for {
r.cpu.tick()
if r.tohost == 0 {
continue
}
if code := r.cpu.ram.Read(r.tohost, word); code != 0 {
if code == 1 {
return nil
}
return fmt.Errorf("terminated, the tohost code is not success but %v", code)
}
}
}
func debug(format string, a ...any) {
if dbg {
fmt.Printf("[debug] %s\n", fmt.Sprintf(format, a...))
}
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run() error {
var (
program = flag.String("p", "", "ELF program to run")
d = flag.Bool("d", false, "print out debug log if specified")
)
flag.Parse()
dbg = *d
file := *program
if file == "" {
return fmt.Errorf("program must be passed with -p option")
}
cpu, err := initCPU(file)
if err != nil {
return fmt.Errorf("initialize emulator: %w", err)
}
if err := cpu.Start(); err != nil {
return fmt.Errorf("run program: %w", err)
}
return nil
}
func initCPU(filename string) (*RV, error) {
of, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("open elf file: %w", err)
}
// load elf file
f, err := elf.Open(filename)
if err != nil {
return nil, fmt.Errorf("open elf file: %w", err)
}
if f.Data != elf.ELFDATA2LSB {
return nil, fmt.Errorf("elf must be little endian")
}
if f.Type != elf.ET_EXEC {
return nil, fmt.Errorf("elf type must be ET_EXEC")
}
if f.Machine != elf.EM_RISCV {
return nil, fmt.Errorf("elf machine must be RISCV")
}
cpu := NewCPU()
for _, p := range f.Progs {
if p.Type != elf.PT_LOAD {
continue
}
for i := 0; i < int(p.Filesz); i++ {
addr := p.Vaddr + uint64(i)
val := make([]byte, byt)
_, err := of.ReadAt(val, int64(p.Off)+int64(i))
if err != nil {
return nil, fmt.Errorf("read program header")
}
cpu.ram.Write(addr, uint64(val[0]), byt)
}
}
cpu.pc = f.Entry
rv := &RV{cpu: cpu, tohost: 0x80001000} // TODO: find tohost from sections
return rv, nil
}