home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / UCM_11.ARJ / ucm_11 / GOODIES / STTODSK.ZIP / STTODSK.S next >
Text File  |  1997-05-12  |  12KB  |  500 lines

  1. *
  2. ** STTODSK.ttp -- A utility to convert .ST files (disk images suitable
  3. **                for the PacifiST Atari ST emulator) to Atari ST
  4. **                double-density disks.
  5. **
  6. **
  7. ** Usage:
  8. **       sttodsk <file>
  9. **
  10. **       The utility deduces the disk format (tracks, sectors/track and
  11. **     number of sides) from the size of the .ST file. This means the
  12. **     file must have NO extra pad bytes on the end or any such silliness!
  13. **       No intelligent error checking is done.
  14. **       This utility is hard-coded to use drive A:. Sorry, make it more
  15. **     flexible yourself if you need it.
  16. **
  17. **       Formats that are considered valid for .ST files are any
  18. **     combination of:
  19. **                      80,81 or 82 tracks
  20. **                      9,10,or 11 sectors/track
  21. **                      1 or 2 sides (what, no three-sided disks? 8-)
  22. **
  23. **       If this utility was useful to you, maybe send me a game I don't
  24. **     have? I'll send you one you need in return!
  25. **
  26. **       Keep ST'ing...    -ArghBlarg (same nick on IRC)
  27. *
  28.  
  29.  
  30.   OPT O+
  31.  
  32.   OUTPUT sttodsk.ttp
  33.  
  34.   SECTION TEXT
  35.  
  36. Begin
  37.     move.l    sp,a5        ;get stack base
  38.     move.l    4(a5),a5    ;basepage in a5
  39.     move.l    $C(a5),d0    ;text seg length
  40.     add.l    $14(a5),d0    ;add to that the length of the data..
  41.     add.l    $1c(a5),d0    ;..and the bss segments
  42.     add.l    #$500,d0    ;also add some stack space
  43.  
  44.     move.l    d0,d1
  45.     add.l    a5,d1        ;length + address of basepage
  46.     and.l    #$fffffffc,d1    ;ensure stack is at even addr
  47.     move.l    d1,sp        ;set our stack address
  48.     move.l    d0,-(sp)    ;size of reserved area
  49.     move.l    a5,-(sp)    ;start of basepage (reserved area)
  50.     clr.w    -(sp)        ;dummy arg
  51.     move.w    #$4a,-(sp)    ;MShrink() function (setblock)
  52.     trap    #1            ;GEMDOS
  53.     add.l    #12,sp        ;fix stack
  54.  
  55. *
  56. ** Now we've shrunk used mem. Find args on command line
  57. *
  58. GetArgs
  59.     lea        $80(a5),a4    ;address of command line
  60.     move.b    (a4)+,d7    ;command line is PASCAL-type string; length byte
  61.     ext.w    d7
  62.  
  63.     move.l    d2,-(sp)        ;save during XBIOS call (d0-d2/a0-a2 are unsafe)
  64.     pea        titlemsg
  65.     move.w    #$9,-(sp)
  66.     trap    #1                ;print out msg
  67.     addq.l    #6,sp
  68.     move.l    (sp)+,d2        ;get back regs unsafe during XBIOS call
  69.  
  70.     tst.w    d7
  71.     beq        Terminate
  72.  
  73. *
  74. ** Assume filename is first arg -- null terminate it
  75. *
  76.     move.l    a4,a3
  77. fname_term
  78.     move.b    (a3)+,d0
  79.     cmpi.b    #' ',d0
  80.     bne.s    fname_term
  81.     clr.b    -1(a3)
  82.  
  83.     lea        dta_area,a2    ;where our DTA will be
  84.     move.l    a2,-(sp)
  85.     move.w    #$1a,-(sp)    ;SetDTA()
  86.     trap    #1            ;GEMDOS
  87.     addq.l    #6,sp        ;fix stack
  88.  
  89.     move.w    #0,-(sp)    ;look for regular files
  90.     move.l    a4,-(sp)    ;filename to find..
  91.     move.w    #$4e,-(sp)    ;SFirst()
  92.     trap    #1            ;GEMDOS
  93.     addq.l    #8,sp        ;fix stack
  94.     tst.l    d0            ;file found?
  95.     bmi        file_error    ;nope, return error
  96.  
  97.     move.l    26(a2),d6    ;d6 should now have file's size in bytes
  98.     move.l    d6,fsize    ;store file size for later
  99.  
  100.     move.w    #0,-(sp)    ;read-only access..
  101.     move.l    a4,-(sp)    ;filename
  102.     move.w    #$3d,-(sp)    ;open()
  103.     trap    #1            ;GEMDOS
  104.     addq.l    #8,sp        ;fix stack
  105.     tst.l    d0            ;error?
  106.     bmi        file_error
  107. fopen_ok
  108.     move.w    d0,fhandle    ;store file handle
  109.  
  110.  
  111. *
  112. ** OK, now we have the file's size. Do some math on it to see which
  113. ** disk format it is.
  114. **   Sizes that are considered are: 80, 81 and 82 tracks
  115. **                                  9,10, or 11 sectors/track
  116. **                                  1 or 2 sides
  117. *
  118. Deduce_format
  119.     pea        deducemsg
  120.     move.w    #$9,-(sp)
  121.     trap    #1                ;print out deducemsg
  122.     addq.l    #6,sp
  123.  
  124.     move.l    d6,d5
  125.     move.w    #82,d4
  126.     divu.w    d4,d5            ;see if fsize evenly divides by 79 tracks
  127.     swap    d5                ;check remainder
  128.     tst.w    d5
  129.     beq        found_tracks    ;OK, it's probably 79 tracks
  130.     move.l    d6,d5
  131.     move.w    #81,d4
  132.     divu.w    d4,d5            ;see if fsize evenly divides by 80 tracks
  133.     swap    d5                ;check remainder
  134.     tst.w    d5
  135.     beq        found_tracks    ;OK, it's probably 80 tracks
  136.     move.l    d6,d5
  137.     move.w    #80,d4
  138.     divu.w    d4,d5            ;see if fsize evenly divides by 81 tracks
  139.     swap    d5                ;check remainder
  140.     tst.w    d5
  141.     beq        found_tracks    ;OK, it's probably 81 tracks
  142. *
  143. ** Huh?! Not a usual ST format.
  144. *
  145.     bra        size_error
  146. found_tracks
  147.     swap    d5                ;get quotient (# of tracks) LSW of d5
  148.     subq.w    #1,d4
  149.     move.w    d4,trks            ;save max track number (e.g., 79 = an 80 track disk)
  150.     ext.l    d5
  151.     move.l    d5,d6
  152.     move.w    #9,d4
  153.     divu.w    d4,d5            ;9 sectors per track?
  154.     swap    d5
  155.     tst.w    d5
  156.     beq        found_sectors
  157.     move.l    d6,d5
  158.     move.w    #10,d4
  159.     divu.w    d4,d5            ;10 sectors per track?
  160.     swap    d5
  161.     tst.w    d5
  162.     beq        found_sectors
  163.     move.l    d6,d5
  164.     move.w    #11,d4
  165.     divu.w    d4,d5            ;11 sectors per track?
  166.     swap    d5
  167.     tst.w    d5
  168.     beq        found_sectors
  169. *
  170. ** Huh?! Not a usual ST format.
  171. *
  172.     bra        size_error
  173. found_sectors
  174.     swap    d5                ;get quotient (sectors/track) in LSW of d5
  175.     move.w    d4,sct            ;save sectors/track
  176.     cmpi.w    #512,d5            ;single-sided disk?
  177.     bne.s    maybe_double
  178.     move.w    #1,sides
  179.     bra        found_sides
  180. maybe_double
  181.     cmpi.w    #1024,d5        ;double-sided disk?
  182.     bne    size_error            ;nope?!? wierd disk.
  183.     move.w    #2,sides
  184. found_sides
  185.  
  186. *
  187. ** Now we've determined the whole disk's format.
  188. **
  189. ** Request memory for reading in whole disk image..
  190. ** Read in whole disk image..
  191. ** Format & copy file to disk in A:
  192. *
  193.  
  194. *
  195. ** Get memory for file
  196. *
  197.     move.l    fsize,-(sp)
  198.     move.w    #$48,-(sp)        ;Malloc()
  199.     trap    #1                ;GEMDOS
  200.     addq.l    #6,sp            ;fix stack
  201.     tst.l    d0                ;error getting mem?
  202.     bmi        mem_err
  203.     move.l    d0,memptr
  204.     move.l    d0,a3
  205.  
  206. *
  207. ** Read in entire file
  208. *
  209.     pea        loadmsg
  210.     move.w    #$9,-(sp)
  211.     trap    #1                ;print out msg
  212.     addq.l    #6,sp
  213.  
  214.     move.l    memptr,-(sp)    ;buffer for read data
  215.     move.l    fsize,-(sp)        ;size of file
  216.     move.w    fhandle,-(sp)    ;file handle
  217.     move.w    #$3f,-(sp)        ;Read()
  218.     trap    #1                ;GEMDOS
  219.     lea        12(sp),sp        ;fix stack
  220.     tst.l    d0                ;error?
  221.     bmi        file_error
  222.  
  223. *
  224. ** Format each track (side 0, then side 1 if needed)
  225. ** Then write all sectors for that track
  226. *
  227.     move.w    trks,d2
  228. track_loop
  229.     move.w    sides,d4
  230.     subq.w    #1,d4
  231. fmt_side
  232.     move.l    d2,-(sp)        ;save during XBIOS call (d0-d2/a0-a2 are unsafe)
  233.     pea        formatmsg
  234.     move.w    #$9,-(sp)
  235.     trap    #1                ;print out msg
  236.     addq.l    #6,sp
  237.     move.l    (sp)+,d2        ;get back regs unsafe during XBIOS call
  238.  
  239.     move.l    d2,-(sp)        ;save during XBIOS call (d0-d2/a0-a2 are unsafe)
  240.     move.w    #$e5e5,-(sp)        ;blank pattern
  241.     move.l    #$87654321,-(sp)    ;magic code
  242.     move.w    #1,-(sp)            ;interleave
  243.     move.w    d4,-(sp)            ;side to format
  244.     move.w    d2,d1
  245.     sub.w    trks,d1                ;-(current_track - trks) = track to write
  246.     neg.w    d1
  247.     move.w    d1,-(sp)
  248.     move.w    sct,-(sp)            ;sectors per track
  249.     move.w    #0,-(sp)            ;drive A:
  250.     move.l    #0,-(sp)            ;filler, unused
  251.     pea        fmtbuff                ;address of data to write
  252.     move.w    #10,-(sp)            ;Flopfmt()
  253.     trap    #14                    ;XBIOS
  254.     lea        26(sp),sp
  255.     move.l    (sp)+,d2        ;get back regs unsafe during XBIOS call
  256.     tst.w    d0
  257.     bmi        file_error
  258.  
  259.     dbra    d4,fmt_side            ;double sided disk?
  260.  
  261.     moveq    #0,d5
  262.     move.w    sides,d4
  263. side_loop
  264.     move.w    sct,d3
  265. ;    subq.w    #1,d3
  266.  
  267.     move.l    d2,-(sp)        ;save during XBIOS call (d0-d2/a0-a2 are unsafe)
  268.     pea        writemsg
  269.     move.w    #$9,-(sp)
  270.     trap    #1                ;print out msg
  271.     addq.l    #6,sp
  272.     move.l    (sp)+,d2        ;get back regs unsafe during XBIOS call
  273.  
  274.     move.l    d2,-(sp)        ;save during XBIOS call (d0-d2/a0-a2 are unsafe)
  275.     move.w    d3,-(sp)            ;write all sectors on this side
  276.     move.w    d5,-(sp)            ;side to write
  277.     move.w    d2,d1
  278.     sub.w    trks,d1                ;-(current_track - trks) = track to write
  279.     neg.w    d1
  280.     move.w    d1,-(sp)
  281.     move.w    #1,-(sp)            ;sector to start writing
  282.     move.w    #0,-(sp)            ;drive A:
  283.     clr.l    -(sp)                ;unused parameter
  284.     move.l    a3,-(sp)            ;address of data to write
  285.     move.w    #9,-(sp)            ;Flopwr()
  286.     trap    #14                    ;XBIOS
  287.     lea        20(sp),sp            ;fix stack
  288.     move.l    (sp)+,d2        ;get back regs unsafe during XBIOS call
  289.     tst.w    d0
  290.     bmi        file_error
  291.  
  292.     move.w    sct,d1
  293.     mulu.w    #512,d1                ;total bytes on this side of track
  294.     lea        (a3,d1.w),a3        ;skip data just written
  295.  
  296.     subq.w    #1,d4                ;double sided disk?
  297.     beq.s    sides_done            ;done 1 (or both) sides
  298.     moveq    #1,d5
  299.     bra.s    side_loop            ;do it again for other side
  300.  
  301. sides_done
  302.  
  303.     dbra    d2,track_loop
  304.  
  305. *
  306. ** Close the input file
  307. ** (Probably not needed, but just to be safe)
  308. *
  309.     move.w    fhandle,-(sp)
  310.     move.w    #$3e,-(sp)            ;close()
  311.     trap    #1                    ;GEMDOS
  312.     addq.l    #4,sp                ;fix stack
  313.     tst.l    d0
  314.     bmi        file_error
  315. *
  316. ** Return malloc()ed memory
  317. *
  318.     move.l    memptr,-(sp)
  319.     move.w    #$49,-(sp)
  320.     trap    #1                    ;GEMDOS
  321.     addq.l    #6,sp
  322.     tst.l    d0
  323.     bmi        mem_err
  324. *
  325. ** Terminate
  326. *
  327.     pea        donemsg
  328.     move.w    #$9,-(sp)
  329.     trap    #1                ;print out msg
  330.     addq.l    #6,sp
  331. Terminate
  332.     moveq    #0,d0
  333.     move.w    #$4c,-(sp)    ;Term()
  334.     trap    #1
  335. *--------------------------------------------------------------------------
  336. file_error
  337.     move.w    d0,-(sp)
  338.     pea        fileerrmsg
  339.     move.w    #$9,-(sp)    ;Cconws() (write line)
  340.     trap    #1            ;GEMDOS
  341.     addq.l    #6,sp
  342. ;    move.w    (sp)+,d0
  343.     move.w    d0,-(sp)    ;error number
  344.     move.w    #$4c,-(sp)    ;Term()
  345.     trap    #1            ;GEMDOS
  346.  
  347. param_err
  348.     move.w    d0,-(sp)
  349.     pea        paramerrmsg
  350.     move.w    #$9,-(sp)    ;Cconws() (write line)
  351.     trap    #1            ;GEMDOS
  352.     addq.l    #6,sp
  353. ;    move.w    (sp)+,d0
  354.     move.w    d0,-(sp)    ;error number
  355.     move.w    #$4c,-(sp)    ;Term()
  356.     trap    #1            ;GEMDOS
  357.  
  358. mem_err
  359.     move.w    d0,-(sp)
  360.     pea        memerrmsg
  361.     move.w    #$9,-(sp)    ;Cconws() (write line)
  362.     trap    #1            ;GEMDOS
  363.     addq.l    #6,sp
  364. ;    move.w    (sp)+,d0
  365.     move.w    d0,-(sp)    ;error number
  366.     move.w    #$4c,-(sp)    ;Term()
  367.     trap    #1            ;GEMDOS
  368.  
  369. size_error
  370.     move.w    d0,-(sp)
  371.     pea        sizeerrmsg
  372.     move.w    #$9,-(sp)    ;Cconws() (write line)
  373.     trap    #1            ;GEMDOS
  374.     addq.l    #6,sp
  375. ;    move.w    (sp)+,d0
  376.     move.w    #-1,-(sp)
  377.     move.w    #$4c,-(sp)    ;Term()
  378.     trap    #1            ;GEMDOS
  379.  
  380. *
  381. ** getdrive -- converts a drive specifier into a device number
  382. **             (A: => 0, B: => 1)
  383. **
  384. ** INPUT: (a0) - drive specifier string (one char)
  385. **                 The first char is the only one listened to: E.g., 'A'
  386. **               will be accepted as well as 'A:', 'Apple', etc.
  387. **                 Only 'A' or 'B' give valid results.
  388. **
  389. ** RETURN: d0.w - drive device number, Carry set and -1 for error
  390. **
  391. ** EFFECTS: Affects d0.l
  392. *
  393. getdrive
  394.     moveq    #'A',d0
  395.     cmp.b    (a0),d0
  396.     bne.s    .not_a
  397.     moveq    #0,d0
  398.     bra.s    getdrive_done
  399. .not_a
  400.     moveq    #'B',d0
  401.     cmp.b    (a0),d0
  402.     bne.s    .not_b
  403.     moveq    #1,d0
  404.     bra.s    getdrive_done
  405. .not_b
  406.     moveq    #-1,d0
  407.     ori.b    #1,ccr
  408. getdrive_done
  409.     rts
  410.  
  411. *
  412. ** getdec -- converts an ASCII string (nondigit delimited) into a number
  413. **
  414. ** INPUT: (a0) - drive specifier string (one char)
  415. **                 The first char is the only one listened to: E.g., 'A'
  416. **               will be accepted as well as 'A:', 'Apple', etc.
  417. **                 Only 'A' or 'B' give valid results.
  418. **
  419. ** RETURN: d0.w - decimal number, Carry set if non-space encountered
  420. **
  421. ** EFFECTS: Affects d0.l
  422. **          (a0) is updated past end of a valid number string
  423. *
  424. getdec
  425.     move.l    d1,-(sp)
  426.     moveq    #0,d0
  427. getdec_loop
  428.     move.b    (a0)+,d1
  429.     cmpi.b    #'0',d1
  430.     bcs.s    .getdec_end
  431.     cmpi.b    #':',d1
  432.     bcc.s    .getdec_end
  433.     mulu.w    #10,d0
  434.     subi.b    #'0',d1
  435.     ext.w    d1
  436.     add.w    d1,d0
  437.     bra.s    getdec_loop
  438. .getdec_end
  439.     cmpi.b    #' ',d1
  440.     beq.s    getdec_done
  441. getdec_err
  442.     moveq    #-1,d0
  443.     ori.b    #1,ccr
  444. getdec_done
  445.     move.l    (sp)+,d1
  446.     rts
  447.  
  448.  
  449.   SECTION DATA
  450. ESC        equ    27
  451. titlemsg    dc.b    13,10,ESC,'bO'
  452.             dc.b    "-=-=-= ArghBlarg's .ST to Disk Utility =-=-=-",13,10
  453.             dc.b    "Usage: sttodsk <file>",13,10
  454.             dc.b    "----------------------",13,10
  455.             dc.b    ESC,'b1',"CAUTION",ESC,'bO',": This utility ",ESC,'b1',"FORMATS and OVERWRITES"
  456.             dc.b    ESC,'bO',13,10
  457.             dc.b    "the disk in drive A: immediately upon startup if <file> is",13,10
  458.             dc.b    "found and validated as a real .ST file!",13,10
  459.             dc.b    "  I take no responsibility for damage caused to your",13,10
  460.             dc.b    " floppies by this utility. -ArghBlarg May 12, 1997",13,10
  461.             dc.b    ESC,'bO',13,10
  462.             dc.b    0
  463.  
  464. loadmsg        dc.b    13,10,ESC,'l'
  465.             dc.b    "Reading input file",13,10,0
  466.  
  467. deducemsg    dc.b    "Deducing disk format..",13,10,0
  468.  
  469. formatmsg    dc.b    ESC,'l',ESC,'b2'
  470.             dc.b    "Formatting track-",0
  471.  
  472. writemsg    dc.b    ESC,'l',ESC,'b1'
  473.             dc.b    "Writing track-  ",0
  474.  
  475. donemsg        dc.b    ESC,'l',ESC,'bO'
  476.             dc.b    "Done.",13,10,0
  477.  
  478. sizeerrmsg    dc.b    ESC,'l',ESC,'bO'
  479.             dc.b    "Unknown disk image format!",13,10,0
  480.  
  481. fileerrmsg    dc.b    ESC,'l',ESC,'bO'
  482.             dc.b    "File operation error!",13,10,0
  483.  
  484. paramerrmsg    dc.b    ESC,'l',ESC,'bO'
  485.             dc.b    "Parameter error!",13,10,0
  486.  
  487. memerrmsg    dc.b    ESC,'l',ESC,'bO'
  488.             dc.b    "Memory allocation error!",13,10,0
  489.  
  490.   SECTION BSS
  491. fhandle        ds.w    1
  492. fsize        ds.l    1
  493. memptr        ds.l    1
  494. trks        ds.w    1        ;tracks in the disk
  495. sct            ds.w    1        ;sectors per track
  496. sides        ds.w    1        ;sides of the disk
  497. dta_area    ds.b    44        ;space for DTA
  498. fmtbuff        ds.b    512*11*2
  499.   END
  500.