

seti $printhelp = $printhelp
seti $help = $help
if $help = 1
  echo 'WD read CHS'
  echo 'This requires the text variable "file" to be set.'
  echo '"file" is the name of the file to dump the data.'

  if $printhelp = 1
    exit 0
  endif
  echo 'Hit ENTER to continue...'
  userinput $choicestring
  previousscript
endif
echo ""

include good_subroutines

# set a blank string variable to compare with user input
# make sure not to set this anyplace else
sets $null = ""

# set this to itself for use with the menu system
# make sure not to set this anyplace else
# if it was not previously set it will = 0
seti $using_menu = $using_menu

# set the file name to be read from
while 1 = 1
  variablecheck $file
  if $error_level < 16
    seti $ask = 1
  elseif $using_menu = 1
    seti $ask = 1
  endif
  if $ask = 1
    echo "Enter file name:"
    userinput $choicestring
    if $choicestring != $null
      sets $file = $choicestring
      break
    else
      echo "Choice cannot be blank!"
    endif
  else
    break
  endif
done

sets $base_file_name = $file
deletefile $file


# set these for later, and to make it easy to change if needed
seti $read_block_size = 512

seti $cylinder_start = 1
seti $sector_start = 1
seti $head = 0
seti $segment_size = 0x80000

# this is only needed to help keep the script happy
if $segment_size < $read_block_size
  seti $read_block_size = $segment_size
endif
seti $divide_check = $segment_size % 512
if $divide_check != 0
  echo "segment_size is not dividable by 512"
  previousscript
endif
seti $divide_check = $read_block_size % 512
if $divide_check != 0
  echo "read_block_size is not dividable by 512"
  previousscript
endif


#gosub initial_read

#gosub initial_wedge

#gosub start_servo

#gosub read_chs

#gosub stop_servo

#gosub read_cylinders


previousscript
end





subroutine initial_read
  gosub enable_vsc

  seti $initial_read_size = 0x200
  scratchpadsize $initial_read_size
  seti $cylinder = $cylinder_start
  seti $remainder = $initial_read_size
  if $remainder != 0
    seti $write_offset = 0
    seti $read_size = $remainder
    hex
    echo "cylinder=0x" $cylinder "  rsize=0x" $read_size
    decimal
    # vsc to prepare for chs read
    echo "prepare to read chs"
    buffersize 0x200
    clearbuffer
    seti $offsetl = $cylinder & 0xff
    seti $offsetm1 = $cylinder > 8
    seti $offsetm1 = $offsetm1 & 0xff
    seti $offsetm2 = $cylinder > 16
    seti $offsetm2 = $offsetm2 & 0xff
    seti $offseth = $cylinder > 24
    seti $offseth = $offseth & 0xff

    seti $sectorl = $sector_start & 0xff
    seti $sectorh = $sector_start > 8
    seti $sectorh = $sectorh & 0xff

    seti $sizel = $read_size & 0xff
    seti $sizem1 = $read_size > 8
    seti $sizem1 = $sizem1 & 0xff
    seti $sizem2 = $read_size > 16
    seti $sizem2 = $sizem2 & 0xff
    seti $sizeh = $read_size > 24
    seti $sizeh = $sizeh & 0xff
    setbuffer 0
      0c 00  01 00  $offsetl $offsetm1  $offsetm2 $offseth  $head 00  $sectorl $sectorh  $sizel $sizem1 $sizem2 $sizeh
    endbuffer
    hex
    echo "keysector= 0c 00  01 00  " $offsetl " " $offsetm1 " " $offsetm2 " " $offseth "  " $head " 00  " $sectorl " " $sectorh "  " $sizel " " $sizem1 " " $sizem2 " " $sizeh
    setwritepio
    ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
    echo "command= d6 01 be 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check
    # vsc to read chs
    seti $sectors = $read_size / 512
    buffersize $read_size
    clearbuffer
    setreadpio
    echo "reading chs"
    ata28cmd 0xd5 $sectors 0xbf 0x4f 0xc2 0xa0 0xb0
    hex
    echo "command= d5 " $sectors " bf 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check
    # copy the buffer to the scratchpad
    #copybuffertoscratchpad 0 $write_offset $read_size
  endif

  printbuffer 0 $initial_read_size

  gosub disable_vsc
endsubroutine





subroutine initial_wedge
  gosub enable_vsc

  seti $initial_read_size = 0x200
  scratchpadsize $initial_read_size
  seti $cylinder = $cylinder_start
  seti $remainder = $initial_read_size
  if $remainder != 0
    seti $write_offset = 0
    seti $read_size = $remainder
    hex
    echo "cylinder=0x" $cylinder "  rsize=0x" $read_size
    decimal
    # vsc to prepare for chs read
    echo "prepare to read chs"
    buffersize 0x200
    clearbuffer
    seti $offsetl = $cylinder & 0xff
    seti $offsetm1 = $cylinder > 8
    seti $offsetm1 = $offsetm1 & 0xff
    seti $offsetm2 = $cylinder > 16
    seti $offsetm2 = $offsetm2 & 0xff
    seti $offseth = $cylinder > 24
    seti $offseth = $offseth & 0xff

    seti $sectorl = $sector_start & 0xff
    seti $sectorh = $sector_start > 8
    seti $sectorh = $sectorh & 0xff

    seti $sizel = $read_size & 0xff
    seti $sizem1 = $read_size > 8
    seti $sizem1 = $sizem1 & 0xff
    seti $sizem2 = $read_size > 16
    seti $sizem2 = $sizem2 & 0xff
    seti $sizeh = $read_size > 24
    seti $sizeh = $sizeh & 0xff
    setbuffer 0
      11 00  01 00  $offsetl $offsetm1  $offsetm2 $offseth  $head 00  $sectorl $sectorh  01 00  00 00  00 02
    endbuffer
    hex
    echo "keysector= 11 00  01 00  " $offsetl " " $offsetm1 " " $offsetm2 " " $offseth "  " $head " 00  " $sectorl " " $sectorh "  01 00  00 00  00 02"
    setwritepio
    ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
    echo "command= d6 01 be 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check
    # vsc to read chs
    seti $sectors = $read_size / 512
    buffersize $read_size
    clearbuffer
    setreadpio
    echo "reading chs"
    ata28cmd 0xd5 $sectors 0xbf 0x4f 0xc2 0xa0 0xb0
    hex
    echo "command= d5 " $sectors " bf 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check
    # copy the buffer to the scratchpad
    #copybuffertoscratchpad 0 $write_offset $read_size
  endif

  printbuffer 0 $initial_read_size

  gosub disable_vsc
endsubroutine





subroutine read_chs
  gosub enable_vsc

  #scratchpadsize $segment_size
  seti $cylinder = $cylinder_start
  seti $sector = $sector_start
  seti $read_size = $read_block_size
  seti $block_count = $segment_size / $read_block_size
  seti $remainder = $segment_size % $read_block_size
  seti $count = 0
  while $count < $block_count
    hex
    echo "cylinder=0x" $cylinder "  sector=0x" $sector
    decimal
    # vsc to prepare for chs read
    echo "prepare to read chs"
    buffersize 0x200
    clearbuffer
    seti $offsetl = $cylinder & 0xff
    seti $offsetm1 = $cylinder > 8
    seti $offsetm1 = $offsetm1 & 0xff
    seti $offsetm2 = $cylinder > 16
    seti $offsetm2 = $offsetm2 & 0xff
    seti $offseth = $cylinder > 24
    seti $offseth = $offseth & 0xff

    seti $sectorl = $sector & 0xff
    seti $sectorh = $sector > 8
    seti $sectorh = $sectorh & 0xff

    seti $sizel = $read_size & 0xff
    seti $sizem1 = $read_size > 8
    seti $sizem1 = $sizem1 & 0xff
    seti $sizem2 = $read_size > 16
    seti $sizem2 = $sizem2 & 0xff
    seti $sizeh = $read_size > 24
    seti $sizeh = $sizeh & 0xff
    setbuffer 0
      0c 00  01 00  $offsetl $offsetm1  $offsetm2 $offseth  $head 00  $sectorl $sectorh  $sizel $sizem1 $sizem2 $sizeh
    endbuffer
    hex
    echo "keysector= 0c 00  01 00  " $offsetl " " $offsetm1 " " $offsetm2 " " $offseth "  " $head " 00  " $sectorl " " $sectorh "  " $sizel " " $sizem1 " " $sizem2 " " $sizeh
    setwritepio
    ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
    echo "command= d6 01 be 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop

    # vsc to read chs
    seti $sectors = $read_size / 512
    buffersize $read_size
    clearbuffer
    setreadpio
    echo "reading chs"
    ata28cmd 0xd5 $sectors 0xbf 0x4f 0xc2 0xa0 0xb0
    hex
    echo "command= d5 " $sectors " bf 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop
    seti $write_offset = $count * $read_size
    # copy the buffer to the scratchpad
    #copybuffertoscratchpad 0 $write_offset $read_size
    # write the buffer to file
    writebuffer $file 0 $write_offset $read_size
    seti $count = $count + 1
    seti $sector = $sector + 1
  done

  if remainder != 0
    echo "WE SHOULD NEVER GET HERE"
  endif

  gosub disable_vsc
  echo "chs read done"
endsubroutine





subroutine read_wedge
  gosub enable_vsc

  #scratchpadsize $segment_size
  seti $cylinder = $cylinder_start
  seti $sector = $sector_start
  seti $read_size = $read_block_size
  seti $block_count = $segment_size / $read_block_size
  seti $remainder = $segment_size % $read_block_size
  seti $count = 0
  while $count < $block_count
    hex
    echo "cylinder=0x" $cylinder "  sector=0x" $sector
    decimal
    # vsc to prepare for wedge read
    echo "prepare to read wedge"
    buffersize 0x200
    clearbuffer
    seti $offsetl = $cylinder & 0xff
    seti $offsetm1 = $cylinder > 8
    seti $offsetm1 = $offsetm1 & 0xff
    seti $offsetm2 = $cylinder > 16
    seti $offsetm2 = $offsetm2 & 0xff
    seti $offseth = $cylinder > 24
    seti $offseth = $offseth & 0xff

    seti $sectorl = $sector & 0xff
    seti $sectorh = $sector > 8
    seti $sectorh = $sectorh & 0xff

    seti $sizel = $read_size & 0xff
    seti $sizem1 = $read_size > 8
    seti $sizem1 = $sizem1 & 0xff
    seti $sizem2 = $read_size > 16
    seti $sizem2 = $sizem2 & 0xff
    seti $sizeh = $read_size > 24
    seti $sizeh = $sizeh & 0xff
    setbuffer 0
      0c 00  01 00  $offsetl $offsetm1  $offsetm2 $offseth  $head 00  $sectorl $sectorh  01 00  00 00  00 02
    endbuffer
    hex
    echo "keysector= 0c 00  01 00  " $offsetl " " $offsetm1 " " $offsetm2 " " $offseth "  " $head " 00  " $sectorl " " $sectorh "  01 00  00 00  00 02"
    setwritepio
    ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
    echo "command= d6 01 be 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop

    # vsc to read chs
    seti $sectors = $read_size / 512
    buffersize $read_size
    clearbuffer
    setreadpio
    echo "reading wedge"
    ata28cmd 0xd5 $sectors 0xbf 0x4f 0xc2 0xa0 0xb0
    hex
    echo "command= d5 " $sectors " bf 4f c2 a0 b0"
    decimal
    # check if command failed
    gosub status_check_no_stop
    seti $write_offset = $count * $read_size
    # copy the buffer to the scratchpad
    #copybuffertoscratchpad 0 $write_offset $read_size
    # write the buffer to file
    writebuffer $file 0 $write_offset $read_size
    seti $count = $count + 1
    seti $sector = $sector + 1
  done

  if remainder != 0
    echo "WE SHOULD NEVER GET HERE"
  endif

  gosub disable_vsc
  echo "wedge read done"
endsubroutine





subroutine read_cylinders
  seti $cylinder_start = -2
  while $cylinder_start > -128
    sets $file = $base_file_name $cylinder
    deletefile $file
    gosub read_chs
    seti $cylinder_start = $cylinder_start - 1
  done
endsubroutine



subroutine start_servo
  gosub enable_vsc

  # vsc to start servo
  echo "start servo"
  buffersize 0x200
  clearbuffer
  setbuffer 0
    17 00  01 00
  endbuffer
  setwritepio
  ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
  # check if command failed
  gosub status_check
  usleep 10000000
  # vsc to init servo
  echo "init servo"
  buffersize 0x200
  clearbuffer
  setbuffer 0
    17 00  03 00
  endbuffer
  setwritepio
  ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
  # check if command failed
  gosub status_check

  gosub disable_vsc
endsubroutine



subroutine stop_servo
  gosub enable_vsc

  # vsc to park servo
  echo "park servo"
  buffersize 0x200
  clearbuffer
  setbuffer 0
    17 00  05 00
  endbuffer
  setwritepio
  ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
  # check if command failed
  gosub status_check
  usleep 1000000
  # vsc to stop servo
  echo "stop servo"
  buffersize 0x200
  clearbuffer
  setbuffer 0
    17 00  02 00
  endbuffer
  setwritepio
  ata28cmd 0xd6 0x01 0xbe 0x4f 0xc2 0xa0 0xb0
  # check if command failed
  gosub status_check

  gosub disable_vsc
endsubroutine





subroutine status_check
  seti $command_failed = 0
  gosub check_command_status
  if $command_failed = 1
    echo "Command failed!"
    gosub show_sense_data
    gosub show_ata_return_status
    previousscript
  endif
endsubroutine



subroutine status_check_no_stop
  seti $command_failed = 0
  gosub check_command_status
  if $command_failed = 1
    echo "Command failed!"
    gosub show_sense_data
    gosub show_ata_return_status
  endif
endsubroutine



subroutine enable_vsc
  # enable vendor specific commands
  echo "enable vsc"
  buffersize 0
  setreadpio
  ata28cmd 0x45 0x0b 0x00 0x44 0x57 0xa0 0x80
  # check if command failed
  gosub status_check
endsubroutine



subroutine disable_vsc
  # disable vendor specific commands
  echo "disable vsc"
  buffersize 0
  setreadpio
  ata28cmd 0x44 0x0b 0x00 0x44 0x57 0xa0 0x80
  # check if command failed
  gosub status_check
endsubroutine



