-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
134 lines (113 loc) · 3.88 KB
/
terminal.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
package main
import (
"fmt"
"io/ioutil"
"time"
"strconv"
termbox "github.com/nsf/termbox-go"
"github.com/peternoyes/dodo-sim"
)
func Terminal() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
keys := new(Keys)
keys.New()
firmware, err := Asset("data/firmware")
if err != nil {
fmt.Println(err)
return
}
game, err := ioutil.ReadFile("fram.bin")
if err != nil {
fmt.Println(err)
return
}
s := new(dodosim.SimulatorSync)
s.CyclesPerFrame = func(cycles uint64) {
drawString(66, 0, "Cycles/Frame: "+strconv.Itoa(int(cycles))+" ")
}
s.Renderer = new(ConsoleRenderer)
s.SimulateSyncInit(firmware, game, nil)
c := time.NewTicker(50 * time.Millisecond).C
for !keys.Done {
state := ""
if keys.IsPressed(A) {
state += "A"
}
if keys.IsPressed(B) {
state += "B"
}
if keys.IsPressed(Up) {
state += "U"
}
if keys.IsPressed(Left) {
state += "L"
}
if keys.IsPressed(Right) {
state += "R"
}
if keys.IsPressed(Down) {
state += "D"
}
s.PumpClock(state)
<-c
}
}
type ConsoleRenderer struct {
}
func drawString(x, y int, text string) {
for _, s := range text {
termbox.SetCell(x, y, s, termbox.ColorDefault, termbox.ColorDefault)
x++
}
termbox.Flush()
}
func (r *ConsoleRenderer) Render(data [1024]byte) {
var x, y int
for y = 0; y < 64; y += 2 {
for x = 0; x < 128; x += 2 {
p1 := (data[x+(y/8)*128] >> uint(y%8)) & 0x1
p2 := (data[x+1+(y/8)*128] >> uint(y%8)) & 0x1
p3 := (data[x+((y+1)/8)*128] >> uint((y+1)%8)) & 0x1
p4 := (data[x+1+((y+1)/8)*128] >> uint((y+1)%8)) & 0x1
if p1 == 0x0 && p2 == 0x0 && p3 == 0x0 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, ' ', termbox.ColorBlack, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x0 && p3 == 0x0 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u2597', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x0 && p3 == 0x1 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u2596', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x0 && p3 == 0x1 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u2584', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x1 && p3 == 0x0 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u259D', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x1 && p3 == 0x0 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u2590', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x1 && p3 == 0x1 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u259E', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x0 && p2 == 0x1 && p3 == 0x1 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u259F', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x0 && p3 == 0x0 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u2598', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x0 && p3 == 0x0 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u259A', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x0 && p3 == 0x1 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u258C', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x0 && p3 == 0x1 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u2599', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x1 && p3 == 0x0 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u2580', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x1 && p3 == 0x0 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u259C', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x1 && p3 == 0x1 && p4 == 0x0 {
termbox.SetCell(x/2, y/2, '\u259B', termbox.ColorGreen, termbox.ColorBlack)
} else if p1 == 0x1 && p2 == 0x1 && p3 == 0x1 && p4 == 0x1 {
termbox.SetCell(x/2, y/2, '\u2588', termbox.ColorGreen, termbox.ColorBlack)
}
}
termbox.Flush()
}
}