Skip to content

Commit

Permalink
fixes #73 - checksums are sanity checked when reading SYN file disk
Browse files Browse the repository at this point in the history
  • Loading branch information
chinenual committed Apr 25, 2022
1 parent 60ca7be commit 1f8fbba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ permalink: /docs/release-notes

* Adds the ability to convert the sequencer events from a SYN file into a MIDI file. See
[SYN to MIDI Converter](https://chinenual.github.io/synergize/docs/syn2midi)

* Fixes [issue #73](https://github.com/chinenual/synergize/issues/73):
SYN files are sanity checked (via its embedded checksum) before loading into the instrument or attempting to convert to MIDI.
## 2.5.0

* Adds the ability convert Yamaha DX7 SYX files to Synergy CRT and VCE
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION=2.6.0-beta1
VERSION=2.6.0-beta2

10 changes: 9 additions & 1 deletion seq/syn2midi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"math"
"time"

"github.com/chinenual/synergize/synio"

"gitlab.com/gomidi/midi/v2"
"gitlab.com/gomidi/midi/v2/smf"

"github.com/chinenual/synergize/logger"

"github.com/chinenual/synergize/data"
"github.com/pkg/errors"
)

// See INTF.Z80 TSTATE and SEQREQ.Z80
Expand All @@ -33,6 +36,11 @@ func GetSYNSequencerState(path string) (trackButtons [4]TrackPlayMode, err error
return
}

if err = synio.CheckSYNCRC(synBytes); err != nil {
err = errors.Wrap(err, "Invalid SYN file - invalid checksum")
return
}

cmosLen := data.BytesToWord(synBytes[1], synBytes[0])
logger.Debugf("CMOS LEN: %v\n", cmosLen)

Expand Down Expand Up @@ -639,7 +647,7 @@ func seqVelocityToSynergy(v uint8) uint8 {
// MIDI velocity to native Synergy velocity (used by unit tests)
// emulate what the firmware does
func midiVelocityToSynergy(v uint8) uint8 {
// MIDI.Z80 convers MIDI velocity in opposite way: (v / 4 + 1)
// MIDI.Z80 converts MIDI velocity in opposite way: (v / 4 + 1)
// YMKEYD:
// ...
// LD A,(HL) ;Get midi velocity
Expand Down
45 changes: 24 additions & 21 deletions synio/synio_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func LoadSYN(bytes []byte) (err error) {
if synioVerbose {
logger.Infof("SYNIO: ** LoadSYN\n")
}
if err = CheckSYNCRC(bytes); err != nil {
err = errors.Wrap(err, "Invalid SYN file - invalid checksum")
return
}
if err = command(OP_STLOAD, "STLOAD"); err != nil {
return
}
Expand Down Expand Up @@ -245,31 +249,30 @@ func SaveSYN() (bytes []byte, err error) {
return
}

// FIXME: these bytes seem out of order vs the length HOB/LOB yet seem to be transmitted the same
// from INTF.Z80 firmware sourcecode - I dont understand something..
crcFromSynergy := data.BytesToWord(crc_buf[0], crc_buf[1])

crcHash.Reset()
bytes = append(len_buf, cmos_buf...)
bytes = append(bytes, seq_buf...)
bytes = append(bytes, crc_buf...)

calcCRCBytes(len_buf)
calcCRCBytes(cmos_buf)
calcCRCBytes(seq_buf)
if synioVerbose {
logger.Infof("SYNIO: CRC from synergy %04x - our calculation %04x\n", crcFromSynergy, crcHash.CRC16())
err = CheckSYNCRC(bytes)
if err == nil {
// errors will implicitly show up in the log but we need to explicitly log success
if synioVerbose {
logger.Infof("SYNIO: STDUMP Success\n")
}
}
return
}

if crcFromSynergy != crcHash.CRC16() {
err = errors.Errorf("STDUMP CRC does not match got %04x, expected %04x",
crcFromSynergy, crcHash.CRC16())
return
}
// errors will implicitly show up in the log but we need to explicitly log success
func CheckSYNCRC(bytes []byte) (err error) {
crcHash.Reset()
calcCRCBytes(bytes[:len(bytes)-2])
crc := crcHash.CRC16()
crcFromSYN := data.BytesToWord(bytes[len(bytes)-2], bytes[len(bytes)-1])
if synioVerbose {
logger.Infof("SYNIO: STDUMP Success\n")
logger.Infof("SYNIO: CRC from synergy %04x - our calculation %04x\n", crc, crcHash.CRC16())
}
if crc != crcFromSYN {
err = errors.Errorf("SYN CRC does not match got %04x, expected %04x", crcFromSYN, crc)
}

bytes = append(len_buf, cmos_buf...)
bytes = append(bytes, seq_buf...)
bytes = append(bytes, crc_buf...)
return
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version = "2.6.0-beta1"
const Version = "2.6.0-beta2"

0 comments on commit 1f8fbba

Please sign in to comment.