-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (73 loc) · 1.56 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
package main
import (
"bytes"
"encoding/binary"
"lc3/pkg/cpu"
"log"
"math"
"os"
)
func readImage(filename string) ([math.MaxUint16 + 1]uint16, error) {
m := [math.MaxUint16 + 1]uint16{}
file, err := os.Open(filename)
if err != nil {
return m, err
}
defer file.Close()
stats, err := file.Stat()
if err != nil {
return m, err
}
// load the origin
var origin uint16
headerBytes := make([]byte, 2)
_, err = file.Read(headerBytes)
if err != nil {
return m, err
}
headerBuffer := bytes.NewBuffer(headerBytes)
err = binary.Read(headerBuffer, binary.BigEndian, &origin)
if err != nil {
return m, err
}
log.Printf("Origin memory location: 0x%04X", origin)
size := stats.Size()
byteArr := make([]byte, size)
log.Printf("Creating memory buffer: %d bytes", size)
_, err = file.Read(byteArr)
if err != nil {
return m, err
}
buffer := bytes.NewBuffer(byteArr)
for i := origin; i < math.MaxUint16; i++ {
var val uint16
binary.Read(buffer, binary.BigEndian, &val)
m[i] = val
}
return m, err
}
func loadArguments() [][math.MaxUint16 + 1]uint16 {
args := os.Args[1:]
if len(args) < 1 {
log.Fatal("lc3 [image-file1] ...\n")
}
var images [][math.MaxUint16 + 1]uint16
for _, arg := range args {
image, err := readImage(arg)
if err != nil {
log.Fatalf("failed to load image: %s, %v", arg, err)
}
images = append(images, image)
}
return images
}
func main() {
args := loadArguments()
for _, args := range args {
cpu := cpu.NewCPU()
err := cpu.Run(args)
if err != nil {
log.Fatalf("Execution failed %v", err)
}
}
}