From 607e53fcee20c1c42013ba8c564cec6c56501f4d Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Wed, 19 May 2021 10:02:49 +0100 Subject: [PATCH] Recalculate checksum if the link order is updated Fixes #2 --- cmd/psx/main.go | 4 +++- memcard.go | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/psx/main.go b/cmd/psx/main.go index 7fc0f2f..01e2b82 100644 --- a/cmd/psx/main.go +++ b/cmd/psx/main.go @@ -51,8 +51,10 @@ func splitMemoryCard(base string, smc *psx.MemoryCard) error { for { // Copy the directory frame and data block tmc.HeaderBlock.DirectoryFrame[j] = df - if df.LinkOrder != psx.LastLink { + if df.LinkOrder != psx.LastLink && tmc.HeaderBlock.DirectoryFrame[j].LinkOrder != uint16(j+1) { + // Block has moved during the copy tmc.HeaderBlock.DirectoryFrame[j].LinkOrder = uint16(j + 1) + tmc.HeaderBlock.DirectoryFrame[j].UpdateChecksum() } tmc.DataBlock[j] = smc.DataBlock[i] diff --git a/memcard.go b/memcard.go index 7420ae7..569077e 100644 --- a/memcard.go +++ b/memcard.go @@ -68,6 +68,12 @@ type directoryFrame struct { Checksum byte } +func (d *directoryFrame) UpdateChecksum() { + h := XOR() + binary.Write(h, binary.LittleEndian, d.directoryFrameFields) + d.Checksum = h.Sum(nil)[0] +} + type unusedFrame struct { AvailableBlocks byte Reserved [3]byte