-
Notifications
You must be signed in to change notification settings - Fork 6
/
armsim.go
129 lines (109 loc) · 2.81 KB
/
armsim.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
package main
import (
"errors"
"flag"
"fmt"
"github.com/lseelenbinder/armsim/armsim"
"github.com/lseelenbinder/armsim/web"
"log"
"os"
"os/exec"
)
type Options struct {
fileName string
memorySize uint
tracing bool
gui bool
exec bool
logFile string
}
func main() {
// Setup Logging
log.SetPrefix("Main: ")
// Welcome the user
fmt.Println("ARMSim by Luke Seelenbinder.")
// Handle command line flags
options, err := processFlags()
if err != nil {
fmt.Println(err)
flag.Usage()
return
}
// Handle logfile
var logFile *os.File
if options.logFile != "" {
logFile, err = os.Create(options.logFile)
if err != nil {
log.Println("Unable to open log file...")
logFile = os.Stderr
} else {
defer logFile.Close()
}
} else {
logFile = os.Stderr
}
// Initialize Computer
c := armsim.NewComputer(uint32(options.memorySize), logFile)
// Setup channels
halting := make(chan bool, 1)
finishing := make(chan bool, 1)
// Disable or Enable tracing
if !options.tracing {
c.DisableTracing()
} else {
defer c.DisableTracing()
}
// Load ELF File
if options.fileName != "" {
err = c.LoadELF(options.fileName)
if err != nil {
fmt.Println("Unable to load file. Encountered error -", err)
return
} else {
fmt.Println("Loaded valid ELF file - checksum is", c.Checksum())
}
}
if options.gui {
log.Println("Loading webserver...")
fmt.Println("Please open your web browser to http://localhost:4567/ to see the gui.")
// Attempt to open Firefox
cmd := exec.Command("firefox", "http://localhost:4567/")
cmd.Start()
s := web.Server{c, options.fileName, halting, finishing, nil, c.Keyboard, c.Console}
// Launch the webserver
s.Launch(logFile)
} else if options.exec {
// Run the program
c.Run(halting, finishing)
}
}
func processFlags() (options *Options, err error) {
// Create Options
options = new(Options)
// Define Options
flag.UintVar(&options.memorySize, "mem", 32768, "RAM size in bytes (1MB max)")
flag.StringVar(&options.fileName, "load", "", "ELF File Name")
flag.StringVar(&options.logFile, "log", "", "Log file")
flag.BoolVar(&options.tracing, "trace", true, "Output trace.log file (default=enabled)")
flag.BoolVar(&options.gui, "gui", true, "Use gui instead of command line")
flag.BoolVar(&options.exec, "exec", false, "Load file, execute and then close program (requires --load)")
// Parse Options
flag.Parse()
// Validate Options
log.Println("RAM Size:", options.memorySize)
if options.memorySize > 1048576 {
err = errors.New("RAM size is too large. Must be under 1MB (1048576).")
return
}
if options.exec && options.fileName != "" {
options.gui = false
}
if !options.gui {
log.Println("File name:", options.fileName)
if options.fileName == "" {
err = errors.New("Please specify a file name.")
return
}
}
return
}