/
main.go
195 lines (172 loc) · 3.53 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
package main
import (
"github.com/xconstruct/dcpu16/assembler"
"github.com/xconstruct/dcpu16/debugger"
"github.com/xconstruct/dcpu16/emulator"
"github.com/xconstruct/dcpu16/words"
"flag"
"fmt"
"os"
"io"
"io/ioutil"
"bufio"
)
func assert(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func main() {
tool := ""
if len(os.Args) > 1 {
tool = os.Args[1]
}
switch tool {
case "a": fallthrough
case "assemble":
runAssembler();
case "d": fallthrough
case "debug":
runDebugger()
case "dis": fallthrough
case "disassemble":
runDisassembler()
case "e": fallthrough
case "emulate":
runEmulator()
case "h": fallthrough
case "hexdump":
runHexdump()
default:
if len(os.Args) > 2 {
printHelp(os.Args[2]);
} else {
printHelp("");
}
}
}
func runEmulator() {
flag.Parse()
path := flag.Arg(1)
if path == "" {
printHelp("emulate")
return
}
file, err := os.Open(path)
assert(err)
dcpu := emulator.NewDCPU()
ram := words.NewReadWriter(dcpu.RAM)
_, err = io.Copy(ram, file)
file.Close()
assert(err)
err = dcpu.Exec()
assert(err)
}
func runDebugger() {
flag.Parse()
path := flag.Arg(1)
if path == "" {
printHelp("debug")
return
}
file, err := os.Open(path)
assert(err)
dcpu := emulator.NewDCPU()
ram := words.NewReadWriter(dcpu.RAM)
_, err = io.Copy(ram, file)
file.Close()
assert(err)
in := bufio.NewReader(os.Stdin)
for {
fmt.Print("(d) ")
buf, _, err := in.ReadLine()
line := string(buf)
if err == io.EOF {
return
}
assert(err)
switch (line) {
case "quit": return
case "step":
err := dcpu.Step()
if err != nil {
fmt.Println("dcpu err: ", err)
}
case "steploop":
err := debugger.StepLoop(dcpu)
if err != nil {
fmt.Println("dcpu err: ", err)
}
case "stepjmp":
err := debugger.StepJmp(dcpu)
if err != nil {
fmt.Println("dcpu err: ", err)
}
case "mem": words.Hexdump(dcpu.RAM, os.Stdout)
case "r": debugger.RDump(dcpu)
case "op": debugger.PrintInstruction(dcpu)
}
}
}
func runDisassembler() {
}
func runAssembler() {
flag.Parse()
srcPath := flag.Arg(1)
if srcPath == "" {
printHelp("assemble")
return
}
src, err := ioutil.ReadFile(srcPath)
assert(err)
var destWriter io.Writer
destPath := flag.Arg(2)
if destPath == "" {
destWriter = os.Stdout
} else {
destWriter, err = os.Open(destPath)
assert(err)
}
gen, err := assembler.Assemble(src)
assert(err)
genReader := words.NewReadWriter(gen)
_, err = io.Copy(destWriter, genReader)
assert(err)
}
func runHexdump() {
flag.Parse()
srcPath := flag.Arg(1)
if srcPath == "" {
printHelp("hexdump")
return
}
src, err := ioutil.ReadFile(srcPath)
assert(err)
w := make([]uint16, len(src)/2)
words.CopyFromBytes(w, src)
words.Hexdump(w, os.Stdout)
}
func printHelp(topic string) {
switch topic {
case "assemble":
fmt.Println(`Usage: dcpu assemble dasmfile [binfile]`)
case "debug":
fmt.Println("Usage: dcpu debug binfile")
case "emulate":
fmt.Println("Usage: dcpu emulate binfile")
case "hexdump":
fmt.Println(`Usage: dcpu hexdump binfile`)
default:
fmt.Println(`Dcpu16 is an assembler suite targeting the DCPU-16.
Usage:
dcpu16 command [arguments]
The commands and their shorthands are:
assemble a converts assembler to machine code
debug d debug a program in the emulator
disassemble dis converts machine code to assembler
emulate e execute a program in the emulator
hexdump h display a binary file in readable format
Use "dcpu help [command]" for more information about a command.`)
}
}