Skip to content

Commit 2d3f60c

Browse files
committed
New panel for setting cpu breakpoints.
1 parent 33b42db commit 2d3f60c

File tree

6 files changed

+162
-81
lines changed

6 files changed

+162
-81
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
## Shortcuts
99
- `p` Displays PPU Register debug panel.
10+
- `o` Displays Breakpoint debugger.
1011

1112

1213
# Status

src/debugger/breakpoints.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package debugger
2+
3+
import "github.com/lachee/raylib-goplus/raylib"
4+
5+
type breakpointDebugger struct {
6+
panel *draggablePanel
7+
}
8+
9+
const width = 300
10+
11+
func NewBreakpointDebugger() *breakpointDebugger {
12+
return &breakpointDebugger{
13+
panel: NewDraggablePanel(
14+
"Debugger · Breakpoints",
15+
raylib.Vector2{300, 350},
16+
width,
17+
400,
18+
),
19+
}
20+
}
21+
22+
func (bp *breakpointDebugger) Toggle() {
23+
bp.panel.SetEnabled(!bp.panel.enabled)
24+
}
25+
26+
func (dbg *breakpointDebugger) Draw() {
27+
if !dbg.panel.Draw() {
28+
return
29+
}
30+
}

src/debugger/controller.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,49 @@ import (
1111

1212
const DEBUG_X_OFFSET = 300
1313

14-
type DebuggerGUI struct {
14+
type Debugger struct {
1515
chrPaletteSelector uint8
1616
overlayTileIdx bool
1717
overlayAttributeTable bool
1818
font *raylib.Font
1919

20-
emulator *nes.Nes
21-
ppuDebugger *PPUDebugger
20+
emulator *nes.Nes
21+
ppuDebugger *PPUDebugger
22+
breakpointDebugger *breakpointDebugger
2223
}
2324

24-
func NewDebuggerGUI(emulator *nes.Nes) DebuggerGUI {
25+
type Panel interface {
26+
Draw()
27+
}
28+
29+
func NewDebugger(emulator *nes.Nes) Debugger {
2530
font := raylib.LoadFont("./assets/Pixel_NES.otf")
31+
raylib.GuiLoadStyle("./assets/style.rgs")
2632

27-
return DebuggerGUI{
33+
return Debugger{
2834
chrPaletteSelector: 0,
2935
overlayTileIdx: false,
3036
overlayAttributeTable: false,
3137
font: font,
3238
emulator: emulator,
3339
ppuDebugger: NewPPUDebugger(emulator.PPU()),
40+
breakpointDebugger: NewBreakpointDebugger(),
3441
}
3542
}
3643

37-
func (dbg *DebuggerGUI) Close() {
44+
func (dbg *Debugger) Close() {
3845
defer raylib.UnloadFont(dbg.font)
3946
}
4047

41-
func (dbg *DebuggerGUI) Tick() {
48+
func (dbg *Debugger) Tick() {
4249
dbg.listenKeyboard()
4350
raylib.DrawFPS(0, 0)
4451
dbg.ppuDebugger.Draw()
45-
52+
dbg.breakpointDebugger.Draw()
4653
//dbg.DrawDebugger(dbg.emulator)
4754
}
4855

49-
func (dbg *DebuggerGUI) DrawDebugger(emulator *nes.Nes) {
56+
func (dbg *Debugger) DrawDebugger(emulator *nes.Nes) {
5057
x := DEBUG_X_OFFSET
5158
y := 10
5259

@@ -99,13 +106,17 @@ func (dbg *DebuggerGUI) DrawDebugger(emulator *nes.Nes) {
99106
//drawObjectAttributeEntries(emulator)
100107
}
101108

102-
func (dbg *DebuggerGUI) listenKeyboard() {
109+
func (dbg *Debugger) listenKeyboard() {
103110
if raylib.IsKeyPressed(raylib.KeyP) {
104111
//dbg.chrPaletteSelector += 1
105112
//if dbg.chrPaletteSelector > (8 - 1) {
106113
// dbg.chrPaletteSelector = 0
107114
//}
108-
dbg.ppuDebugger.SetEnabled(true)
115+
dbg.ppuDebugger.Toggle()
116+
}
117+
118+
if raylib.IsKeyPressed(raylib.KeyO) {
119+
dbg.breakpointDebugger.Toggle()
109120
}
110121
}
111122

@@ -140,7 +151,7 @@ func drawASM(console *nes.Nes) {
140151
}
141152
}
142153

143-
func drawPalettes(console *nes.Nes, scale int, xOffset int, yOffset int, debuggerGUI *DebuggerGUI) {
154+
func drawPalettes(console *nes.Nes, scale int, xOffset int, yOffset int, debuggerGUI *Debugger) {
144155
// Draw defined palettes (8)
145156
x := xOffset
146157
y := yOffset
@@ -182,7 +193,7 @@ func drawColorWatch(coordX int, coordY int, width int, height int, color color.C
182193
raylib.DrawRectangle(coordX, coordY, width, height, pixelColor2RaylibColor(color))
183194
}
184195

185-
func drawCHR(console *nes.Nes, scale int, xOffset int, yOffset int, font *raylib.Font, debuggerGUI *DebuggerGUI) {
196+
func drawCHR(console *nes.Nes, scale int, xOffset int, yOffset int, font *raylib.Font, debuggerGUI *Debugger) {
186197
drawIndexes := false
187198

188199
if raylib.IsKeyDown(raylib.KeyZero) {

src/debugger/debuggerPanel.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package debugger
2+
3+
import "github.com/lachee/raylib-goplus/raylib"
4+
5+
type draggablePanel struct {
6+
title string
7+
enabled bool
8+
position raylib.Vector2
9+
width float32
10+
height float32
11+
dragWindow bool
12+
positionOnStartDrag raylib.Vector2
13+
}
14+
15+
func NewDraggablePanel(title string, position raylib.Vector2, width int, height int) *draggablePanel {
16+
return &draggablePanel{
17+
title: title,
18+
enabled: false,
19+
position: position,
20+
width: float32(width),
21+
height: float32(height),
22+
}
23+
}
24+
25+
func (panel *draggablePanel) SetEnabled(enabled bool) {
26+
panel.enabled = enabled
27+
}
28+
29+
// Draw returns true if panel is active
30+
func (panel *draggablePanel) Draw() bool {
31+
if panel.enabled == false {
32+
return panel.enabled
33+
}
34+
35+
panel.updateWindowPosition()
36+
shouldClose := raylib.GuiWindowBox(
37+
raylib.Rectangle{
38+
X: panel.position.X,
39+
Y: panel.position.Y,
40+
Width: panel.width,
41+
Height: panel.height,
42+
},
43+
panel.title,
44+
)
45+
if shouldClose {
46+
panel.Close()
47+
}
48+
49+
return true
50+
}
51+
52+
func (panel *draggablePanel) Close() {
53+
panel.enabled = false
54+
}
55+
56+
func (panel *draggablePanel) updateWindowPosition() {
57+
mousePosition := raylib.GetMousePosition()
58+
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
59+
60+
if raylib.CheckCollisionPointRec(mousePosition, panel.statusBarPosition()) {
61+
panel.dragWindow = true
62+
panel.positionOnStartDrag = raylib.Vector2{
63+
X: mousePosition.X - panel.position.X,
64+
Y: mousePosition.Y - panel.position.Y,
65+
}
66+
}
67+
}
68+
69+
if panel.dragWindow {
70+
panel.position.X = mousePosition.X - panel.positionOnStartDrag.X
71+
panel.position.Y = mousePosition.Y - panel.positionOnStartDrag.Y
72+
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) {
73+
panel.dragWindow = false
74+
}
75+
}
76+
}
77+
78+
func (panel *draggablePanel) statusBarPosition() raylib.Rectangle {
79+
return raylib.Rectangle{
80+
X: panel.position.X,
81+
Y: panel.position.Y,
82+
Width: ppuPanelWidth - 20,
83+
Height: 20,
84+
}
85+
}

src/debugger/ppu.go

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,53 @@ import (
66
"github.com/raulferras/nes-golang/src/nes/ppu"
77
)
88

9-
const debuggerWidth = 350
9+
const ppuPanelWidth = 350
1010

1111
type PPUDebugger struct {
12-
enabled bool
13-
windowRectangle raylib.Rectangle
14-
ppu *ppu.Ppu2c02
15-
dragWindow bool
16-
positionOnStartDrag raylib.Vector2
12+
panel *draggablePanel
13+
ppu *ppu.Ppu2c02
1714
}
1815

1916
func NewPPUDebugger(ppu *ppu.Ppu2c02) *PPUDebugger {
2017
return &PPUDebugger{
21-
enabled: false,
22-
windowRectangle: raylib.Rectangle{X: 300, Width: debuggerWidth, Height: 450},
23-
dragWindow: false,
24-
ppu: ppu,
18+
panel: NewDraggablePanel(
19+
"PPU Registers",
20+
raylib.Vector2{300, 0},
21+
ppuPanelWidth,
22+
450,
23+
),
24+
ppu: ppu,
2525
}
2626
}
2727

28-
func (dbg *PPUDebugger) SetEnabled(enabled bool) {
29-
dbg.enabled = enabled
28+
func (dbg *PPUDebugger) Toggle() {
29+
dbg.panel.SetEnabled(!dbg.panel.enabled)
3030
}
3131

3232
func (dbg *PPUDebugger) Draw() {
33-
if dbg.enabled == false {
33+
if !dbg.panel.Draw() {
3434
return
3535
}
36-
37-
dbg.updateWindowPosition()
38-
shouldClose := raylib.GuiWindowBox(dbg.windowRectangle, "PPU Registers")
39-
if shouldClose {
40-
dbg.Close()
41-
}
4236
padding := float32(5)
43-
fullWidth := debuggerWidth - (padding * 2)
37+
fullWidth := ppuPanelWidth - (padding * 2)
4438

45-
y := dbg.windowRectangle.Y + 30 + padding
46-
dbg.ppuControlGroup(fullWidth, dbg.windowRectangle.X+padding, y)
39+
y := dbg.panel.position.Y + 30 + padding
40+
dbg.ppuControlGroup(fullWidth, dbg.panel.position.X+padding, y)
4741

4842
y += 64 + padding*2
49-
dbg.ppuStatusGroup(fullWidth, dbg.windowRectangle.X+padding, y)
43+
dbg.ppuStatusGroup(fullWidth, dbg.panel.position.X+padding, y)
5044

5145
y += 32 + padding*2
52-
dbg.ppuMaskGroup(fullWidth, dbg.windowRectangle.X+padding, y)
46+
dbg.ppuMaskGroup(fullWidth, dbg.panel.position.X+padding, y)
5347

5448
y += 64 + padding*2
55-
dbg.loopyRegister(fullWidth, dbg.windowRectangle.X+padding, y, dbg.ppu.VRam(), "V")
49+
dbg.loopyRegister(fullWidth, dbg.panel.position.X+padding, y, dbg.ppu.VRam(), "V")
5650

5751
y += 64 + padding*2
58-
dbg.loopyRegister(fullWidth, dbg.windowRectangle.X+padding, y, dbg.ppu.TRam(), "T")
52+
dbg.loopyRegister(fullWidth, dbg.panel.position.X+padding, y, dbg.ppu.TRam(), "T")
5953

6054
y += 64 + padding*2
61-
dbg.renderingInfo(fullWidth, dbg.windowRectangle.X+padding, y)
55+
dbg.renderingInfo(fullWidth, dbg.panel.position.X+padding, y)
6256
}
6357

6458
func (dbg *PPUDebugger) ppuControlGroup(fullWidth float32, x float32, y float32) {
@@ -227,38 +221,3 @@ func (dbg *PPUDebugger) renderingInfo(fullWidth float32, x float32, y float32) {
227221
fmt.Sprintf("Render Cycle: %d", dbg.ppu.RenderCycle()),
228222
)
229223
}
230-
231-
func (dbg *PPUDebugger) updateWindowPosition() {
232-
mousePosition := raylib.GetMousePosition()
233-
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
234-
235-
if raylib.CheckCollisionPointRec(mousePosition, dbg.statusBarPosition()) {
236-
dbg.dragWindow = true
237-
dbg.positionOnStartDrag = raylib.Vector2{
238-
X: mousePosition.X - dbg.windowRectangle.X,
239-
Y: mousePosition.Y - dbg.windowRectangle.Y,
240-
}
241-
}
242-
}
243-
244-
if dbg.dragWindow {
245-
dbg.windowRectangle.X = mousePosition.X - dbg.positionOnStartDrag.X
246-
dbg.windowRectangle.Y = mousePosition.Y - dbg.positionOnStartDrag.Y
247-
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) {
248-
dbg.dragWindow = false
249-
}
250-
}
251-
}
252-
253-
func (dbg *PPUDebugger) statusBarPosition() raylib.Rectangle {
254-
return raylib.Rectangle{
255-
X: dbg.windowRectangle.X,
256-
Y: dbg.windowRectangle.Y,
257-
Width: debuggerWidth - 20,
258-
Height: 20,
259-
}
260-
}
261-
262-
func (dbg *PPUDebugger) Close() {
263-
dbg.enabled = false
264-
}

src/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func loop(console *nes.Nes) {
7878
cpuAdvance = true
7979
console.Start()
8080
_timestamp := r.GetTime()
81-
debuggerGUI := debugger.NewDebuggerGUI(console)
81+
debuggerGUI := debugger.NewDebugger(console)
8282

8383
for !r.WindowShouldClose() {
8484
if console.Stopped() {
@@ -91,14 +91,9 @@ func loop(console *nes.Nes) {
9191
if dt > 1 {
9292
dt = 0
9393
}
94-
//fmt.Printf("%f sec\n", dt)
95-
//if r.IsKeyPressed(r.KeySpace) {
96-
// cpuAdvance = true
97-
//}
9894

9995
// Update emulator
10096
if cpuAdvance {
101-
//console.Tick()
10297
console.TickForTime(dt)
10398
}
10499

0 commit comments

Comments
 (0)