/
stack.go
150 lines (127 loc) · 2.34 KB
/
stack.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
package main
import (
"fmt"
"strconv"
"time"
"github.com/jroimartin/gocui"
)
type stack struct {
c chan event
live chan bool
demand chan bool
contents []byte
written bool
sx, sy int
lastsp uint16
lastaddr uint16
lastpc uint16
bump int
}
func (self *stack) deliver(e event) {
self.c <- e
}
func (self *stack) makechan() {
self.c = make(chan event)
}
func (self *stack) draw(v *gocui.View, refresh bool) {
if refresh {
self.written = false
}
if self.written {
return
}
v.Clear()
self.sx, self.sy = v.Size()
blob := self.contents[self.bump:]
for off, b := range blob {
cur := self.lastaddr + uint16(off) + uint16(self.bump)
if off%2 == 0 {
if cur == self.lastsp {
fmt.Fprintf(v, "%0.4x:>> ", cur)
} else {
fmt.Fprintf(v, "%0.4x: ", cur)
}
fmt.Fprintf(v, "%0.2x", b)
} else {
fmt.Fprintf(v, "%0.2x\n", b)
}
}
self.written = true
}
func (self *stack) update() {
size := self.sy * 2
a, _ := strconv.ParseUint(CurrentStatus.stat.Cpu.Sp, 16, 16)
self.lastsp = uint16(a)
if self.lastsp != 0 {
if self.lastsp > uint16(size) {
self.lastaddr = self.lastsp - (uint16(size) / 2)
if self.lastpc%2 == 0 {
self.lastaddr += 1
}
} else {
self.lastaddr = 0
}
self.contents = peek(self.lastaddr, size)
self.written = false
redraw()
}
}
func (self *stack) init() {
time.Sleep(2 * time.Second)
v, _ := g.View("tabview")
self.sx, self.sy = v.Size()
// rate limit live updates
self.live = make(chan bool)
go func() {
cts := true
for {
select {
case <-self.live:
if cts {
self.update()
cts = false
}
case <-time.After(5 * time.Second):
cts = true
}
}
}()
// debounce keystroke updates
self.demand = make(chan bool)
go func() {
hit := false
fire := false
for {
select {
case hit = <-self.demand:
fire = false
case <-time.After(500 * time.Millisecond):
if !fire {
fire = true
} else if hit && fire {
self.update()
hit = false
fire = false
}
}
}
}()
self.update()
}
func (self *stack) loop() {
self.init()
for {
e := <-self.c
switch e.kind {
case FETCH_LIVE:
if uint16(e.addr) != self.lastpc {
self.lastpc = uint16(e.addr)
self.live <- true
}
case STACK_BUMP:
self.bump = (self.bump + 1) % 2
self.written = false
redraw()
}
}
}