forked from scottferg/dcpu16
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
62 lines (48 loc) · 1.04 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
package main
import (
"github.com/nsf/termbox-go"
"io/ioutil"
"os"
)
func run(program []Word) {
cpu := new(Dcpu)
Memory = make([]Word, 0xffff)
cpu.PC = 0
cpu.SP = 0xfffe
video := new(Video)
video.Init()
event_queue := make(chan termbox.Event)
go func() {
for {
ev := termbox.PollEvent()
event_queue <- ev
}
}()
for index, value := range program {
Memory[index] = value
}
runloop:
for int(cpu.PC) < len(program) {
cpu.Step()
select {
case ev := <-event_queue:
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyEsc {
break runloop
}
default:
cpu.Step()
video.DrawScreen()
}
}
video.Close()
}
func main() {
if contents, err := ioutil.ReadFile(os.Args[1]); err == nil {
fileLength := len(contents)
var program []Word
for i := 0; i < fileLength; i += 2 {
program = append(program, (Word(contents[i]) << 8) + Word(contents[i + 1]))
}
run(program)
}
}