Creating a Yamaha Checksum Byte in Lua

Hi all,

My first entirely Lua patch is nearly done, for the Yamaha PSS480, I’ve been working away on it for a while.

The PSS 480 sends the whole patch with every control adjustment, so every control uses the same function, which assembles and array of SysEx bytes to transmit. This is all fine and works, thhe issue I have is the last byte before the F7 is a “checksum”.

I’ve hit a bit of a wall of understanding here, as my core maths is quite poor, so I don’t understand what this is saying:

checksum = ((NOT(sum AND 255)) AND 127)+1

Martin Russ in an old SOS issue showed this as the concept of doing it (not code, just a description):

sum = o
checksum = 0
get first byte
   sum = sum + byte
   get next byte
loop for the whole block of data
checksum = ((NOT(sum AND 255)) AND 127)+1

I also found a JS based editor that uses this code for it’s checksum byte generation and here is the context:

  let checksum = 0;
  for (let i = 4; i < 70; i++) {
    checksum += bytes[i] || 0;
  checksum = (~(checksum & 0xFF) + 1) & 0x7F;

I just can’t work out how to translate my array of bytes into that last checksum byte, it’s very opaque to me…

If anyone can help it’d be most appreciated,


Hi all, this seems to be working for me, where sysexArray is my array:

    local checksum = 0
    for i = 5, 70 do
        checksum = checksum + (sysexArray[i] or 0)
    checksum = ((~checksum & 0xFF) + 1) & 0x7F