This commit is contained in:
Michael Smith 2025-08-13 15:54:10 +02:00
parent 3cb7e3a5c9
commit 69db4fa67c
5 changed files with 110 additions and 6 deletions

35
gb/bus.go Normal file
View File

@ -0,0 +1,35 @@
package gb
// import (
// "fmt"
// )
// type Bus struct {
// Cart *Cartridge
// }
// func NewBus(cart *Cartridge) *Bus {
// bus := Bus{Cart: cart}
// return &bus
// }
// func (bus *Bus) Read(address uint16) (byte, error) {
// if address < 0x8000 {
// // ROM data
// return bus.Cart.Read(address), nil
// } else {
// return 0, fmt.Errorf("Reading from address %X not implemented!", address)
// }
// }
// func (bus *Bus) Write(address uint16, value byte) error {
// if address < 0x8000 {
// // ROM data
// bus.Cart.Write(address, value)
// return nil
// } else {
// return fmt.Errorf("Writing to address %X not implemented!", address)
// }
// }

View File

@ -11,15 +11,19 @@ const (
)
type Console struct {
CPU *CPU
Cartridge *Cartridge
front *image.RGBA
}
func NewConsole(path string) (*Console, error) {
cartridge := InsertCartridge(path)
cartridge, err := InsertCartridge(path)
if err != nil {
return &Console{}, err
}
buffer := image.NewRGBA(image.Rect(0, 0, ConsoleWidth, ConsoleHeight))
console := Console{Cartridge: cartridge, front: buffer}
console := Console{Cartridge: cartridge, CPU: NewCPU(), front: buffer}
return &console, nil
}

45
gb/cpu.go Normal file
View File

@ -0,0 +1,45 @@
package gb
const CPUFrequency = 4194304
type Registers struct {
A byte
F byte
B byte
C byte
D byte
E byte
H byte
L byte
PC uint16
SP uint16
}
type CPU struct {
Regs Registers
FetchedData uint16
MemoryDestination uint16
DestinationIsMemory bool
CurrentOPcode byte
// CurrentInstruction *Instruction
Halted bool
Stepping bool
InterruptMasterEnabled bool
}
func NewCPU() *CPU {
cpu := CPU{}
cpu.Regs = Registers{}
cpu.Stepping = true
return &cpu
}
func (cpu *CPU) Step() bool {
if cpu.Stepping {
cpu.Stepping = false
return true
}
return false
}

25
main.go
View File

@ -1,9 +1,15 @@
package main
import (
"gb-player/ui"
"fmt"
"log"
// "gb-player/ui"
"gb-player/gb"
)
var running = true
func main() {
// FIXME(m): Allow specifying rom file on command line
// if len(os.Args) != 2 {
@ -12,5 +18,20 @@ func main() {
// romPath := os.Args[1]
romPath := "./roms/dmg-acid2.gb"
ui.Run(romPath)
// ui.Run(romPath)
console, err := gb.NewConsole(romPath)
if err != nil {
log.Fatal(err)
}
running := true
for running {
if !console.CPU.Step() {
fmt.Println("CPU stopped")
running = false
} else {
fmt.Println("CPU step")
}
}
}

View File

@ -22,8 +22,7 @@ func (view *View) Update(dt uint64) {
console := view.console
renderer := view.controller.renderer
// console.StepMilliSeconds(dt)
console.StepMilliSeconds(sdl.GetTicks64())
console.StepMilliSeconds(dt)
buffer := console.Buffer()