home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / lrr.arc / lrr.doc < prev    next >
Text File  |  1992-11-11  |  67KB  |  1,809 lines

  1. =============================================================================
  2. LITTLE RED READER: MS-DOS file reader for the 128 and 1571/81 drives.
  3.  
  4. by Craig Bruce  <csbruce@neumann.uwaterloo.ca>
  5.  
  6. 1. INTRODUCTION
  7.  
  8. This article presents a program that reads MS-DOS files and the root directory
  9. of MS-DOS disks.  The program copies only from drive to drive without
  10. buffering file data internally.  This is simpler and imposes no limits on the
  11. size of the files transferred, although it requires the use of two disk drives
  12. (or a logical drive).  The user-interface code is written in BASIC and
  13. presents a full-screen file selection menu.  The grunt-work code is written in
  14. assembly language and operates at maximum velosity.
  15.  
  16. The Burst Command Instruction Set of the 1571/81 is used to read the MS-DOS
  17. disk blocks and the standard kernel routines are used for outputting the
  18. data.  (I am an operating systems specialist, so I call it a kernEl!)  Thus,
  19. the MS-DOS files must be read from a 1571 or 1581 disk drive, but the output
  20. device may be any disk drive type, the screen or a printer, or a virtual drive
  21. type such as RAMLink, RAMDrive, or RAMDOS (for the REU).  It is interesting to
  22. note that the data can be read in from an MS-DOS disk faster than it can be
  23. written out to a 1571, 1581, or even a RAMDOS file.  A RAMLink can swallow the
  24. data only slightly faster than it can be read.
  25.  
  26. Little Red Reader (LRR) supports double density 3.5" disks formatted with 80
  27. tracks, 9 sectors per track, and 2 sides with a 1581 and 5.25" double density
  28. disks formatted with 40 tracks, 9 sectors per track, and 2 sides with a 1571.
  29. A limit of 128 directory entries and 3 File Allocation Table (FAT) sectors is
  30. imposed.  There must be 2 copies of the FAT and the cluster size may be 1 or 2
  31. sectors.  The sector size must be 512 bytes.
  32.  
  33. Oh, about the name.  It is a play on the name of another MS-DOS file copier
  34. available for the C-128.  "Little" means that it is smaller in scope than the
  35. other program, and "Red" is a different primary color to avoid any legal
  36. complications.  It is also the non-white color of the flag of the country of
  37. origin of this program (no, I am not Japanese).  Also, this program is Public
  38. Domain Software, as is all software I develop for 8-bit Commodore Computers.
  39. Feel free to E-mail me if you have questions or comments about this article.
  40.  
  41. 2. USER GUIDE
  42.  
  43. LOAD and RUN the "lrr.128" BASIC program file.  When the program is first run,
  44. it will display an "initializing" message and will load in the binary machine
  45. language package from the "current" Commodore DOS drive (the current drive is
  46. obtained from PEEK(186) - the last device accessed).  The binary package is
  47. loaded only on the first run and is not reloaded on subsequent runs if the
  48. package ID field is in place.
  49.  
  50. 2.1. MAIN SCREEN
  51.  
  52. The main screen of the program is then displayed.  The main screen of the
  53. program will look something like this:
  54.  
  55.    MS-DEV=9    MS-TYPE=1581    CBM-DEV=8
  56.  
  57.    NUM  S  TRN  TYP  FILENAME  EXT  LENGTH
  58.    ---  -  ---  ---  --------  ---  ------
  59.      1  *  ASC  SEQ  HACK4     TXT  120732
  60.      2     BIN  PRG  RAMDOS    SFX   34923
  61.  
  62.    D=DIRECTORY  M=MS-DEV  F=CBM-DEV Q=QUIT
  63.    T=TOGGLE-COLUMN, C=COPY-FILES, +/- PAGE
  64.  
  65. except that immediately after starting up, "<no files>" will be displayed
  66. rather than filenames.  The "MS-DEV" and "MS-TYPE" fields give the device
  67. number and type of the drive containing the MS-DOS disk to copy from, and the
  68. "CBM-DEV" gives the device number of the drive/virtual drive/character device
  69. to copy file data to.
  70.  
  71. Information about all MS-DOS files in the root directory of the MS-DOS disk is
  72. displayed in columns below the drive information.  "NUM" gives the number of
  73. the MS-DOS file in the directory listing, and "S" indicates whether the file
  74. is "selected" or not.  If the file is selected, an asterisk (*) is displayed;
  75. otherwise, a blank is displayed.  When you later enter Copy Mode, only the
  76. files that have been "selected" are copied.
  77.  
  78. The "TRN" field indicates the character translation scheme to be used when the
  79. file is copied.  A value of "BIN" (binary) means no translation and a value of
  80. "ASC" (ascii) means the file characters are to be translated from MS-DOS ASCII
  81. (or "ASCII-CrLf") to PETSCII.  The "TYP" field indicates the type of
  82. Commodore-DOS file to create for writing the MS-DOS file contents into.  The
  83. possible values are "SEQ" (sequential) and "PRG" (program).  The values of the
  84. TRN and TYP fileds are set independently, so you can copy binary data to SEQ
  85. files and ascii data to PRG files if you wish.
  86.  
  87. The "FILENAME" and "EXT" fields give the filename and extension type of the
  88. MS-DOS files and "LENGTH" gives the exact length of the files in bytes.  Note
  89. that if you perform "ASC" translation on a file, its PETSCII version will have
  90. a shorter length.
  91.  
  92. 2.2. USER COMMANDS
  93.  
  94. The bottom of the screen gives the command summary.  After starting the
  95. program, you will want to setup the MS-DOS and CBM-DOS drives with the "M" and
  96. "F" commands.  Simply press the (letter) key corresponding to the command
  97. name to activate the command.  Pressing M will prompt you for the MS-DOS Drive
  98. Number and the MS-DOS Drive Type.  In both cases, type the number and press
  99. RETURN.  (Sorry for insulting all non-novices out there, but I want to be
  100. complete).  The MS-DOS drive number cannot be the same as the CBM-DOS drive
  101. number (since the program copies from drive-to-drive without internal
  102. buffering).  For the drive type, enter an "8", "81", or "1581" for a 1581
  103. drive or anything else for a 1571 drive.
  104.  
  105. Pressing F will prompt you for the CBM-DOS device number.  You may enter a
  106. number from 0 to 30, except that it must not be the MS-DOS drive number.
  107. Enter a "1" for Cassette Drive (God forbid!), a "3" for the screen, a "4" for
  108. the printer (with an automatic secondary address of 7 (lowercase)), any number
  109. above 7 for a Commodore disk drive or special virtual drive, or a value of "0"
  110. for the special "null" drive.  A CBM-DEV value of 0 will case the program to
  111. read MS-DOS files and do nothing with the output.  You can use this feature to
  112. check out the raw reading speed of the program.
  113.  
  114. After setting up the drives, press D to read in the root directory off the
  115. MS-DOS disk.  The data will come blazing in from the disk but BASIC will take
  116. its good ole time sifting through it.  Filenames are displayed on the screen
  117. as they are scanned in.  The program will (eventually) return to the main
  118. screen and display the formatted file information.  One note: the process of
  119. logging in a 1581 MS-DOS disk takes about 12 seconds (on my 1581, anyway), so
  120. be patient.  An MS-DOS disk will have to be "logged in" every time you change
  121. MS-DOS disks.  (Disks are logged in automatically).
  122.  
  123. A couple of notes about accessing MS-DOS disks: don't try to access a device
  124. that is not present because the machine language routines cannot handle this
  125. error for some reason and will lock up, requiring a STOP+RESTORE.  Also, make
  126. sure that an actual MS-DOS disk is loaded into the drive.  If you accidentally
  127. place Commodore-DOS disk into the MS-DOS drive, the 1581 will report an
  128. invalid boot parameters error (#60), but a 1571 will lock up (since I don't
  129. check the sector size and my burst routines are expecting 512 bytes to come
  130. out of a sector whereas Commodore disks have only 256 bytes per sector).
  131.  
  132. Now you are ready to pick what files you want copied and how you want them
  133. copied.  You will notice that a "cursor" appears in the "S" column of the
  134. first file.  You may move the cursor around with the cursor keys: UP, DOWN,
  135. LEFT, RIGHT, HOME, and CLR.  CLR (SHIFT-HOME) will move the cursor back to the
  136. first file on the first screen.  You can move the cursor among the select,
  137. translation, and file-type columns of all the files.  Pressing a SPACE or a
  138. RETURN will toggle the value of the field that the cursor is on.  To toggle
  139. all of the values of the "cursor" column (including files on all other
  140. screens), press T.  You will notice that moving the cursor around and toggling
  141. fields is a bit sluggish, especially if you are in Slow mode on the 40-column
  142. screen.  Did I mention that this program will run on either the 40 or
  143. 80-column screen?  Toggling an entire column can take a couple of seconds.
  144.  
  145. If there are more than 18 MS-DOS files, you can press the "+" and "-" keys to
  146. move among all of the screens of files.  The cursor movement keys will wrap
  147. around on the current screen.  "+" is page forward, and "-" is page backward.
  148. The screens wrap around too.
  149.  
  150. After you have selected all of the files you want to copy and their translation
  151. and file-type fields have been set, press the C key to go into Copy Mode (next
  152. section).  After copying, you are returned to the main screen with all of the
  153. field settings still intact.  To exit from the program, press Q.
  154.  
  155. 2.3. COPY MODE
  156.  
  157. When you enter copy mode, the screen will clear and the name of each selected
  158. file is displayed as it is being copied.  If an error is encountered on either
  159. the MS-DOS or CBM-DOS drive during copying, an error message will be displayed
  160. and copying will continue (after you press a key for MS-DOS errors).
  161.  
  162. To generate a CBM-DOS filename from an MS-DOS filename, the eight filename
  163. characters are taken (including spaces) and a dot (.) and the three characters
  164. of the extension are appended.  Then, all spaces are removed, and if the name
  165. ends with a dot (.) character, then that dot character is removed as well.  I
  166. think this is fairly reasonable.
  167.  
  168. If there already is a file with the same filename on the CBM-DOS disk, then
  169. you will be prompted if you want to overwrite the file or not.  Entering an
  170. "n" will abort the copying of that file and go on to the next file, and
  171. entering a "y" (or anything else) will cause the CBM-DOS file to be
  172. "scratched" and then re-written.
  173.  
  174. The physical copying of the file is done completely in machine language and
  175. nothing is displayed on the screen while this is happening, but you can follow
  176. things by looking at das blinkin lichtes and listening for clicks and grinds.
  177. You will probably be surprised by the MS-DOS file reading speed (I mean in a
  178. good way).  The disk data is read in whole tracks and cached in memory and the
  179. directory information and the FAT are retained in memory as well.  The result
  180. is that minimal time is spent reading disk data, and no costly seeks are
  181. required for opening a new MS-DOS file.  A result is that small files are
  182. copied one after another very quickly.  You will have to wait, however, on the
  183. relatively slow standard kernel/Commodore-DOS file writing.
  184.  
  185. A few changes had to be made to the program to accomodate the RAMDOS program.
  186. RAMDOS uses memory from $2300 to $3FFF of RAM0, which is not really a good
  187. place for a device driver, and it uses some of the zero-page locations that I
  188. wanted to use.  But, difficulties were overcome.  The importance of RAMDOS
  189. compatibility is that if you only have one disk drive but you have an REU, you
  190. can use RAMDOS to store the MS-DOS files temporarily.  If you only have one
  191. disk drive and no REU, you are SOL (Out of Luck) unless you can get a
  192. RamDisk-type program for an unexpanded 128.  The RAMDOS program is available
  193. from FTP site "ccosun.caltech.edu" in file "/pub/rknop/util128/ramdosii.sfx".
  194. One note I found out about RAMDOS: you cannot use a
  195.  
  196. DOPEN#1,(CF$),U(CD),W
  197.  
  198. with it like you are supposed to be able to; you have to use a
  199.  
  200. DOPEN#1,(CF$+",W"),U(CD)
  201.  
  202. Here is a table of copying speeds for copying from 1571s and 1581s with ASC
  203. and BIN translation modes.  All figures are in bytes/second.  These results
  204. were obtained from copying a 127,280 byte text file (the text of C= Hacking
  205. Issue #3).
  206.  
  207.    FROM  \ TO: "null"     RAMLink     RAMDOS     JD1581     JD1571
  208.    -------+    ------     -------     ------     ------     ------
  209.    81-bin |      5772        3441       2146        n/a        644
  210.    81-asc |      5772        3434       2164        n/a        661
  211.    71-bin |      4323        2991       1949       1821        n/a
  212.    71-asc |      4323        2982       1962       1847        n/a
  213.  
  214. The "null" device is that "0" CBM-DOS device number, and a couple of entries
  215. are "n/a" since I only have one 1571 and one 1581.  Note that my 71 and 81 are
  216. JiffyDOS-ified, so the performance of a stock 71/81 will be poorer.  JiffyDOS
  217. gives about a 2x performance improvement for the standard file accessing calls
  218. (open, close, chrin, chrout).  RAMDOS doesn't seem to be as snappy as you
  219. might think.
  220.  
  221. The "null" figures are quite impressive, but the raw sector reading speed
  222. without the overhead of mucking around with file organization is 6700
  223. bytes/sec for a 1581 and 4600 B/s for a 71.  The reason that the 1571 operates
  224. so quickly is that I use a sector interleave of 4 (which is optimal) for
  225. reading the tracks.  I think that other MS-DOS file copier program uses an
  226. interleave of 1 (which is not optimal).  I lose some of the raw performance
  227. because I copy the file data internally once before outputting it (to simplify
  228. some of the code).
  229.  
  230. In a couple of places you will notice that ASC translation gives slightly
  231. better or slightly worse performance than BIN.  This is because although
  232. slightly more work is required to translate the characters, slightly fewer
  233. characters will have to be written to the CBM-DOS file, since PETSCII uses
  234. only CR where MS-DOS ASCII uses CR and LF to represent end-of-line.
  235. Translation is done by using a table (that you can change if you wish).  Many
  236. entries in this table contain a value of zero, which means that no character
  237. will be output on translation.  Most of the control characters and all of the
  238. characters of value 128 (0x80) or greater are thrown away on being
  239. translated.  The table is set up so that CR characters are thrown away and the
  240. LF character is translated to a CBM-DOS CR character.  Thus, both MS-DOS ASCII
  241. files and UNIX ASCII files can be translated correctly.
  242.  
  243. 2. BURST COMMANDS
  244.  
  245. Three burst commands from the 1571/81 disk drive Burst Command Instruction Set
  246. are required to allow this program to read the MS-DOS disks: Query Disk
  247. Format, Sector Interleave, and Read.  The grungy details about issuing burst
  248. commands and burst mode handshaking are covered in C= Hacking Issue #3.  The
  249. Query Disk Format command is used to "log in" the MS-DOS disk.  The Inquire
  250. Disk burst command cannot be used with an MS-DOS disk on the 1581 for some
  251. unknown reason.  I found this out the hard way.  The Query Disk Format command
  252. has the following format:
  253.  
  254.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  255.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  256.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  257.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  258.      2    |     F  |  X  |  X  |  S  |  1  |  0  |  1  |  N  |  10
  259.    -------+--------------------------------------------------+-------
  260.  
  261. where the F, S, and N bits have a value of 0 for our purposes.  A response of
  262. a burst status byte and six other throw-away bytes is given from the drive.
  263. This command takes quite a long time to execute on my 1581 but works quite
  264. quickly on my 1571.  You only have to log in a disk whenever you change
  265. disks.
  266.  
  267. The Sector Interleave command is used to set a soft interleave for the Read
  268. command.  I use an interleave of 1 for the 1581 and an interleave of 4 for the
  269. 1571.  This means that the MS-DOS sectors will come from 1571 to the computer
  270. in the following order: 1, 5, 9, 4, 8, 3, 7, 2, 6 (there are 9 sectors per
  271. track on an MS-DOS disk (both 3.5" and 5.25"), numbered from 1 to 9).  LRR
  272. handles the data coming in in this order, and in straight order from the
  273. 1581.  The Sector Interleave command has the following format, where the W and
  274. N bits are 0 for us:
  275.  
  276.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  277.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  278.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  279.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  280.      2    |     W  |  X  |  X  |  0  |  1  |  0  |  0  |  N  |   8
  281.      3    |                   <interleave>                   | 1 or 4
  282.    -------+--------------------------------------------------+-------
  283.  
  284. The Read command is used to transfer the nine sectors of a track to the
  285. computer in the order specified by the interleave.  The format is:
  286.  
  287.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  288.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  289.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  290.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  291.      2    |    T/L |  E  | B/X |  S  |  0  |  0  |  0  |  N  | 0 or 16
  292.      3    |                      <track>                     |  ???
  293.      4    |                      <sector>                    |   1
  294.      5    |                <number of sectors>               |   9
  295.    -------+--------------------------------------------------+-------
  296.  
  297. There are a couple of differences between the 1571 and 1581 versions of this
  298. command.  Most important, the S bit (Side of disk to use) has the opposite
  299. meaning on the two drives.  There's no good reason that I know of for this
  300. inconsistency.  This is the reason that LRR needs to know what type of MS-DOS
  301. drive it is dealing with (plus interleaving).
  302.  
  303. The read command returns the following data using burst mode handshaking:
  304.  
  305.        +-------------------+
  306.      0 | Burst Status Byte |
  307.        +-------------------+
  308.      1 |                   |
  309.    ... +  512 Data Bytes   |
  310.    512 |                   |
  311.        +-------------------+
  312.  
  313. for each sector transferred.  If the Burst Status Byte indicates an error,
  314. then the data is not transferred and none of the following sectors are
  315. either.  If the status byte gives a "Disk Changed" error, then you have to log
  316. in the disk with the Query Disk Format command before read will work
  317. properly.  This is actually a good feature since it lets you know about a disk
  318. change so you can update any data structures you may have.  LRR simply re-logs
  319. in the disk without updating any data structures and re-tries the failed read
  320. operation.
  321.  
  322. 3. MS-DOS DISK FORMAT
  323.  
  324. An MS-DOS disk is separated into 4 different parts: the Boot Sector, the
  325. FAT(s), the Root Directory, and the File Data Sectors.  The logical sectors
  326. (blocks) of a disk are numbered from 0 to some maximum number (1439 for a
  327. 3.5", 719 for a 5.25" DD disk).  The physical layout and the logical sector
  328. numbers typically used by a 3.5" disk are shown here:
  329.  
  330.          +-------------------+
  331.        0 |    Boot Sector    |
  332.          +-------------------+
  333.     1..3 |    FAT copy #1    |
  334.          +-------------------+
  335.     4..6 |    FAT copy #2    |
  336.          +-------------------+
  337.    7..14 |  Root Directory   |
  338.          +-------------------+
  339.       15 |                   |
  340.      ... | File Data Sectors |
  341.     1439 |                   |
  342.          +-------------------+
  343.  
  344. 3.1. THE BOOT SECTOR
  345.  
  346. The Boot Sector is always at logical sector number 0.  It contains some
  347. important information about the format of the disk and it also contains code
  348. to boot an MS-DOS machine from.  We aren't concerned with the bootstrapping
  349. code, but the important values we need to obtain from the boot sector are:
  350.  
  351.    ABBR     OFFSET     1571     1581     DESCRIPTION
  352.    ----     ------     ----     ----     -----------
  353.     CS        13          2        2     Cluster size in sectors
  354.     NB        14          1        1     Number of boot sectors
  355.     NF        16          2        2     Number of FATs
  356.     FL        23          2        3     FAT size in sectors
  357.     DE        17        112      112     Number of root directory entries
  358.     TS       19,20      720     1440     Total Number of sectors
  359.     NS        24          9        9     Number of sectors per track
  360.     NH        26          2        2     Number of sides
  361.  
  362. The 1571 and 1581 columns give the typical values of these parameters for
  363. 5.25" and 3.5" disks.  The OFFSET is the address of the parameter within the
  364. boot sector.  The total number of sectors is given in low-byte, high-byte
  365. order (since the 80x86 family is little-endian like the 6502 family).  From
  366. the above parameters, we can derive the following important parameters:
  367.  
  368.    ABBR     FORMULA       1571     1581     DESCRIPTION
  369.    ----      -------      ----     ----     -----------
  370.     F1      NB+NF*FL         5        7     First root directory sector
  371.     FS     NB+NF*FL+DE      12       14     First file data sector number
  372.     NC     (TS-FS)/CS      354      713     Total number of file clusters
  373.  
  374. LRR imposes a number of limits on these parameters and will error-out if you
  375. try to use a disk that is outside of LRR's limits.
  376.  
  377. 3.2. CHEWING THE FAT
  378.  
  379. MS-DOS disks use a data structure called a File Allocation Table (FAT) to
  380. record which clusters belong to which file in what order and which blocks are
  381. free.  A cluster is a set of contiguous sectors which are allocated to files
  382. as a group.  LRR handles cluster sizes of 1 and 2 sectors, giving a logical
  383. file block size of 512 or 1024 bytes.  Typically, a cluster size of 2 sectors
  384. is used.
  385.  
  386. The FAT is an array of 12-bit numbers, with an entry corresponding to each
  387. cluster that can be allocated to files.  FAT entries 0 and 1 are reserved.  If
  388. a FAT entry contains a value of $000, then the corresponding cluster is free
  389. and can be allocated to a file; otherwise, the cluster is allocated and the
  390. FAT entry contains the number of the NEXT FAT entry that belongs to the file.
  391. Thus, MS-DOS files are stored in a singly-linked list of clusters like
  392. Commodore-DOS files are, except that the links are not in the data sectors but
  393. rather are in the FAT.  The pointer to the first FAT entry for a file is given
  394. in the file's directory entry.
  395.  
  396. A special NULL/NIL pointer value of $FFF is used to indicate the end of the
  397. chain of clusters allocated to a file.  This value is stored in the FAT entry
  398. of the last cluster allocated to a file (of course).  Consider the following
  399. example FAT:
  400.  
  401.    ENTRY       VALUE
  402.    -----       -----
  403.     $000        $FFF
  404.     $001        $FFF
  405.     $002   |----$003 <------Directory Entry
  406.     $003   +--> $005----+
  407.     $004        $000    |
  408.     $005        $FFF <--+
  409.  
  410. Entries 0 and 1 are insignificant since they are reserved.  Say that a file
  411. starts at FAT entry #2.  Then, it consists of the following chain of clusters:
  412. 2, 3, and 5.  Cluster #4 is free.  Clusters can be allocated to a file in
  413. random order, but if they are allocated contiguously in forward order, then
  414. the file will be able to be read faster.  The FAT is such an important data
  415. structure that typically two copies are kept on the disk incase one of them
  416. should become corrupted.
  417.  
  418. The MS-DOS designers were a little sneaky in storing the 12-bit FAT entries -
  419. they used only 12 real bits per entry.  Ie., they store two FAT entries in
  420. three bytes, where the two entries share the two nybbles of the middle byte.
  421. The following diagram shows how the nybbles 1 (high), 2 (mid), and 3 (low) are
  422. stored into FAT entries A and B:
  423.  
  424.      BYTE:       0            1            2
  425.              +---+---+    +---+---+    +---+---+
  426.     ENTRY:   | A | A |    | B | A |    | B | B |
  427.    NYBBLE:   | 2 | 3 |    | 3 | 1 |    | 1 | 2 |
  428.              +---+---+    +---+---+    +---+---+
  429.  
  430. Anyway, let's just say it's a bit tricky to extract the 12-bit values from
  431. this compressed data structure.  On top of that, I don't think there is any
  432. saving in disk space resulting from compressing this structure; they might as
  433. well have just used a 16-bit FAT (like they do nowadays on larger disks).
  434.  
  435. 3.3. THE ROOT DIRECTORY
  436.  
  437. The root directory has a fixed size, although I don't think that
  438. subdirectories do.  LRR cannot access subdirectories.  Each 512-byte sector of
  439. the root directory contains sixteen 32-byte directory entries.  One directory
  440. entry is required for each file stored on the disk.  A directory entry has the
  441. following structure:
  442.  
  443.    OFFSET     LEN     DESCRIPTION
  444.    ------     ---     -----------
  445.      0..7       8     Filename
  446.     8..10       3     Extension
  447.        11       1     <unused?>
  448.        12       1     Attributes: $10=Directory, $08=VolumeId
  449.    13..21       9     <unused>
  450.    22..25       4     Date
  451.    26..27       2     Starting FAT entry number
  452.    28..31       4     File length in bytes
  453.  
  454. The filename and extension are stored with trailing padding spaces.  If a
  455. directory entry is unused or deleted, then the first character of the filename
  456. is either a $00 or a $E5 (229).  This is why you have to provide the first
  457. character of a filename if you are undeleting a file on an MS-DOS machine.
  458. Note that there is enough unused space that Microsoft or IBM could have
  459. ditched the annoying 8+3 character filename format.
  460.  
  461. The attributes bits tell whether the directory entry is for a regular file, a
  462. subdirectory, a disk volume name (in which case there is no file data), and a
  463. couple of other things I can't remember.  I'm not sure about the exact
  464. position or format of the date, but LRR doesn't use it anyway.  The starting
  465. FAT entry number and the file length are stored in lower-byte-first order.
  466.  
  467. 3.4. THE FILE DATA SPACE
  468.  
  469. The ramainder of the disk space is used for storing file data in clusters of 1
  470. or 2 sectors each.  Given a cluster number (which is also the FAT entry
  471. number), the following formula is used to calculate the starting logical
  472. sector number of the cluster:
  473.  
  474. (ClusterNumber - 2) * ClusterSizeInBlocks + FirstFileDataLogicalSectorNumber
  475.  
  476. where "FirstFileDataLogicalSectorNumber" is the "FS" parameter derived
  477. earlier.  The following consecutive logical sector numbers up to the number of
  478. sectors per cluster form the rest of the cluster.  Note that a single cluster
  479. can span sectors from one side of the disk to another or from one track to
  480. another.  We perform the "(ClusterNumber - 2)" portion of the calculation
  481. since the first two FAT entries are reserved.
  482.  
  483. Since the Read burst command of the 1571/81 wants the side, track, and sector
  484. number of a sector rather than its logical number, we also need formulae for
  485. these conversions:
  486.  
  487.    Track  = LogicalSectorNumber / 18
  488.    Sector = LogicalSectorNumber % 9 + 1
  489.    Side   = (LogicalSectorNumber / 9) % 2
  490.  
  491. These formulae are more problematic than the previous one since they require
  492. division by 9 and 18.  LRR uses the method of repeated subtraction to perfrom
  493. the necessary division (only one division is necessary).  The above formulae
  494. imply that sequential logical sectors are stored on the top of the disk
  495. first and then the bottom of the disk of the same track, and then on the top
  496. of the next track, etc.  This is a good sector numbering scheme (unlike the
  497. CBM-DOS scheme for 1571 sectors) since it is faster to switch sides of the
  498. disk than it is to switch tracks, so you can read the disk faster.
  499.  
  500. Oh yeah, the way that you know how many file data bytes are in the last
  501. cluster of a file chain (the cluster with the NULL FAT entry) is to take the
  502. file length from the directory entry and "mod" (the C language % operator) it
  503. with the cluster size.  One special case is if this calculation results in a
  504. zero, then the last cluster is completely full (rather than completely empty
  505. as the calculation would suggest).  This calculation is easily done in
  506. machine language with an AND operation since the cluster size is always a
  507. power of two.
  508.  
  509. 4. FILE COPYING PACKAGE
  510.  
  511. This section discusses the interface to and implementation of the MS-DOS file
  512. copying package.  It is written in assembly language and is loaded into memory
  513. at address $8000 on bank 0 and requires about 13K of memory.  The package is
  514. loaded at this high address to be out of the way of the main BASIC program,
  515. even if RAMDOS is installed.
  516.  
  517. 4.1. INTERFACE
  518.  
  519. The subroutine call and parameter passing interface to the file copying
  520. package is summarized as follows:
  521.  
  522.    ADDRESS     DESCRIPTION
  523.    -------     -----------
  524.    PK          InitPackage subroutine
  525.    PK+3        LoadDirectory subroutine
  526.    PK+6        CopyFile subroutine
  527.    PK+9        two-byte package identification number
  528.    PK+15       errno : error code returned
  529.    PK+16       MS-DOS device number (8 to 30)
  530.    PK+17       MS-DOS device type ($00=1571, $FF=1581)
  531.    PK+18       two-byte starting cluster number for file copying
  532.    PK+20       low and mid bytes of file length for copying
  533.  
  534. where "PK" is the load address of the package.  Additional subroutine
  535. parameters are passed in the processor registers.
  536.  
  537. The "InitPackage" subroutine should be called when the package is first
  538. installed, whenever the MS-DOS device number is changed, and whenever a new
  539. disk is mounted to invalidate the internal track cache.  It requires no
  540. parameters.
  541.  
  542. The "LoadDirectory" subroutine will load the directory, FAT, and the Boot
  543. Sector parameters into the internal memory of the package from the current
  544. MS-DOS device number.  No (other) input parameters are needed and the
  545. subroutine returns a pointer to the directory space in the .AY registers and
  546. the number of directory entries in the .X register.  If an error occurs, then
  547. the subroutine returns with the Carry flag set and the error code is available
  548. in the "errno" interface variable.  The directory entry data is in the
  549. directory space as it was read in raw from the directory sectors on the MS-DOS
  550. disk.
  551.  
  552. The "CopyFile" subroutine will copy a single file from the MS-DOS disk to a
  553. specified CBM-Kernal logical file number (the CBM file must already be
  554. opened).  If the CBM logical file number is zero, then the file data is simply
  555. discarded after it is read from the MS-DOS file.  The starting cluster number
  556. of the file to copy and the low and mid bytes of the file length are passed in
  557. the PK+18 and PK+20 interface words.  The translation mode to use is passed in
  558. the .A register ($00=binary, $FF=ascii) and the CBM logical file number to
  559. output to is passed in the .X register.  If an error occurs, the routine
  560. returns with the Carry flag set and the error code in the "errno" interface
  561. variable.  There are no other output parameters.
  562.  
  563. Note that since the starting cluster number and low-file length of the file to
  564. be copied are required rather than the filename, it is the responsibility of
  565. the front-end application program to dig through the raw directory sector data
  566. to get this information.  The application must also open the Commodore-DOS
  567. file of whatever filetype on whatever device is required; the package does not
  568. need to know the Commodore-DOS device number.
  569.  
  570. The MS-DOS device number and device type interface variables allow you to set
  571. the MS-DOS drive and the package identification number allows the application
  572. program to check if the package is already loaded into memory so that it only
  573. has to load the package the first time the application is run and not on
  574. re-runs.  The identification sequence is a value of $CB followed by a value
  575. of 131.
  576.  
  577. 4.2. IMPLEMENTATION
  578.  
  579. This section presents the code that implements the MS-DOS file reading
  580. package.  It is here in a special form; each code line is preceded by a few
  581. special characters and the line number.  The special characters are there to
  582. allow you to easily extract the assembler code from the rest of this magazine
  583. (and all of my ugly comments).  On a Unix system, all you have to do is
  584. execute the following command line (substitute filenames as appropriate):
  585.  
  586. grep '^\.%...\!' Hack4 | sed 's/^.%...\!..//' | sed 's/.%...\!//' >lrr.s
  587.  
  588. You'll notice that the initial comment lines here were an afterthought.
  589.  
  590. .%000!  ;Little Red Reader MS-DOS file copier program
  591. .%000!  ;written 92/10/03 by Craig Bruce for C= Hacking Net Magazine
  592. .%000!
  593.  
  594. The code is written for the Buddy assembler and here are a couple setup
  595. directives.  Note that my comments come before the section of code.
  596.  
  597. .%001!  .org $8000
  598. .%002!  .obj "@:lrr.bin"
  599. .%003!
  600. .%004!  ;====jump table and parameters interface ====
  601. .%005!
  602. .%006!  jmp initPackage
  603. .%007!  jmp loadDirectory
  604. .%008!  jmp copyFile
  605. .%009!
  606. .%010!  .byte $cb,131   ;identification
  607. .%011!  .byte 0,0,0,0
  608. .%012!
  609.  
  610. These variables are included in the package program space to minimize unwanted
  611. interaction with other programs loaded at the same time, such as the RAMDOS
  612. device driver.
  613.  
  614. .%013!  errno           .buf 1    ;(location pk+15)
  615. .%014!  sourceDevice    .buf 1
  616. .%015!  sourceType      .buf 1    ;$00=1571, $ff=1581
  617. .%016!  startCluster    .buf 2
  618. .%017!  lenML           .buf 2    ;length medium and low bytes
  619. .%018!
  620. .%019!  ;====global declaraions====
  621. .%020!
  622. .%021!  kernelListen = $ffb1
  623. .%022!  kernelSecond = $ff93
  624. .%023!  kernelUnlsn  = $ffae
  625. .%024!  kernelAcptr  = $ffa2
  626. .%025!  kernelCiout  = $ffa8
  627. .%026!  kernelSpinp  = $ff47
  628. .%027!  kernelChkout = $ffc9
  629. .%028!  kernelClrchn = $ffcc
  630. .%029!  kernelChrout = $ffd2
  631. .%030!
  632. .%031!  st = $d0
  633. .%032!  ciaClock = $dd00
  634. .%033!  ciaFlags = $dc0d
  635. .%034!  ciaData  = $dc0c
  636. .%035!
  637.  
  638. These are the parameters and derived parameters from the boot sector.  They
  639. are kept in the program space to avoid interactions.
  640.  
  641. .%036!  clusterBlockCount .buf 1        ;1 or 2
  642. .%037!  fatBlocks         .buf 1        ;up to 3
  643. .%038!  rootDirBlocks     .buf 1        ;up to 8
  644. .%039!  rootDirEntries    .buf 1        ;up to 128
  645. .%040!  totalSectors      .buf 2        ;up to 1440
  646. .%041!  firstFileBlock    .buf 1
  647. .%042!  firstRootDirBlock .buf 1
  648. .%043!  fileClusterCount  .buf 2
  649. .%044!
  650.  
  651. The cylinder (track) and side that is currently stored in the trach cache.
  652.  
  653. .%045!  bufCylinder     .buf 1
  654. .%046!  bufSide         .buf 1
  655. .%047!  formatParms     .buf 6
  656. .%048!
  657.  
  658. This package is split into a number of levels.  This level interfaces with the
  659. Kernal serial bus routines and the burst command protocol of the disk drives.
  660.  
  661. .%049!  ;====hardware level====
  662. .%050!
  663.  
  664. Connect to the MS-DOS device and send the "U0" burst command prefix and the
  665. burst command byte.
  666.  
  667. .%051!  sendU0 = *  ;( .A=burstCommandCode ) : .CS=err
  668. .%052!     pha
  669. .%053!     lda #0
  670. .%054!     sta st
  671. .%055!     lda sourceDevice
  672. .%056!     jsr kernelListen
  673. .%057!     lda #$6f
  674. .%058!     jsr kernelSecond
  675. .%059!     lda #"u"
  676. .%060!     jsr kernelCiout
  677. .%061!     bit st
  678. .%062!     bmi sendU0Error
  679. .%063!     lda #"0"
  680. .%064!     jsr kernelCiout
  681. .%065!     pla
  682. .%066!     jsr kernelCiout
  683. .%067!     bit st
  684. .%068!     bmi sendU0Error
  685. .%069!     clc
  686. .%070!     rts
  687. .%071!
  688. .%072!     sendU0Error = *
  689. .%073!     lda #5
  690. .%074!     sta errno
  691. .%075!     sec
  692. .%076!     rts
  693. .%077!
  694.  
  695. Toggle the "Data Accepted / Ready For More" clock signal for the burst
  696. transfer protocol.
  697.  
  698. .%078!  toggleClock = *
  699. .%079!     lda ciaClock
  700. .%080!     eor #$10
  701. .%081!     sta ciaClock
  702. .%082!     rts
  703. .%083!
  704.  
  705. Wait for a burst byte to arrive in the serial data register of CIA#1 from the
  706. fast serial bus.
  707.  
  708. .%084!  serialWait = *
  709. .%085!     lda #$08
  710. .%086!  -  bit ciaFlags
  711. .%087!     beq -
  712. .%088!     rts
  713. .%089!
  714.  
  715. Wait for and get a burst byte from the fast serial bus, and send the "Data
  716. Accepted" signal.
  717.  
  718. .%090!  getBurstByte = *
  719. .%091!     jsr serialWait
  720. .%092!     ldx ciaData
  721. .%093!     jsr toggleClock
  722. .%094!     txa
  723. .%095!     rts
  724. .%096!
  725.  
  726. Send the burst commands to "log in" the MS-DOS disk and set the Read sector
  727. interleave factor.
  728.  
  729. .%097!  mountDisk = *  ;() : .CS=err
  730. .%098!     lda #%00011010
  731. .%099!     jsr sendU0
  732. .%100!     bcc +
  733. .%101!     rts
  734. .%102!  +  jsr kernelUnlsn
  735. .%103!     bit st
  736. .%104!     bmi sendU0Error
  737. .%105!     clc
  738. .%106!     jsr kernelSpinp
  739. .%107!     bit ciaFlags
  740. .%108!     jsr toggleClock
  741. .%109!     jsr getBurstByte
  742. .%110!     sta errno
  743. .%111!     and #$0f
  744. .%112!     cmp #2
  745. .%113!     bcs mountExit
  746.  
  747. Grab the throw-away parameters from the mount operation.
  748.  
  749. .%114!     ldy #0
  750. .%115!  -  jsr getBurstByte
  751. .%116!     sta formatParms,y
  752. .%117!     iny
  753. .%118!     cpy #6
  754. .%119!     bcc -
  755. .%120!     clc
  756.  
  757. Set the sector interleave to 1 for a 1581 or 4 for a 1571.
  758.  
  759. .%121!     ;** set interleave
  760. .%122!     lda #%00001000
  761. .%123!     jsr sendU0
  762. .%124!     bcc +
  763. .%125!     rts
  764. .%126!  +  lda #1            ;interleave of 1 for 1581
  765. .%127!     bit sourceType
  766. .%128!     bmi +
  767. .%129!     lda #4            ;interleave of 4 for 1571
  768. .%130!  +  jsr kernelCiout
  769. .%131!     jsr kernelUnlsn
  770. .%132!     mountExit = *
  771. .%133!     rts
  772. .%134!
  773.  
  774. Read all of the sectors of a given track into the track cache.
  775.  
  776. .%135!  bufptr = 2
  777. .%136!  secnum = 4
  778. .%137!
  779. .%138!  readTrack = *  ;( .A=cylinder, .X=side ) : trackbuf, .CS=err
  780. .%139!     pha
  781. .%140!     txa
  782.  
  783. Get the side and put it into the command byte.  Remember that we have to flip
  784. the side bit for a 1581.
  785.  
  786. .%141!     and #$01
  787. .%142!     asl
  788. .%143!     asl
  789. .%144!     asl
  790. .%145!     asl
  791. .%146!     bit sourceType
  792. .%147!     bpl +
  793. .%148!     eor #$10
  794. .%149!  +  jsr sendU0
  795. .%150!     bcc +
  796. .%151!     rts
  797. .%152!  +  pla                  ;cylinder number
  798. .%153!     jsr kernelCiout
  799. .%154!     lda #1               ;start sector number
  800. .%155!     jsr kernelCiout
  801. .%156!     lda #9               ;sector count
  802. .%157!     jsr kernelCiout
  803. .%158!     jsr kernelUnlsn
  804.  
  805. Prepare to receive the track data.
  806.  
  807. .%159!     sei
  808. .%160!     clc
  809. .%161!     jsr kernelSpinp
  810. .%162!     bit ciaFlags
  811. .%163!     jsr toggleClock
  812. .%164!     lda #<trackbuf
  813. .%165!     ldy #>trackbuf
  814. .%166!     sta bufptr
  815. .%167!     sty bufptr+1
  816.  
  817. Get the sector data for each of the 9 sectors of the track.
  818.  
  819. .%168!     lda #0
  820. .%169!     sta secnum
  821. .%170!  -  bit sourceType
  822. .%171!     bmi +
  823.  
  824. If we are dealing with a 1571, we have to set the buffer pointer for the next
  825. sector, taking into account the soft interleave of 4.
  826.  
  827. .%172!     jsr get1571BufPtr
  828. .%173!  +  jsr readSector
  829. .%174!     bcs trackExit
  830. .%175!     inc secnum
  831. .%176!     lda secnum
  832. .%177!     cmp #9
  833. .%178!     bcc -
  834. .%179!     clc
  835. .%180!     trackExit = *
  836. .%181!     cli
  837. .%182!     rts
  838. .%183!
  839.  
  840. Get the buffer pointer for the next 1571 sector.
  841.  
  842. .%184!  get1571BufPtr = *
  843. .%185!     lda #<trackbuf
  844. .%186!     sta bufptr
  845. .%187!     ldx secnum
  846. .%188!     clc
  847. .%189!     lda #>trackbuf
  848. .%190!     adc bufptr1571,x
  849. .%191!     sta bufptr+1
  850. .%192!     rts
  851. .%193!
  852. .%194!  bufptr1571 = *
  853. .%195!     .byte 0,8,16,6,14,4,12,2,10
  854. .%196!
  855.  
  856. Read an individual sector into memory at the specified address.
  857.  
  858. .%197!  readSector = *  ;( bufptr ) : .CS=err
  859.  
  860. Get and check the burst status byte for errors.
  861.  
  862. .%198!     jsr getBurstByte
  863. .%199!     sta errno
  864. .%200!     and #$0f
  865. .%201!     cmp #2
  866. .%202!     bcc +
  867. .%203!     rts
  868. .%204!  +  ldx #2
  869. .%205!     ldy #0
  870. .%206!
  871.  
  872. Receive the 512 sector data bytes into memory.
  873.  
  874. .%207!     readByte = *
  875. .%208!     lda #$08
  876. .%209!  -  bit ciaFlags
  877. .%210!     beq -
  878. .%211!     lda ciaClock
  879. .%212!     eor #$10
  880. .%213!     sta ciaClock
  881. .%214!     lda ciaData
  882. .%215!     sta (bufptr),y
  883. .%216!     iny
  884. .%217!     bne readByte
  885. .%218!     inc bufptr+1
  886. .%219!     dex
  887. .%220!     bne readByte
  888. .%221!     rts
  889. .%222!
  890.  
  891. This next level of routines deals with logical sectors and the track cache
  892. rather than with hardware.
  893.  
  894. .%223!  ;====logical sector level====
  895. .%224!
  896.  
  897. Invalidate the track cache if the MS-DOS drive number is changed or if a new
  898. disk is inserted.  This routine has to establish a RAM configuration of $0E
  899. since it will be called from RAM0.  Configuration $0E gives RAM0 from $0000 to
  900. $BFFF, Kernal ROM from $C000 to $FFFF, and the I/O space over the Kernal from
  901. $D000 to $DFFF.  This configuration is set by all application interface
  902. subroutines.
  903.  
  904. .%225!  initPackage = *
  905. .%226!     lda #$0e
  906. .%227!     sta $ff00
  907. .%228!     lda #$ff
  908. .%229!     sta bufCylinder
  909. .%230!     sta bufSide
  910. .%231!     clc
  911. .%232!     rts
  912. .%233!
  913.  
  914. Locate a sector (block) in the track cache, or read the corresponding physical
  915. track into the track cache if necessary.  This routine accepts the cylinder,
  916. side, and sector numbers of the block.
  917.  
  918. .%234!  sectorSave = 5
  919. .%235!
  920. .%236!  readBlock = *  ;( .A=cylinder,.X=side,.Y=sector ) : .AY=blkPtr,.CS=err
  921.  
  922. Check if the correct track is in the track cache.
  923.  
  924. .%237!     cmp bufCylinder
  925. .%238!     bne readBlockPhysical
  926. .%239!     cpx bufSide
  927. .%240!     bne readBlockPhysical
  928.  
  929. If so, then locate the sector's address and return that.
  930.  
  931. .%241!     dey
  932. .%242!     tya
  933. .%243!     asl
  934. .%244!     clc
  935. .%245!     adc #>trackbuf
  936. .%246!     tay
  937. .%247!     lda #<trackbuf
  938. .%248!     clc
  939. .%249!     rts
  940. .%250!
  941.  
  942. Here, we have to read the physical track into the track cache.  We save the
  943. input parameters and call the hardware-level track-reading routine.
  944.  
  945. .%251!     readBlockPhysical = *
  946. .%252!     sta bufCylinder
  947. .%253!     stx bufSide
  948. .%254!     sty sectorSave
  949. .%255!     jsr readTrack
  950.  
  951. Check for errors.
  952.  
  953. .%256!     bcc readBlockPhysicalOk
  954. .%257!     lda errno
  955. .%258!     and #$0f
  956. .%259!     cmp #11    ;disk change
  957. .%260!     beq +
  958. .%261!     sec
  959. .%262!     rts
  960.  
  961. If the error that happened is a "Disk Change" error, then mount the disk and
  962. try to read the physical track again.
  963.  
  964. .%263!  +  jsr mountDisk
  965. .%264!     lda bufCylinder
  966. .%265!     ldx bufSide
  967. .%266!     ldy sectorSave
  968. .%267!     bcc readBlockPhysical
  969. .%268!     rts
  970. .%269!
  971.  
  972. Here, the physical track has been read into the track cache ok, so we recover
  973. the original input parameters and try the top of the routine again.
  974.  
  975. .%270!     readBlockPhysicalOk = *
  976. .%271!     lda bufCylinder
  977. .%272!     ldx bufSide
  978. .%273!     ldy sectorSave
  979. .%274!     jmp readBlock
  980. .%275!
  981.  
  982. Divide the given number by 18.  This is needed for the calculations to convert
  983. a logical sector number to the corresponding physical cylinder, side, and
  984. sector numbers that the lower-level routines require.  The method of repeated
  985. subtraction is used.  This routine would probably work faster if we tried to
  986. repeatedly subtract 360 (18*20) at the top, but I didn't bother.
  987.  
  988. .%276!  divideBy18 = *  ;( .AY=number ) : .A=quotient, .Y=remainder
  989. .%277!     ;** could repeatedly subtract 360 here
  990. .%278!     ldx #$ff
  991. .%279!  -  inx
  992. .%280!     sec
  993. .%281!     sbc #18
  994. .%282!     bcs -
  995. .%283!     dey
  996. .%284!     bpl -
  997. .%285!     clc
  998. .%286!     adc #18
  999. .%287!     iny
  1000. .%288!     tay
  1001. .%289!     txa
  1002. .%290!     rts
  1003. .%291!
  1004.  
  1005. Convert the given logical block number to the corresponding physical cylinder,
  1006. side, and sector numbers.  This routine follows the formulae given earlier
  1007. with a few simplifying tricks.
  1008.  
  1009. .%292!  convertLogicalBlockNum = *  ;( .AY=blockNum ) : .A=cyl, .X=side, .Y=sec
  1010. .%293!     jsr divideBy18
  1011. .%294!     ldx #0
  1012. .%295!     cpy #9
  1013. .%296!     bcc +
  1014. .%297!     pha
  1015. .%298!     tya
  1016. .%299!     sbc #9
  1017. .%300!     tay
  1018. .%301!     pla
  1019. .%302!     ldx #1
  1020. .%303!  +  iny
  1021. .%304!     rts
  1022. .%305!
  1023.  
  1024. Copy a sequential group of logical sectors into memory.  This routine is used
  1025. by the directory loading routine to load the FAT and Root Directory, and is
  1026. used by the cluster reading routine to retrieve all of the blocks of a
  1027. cluster.  After the given starting logical sector number is converted into its
  1028. physical cylinder, side, and sector equivalent, the physical values are
  1029. incremented to get the address of successive sectors of the group.  This
  1030. avoids the overhead of the logical to physical conversion.  Quite a number of
  1031. temporaries are needed.
  1032.  
  1033. .%306!  destPtr = 6
  1034. .%307!  curCylinder = 8
  1035. .%308!  curSide = 9
  1036. .%309!  curSector = 10
  1037. .%310!  blockCountdown = 11
  1038. .%311!  sourcePtr = 12
  1039. .%312!
  1040. .%313!  copyBlocks = *  ;( .AY=startBlock, .X=blockCount, ($6)=dest ) : .CS=err
  1041. .%314!     stx blockCountdown
  1042. .%315!     jsr convertLogicalBlockNum
  1043. .%316!     sta curCylinder
  1044. .%317!     stx curSide
  1045. .%318!     sty curSector
  1046. .%319!
  1047. .%320!     copyBlockLoop = *
  1048. .%321!     lda curCylinder
  1049. .%322!     ldx curSide
  1050. .%323!     ldy curSector
  1051. .%324!     jsr readBlock
  1052. .%325!     bcc +
  1053. .%326!     rts
  1054. .%327!  +  sta sourcePtr
  1055. .%328!     sty sourcePtr+1
  1056. .%329!     ldx #2
  1057. .%330!     ldy #0
  1058.  
  1059. Here I unroll the copying loop a little bit to cut the overhead of the branch
  1060. instruction in half.  (A cycle saved... you know).
  1061.  
  1062. .%331!  -  lda (sourcePtr),y
  1063. .%332!     sta (destPtr),y
  1064. .%333!     iny
  1065. .%334!     lda (sourcePtr),y
  1066. .%335!     sta (destPtr),y
  1067. .%336!     iny
  1068. .%337!     bne -
  1069. .%338!     inc sourcePtr+1
  1070. .%339!     inc destPtr+1
  1071. .%340!     dex
  1072. .%341!     bne -
  1073.  
  1074. Increment the cylinder, side, sector values.
  1075.  
  1076. .%342!     inc curSector
  1077. .%343!     lda curSector
  1078. .%344!     cmp #10
  1079. .%345!     bcc +
  1080. .%346!     lda #1
  1081. .%347!     sta curSector
  1082. .%348!     inc curSide
  1083. .%349!     lda curSide
  1084. .%350!     cmp #2
  1085. .%351!     bcc +
  1086. .%352!     lda #0
  1087. .%353!     sta curSide
  1088. .%354!     inc curCylinder
  1089. .%355!  +  dec blockCountdown
  1090. .%356!     bne copyBlockLoop
  1091. .%357!     clc
  1092. .%358!     rts
  1093. .%359!
  1094.  
  1095. Read a cluster into the Cluster Buffer, given the cluster number.  The cluster
  1096. number is converted to a logical sector number and then the sector copying
  1097. routine is called.  The formula given earlier is used for the conversion.
  1098.  
  1099. .%360!  readCluster = *  ;( .AY=clusterNumber ) : clusterBuf, .CS=err
  1100. .%361!     ;** convert cluster number to logical block number
  1101. .%362!     sec
  1102. .%363!     sbc #2
  1103. .%364!     bcs +
  1104. .%365!     dey
  1105. .%366!  +  ldx clusterBlockCount
  1106. .%367!     cpx #1
  1107. .%368!     beq +
  1108. .%369!     asl
  1109. .%370!     sty 7
  1110. .%371!     rol 7
  1111. .%372!     ldy 7
  1112. .%373!  +  clc
  1113. .%374!     adc firstFileBlock
  1114. .%375!     bcc +
  1115. .%376!     iny
  1116. .%377!
  1117. .%378!     ;** read logical blocks comprising cluster
  1118. .%379!  +  ldx #<clusterBuf
  1119. .%380!     stx 6
  1120. .%381!     ldx #>clusterBuf
  1121. .%382!     stx 7
  1122. .%383!     ldx clusterBlockCount
  1123. .%384!     jmp copyBlocks
  1124. .%385!
  1125.  
  1126. This next level of routines deal with the data structures of the MS-DOS disk
  1127. format.
  1128.  
  1129. .%386!  ;====MS-DOS format level====
  1130. .%387!
  1131. .%388!  bootBlock = 2
  1132. .%389!
  1133.  
  1134. Read the disk format parameters, directory, and FAT into memory.
  1135.  
  1136. .%390!  loadDirectory = *  ;( ) : .AY=dirbuf, .X=dirEntries, .CS=err
  1137. .%391!     lda #$0e
  1138. .%392!     sta $ff00
  1139. .%393!
  1140.  
  1141. Read the boot sector and extract the parameters.
  1142.  
  1143. .%394!     ;** get parameters from boot sector
  1144. .%395!     lda #0
  1145. .%396!     ldy #0
  1146. .%397!     jsr convertLogicalBlockNum
  1147. .%398!     jsr readBlock
  1148. .%399!     bcc +
  1149. .%400!     rts
  1150. .%401!  +  sta bootBlock
  1151. .%402!     sty bootBlock+1
  1152. .%403!     ldy #13              ;get cluster size
  1153. .%404!     lda (bootBlock),y
  1154. .%405!     sta clusterBlockCount
  1155. .%406!     cmp #3
  1156. .%407!     bcc +
  1157. .%408!
  1158.  
  1159. If a disk parameter is found to exceed the limits of LRR, error code #60 is
  1160. returned.
  1161.  
  1162. .%409!     invalidParms = *
  1163. .%410!     lda #60
  1164. .%411!     sta errno
  1165. .%412!     sec
  1166. .%413!     rts
  1167. .%414!
  1168. .%415!  +  ldy #16              ;check FAT replication count, must be 2
  1169. .%416!     lda (bootBlock),y
  1170. .%417!     cmp #2
  1171. .%418!     bne invalidParms
  1172. .%419!     ldy #22              ;get FAT size in sectors
  1173. .%420!     lda (bootBlock),y
  1174. .%421!     sta fatBlocks
  1175. .%422!     cmp #4
  1176. .%423!     bcs invalidParms
  1177. .%424!     ldy #17              ;get directory size
  1178. .%425!     lda (bootBlock),y
  1179. .%426!     sta rootDirEntries
  1180. .%427!     cmp #129
  1181. .%428!     bcs invalidParms
  1182. .%429!     lsr
  1183. .%430!     lsr
  1184. .%431!     lsr
  1185. .%432!     lsr
  1186. .%433!     sta rootDirBlocks
  1187. .%434!     ldy #19              ;get total sector count
  1188. .%435!     lda (bootBlock),y
  1189. .%436!     sta totalSectors
  1190. .%437!     iny
  1191. .%438!     lda (bootBlock),y
  1192. .%439!     sta totalSectors+1
  1193. .%440!     ldy #24              ;check sectors per track, must be 9
  1194. .%441!     lda (bootBlock),y
  1195. .%442!     cmp #9
  1196. .%443!     bne invalidParms
  1197. .%444!     ldy #26
  1198. .%445!     lda (bootBlock),y
  1199. .%446!     cmp #2               ;check number of sides, must be 2
  1200. .%447!     bne invalidParms
  1201. .%448!     ldy #14              ;check number of boot sectors, must be 1
  1202. .%449!     lda (bootBlock),y
  1203. .%450!     cmp #1
  1204. .%451!     bne invalidParms
  1205. .%452!
  1206.  
  1207. Calculate the derived parameters.
  1208.  
  1209. .%453!     ;** get derived parameters
  1210. .%454!     lda fatBlocks        ;first root directory sector
  1211. .%455!     asl
  1212. .%456!     clc
  1213. .%457!     adc #1
  1214. .%458!     sta firstRootDirBlock
  1215. .%459!     clc                  ;first file sector
  1216. .%460!     adc rootDirBlocks
  1217. .%461!     sta firstFileBlock
  1218. .%462!     lda totalSectors     ;number of file clusters
  1219. .%463!     ldy totalSectors+1
  1220. .%464!     sec
  1221. .%465!     sbc firstFileBlock
  1222. .%466!     bcs +
  1223. .%467!     dey
  1224. .%468!  +  sta fileClusterCount
  1225. .%469!     sty fileClusterCount+1
  1226. .%470!     lda clusterBlockCount
  1227. .%471!     cmp #2
  1228. .%472!     bne +
  1229. .%473!     lsr fileClusterCount+1
  1230. .%474!     ror fileClusterCount
  1231. .%475!
  1232.  
  1233. Gee, I have more comments embedded in the code than I did last issue.
  1234.  
  1235. .%476!     ;** load FAT
  1236. .%477!  +  lda #<fatbuf
  1237. .%478!     ldy #>fatbuf
  1238. .%479!     sta 6
  1239. .%480!     sty 7
  1240. .%481!     lda #1
  1241. .%482!     ldy #0
  1242. .%483!     ldx fatBlocks
  1243. .%484!     jsr copyBlocks
  1244. .%485!     bcc +
  1245. .%486!     rts
  1246. .%487!
  1247. .%488!     ;** load actual directory
  1248. .%489!  +  lda #<dirbuf
  1249. .%490!     ldy #>dirbuf
  1250. .%491!     sta 6
  1251. .%492!     sty 7
  1252. .%493!     lda firstRootDirBlock
  1253. .%494!     ldy #0
  1254. .%495!     ldx rootDirBlocks
  1255. .%496!     jsr copyBlocks
  1256. .%497!     bcc +
  1257. .%498!     rts
  1258. .%499!  +  lda #<dirbuf
  1259. .%500!     ldy #>dirbuf
  1260. .%501!     ldx rootDirEntries
  1261. .%502!     clc
  1262. .%503!     rts
  1263. .%504!
  1264.  
  1265. This routine locates the given FAT table entry number and returns the value
  1266. stored in it.  Some work is needed to deal with the 12-bit compressed data
  1267. structure.
  1268.  
  1269. .%505!  entryAddr = 2
  1270. .%506!  entryWork = 4
  1271. .%507!  entryBits = 5
  1272. .%508!  entryData0 = 6
  1273. .%509!  entryData1 = 7
  1274. .%510!  entryData2 = 8
  1275. .%511!
  1276. .%512!  getFatEntry = *  ;( .AY=fatEntryNumber ) : .AY=fatEntryValue
  1277. .%513!     sta entryBits
  1278.  
  1279. Divide the FAT entry number by two and multiply by three because two FAT
  1280. entries are stored in three bytes.  Then add the FAT base address and we have
  1281. the address of the three bytes that contain the FAT entry we are interested
  1282. in.  I retrieve the three bytes into zero-page memory for easy manipulation.
  1283.  
  1284. .%514!     ;** divide by two
  1285. .%515!     sty entryAddr+1
  1286. .%516!     lsr entryAddr+1
  1287. .%517!     ror
  1288. .%518!
  1289. .%519!     ;** times three
  1290. .%520!     sta entryWork
  1291. .%521!     ldx entryAddr+1
  1292. .%522!     asl
  1293. .%523!     rol entryAddr+1
  1294. .%524!     clc
  1295. .%525!     adc entryWork
  1296. .%526!     sta entryAddr
  1297. .%527!     txa
  1298. .%528!     adc entryAddr+1
  1299. .%529!     sta entryAddr+1
  1300. .%530!
  1301. .%531!     ;** add base, get data
  1302. .%532!     clc
  1303. .%533!     lda entryAddr
  1304. .%534!     adc #<fatbuf
  1305. .%535!     sta entryAddr
  1306. .%536!     lda entryAddr+1
  1307. .%537!     adc #>fatbuf
  1308. .%538!     sta entryAddr+1
  1309. .%539!     ldy #2
  1310. .%540!  -  lda (entryAddr),y
  1311. .%541!     sta entryData0,y
  1312. .%542!     dey
  1313. .%543!     bpl -
  1314. .%544!     lda entryBits
  1315. .%545!     and #1
  1316. .%546!     bne +
  1317. .%547!
  1318.  
  1319. If the original given FAT entry number is even, then we want the first 12-bit
  1320. compressed field.  The nybbles are extracted according to the diagram shown
  1321. earlier.
  1322.  
  1323. .%548!     ;** case 1: first 12-bit cluster
  1324. .%549!     lda entryData1
  1325. .%550!     and #$0f
  1326. .%551!     tay
  1327. .%552!     lda entryData0
  1328. .%553!     rts
  1329. .%554!
  1330.  
  1331. Otherwise, we want the second 12-bit field.
  1332.  
  1333. .%555!     ;** case 2: second 12-bit cluster
  1334. .%556!  +  lda entryData1
  1335. .%557!     ldx #4
  1336. .%558!  -  lsr entryData2
  1337. .%559!     ror
  1338. .%560!     dex
  1339. .%561!     bne -
  1340. .%562!     ldy entryData2
  1341. .%563!     rts
  1342. .%564!
  1343.  
  1344. Finally, this is the file copying level.  It deals with reading the clusters
  1345. of MS-DOS files and copying the data they contain to the already-open CBM
  1346. Kernal file, possibly with ASCII-to-PETSCII translation.
  1347.  
  1348. .%565!  ;====file copy level====
  1349. .%566!
  1350. .%567!  transMode = 14
  1351. .%568!  lfn = 15
  1352. .%569!  cbmDataPtr = $60
  1353. .%570!  cbmDataLen = $62
  1354. .%571!  cluster = $64
  1355. .%572!
  1356.  
  1357. Copy the given cluster to the CBM output file.  This routine fetches the next
  1358. cluster of the file for the next time this routine is called, and if it hits
  1359. the NULL pointer of the last cluster of a file, it adjusts the number of valid
  1360. file data bytes the current cluster contains to FileLength % ClusterLength
  1361. (see note below).
  1362.  
  1363. .%573!  copyFileCluster = *  ;( cluster, lfn, transMode ) : .CS=err
  1364.  
  1365. Read the cluster and setup to copy the whole cluster to the CBM file.
  1366.  
  1367. .%574!     lda cluster
  1368. .%575!     ldy cluster+1
  1369. .%576!     jsr readCluster
  1370. .%577!     bcc +
  1371. .%578!     rts
  1372. .%579!  +  lda #<clusterBuf
  1373. .%580!     ldy #>clusterBuf
  1374. .%581!     sta cbmDataPtr
  1375. .%582!     sty cbmDataPtr+1
  1376. .%583!     lda #0
  1377. .%584!     sta cbmDataLen
  1378. .%585!     lda clusterBlockCount
  1379. .%586!     asl
  1380. .%587!     sta cbmDataLen+1
  1381. .%588!
  1382.  
  1383. Fetch the next cluster number of the file, and adjust the cluster data length
  1384. for the last cluster of the file.
  1385.  
  1386. .%589!     ;**get next cluster
  1387. .%590!     lda cluster
  1388. .%591!     ldy cluster+1
  1389. .%592!     jsr getFatEntry
  1390. .%593!     sta cluster
  1391. .%594!     sty cluster+1
  1392. .%595!     cmp #$ff
  1393. .%596!     bne copyFileClusterData
  1394. .%597!     cpy #$0f
  1395. .%598!     bne copyFileClusterData
  1396. .%599!     lda lenML
  1397. .%600!     sta cbmDataLen
  1398. .%601!     lda #$01
  1399. .%602!     ldx clusterBlockCount
  1400. .%603!     cpx #1
  1401. .%604!     beq +
  1402. .%605!     lda #$03
  1403. .%606!  +  and lenML+1
  1404.  
  1405. The following three lines were added in a last minute panic after realizing
  1406. that if FileLength % ClusterSize == 0, then the last cluster of the file
  1407. contains ClusterSize bytes, not zero bytes.
  1408.  
  1409. .%000!     bne +
  1410. .%000!     ldx lenML
  1411. .%000!     beq copyFileClusterData
  1412. .%607!  +  sta cbmDataLen+1
  1413. .%608!
  1414. .%609!     copyFileClusterData = *
  1415. .%610!     jsr commieOut
  1416. .%611!     rts
  1417. .%612!
  1418.  
  1419. Copy the file data in the MS-DOS cluster buffer to the CBM output file.
  1420.  
  1421. .%613!  cbmDataLimit = $66
  1422. .%614!
  1423. .%615!  commieOut = *  ;( cbmDataPtr, cbmDataLen ) : .CS=err
  1424.  
  1425. If the the logical file number to copy to is 0 ("null device"), then don't
  1426. bother copying anything.
  1427.  
  1428. .%616!     ldx lfn
  1429. .%617!     bne +
  1430. .%618!     clc
  1431. .%619!     rts
  1432.  
  1433. Otherwise, prepare the logical file number for output.
  1434.  
  1435. .%620!  +  jsr kernelChkout
  1436. .%621!     bcc commieOutMore
  1437. .%622!     sta errno
  1438. .%623!     rts
  1439. .%624!
  1440. .%625!     commieOutMore = *
  1441.  
  1442. Process the cluster data in chunks of up to 255 bytes or the number of data
  1443. bytes remaining in the cluster.
  1444.  
  1445. .%626!     lda #255
  1446. .%627!     ldx cbmDataLen+1
  1447. .%628!     bne +
  1448. .%629!     lda cbmDataLen
  1449. .%630!  +  sta cbmDataLimit
  1450. .%631!     ldy #0
  1451. .%632!  -  lda (cbmDataPtr),y
  1452. .%633!     bit transMode
  1453. .%634!     bpl +
  1454.  
  1455. If we have to translate the current ASCII character, look up the PETSCII value
  1456. in the translation table and output that value.  If the translation table
  1457. entry value is $00, then don't output a character (filter out invalid
  1458. character codes).
  1459.  
  1460. .%635!     tax
  1461. .%636!     lda transBuf,x
  1462. .%637!     beq commieNext
  1463. .%638!  +  jsr kernelChrout
  1464. .%639!     commieNext = *
  1465. .%640!     iny
  1466. .%641!     cpy cbmDataLimit
  1467. .%642!     bne -
  1468. .%643!
  1469.  
  1470. Increment the cluster buffer pointer and decrement the cluster buffer character
  1471. count according to the number of bytes just processed, and repeat the above if
  1472. more file data remains in the current cluster.
  1473.  
  1474. .%644!     clc
  1475. .%645!     lda cbmDataPtr
  1476. .%646!     adc cbmDataLimit
  1477. .%647!     sta cbmDataPtr
  1478. .%648!     bcc +
  1479. .%649!     inc cbmDataPtr+1
  1480. .%650!  +  sec
  1481. .%651!     lda cbmDataLen
  1482. .%652!     sbc cbmDataLimit
  1483. .%653!     sta cbmDataLen
  1484. .%654!     bcs +
  1485. .%655!     dec cbmDataLen+1
  1486. .%656!  +  lda cbmDataLen
  1487. .%657!     ora cbmDataLen+1
  1488. .%658!     bne commieOutMore
  1489.  
  1490. If we are finished with the cluster, then clear the CBM Kernal output channel.
  1491.  
  1492. .%659!     jsr kernelClrchn
  1493. .%660!     clc
  1494. .%661!     rts
  1495. .%662!
  1496.  
  1497. The file copying main routine.  Set up for the starting cluster, and call
  1498. the cluster copying routine until end-of-file is reached.  Checks for a
  1499. NULL cluster pointer in the directory entry to handle zero-length files.
  1500.  
  1501. .%663!  copyFile = *  ;( startCluster, lenML, .A=transMode, .X=lfn ) : .CS=err
  1502. .%664!     ldy #$0e
  1503. .%665!     sty $ff00
  1504. .%666!     sta transMode
  1505. .%667!     stx lfn
  1506. .%668!     lda startCluster
  1507. .%669!     ldy startCluster+1
  1508. .%670!     sta cluster
  1509. .%671!     sty cluster+1
  1510. .%672!     jmp +
  1511. .%673!  -  jsr copyFileCluster
  1512. .%674!     bcc +
  1513. .%675!     rts
  1514. .%676!  +  lda cluster
  1515. .%677!     cmp #$ff
  1516. .%678!     bne -
  1517. .%679!     lda cluster+1
  1518. .%680!     cmp #$0f
  1519. .%681!     bne -
  1520. .%682!     clc
  1521. .%683!     rts
  1522. .%684!
  1523.  
  1524. This is the translation table used to convert from ASCII to PETSCII.  You can
  1525. modify it to suit your needs if you wish.  If you cannot reassemble this file,
  1526. then you can sift through the binary file and locate the tabel and change it
  1527. there.  An entry of $00 means the corresponding ASCII character will not be
  1528. translated.  You'll notice that I have set up translations for the following
  1529. ASCII control characters into PETSCII: Backspace, Tab, Linefeed (CR), and
  1530. Formfeed.  I also translate the non-PETSCII characters such as {, |, ~, and _
  1531. according to what they probably would have been if Commodore wasn't so
  1532. concerned with the graphics characters.
  1533.  
  1534. .%685!  transBuf = *
  1535. .%686!         ;0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
  1536. .%687!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$14,$09,$0d,$00,$93,$00,$00,$00 ;0
  1537. .%688!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;1
  1538. .%689!  .byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2a,$2b,$2c,$2d,$2e,$2f ;2
  1539. .%690!  .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3a,$3b,$3c,$3d,$3e,$3f ;3
  1540. .%691!  .byte $40,$c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9,$ca,$cb,$cc,$cd,$ce,$cf ;4
  1541. .%692!  .byte $d0,$d1,$d2,$d3,$d4,$d5,$d6,$d7,$d8,$d9,$da,$5b,$5c,$5d,$5e,$5f ;5
  1542. .%693!  .byte $c0,$41,$42,$43,$44,$45,$46,$47,$48,$49,$4a,$4b,$4c,$4d,$4e,$4f ;6
  1543. .%694!  .byte $50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5a,$db,$dc,$dd,$de,$df ;7
  1544. .%695!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;8
  1545. .%696!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;9
  1546. .%697!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;a
  1547. .%698!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;b
  1548. .%699!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;c
  1549. .%700!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;d
  1550. .%701!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;e
  1551. .%702!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;f
  1552. .%703!
  1553.  
  1554. This is where the track cache, etc. are stored.  This section requires 11K of
  1555. storage space but does not increase the length of the binary program file
  1556. since these storage areas are DEFINED rather than allocated with ".buf"
  1557. directives.  The Unix terminology for this type of uninitialized data is "bss".
  1558.  
  1559. .%704!  ;====bss storage====
  1560. .%705!
  1561. .%706!  bss = *
  1562. .%707!  trackbuf   = bss
  1563. .%708!  clusterBuf = trackbuf+4608  
  1564. .%709!  fatbuf     = clusterBuf+1024
  1565. .%710!  dirbuf     = fatbuf+1536    
  1566. .%711!  end        = dirbuf+4096
  1567.  
  1568. 5. USER-INTERFACE PROGRAM
  1569.  
  1570. This section presents the listing of the user-interface BASIC program.  You
  1571. should be aware that you can easily change some of the defaults to your own
  1572. preferences if you wish.  This program is not listed in the ".%nnn!" format
  1573. that the assembler listing is since you can recover this listing from the
  1574. uuencoded binary program file.  This program should be a little easier to
  1575. follow than the assembler listing since BASIC is a self-commenting language. :-)
  1576.  
  1577. 10 rem little red reader, by craig bruce, 30-sep-92, for c= hacking netmag
  1578. 11 :
  1579.  
  1580. These lines set up the default CBM-DOS and MS-DOS device numbers, taking care
  1581. to disallow them to be the same device.  You can change this to your own drive
  1582. configuration.
  1583.  
  1584. 20 cd=peek(186)  : rem ** default cbm-dos drive **
  1585. 25 dv=9:dt=0  :  rem ** ms-dos drive, type (0=1571,255=1581)
  1586. 26 if dv=cd then dv=8:dt=0 : rem ** alternate ms-dos drive
  1587. 27 :
  1588. 30 print chr$(147);"initializing..." : print
  1589. 40 bank0 : pk=dec("8000")
  1590. 50 if peek(pk+9)=dec("cb") and peek(pk+10)=131 then 60
  1591. 55 print"loading machine language routines..." : bload"lrr.bin",u(cd)
  1592. 60 poke pk+16,dv : poke pk+17,dt : sys pk
  1593.  
  1594. I "dim" the following variables before the arrays to avoid the overhead of
  1595. pushing the arrays around when creating new scalar variables.
  1596.  
  1597. 70 dim t,r,b,i,a$,c,dt$,fl$,il$,x,x$
  1598. 80 dim di$(128),cl(128),sz(128)
  1599. 90 if dt=255 then dt$="1581" :else dt$="1571"
  1600. 100 fl$=chr$(19)+chr$(17)+chr$(17)+chr$(17)+chr$(17)
  1601. 110 il$=fl$:fori=1to19:il$=il$+chr$(17):next
  1602. 120 goto 500
  1603. 130 :
  1604. 131 rem ** load ms-dos directory **
  1605. 140 print"loading directory..." : print
  1606. 150 sys pk : sys pk+3
  1607. 160 dl=0
  1608.  
  1609. The "rreg" instruction returns the return values of the .A, .X, .Y, and .S
  1610. registers from the last "sys" call.  I check the 1-bit of the .S register
  1611. (the Carry flag) for error returns.
  1612.  
  1613. 170 rreg bl,dc,bh,s : e=peek(pk+15)
  1614. 180 if (s and 1) then gosub 380 : return
  1615. 190 print"scanning directory..." : print
  1616. 200 db=bl+256*bh
  1617. 210 if dc=0 then 360
  1618. 220 for dp=db to db+32*(dc-1) step 32
  1619. 230 if peek(dp)=0 or peek(dp)=229 then 350
  1620. 240 if peek(dp+12) and 24 then 350
  1621. 250 dl=dl+1
  1622.  
  1623. This next line is where I set the default selection status, translation type,
  1624. and CBM file type for the MS-DOS files.  You can change these defaults simply
  1625. by overtyping the string in   (  |  |||  |||  ) the "V" locations.
  1626.                                  V  VVV  VVV
  1627. 260 d$=right$(" "+str$(dl),3)+"     asc  seq  " : rem ** default sel/tr/ft **
  1628. 270 a$="" : fori=0to10 : a$=a$+chr$(peek(dp+i)) : next
  1629. 280 a$=left$(a$,8)+"  "+right$(a$,3)
  1630. 290 print dl; a$
  1631. 300 d$=d$+a$+"  "
  1632. 310 cl(dl)=peek(dp+26)+256*peek(dp+27)
  1633. 320 sz=peek(dp+28)+256*peek(dp+29)+65536*peek(dp+30)
  1634. 330 di$(dl)=d$+right$("    "+str$(sz),6)
  1635. 340 sz(dl)=sz
  1636. 350 next dp
  1637. 360 return
  1638. 370 :
  1639. 371 rem ** report ms-dos disk error **
  1640. 380 print chr$(18);"ms-dos disk error #";mid$(str$(e),2);
  1641. 390 print " ($";mid$(hex$(e),3);"), press key.";chr$(146)
  1642. 400 getkey a$ : return
  1643. 410 :
  1644. 411 rem ** screen heading **
  1645. 420 printchr$(147);"ms-dev=";mid$(str$(dv),2);"    ms-type=";dt$;
  1646. 430 print"    cbm-dev=";mid$(str$(cd),2):print
  1647. 440 return
  1648. 450 :
  1649. 451 rem ** screen footing **
  1650. 460 print il$;"d=directory  m=ms-dev  f=cbm-dev q=quit"
  1651. 470 print"t=toggle-column, c=copy-files, +/- page";
  1652. 480 return
  1653. 490 :
  1654. 491 rem ** main routine **
  1655. 500 t=1 : c=0
  1656. 510 r=0
  1657. 520 gosub 420
  1658. 530 print "num  s  trn  typ  filename  ext  length"
  1659. 540 print "---  -  ---  ---  --------  ---  ------"
  1660. 550 gosub 460
  1661. 560 b=t+17 : if b>dl then b=dl
  1662. 570 print fl$;: if t>dl then 590
  1663. 580 for i=t to b : print di$(i) : next
  1664. 590 if dl=0 then print chr$(18);"<no files>";chr$(146)
  1665. 600 if dl=0 then 660
  1666. 610 print left$(il$,r+5);chr$(18);
  1667. 620 on c+1 goto 630,640,650
  1668. 630 print spc(4);mid$(di$(t+r),5,3) : goto 660
  1669. 640 print spc(7);mid$(di$(t+r),8,5) : goto 660
  1670. 650 print spc(12);mid$(di$(t+r),13,5) : goto 660
  1671. 660 getkey a$
  1672.  
  1673. Oh shi^Hoot.  I screwed up the following line in the string after the
  1674. "+chr$(13)+" part.  You'll notice that I have avoided putting cursor control
  1675. characters into the strings everywhere else, but I forgot to do that here.
  1676. The "{stuff}" should be CursorUp, CursorDown, CursorLeft, CursorRight,
  1677. CursorHome, and CursorCLR control characters, respectively.  These characters
  1678. give the index for the "on" statement below.
  1679.  
  1680. 670 i=instr("dmftc+-q "+chr$(13)+"{stuff}",a$)
  1681. 680 print left$(il$,r+5);di$(t+r)
  1682. 690 if i=0 then 600
  1683. 700 onigoto760,1050,1110,950,1150,1000,1020,730,860,860,770,790,810,830,850,500
  1684. 710 stop
  1685. 720 :
  1686. 721 rem ** various menu options **
  1687. 730 print chr$(147);"have an awesome day."
  1688. 740 end
  1689. 760 gosub 420 : gosub 140 : goto 500
  1690. 770 r=r-1 : if r<0 then r=b-t
  1691. 780 goto 600
  1692. 790 r=r+1 : if t+r>b then r=0
  1693. 800 goto 600
  1694. 810 c=c-1 : if c<0 then c=2
  1695. 820 goto 600
  1696. 830 c=c+1 : if c>2 then c=0
  1697. 840 goto 600
  1698. 850 r=0 : c=0 : goto 600
  1699. 860 if dl=0 then 600
  1700. 870 x=t+r : on c+1 gosub 890,910,930
  1701. 880 print left$(il$,r+5);di$(x) : goto 600
  1702. 890 if mid$(di$(x),6,1)=" " then x$="*" :else x$=" "
  1703. 900 mid$(di$(x),6,1)=x$ : return
  1704. 910 if mid$(di$(x),9,1)="a" then x$="bin" :else x$="asc"
  1705. 920 mid$(di$(x),9,3)=x$ : return
  1706. 930 if mid$(di$(x),14,1)="s" then x$="prg" :else x$="seq"
  1707. 940 mid$(di$(x),14,3)=x$ : return
  1708. 950 if dl=0 then 600
  1709. 960 for x=1 to dl
  1710. 970 on c+1 gosub 890,910,930
  1711. 980 next x
  1712. 990 goto 520
  1713. 1000 if b=dl then t=1 : goto 510
  1714. 1010 t=t+18 : goto 510
  1715. 1020 if t=1 then t=dl-(dl-int(dl/18)*18)+1 : goto 510
  1716. 1030 t=t-18 : if t<1 then t=1
  1717. 1040 goto 510
  1718. 1050 print il$;chr$(27);"@";
  1719. 1060 input"ms-dos device number (8-30)";dv
  1720. 1061 if cd=dv then print"ms-dos and cbm-dos devices must be different!":goto1060
  1721. 1070 input"ms-dos device type  (71/81)";x
  1722. 1080 if x=8 or x=81 or x=1581 then dt=255:dt$="1581" :else dt=0:dt$="1571"
  1723. 1090 poke pk+16,dv : poke pk+17,dt : sys pk
  1724. 1100 goto 520
  1725. 1110 print il$;chr$(27);"@";
  1726. 1120 input "cbm-dos device number (0-30)";cd
  1727. 1130 if cd=dv then print"ms-dos and cbm-dos devices must be different!":goto1120
  1728. 1140 goto 520
  1729. 1141 :
  1730. 1142 rem ** copy files **
  1731. 1150 print chr$(147);"copy files":print:print
  1732. 1160 if dl=0 then fc=0 : goto 1190
  1733. 1170 fc=0 : for f=1 to dl : if mid$(di$(f),6,1)="*" then gosub 1200
  1734. 1180 next f
  1735. 1190 print : print"files copied =";fc;" - press key"
  1736. 1191 getkey a$ : goto 520
  1737. 1200 fc=fc+1
  1738. 1210 x$=mid$(di$(f),19,8)+"."+mid$(di$(f),29,3)
  1739. 1220 cf$="":fori=1tolen(x$):if mid$(x$,i,1)<>" " then cf$=cf$+mid$(x$,i,1)
  1740. 1230 next
  1741. 1231 if right$(cf$,1)="." then cf$=left$(cf$,len(cf$)-1)
  1742. 1232 cf$=cf$+","+mid$(di$(f),14,1)
  1743. 1240 print str$(fc);". ";chr$(34);cf$;chr$(34);tab(20);sz(f)"bytes";
  1744. 1245 print tab(35);mid$(di$(f),9,3)
  1745. 1250 cl=cl(f) : lb=sz(f) - int(sz(f)/65536)*65536
  1746.  
  1747. I had to use a DOPEN statement here for disk files because the regular OPEN
  1748. statment does not redirect the DS and DS$ pseudo-variables.  You'll notice
  1749. that the non-disk OPEN statment below has a secondary address of 7.  This is
  1750. to put the printer into lowercase mode if you are outputting directly to it.
  1751. You can replace this with a 5 (or whatever) if you have a special interface
  1752. to an IBM-compatible printer and you want to print directly in ASCII.  In this
  1753. case, you would select the "BIN" translation mode for the file you are routing
  1754. directly to the printer.
  1755.  
  1756. 1260 if cd>=8 then dopen#1,(cf$+",w"),u(cd) :else if cd<>0 then open 1,cd,7
  1757. 1265 if cd<8 then 1288
  1758. 1270 if ds<>63 then 1288
  1759. 1275 x$="y" : print "file exists; overwrite (y/n)";
  1760. 1280 close 1 : input x$ : if x$="n" then fc=fc-1 : return
  1761. 1285 scratch(cf$),u(cd)
  1762. 1286 dopen#1,(cf$+",w"),u(cd)
  1763. 1288 if cd<8 then 1320
  1764. 1300 if ds<20 then 1320
  1765. 1310 print chr$(18)+"cbm disk error: "+ds$ : fc=fc-1 : close1 : return
  1766. 1320 poke pk+19,cl/256 : poke pk+18,cl-peek(pk+19)*256
  1767. 1330 poke pk+21,lb/256 : poke pk+20,lb-peek(pk+21)*256
  1768. 1340 tr=0 : if mid$(di$(f),9,1)="a" then tr=255
  1769. 1346 x=1 : if cd=0 then x=0
  1770. 1350 sys pk+6,tr,x
  1771. 1355 rreg x,x,x,s : e=peek(pk+15)
  1772. 1356 if (s and 1) then gosub 380 : fc=fc-1
  1773. 1360 if cd<>0 and cd<8 then close1
  1774. 1370 if cd>=8 then dclose#1 : if ds>=20 then 1310
  1775. 1380 return
  1776.  
  1777. 6. UUENCODED FILES
  1778.  
  1779. Here are the binary executables in uuencoded form.  The CRC32s of the two
  1780. files are as follows:
  1781.  
  1782.    "lrr.128"    1106058594
  1783.    "lrr.bin"     460671650
  1784.  
  1785. The "lrr.128" file is the main BASIC program and the "lrr.bin" file contains
  1786. the machine lanugage disk-accessing routines.
  1787.  
  1788. [LRR.128 and LRR.BIN are included in this archive. -RW]
  1789.  
  1790. 7. BIBLIOGRAPHY
  1791.  
  1792. The following works were consulted in creating this article:
  1793.  
  1794. [1] Jim Butterfield, "Jim Butterfield's Complete C128 Memory Map",
  1795.     _The_Transactor_, Volume 7, Issue 01, July 1986 (A Must!).
  1796.  
  1797. [2] Commodore Business Machines, _Commodore_1571_Disk_Drive_User's_Guide_,
  1798.     CBM, 1985.
  1799.  
  1800. [3] Some program called "msdos-to-128" included with "cs-dos" by
  1801.     M. G-something.  Originally published in COMPUTE!'s Gazzette, I think.
  1802.  
  1803. [4] Commodore Business Machines, _Commodore_128_Programmer's_Reference_Guide_,
  1804.     Bantam Books, 1986.
  1805.  
  1806. [5] _The_Transactor_, Volume 4, Issue 05 ("The Reference Issue"), May 1983.
  1807.  
  1808. =============================================================================
  1809.