REM > SectorOp
REM Disc sector reader and writer
REM James Bursa - 14 Jun 2000


ON ERROR ON ERROR OFF: REPORT: PRINT ERL: END


REM change these for other FSs
describe$ = "ADFS_DescribeDisc"
sector_op$ = "ADFS_SectorDiscOp"    :REM sometimes <FS>_SectorOp


PRINT "SectorOp - disc sector reader and writer"'
PRINT "WARNING: This program is potentially dangerous and could"
PRINT "cause data loss. If you do not know what you are doing,"
PRINT "press Escape now."'


REPEAT
  INPUT "Drive number: " drive%
  IF drive% < 0 OR drive% > 8 THEN PRINT "Drive must be 0-8"
UNTIL drive% >= 0 AND drive% <= 8

PROCread_record


WHILE 1
  PRINT "[R]ead sector, [W]rite sector, or [Q]uit? ";
  REPEAT
    i$ = GET$
  UNTIL INSTR("rRwWqQ", i$)
  PRINT i$
  CASE i$ OF
    WHEN "r", "R":
      PROCread
    WHEN "w", "W":
      PROCwrite
    WHEN "q", "Q":
      END
  ENDCASE
ENDWHILE

END


REM--------------------------------------------------------------------------------


DEF PROCread
WHILE 1
  PRINT "Read sector number (0-" + STR$(sectors% - 1) + ", blank to end): ";
  INPUT "" sector$
  IF sector$ = "" THEN ENDPROC
  sector% = EVAL(sector$)

  SYS sector_op$,, 1, drive%<<29 OR sector%, M%, secsize%
  PROCdump

  PRINT "Save this sector [y/n]? ";
  REPEAT
    i$ = GET$
  UNTIL INSTR("yYnN", i$)
  PRINT i$
  IF INSTR("yY", i$) THEN
    INPUT "Filename: " file$
    SYS"OS_File", 10, file$, &ffd,, M%, M% + secsize%
  ENDIF
ENDWHILE
ENDPROC


REM--------------------------------------------------------------------------------


DEF PROCread_record
DIM R% 100
SYS describe$, ":" + STR$drive%, R%
big% = (R%?&29 AND 1) = 1
secsize% = 2^R%?00
sectors% = (R%!16 / (secsize% * R%?01 * R%?02)) * R%?01 * R%?02
PRINT " Current disc record contents:"
PRINT "  Bytes/sector: "; secsize%
PRINT "  Sectors/head: "; R%?01
PRINT "  Heads/track: "; R%?02
PRINT "  Tracks/disc: "; (R%!16 / (secsize% * R%?01 * R%?02))
PRINT "  LFAU: "; 2^R%?05
PRINT "  Zones: "; R%?09
IF big% THEN
  PRINT  "  Disc size: &" + STR$~R%!&24 + RIGHT$("00000000" + STR$~R%!16, 8)
  sectors% = 1
ELSE
  PRINT "  Disc size: "; R%!16
ENDIF
DIM M% secsize%
ENDPROC


REM--------------------------------------------------------------------------------


DEF PROCwrite
INPUT "Filename: " file$
SYS"OS_File", 23, file$ TO type%,,,, len%
IF type% <> 1 THEN PRINT "Not a file": ENDPROC
IF len% <> secsize% THEN PRINT "Not sector sized (should be " + STR$secsize% + " bytes)": ENDPROC
SYS"OS_File", 16, file$, M%
PROCdump
PRINT "Write sector number (0-" + STR$(sectors% - 1) + "): ";
INPUT "" sector$
sector% = EVAL(sector$)
PRINT "WARNING: about to write sector " + STR$sector% + ", press O to confirm"
i$ = GET$
IF INSTR("oO", i$) THEN
  SYS sector_op$,, 2, drive%<<29 OR sector%, M%, secsize%
  PRINT "Sector written"
ELSE
  PRINT "Aborted"
ENDIF
ENDPROC


REM--------------------------------------------------------------------------------


DEF PROCdump
FOR a% = 0 TO (secsize% - 64) STEP 64
  a$ = "  "
  FOR b% = 0 TO 63
    c% = M%?(a% + b%)
    IF c% < 32 OR c% = 127 THEN
      a$ += "."
    ELSE
      a$ += CHR$c%
    ENDIF
  NEXT
  PRINT a$
NEXT
ENDPROC