Skip to content

Commit

Permalink
Further reduce the number of things that know about HostInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed May 15, 2024
1 parent cdac38e commit b8693db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
27 changes: 13 additions & 14 deletions devices/serial_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@ func (sc *SerialCtrl) SetClockInternal(enabled bool) {
}

type SerialPort struct {
clk uint
ctrl SerialCtrl
recv byte
buf byte
host HostInterface
clk uint
ctrl SerialCtrl
recv byte
buf byte
cable SerialCable
}

func NewSerialPort(host HostInterface) *SerialPort {
func NewSerialPort() *SerialPort {
return &SerialPort{
host: host,
cable: &NullSerialCable{},
}
}

func (sp *SerialPort) AttachCable(cable SerialCable) {
sp.cable = cable
}

func (sp *SerialPort) Step(cycles uint8, ic *InterruptController) {
if !sp.ctrl.IsTransferEnabled() {
return
Expand Down Expand Up @@ -122,17 +126,12 @@ func (sp *SerialPort) OnWrite(mmu *mem.MMU, addr uint16, value byte) mem.MemWrit
sp.ctrl.Write(value)

if sp.ctrl.IsTransferEnabled() && sp.ctrl.IsClockInternal() {
cable := sp.host.SerialCable()

// TODO(GBC): derive this somehow and factor in GBC speeds when relevant
sp.clk = 8192

err := cable.WriteByte(sp.buf)
if err != nil {
sp.host.Log("Unable to write 0x%02X to serial cable: %v", value, err)
}
_ = sp.cable.WriteByte(sp.buf)

recvVal, err := cable.ReadByte()
recvVal, err := sp.cable.ReadByte()
if err != nil {
sp.recv = 0xFF
} else {
Expand Down
5 changes: 3 additions & 2 deletions hardware/dmg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DMG struct {
debuggerHandler mem.MemHandlerHandle
}

func NewDMG(host devices.HostInterface, opts ...DMGOption) (*DMG, error) {
func NewDMG(opts ...DMGOption) (*DMG, error) {
cpu, err := cpu.NewCPU()
if err != nil {
return nil, err
Expand All @@ -54,7 +54,7 @@ func NewDMG(host devices.HostInterface, opts ...DMGOption) (*DMG, error) {
debugger: debug.NewNullDebugger(),
ic: devices.NewInterruptController(),
lcd: devices.NewLCD(),
serial: devices.NewSerialPort(host),
serial: devices.NewSerialPort(),
timer: devices.NewTimer(),
}

Expand Down Expand Up @@ -122,6 +122,7 @@ func (dmg *DMG) Run(host devices.HostInterface) error {
framebuffer := host.Framebuffer()
defer close(framebuffer)

dmg.serial.AttachCable(host.SerialCable())
dmg.debugger.Setup(dmg.cpu, dmg.mmu)

hostExit := host.Exited()
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,13 @@ func initHost(options *CLIOptions) (host.Host, error) {
return hostDevice, nil
}

func initDMG(hostDevice host.Host, options *CLIOptions) (*hardware.DMG, error) {
func initDMG(options *CLIOptions) (*hardware.DMG, error) {
debugger, err := debug.NewDebugger(options.debugger)
if err != nil {
return nil, fmt.Errorf("unable to initialize Debugger: %w", err)
}

dmg, err := hardware.NewDMG(
hostDevice,
hardware.WithDebugger(debugger),
)
if err != nil {
Expand Down Expand Up @@ -193,7 +192,7 @@ func runCart(options *CLIOptions) error {
return fmt.Errorf("unable to initialize host device: %w", err)
}

dmg, err := initDMG(hostDevice, options)
dmg, err := initDMG(options)
if err != nil {
return err
}
Expand Down

0 comments on commit b8693db

Please sign in to comment.