//
// This is a good example of a scan script:
//

disabledebug;                          // Switch extended logging off

print "^01\15Surface Scan (LBA28, 128GB Limit)"

xprint "Enter start LBA: "
%StartLba = userinput                  // Get some values from keyboard

xprint "Enter end LBA: "
%EndLba = userinput                    // Get some values from keyboard

print ""
print "Start: %StartLba, End: %EndLba."

%CurrentLba = %StartLba                // Setting %CurrentLBA to start position
print "Scanning..."

@scanloop:                             // Label

   if %currentlba > %endlba @endscan   // If we reached the end LBA then
                                       // stop the test.
   %temp = %endlba - %currentlba
   %temp = %temp + 1
   if %temp < 256 @putcmd
   %temp = 0                           // 0 means 256
   @putcmd:
   regs_putlba28 = %currentlba         // Put current LBA number into registers
   r2 = %temp                          // Sector Count. 0 means 256
   r7 = $40                            // Put 0x040 (cmd 40H, verify sectors) into Command register

   waitnbsy                            // Wait for ready
   checkesc                            // Check if <ESC> pressed
   %error = iserror                    // Check for error
   if %error = 0 @noerror
      %error = regs_getlba28           // Get error LBA from registers
      print "\12Error: LBA %error"
      %currentlba = %error + 1         // Restart scan from the next sector after error
      goto @scanloop
   @noerror:

   xprint "\07LBA: \15%currentlba"     // Print without CR

   if %temp > 0 @increment
   %temp = 256
   @increment:
   %currentlba = %currentlba + %temp   // Increment CurrentLBA

   goto @scanloop

@endscan:

print ""
%currentlba = %currentlba - 1
print "Last scanned LBA: %currentlba"
