Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit df2183f
Author: pokemium <bluejapan73+dev@gmail.com>
Date:   Wed Apr 7 11:36:34 2021 +0900

    feat: SRAM sav
  • Loading branch information
akatsuki105 committed Apr 7, 2021
1 parent 7299e4e commit ba03c6c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ $ ./build/darwin-amd64/mettaur XXXX.gba
## ToDo

- [ ] Sound
- [ ] SRAM Save
- [ ] Window
- [ ] Mosaic
- [ ] Blend
Expand Down
36 changes: 32 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/hajimehoshi/ebiten/v2"
Expand Down Expand Up @@ -53,8 +54,9 @@ func main() {
// Run program
func Run() ExitCode {
var (
showVersion = flag.Bool("v", false, "show version")
showCartInfo = flag.Bool("c", false, "show cartridge info")
showVersion = flag.Bool("v", false, "show version")
showBIOSIntro = flag.Bool("b", false, "show BIOS intro")
showCartInfo = flag.Bool("c", false, "show cartridge info")
)

flag.Parse()
Expand All @@ -72,15 +74,20 @@ func Run() ExitCode {

emu := &Emulator{
gba: gba.New(data),
rom: path,
}
if *showCartInfo {
fmt.Println(emu.gba.CartInfo())
return ExitCodeOK
}

emu.SetupCloseHandler()
emu.gba.Reset()
// emu.gba.SoftReset()
emu.loadSav()
if *showBIOSIntro {
emu.gba.Reset()
} else {
emu.gba.SoftReset()
}

ebiten.SetWindowResizable(true)
ebiten.SetWindowTitle(title)
Expand Down Expand Up @@ -112,6 +119,7 @@ func readROM(path string) ([]byte, error) {

type Emulator struct {
gba *gba.GBA
rom string
}

func (e *Emulator) Update() error {
Expand All @@ -123,11 +131,16 @@ func (e *Emulator) Update() error {
}
}()
e.gba.Update()
if e.gba.DoSav && e.gba.Frame%60 == 0 {
e.writeSav()
}
return nil
}

func (e *Emulator) Draw(screen *ebiten.Image) {
screen.DrawImage(ebiten.NewImageFromImage(e.gba.Draw()), nil)
}

func (e *Emulator) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 240, 160
}
Expand All @@ -141,3 +154,18 @@ func (e *Emulator) SetupCloseHandler() {
os.Exit(0)
}()
}

func (e *Emulator) writeSav() {
path := strings.ReplaceAll(e.rom, ".gba", ".sav")
os.WriteFile(path, e.gba.RAM.SRAM[:], os.ModePerm)
e.gba.DoSav = false
}

func (e *Emulator) loadSav() {
path := strings.ReplaceAll(e.rom, ".gba", ".sav")
if f, err := os.Stat(path); os.IsNotExist(err) || f.IsDir() {
return
} else if sav, err := os.ReadFile(path); err == nil {
e.gba.LoadSav(sav)
}
}
16 changes: 13 additions & 3 deletions pkg/gba/gba.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ type GBA struct {
RAM ram.RAM
inst Inst
cycle int
frame uint
Frame uint
line int
halt bool
pipe Pipe
timers timer.Timers
dma [4]*DMA
joypad joypad.Joypad
DoSav bool
}

type Pipe struct {
Expand Down Expand Up @@ -180,11 +181,11 @@ func (g *GBA) Update() {
// line 227
g.scanline()

if g.frame%2 == 0 {
if g.Frame%2 == 0 {
g.joypad.Read()
}

g.frame++
g.Frame++
}

func (g *GBA) scanline() {
Expand Down Expand Up @@ -323,3 +324,12 @@ func (g *GBA) CartInfo() string {
ROM size: %s`
return fmt.Sprintf(str, g.CartHeader, util.FormatSize(uint(g.RAM.ROMSize)))
}

func (g *GBA) LoadSav(bs []byte) {
if len(bs) > 65536 {
return
}
for i, b := range bs {
g.RAM.SRAM[i] = b
}
}
5 changes: 3 additions & 2 deletions pkg/gba/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func (g *GBA) setRAM8(addr uint32, b byte, s bool) {
g._setRAM8(addr, b)
}

var ctr = 0

func (g *GBA) _setRAM8(addr uint32, b byte) {
defer func() {
if err := recover(); err != nil {
Expand Down Expand Up @@ -132,6 +130,9 @@ func (g *GBA) _setRAM8(addr uint32, b byte) {
g.GPU.VRAM[ram.VRAMOffset(addr)] = b
case ram.OAM(addr):
g.GPU.OAM[ram.OAMOffset(addr)] = b
case ram.SRAM(addr):
g.RAM.Set8(addr, b)
g.DoSav = true
default:
g.RAM.Set8(addr, b)
}
Expand Down

0 comments on commit ba03c6c

Please sign in to comment.