-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
244 lines (220 loc) · 5.2 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
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/jpr98/memsim/cpu"
"github.com/jpr98/memsim/mem"
)
var comp cpu.CPU
var reader *bufio.Reader
var breaking *bool
func main() {
filename := flag.String("filename", "input.txt", "the name of the file to read instructions from")
debug := flag.Bool("debug", false, "debug mode, pass true to see memory allocation")
policy := flag.String("policy", "FIFO", "sets the replacement policy to be used when swapping pages")
breaking = flag.Bool("breaking", true, "*experimental* asks user to continue or quit execution if there is an error")
server := flag.Bool("server", false, "if true a server with live monitoring will start")
flag.Parse()
file, err := os.Open(*filename)
if err != nil {
errorMessage := fmt.Sprintf("File %s couldn't be opened. Verify it's existance.", *filename)
fmt.Println(errorMessage)
fmt.Println("Stopping execution")
os.Exit(1)
return
}
defer file.Close()
MMU, err := mem.NewMMU(2048, 4096, 16, *policy)
if err != nil {
fmt.Println(err.Error())
fmt.Println("Stopping execution")
os.Exit(1)
}
comp = cpu.New(*MMU)
if *breaking {
fmt.Println("")
fmt.Println("* You are using breaking, which is an experimental feature, if you decide to continue after an error anything could go wrong *")
fmt.Println("")
}
c := make(chan bool)
if *server {
go startServer(c)
}
reader = bufio.NewReader(os.Stdin)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
err := parseCommand(scanner.Text())
if err != nil {
fmt.Printf("Error parsing instruction: %s\n", err.Error())
if cont := askBreak(); cont {
continue
}
}
comp.Print(*debug, os.Stdout)
}
fmt.Println("Done!")
if *server {
<-c
}
}
func startServer(c chan bool) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
comp.Print(true, w)
})
http.ListenAndServe(":8000", nil)
}
func parseCommand(cmdStr string) error {
cmd := strings.Fields(cmdStr)
//start := time.Now()
switch strings.ToUpper(cmd[0]) {
case "P":
if len(cmd) < 3 {
return cmdArgsError("P", 3)
}
handleCreateProcess(cmd)
case "A":
if len(cmd) < 4 {
return cmdArgsError("A", 4)
}
handleAccess(cmd)
case "L":
if len(cmd) < 2 {
return cmdArgsError("L", 2)
}
handleClear(cmd)
case "C":
if len(cmd) < 1 {
return cmdArgsError("C", 2)
}
handleComment(cmd)
case "F":
if len(cmd) > 1 {
if confirmIntention("Did you mean F (finalize)?") {
handleFinalize()
} else {
return cmdArgsError("F", 1)
}
} else {
handleFinalize()
}
case "E":
if len(cmd) > 1 {
if confirmIntention("Did you mean E (end)?") {
handleEnd()
} else {
return cmdArgsError("E", 1)
}
} else {
handleEnd()
}
default:
fmt.Println("Invalid command")
}
//fmt.Println(time.Since(start))
return nil
}
// askBreak checks if user wants to continue even though an error ocurred, something can go wrong if they decide to continue
func askBreak() bool {
if *breaking {
fmt.Print("Do you wish to continue with next instruction? [y/n] ")
resp, _ := reader.ReadString('\n')
resp = strings.ToLower(resp)
resp = strings.TrimSpace(resp)
if resp == "n" {
fmt.Println("Stopping execution")
os.Exit(1)
return false
}
return true
}
fmt.Println("Stopping execution")
os.Exit(1)
return false
}
func confirmIntention(message string) bool {
fmt.Printf("%s [y/n] ", message)
resp, _ := reader.ReadString('\n')
resp = strings.ToLower(resp)
resp = strings.TrimSpace(resp)
if resp == "n" {
return false
}
return true
}
func handleCreateProcess(cmd []string) {
size, err := strconv.Atoi(cmd[1])
if err != nil {
fmt.Printf("Argument %s needs to be a number\n", cmd[1])
if cont := askBreak(); cont {
return
}
}
pid := cmd[2]
fmt.Printf("Loading PID: %s size: %d\n", pid, size)
err = comp.CreateProcess(pid, size)
if err != nil {
fmt.Println(err.Error())
if cont := askBreak(); cont {
return
}
}
}
func handleAccess(cmd []string) {
address, err := strconv.Atoi(cmd[1])
if err != nil {
fmt.Printf("Argument %s needs to be a number\n", cmd[1])
if cont := askBreak(); cont {
return
}
}
pid := cmd[2]
modify, err := strconv.ParseBool(cmd[3])
if err != nil {
fmt.Printf("Argument %s needs to be a boolean\n", cmd[3])
if cont := askBreak(); cont {
return
}
}
fmt.Printf("Accessing PID: %s address: %d modify: %t\n", pid, address, modify)
add, err := comp.AccessProcess(pid, address)
if err != nil {
fmt.Println(err.Error())
if cont := askBreak(); cont {
return
}
}
fmt.Printf("found at real address %d\n", add)
}
func handleClear(cmd []string) {
pid := cmd[1]
fmt.Printf("Clearing PID: %s\n", pid)
err := comp.DeleteProcess(pid)
if err != nil {
fmt.Println(err.Error())
if cont := askBreak(); cont {
return
}
}
}
func handleComment(cmd []string) {
if len(cmd) == 0 {
fmt.Println("")
return
}
comment := strings.Join(cmd[1:], " ")
fmt.Printf("Comment: %s\n", comment)
}
func handleFinalize() {
fmt.Println("Finalized this sequence of instructions")
comp.ReportStats()
fmt.Println("Reseting system")
fmt.Println("----------------------------------------------------")
}
func handleEnd() {
fmt.Println("End. Thanks for using the program!")
}