Skip to content

Commit d3d50f4

Browse files
committed
Be able to register a breakpoint, enable debugging and added button to go to next cpu step.
1 parent 2d3f60c commit d3d50f4

File tree

4 files changed

+221
-17
lines changed

4 files changed

+221
-17
lines changed

src/debugger/breakpointAdd.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package debugger
2+
3+
import (
4+
"fmt"
5+
"github.com/FMNSSun/hexit"
6+
"github.com/lachee/raylib-goplus/raylib"
7+
)
8+
9+
type breakpointAdd struct {
10+
panel *draggablePanel
11+
breakPointAddress uint16
12+
inputAddress string
13+
onAddBreakPointCallback func(addressBreakPoint uint16)
14+
}
15+
16+
func NewBreakpointAddPanel(onOk func(addressBreakPoint uint16)) *breakpointAdd {
17+
return &breakpointAdd{
18+
breakPointAddress: 0,
19+
inputAddress: "",
20+
onAddBreakPointCallback: onOk,
21+
panel: NewDraggablePanel(
22+
"Add breakpoint",
23+
raylib.Vector2{
24+
X: 0,
25+
Y: 0,
26+
},
27+
300,
28+
300,
29+
),
30+
}
31+
}
32+
33+
func (dbg *breakpointAdd) Open(x float32, y float32) {
34+
dbg.panel.position.X = x
35+
dbg.panel.position.Y = y
36+
dbg.panel.SetEnabled(true)
37+
}
38+
39+
func (dbg *breakpointAdd) Draw() {
40+
if !dbg.panel.Draw() {
41+
return
42+
}
43+
44+
raylib.GuiLabel(
45+
raylib.Rectangle{
46+
X: dbg.panel.position.X + 5,
47+
Y: dbg.panel.position.Y + 30 + 5,
48+
Height: 20,
49+
},
50+
"Address",
51+
)
52+
pressed, value := raylib.GuiTextBox(
53+
raylib.Rectangle{
54+
X: dbg.panel.position.X + 40 + 5 + 5,
55+
Y: dbg.panel.position.Y + 30 + 5,
56+
Width: 70,
57+
Height: 20,
58+
},
59+
dbg.inputAddress,
60+
5,
61+
true,
62+
)
63+
valueValid := false
64+
dbg.inputAddress = value
65+
formattedAddress := fmt.Sprintf("%04s", value)
66+
if len(formattedAddress) == 4 {
67+
valueValid = true
68+
dbg.breakPointAddress = hexit.UnhexUint16Str(formattedAddress)
69+
}
70+
71+
if valueValid {
72+
pressed = raylib.GuiButton(
73+
raylib.Rectangle{
74+
X: dbg.panel.position.X + 5,
75+
Y: dbg.panel.position.Y + dbg.panel.height - 5 - 20,
76+
Width: dbg.panel.width - 5 - 5,
77+
Height: 20,
78+
},
79+
"Ok",
80+
)
81+
}
82+
83+
if pressed {
84+
dbg.panel.Close()
85+
dbg.onAddBreakPointCallback(dbg.breakPointAddress)
86+
}
87+
}

src/debugger/breakpoints.go

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,136 @@
11
package debugger
22

3-
import "github.com/lachee/raylib-goplus/raylib"
3+
import (
4+
"fmt"
5+
"github.com/lachee/raylib-goplus/raylib"
6+
)
47

58
type breakpointDebugger struct {
6-
panel *draggablePanel
9+
panel *draggablePanel
10+
breakpointAddPanel *breakpointAdd
11+
breakpoints [4]uint16
12+
breakpointsCount uint8
13+
breakpointEnabled bool
714
}
815

9-
const width = 300
16+
const breakpointDebuggerWidth = 500
1017

1118
func NewBreakpointDebugger() *breakpointDebugger {
1219
return &breakpointDebugger{
1320
panel: NewDraggablePanel(
1421
"Debugger · Breakpoints",
1522
raylib.Vector2{300, 350},
16-
width,
23+
breakpointDebuggerWidth,
1724
400,
1825
),
26+
breakpointAddPanel: nil,
27+
breakpointsCount: 0,
1928
}
2029
}
2130

22-
func (bp *breakpointDebugger) Toggle() {
23-
bp.panel.SetEnabled(!bp.panel.enabled)
31+
func (dbg *breakpointDebugger) Toggle() {
32+
dbg.panel.SetEnabled(!dbg.panel.enabled)
2433
}
2534

2635
func (dbg *breakpointDebugger) Draw() {
2736
if !dbg.panel.Draw() {
2837
return
2938
}
39+
padding := float32(5)
40+
anchor := raylib.Vector2{dbg.panel.position.X + padding, dbg.panel.position.Y + 30}
41+
42+
raylib.GuiLabel(
43+
raylib.Rectangle{anchor.X, anchor.Y, 200, 20},
44+
"Disassembler",
45+
)
46+
raylib.GuiListViewEx(
47+
raylib.Rectangle{anchor.X, anchor.Y + 20, 200, 300},
48+
[]string{
49+
"hola",
50+
"dos",
51+
},
52+
2,
53+
0,
54+
0,
55+
-1,
56+
)
57+
58+
dbg.breakPointControls(anchor)
59+
}
60+
61+
func (dbg *breakpointDebugger) breakPointControls(windowAnchor raylib.Vector2) {
62+
padding := float32(5)
63+
anchor := raylib.Vector2{windowAnchor.X + 200 + padding, windowAnchor.Y}
64+
width := float32(290)
65+
controlsWidth := 290 - padding*2
66+
67+
raylib.GuiGroupBox(
68+
raylib.Rectangle{anchor.X, anchor.Y, width, 300},
69+
"Breakpoints",
70+
)
71+
y := dbg.panel.registerStackedControl(anchor.Y+20, padding)
72+
73+
addBreakPointClicked := raylib.GuiButton(
74+
raylib.Rectangle{anchor.X, y, controlsWidth, 20},
75+
"Add breakpoint",
76+
)
77+
y = dbg.panel.registerStackedControl(20, padding)
78+
79+
// List of breakpoints
80+
var breakpoints []string
81+
for i := uint8(0); i < dbg.breakpointsCount; i++ {
82+
breakpoints = append(breakpoints, fmt.Sprintf("0x%X", dbg.breakpoints[i]))
83+
}
84+
breakpointListHeight := float32(20 * 4)
85+
raylib.GuiListViewEx(
86+
raylib.Rectangle{anchor.X, y, controlsWidth, breakpointListHeight},
87+
breakpoints,
88+
len(breakpoints),
89+
0,
90+
0,
91+
-1,
92+
)
93+
y = dbg.panel.registerStackedControl(breakpointListHeight, padding)
94+
95+
// Emulator control
96+
dbg.breakpointEnabled = raylib.GuiCheckBox(
97+
raylib.Rectangle{anchor.X, y, 20, 20},
98+
"Listen BP",
99+
dbg.breakpointEnabled,
100+
)
101+
y = dbg.panel.registerStackedControl(20, padding)
102+
103+
raylib.GuiButton(
104+
raylib.Rectangle{anchor.X, y, 100, 20},
105+
//raylib.GuiIconText(raylib.)
106+
"Step",
107+
)
108+
109+
if dbg.breakpointsCount < 4 {
110+
if addBreakPointClicked {
111+
dbg.showBreakpointAdd()
112+
}
113+
dbg.updateBreakpointAdd()
114+
}
115+
}
116+
117+
func (dbg *breakpointDebugger) showBreakpointAdd() {
118+
if dbg.breakpointAddPanel == nil {
119+
dbg.breakpointAddPanel = NewBreakpointAddPanel(dbg.onAddBreakpoint)
120+
}
121+
dbg.breakpointAddPanel.Open(dbg.panel.position.X+10, dbg.panel.position.Y+30)
122+
}
123+
124+
func (dbg *breakpointDebugger) updateBreakpointAdd() {
125+
if dbg.breakpointAddPanel == nil {
126+
return
127+
}
128+
129+
dbg.breakpointAddPanel.Draw()
130+
}
131+
132+
func (dbg *breakpointDebugger) onAddBreakpoint(address uint16) {
133+
fmt.Printf("Breakpoint created: %X\n", address)
134+
dbg.breakpoints[dbg.breakpointsCount] = address
135+
dbg.breakpointsCount++
30136
}

src/debugger/debuggerPanel.go renamed to src/debugger/draggablePanel.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type draggablePanel struct {
1010
height float32
1111
dragWindow bool
1212
positionOnStartDrag raylib.Vector2
13+
layoutYPositions []float32
1314
}
1415

1516
func NewDraggablePanel(title string, position raylib.Vector2, width int, height int) *draggablePanel {
@@ -32,6 +33,7 @@ func (panel *draggablePanel) Draw() bool {
3233
return panel.enabled
3334
}
3435

36+
panel.layoutYPositions = nil
3537
panel.updateWindowPosition()
3638
shouldClose := raylib.GuiWindowBox(
3739
raylib.Rectangle{
@@ -79,7 +81,17 @@ func (panel *draggablePanel) statusBarPosition() raylib.Rectangle {
7981
return raylib.Rectangle{
8082
X: panel.position.X,
8183
Y: panel.position.Y,
82-
Width: ppuPanelWidth - 20,
84+
Width: panel.width - 20,
8385
Height: 20,
8486
}
8587
}
88+
89+
// registerStackedControl registers the height of a gui control rendered, and returns the Y position for the next element
90+
func (panel *draggablePanel) registerStackedControl(height float32, padding float32) float32 {
91+
panel.layoutYPositions = append(panel.layoutYPositions, height)
92+
sum := float32(0)
93+
for i := 0; i < len(panel.layoutYPositions); i++ {
94+
sum += panel.layoutYPositions[i] + padding
95+
}
96+
return sum
97+
}

src/debugger/ppu.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,33 @@ func (dbg *PPUDebugger) ppuControlGroup(fullWidth float32, x float32, y float32)
6464
spriteSizeEnabled := false
6565
masterSlaveEnabled := false
6666
generateNMIEnabled := false
67-
ppuControl := dbg.ppu.PpuControl
68-
if ppuControl.NameTableX == 1 {
67+
if dbg.ppu.PpuControl.NameTableX == 1 {
6968
ntXEnabled = true
7069
}
71-
if ppuControl.NameTableY == 1 {
70+
if dbg.ppu.PpuControl.NameTableY == 1 {
7271
ntYEnabled = true
7372
}
74-
if ppuControl.IncrementMode == 1 {
73+
if dbg.ppu.PpuControl.IncrementMode == 1 {
7574
incrementModeEnabled = true
7675
}
77-
if ppuControl.SpritePatternTableAddress == 1 {
76+
if dbg.ppu.PpuControl.SpritePatternTableAddress == 1 {
7877
spPatternEnabled = true
7978
}
80-
if ppuControl.BackgroundPatternTableAddress == 1 {
79+
if dbg.ppu.PpuControl.BackgroundPatternTableAddress == 1 {
8180
bgPatternEnabled = true
8281
}
83-
if ppuControl.SpriteSize == 1 {
82+
if dbg.ppu.PpuControl.SpriteSize == 1 {
8483
spriteSizeEnabled = true
8584
}
86-
if ppuControl.MasterSlaveSelect == 1 {
85+
if dbg.ppu.PpuControl.MasterSlaveSelect == 1 {
8786
masterSlaveEnabled = true
8887
}
89-
if ppuControl.GenerateNMIAtVBlank {
88+
if dbg.ppu.PpuControl.GenerateNMIAtVBlank {
9089
generateNMIEnabled = true
9190
}
9291

9392
anchor := raylib.Vector2{x, y}
94-
raylib.GuiGroupBox(raylib.Rectangle{anchor.X + 0, anchor.Y + 0, fullWidth, 64}, fmt.Sprintf("PPUControl: 0x%0X", ppuControl.Value()))
93+
raylib.GuiGroupBox(raylib.Rectangle{anchor.X + 0, anchor.Y + 0, fullWidth, 64}, fmt.Sprintf("PPUControl: 0x%0X", dbg.ppu.PpuControl.Value()))
9594

9695
raylib.GuiCheckBox(raylib.Rectangle{anchor.X + 10, anchor.Y + 10, 12, 12}, "nt X", ntXEnabled)
9796
raylib.GuiCheckBox(raylib.Rectangle{anchor.X + 10, anchor.Y + 24, 12, 12}, "nt Y", ntYEnabled)

0 commit comments

Comments
 (0)