diff --git a/gb/bus.go b/gb/bus.go new file mode 100644 index 0000000..c6ebd2e --- /dev/null +++ b/gb/bus.go @@ -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) +// } +// } diff --git a/gb/console.go b/gb/console.go index 6397a4a..3e541cc 100644 --- a/gb/console.go +++ b/gb/console.go @@ -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 } diff --git a/gb/cpu.go b/gb/cpu.go new file mode 100644 index 0000000..d6d1ac8 --- /dev/null +++ b/gb/cpu.go @@ -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 +} diff --git a/main.go b/main.go index 4f5a35e..da822d5 100644 --- a/main.go +++ b/main.go @@ -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") + } + } } diff --git a/ui/view.go b/ui/view.go index d27d0d9..c09ab00 100644 --- a/ui/view.go +++ b/ui/view.go @@ -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()