Skip to content

Commit cc7c6f3

Browse files
committed
Be able to setup breakpoints.
1 parent 69d684e commit cc7c6f3

File tree

12 files changed

+73
-34
lines changed

12 files changed

+73
-34
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.14
55
require (
66
github.com/FMNSSun/hexit v0.0.0-20180713092704-89e1f9f0820e
77
github.com/lachee/raylib-goplus v0.0.0-20200605081007-7ca39b8afc71
8+
github.com/pkg/profile v1.6.0 // indirect
89
github.com/stretchr/testify v1.6.1
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/lachee/raylib-goplus v0.0.0-20200605081007-7ca39b8afc71 h1:H4fcwldyU7pQvzyR3fuNLe0Gs3f7yLg/lH7NorUTc7I=
66
github.com/lachee/raylib-goplus v0.0.0-20200605081007-7ca39b8afc71/go.mod h1:dBwvFApqhdnnQuWzvHZnvWph/zlTAHC3nd/Hy6yBE8U=
7+
github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM=
8+
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
79
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
810
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
911
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=

src/debugger/breakpoints.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package debugger
33
import (
44
"fmt"
55
"github.com/lachee/raylib-goplus/raylib"
6+
"github.com/raulferras/nes-golang/src/nes"
7+
"github.com/raulferras/nes-golang/src/nes/types"
68
)
79

810
type breakpointDebugger struct {
11+
emulator *nes.Nes
912
panel *draggablePanel
1013
breakpointAddPanel *breakpointAdd
1114
breakpoints [4]uint16
@@ -15,8 +18,9 @@ type breakpointDebugger struct {
1518

1619
const breakpointDebuggerWidth = 500
1720

18-
func NewBreakpointDebugger() *breakpointDebugger {
21+
func NewBreakpointDebugger(emulator *nes.Nes) *breakpointDebugger {
1922
return &breakpointDebugger{
23+
emulator: emulator,
2024
panel: NewDraggablePanel(
2125
"Debugger · Breakpoints",
2226
raylib.Vector2{300, 350},
@@ -133,4 +137,5 @@ func (dbg *breakpointDebugger) onAddBreakpoint(address uint16) {
133137
fmt.Printf("Breakpoint created: %X\n", address)
134138
dbg.breakpoints[dbg.breakpointsCount] = address
135139
dbg.breakpointsCount++
140+
dbg.emulator.Debugger().AddBreakPoint(types.Address(address))
136141
}

src/debugger/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewDebugger(emulator *nes.Nes) GuiDebugger {
3737
font: font,
3838
emulator: emulator,
3939
ppuDebugger: NewPPUDebugger(emulator.PPU()),
40-
breakpointDebugger: NewBreakpointDebugger(),
40+
breakpointDebugger: NewBreakpointDebugger(emulator),
4141
}
4242
}
4343

src/main.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,33 @@ import (
44
"flag"
55
"fmt"
66
r "github.com/lachee/raylib-goplus/raylib"
7+
"github.com/pkg/profile"
78
"github.com/raulferras/nes-golang/src/debugger"
89
"github.com/raulferras/nes-golang/src/graphics"
910
"github.com/raulferras/nes-golang/src/nes"
1011
"github.com/raulferras/nes-golang/src/nes/gamePak"
1112
"github.com/raulferras/nes-golang/src/nes/types"
1213
"github.com/raulferras/nes-golang/src/utils"
1314
"image"
14-
"log"
1515
"math/rand"
1616
_ "net/http/pprof"
17-
"os"
18-
"runtime/pprof"
1917
"time"
2018
)
2119

2220
var cpuAdvance bool
2321

2422
func main() {
25-
/*
26-
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
27-
var romPath = flag.String("rom", "", "path to rom")
28-
var debugPPU = flag.Bool("debugPPU", false, "Displays PPU debug information")
29-
flag.Parse()*/
30-
cpuprofile, romPath, debugPPU, maxCPUCycle := cmdLineArguments()
23+
scale, cpuprofile, romPath, debugPPU, maxCPUCycle := cmdLineArguments()
3124
if cpuprofile != "" {
32-
f, err := os.Create(cpuprofile)
25+
/*f, err := os.Create(cpuprofile)
3326
if err != nil {
3427
log.Fatal(err)
3528
}
3629
pprof.StartCPUProfile(f)
30+
pprof.
3731
defer pprof.StopCPUProfile()
32+
*/
33+
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
3834
}
3935

4036
rand.Seed(time.Now().UnixNano())
@@ -71,11 +67,11 @@ func main() {
7167
),
7268
)
7369

74-
loop(console)
70+
loop(console, scale)
7571
r.CloseWindow()
7672
}
7773

78-
func loop(console *nes.Nes) {
74+
func loop(console *nes.Nes, scale int) {
7975
cpuAdvance = true
8076
console.Start()
8177
_timestamp := r.GetTime()
@@ -130,12 +126,13 @@ func drawEmulation(frame *image.RGBA) {
130126
}
131127
}
132128

133-
func cmdLineArguments() (string, string, bool, int64) {
129+
func cmdLineArguments() (int, string, string, bool, int64) {
134130
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
135131
var romPath = flag.String("rom", "", "path to rom")
136132
var debugPPU = flag.Bool("debugPPU", false, "Displays PPU debug information")
137133
var stopAtCpuCycle = flag.Int64("maxCpuCycle", -1, "stops emulation at given cpu cycle")
134+
var scale = flag.Int("scale", 1, "scale resolution")
138135
flag.Parse()
139136

140-
return *cpuprofile, *romPath, *debugPPU, *stopAtCpuCycle
137+
return *scale, *cpuprofile, *romPath, *debugPPU, *stopAtCpuCycle
141138
}

src/nes/Debugger.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,49 @@ import (
1010
// Debugger offers an api to interact externally with
1111
// NES components
1212
type Debugger struct {
13-
debug bool
14-
cpu *Cpu6502
15-
ppu *ppu.Ppu2c02
16-
logPath string
17-
maxCPUCycle int64
18-
13+
cpu *Cpu6502
14+
ppu *ppu.Ppu2c02
15+
logPath string
16+
maxCPUCycle int64
1917
disassembled map[types.Address]string
2018
DebugPPU bool
19+
debugCPU bool
20+
// debugging related
21+
cpuBreakPoints map[types.Address]bool
22+
cpuStepByStepMode bool
2123
}
2224

23-
func CreateNesDebugger(logPath string, debug bool, debugPPU bool, maxCPUCycle int64) *Debugger {
25+
func CreateNesDebugger(logPath string, debugCPU bool, debugPPU bool, maxCPUCycle int64) *Debugger {
2426
return &Debugger{
25-
debug: debug,
2627
cpu: nil,
2728
ppu: nil,
2829
logPath: logPath,
2930
maxCPUCycle: maxCPUCycle,
3031
disassembled: nil,
32+
debugCPU: debugCPU,
3133
DebugPPU: debugPPU,
34+
35+
cpuBreakPoints: make(map[types.Address]bool),
36+
}
37+
}
38+
39+
func (debugger *Debugger) AddBreakPoint(address types.Address) {
40+
debugger.cpuBreakPoints[address] = true
41+
}
42+
43+
func (debugger *Debugger) RemoveBreakPoint(address types.Address) {
44+
debugger.cpuBreakPoints[address] = false
45+
}
46+
47+
func (debugger *Debugger) shouldPauseBecauseBreakpoint() bool {
48+
pc := debugger.cpu.ProgramCounter()
49+
enabled, exist := debugger.cpuBreakPoints[pc]
50+
if enabled && exist {
51+
debugger.cpuStepByStepMode = true
52+
return true
3253
}
54+
55+
return false
3356
}
3457

3558
func (debugger *Debugger) Disassembled() map[types.Address]string {

src/nes/Nes.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/raulferras/nes-golang/src/nes/ppu"
88
"github.com/raulferras/nes-golang/src/nes/types"
99
"image"
10+
"log"
1011
)
1112

1213
type Nes struct {
@@ -26,7 +27,7 @@ func CreateNes(gamePak *gamePak.GamePak, debugger *Debugger) *Nes {
2627
cpuBus := newNESCPUMemory(thePPU, gamePak)
2728
cpu := CreateCPU(
2829
cpuBus,
29-
cpu2.NewDebugger(debugger.debug, debugger.logPath+"/cpu.log"),
30+
cpu2.NewDebugger(debugger.debugCPU, debugger.logPath+"/cpu.log"),
3031
)
3132
debugger.cpu = cpu
3233
debugger.ppu = thePPU
@@ -104,8 +105,26 @@ func (nes *Nes) Tick() byte {
104105
}
105106

106107
func (nes *Nes) TickForTime(seconds float64) {
108+
/*
109+
if nes.Debugger().shouldPauseBecauseBreakpoint() {
110+
fmt.Printf("1\n")
111+
time.Sleep(100 * time.Millisecond)
112+
return
113+
} else if nes.Debugger().cpuStepByStepMode {
114+
fmt.Printf("2\n")
115+
time.Sleep(100 * time.Millisecond)
116+
nes.Tick()
117+
return
118+
}*/
119+
107120
cycles := int(1789773 * seconds)
121+
//log.Printf("Running for %.2fsec\n", seconds)
108122
for cycles > 0 {
123+
if nes.Debugger().shouldPauseBecauseBreakpoint() {
124+
log.Printf("Brekapoint reached")
125+
break
126+
}
127+
109128
cycles -= int(nes.Tick())
110129
if nes.Stopped() {
111130
break

src/nes/cpu6502.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ type Cpu6502 struct {
1919

2020
addressEvaluators [13]AddressModeMethod
2121

22-
// Debug parameters
23-
debugger *cpu.Debugger
24-
cyclesLimit uint16
22+
debugger *cpu.Debugger
2523
}
2624

2725
func CreateCPU(memory Memory, debugger *cpu.Debugger) *Cpu6502 {

src/nes/cpu6502_Logger.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/nes/cpu6502_handler.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,5 @@ func (cpu6502 *Cpu6502) logStep(registers cpu.Registers, opcode byte, operand [3
9494
}*/
9595

9696
func (cpu6502 *Cpu6502) Stop() {
97-
//if cpu6502.debug {
98-
// cpu6502.Logger.Close()
99-
//}
10097
cpu6502.debugger.Stop()
10198
}

0 commit comments

Comments
 (0)