diff --git a/DiskCopy42.cpp b/DiskCopy42.cpp index 56d4d0d..5a94736 100644 --- a/DiskCopy42.cpp +++ b/DiskCopy42.cpp @@ -11,6 +11,29 @@ #include +uint32_t DiskCopy42::CheckSum(uint8_t *buffer, unsigned length) +{ + uint32_t checksum = 0; + if (length & 0x01) return -1; + + /* + * checksum starts at 0 + * foreach big-endian 16-bit word w: + * checksum += w + * checksum = checksum rotate right 1 (bit 0 --> bit 31) + */ + + for(unsigned i = 0; i < length; i += 2) + { + checksum += (buffer[i] << 8); + checksum += buffer[i + 1]; + checksum = (checksum >> 1) | (checksum << 31); + //if (checksum & 0x01) checksum = (checksum >> 1) | 0x80000000; + //else checksum >>= 1; + } + return checksum; + +} bool DiskCopy42::Load(const uint8_t *buffer) { diff --git a/DiskCopy42.h b/DiskCopy42.h index a412c67..21fa8ea 100644 --- a/DiskCopy42.h +++ b/DiskCopy42.h @@ -15,6 +15,7 @@ struct DiskCopy42 { bool Load(const uint8_t *buffer); + static uint32_t CheckSum(uint8_t *buffer, unsigned length); char disk_name[64]; uint32_t data_size;