<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Wed, 25 Oct 2006 16:11:50 +0100
From   : dl.harper@... (David Harper)
Subject: ADFS sector checksums

Jonathan Graham Harston wrote:


> I wrote it that way because I could not get:
>
> DEFFNsum:LOCAL sum%
> FOR A%=0 TO 254:sum%=sum%+mem%?A%:IF sum%>255:sum%=(sum%+1)AND255
> NEXT:=sum%
>
> to give the correct checksum. It was always one off for sector
> one, so I added one, and arbitarily defined it as adding the
> sector number.

This algorithm isn't quite correct. Obviously the calculation is done with 
ADC instructions, not  BASIC, but that does give the clue as to what is 
wrong. The carry bit after the last sum is not included in the checksum 
value. If you want to correct the BASIC algorithm you need to make two 
alterations:

a) You have to do the sum downwards rather than upwards (starting with the 
byte at offset &FE and finishing with the byte at offset 0). The correct 
algorithm is not top-to-bottom symmetric.

b) The extra 1 needs to be added if the previous sum (not the current sum) 
exceeds &FF

Try this on your example: F9 FF 1F 00 00 .... 00 27 E6 00 03 and you get:

    03 + 00 = 03
    03 + E6 = E9
    E9 + 27 = 110 => 10 + Carry
    10 + 00 + 1 = 11
    11 + 00 = 11
    ... ...
    11 + 1F = 30
    30 + FF = 12F => 2F + Carry
    2F + F9 + 1 = 129 => 29 + Carry

Result: 29 - ignore final carry. This is actually the correct value.

David Harper 
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>