Skip to content

Commit

Permalink
Have DebugPrint take a logger
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Jan 7, 2024
1 parent 86f9e12 commit 0c05705
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
5 changes: 3 additions & 2 deletions cart/cartridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"

"github.com/maxfierke/gogo-gb/cart/mbc"
"github.com/maxfierke/gogo-gb/mem"
Expand All @@ -23,9 +24,9 @@ func NewCartridge() *Cartridge {
return &Cartridge{}
}

func (c *Cartridge) DebugPrint() {
func (c *Cartridge) DebugPrint(logger *log.Logger) {
if c.mbc != nil {
c.Header.DebugPrint()
c.Header.DebugPrint(logger)
}
}

Expand Down
28 changes: 14 additions & 14 deletions cart/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cart

import (
"encoding/binary"
"fmt"
"log"
)

var be = binary.BigEndian
Expand Down Expand Up @@ -222,20 +222,20 @@ func (hdr *Header) RamSizeBytes() uint {
}
}

func (hdr *Header) DebugPrint() {
fmt.Printf("== Cartridge Info ==\n\n")
func (hdr *Header) DebugPrint(logger *log.Logger) {
logger.Printf("== Cartridge Info ==\n\n")

fmt.Printf("Title: %s\n", hdr.Title)
fmt.Printf("Licensee: %s\n", hdr.Licensee())
fmt.Printf("Color: %s (0x%x)\n", hdr.Cgb(), hdr.cgb)
fmt.Printf("TV-Ready: %s (0x%x)\n", hdr.SgbMode(), hdr.sgb)
fmt.Printf("Cart Type: %s (0x%x)\n", hdr.CartTypeName(), hdr.CartType)
fmt.Printf("ROM Size: %d KiB\n", hdr.RomSizeBytes()/1024)
fmt.Printf("RAM Size: %d KiB\n", hdr.RamSizeBytes()/1024)
fmt.Printf("Destination: %s (0x%x)\n", hdr.Destination(), hdr.destinationCode)
fmt.Printf("Mask ROM Version: 0x%x\n", hdr.maskROMVersion)
fmt.Printf("Header Checksum: 0x%x\n", hdr.HeaderChecksum)
fmt.Printf("Global Checksum: 0x%x\n", hdr.GlobalChecksum)
logger.Printf("Title: %s\n", hdr.Title)
logger.Printf("Licensee: %s\n", hdr.Licensee())
logger.Printf("Color: %s (0x%x)\n", hdr.Cgb(), hdr.cgb)
logger.Printf("TV-Ready: %s (0x%x)\n", hdr.SgbMode(), hdr.sgb)
logger.Printf("Cart Type: %s (0x%x)\n", hdr.CartTypeName(), hdr.CartType)
logger.Printf("ROM Size: %d KiB\n", hdr.RomSizeBytes()/1024)
logger.Printf("RAM Size: %d KiB\n", hdr.RamSizeBytes()/1024)
logger.Printf("Destination: %s (0x%x)\n", hdr.Destination(), hdr.destinationCode)
logger.Printf("Mask ROM Version: 0x%x\n", hdr.maskROMVersion)
logger.Printf("Header Checksum: 0x%x\n", hdr.HeaderChecksum)
logger.Printf("Global Checksum: 0x%x\n", hdr.GlobalChecksum)
}

func (hdr *Header) Licensee() string {
Expand Down
13 changes: 7 additions & 6 deletions cpu/isa/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"strings"
)
Expand Down Expand Up @@ -104,17 +105,17 @@ func (opcodes *Opcodes) InstructionFromByte(addr uint16, value byte, prefixed bo
}, true
}

func (opcodes *Opcodes) DebugPrint() {
fmt.Println("== Opcodes ==")
func (opcodes *Opcodes) DebugPrint(logger *log.Logger) {
logger.Println("== Opcodes ==")

fmt.Printf("=== Unprefixed: \n\n")
logger.Printf("=== Unprefixed: \n\n")
for k := range opcodes.Unprefixed {
fmt.Printf("0x%02X %s\n", k, opcodes.Unprefixed[k].String())
logger.Printf("0x%02X %s\n", k, opcodes.Unprefixed[k].String())
}

fmt.Printf("\n=== Cbprefixed: \n\n")
logger.Printf("\n=== Cbprefixed: \n\n")
for k := range opcodes.CbPrefixed {
fmt.Printf("0x%02X %s\n", k, opcodes.CbPrefixed[k].String())
logger.Printf("0x%02X %s\n", k, opcodes.CbPrefixed[k].String())
}
}

Expand Down
4 changes: 2 additions & 2 deletions hardware/dmg.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (dmg *DMG) LoadCartridge(r *cart.Reader) error {
return dmg.cartridge.LoadCartridge(r)
}

func (dmg *DMG) DebugPrint() {
dmg.cartridge.DebugPrint()
func (dmg *DMG) DebugPrint(logger *log.Logger) {
dmg.cartridge.DebugPrint(logger)
}

func (dmg *DMG) Step() bool {
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ func debugPrintCartHeader(options *CLIOptions) {
logger.Fatalf("Unable to load cartridge. Please ensure it's inserted correctly or trying blowing on it: %v\n", err)
}

cartReader.Header.DebugPrint()
cartReader.Header.DebugPrint(logger)
}

func debugPrintOpcodes(options *CLIOptions) {
logger := options.logger

opcodes, err := isa.LoadOpcodes()
if err != nil {
options.logger.Fatalf("Unable to load opcodes: %v\n", err)
logger.Fatalf("Unable to load opcodes: %v\n", err)
}

opcodes.DebugPrint()
opcodes.DebugPrint(logger)
}

func initDMG(options *CLIOptions) *hardware.DMG {
Expand Down

0 comments on commit 0c05705

Please sign in to comment.