Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SNA #3

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added support for SNA format
  • Loading branch information
jmbert committed Jan 26, 2024
commit 545778e85f46a9f34f055049d80080594754a87f
23 changes: 21 additions & 2 deletions cmd/zog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import (
"github.com/jbert/zog/speccy"
)

func getFormat(format string) (file.ImageFormat, error) {
switch format {
case "z80":
return &file.Z80Snapshot{}, nil
case "sna":
return &file.SNASnapshot{}, nil
default:
return nil, fmt.Errorf("Unsupported format %s\n", format)
}

}

func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile `file`")
trace := flag.String("trace", "", "Trace addresses: start-end,s2-e2")
Expand All @@ -25,6 +37,7 @@ func main() {
imageFname := flag.String("image", "", "Name of image file (.z80 supported)")
quiet := flag.Bool("quiet", false, "Suppress messages")
mode := flag.String("mode", "run", "Operation mode (run, disassemble)")
format := flag.String("format", "z80", "Image format (z80, sna)")

flag.Parse()

Expand Down Expand Up @@ -86,20 +99,26 @@ func main() {
var runErr error

if *imageFname != "" {
h := file.Z80header{}
h, err := getFormat(*format)
if err != nil {
panic(err)
}
f, err := os.Open(*imageFname)
if err != nil {
panic(err)
}
defer f.Close()
err = file.Z80readHeader(f, &h)
err = h.Parse(f)
if err != nil {
panic(err)
}
err = h.Load(f, z)
if err != nil {
panic(err)
}

fmt.Printf("z.State(): %v\n", z.State())

// JB - TODO hack. Elite z80 file has interrupts off
// (or we aren't parsing it correctly)
// z.LoadInterruptState(zog.InterruptState{IFF1: true, IFF2: true, Mode: 1})
Expand Down
12 changes: 12 additions & 0 deletions file/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package file

import (
"io"

"github.com/jbert/zog"
)

type ImageFormat interface {
Parse(r io.Reader) error
Load(r io.Reader, z *zog.Zog) error
}
90 changes: 90 additions & 0 deletions file/sna.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package file

import (
"encoding/binary"
"fmt"
"io"

"github.com/jbert/zog"
)

type SNASnapshot struct {
I uint8
HLPrime, DEPrime, BCPrime, AFPrime uint16
HL, DE, BC uint16
IY, IX uint16
IFF2 uint8
R uint8
AF, SP uint16
IMODE, BCOL uint8
}

func (s *SNASnapshot) Parse(r io.Reader) error {

binary.Read(r, binary.LittleEndian, s)

return nil
}

func getBytes(r io.Reader, n uint16) ([]byte, error) {
var b []byte = make([]byte, n)
_, err := r.Read(b)
return b, err
}

func (s *SNASnapshot) Load(r io.Reader, z *zog.Zog) error {
fmt.Printf("s: %v\n", s)
var regs zog.Registers

regs.Write16(zog.AF, s.AF)
regs.Write16(zog.BC, s.BC)
regs.Write16(zog.DE, s.DE)
regs.Write16(zog.HL, s.HL)

regs.Write16(zog.AF_PRIME, s.AFPrime)
regs.Write16(zog.BC_PRIME, s.BCPrime)
regs.Write16(zog.DE_PRIME, s.DEPrime)
regs.Write16(zog.HL_PRIME, s.HLPrime)

regs.Write16(zog.SP, s.SP)

regs.Write16(zog.IY, s.IY)
regs.Write16(zog.IX, s.IX)

regs.Write8(zog.I, s.I)
regs.Write8(zog.R, s.R)

z.LoadRegisters(regs)

var state zog.InterruptState
state.Mode = s.IMODE
state.IFF2 = (s.IFF2 == 2)
state.IFF1 = state.IFF2

z.LoadInterruptState(state)

fmt.Printf("z.State(): %v\n", z.State())

var mem []byte = make([]byte, 0xC000)

_, err := r.Read(mem)
if err != nil {
return err
}

err = z.LoadBytes(0x4000, mem)
if err != nil {
return err
}

pc, err := z.Mem.Peek16(z.GetRegisters().SP)
if err != nil {
return err
}
regs = z.GetRegisters()
regs.SP += 2
regs.PC = pc
z.LoadRegisters(regs)

return nil
}
13 changes: 13 additions & 0 deletions file/z80.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (
"github.com/jbert/zog"
)

type Z80Snapshot struct {
Z80header
}

func (z *Z80Snapshot) Parse(r io.Reader) error {
err := Z80readHeader(r, &z.Z80header)
return err
}

func (snap *Z80Snapshot) Load(r io.Reader, z *zog.Zog) error {
return snap.Z80header.Load(r, z)
}

// From http://www.worldofspectrum.org/faq/reference/z80format.htm
type Z80header struct {
A, F byte
Expand Down