<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Fri, 27 Oct 2006 00:58:05 +0100
From   : jgh@... (Jonathan Graham Harston)
Subject: Fixed ADFS sector checksum code

>Message-ID: <3lcuj2dthslh96i0qbfkkp0eb4q7dd86fc@...>
 
John Kortink <kortink@...> wrote:
> On 25 Oct 2006 01:28:49 +0100, Jonathan Graham Harston wrote:
> >Sector 1: F9 FF 1F 00 00 .... 00 27 E6 00 03 xx
> >FNsum returns &2A, but actual checksum is &2B.
> 
> No, the actual checksum is &29.
 
Ah ha!
 
I had one of those hand-meets-head moments, and thought I had the
problem. My code was doing:
 
sum=sum+byte:IF sum>255 THEN sum=(sum+1)AND255
 
I suddenly realise that that doesn't do 'ADC byte', it does
'ADD byte:ADC #0' - in other words, it adds the carry /after/ the
current addition, not the carry from the previous addition.
 
So, here's the code:
 
   10DIM mem% 255
   20FOR A%=0 TO 255:mem%?A%=0:NEXT
   30mem%!0=&04F9:mem%?254=3
   40PRINT"Checksum: &";~FNsum(mem%)
   50:
   60FOR A%=0 TO 255:mem%?A%=0:NEXT
   70mem%!0=&1FFFF9:mem%!&FB=&0300E627
   80PRINT"Checksum: &";~FNsum(mem%)
   90:
  100END
  110:
  120DEFFNsum(mem%):LOCAL sum%:sum%=255
  130FOR A%=0 TO 254
  140IF sum%>255:sum%=(sum%+1)AND255
  150sum%=sum%+mem%?A%:NEXT:=sum%AND255
>RUN
Checksum: &0
Checksum: &2A
 
Grrr! Still doesn't give &29 as specified.  Hmmm. The ADFS code
counts downwards. But addition is cummutative, surely it won't
make any difference:
 
> 130FOR A%=254 TO 0 STEP -1
>RUN
Checksum: &0
Checksum: &29
>WOW!
 
So, here is the definitive, correct, working BASIC code to
calculate ADFS checksums:
 
DEFFNadfs_sum(mem%):LOCAL sum%:sum%=&FF
FOR A%=&FE TO 0 STEP -1:IF sum%>255:sum%=(sum%+1)AND255
sum%=sum%+mem%?A%:NEXT:=sum%AND255
 
I shall update HDInit and the ADFS documents appropriately.
 
-- 
J.G.Harston - jgh@...                - mdfs.net/User/JGH
BBC BASIC for Windows and Internationalisation
  See http://mdfs.net/Software/BBCBasic/Windows/ProgTips
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>