Skip to content

Commit

Permalink
luabridge: implement MultiWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDunne committed Jun 19, 2021
1 parent 0f19045 commit 1e61a20
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions snes/luabridge/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,55 @@ func (d *Device) MultiReadMemory(ctx context.Context, reads ...snes.MemoryReadRe
return
}

func (d *Device) MultiWriteMemory(ctx context.Context, writes ...snes.MemoryWriteRequest) ([]snes.MemoryWriteResponse, error) {
panic("implement me")
func (d *Device) MultiWriteMemory(ctx context.Context, writes ...snes.MemoryWriteRequest) (rsp []snes.MemoryWriteResponse, err error) {
defer func() {
if err != nil {
rsp = nil
closeErr := d.Close()
if closeErr != nil {
log.Printf("luabridge: close error: %v\n", closeErr)
}
}
}()

deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(readWriteTimeout)
}

rsp = make([]snes.MemoryWriteResponse, len(writes))
for j, write := range writes {
addr := mapping.TranslateAddress(
write.RequestAddress,
write.RequestAddressSpace,
d.Mapping,
sni.AddressSpace_SnesABus,
)

// preallocate enough space to write the whole command:
sb := bytes.NewBuffer(make([]byte, 0, 24 + 4*len(write.Data)))
_, _ = fmt.Fprintf(sb, "Write|%d", addr)
for _, b := range write.Data {
_, _ = fmt.Fprintf(sb, "|%d", b)
}
sb.WriteByte('\n')

// send the command:
var n int
n, err = d.WriteDeadline(sb.Bytes(), deadline)
if err != nil {
return
}
_ = n

rsp[j] = snes.MemoryWriteResponse{
RequestAddress: write.RequestAddress,
RequestAddressSpace: write.RequestAddressSpace,
DeviceAddress: addr,
DeviceAddressSpace: sni.AddressSpace_SnesABus,
Size: len(write.Data),
}
}

return
}

0 comments on commit 1e61a20

Please sign in to comment.