home *** CD-ROM | disk | FTP | other *** search
/ Brotikasten / BROTCD01.iso / texte / cbmhack4.txt < prev    next >
Text File  |  1995-08-20  |  160KB  |  3,622 lines

  1.                    ########
  2.              ##################
  3.          ######            ######
  4.       #####
  5.     #####  ####  ####      ##      #####   ####  ####  ####  ####  ####   #####
  6.   #####    ##    ##      ####    ##   ##   ##  ###     ##    ####  ##   ##   ##
  7.  #####    ########     ##  ##   ##        #####       ##    ## ## ##   ##
  8. #####    ##    ##    ########  ##   ##   ##  ###     ##    ##  ####   ##   ##
  9. #####  ####  ####  ####  ####  #####   ####  ####  ####  ####  ####   ######
  10. #####                                                                    ##
  11.  ######            ######        Volume 1, Issue #4
  12.    ##################             October 5, 1992
  13.        ########
  14.  
  15. -----------------------------------------------------------------------------
  16. Editor's Notes:
  17. by Craig Taylor (duck@pembvax1.pembroke.edu)
  18.  
  19.   My apologies about this issue being posted later than was mentioned in a
  20.   preview post on comp.sys.cbm newsgroup. Due to some problems with coding,
  21.   school and Murphy's law the issue had to be delayed until now.
  22.  
  23.   I have asked the system admin's at my site concerning a mail-server but they
  24.   said they did not have enough man-power (go figure) to get somebody to run it.
  25.   I will be implementing a mail-server system in my account in the near
  26.   future for retrieval of programs and back-issues. I'll post descriptions of
  27.   how to use it it the next issue of C= Hacking as well as on the newsgroup
  28.   comp.sys.cbm when I finish writing it.
  29.  
  30.   In this issue of C= Hacking we also start on an ambitious task: Developing
  31.   a game for both the C128 and C64 modes that includes all of the features
  32.   found in commercial games. Take a look in the Learning ML Column for more
  33.   information.
  34.  
  35.   Also, The article concerning the 1351 mouse has _again_ been delayed due
  36.   to time constraints. Rest assured that it will be in the next issue of
  37.   C= Hacking.
  38.  
  39.   If you are interested in helping write for C= Hacking please feel free to
  40.   mail duck@pembvax1.pembroke.edu (or duck@handy.pembroke.edu). We're always
  41.   looking for new authors on almost any subject, software or hardware.
  42.  
  43. ================================================================================
  44.  
  45.   Also note that this issue and prior ones are available via anonymous ftp from
  46.   ccosun.caltech.edu under pub/rknop/HACKING.MAG.
  47.  
  48. ================================================================================
  49.  
  50.   NOTICE: Permission is granted to re-distribute this "net-magazine", in 
  51.   whole, freely for non-profit use. However, please contact individual 
  52.   authors for permission to publish or re-distribute articles seperately.
  53.  
  54.      *** AUTHORS LISTED BELOW RETAIN ALL RIGHTS TO THEIR ARTICLES ***
  55.  
  56. ================================================================================
  57. In this issue:
  58.  
  59. Learning ML - Part 4
  60.  
  61.   In the next issue we'll embark on a project of making a space invaders style
  62. game for the C=64/128 from scratch using custom characters, interrupt-driven
  63. music, animation, using the joystick, mouse or keyboard. The C64 and C128 
  64. versions will be developed con-currently, each program taking advantage of 
  65. the machine's capabilities. This is the first in a series - written by 
  66. Craig Taylor.
  67.  
  68. The Demo Corner: FLI - more color to the screen
  69.  
  70. All of us have heard complaints about the color constraints on C64.
  71. FLI picture can have all of the 16 colors in one character position.
  72. What then is this FLI and how it is done ? Written by Pasi 'Albert' Ojala.
  73.  
  74. RS-232 Converter
  75.  
  76.   This article details plan for a User port TO RS232 connector using just ONE
  77. IC and 4 capacitors. The circuit is included, and suggestions on alternative
  78. chips and parts are examined.  Written by Warren Tustin
  79.  
  80. Introduction to the VIC-II
  81.  
  82.   This article examines the VIC-II chip in detail and provides an explanation
  83. of the various registers associated with the chip. Written by Pasi 'Albert'
  84. Ojala.
  85.  
  86. LITTLE RED READER: MS-DOS file reader for the 128 and 1571/81 drives.
  87.  
  88. This article presents a program that reads MS-DOS files and the root directory
  89. of MS-DOS disks.  This program copies files from disk to disk so two disk
  90. drives are required to use it (or a "virtual" drive).  This scheme imposes no
  91. limit on the maximum size of a file to be transferred.  The user-interface
  92. code is written in BASIC and presents a full-screen file selection menu.  The
  93. grunt-work code is written in assembly language and operates at maximum
  94. velosity.  Complete, explained code lisitings are included.  By Craig Bruce.
  95.  
  96. =============================================================================
  97. Learning Machine Language - Part 4
  98. by Craig Taylor (duck@pembvax1.pembroke.edu)
  99.  
  100.                      +---------------------------+
  101.                      | Space Invasion - Part 1   |
  102.                      |                           |
  103.                      | Programming: Craig Taylor |
  104.                      | Graphics   : Pasi Ojala   |
  105.                      | Music/Sound:              |
  106.                      |                           |
  107.                      +---------------------------+
  108.  
  109. I. Introduction
  110.    ------------
  111.  
  112.   In this and future Learning Machine Language's we will develop a game called
  113.   Space Invasion. The game will be similair to Space Indvaders and will run on
  114.   the Commodore 64 or the Commodore 128 in 80 columns. It will feature all the
  115.   "features" and "parts" that are found in commercial games with interrupt-
  116.   driven music, custom character definitions, 100% machine language, multi-level
  117.   game play, and input from the keyboard, joystick or mouse.
  118.  
  119.   Note | I am looking for someone to help aid music composition that will
  120.   -----+ be introduced in a later issue. Programming of the 6502 is helpful
  121.   but not a requirement. Please email me at duck@pembvax1.pembroke.edu if
  122.   you are interested.
  123.  
  124.   Many thanks to Pasi Ojala for his work with the graphics in this program.
  125.  
  126.   Also please note: This entire program has been assembled sucessfully with 
  127.   the Buddy-128 assembler for both the C=128 and C=64 version. Due to the
  128.   length of the source files (over 1,500 lines) I'm not sure if Buddy-64 will
  129.   handle it. Thus if you get errors during assembly, all I can say is: sorry.
  130.   If this is the case then the next issue will handle dividing the program and
  131.   data up into segments which can then each be loaded seperatly.
  132.  
  133.  
  134. II. Machine Notes
  135.     -------------
  136.  
  137.   The Commodore 64 and 128 programs for Space Invasion will differ slightly,
  138.   mostly in the following areas:
  139.  
  140.        - custom character definition
  141.        - memory initialization / setup
  142.        - sound / music
  143.  
  144.   Because the actual game play and the changes nesscesary between the areas 
  145.   listed above, we will use the Buddy Assembler notation for conditional 
  146.   assembly to allow the development of only one file containing the source
  147.   code. In addition to conditional assembly most of the routines will be
  148.   written as one with jumps to subroutines containing the C64 or 128 direct
  149.   code as the algorithims are usually the same for each.
  150.  
  151.   In addation there will be several source files and some miscellaneous include
  152.   files for graphics and sound. For those of you who are or will be converting
  153.   the assembly source over to a different assembler the conditional 
  154.   assembly directives .if (condition) will only be true if the condition is
  155.   non-zero. Ie: if the symbol computer is defined as 128 then the following
  156.   example illustrates it:
  157.  
  158.        computer = 128
  159.        .if computer-64         ; non-zero answer so therefore
  160.        ; 128 code goes here
  161.        .else
  162.        ; 64 code goes here
  163.        .ife                    ; end the .if condition.
  164.  
  165.   Also note that for much of the program we will _not_ be using the computer
  166.   routines and instead be developing our own.
  167.  
  168.   In addition the program will show you how to use IRQ interrupts to simplify
  169.   programming. We will be using them to play music in the background on three
  170.   voices (sound effects will temporarily pre-empt the third voice from playing).
  171.   Also animation of characters will be done via the IRQ. A little background
  172.   on interrupts for those of you who are a bit hazy on what they are or have
  173.   never seen them before (Also try taking a look at Rasters: What they are and
  174.   how to use them in C= Hacking #3 - While this does not necessarily cover
  175.   what we are going to be using interrupts for it does describe them quite 
  176.   well.) Basically the computer generates an interrupt every 1/60th a second
  177.   from a timer on the computer (usually from the CIA chip or the screen for 
  178.   those of you who are curious). The computer will save all the registers, jump
  179.   to a subroutine - perform the instructions there (usually updating time,
  180.   scanning the keyboard etc...) and then recall all the registers and return
  181.   to the user program. This is an interrupt. An IRQ interrupt describes an
  182.   interrupt that we can allow to be "turned on" and "turned off" - ie: we
  183.   can temporarily disable it if we have to. A NMI interrupt describes an
  184.   interrupt which we can _not_ temporarily disable -- we will not be using
  185.   NMI interrupts in this program.
  186.  
  187.   
  188. III. The Process
  189.      -----------
  190.  
  191.   Part of what this series of articles is focused at is the development of being
  192.   able to analyze programming tasks and break them down into smaller workable
  193.   problems. Once these problems or subroutines are completed your original
  194.   problem is solved.
  195.  
  196.   Let's take this approach to Space Invasion:
  197.  
  198.   Problem Statement: Build a Space Invader program called Space Invasion.
  199.   -----------------
  200.  
  201.   Usually, given a problem you have to re-work the problem statement to 
  202.   encompass all of what you want. Let's try again:
  203.  
  204.   Problem Statement: Develop a Space Invader program called Space Invasion
  205.   -----------------  utilizing the 64 or 128 screen with interrupt driven
  206.                      music / sound, and allowing input from the keyboard, 
  207.                      joystick or mouse.
  208.  
  209.   Hmmm... The problem statement listed above is better but it has no real order;
  210.   we have no clear idea of where to start and what we need to do. It does 
  211.   however tell us that we have the following sections:
  212.  
  213.        - 64 / 128 Screen Handling
  214.        - Music / Sound
  215.        - Input Handling
  216.        - Game Driver (implied)
  217.  
  218.   Let's think a bit more about each of these sections and what each will
  219.   involve:
  220.  
  221.   128 / 64 Screen Handling:      - Putting characters on screen.
  222.   ------------------------       - Initializing the Screen / Registers.
  223.                                  - Setting up the Custom Characters.
  224.                                  - Handling any Animation.
  225.  
  226.   Music / Sound:                 - Setting up the Sound Chip Registers.
  227.   -------------                  - Playing a note read from Memory.
  228.                                  - Executing a Sound Effect.
  229.  
  230.   Input Handling:                - Device Selection (keyboard, mouse, joystick).
  231.   --------------                 - Keyboard Scanning.
  232.                                  - Mouse Scanning.
  233.                                  - Joystick Scanning.
  234.  
  235.   Game Driver:                   - Title Screen.
  236.   -----------                    - Initialization of Memory.
  237.                                  - Level Setup.
  238.                                  - Movement of Aliens.
  239.                                  - Movement of Missles.
  240.                                  - Movement of Player.
  241.                                  - Collision Checking.
  242.                                  - Collision Handling.
  243.                                  - End of Level.
  244.                                  - Score Updating.
  245.                                  - End - Game handling.
  246.                                  - High Score Update.
  247.  
  248.   Shrew! Long list 'eh? - Now you may have thought of some not listed above,
  249.   and we may have possibly overlooked some crucial routines -- that's fine --
  250.   the above is just intended as a building block - a place to start coding from.
  251.  
  252.   If we think of these as subroutines we can build a skeleton outline of the 
  253.   program - yet we need some order in how we call them. Obviously we aren't
  254.   going to move the player until we scan the input and that requires prior 
  255.   device selection etc... 
  256.  
  257.   Hmm... Taking order into account we can re-state the problem as:
  258.  
  259.   Problem Statement: Develop a game similair to Space Invaders called Space
  260.   -----------------  Invasion by initializing memory, the display device,
  261.                      setting up Custom Characters, setting up the Music
  262.                      Registers and displaying the title screen. From there,
  263.                      select the input device and after that setup the current
  264.                      level. Next, while playing music in the background and
  265.                      scanning the input device, move the aliens, missles and
  266.                      player checking for collisions and taking appropriate
  267.                      action as required (player dies, score increases etc or
  268.                      what-not). After each level display if the player is dead,
  269.                      or set-up for the next level and repeat. When the game has
  270.                      ended update the high score if necessary.
  271.  
  272.   Try saying that five times real fast! :-) But that problem statement is a
  273.   whole lot better than the one we had at the beginning which simply said to 
  274.   develop a game.
  275.  
  276.  
  277. IV. Not All At One Time - What We're Doing This Time
  278.     ------------------------------------------------
  279.  
  280.   Now this program is too complex, (as seen by the problem statement above) to
  281.   have in one article so this issue we'll concentrate on the basic main loop
  282.   and the initialization of the Custom Characters and the title screen. 
  283.  
  284.   Originally, I was planning on updating and listing the revised code in each
  285.   issue. However, due to space limitations and the enormity of the program 
  286.   currently (1,500+ lines!!) it will be placed for anonymous ftp at 
  287.   ccosun.caltech.edu under the directory: pub/rknop/HACKING.MAG.
  288.  
  289.  
  290. V. The Main Loop
  291.    -------------
  292.  
  293.   What is a main loop? Basically it's where everything gets done. It calls other
  294.   subroutines and keeps repeating until certain criteria are met - usually when
  295.   the player requests to exit the game. However, inside you'll find inner loops
  296.   for level play etc. 
  297.  
  298.   Our main loop for this program will be:
  299.  
  300. -------------------------------------------------------------------------------
  301. ;; * Main Loop - This should be the last section in the source code.
  302. ;
  303. ; Main Loop
  304. ;
  305.  
  306. main'loop = *
  307.                jsr memory'setup        ; Set-Up memory.
  308.                jsr display'setup       ;  "  "  display.
  309.                jsr char'setup          ;  "  "  custom character display.
  310.                jsr music'setup         ;  "  "  music chip.
  311.                jsr title'screen        ; Display the title screen.
  312.                jsr select'input        ; Select Input Device.
  313.  
  314. level'loop = *
  315.                jsr play'music          ; Start the music playing.
  316.                jsr setup'level         ; Setup the current level.
  317.  
  318.              - jsr alien'move          ; Move aliens
  319.                jsr missle'move         ;  "   missles
  320.                jsr player'move         ;  "   player
  321.                jsr check'collision     ; Check for collisions
  322.                ldx collision'flag      ; Check collision flag.
  323.                beq -
  324.  
  325.                dex                     ; Decrease .X by 1 so if X was 1 then
  326.                beq player'die          ;    it's now 0 so we know player died.
  327.                dex                     ; Decrease .X again so if X was 2 then
  328.                beq alien'die'sound     ;    it's now 0 so we make alien death.
  329.                jsr end'level           ; If we got here - than end of level.
  330.                jsr wait'next           ; Wait for next keypress.
  331.                jsr increase'level      ; increase level #.
  332.                sec                     ; And go back....
  333.                bcs level'loop             
  334.  
  335. alien'die'sound = *
  336.                jsr make'alien'sound    ; make alien sound.
  337.                sec                     ; set carry 
  338.                bcs -                   ; and jump back.
  339.  
  340. player'die     jsr show'player'die     ; Show it on-screen.
  341.                lda lives               ; Check # of lives.
  342.                beq end'of'game         ; If 0 the end-of-game.
  343.                bne level'loop          ; go back and re-start level.
  344.                brk                     ; If we get here - than an error.
  345.  
  346. end'of'game    jsr end'game'screen     ; Show end-of-game screen.
  347.                jsr high'score'update   ; Update the high score if need-be.
  348.                jsr wait'next           ; Wait for next-game selection.
  349.                lda quit
  350.                beq +
  351.                jsr setup'level'1       ; Set-Up first level.
  352.                sec
  353.                bcs level'loop          ; and start playing it.
  354.  
  355.              + jmp quit'game
  356. ;
  357. ; End of Main Loop
  358. ;
  359. -------------------------------------------------------------------------------
  360.  
  361.   Some of the routines listed above we will later replace with actual code. It's
  362.   much easier to see:
  363.  
  364.                inc level 
  365.  
  366.   than to see a 
  367.  
  368.                jsr increase'level
  369.  
  370.   and try to hunt down the code. I've included them in for now so that we can
  371.   have a better idea of what is going on.
  372.  
  373.   In the file: invasion.src most of the statements above are commented out. 
  374.   Once we write the routines we'll un-comment them. For now, this serves to
  375.   still remind us of the routines we need to write.
  376.  
  377.   Also there are a couple of programming tricks that I used in the main loop 
  378.   that probably need some clarifying.
  379.  
  380.   When handling the collisions the .X register is loaded with the result of the
  381.   collision checking - $00 = no collisions, $01 = player died, $02 = alien died,
  382.   $03 = end of level. Anytime a load to a register is done the flags are 
  383.   automatically set as if you had compared it to 0 - hence we can ldx the 
  384.   collision flag and immediately branch if equal to zero for no collisions. In
  385.   addition to the load anytime the .X or .Y registers are incremented or 
  386.   decremented an implicit comparison to zero is performed. So if the .X register
  387.   is 1 previously, we decrement it then it will be zero and our BEQ instruction
  388.   will branch. If it's two then it will be one and we can continue like this.
  389.     [NOTE: Technically it's not a real comparison to zero but calling it a
  390.     comparison to zero servers our purpose here. The only significant difference
  391.     would be in the effect of the carry flag which is insignificant in our 
  392.     code segment here.]
  393.  
  394.   Also in several locations are the two instructions:
  395.  
  396.                sec
  397.                bcs [label]
  398.  
  399.   What these are doing are simply programming style - they could be substituted
  400.   with JMP [label] - however they offer advantages over JMP.  They take up the
  401.   a larger amount of execution time, however they are relocatable so any mucking
  402.   around / moving sections of code during debugging will be less likely to
  403.   crash. Using other flags are also valid -- the use of which flag (I prefer
  404.   the carry flag) is usually dependent on the programmer. Geos defines a
  405.   similair macro called BRA (branch always) which is equivlent to:
  406.  
  407.                clv
  408.                bvc [label]
  409.   
  410.   Note that the above is just programming style, held over from my programming
  411.   in assembly days. The use of JMP is probably preferable in terms of 
  412.   execution and also in being able to branch more than 127 bytes away (the 
  413.   branch instructions only have a range of +128/-127).
  414.  
  415.  
  416. VI. Custom Characters
  417.     -----------------
  418.  
  419.   Since we're writing for each of the seperate modes (64 mode, 128 mode) we have
  420.   to take a look at the differences between the VIC chip (64 mode) and the 8563
  421.   chip in the 128.  
  422.  
  423.   The Vic-Chip
  424.   ------------
  425.   The character sets in the VIC chip are defined as in the example below of 
  426.   the character code $00 "@" (all references are to screen "poke" codes - not
  427.   print codes).
  428.  
  429.          .byt #%00111100    Try holding the page (or moving away from the
  430.          .byt #%01100110    screen) and taking a look at the patterns the 1's
  431.          .byt #%01101110    and 0's make. Each character is thus defined as 
  432.          .byt #%01101110    eight bytes who's bit patterns define it.  Having a
  433.          .byt #%01100000    total of 256 characters available makes it
  434.          .byt #%01100010    neccesary to set aside a total of 2,048 bytes. 
  435.          .byt #%00111100
  436.          .byt #%00000000
  437.  
  438.   Now, instead of designing all 256 character sets we'll just take advantage of
  439.   the fact that the letters and numbers we want will already be there -- we'll
  440.   just copy them from the ROM set into RAM, modify some of the other characters
  441.   to reflect what we want and then tell the VIC chip to look at RAM to get the
  442.   character set definitions.
  443.  
  444.   There are some problems with copying the 'system' characters, however. The
  445.   Commodore 64 usually masks out the character set and typically it is only
  446.   available to the VIC chip so that more space can be present for user programs
  447.   and such.  It also takes up the section of memory that the I/O block in 
  448.   $d000-$dfff does so that switching it in while interrupts are enabled is sure
  449.   to result in a crash. 
  450.  
  451.   We're also going to be doing a few things that you may not expect -- instead
  452.   of copying all 256 characters - we're gonna _just_ copy the first 128. This
  453.   will give us all of the normal characters as the last 128 are the reverse-
  454.   video counterparts to the first 128 characters. We're doing this to conserve
  455.   space and because we really don't need that many characters defined.
  456.  
  457.   Also location $01 contains what $d000-$dfff holds and we will have to modify
  458.   bit 2 to switch the character ROM in. Hence, the following program code is
  459.   used to copy the character set:
  460.  
  461. -------------------------------------------------------------------------------
  462. copy'chars = *                 ; must be run w/ interrupts disabled
  463.                lda $01         ; register 1 = the control to switch in the char.
  464.                                ; rom.
  465.                pha             ; save it as we'll later need to sta' it back.
  466.                and #%11111011  ; Bit 2 controls it - clear it to switch it in.
  467.                sta $01         ; and make it so we can read it in.
  468.                lda #>$3000     ; move chars to $3000
  469.                sta dest+1
  470.                lda #>$d800     ; from $d800 (start of char set) (lower-case)
  471.                sta src+1
  472.                ldy #$00        ; lo-bytes of both src, dest = $00.
  473.                sty src
  474.                sty dest
  475.                ldx #$10        ; copy 2k of data.
  476.              - lda (src),y     ; copy byte.
  477.                sta (dest),y
  478.                iny
  479.                bne -           ; continue until .Y = 0.
  480.                inc src+1       ; increase source & dest by 256
  481.                inc dest+1
  482.                dex             ; decrease .X count.
  483.                bne -           ; if non-zero then continue copying, else
  484.                pla             ; restore value of $01 
  485.                sta $01         ; and put back.
  486.                lda $d018       ; set VIC-chip address.
  487.                and #$f1        ; to show char set.
  488.                ora #$0c       
  489.                sta $d018       ; and finally tell VIC where the char set is...
  490.                rts             ; and return.
  491. -------------------------------------------------------------------------------
  492.   
  493.   Note that we still need to change the actual characters we're gonna be using.
  494.   That will be handled in the section after next: Changing the Characters as 
  495.   there is a great deal of similarity between the 128 and 64 implementations.
  496.  
  497.   The 8563 Chip
  498.   -------------
  499.   The 8563 80-Column chip usually has 16k or 64k Ram attatched to the chip 
  500.   which the CPU does not have direct control over. It has to direct the 8563 
  501.   to store and retrieve values to that memory. What makes control over that 
  502.   memory all the more difficult is the fact that the 8563 only has two lines
  503.   or addresses that the CPU can control.  
  504.  
  505.   The 8563 has a character set in much the same way the VIC chip does, save
  506.   one exception - each character set can have up to 16 lines. Normally, the last
  507.   eight lines are filled with $00 and are not shown. (Provisions can be made to
  508.   have 8x16 characters but it is not needed for this game and thus, will not be
  509.   shown - For more information See C= Hacking Issue #2: 8563: An In-Depth Look.)
  510.   Thus the algorithim is similair to the C=64 but 8 zero-bytes will need to be
  511.   written at the end of every eight bytes read.
  512.  
  513.   However, the 8563 does make things easier for us! - When the computer is first
  514.   turned on a copy of the Character Set from ROM is copied into the 8563. The
  515.   8563 has no ROM Character Set associated with it and thus we are able to just
  516.   simply modify the character set that is in the 8563 memory instead of copying
  517.   it over.  Because of this no routine will be presented to copy a character
  518.   set into the 8563 memory, rather the discussion of copying individually 
  519.   defined characters will take place in the next section. The C=128 also makes
  520.   life even easier for us at the end when we will exit the program, 
  521.   modifying the character set back to the "standard" Commodore character set
  522.   by a routine in the KERNAL that will copy the characters back. We'll take a
  523.   look at it closer when we write the exit routine.
  524.  
  525.   Also note that since the 8563 chip supports the 80 column screen we will
  526.   be defining two characters that can be placed side by side for each alien
  527.   so that the playing field will be similair to the C64 version. However, for
  528.   the title screen we will be switching the 8563 into a "40 column" mode
  529.   to make programming easier, in addition to expanding the character bit-mapped
  530.   logo.
  531.   
  532.   Changing the Characters
  533.   -----------------------
  534.   A lot of the times you'll find yourself re-using subroutines and code that
  535.   you have previously created, gradually, over a period of time building up
  536.   a library of routines. When thinking through the purpose and intent of this
  537.   routine I thought about possibly building it so it would read a table and
  538.   change the character set based on that table. The 64/128 character sets
  539.   would be the same - this routine would automatically generate the eight 
  540.   additional bytes needed by the 8563 if need-be and it would call the 
  541.   appropriate storage routine - store to either the 8563 or the computer
  542.   memory. 
  543.  
  544.   Now you may be asking why would you want to store to the computer memory
  545.   in 128 mode? Why not just have two seperate versions? - Yes - that could
  546.   be possible but I'm implementing it this way because in the future I may
  547.   see a need to define custom characters in 128 mode for the 40 column screen.
  548.   This way I can just extract the routine, pop it into my program and I've got
  549.   that section of the code complete. 
  550.  
  551.   This is what I was thinking of for the data table:
  552.  
  553.       .byt 1 = 8563, 0 = comp. memory.
  554.       .word address ; address base of char-set in computer or 8563 memory.
  555.       .byte char #  ; (to start)
  556.       .byte # of chars to define
  557.       .byte # of characters to define
  558.       .byte data,data,....,data8 ; character data.
  559.       .byte data,data,....,data8 ; character data. etc....
  560.       . . .
  561.    
  562.   Entrance into the routine will consist of .AY holding the location of the 
  563.   table.  We will keep the address of the table and keep incrementing it as
  564.   we go along in z-page locations.
  565.  
  566. -------------------------------------------------------------------------------
  567.        install'char = *
  568.                sta zp1                 ; save .ay in table address
  569.                sty zp1+1
  570.                ldy #$00                ; read computer mode.
  571.                jsr get'byte
  572.                sta mode
  573.                jsr get'byte            ; get address base.
  574.                sta adr
  575.                jsr get'byte
  576.                sta adr+1
  577.                jsr get'byte            ; get number of characters to copy.
  578.                sta numb            
  579.                jsr get'byte            ; get next character #.  
  580.                sta wrk                 ; save in temp. location.
  581.                lda #$00
  582.                sta wrk+1
  583.                asl wrk                 ; shift left x3 times = *8
  584.                rol wrk+1
  585.                asl wrk
  586.                rol wrk+1
  587.                asl wrk
  588.                rol wrk+1
  589.                lda mode                ; if for 8563 then multiply 1 more time.
  590.                beq +      
  591.                asl wrk
  592.                rol wrk+1
  593.              + lda adr                 ; add character address in.
  594.                clc
  595.                adc wrk
  596.                sta wrk
  597.                lda adr+1
  598.                adc wrk+1
  599.                sta wrk+1               ; address now calculated
  600.                jsr setadrs             ; set address in proper chip
  601.   loop'install ldx #$08                ; copy 8 bytes.
  602.              - jsr get'byte
  603.                jsr writebyte           ; write out byte.
  604.                dex
  605.                bne -
  606.                lda mode                ; if 128 then fill out 8 more $00 bytes.
  607.                beq +
  608.                lda #$00
  609.                ldx #$08
  610.              - jsr writebyte
  611.                dex
  612.                bne -
  613.              + dec numb
  614.                bne loop'install
  615.                rts       
  616. -------------------------------------------------------------------------------
  617.  
  618.   What? We have thre subroutines : writebyte, setadrs, and get'byte that we
  619.   haven't examined yet. These are going to be the routines that are dependant
  620.   on the computer type. Also, writebyte will require that .XY not be disturbed;
  621.   setadrs requires that .Y not be disturbed hence the following:
  622.  
  623. -------------------------------------------------------------------------------
  624.        setadrs tya             ; save .yx
  625.                pha
  626.                txa
  627.                pha
  628.                lda mode        ; check computer type.
  629.                beq +           ; if C=64, then jump ahead.
  630.                ldx #18         ; VDC register - current memory address hi
  631.                lda wrk+1       ; get address hi
  632.                jsr wr'vdc
  633.                ldx #19         ; VDC register - current memory address lo
  634.                lda wrk         ; get address lo
  635.                jsr wr'vdc
  636.              + pla             ; restore .XY
  637.                tax
  638.                pla
  639.                tay
  640.                rts             ; and return.
  641. -------------------------------------------------------------------------------
  642.   
  643.   Note that we really don't need a setadrs for the C=64 -- we can just index 
  644.   off (wrk) in the writebyte routine which follows:
  645.  
  646. -------------------------------------------------------------------------------
  647.   writebyte    sta temp        ; save as we need it later.
  648.                txa             ; Save .XY
  649.                pha
  650.                tya
  651.                pha
  652.                lda mode        ; now check computer type.
  653.                beq +           ; if c64 jump ahead
  654.                lda temp        ; recall temp.
  655.                jsr wr'vram 
  656.                sec
  657.                bcs ++          ; jump ahead
  658.              + ldy #$00        ; C64 / y-index = $00
  659.                lda temp        ; get value
  660.                sta (wrk),y     ; store
  661.                inc wrk         ; now increase address
  662.                bne +
  663.                inc wrk+1
  664.              + pla             ; now return after recalling .XY
  665.                tay
  666.                pla
  667.                tax
  668.                rts             ; and return.
  669. -------------------------------------------------------------------------------
  670.  
  671.   Note that the following routine is fairly short but it is called numerous
  672.   times within the routines that use data tables such as install'char, 
  673.   write'txt and write'col. 
  674.  
  675. -------------------------------------------------------------------------------
  676.   get'byte = *
  677.                lda (zp1),y
  678.                iny
  679.                bne +           ; if zero then increase zp1 hi
  680.                inc zp1+1
  681.              + rts
  682. -------------------------------------------------------------------------------
  683.  
  684.   Not bad 'eh?  A quick note: The instructions: PLA, TAY, PLA, TAX, PHA, etc.. 
  685.   are routines that Push or Pull (pha,pla) the .A onto the stack. The TAY, TAX,
  686.   TXA, TYA are instructions that transfer a register to another (ie: the TAY
  687.   transfers the A register to .Y, TXA transfers .X to .A etc...) By using the
  688.   combination of these with the stack we can save the registers and later
  689.   re-call them so that they are the same when we entered the routine. The
  690.   stack is usually a "mystery" item to new programmers of the 6502 series. 
  691.   Basically it's just like any other stacks in the real world - the last item
  692.   thrown (I'm non-practicing perfectionist so I throw stuff.. ;-) ) or pushed
  693.   on the stack will the first item removed or pulled from the stack. For 
  694.   example I've got a stack of books sitting near me :
  695.  
  696.                Mapping the Commodore 128
  697.                128 Internals
  698.  
  699.   and I'm holding Mapping the Commodore 64 in my hands. If I push (or toss)
  700.   the book onto the stack (and hopefully hit the stack instead of the floor)
  701.   I'll have the following stack:
  702.  
  703.                Mapping the Commodore 64
  704.                Mapping the Commodore 128
  705.                128 Internals
  706.  
  707.   and it should be easy to see that if I "pull" the next book off the stack
  708.   that I'll get the Mapping the Commodore 64 book. The next book to be "pull"ed
  709.   after that would be the Mapping the Commodore 128 book. This idea can be
  710.   applied to the 6502 stack -- It will keep storing values (up to 256) when you
  711.   "push" them on (via the PHA instruction) and will retrieve the last value
  712.   stored when you "pull" them off (via the PLA instruction). Another PLA
  713.   instruction would return the next value that had been stored.
  714.  
  715.   The Character Bitmaps
  716.   ---------------------
  717.  
  718.   Pasi Ojala is to be credited with all the graphics and many thanks go out
  719.   to him.
  720.  
  721.   The game logo is made up of 120 custom defined characters that will be 
  722.   printed in the following manner (on the 128 screen they will be centered).
  723.  
  724.   (in reverse video)...
  725.  
  726.            ABCDEFGH . . . [up to 40 characters]
  727.            IJKLMNOP
  728.            QRSTUVWX    
  729.  
  730.   and everything will line up.
  731.  
  732.   So that it will look like a "mini-bitmap".  We could have used bitmap mode
  733.   and made a very nice looking title screen but that would have involved 
  734.   switching and allocating memory for the bitmap, etc . . . On both the
  735.   8563 and the VIC that involves a bit more work and so custom characters
  736.   will be used for the title screen. The regular letter and numeric characters
  737.   will be available so that we can display credits and game instructions
  738.   below the logo.
  739.  
  740.   Now - in the program listing we could list them as binary #'s and that would
  741.   make editing them very easy but we're gonna use their decimal representation
  742.   in the program listing.
  743.  
  744.   The characters are defined similair to the logo except they are treated as
  745.   single characters. In the 128 version due to the 80 column screen we are
  746.   going to use two characters side by side to simulate one alien so that the
  747.   playing field will be similair to the C64 version. In addation, during the
  748.   main loop we will modify the character sets to support animation of the 
  749.   aliens. In the data listing there is a reference to "frames" - for each of
  750.   the aliens there are 8 differant frames.
  751.  
  752.   Oh! - There will be more characters defined in the future. Right now I'm 
  753.   mainly interested in getting some base characters down so you can see how
  754.   custom characters are implemented.  When we start setting up different levels
  755.   and such we'll add more characters then. Currently the custom characters
  756.   are not used - only the characters for the logo. For those of you who are
  757.   curious try installing the characters via install'char and taking a look
  758.   at the aliens.
  759.  
  760.  
  761. VII. Title Screen
  762.      ------------
  763.  
  764.   The title screen is usually a lead-in to the actual game and it's aim is
  765.   to tell the player how to play the game, any available options and p'haps
  766.   present a nice graphic or two to "wow" the user into playing. In addation,
  767.   the main musical theme can be introduced here to unify the game-playing.
  768.   The discussion below does not take into account color but rest-assured we
  769.   will be using varying colors in the title screen. The format for the color
  770.   data will be almost identical to the title screen format except it will
  771.   be structured via the following:
  772.  
  773.        .word address
  774.        .byte num_of_chars to put color ($00= end of data)
  775.        .byte color_value
  776.  
  777.   The routine (color'text) can be found in the source listings at the end of
  778.   this article. Because of the similarity between it and write'text it is
  779.   not discussed in this article.
  780.  
  781.   Title Screen BackGround
  782.   -----------------------
  783.   The title screen I envisoned as a bordered screen (using the normal C= 
  784.   character set - ie: C= A,S,Z,X on the keyboard) with our bitmap in the middle
  785.   and under-neath it a short description of the game and game-play instructions.
  786.  
  787.   Now this is my idea of the screen layout (rough drawing as we're not using
  788.   the actual screen dimensions):
  789.  
  790.      +-------------------------------------------------------------+
  791.      | -LOGO ----------------------------------------------------- |
  792.      | --------------x 3 lines------------------------------------ |
  793.      | ----------------------------------------------------------- |
  794.      |                                                             |
  795.      |                   Space Invasion C64/128                    |
  796.      |                     Programming : Craig Taylor              |
  797.      |                     Graphics    : Pasi Ojala                |
  798.      |                     Sound       : ????????????              |
  799.      |                                                             |
  800.      | ----------------------------------------------------------- |
  801.      | To Play:                                                    |
  802.      |    Use joystick in port 2, mouse in port 1 or keyboard:     |
  803.      |            A - Left, Z - Right   Space - Fire               |
  804.      |              F1 - Restart                                   |
  805.      |                                                             |
  806.      +-------------------------------------------------------------+
  807.  
  808.   Title Screen Formatting
  809.   -----------------------
  810.   We come into a problem here -- the screen is some 1000 characters on the C64,
  811.   and 2000 characters for the C=128.  It would be extremely wasteful to store
  812.   that many characters in memory just to reproduce a title screen - and most of
  813.   them consisting of spaces at that!!  
  814.  
  815.   What we'll do is to just specify the address on screen, the # of characters
  816.   and then list the characters. It will be similair to our custom character
  817.   table driver above but will be different enough that a new routine is 
  818.   warrented. We will however use the two subroutines writebyte and setadrs
  819.   that were developed in the previous routine. The data will look like the
  820.   following:
  821.  
  822.        .word address
  823.        .byte num_of_chars ($00= end of data)
  824.        .ascii "text"
  825.        .byte address .... etc.... 
  826.  
  827.   and we'll enter with .AY containing the address of the table.
  828.  
  829.   So basically we come up with the following:
  830.  
  831. -------------------------------------------------------------------------------
  832.        write'txt = *
  833.                sta zp1         ; save .ay in table address
  834.                sty zp1+1
  835.                ldy #$00
  836.    loop'w'text = *
  837.                jsr get'byte   ; set address.
  838.                sta wrk
  839.                jsr get'byte
  840.                sta wrk+1
  841.                jsr get'byte    ; get # of chars to write out.
  842.                cmp #$00
  843.                beq +           ; if zero then exit.
  844.                tax
  845.                jsr setadrs     ; set address to wrk,wrk+1
  846.              - jsr get'byte
  847.                jsr writebyte   ; write out byte.
  848.                dex
  849.                bne -
  850.                sec
  851.                bcs loop'w'text ; this is an absolute jump to loop 
  852.              + rts             ; return.
  853.  
  854. -------------------------------------------------------------------------------
  855.  
  856.   This is similair to our previous routine, and was in fact copied and modified 
  857.   from the previous routine. 
  858.  
  859.  
  860. VIII. Debugging
  861.       ---------
  862.  
  863.   Now, not all programs are perfect, and during the development of this 
  864.   portion of the game there were several errors found. Tracing an error in
  865.   Machine/Assembly-Language is like trying to find a grammatical error in a
  866.   language you don't know. ;-) But seriously, there are several ways to track
  867.   down errors in your code.
  868.  
  869.   1 - Try tracing it through by hand playing "What if I were the computer" and
  870.       following what each register does.
  871.  
  872.   2 - Are you switching the LoHi order of variables? Ie: is it lda #< or 
  873.       lda #>?? 
  874.  
  875.   3 - Set BRK points and run the program / subroutine within a machine language
  876.       monitor and make sure the registers / memory locations contain the values
  877.       that they should. If not, find out why.
  878.  
  879.   4 - Try to simplify your code in terms of programming ease - Make the 
  880.       assembler do the work for you - it's a lot less likely to make errors 
  881.       than you are.
  882.   
  883.   5 - Think logically!!!
  884.   
  885.   6 - Change something at random and pray.
  886.  
  887.   I can't stress numbers 3 and 5 enough. During the writing of the install'char 
  888.   routine there were numerous bugs that were eventually tracked down by 
  889.   setting a BRK instruction further along in the code and seeing exactly what
  890.   the register / memory locations were. Also the use of temporary load and
  891.   store instructions into "safe" regions of memory helped me monitor what some
  892.   of the values were.
  893.  
  894.   For example, at one point I had a section of code similair to the following:
  895.  
  896.              clc
  897.              lda value
  898.              adc data
  899.              bne +
  900.              inc data+1
  901.            + [.... ]
  902.  
  903.   And it's purpose was to add value to data. Now I've found simple errors are
  904.   usually found last, after complex errors. And not until a set a break point
  905.   like:
  906.  
  907.              clc
  908.              lda value
  909.              adc data    <-----Missing Instruction after here-------------+
  910.              bne +                                                        |
  911.              inc data+1                                                   |
  912.            + BRK                                                          |
  913.              [.... ]                                                      |   
  914.                                                                           |
  915.   did I actually figure out that I was missing the STA DATA instruction --+
  916.  
  917.   So, when writing, modifying, and trying to debug code try to take your time
  918.   and isolate every possible problem. Also don't be afraid to stop the code
  919.   mid-stream as in the above with use of the BRK. You can always remove it
  920.   (and probahly should) in the final code and it serves as a very valuable 
  921.   debugging tool with the aid of a machine-language monitor.
  922.  
  923.   
  924. IX. Memory Map Considerations
  925.     -------------------------
  926.  
  927.   Before you start a program it's a good idea to consider where in memory you
  928.   will have everything. Now we've already started some of the program above
  929.   and just blindly picked numbers at random it seemed like $3000 for the 
  930.   character set for the C=64 etc... We didn't - I'm introducing the Memory
  931.   Map Considerations here to show the example of what if we didn't think
  932.   about how memory was going to be organized.
  933.  
  934.   The C=64 only has 64k of memory of which typically the range $0800-$a000
  935.   is available and $c000-$cfff is also.  If we had blindly picked numbers
  936.   all over the place to store our code then we would have a disorganized
  937.   program that would most likely accidentally use one subroutines storage
  938.   as temporary data for another. It's like shooting randomly in Laser Tag
  939.   not checking to see if there is a target there or not first... The end
  940.   result: Chaos.
  941.  
  942.   Currently we're not following the rule for "temporary variables" but as
  943.   we gradually fade out of the normal C-64/128 default mode and write our
  944.   own routines / interrupt handlers we'll switch things over. Also, on the
  945.   C=128 instead of using Bank 0 with the I/O block enabled we're currently
  946.   using the BANK 15 configuration as the program doesn't extend past $4000
  947.   yet ($0000-$4000 is common memory in the normal C=128 configuartion).
  948.  
  949.   64 Considerations
  950.   -----------------
  951.   The 64 will have free memory in the following areas: $0800-$a000, and
  952.   $c000-$cfff. However, if we disable the Basic Rom we can have the whole
  953.   area from $0800-$cfff free for our program. Because we don't need the
  954.   Basic Rom we will do just that (in the listing now we currently won't but 
  955.   it will be done in a future issue). Therefore having the character set
  956.   at $3000-$5000, the music data at $5000-$8000, the program will have the
  957.   area free from $8000-$cfff. $0800-$3000 will be available if needed for
  958.   routines who need temporary storage. 
  959.  
  960.   Temporary Storage is going to be defined as follows. Each routine that needs
  961.   temporary storage will be assigned a "level" number. The lower levels will
  962.   be assigned level 1 on up to level 3. The range $0800-$3000 will be 
  963.   broken down into the following sub-ranges.
  964.  
  965.        Level 1: $0800-$1000
  966.        Level 2: $1000-$1800
  967.        Level 3: $1800-$3000
  968.   
  969.   This way when writing the sub-routines we can be assured that a section of
  970.   memory is not overwritten by a subroutine we call. When we actually start
  971.   programming we'll decide where in each sub-range the routine will have 
  972.   access to.
  973.  
  974.   128 Considerations
  975.   ------------------
  976.   The 128 has two "banks" of 64k each. Normally for large programs we would 
  977.   think about using both banks - (from the idea: Hey! - We got it, why not
  978.   flaunt it?) but we won't be using both banks.
  979.  
  980.   Free memory on the C128 typically consists of the range $0400-$09ff
  981.   (where we'll be overwriting the 40 column screen (which we're gonna blank
  982.   anyway) and the Basic run-time stack.) Also the area from $0b00-$0fff
  983.   is free (overwriting the tape area, the rs-232 buffers,l and the sprite
  984.   definition area). Also $1300-$cfff will be free. 
  985.  
  986.   Now, the C=128 has different memory maps it can configure itself to - 
  987.   Bank 15 is the standard mode under most basic programs and allows the
  988.   programmer to directly "sys" to calls. The MMU (memory management unit -
  989.   the chip that does everything) sees memory in a slightly differant way
  990.   than from basic. We'll cover it in more detail when we examine the mem_init
  991.   routine. For now, we're just gonna set up in the program and not in the
  992.   coding segments. The explanation of what we're doing will be "revealed"
  993.   in a future issue. 
  994.  
  995.   We will use Bank 0 of memory and from $1300+ will be the program. The ranges
  996.   of $0400-$09ff and $0b00-$0fff will be used in a similair mannar as the C64
  997.   ranges were for Temporary Storage. We will also have the I/O section from
  998.   $d000-$dfff swapped in. This is not a standard "basic BANK #" but when we
  999.   cover the init'memory routine we'll see how we can do this. Music data
  1000.   will be from $a000-$d000. 
  1001.   
  1002.  
  1003. X.  Looking Forward / Back
  1004.     ----------------------
  1005.  
  1006.   Hopefully through the listing and the discussion of the routines you have
  1007.   started to understand the basic concept of programming: breaking down problems
  1008.   into smaller solvable steps. Try looking back over the code asking yourself
  1009.   why that instruction is there. What would happen if you switched the order?
  1010.   Is there an easier, better way to do the same thing? Why? Better yet, how?
  1011.   Examine the code, mess with it, muck it up so it doesn't work and then figure
  1012.   out exactly why. The only way to learn is by experimentation. (BTW, muck up
  1013.   a _copy_ of it - not the original ... *grins*)
  1014.  
  1015.   Take a look at the different sections of code and analyze them to see how
  1016.   they do what they do. Take a look at how the code was organized in terms
  1017.   of simplification. Trace through each subroutine so that you're able to 
  1018.   know what the return values will be. In other words: Study, Study, Study!!
  1019.   I'm in school and so I know I just used the dreaded 'S' word but that's 
  1020.   what you're going to have to do if you're interested in learning 65xx/85xx
  1021.   machine language. The only way to learn it (easily) is to study other 
  1022.   people's code and try to understand why they did what they did.
  1023.  
  1024.   Next time we will take a look at the input routines for the mouse, joystick
  1025.   and keyboard scanning. In addition we will also allow the player to move
  1026.   the ship around on the screen to test the input drivers. 
  1027.  
  1028.   In addation, I am still looking for an individual to help with music and
  1029.   sound composition for this program. A knowledge of the SID chip and 
  1030.   programming is helpful but not required. If you're willing to help then
  1031.   please email me at duck@pembvax1.pembroke.edu
  1032.  
  1033.  
  1034. XI. Listings
  1035.     --------
  1036.  
  1037.   Because of the enormity of the program listing (some 1,500+ lines) it will 
  1038.   not be listed in this article but will instead be available via anonymous
  1039.   ftp at ccosun.caltech.edu under pub/rknop/HACKING.MAG as invas1.sfx. 
  1040.  
  1041.   For those of you on the mailing list who would like to recieve it, a Mail-
  1042.   Server will be set up soon to handle requests and information will be
  1043.   sent to you concerning information about using it as soon as it's completed.
  1044.  
  1045.   In the invasion1.sfx file there are the following files:
  1046.  
  1047.        invasion.src - the main file
  1048.        graphics.src - handles all graphics routines
  1049.        logo.dat     - custom character logo
  1050.        chars64.dat  - alien custom characters for C=64
  1051.        chars128.dat - alien custom characters for C=128
  1052.        titletxt.dat - text data for title screen
  1053.        titlecol.dat - color data for title screen
  1054.        invasion-128 - executable version of Space Invasion so far for C=128
  1055.        invasion-64  - executable version of Space Invasion so far for C=64
  1056.  
  1057.   Note: For the Commodore 128 it's recommended that you do a run/stop-restore
  1058.   and then a "BANK15:SYS7168" to execute the program. For the Commodore 64 it's
  1059.   recommended the border be changed via: "POKE53280,0:POKE53281,0:SYS 32768" to
  1060.   run the program.
  1061.  
  1062. ==============================================================================
  1063. The Demo Corner:
  1064.  
  1065. FLI - more color to the screen   
  1066. by Pasi 'Albert' Ojala (po87553@cs.tut.fi or albert@cc.tut.fi)
  1067.                         Written on 16-May-91   Translation 01-Jun-92
  1068.     
  1069. (All timings are in PAL, altho the principles will apply to NTSC too)
  1070.  
  1071. All of us have heard complaints about the color constraints on C64. One 8x8
  1072. pixel character position may only carry four different colors.  FLI picture
  1073. can have all of the 16 colors in one char position.  What then is this FLI
  1074. and how it is done ?
  1075.  
  1076. In the normal multicolor mode can one character position (4x8 pixels) have
  1077. only four different colors and one of them is the common background color.
  1078. Color codes are stored in half bytes (nybbles) to the video matrix memory
  1079. (anywhere video matrix pointer points at, normally $0400) and to the color
  1080. memory ($D800-$DBFF). In multicolor mode the color of each pixel is
  1081. determined by two bits in the graphics memory. Bit pair 11 will refer to
  1082. color memory, background color is the color for bit pair 00, and video
  1083. matrix will define the colors for bit pairs 01 and 10.
  1084.  
  1085.  
  1086. _What happens in the VIC ?_
  1087.  
  1088. VIC (Video Interface Controller) fetches color information from memory on
  1089. each bad line. This will steal time from processor, because VIC needs to use
  1090. processor's bus cycles. Bad line is a curse in the C64 world. Fortunately
  1091. VIC's data bus is 12 bits wide and so the color data fetch for each character
  1092. position will take only one bus cycle. Color memory is physically wired to
  1093. the VIC databus lines D8-D11.
  1094.  
  1095. How does VIC know where to fetch the graphical information ? Some of you know
  1096. the mystical formulas needed to mess with the pixels in the hires screen.
  1097. How are these functions obtained ? Are they just magic ? No, there are some
  1098. internal counters in VIC. They always point to the right place in grafix
  1099. memory and the address is determined like this:
  1100.  
  1101. A13  A12 A11 A10 A9  A8  A7  A6  A5  A4  A3  A2  A1  A0
  1102. CB13 VC9 VC8 VC7 VC6 VC5 VC4 VC3 VC2 VC1 VC0 RC2 RC1 RC0
  1103.  
  1104. Address bits A15 and A14 change according to the selected video bank.
  1105. Address bit A13 is CB13, which may be found in VIC register $18. It
  1106. selects the right side of the video bank to be the bitmap memory. With
  1107. these bits you can set the bitmap to eight different places in memory.
  1108. However, some of them are useless because of the character ROM images and
  1109. zero page/stack. Rest of the bits come from the internal counters.
  1110.  
  1111. VC9-VC0 (Video Counter) forms the address bits 12-3. The counter rolls
  1112. through all 1000 character positions, 0-39 on the first eight lines, 40-79
  1113. on the second eight lines and so on. The lowest three bits come from the row
  1114. counter, RC2-RC0. This is another VIC counter and it counts the scan lines
  1115. from zero to seven.
  1116.  
  1117.  
  1118. _A software graphics mode - FLI_
  1119.  
  1120. VIC will systematically go through every byte in the bitmap memory, but how
  1121. does it know where and when to get the color information ? This is where
  1122. the main principle of FLI (Flexible Line Interpretation) lies. Color data
  1123. is fetched (and this means it is a bad line), when the line counter matches
  1124. with the vertical scroll register. VC9-VC0 defines where the color data is
  1125. inside the video matrix and color memory.
  1126.  
  1127. If we change the vertical scroll register, we can fool VIC to think that
  1128. every line is a bad line, so it will fetch the color information on every
  1129. line too. Because VIC will fetch the colors continuosly, we can get
  1130. independent colors on each scan line. We just have to change colors and VIC
  1131. will handle the rest. Unfortunately the result is the loss of 40 processor
  1132. cycles per line (see the Missing Cycles article for more information about
  1133. VIC stealing cycles).
  1134.  
  1135.  
  1136. _Doing it in practice_
  1137.  
  1138. In practice there is no time to change color memory, but in multicolor
  1139. mode VIC uses video matrix for color information too. We have just enough
  1140. time to change the video matrix pointer, $D018. Now VIC will see a
  1141. different video matrix on each scan line, different block of memory. With
  1142. the four upper bits in the register we select one of the 16 video memories
  1143. in the video bank. Just remember that the register also selects the position
  1144. of the graphics memory (bitmap) inside the video bank.
  1145.  
  1146. Because we have to keep the bitmap in the same video bank, we only have half
  1147. of the bank free for video matrices. Fortunately, that's all we need to get
  1148. individual multicolor colors for each line and character position.
  1149. VIC will fetch the color data from the eight video matrices and then it will
  1150. roll on to the next 40 bytes. After eight lines and matrices we will select
  1151. the first video matrix again. (See picture 1)
  1152.  
  1153. Usually it is not necassary to use the whole screen for a FLI picture,
  1154. especially if you want to have a scroller or some other effects. You just
  1155. have to make sure that VIC is foolable in the usual way. The timing is also
  1156. very important, even one cycle variations in the routine entry are not
  1157. allowed. There is many ways to do the synchronization. One way is to use a
  1158. sprite, as in the previous article. (See C= Hacking, Vol. 1, Iss. 3, The
  1159. Demo Corner: Missing Cycles).
  1160.  
  1161.  
  1162. _Not much time_
  1163.  
  1164. Because a bad line will steal 40 cycles, there is only 23 cycles left on
  1165. each scan line. It is enough for changing the video matrix and backgroud
  1166. color. There is not a moment to lose, because you must change the vertical
  1167. scroll register, video matrix pointer and the background color. This is why
  1168. you can't have sprites in front of a FLI picture.
  1169.  
  1170. With FLI we get two selectable colors for each character position and line,
  1171. each scan line can have it's own background color and each character position
  1172. still has its own character color from color memory. In theory each character
  1173. position could have 25 different colors, unfortunately VIC only has 16.
  1174.  
  1175.  
  1176. _A little feature_
  1177.  
  1178. VIC does not like it when we change the vertical scroll register ($D011),
  1179. and is a bit annoyed. It 'sees' code 255 (light gray) in video matrix
  1180. and 9 (brown) in the color memory instead of the correct values stored there.
  1181. Actually the color value seems to be the lower nybble of the data byte
  1182. currently on the data bus (accessed by the processor (LDA#=$A9)).
  1183. Unfortunately there is no chance to do the register change in the border
  1184. and thus the three leftmost character columns are a bit useless, because
  1185. the colors are fixed.
  1186.  
  1187. However, this doesn't mean that you can't use those three columns. FLI
  1188. editors may not support the fixed colors though, so it may be hard to use
  1189. them.
  1190.  
  1191.  
  1192. _What to do with FLI ?_
  1193.  
  1194. Because FLI will eat up all the available processor time (no Copper :-),
  1195. it is not suitable for any action-games. Each FLI picture takes about 17 kB
  1196. of memory: not so many pictures fit on one floppy. So, the only place for FLI
  1197. is demos, intros, board-type games and maybe a GIF viewer..
  1198.  
  1199. --------------------------------------------------------------------------
  1200. Picture 1: From which matrix VIC fetches the multicolor values
  1201.  
  1202.  _          ___________________________________________________________
  1203. |      ... | Matrix0       | Matrix0       | Matrix0       |
  1204. |      ,  .|____3__________|____4__________|____5__________|  ...
  1205. |       U .| Matrix1       | Matrix1       | Matrix1       |
  1206. |       s .|____3__________|____4__________|____5__________| .
  1207. |Char   e  | Matrix2       | Matrix2       | Matrix2       | .
  1208. |Line   l  |____3__________|____4__________|____5__________| .
  1209. |Zero   e  | Matrix3       | Matrix3       | Matrix3       |
  1210. |       s  |____3__________|____4__________|____5__________|
  1211. |       s, | Matrix4       | Matrix4       | Matrix4       |
  1212. |          |____3__________|____4__________|____5__________|
  1213. |       c  | Matrix5       | Matrix5       | Matrix5       |
  1214. |       o  |____3__________|____4__________|____5__________|
  1215. |       l  | Matrix6       | Matrix6       | Matrix6       |
  1216. |       u  |____3__________|____4__________|____5__________|
  1217. |       m  | Matrix7       | Matrix7       | Matrix7       |
  1218. |_      n  |____3__________|____4__________|____5__________|
  1219.         s  | Matrix0       | Matrix0       | Matrix0       |
  1220.            |___43__________|___44__________|___45__________|
  1221.            | Matrix1       | Matrix1       | Matrix1       |
  1222.            |___43__________|___44__________|___45__________|
  1223.            |
  1224.            |   ...
  1225.            | .
  1226.            | .
  1227.            | .
  1228.  
  1229. --------------------------------------------------------------------------
  1230. _Additional reading_
  1231.  
  1232. If you have an Amiga you might want to get your hands into my conversion
  1233. programs in C64GFX1.lha. The packet also includes FLI viewer for PAL C64's
  1234. and some documentation about the FLI file format. It also has the same
  1235. utilities for Koala format pictures.
  1236.  
  1237. Available from:
  1238. cwaves.stfx.ca
  1239. nic.funet.fi:/pub/amiga/graphics/applications/convert
  1240.  
  1241.  
  1242. C64GFX.doc
  1243.                         C64Gfx1.0
  1244.            A C64 grafix format conversion package
  1245.            )1991,1992  Pasi 'Albert' Ojala
  1246.  
  1247.            E-mail: po87553@cs.tut.fi
  1248.                    albert@cc.tut.fi
  1249.  
  1250. This package contains programs which are used to convert portable
  1251. pixmap (ppm) files to C64 graphics formats (FLI and koala) under
  1252. AmigaOS. The package includes C source codes for the programs, so
  1253. it is possible to port the programs to another environment. C64GFX1.1
  1254. includes Unix-compilable sources.
  1255.  
  1256. In addition to this package you need e.g. PBMPlus to convert Amiga
  1257. ilbm files to ppm first. And of course some way to transfer files
  1258. between the machines.
  1259.  
  1260. =============================================================================
  1261. RS-232 Converter
  1262. by Warren Tustin (warren@col.hp.com)
  1263.  
  1264.   This article presents a way to interface from the C= rs232 hardware
  1265. behind the user port to a standard 25pin female rs232 connector using only
  1266. one IC and a few capacitors.  It is not a UART or a SWIFTLINK type interface
  1267. which take place of the internal C= rs232 circuitry, but a simple level shifting
  1268. interface that uses the internal rs232 routines and translates the user port
  1269. levels to rs232 levels.  Therefore you can only get upto 2400baud/9600baud
  1270. (C=64/C=128) with this design.
  1271.  
  1272.   The "old" way to do this was to used MC1488 and MC1489 parts (a line
  1273. driver and line receiver), however these required a negative supply to interface
  1274. properly.  The user port only supplies +5volts, hence this presents a problem.
  1275. There has been success using these parts or discrete transistors and resistors
  1276. since many modems are somewhat friendly and seem to work even though the levels
  1277. were marginal.  Also, some signals were not used, allowing for potential
  1278. problems.  Another way to solve this problem was to buy a $25-30 interface.  If
  1279. you can find the IC below, you have another choice that is relatively
  1280. inexpensive.
  1281.  
  1282.    The LT1133 is basically the MC1488 and 1489 put together into one part
  1283. with an internal charge pump scheme that allows the internal drivers to output
  1284. +5 and -5 volts to the rs232 connector.  It also has enough drivers and
  1285. receivers to handle all of the signals that the C= uses for rs232.
  1286.  
  1287.    So with this IC, 5 capacitors and the two connectors (user port and
  1288. rs232) you can build your own interface to the standard 25 pin modem cable.
  1289.  
  1290.    Here are the plans for an User port TO RS232 connector using just ONE IC and
  1291. 4 capacitors.  It uses a Linear Technology LT1133 buffer that has 3 RS232
  1292. drivers and 5 receivers.  It has worked for me with no problems and takes
  1293. a minimum amout of wiring to get to work.  My board is only the width of
  1294. the user port and about 1.5 inches deep in size.
  1295.  
  1296. Parts list:
  1297. ----------
  1298.      LT1133CN plastic dip or LT1133CJ ceramic dip RS232 driver from Linear
  1299.         Technologies
  1300.  
  1301.         (It takes 27mA max (17mA typical) so is well below the 100mA 
  1302.          limit of the user port)
  1303.  
  1304.          Driver In pins  (15,19,21)  TTL/CMOS compatible.  Unused inputs
  1305.                            |  |  |   should be tied to +5v.
  1306.          Driver Out pins (11, 7, 5)  RS232 compatible.  Short circuit 
  1307.                                      protected from -30v to +30v.
  1308.          Receiver In pins  (6, 8, 9, 10,12)  Accept RS232 levels (+-30v)
  1309.                             |  |  |   |  |   and have 0.4v of hysteresis to
  1310.                             |  |  |   |  |   provide noise immunity.
  1311.          Receiver Out pins (20,18,17,16,14)  TTL/CMOS outputs.
  1312.  
  1313.          NOTE: Lines above indicate which inputs go with which outputs, and
  1314.                the pairs can be interchanged freely.  I connected them as
  1315.                described below because the wiring worked out the best for me. 
  1316.  
  1317.      4 - >= 1uF capacitors   Used to generate RS232 voltages by a charge pump
  1318.                              technique inside IC
  1319.      1   1uF capacitor  To bypass the 5volt supply for noise rejection.
  1320.  
  1321.      1   User port female connector.  (I just dug this up, I'm not sure 
  1322.          where these can be found, I think it is 0.159" spacing, 24pin.
  1323.  
  1324.      1   RS232 25pin female connector.  Can be found at R-Shack
  1325.         
  1326.      Some sort of .1" spacing proto board
  1327.  
  1328. Connections:
  1329. -----------
  1330.  
  1331.   User port connector (Looking into the C64 or C128)
  1332.  
  1333.          1  2  3  4  5  6  7  8  9 10 11 12
  1334.         ------------------------------------
  1335.         |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
  1336.         ------------------------------------
  1337.          A  B  C  D  E  F  H  J  K  L  M  N
  1338.  
  1339. Ground & Power:
  1340. --------------
  1341.  
  1342.   Pins 1, A, 12, N to Ground of board.
  1343.   LT1133 pin 2 to pin 2 of User port connector (+5 volts)
  1344.   1uF capacitor between pin2 and ground (bypass cap)
  1345.   LT1133 pin 13 to Ground.
  1346.   RS232 connector pins 7 & 1 to Ground.
  1347.  
  1348. LT1133 capacitors:
  1349.   1uF from pin 1 (V+) to ground  (If polarized (electrolytic) + side to pin 1)
  1350.   1uF from pin 24 (V-) to ground (As above but + side to ground)
  1351.   1uF from pin 3 (C1+) to pin 4 (C1-) (Again if polarized, + side to pin 3)
  1352.   1uF from pin 22 (C2+) to pin 23 (C2-) (Again if polarized, + side to pin 22)
  1353.     
  1354.  
  1355.     Commodore side                                       RS232 side
  1356.   User port    Signal    Pin of       Signal        Pin of       RS232
  1357.   pin          name      LT1133      direction      LT1133       connector
  1358.   ---------    ------    ------      ---------      ------       ---------
  1359.    B  FLAG2     Din        20         <--             6              3
  1360.    C   PB0      Din     also connect to above pin 20
  1361.    D   PB1      RTS        21             -->         5              4
  1362.    E   PB2      DTR        19         <--             7              20
  1363.    F   PB3      RI         18             -->         8              22
  1364.    H   PB4      DCD        17             -->         9              8
  1365.    J   PB5      Not used
  1366.    K   PB6      CTS        16             -->         10             5
  1367.    L   PB7      DSR        14         <--             12             6
  1368.    M   PA2      Dout       15             -->         11             2
  1369.  
  1370.  
  1371. This assumes that you want to connect all of the communication lines.  I did
  1372. it this way because the C128 programmers reference guide had all of the signals
  1373. above listed.  If you want to drop RI (ring indicator) you could also use an
  1374. LT1134 which has 4 drivers and 4 receivers.
  1375.  
  1376. Parts Substitution:
  1377.  
  1378.    There are other IC's available which will work in this application.
  1379. The rs232 bus levels for the LT parts are spec'ed at ~ +/- 7v typical, while
  1380. the MAXIM parts are +/-9v typical.  (Both are min at +/- 5v which should work
  1381. in all applications).  I think that an interface can be done with only 3
  1382. lines, Din(Rx), Dout(Tx), and either DSR or CTS, so if you can't get the
  1383. LT1133 one of these others might work, although the pinouts would be different.
  1384. The max you would need is what the LT1133 supplies, 3 drivers & 5 receivers.
  1385.  
  1386. Here is a description of parts that might be substituted:
  1387. (Note, with all of them you should use a bypass cap on the +5v supply which
  1388.  I have NOT included in the counts below.)
  1389.  
  1390.                  RS232     RS232    # of EXTERNAL
  1391. NAME     PINS   DRIVERS  RECEIVERS       CAPS       COMMENTS
  1392. -------------------------------------------------------------------------
  1393. Linear Technology parts...
  1394. (On the parts with shutdown (SD), the pin must be tied to +5 to operate)
  1395. LT1133    24       3         5             4        In article above
  1396. -------------------------------------------------------------------------
  1397. LT1130    28       5         5             4        Overkill (SDp14)
  1398. LT1131    28       5         4             4        2 ex Dr, 1 < Rcvr
  1399. LT1132    24       5         3             4        2 ex Dr, 2 < Rcvr
  1400. LT1134    24       4         4             4        1 < Rcvr
  1401. LT1136    28       4         5             4        1 ex Dr  (SDp14)
  1402. LT1137    28       3         5             4        SDp13
  1403. LT1138    28       5         3             4        LT1132 w/SDp13
  1404.  
  1405. MAXIM parts... (Some also have a TTL EN_ pin that must be tied to 0v to operate)
  1406.                (On these parts, SD must be tied to 0v to operate!)
  1407. MAX232    16       2         2             4        May work
  1408. MAX233    20       2         2             0!       No external caps
  1409. MAX235    24       5         5             0!       Overkill, but NO caps,
  1410.                                                     SDp21, EN_p20
  1411. MAX236    24       4         3             4        1 ex Dr, 2 < Rcvr,
  1412.                                                     SDp21, EN_p20
  1413. MAX237    24       5         3             4        2 ex DR, 2 < Rcvr
  1414. MAX238    24       4         4             4        1 ex DR, 1 < Rcvr
  1415.  
  1416.   In summary, you can see there are many different parts you could use,
  1417. especially if you don't need all the signals.  The MAXIM parts seem to
  1418. do the job in fewer pins and a little better typical drive spec and I
  1419. would recommend the MAX235 overall since you only need 1 bypass cap to
  1420. make it operate!
  1421.  
  1422. =============================================================================
  1423. Introduction to the VIC-II
  1424. by Pasi 'Albert' Ojala ( ... )
  1425.  
  1426. The Video Interface Controller used in C64 have several different operating
  1427. modes and different graphical primitives. Basically there is a) character
  1428. mode, b) bitmap mode and c) movable objects that can be mixed with the
  1429. other graphics. These primitives can also be put into a more colorful mode,
  1430. but you lose half of the resolution in that process.
  1431.  
  1432.  
  1433. I. Standard Character Display Mode
  1434.  
  1435. In the character display mode, VIC fetches character pointers from video
  1436. matrix, which consists of 1000 8-bit bytes formatted as 25 rows of 40
  1437. characters each. The 8-bit character code implies 256 different characters
  1438. simultaneously onscreen.
  1439.  
  1440. Each character code can have an unique image, which consists of 8 bytes in
  1441. character memory. The position of the character memory can be moved with the
  1442. character base pointer and thus it is possible to have several character sets
  1443. simultaneosly in memory. One character memory is 2048 bytes.
  1444.  
  1445. In addition to the character code, each position in video matrix has an
  1446. associated color nybble (4 bits) in color memory ($D800-$DFFF). For each
  1447. zero-bit in the charset the background color from register $21 is displayed,
  1448. the color-nybble is used for the one-bit.
  1449.  
  1450.  
  1451. II. Character Multicolor Mode
  1452.  
  1453. In character multicolor mode, color selection is increased. Each byte is
  1454. fetched from the character memory just like in the standard character mode,
  1455. but they are interpreted differently. In this mode bytes are divided into
  1456. bit-pairs. For bit pair "00" the background color from register $21 is
  1457. displayed, background color #1 is used for bit pair "01" and background
  1458. color #2 for bit pair "10". The color nybble will define the color for bit
  1459. pair "11".
  1460.  
  1461. The highest bit in the color memory defines whether the character is to be
  1462. displayed in multicolor ("1") or in standard mode ("0"). Because of this,
  1463. only colors in the range from 0 to 7 are possible for bit pair "11". And since
  1464. two bits are required to specify one dot color, the character is now displayed
  1465. as a 4x8 matrix instead of the 8x8 matrix and the size of the dots are doubled
  1466. horizontally.
  1467.  
  1468.  
  1469. III. Extended Color Mode
  1470.  
  1471. The extended color mode allows the selection of one background color from four
  1472. possibilities for each character position in the normal 8x8 resolution. The
  1473. character image data is processed like in standard character mode, but the
  1474. two most significant bits in the character code (video matrix) are used to
  1475. select the right background color register. Only character images from 0 to 63
  1476. are accessible, because two of the most significant bits are used for the
  1477. background color selection.
  1478.  
  1479. Extended color mode and multicolor mode should not be selected simultaneosly,
  1480. because this will result a black screen. However, this is a very easy way to
  1481. hide something if needed.
  1482.  
  1483.  
  1484. IV. Standard Bit Map Mode
  1485.  
  1486. In bit map mode, a one-to-one correspondence exists between each displayed
  1487. dot and a memory bit. The bit map provides a resolution of 320H x 200V
  1488. individually controlled pixels. The video matrix is still accessed as in
  1489. character mode, but the data is interpreted as color data. When a bit is "0"
  1490. in the bit map data, the color from the lower nybble is selected. The
  1491. higher nybble from the video matrix is used for the bit "1".
  1492.  
  1493.  
  1494. V. Multicolor Bit Map Mode
  1495.  
  1496. In multicolor bit map mode two bits in the bit map memory determine the color
  1497. of one pixel. If the bit pair is "11", the color found from the color memory
  1498. is used. The background color is used for bit pair "00" and the video matrix
  1499. defines the colors for bit pairs "01" and "10". As it takes two bits to define
  1500. one pixel color, the horizaontal resolution is halved to 160H x 200V.
  1501.  
  1502.  
  1503. VI. Movable Object Blocks (MOBs)
  1504.  
  1505. The movable object block is a special type of graphical object which can be
  1506. displayed independently from the other graphics. Each one of the MOBs can
  1507. be moved independently anywhere in the screen. Eight unique MOBs can be
  1508. displayed simulataniously, each defined by 64 bytes in memory which are
  1509. displayed as a 24 x 21 pixel array.
  1510.  
  1511. Each MOB can be selectively enabled (MnE="1") or disabled (MnE="0"). A MOB
  1512. is positioned via its X and Y position registers. Nine bits are needed to
  1513. define the vertical position and the most significant bits of all MOBs are
  1514. stored in the register $10. As X locations 23 to 347 and Y locations 50 to
  1515. 249 are entirely visible on the screen, MOBs can be smoothly moved to an
  1516. off-screen position.
  1517.  
  1518. Each MOB has its own color register and a MOB can be displayed either in
  1519. standard or multicolor mode (MnMC="1"). As usually, multicolor mode gives
  1520. more colors, but halves the horizontal resolution. In multicolor mode bit
  1521. pair "00" is transparent, the MOB color register defines the color for pair
  1522. "10", and MOB multicolor registers give the colors for pairs "01" and "11".
  1523.  
  1524. MOBs can be selectively expanded in both directions. When MOB is expanded,
  1525. the pixel size also expands and it is still displayed as 24 x 21 matrix
  1526. (12 x 21 in multicolor mode).
  1527.  
  1528. MOB priorities define whether a MOB appears behind or on top of the character
  1529. or bit map graphics. A "1" in MnDP means MOB is displayed behind. MOB
  1530. collision registers may be used to detect if a non-transparent data of two
  1531. MOBs or a MOB and character or bitmap foreground data is colliding.
  1532.  
  1533. [Ed's Note: MOB's are Sprites. Commodore initially referred to them as MOB's
  1534. and still does in some areas.]
  1535.  
  1536.  
  1537. VII. Other features
  1538.  
  1539. The display screen may be blanked by setting the DEN bit to a "0". The entire
  1540. screen will be filled with the border color as set in register 32 ($20).
  1541. When the screen is blanked, VIC will need only transparent memory cycles and
  1542. the processor is not slowed down. However, MOB data is still fetched, if
  1543. the MOBs are not also disabled.
  1544.  
  1545. The normal display consists of 25 rows of 40 characters each. The display
  1546. window can be reduced to 24 rows and 38 characters. This has no effect on how
  1547. the data is interpreted, only the characters next to the border are covered
  1548. by the border. RSEL controls the number of rows ("1" for 25 rows) and CSEL
  1549. controls the number of columns ("1" for 40 columns).
  1550.  
  1551. The display data may be scrolled up to one character space in both vertical
  1552. and horizontal direction. Position of the screen is set with the 3 lowest
  1553. order (least significant) bits in registers 22 ($16) and 17 ($11).
  1554.  
  1555. Light pen latch is used to catch the position of the light pen when a pulse
  1556. is received in the LP pin. The value is latched only once in a frame.
  1557.  
  1558. The raster register is a dual-function register. A read from the raster
  1559. register returns the current raster position and a write to it will set the
  1560. raster compare value. When the written value and the current raster line
  1561. matches, a raster interrupt is generated if enabled. Raster register has its
  1562. most significant (9th) bit in register 17 ($11).
  1563.  
  1564. The interrupt register shows the status of the four sources of interrupt.
  1565. A corresponding bit will be set to "1" when an interrupt source has generated
  1566. an interrupt request. To enable an interrupt request to set the /IRQ output
  1567. to zero, the corresponding enable bit in register 26 ($1a) must be set to
  1568. "1". The interrupt latch may only be cleared by writing a "1" to the
  1569. desired latch in the interrupt register.
  1570.  
  1571. -----------------------------------------------------------------------------
  1572. VIC register map (Base address $d000)
  1573.  
  1574. Address DB7   DB6   DB5   DB4   DB3   DB2   DB1   DB0   Description
  1575. -------------------------------------------------------------------
  1576. 00 $00  M0X7  M0X6  M0X5  M0X4  M0X3  M0X2  M0X1  M0X0  MOB 0 X-position
  1577. 01 $01  M0Y7  M0Y6  M0Y5  M0Y4  M0Y3  M0Y2  M0Y1  M0Y0  MOB 0 Y-position
  1578. 02 $02  M1X7  M1X6  M1X5  M1X4  M1X3  M1X2  M1X1  M1X0  MOB 1 X-position
  1579. 03 $03  M1Y7  M1Y6  M1Y5  M1Y4  M1Y3  M1Y2  M1Y1  M1Y0  MOB 1 Y-position
  1580. 04 $04  M2X7  M2X6  M2X5  M2X4  M2X3  M2X2  M2X1  M2X0  MOB 2 X-position
  1581. 05 $05  M2Y7  M2Y6  M2Y5  M2Y4  M2Y3  M2Y2  M2Y1  M2Y0  MOB 2 Y-position
  1582. 06 $06  M3X7  M3X6  M3X5  M3X4  M3X3  M3X2  M3X1  M3X0  MOB 3 X-position
  1583. 07 $07  M3Y7  M3Y6  M3Y5  M3Y4  M3Y3  M3Y2  M3Y1  M3Y0  MOB 3 Y-position
  1584. 08 $08  M4X7  M4X6  M4X5  M4X4  M4X3  M4X2  M4X1  M4X0  MOB 4 X-position
  1585. 09 $09  M4Y7  M4Y6  M4Y5  M4Y4  M4Y3  M4Y2  M4Y1  M4Y0  MOB 4 Y-position
  1586. 10 $0a  M5X7  M5X6  M5X5  M5X4  M5X3  M5X2  M5X1  M5X0  MOB 5 X-position
  1587. 11 $0b  M5Y7  M5Y6  M5Y5  M5Y4  M5Y3  M5Y2  M5Y1  M5Y0  MOB 5 Y-position
  1588. 12 $0c  M6X7  M6X6  M6X5  M6X4  M6X3  M6X2  M6X1  M6X0  MOB 6 X-position
  1589. 13 $0d  M6Y7  M6Y6  M6Y5  M6Y4  M6Y3  M6Y2  M6Y1  M6Y0  MOB 6 Y-position
  1590. 14 $0e  M7X7  M7X6  M7X5  M7X4  M7X3  M7X2  M7X1  M7X0  MOB 7 X-position
  1591. 15 $0f  M7Y7  M7Y6  M7Y5  M7Y4  M7Y3  M7Y2  M7Y1  M7Y0  MOB 7 Y-position
  1592. 16 $10  M7X8  M6X8  M5X8  M4X8  M3X8  M2X8  M1X8  M0X8  MSB of X-position
  1593.  
  1594. 17 $11  RC8   ECM   BMM   DEN   RSEL  Y2    Y1    Y0    (See text)
  1595. 18 $12  RC7   RC6   RC5   RC4   RC3   RC2   RC1   RC0   Raster register
  1596. 19 $13  LPX8  LPX7  LPX6  LPX5  LPX4  LPX3  LPX2  LPX1  Light Pen X
  1597. 20 $14  LPY7  LPY6  LPY5  LPY4  LPY3  LPY2  LPY1  LPY0  Light Pen Y
  1598. 21 $15  M7E   M6E   M5E   M4E   M3E   M2E   M1E   M0E   MOB Enable
  1599. 22 $16  -     -     RES   MCM   CSEL  X2    X1    X0    (See text)
  1600. 23 $17  M7YE  M6YE  M5YE  M4YE  M3YE  M2YE  M1YE  M0YE  MOB Y-expand
  1601. 24 $18  VM13  VM12  VM11  VM10  CB13  CB12  CB11  -     Memory Pointers
  1602. 25 $19  IRQ   -     -     -     ILP   IMMC  IMBC  IRST  Interrupt Register
  1603. 26 $1a  -     -     -     -     ELP   EMMC  EMBC  ERST  Enable Interrupt
  1604. 27 $1b  M7DP  M6DP  M5DP  M4DP  M3DP  M2DP  M1DP  M0DP  MOB-DATA Priority
  1605. 28 $1c  M7MC  M6MC  M5MC  M4MC  M3MC  M2MC  M1MC  M0MC  MOB Multicolor select
  1606. 29 $1d  M7XE  M6XE  M5XE  M4XE  M3XE  M2XE  M1XE  M0XE  MOB X-Expand
  1607. 30 $1e  M7M   M6M   M5M   M4M   M3M   M2M   M1M   M0M   MOB-MOB  Collision
  1608. 31 $1f  M7D   M6D   M5D   M4D   M3D   M2D   M1D   M0D   MOB-DATA Collision
  1609.  
  1610. 32 $20  -     -     -     -     EC3   EC2   EC1   EC0   Exterior Color
  1611. 33 $21  -     -     -     -     B0C3  B0C2  B0C1  B0C0  Background #0 Color
  1612. 34 $22  -     -     -     -     B1C3  B1C2  B1C1  B1C0  Background #1 Color
  1613. 35 $23  -     -     -     -     B2C3  B2C2  B2C1  B2C0  Background #2 Color
  1614. 36 $24  -     -     -     -     B3C3  B3C2  B3C1  B3C0  Background #3 Color
  1615. 37 $25  -     -     -     -     MM03  MM02  MM01  MM00  MOB Multicolor #0
  1616. 38 $26  -     -     -     -     MM13  MM12  MM11  MM10  MOB Multicolor #1
  1617.  
  1618. 39 $27  -     -     -     -     M0C3  M0C2  M0C1  M0C0  MOB 0 Color
  1619. 40 $28  -     -     -     -     M1C3  M1C2  M1C1  M1C0  MOB 1 Color
  1620. 41 $29  -     -     -     -     M2C3  M2C2  M2C1  M2C0  MOB 2 Color
  1621. 42 $2a  -     -     -     -     M3C3  M3C2  M3C1  M3C0  MOB 3 Color
  1622. 43 $2b  -     -     -     -     M4C3  M4C2  M4C1  M4C0  MOB 4 Color
  1623. 44 $2c  -     -     -     -     M5C3  M5C2  M5C1  M5C0  MOB 5 Color
  1624. 45 $2d  -     -     -     -     M6C3  M6C2  M6C1  M6C0  MOB 6 Color
  1625. 46 $2e  -     -     -     -     M7C3  M7C2  M7C1  M7C0  MOB 7 Color
  1626.  
  1627. MnX  = MOB n X position         MnY  = MOB n Y position
  1628. RC   = Raster compare register  ECM  = Extended color mode
  1629. MBB  = Bit map mode             DEN  = Display enable
  1630. RSEL = Row select               Y    = Screen Y position
  1631. LPX  = Light pen X position     LPY  = Light pen Y position
  1632. MnE  = MOB n Enable             RES  = Always set to zero!
  1633. MCM  = Multicolor mode          CSEL = Column select
  1634. X    = Screen X position        MnYE = MOB n Y expand
  1635. VM   = Video matrix pointer     CB   = Character base pointer
  1636. MnDP = MOB to data priority     MnMC = MOB n multicolor select
  1637. MnXE = MOB n X expand
  1638.  
  1639. =============================================================================
  1640. LITTLE RED READER: MS-DOS file reader for the 128 and 1571/81 drives.
  1641.  
  1642. by Craig Bruce  <csbruce@neumann.uwaterloo.ca>
  1643.  
  1644. 1. INTRODUCTION
  1645.  
  1646. This article presents a program that reads MS-DOS files and the root directory
  1647. of MS-DOS disks.  The program copies only from drive to drive without
  1648. buffering file data internally.  This is simpler and imposes no limits on the
  1649. size of the files transferred, although it requires the use of two disk drives
  1650. (or a logical drive).  The user-interface code is written in BASIC and
  1651. presents a full-screen file selection menu.  The grunt-work code is written in
  1652. assembly language and operates at maximum velosity.
  1653.  
  1654. The Burst Command Instruction Set of the 1571/81 is used to read the MS-DOS
  1655. disk blocks and the standard kernel routines are used for outputting the
  1656. data.  (I am an operating systems specialist, so I call it a kernEl!)  Thus,
  1657. the MS-DOS files must be read from a 1571 or 1581 disk drive, but the output
  1658. device may be any disk drive type, the screen or a printer, or a virtual drive
  1659. type such as RAMLink, RAMDrive, or RAMDOS (for the REU).  It is interesting to
  1660. note that the data can be read in from an MS-DOS disk faster than it can be
  1661. written out to a 1571, 1581, or even a RAMDOS file.  A RAMLink can swallow the
  1662. data only slightly faster than it can be read.
  1663.  
  1664. Little Red Reader (LRR) supports double density 3.5" disks formatted with 80
  1665. tracks, 9 sectors per track, and 2 sides with a 1581 and 5.25" double density
  1666. disks formatted with 40 tracks, 9 sectors per track, and 2 sides with a 1571.
  1667. A limit of 128 directory entries and 3 File Allocation Table (FAT) sectors is
  1668. imposed.  There must be 2 copies of the FAT and the cluster size may be 1 or 2
  1669. sectors.  The sector size must be 512 bytes.
  1670.  
  1671. Oh, about the name.  It is a play on the name of another MS-DOS file copier
  1672. available for the C-128.  "Little" means that it is smaller in scope than the
  1673. other program, and "Red" is a different primary color to avoid any legal
  1674. complications.  It is also the non-white color of the flag of the country of
  1675. origin of this program (no, I am not Japanese).  Also, this program is Public
  1676. Domain Software, as is all software I develop for 8-bit Commodore Computers.
  1677. Feel free to E-mail me if you have questions or comments about this article.
  1678.  
  1679. 2. USER GUIDE
  1680.  
  1681. LOAD and RUN the "lrr.128" BASIC program file.  When the program is first run,
  1682. it will display an "initializing" message and will load in the binary machine
  1683. language package from the "current" Commodore DOS drive (the current drive is
  1684. obtained from PEEK(186) - the last device accessed).  The binary package is
  1685. loaded only on the first run and is not reloaded on subsequent runs if the
  1686. package ID field is in place.
  1687.  
  1688. 2.1. MAIN SCREEN
  1689.  
  1690. The main screen of the program is then displayed.  The main screen of the
  1691. program will look something like this:
  1692.  
  1693.    MS-DEV=9    MS-TYPE=1581    CBM-DEV=8
  1694.  
  1695.    NUM  S  TRN  TYP  FILENAME  EXT  LENGTH
  1696.    ---  -  ---  ---  --------  ---  ------
  1697.      1  *  ASC  SEQ  HACK4     TXT  120732
  1698.      2     BIN  PRG  RAMDOS    SFX   34923
  1699.  
  1700.    D=DIRECTORY  M=MS-DEV  F=CBM-DEV Q=QUIT
  1701.    T=TOGGLE-COLUMN, C=COPY-FILES, +/- PAGE
  1702.  
  1703. except that immediately after starting up, "<no files>" will be displayed
  1704. rather than filenames.  The "MS-DEV" and "MS-TYPE" fields give the device
  1705. number and type of the drive containing the MS-DOS disk to copy from, and the
  1706. "CBM-DEV" gives the device number of the drive/virtual drive/character device
  1707. to copy file data to.
  1708.  
  1709. Information about all MS-DOS files in the root directory of the MS-DOS disk is
  1710. displayed in columns below the drive information.  "NUM" gives the number of
  1711. the MS-DOS file in the directory listing, and "S" indicates whether the file
  1712. is "selected" or not.  If the file is selected, an asterisk (*) is displayed;
  1713. otherwise, a blank is displayed.  When you later enter Copy Mode, only the
  1714. files that have been "selected" are copied.
  1715.  
  1716. The "TRN" field indicates the character translation scheme to be used when the
  1717. file is copied.  A value of "BIN" (binary) means no translation and a value of
  1718. "ASC" (ascii) means the file characters are to be translated from MS-DOS ASCII
  1719. (or "ASCII-CrLf") to PETSCII.  The "TYP" field indicates the type of
  1720. Commodore-DOS file to create for writing the MS-DOS file contents into.  The
  1721. possible values are "SEQ" (sequential) and "PRG" (program).  The values of the
  1722. TRN and TYP fileds are set independently, so you can copy binary data to SEQ
  1723. files and ascii data to PRG files if you wish.
  1724.  
  1725. The "FILENAME" and "EXT" fields give the filename and extension type of the
  1726. MS-DOS files and "LENGTH" gives the exact length of the files in bytes.  Note
  1727. that if you perform "ASC" translation on a file, its PETSCII version will have
  1728. a shorter length.
  1729.  
  1730. 2.2. USER COMMANDS
  1731.  
  1732. The bottom of the screen gives the command summary.  After starting the
  1733. program, you will want to setup the MS-DOS and CBM-DOS drives with the "M" and
  1734. "F" commands.  Simply press the (letter) key corresponding to the command
  1735. name to activate the command.  Pressing M will prompt you for the MS-DOS Drive
  1736. Number and the MS-DOS Drive Type.  In both cases, type the number and press
  1737. RETURN.  (Sorry for insulting all non-novices out there, but I want to be
  1738. complete).  The MS-DOS drive number cannot be the same as the CBM-DOS drive
  1739. number (since the program copies from drive-to-drive without internal
  1740. buffering).  For the drive type, enter an "8", "81", or "1581" for a 1581
  1741. drive or anything else for a 1571 drive.
  1742.  
  1743. Pressing F will prompt you for the CBM-DOS device number.  You may enter a
  1744. number from 0 to 30, except that it must not be the MS-DOS drive number.
  1745. Enter a "1" for Cassette Drive (God forbid!), a "3" for the screen, a "4" for
  1746. the printer (with an automatic secondary address of 7 (lowercase)), any number
  1747. above 7 for a Commodore disk drive or special virtual drive, or a value of "0"
  1748. for the special "null" drive.  A CBM-DEV value of 0 will case the program to
  1749. read MS-DOS files and do nothing with the output.  You can use this feature to
  1750. check out the raw reading speed of the program.
  1751.  
  1752. After setting up the drives, press D to read in the root directory off the
  1753. MS-DOS disk.  The data will come blazing in from the disk but BASIC will take
  1754. its good ole time sifting through it.  Filenames are displayed on the screen
  1755. as they are scanned in.  The program will (eventually) return to the main
  1756. screen and display the formatted file information.  One note: the process of
  1757. logging in a 1581 MS-DOS disk takes about 12 seconds (on my 1581, anyway), so
  1758. be patient.  An MS-DOS disk will have to be "logged in" every time you change
  1759. MS-DOS disks.  (Disks are logged in automatically).
  1760.  
  1761. A couple of notes about accessing MS-DOS disks: don't try to access a device
  1762. that is not present because the machine language routines cannot handle this
  1763. error for some reason and will lock up, requiring a STOP+RESTORE.  Also, make
  1764. sure that an actual MS-DOS disk is loaded into the drive.  If you accidentally
  1765. place Commodore-DOS disk into the MS-DOS drive, the 1581 will report an
  1766. invalid boot parameters error (#60), but a 1571 will lock up (since I don't
  1767. check the sector size and my burst routines are expecting 512 bytes to come
  1768. out of a sector whereas Commodore disks have only 256 bytes per sector).
  1769.  
  1770. Now you are ready to pick what files you want copied and how you want them
  1771. copied.  You will notice that a "cursor" appears in the "S" column of the
  1772. first file.  You may move the cursor around with the cursor keys: UP, DOWN,
  1773. LEFT, RIGHT, HOME, and CLR.  CLR (SHIFT-HOME) will move the cursor back to the
  1774. first file on the first screen.  You can move the cursor among the select,
  1775. translation, and file-type columns of all the files.  Pressing a SPACE or a
  1776. RETURN will toggle the value of the field that the cursor is on.  To toggle
  1777. all of the values of the "cursor" column (including files on all other
  1778. screens), press T.  You will notice that moving the cursor around and toggling
  1779. fields is a bit sluggish, especially if you are in Slow mode on the 40-column
  1780. screen.  Did I mention that this program will run on either the 40 or
  1781. 80-column screen?  Toggling an entire column can take a couple of seconds.
  1782.  
  1783. If there are more than 18 MS-DOS files, you can press the "+" and "-" keys to
  1784. move among all of the screens of files.  The cursor movement keys will wrap
  1785. around on the current screen.  "+" is page forward, and "-" is page backward.
  1786. The screens wrap around too.
  1787.  
  1788. After you have selected all of the files you want to copy and their translation
  1789. and file-type fields have been set, press the C key to go into Copy Mode (next
  1790. section).  After copying, you are returned to the main screen with all of the
  1791. field settings still intact.  To exit from the program, press Q.
  1792.  
  1793. 2.3. COPY MODE
  1794.  
  1795. When you enter copy mode, the screen will clear and the name of each selected
  1796. file is displayed as it is being copied.  If an error is encountered on either
  1797. the MS-DOS or CBM-DOS drive during copying, an error message will be displayed
  1798. and copying will continue (after you press a key for MS-DOS errors).
  1799.  
  1800. To generate a CBM-DOS filename from an MS-DOS filename, the eight filename
  1801. characters are taken (including spaces) and a dot (.) and the three characters
  1802. of the extension are appended.  Then, all spaces are removed, and if the name
  1803. ends with a dot (.) character, then that dot character is removed as well.  I
  1804. think this is fairly reasonable.
  1805.  
  1806. If there already is a file with the same filename on the CBM-DOS disk, then
  1807. you will be prompted if you want to overwrite the file or not.  Entering an
  1808. "n" will abort the copying of that file and go on to the next file, and
  1809. entering a "y" (or anything else) will cause the CBM-DOS file to be
  1810. "scratched" and then re-written.
  1811.  
  1812. The physical copying of the file is done completely in machine language and
  1813. nothing is displayed on the screen while this is happening, but you can follow
  1814. things by looking at das blinkin lichtes and listening for clicks and grinds.
  1815. You will probably be surprised by the MS-DOS file reading speed (I mean in a
  1816. good way).  The disk data is read in whole tracks and cached in memory and the
  1817. directory information and the FAT are retained in memory as well.  The result
  1818. is that minimal time is spent reading disk data, and no costly seeks are
  1819. required for opening a new MS-DOS file.  A result is that small files are
  1820. copied one after another very quickly.  You will have to wait, however, on the
  1821. relatively slow standard kernel/Commodore-DOS file writing.
  1822.  
  1823. A few changes had to be made to the program to accomodate the RAMDOS program.
  1824. RAMDOS uses memory from $2300 to $3FFF of RAM0, which is not really a good
  1825. place for a device driver, and it uses some of the zero-page locations that I
  1826. wanted to use.  But, difficulties were overcome.  The importance of RAMDOS
  1827. compatibility is that if you only have one disk drive but you have an REU, you
  1828. can use RAMDOS to store the MS-DOS files temporarily.  If you only have one
  1829. disk drive and no REU, you are SOL (Out of Luck) unless you can get a
  1830. RamDisk-type program for an unexpanded 128.  The RAMDOS program is available
  1831. from FTP site "ccosun.caltech.edu" in file "/pub/rknop/util128/ramdosii.sfx".
  1832. One note I found out about RAMDOS: you cannot use a
  1833.  
  1834. DOPEN#1,(CF$),U(CD),W
  1835.  
  1836. with it like you are supposed to be able to; you have to use a
  1837.  
  1838. DOPEN#1,(CF$+",W"),U(CD)
  1839.  
  1840. Here is a table of copying speeds for copying from 1571s and 1581s with ASC
  1841. and BIN translation modes.  All figures are in bytes/second.  These results
  1842. were obtained from copying a 127,280 byte text file (the text of C= Hacking
  1843. Issue #3).
  1844.  
  1845.    FROM  \ TO: "null"     RAMLink     RAMDOS     JD1581     JD1571
  1846.    -------+    ------     -------     ------     ------     ------
  1847.    81-bin |      5772        3441       2146        n/a        644
  1848.    81-asc |      5772        3434       2164        n/a        661
  1849.    71-bin |      4323        2991       1949       1821        n/a
  1850.    71-asc |      4323        2982       1962       1847        n/a
  1851.  
  1852. The "null" device is that "0" CBM-DOS device number, and a couple of entries
  1853. are "n/a" since I only have one 1571 and one 1581.  Note that my 71 and 81 are
  1854. JiffyDOS-ified, so the performance of a stock 71/81 will be poorer.  JiffyDOS
  1855. gives about a 2x performance improvement for the standard file accessing calls
  1856. (open, close, chrin, chrout).  RAMDOS doesn't seem to be as snappy as you
  1857. might think.
  1858.  
  1859. The "null" figures are quite impressive, but the raw sector reading speed
  1860. without the overhead of mucking around with file organization is 6700
  1861. bytes/sec for a 1581 and 4600 B/s for a 71.  The reason that the 1571 operates
  1862. so quickly is that I use a sector interleave of 4 (which is optimal) for
  1863. reading the tracks.  I think that other MS-DOS file copier program uses an
  1864. interleave of 1 (which is not optimal).  I lose some of the raw performance
  1865. because I copy the file data internally once before outputting it (to simplify
  1866. some of the code).
  1867.  
  1868. In a couple of places you will notice that ASC translation gives slightly
  1869. better or slightly worse performance than BIN.  This is because although
  1870. slightly more work is required to translate the characters, slightly fewer
  1871. characters will have to be written to the CBM-DOS file, since PETSCII uses
  1872. only CR where MS-DOS ASCII uses CR and LF to represent end-of-line.
  1873. Translation is done by using a table (that you can change if you wish).  Many
  1874. entries in this table contain a value of zero, which means that no character
  1875. will be output on translation.  Most of the control characters and all of the
  1876. characters of value 128 (0x80) or greater are thrown away on being
  1877. translated.  The table is set up so that CR characters are thrown away and the
  1878. LF character is translated to a CBM-DOS CR character.  Thus, both MS-DOS ASCII
  1879. files and UNIX ASCII files can be translated correctly.
  1880.  
  1881. 2. BURST COMMANDS
  1882.  
  1883. Three burst commands from the 1571/81 disk drive Burst Command Instruction Set
  1884. are required to allow this program to read the MS-DOS disks: Query Disk
  1885. Format, Sector Interleave, and Read.  The grungy details about issuing burst
  1886. commands and burst mode handshaking are covered in C= Hacking Issue #3.  The
  1887. Query Disk Format command is used to "log in" the MS-DOS disk.  The Inquire
  1888. Disk burst command cannot be used with an MS-DOS disk on the 1581 for some
  1889. unknown reason.  I found this out the hard way.  The Query Disk Format command
  1890. has the following format:
  1891.  
  1892.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  1893.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  1894.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  1895.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  1896.      2    |     F  |  X  |  X  |  S  |  1  |  0  |  1  |  N  |  10
  1897.    -------+--------------------------------------------------+-------
  1898.  
  1899. where the F, S, and N bits have a value of 0 for our purposes.  A response of
  1900. a burst status byte and six other throw-away bytes is given from the drive.
  1901. This command takes quite a long time to execute on my 1581 but works quite
  1902. quickly on my 1571.  You only have to log in a disk whenever you change
  1903. disks.
  1904.  
  1905. The Sector Interleave command is used to set a soft interleave for the Read
  1906. command.  I use an interleave of 1 for the 1581 and an interleave of 4 for the
  1907. 1571.  This means that the MS-DOS sectors will come from 1571 to the computer
  1908. in the following order: 1, 5, 9, 4, 8, 3, 7, 2, 6 (there are 9 sectors per
  1909. track on an MS-DOS disk (both 3.5" and 5.25"), numbered from 1 to 9).  LRR
  1910. handles the data coming in in this order, and in straight order from the
  1911. 1581.  The Sector Interleave command has the following format, where the W and
  1912. N bits are 0 for us:
  1913.  
  1914.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  1915.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  1916.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  1917.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  1918.      2    |     W  |  X  |  X  |  0  |  1  |  0  |  0  |  N  |   8
  1919.      3    |                   <interleave>                   | 1 or 4
  1920.    -------+--------------------------------------------------+-------
  1921.  
  1922. The Read command is used to transfer the nine sectors of a track to the
  1923. computer in the order specified by the interleave.  The format is:
  1924.  
  1925.     BYTE \ bit: 7     6     5     4     3     2     1     0  | Value
  1926.    -------+--------+-----+-----+-----+-----+-----+-----+-----+-------
  1927.      0    |     0  |  1  |  0  |  1  |  0  |  1  |  0  |  1  |  "U"
  1928.      1    |     0  |  0  |  1  |  1  |  0  |  0  |  0  |  0  |  "0"
  1929.      2    |    T/L |  E  | B/X |  S  |  0  |  0  |  0  |  N  | 0 or 16
  1930.      3    |                      <track>                     |  ???
  1931.      4    |                      <sector>                    |   1
  1932.      5    |                <number of sectors>               |   9
  1933.    -------+--------------------------------------------------+-------
  1934.  
  1935. There are a couple of differences between the 1571 and 1581 versions of this
  1936. command.  Most important, the S bit (Side of disk to use) has the opposite
  1937. meaning on the two drives.  There's no good reason that I know of for this
  1938. inconsistency.  This is the reason that LRR needs to know what type of MS-DOS
  1939. drive it is dealing with (plus interleaving).
  1940.  
  1941. The read command returns the following data using burst mode handshaking:
  1942.  
  1943.        +-------------------+
  1944.      0 | Burst Status Byte |
  1945.        +-------------------+
  1946.      1 |                   |
  1947.    ... +  512 Data Bytes   |
  1948.    512 |                   |
  1949.        +-------------------+
  1950.  
  1951. for each sector transferred.  If the Burst Status Byte indicates an error,
  1952. then the data is not transferred and none of the following sectors are
  1953. either.  If the status byte gives a "Disk Changed" error, then you have to log
  1954. in the disk with the Query Disk Format command before read will work
  1955. properly.  This is actually a good feature since it lets you know about a disk
  1956. change so you can update any data structures you may have.  LRR simply re-logs
  1957. in the disk without updating any data structures and re-tries the failed read
  1958. operation.
  1959.  
  1960. 3. MS-DOS DISK FORMAT
  1961.  
  1962. An MS-DOS disk is separated into 4 different parts: the Boot Sector, the
  1963. FAT(s), the Root Directory, and the File Data Sectors.  The logical sectors
  1964. (blocks) of a disk are numbered from 0 to some maximum number (1439 for a
  1965. 3.5", 719 for a 5.25" DD disk).  The physical layout and the logical sector
  1966. numbers typically used by a 3.5" disk are shown here:
  1967.  
  1968.          +-------------------+
  1969.        0 |    Boot Sector    |
  1970.          +-------------------+
  1971.     1..3 |    FAT copy #1    |
  1972.          +-------------------+
  1973.     4..6 |    FAT copy #2    |
  1974.          +-------------------+
  1975.    7..14 |  Root Directory   |
  1976.          +-------------------+
  1977.       15 |                   |
  1978.      ... | File Data Sectors |
  1979.     1439 |                   |
  1980.          +-------------------+
  1981.  
  1982. 3.1. THE BOOT SECTOR
  1983.  
  1984. The Boot Sector is always at logical sector number 0.  It contains some
  1985. important information about the format of the disk and it also contains code
  1986. to boot an MS-DOS machine from.  We aren't concerned with the bootstrapping
  1987. code, but the important values we need to obtain from the boot sector are:
  1988.  
  1989.    ABBR     OFFSET     1571     1581     DESCRIPTION
  1990.    ----     ------     ----     ----     -----------
  1991.     CS        13          2        2     Cluster size in sectors
  1992.     NB        14          1        1     Number of boot sectors
  1993.     NF        16          2        2     Number of FATs
  1994.     FL        23          2        3     FAT size in sectors
  1995.     DE        17        112      112     Number of root directory entries
  1996.     TS       19,20      720     1440     Total Number of sectors
  1997.     NS        24          9        9     Number of sectors per track
  1998.     NH        26          2        2     Number of sides
  1999.  
  2000. The 1571 and 1581 columns give the typical values of these parameters for
  2001. 5.25" and 3.5" disks.  The OFFSET is the address of the parameter within the
  2002. boot sector.  The total number of sectors is given in low-byte, high-byte
  2003. order (since the 80x86 family is little-endian like the 6502 family).  From
  2004. the above parameters, we can derive the following important parameters:
  2005.  
  2006.    ABBR     FORMULA       1571     1581     DESCRIPTION
  2007.    ----      -------      ----     ----     -----------
  2008.     F1      NB+NF*FL         5        7     First root directory sector
  2009.     FS     NB+NF*FL+DE      12       14     First file data sector number
  2010.     NC     (TS-FS)/CS      354      713     Total number of file clusters
  2011.  
  2012. LRR imposes a number of limits on these parameters and will error-out if you
  2013. try to use a disk that is outside of LRR's limits.
  2014.  
  2015. 3.2. CHEWING THE FAT
  2016.  
  2017. MS-DOS disks use a data structure called a File Allocation Table (FAT) to
  2018. record which clusters belong to which file in what order and which blocks are
  2019. free.  A cluster is a set of contiguous sectors which are allocated to files
  2020. as a group.  LRR handles cluster sizes of 1 and 2 sectors, giving a logical
  2021. file block size of 512 or 1024 bytes.  Typically, a cluster size of 2 sectors
  2022. is used.
  2023.  
  2024. The FAT is an array of 12-bit numbers, with an entry corresponding to each
  2025. cluster that can be allocated to files.  FAT entries 0 and 1 are reserved.  If
  2026. a FAT entry contains a value of $000, then the corresponding cluster is free
  2027. and can be allocated to a file; otherwise, the cluster is allocated and the
  2028. FAT entry contains the number of the NEXT FAT entry that belongs to the file.
  2029. Thus, MS-DOS files are stored in a singly-linked list of clusters like
  2030. Commodore-DOS files are, except that the links are not in the data sectors but
  2031. rather are in the FAT.  The pointer to the first FAT entry for a file is given
  2032. in the file's directory entry.
  2033.  
  2034. A special NULL/NIL pointer value of $FFF is used to indicate the end of the
  2035. chain of clusters allocated to a file.  This value is stored in the FAT entry
  2036. of the last cluster allocated to a file (of course).  Consider the following
  2037. example FAT:
  2038.  
  2039.    ENTRY       VALUE
  2040.    -----       -----
  2041.     $000        $FFF
  2042.     $001        $FFF
  2043.     $002   |----$003 <------Directory Entry
  2044.     $003   +--> $005----+
  2045.     $004        $000    |
  2046.     $005        $FFF <--+
  2047.  
  2048. Entries 0 and 1 are insignificant since they are reserved.  Say that a file
  2049. starts at FAT entry #2.  Then, it consists of the following chain of clusters:
  2050. 2, 3, and 5.  Cluster #4 is free.  Clusters can be allocated to a file in
  2051. random order, but if they are allocated contiguously in forward order, then
  2052. the file will be able to be read faster.  The FAT is such an important data
  2053. structure that typically two copies are kept on the disk incase one of them
  2054. should become corrupted.
  2055.  
  2056. The MS-DOS designers were a little sneaky in storing the 12-bit FAT entries -
  2057. they used only 12 real bits per entry.  Ie., they store two FAT entries in
  2058. three bytes, where the two entries share the two nybbles of the middle byte.
  2059. The following diagram shows how the nybbles 1 (high), 2 (mid), and 3 (low) are
  2060. stored into FAT entries A and B:
  2061.  
  2062.      BYTE:       0            1            2
  2063.              +---+---+    +---+---+    +---+---+
  2064.     ENTRY:   | A | A |    | B | A |    | B | B |
  2065.    NYBBLE:   | 2 | 3 |    | 3 | 1 |    | 1 | 2 |
  2066.              +---+---+    +---+---+    +---+---+
  2067.  
  2068. Anyway, let's just say it's a bit tricky to extract the 12-bit values from
  2069. this compressed data structure.  On top of that, I don't think there is any
  2070. saving in disk space resulting from compressing this structure; they might as
  2071. well have just used a 16-bit FAT (like they do nowadays on larger disks).
  2072.  
  2073. 3.3. THE ROOT DIRECTORY
  2074.  
  2075. The root directory has a fixed size, although I don't think that
  2076. subdirectories do.  LRR cannot access subdirectories.  Each 512-byte sector of
  2077. the root directory contains sixteen 32-byte directory entries.  One directory
  2078. entry is required for each file stored on the disk.  A directory entry has the
  2079. following structure:
  2080.  
  2081.    OFFSET     LEN     DESCRIPTION
  2082.    ------     ---     -----------
  2083.      0..7       8     Filename
  2084.     8..10       3     Extension
  2085.        11       1     <unused?>
  2086.        12       1     Attributes: $10=Directory, $08=VolumeId
  2087.    13..21       9     <unused>
  2088.    22..25       4     Date
  2089.    26..27       2     Starting FAT entry number
  2090.    28..31       4     File length in bytes
  2091.  
  2092. The filename and extension are stored with trailing padding spaces.  If a
  2093. directory entry is unused or deleted, then the first character of the filename
  2094. is either a $00 or a $E5 (229).  This is why you have to provide the first
  2095. character of a filename if you are undeleting a file on an MS-DOS machine.
  2096. Note that there is enough unused space that Microsoft or IBM could have
  2097. ditched the annoying 8+3 character filename format.
  2098.  
  2099. The attributes bits tell whether the directory entry is for a regular file, a
  2100. subdirectory, a disk volume name (in which case there is no file data), and a
  2101. couple of other things I can't remember.  I'm not sure about the exact
  2102. position or format of the date, but LRR doesn't use it anyway.  The starting
  2103. FAT entry number and the file length are stored in lower-byte-first order.
  2104.  
  2105. 3.4. THE FILE DATA SPACE
  2106.  
  2107. The ramainder of the disk space is used for storing file data in clusters of 1
  2108. or 2 sectors each.  Given a cluster number (which is also the FAT entry
  2109. number), the following formula is used to calculate the starting logical
  2110. sector number of the cluster:
  2111.  
  2112. (ClusterNumber - 2) * ClusterSizeInBlocks + FirstFileDataLogicalSectorNumber
  2113.  
  2114. where "FirstFileDataLogicalSectorNumber" is the "FS" parameter derived
  2115. earlier.  The following consecutive logical sector numbers up to the number of
  2116. sectors per cluster form the rest of the cluster.  Note that a single cluster
  2117. can span sectors from one side of the disk to another or from one track to
  2118. another.  We perform the "(ClusterNumber - 2)" portion of the calculation
  2119. since the first two FAT entries are reserved.
  2120.  
  2121. Since the Read burst command of the 1571/81 wants the side, track, and sector
  2122. number of a sector rather than its logical number, we also need formulae for
  2123. these conversions:
  2124.  
  2125.    Track  = LogicalSectorNumber / 18
  2126.    Sector = LogicalSectorNumber % 9 + 1
  2127.    Side   = (LogicalSectorNumber / 9) % 2
  2128.  
  2129. These formulae are more problematic than the previous one since they require
  2130. division by 9 and 18.  LRR uses the method of repeated subtraction to perfrom
  2131. the necessary division (only one division is necessary).  The above formulae
  2132. imply that sequential logical sectors are stored on the top of the disk
  2133. first and then the bottom of the disk of the same track, and then on the top
  2134. of the next track, etc.  This is a good sector numbering scheme (unlike the
  2135. CBM-DOS scheme for 1571 sectors) since it is faster to switch sides of the
  2136. disk than it is to switch tracks, so you can read the disk faster.
  2137.  
  2138. Oh yeah, the way that you know how many file data bytes are in the last
  2139. cluster of a file chain (the cluster with the NULL FAT entry) is to take the
  2140. file length from the directory entry and "mod" (the C language % operator) it
  2141. with the cluster size.  One special case is if this calculation results in a
  2142. zero, then the last cluster is completely full (rather than completely empty
  2143. as the calculation would suggest).  This calculation is easily done in
  2144. machine language with an AND operation since the cluster size is always a
  2145. power of two.
  2146.  
  2147. 4. FILE COPYING PACKAGE
  2148.  
  2149. This section discusses the interface to and implementation of the MS-DOS file
  2150. copying package.  It is written in assembly language and is loaded into memory
  2151. at address $8000 on bank 0 and requires about 13K of memory.  The package is
  2152. loaded at this high address to be out of the way of the main BASIC program,
  2153. even if RAMDOS is installed.
  2154.  
  2155. 4.1. INTERFACE
  2156.  
  2157. The subroutine call and parameter passing interface to the file copying
  2158. package is summarized as follows:
  2159.  
  2160.    ADDRESS     DESCRIPTION
  2161.    -------     -----------
  2162.    PK          InitPackage subroutine
  2163.    PK+3        LoadDirectory subroutine
  2164.    PK+6        CopyFile subroutine
  2165.    PK+9        two-byte package identification number
  2166.    PK+15       errno : error code returned
  2167.    PK+16       MS-DOS device number (8 to 30)
  2168.    PK+17       MS-DOS device type ($00=1571, $FF=1581)
  2169.    PK+18       two-byte starting cluster number for file copying
  2170.    PK+20       low and mid bytes of file length for copying
  2171.  
  2172. where "PK" is the load address of the package.  Additional subroutine
  2173. parameters are passed in the processor registers.
  2174.  
  2175. The "InitPackage" subroutine should be called when the package is first
  2176. installed, whenever the MS-DOS device number is changed, and whenever a new
  2177. disk is mounted to invalidate the internal track cache.  It requires no
  2178. parameters.
  2179.  
  2180. The "LoadDirectory" subroutine will load the directory, FAT, and the Boot
  2181. Sector parameters into the internal memory of the package from the current
  2182. MS-DOS device number.  No (other) input parameters are needed and the
  2183. subroutine returns a pointer to the directory space in the .AY registers and
  2184. the number of directory entries in the .X register.  If an error occurs, then
  2185. the subroutine returns with the Carry flag set and the error code is available
  2186. in the "errno" interface variable.  The directory entry data is in the
  2187. directory space as it was read in raw from the directory sectors on the MS-DOS
  2188. disk.
  2189.  
  2190. The "CopyFile" subroutine will copy a single file from the MS-DOS disk to a
  2191. specified CBM-Kernal logical file number (the CBM file must already be
  2192. opened).  If the CBM logical file number is zero, then the file data is simply
  2193. discarded after it is read from the MS-DOS file.  The starting cluster number
  2194. of the file to copy and the low and mid bytes of the file length are passed in
  2195. the PK+18 and PK+20 interface words.  The translation mode to use is passed in
  2196. the .A register ($00=binary, $FF=ascii) and the CBM logical file number to
  2197. output to is passed in the .X register.  If an error occurs, the routine
  2198. returns with the Carry flag set and the error code in the "errno" interface
  2199. variable.  There are no other output parameters.
  2200.  
  2201. Note that since the starting cluster number and low-file length of the file to
  2202. be copied are required rather than the filename, it is the responsibility of
  2203. the front-end application program to dig through the raw directory sector data
  2204. to get this information.  The application must also open the Commodore-DOS
  2205. file of whatever filetype on whatever device is required; the package does not
  2206. need to know the Commodore-DOS device number.
  2207.  
  2208. The MS-DOS device number and device type interface variables allow you to set
  2209. the MS-DOS drive and the package identification number allows the application
  2210. program to check if the package is already loaded into memory so that it only
  2211. has to load the package the first time the application is run and not on
  2212. re-runs.  The identification sequence is a value of $CB followed by a value
  2213. of 131.
  2214.  
  2215. 4.2. IMPLEMENTATION
  2216.  
  2217. This section presents the code that implements the MS-DOS file reading
  2218. package.  It is here in a special form; each code line is preceded by a few
  2219. special characters and the line number.  The special characters are there to
  2220. allow you to easily extract the assembler code from the rest of this magazine
  2221. (and all of my ugly comments).  On a Unix system, all you have to do is
  2222. execute the following command line (substitute filenames as appropriate):
  2223.  
  2224. grep '^\.%...\!' Hack4 | sed 's/^.%...\!..//' | sed 's/.%...\!//' >lrr.s
  2225.  
  2226. You'll notice that the initial comment lines here were an afterthought.
  2227.  
  2228. .%000!  ;Little Red Reader MS-DOS file copier program
  2229. .%000!  ;written 92/10/03 by Craig Bruce for C= Hacking Net Magazine
  2230. .%000!
  2231.  
  2232. The code is written for the Buddy assembler and here are a couple setup
  2233. directives.  Note that my comments come before the section of code.
  2234.  
  2235. .%001!  .org $8000
  2236. .%002!  .obj "@:lrr.bin"
  2237. .%003!
  2238. .%004!  ;====jump table and parameters interface ====
  2239. .%005!
  2240. .%006!  jmp initPackage
  2241. .%007!  jmp loadDirectory
  2242. .%008!  jmp copyFile
  2243. .%009!
  2244. .%010!  .byte $cb,131   ;identification
  2245. .%011!  .byte 0,0,0,0
  2246. .%012!
  2247.  
  2248. These variables are included in the package program space to minimize unwanted
  2249. interaction with other programs loaded at the same time, such as the RAMDOS
  2250. device driver.
  2251.  
  2252. .%013!  errno           .buf 1    ;(location pk+15)
  2253. .%014!  sourceDevice    .buf 1
  2254. .%015!  sourceType      .buf 1    ;$00=1571, $ff=1581
  2255. .%016!  startCluster    .buf 2
  2256. .%017!  lenML           .buf 2    ;length medium and low bytes
  2257. .%018!
  2258. .%019!  ;====global declaraions====
  2259. .%020!
  2260. .%021!  kernelListen = $ffb1
  2261. .%022!  kernelSecond = $ff93
  2262. .%023!  kernelUnlsn  = $ffae
  2263. .%024!  kernelAcptr  = $ffa2
  2264. .%025!  kernelCiout  = $ffa8
  2265. .%026!  kernelSpinp  = $ff47
  2266. .%027!  kernelChkout = $ffc9
  2267. .%028!  kernelClrchn = $ffcc
  2268. .%029!  kernelChrout = $ffd2
  2269. .%030!
  2270. .%031!  st = $d0
  2271. .%032!  ciaClock = $dd00
  2272. .%033!  ciaFlags = $dc0d
  2273. .%034!  ciaData  = $dc0c
  2274. .%035!
  2275.  
  2276. These are the parameters and derived parameters from the boot sector.  They
  2277. are kept in the program space to avoid interactions.
  2278.  
  2279. .%036!  clusterBlockCount .buf 1        ;1 or 2
  2280. .%037!  fatBlocks         .buf 1        ;up to 3
  2281. .%038!  rootDirBlocks     .buf 1        ;up to 8
  2282. .%039!  rootDirEntries    .buf 1        ;up to 128
  2283. .%040!  totalSectors      .buf 2        ;up to 1440
  2284. .%041!  firstFileBlock    .buf 1
  2285. .%042!  firstRootDirBlock .buf 1
  2286. .%043!  fileClusterCount  .buf 2
  2287. .%044!
  2288.  
  2289. The cylinder (track) and side that is currently stored in the trach cache.
  2290.  
  2291. .%045!  bufCylinder     .buf 1
  2292. .%046!  bufSide         .buf 1
  2293. .%047!  formatParms     .buf 6
  2294. .%048!
  2295.  
  2296. This package is split into a number of levels.  This level interfaces with the
  2297. Kernal serial bus routines and the burst command protocol of the disk drives.
  2298.  
  2299. .%049!  ;====hardware level====
  2300. .%050!
  2301.  
  2302. Connect to the MS-DOS device and send the "U0" burst command prefix and the
  2303. burst command byte.
  2304.  
  2305. .%051!  sendU0 = *  ;( .A=burstCommandCode ) : .CS=err
  2306. .%052!     pha
  2307. .%053!     lda #0
  2308. .%054!     sta st
  2309. .%055!     lda sourceDevice
  2310. .%056!     jsr kernelListen
  2311. .%057!     lda #$6f
  2312. .%058!     jsr kernelSecond
  2313. .%059!     lda #"u"
  2314. .%060!     jsr kernelCiout
  2315. .%061!     bit st
  2316. .%062!     bmi sendU0Error
  2317. .%063!     lda #"0"
  2318. .%064!     jsr kernelCiout
  2319. .%065!     pla
  2320. .%066!     jsr kernelCiout
  2321. .%067!     bit st
  2322. .%068!     bmi sendU0Error
  2323. .%069!     clc
  2324. .%070!     rts
  2325. .%071!
  2326. .%072!     sendU0Error = *
  2327. .%073!     lda #5
  2328. .%074!     sta errno
  2329. .%075!     sec
  2330. .%076!     rts
  2331. .%077!
  2332.  
  2333. Toggle the "Data Accepted / Ready For More" clock signal for the burst
  2334. transfer protocol.
  2335.  
  2336. .%078!  toggleClock = *
  2337. .%079!     lda ciaClock
  2338. .%080!     eor #$10
  2339. .%081!     sta ciaClock
  2340. .%082!     rts
  2341. .%083!
  2342.  
  2343. Wait for a burst byte to arrive in the serial data register of CIA#1 from the
  2344. fast serial bus.
  2345.  
  2346. .%084!  serialWait = *
  2347. .%085!     lda #$08
  2348. .%086!  -  bit ciaFlags
  2349. .%087!     beq -
  2350. .%088!     rts
  2351. .%089!
  2352.  
  2353. Wait for and get a burst byte from the fast serial bus, and send the "Data
  2354. Accepted" signal.
  2355.  
  2356. .%090!  getBurstByte = *
  2357. .%091!     jsr serialWait
  2358. .%092!     ldx ciaData
  2359. .%093!     jsr toggleClock
  2360. .%094!     txa
  2361. .%095!     rts
  2362. .%096!
  2363.  
  2364. Send the burst commands to "log in" the MS-DOS disk and set the Read sector
  2365. interleave factor.
  2366.  
  2367. .%097!  mountDisk = *  ;() : .CS=err
  2368. .%098!     lda #%00011010
  2369. .%099!     jsr sendU0
  2370. .%100!     bcc +
  2371. .%101!     rts
  2372. .%102!  +  jsr kernelUnlsn
  2373. .%103!     bit st
  2374. .%104!     bmi sendU0Error
  2375. .%105!     clc
  2376. .%106!     jsr kernelSpinp
  2377. .%107!     bit ciaFlags
  2378. .%108!     jsr toggleClock
  2379. .%109!     jsr getBurstByte
  2380. .%110!     sta errno
  2381. .%111!     and #$0f
  2382. .%112!     cmp #2
  2383. .%113!     bcs mountExit
  2384.  
  2385. Grab the throw-away parameters from the mount operation.
  2386.  
  2387. .%114!     ldy #0
  2388. .%115!  -  jsr getBurstByte
  2389. .%116!     sta formatParms,y
  2390. .%117!     iny
  2391. .%118!     cpy #6
  2392. .%119!     bcc -
  2393. .%120!     clc
  2394.  
  2395. Set the sector interleave to 1 for a 1581 or 4 for a 1571.
  2396.  
  2397. .%121!     ;** set interleave
  2398. .%122!     lda #%00001000
  2399. .%123!     jsr sendU0
  2400. .%124!     bcc +
  2401. .%125!     rts
  2402. .%126!  +  lda #1            ;interleave of 1 for 1581
  2403. .%127!     bit sourceType
  2404. .%128!     bmi +
  2405. .%129!     lda #4            ;interleave of 4 for 1571
  2406. .%130!  +  jsr kernelCiout
  2407. .%131!     jsr kernelUnlsn
  2408. .%132!     mountExit = *
  2409. .%133!     rts
  2410. .%134!
  2411.  
  2412. Read all of the sectors of a given track into the track cache.
  2413.  
  2414. .%135!  bufptr = 2
  2415. .%136!  secnum = 4
  2416. .%137!
  2417. .%138!  readTrack = *  ;( .A=cylinder, .X=side ) : trackbuf, .CS=err
  2418. .%139!     pha
  2419. .%140!     txa
  2420.  
  2421. Get the side and put it into the command byte.  Remember that we have to flip
  2422. the side bit for a 1581.
  2423.  
  2424. .%141!     and #$01
  2425. .%142!     asl
  2426. .%143!     asl
  2427. .%144!     asl
  2428. .%145!     asl
  2429. .%146!     bit sourceType
  2430. .%147!     bpl +
  2431. .%148!     eor #$10
  2432. .%149!  +  jsr sendU0
  2433. .%150!     bcc +
  2434. .%151!     rts
  2435. .%152!  +  pla                  ;cylinder number
  2436. .%153!     jsr kernelCiout
  2437. .%154!     lda #1               ;start sector number
  2438. .%155!     jsr kernelCiout
  2439. .%156!     lda #9               ;sector count
  2440. .%157!     jsr kernelCiout
  2441. .%158!     jsr kernelUnlsn
  2442.  
  2443. Prepare to receive the track data.
  2444.  
  2445. .%159!     sei
  2446. .%160!     clc
  2447. .%161!     jsr kernelSpinp
  2448. .%162!     bit ciaFlags
  2449. .%163!     jsr toggleClock
  2450. .%164!     lda #<trackbuf
  2451. .%165!     ldy #>trackbuf
  2452. .%166!     sta bufptr
  2453. .%167!     sty bufptr+1
  2454.  
  2455. Get the sector data for each of the 9 sectors of the track.
  2456.  
  2457. .%168!     lda #0
  2458. .%169!     sta secnum
  2459. .%170!  -  bit sourceType
  2460. .%171!     bmi +
  2461.  
  2462. If we are dealing with a 1571, we have to set the buffer pointer for the next
  2463. sector, taking into account the soft interleave of 4.
  2464.  
  2465. .%172!     jsr get1571BufPtr
  2466. .%173!  +  jsr readSector
  2467. .%174!     bcs trackExit
  2468. .%175!     inc secnum
  2469. .%176!     lda secnum
  2470. .%177!     cmp #9
  2471. .%178!     bcc -
  2472. .%179!     clc
  2473. .%180!     trackExit = *
  2474. .%181!     cli
  2475. .%182!     rts
  2476. .%183!
  2477.  
  2478. Get the buffer pointer for the next 1571 sector.
  2479.  
  2480. .%184!  get1571BufPtr = *
  2481. .%185!     lda #<trackbuf
  2482. .%186!     sta bufptr
  2483. .%187!     ldx secnum
  2484. .%188!     clc
  2485. .%189!     lda #>trackbuf
  2486. .%190!     adc bufptr1571,x
  2487. .%191!     sta bufptr+1
  2488. .%192!     rts
  2489. .%193!
  2490. .%194!  bufptr1571 = *
  2491. .%195!     .byte 0,8,16,6,14,4,12,2,10
  2492. .%196!
  2493.  
  2494. Read an individual sector into memory at the specified address.
  2495.  
  2496. .%197!  readSector = *  ;( bufptr ) : .CS=err
  2497.  
  2498. Get and check the burst status byte for errors.
  2499.  
  2500. .%198!     jsr getBurstByte
  2501. .%199!     sta errno
  2502. .%200!     and #$0f
  2503. .%201!     cmp #2
  2504. .%202!     bcc +
  2505. .%203!     rts
  2506. .%204!  +  ldx #2
  2507. .%205!     ldy #0
  2508. .%206!
  2509.  
  2510. Receive the 512 sector data bytes into memory.
  2511.  
  2512. .%207!     readByte = *
  2513. .%208!     lda #$08
  2514. .%209!  -  bit ciaFlags
  2515. .%210!     beq -
  2516. .%211!     lda ciaClock
  2517. .%212!     eor #$10
  2518. .%213!     sta ciaClock
  2519. .%214!     lda ciaData
  2520. .%215!     sta (bufptr),y
  2521. .%216!     iny
  2522. .%217!     bne readByte
  2523. .%218!     inc bufptr+1
  2524. .%219!     dex
  2525. .%220!     bne readByte
  2526. .%221!     rts
  2527. .%222!
  2528.  
  2529. This next level of routines deals with logical sectors and the track cache
  2530. rather than with hardware.
  2531.  
  2532. .%223!  ;====logical sector level====
  2533. .%224!
  2534.  
  2535. Invalidate the track cache if the MS-DOS drive number is changed or if a new
  2536. disk is inserted.  This routine has to establish a RAM configuration of $0E
  2537. since it will be called from RAM0.  Configuration $0E gives RAM0 from $0000 to
  2538. $BFFF, Kernal ROM from $C000 to $FFFF, and the I/O space over the Kernal from
  2539. $D000 to $DFFF.  This configuration is set by all application interface
  2540. subroutines.
  2541.  
  2542. .%225!  initPackage = *
  2543. .%226!     lda #$0e
  2544. .%227!     sta $ff00
  2545. .%228!     lda #$ff
  2546. .%229!     sta bufCylinder
  2547. .%230!     sta bufSide
  2548. .%231!     clc
  2549. .%232!     rts
  2550. .%233!
  2551.  
  2552. Locate a sector (block) in the track cache, or read the corresponding physical
  2553. track into the track cache if necessary.  This routine accepts the cylinder,
  2554. side, and sector numbers of the block.
  2555.  
  2556. .%234!  sectorSave = 5
  2557. .%235!
  2558. .%236!  readBlock = *  ;( .A=cylinder,.X=side,.Y=sector ) : .AY=blkPtr,.CS=err
  2559.  
  2560. Check if the correct track is in the track cache.
  2561.  
  2562. .%237!     cmp bufCylinder
  2563. .%238!     bne readBlockPhysical
  2564. .%239!     cpx bufSide
  2565. .%240!     bne readBlockPhysical
  2566.  
  2567. If so, then locate the sector's address and return that.
  2568.  
  2569. .%241!     dey
  2570. .%242!     tya
  2571. .%243!     asl
  2572. .%244!     clc
  2573. .%245!     adc #>trackbuf
  2574. .%246!     tay
  2575. .%247!     lda #<trackbuf
  2576. .%248!     clc
  2577. .%249!     rts
  2578. .%250!
  2579.  
  2580. Here, we have to read the physical track into the track cache.  We save the
  2581. input parameters and call the hardware-level track-reading routine.
  2582.  
  2583. .%251!     readBlockPhysical = *
  2584. .%252!     sta bufCylinder
  2585. .%253!     stx bufSide
  2586. .%254!     sty sectorSave
  2587. .%255!     jsr readTrack
  2588.  
  2589. Check for errors.
  2590.  
  2591. .%256!     bcc readBlockPhysicalOk
  2592. .%257!     lda errno
  2593. .%258!     and #$0f
  2594. .%259!     cmp #11    ;disk change
  2595. .%260!     beq +
  2596. .%261!     sec
  2597. .%262!     rts
  2598.  
  2599. If the error that happened is a "Disk Change" error, then mount the disk and
  2600. try to read the physical track again.
  2601.  
  2602. .%263!  +  jsr mountDisk
  2603. .%264!     lda bufCylinder
  2604. .%265!     ldx bufSide
  2605. .%266!     ldy sectorSave
  2606. .%267!     bcc readBlockPhysical
  2607. .%268!     rts
  2608. .%269!
  2609.  
  2610. Here, the physical track has been read into the track cache ok, so we recover
  2611. the original input parameters and try the top of the routine again.
  2612.  
  2613. .%270!     readBlockPhysicalOk = *
  2614. .%271!     lda bufCylinder
  2615. .%272!     ldx bufSide
  2616. .%273!     ldy sectorSave
  2617. .%274!     jmp readBlock
  2618. .%275!
  2619.  
  2620. Divide the given number by 18.  This is needed for the calculations to convert
  2621. a logical sector number to the corresponding physical cylinder, side, and
  2622. sector numbers that the lower-level routines require.  The method of repeated
  2623. subtraction is used.  This routine would probably work faster if we tried to
  2624. repeatedly subtract 360 (18*20) at the top, but I didn't bother.
  2625.  
  2626. .%276!  divideBy18 = *  ;( .AY=number ) : .A=quotient, .Y=remainder
  2627. .%277!     ;** could repeatedly subtract 360 here
  2628. .%278!     ldx #$ff
  2629. .%279!  -  inx
  2630. .%280!     sec
  2631. .%281!     sbc #18
  2632. .%282!     bcs -
  2633. .%283!     dey
  2634. .%284!     bpl -
  2635. .%285!     clc
  2636. .%286!     adc #18
  2637. .%287!     iny
  2638. .%288!     tay
  2639. .%289!     txa
  2640. .%290!     rts
  2641. .%291!
  2642.  
  2643. Convert the given logical block number to the corresponding physical cylinder,
  2644. side, and sector numbers.  This routine follows the formulae given earlier
  2645. with a few simplifying tricks.
  2646.  
  2647. .%292!  convertLogicalBlockNum = *  ;( .AY=blockNum ) : .A=cyl, .X=side, .Y=sec
  2648. .%293!     jsr divideBy18
  2649. .%294!     ldx #0
  2650. .%295!     cpy #9
  2651. .%296!     bcc +
  2652. .%297!     pha
  2653. .%298!     tya
  2654. .%299!     sbc #9
  2655. .%300!     tay
  2656. .%301!     pla
  2657. .%302!     ldx #1
  2658. .%303!  +  iny
  2659. .%304!     rts
  2660. .%305!
  2661.  
  2662. Copy a sequential group of logical sectors into memory.  This routine is used
  2663. by the directory loading routine to load the FAT and Root Directory, and is
  2664. used by the cluster reading routine to retrieve all of the blocks of a
  2665. cluster.  After the given starting logical sector number is converted into its
  2666. physical cylinder, side, and sector equivalent, the physical values are
  2667. incremented to get the address of successive sectors of the group.  This
  2668. avoids the overhead of the logical to physical conversion.  Quite a number of
  2669. temporaries are needed.
  2670.  
  2671. .%306!  destPtr = 6
  2672. .%307!  curCylinder = 8
  2673. .%308!  curSide = 9
  2674. .%309!  curSector = 10
  2675. .%310!  blockCountdown = 11
  2676. .%311!  sourcePtr = 12
  2677. .%312!
  2678. .%313!  copyBlocks = *  ;( .AY=startBlock, .X=blockCount, ($6)=dest ) : .CS=err
  2679. .%314!     stx blockCountdown
  2680. .%315!     jsr convertLogicalBlockNum
  2681. .%316!     sta curCylinder
  2682. .%317!     stx curSide
  2683. .%318!     sty curSector
  2684. .%319!
  2685. .%320!     copyBlockLoop = *
  2686. .%321!     lda curCylinder
  2687. .%322!     ldx curSide
  2688. .%323!     ldy curSector
  2689. .%324!     jsr readBlock
  2690. .%325!     bcc +
  2691. .%326!     rts
  2692. .%327!  +  sta sourcePtr
  2693. .%328!     sty sourcePtr+1
  2694. .%329!     ldx #2
  2695. .%330!     ldy #0
  2696.  
  2697. Here I unroll the copying loop a little bit to cut the overhead of the branch
  2698. instruction in half.  (A cycle saved... you know).
  2699.  
  2700. .%331!  -  lda (sourcePtr),y
  2701. .%332!     sta (destPtr),y
  2702. .%333!     iny
  2703. .%334!     lda (sourcePtr),y
  2704. .%335!     sta (destPtr),y
  2705. .%336!     iny
  2706. .%337!     bne -
  2707. .%338!     inc sourcePtr+1
  2708. .%339!     inc destPtr+1
  2709. .%340!     dex
  2710. .%341!     bne -
  2711.  
  2712. Increment the cylinder, side, sector values.
  2713.  
  2714. .%342!     inc curSector
  2715. .%343!     lda curSector
  2716. .%344!     cmp #10
  2717. .%345!     bcc +
  2718. .%346!     lda #1
  2719. .%347!     sta curSector
  2720. .%348!     inc curSide
  2721. .%349!     lda curSide
  2722. .%350!     cmp #2
  2723. .%351!     bcc +
  2724. .%352!     lda #0
  2725. .%353!     sta curSide
  2726. .%354!     inc curCylinder
  2727. .%355!  +  dec blockCountdown
  2728. .%356!     bne copyBlockLoop
  2729. .%357!     clc
  2730. .%358!     rts
  2731. .%359!
  2732.  
  2733. Read a cluster into the Cluster Buffer, given the cluster number.  The cluster
  2734. number is converted to a logical sector number and then the sector copying
  2735. routine is called.  The formula given earlier is used for the conversion.
  2736.  
  2737. .%360!  readCluster = *  ;( .AY=clusterNumber ) : clusterBuf, .CS=err
  2738. .%361!     ;** convert cluster number to logical block number
  2739. .%362!     sec
  2740. .%363!     sbc #2
  2741. .%364!     bcs +
  2742. .%365!     dey
  2743. .%366!  +  ldx clusterBlockCount
  2744. .%367!     cpx #1
  2745. .%368!     beq +
  2746. .%369!     asl
  2747. .%370!     sty 7
  2748. .%371!     rol 7
  2749. .%372!     ldy 7
  2750. .%373!  +  clc
  2751. .%374!     adc firstFileBlock
  2752. .%375!     bcc +
  2753. .%376!     iny
  2754. .%377!
  2755. .%378!     ;** read logical blocks comprising cluster
  2756. .%379!  +  ldx #<clusterBuf
  2757. .%380!     stx 6
  2758. .%381!     ldx #>clusterBuf
  2759. .%382!     stx 7
  2760. .%383!     ldx clusterBlockCount
  2761. .%384!     jmp copyBlocks
  2762. .%385!
  2763.  
  2764. This next level of routines deal with the data structures of the MS-DOS disk
  2765. format.
  2766.  
  2767. .%386!  ;====MS-DOS format level====
  2768. .%387!
  2769. .%388!  bootBlock = 2
  2770. .%389!
  2771.  
  2772. Read the disk format parameters, directory, and FAT into memory.
  2773.  
  2774. .%390!  loadDirectory = *  ;( ) : .AY=dirbuf, .X=dirEntries, .CS=err
  2775. .%391!     lda #$0e
  2776. .%392!     sta $ff00
  2777. .%393!
  2778.  
  2779. Read the boot sector and extract the parameters.
  2780.  
  2781. .%394!     ;** get parameters from boot sector
  2782. .%395!     lda #0
  2783. .%396!     ldy #0
  2784. .%397!     jsr convertLogicalBlockNum
  2785. .%398!     jsr readBlock
  2786. .%399!     bcc +
  2787. .%400!     rts
  2788. .%401!  +  sta bootBlock
  2789. .%402!     sty bootBlock+1
  2790. .%403!     ldy #13              ;get cluster size
  2791. .%404!     lda (bootBlock),y
  2792. .%405!     sta clusterBlockCount
  2793. .%406!     cmp #3
  2794. .%407!     bcc +
  2795. .%408!
  2796.  
  2797. If a disk parameter is found to exceed the limits of LRR, error code #60 is
  2798. returned.
  2799.  
  2800. .%409!     invalidParms = *
  2801. .%410!     lda #60
  2802. .%411!     sta errno
  2803. .%412!     sec
  2804. .%413!     rts
  2805. .%414!
  2806. .%415!  +  ldy #16              ;check FAT replication count, must be 2
  2807. .%416!     lda (bootBlock),y
  2808. .%417!     cmp #2
  2809. .%418!     bne invalidParms
  2810. .%419!     ldy #22              ;get FAT size in sectors
  2811. .%420!     lda (bootBlock),y
  2812. .%421!     sta fatBlocks
  2813. .%422!     cmp #4
  2814. .%423!     bcs invalidParms
  2815. .%424!     ldy #17              ;get directory size
  2816. .%425!     lda (bootBlock),y
  2817. .%426!     sta rootDirEntries
  2818. .%427!     cmp #129
  2819. .%428!     bcs invalidParms
  2820. .%429!     lsr
  2821. .%430!     lsr
  2822. .%431!     lsr
  2823. .%432!     lsr
  2824. .%433!     sta rootDirBlocks
  2825. .%434!     ldy #19              ;get total sector count
  2826. .%435!     lda (bootBlock),y
  2827. .%436!     sta totalSectors
  2828. .%437!     iny
  2829. .%438!     lda (bootBlock),y
  2830. .%439!     sta totalSectors+1
  2831. .%440!     ldy #24              ;check sectors per track, must be 9
  2832. .%441!     lda (bootBlock),y
  2833. .%442!     cmp #9
  2834. .%443!     bne invalidParms
  2835. .%444!     ldy #26
  2836. .%445!     lda (bootBlock),y
  2837. .%446!     cmp #2               ;check number of sides, must be 2
  2838. .%447!     bne invalidParms
  2839. .%448!     ldy #14              ;check number of boot sectors, must be 1
  2840. .%449!     lda (bootBlock),y
  2841. .%450!     cmp #1
  2842. .%451!     bne invalidParms
  2843. .%452!
  2844.  
  2845. Calculate the derived parameters.
  2846.  
  2847. .%453!     ;** get derived parameters
  2848. .%454!     lda fatBlocks        ;first root directory sector
  2849. .%455!     asl
  2850. .%456!     clc
  2851. .%457!     adc #1
  2852. .%458!     sta firstRootDirBlock
  2853. .%459!     clc                  ;first file sector
  2854. .%460!     adc rootDirBlocks
  2855. .%461!     sta firstFileBlock
  2856. .%462!     lda totalSectors     ;number of file clusters
  2857. .%463!     ldy totalSectors+1
  2858. .%464!     sec
  2859. .%465!     sbc firstFileBlock
  2860. .%466!     bcs +
  2861. .%467!     dey
  2862. .%468!  +  sta fileClusterCount
  2863. .%469!     sty fileClusterCount+1
  2864. .%470!     lda clusterBlockCount
  2865. .%471!     cmp #2
  2866. .%472!     bne +
  2867. .%473!     lsr fileClusterCount+1
  2868. .%474!     ror fileClusterCount
  2869. .%475!
  2870.  
  2871. Gee, I have more comments embedded in the code than I did last issue.
  2872.  
  2873. .%476!     ;** load FAT
  2874. .%477!  +  lda #<fatbuf
  2875. .%478!     ldy #>fatbuf
  2876. .%479!     sta 6
  2877. .%480!     sty 7
  2878. .%481!     lda #1
  2879. .%482!     ldy #0
  2880. .%483!     ldx fatBlocks
  2881. .%484!     jsr copyBlocks
  2882. .%485!     bcc +
  2883. .%486!     rts
  2884. .%487!
  2885. .%488!     ;** load actual directory
  2886. .%489!  +  lda #<dirbuf
  2887. .%490!     ldy #>dirbuf
  2888. .%491!     sta 6
  2889. .%492!     sty 7
  2890. .%493!     lda firstRootDirBlock
  2891. .%494!     ldy #0
  2892. .%495!     ldx rootDirBlocks
  2893. .%496!     jsr copyBlocks
  2894. .%497!     bcc +
  2895. .%498!     rts
  2896. .%499!  +  lda #<dirbuf
  2897. .%500!     ldy #>dirbuf
  2898. .%501!     ldx rootDirEntries
  2899. .%502!     clc
  2900. .%503!     rts
  2901. .%504!
  2902.  
  2903. This routine locates the given FAT table entry number and returns the value
  2904. stored in it.  Some work is needed to deal with the 12-bit compressed data
  2905. structure.
  2906.  
  2907. .%505!  entryAddr = 2
  2908. .%506!  entryWork = 4
  2909. .%507!  entryBits = 5
  2910. .%508!  entryData0 = 6
  2911. .%509!  entryData1 = 7
  2912. .%510!  entryData2 = 8
  2913. .%511!
  2914. .%512!  getFatEntry = *  ;( .AY=fatEntryNumber ) : .AY=fatEntryValue
  2915. .%513!     sta entryBits
  2916.  
  2917. Divide the FAT entry number by two and multiply by three because two FAT
  2918. entries are stored in three bytes.  Then add the FAT base address and we have
  2919. the address of the three bytes that contain the FAT entry we are interested
  2920. in.  I retrieve the three bytes into zero-page memory for easy manipulation.
  2921.  
  2922. .%514!     ;** divide by two
  2923. .%515!     sty entryAddr+1
  2924. .%516!     lsr entryAddr+1
  2925. .%517!     ror
  2926. .%518!
  2927. .%519!     ;** times three
  2928. .%520!     sta entryWork
  2929. .%521!     ldx entryAddr+1
  2930. .%522!     asl
  2931. .%523!     rol entryAddr+1
  2932. .%524!     clc
  2933. .%525!     adc entryWork
  2934. .%526!     sta entryAddr
  2935. .%527!     txa
  2936. .%528!     adc entryAddr+1
  2937. .%529!     sta entryAddr+1
  2938. .%530!
  2939. .%531!     ;** add base, get data
  2940. .%532!     clc
  2941. .%533!     lda entryAddr
  2942. .%534!     adc #<fatbuf
  2943. .%535!     sta entryAddr
  2944. .%536!     lda entryAddr+1
  2945. .%537!     adc #>fatbuf
  2946. .%538!     sta entryAddr+1
  2947. .%539!     ldy #2
  2948. .%540!  -  lda (entryAddr),y
  2949. .%541!     sta entryData0,y
  2950. .%542!     dey
  2951. .%543!     bpl -
  2952. .%544!     lda entryBits
  2953. .%545!     and #1
  2954. .%546!     bne +
  2955. .%547!
  2956.  
  2957. If the original given FAT entry number is even, then we want the first 12-bit
  2958. compressed field.  The nybbles are extracted according to the diagram shown
  2959. earlier.
  2960.  
  2961. .%548!     ;** case 1: first 12-bit cluster
  2962. .%549!     lda entryData1
  2963. .%550!     and #$0f
  2964. .%551!     tay
  2965. .%552!     lda entryData0
  2966. .%553!     rts
  2967. .%554!
  2968.  
  2969. Otherwise, we want the second 12-bit field.
  2970.  
  2971. .%555!     ;** case 2: second 12-bit cluster
  2972. .%556!  +  lda entryData1
  2973. .%557!     ldx #4
  2974. .%558!  -  lsr entryData2
  2975. .%559!     ror
  2976. .%560!     dex
  2977. .%561!     bne -
  2978. .%562!     ldy entryData2
  2979. .%563!     rts
  2980. .%564!
  2981.  
  2982. Finally, this is the file copying level.  It deals with reading the clusters
  2983. of MS-DOS files and copying the data they contain to the already-open CBM
  2984. Kernal file, possibly with ASCII-to-PETSCII translation.
  2985.  
  2986. .%565!  ;====file copy level====
  2987. .%566!
  2988. .%567!  transMode = 14
  2989. .%568!  lfn = 15
  2990. .%569!  cbmDataPtr = $60
  2991. .%570!  cbmDataLen = $62
  2992. .%571!  cluster = $64
  2993. .%572!
  2994.  
  2995. Copy the given cluster to the CBM output file.  This routine fetches the next
  2996. cluster of the file for the next time this routine is called, and if it hits
  2997. the NULL pointer of the last cluster of a file, it adjusts the number of valid
  2998. file data bytes the current cluster contains to FileLength % ClusterLength
  2999. (see note below).
  3000.  
  3001. .%573!  copyFileCluster = *  ;( cluster, lfn, transMode ) : .CS=err
  3002.  
  3003. Read the cluster and setup to copy the whole cluster to the CBM file.
  3004.  
  3005. .%574!     lda cluster
  3006. .%575!     ldy cluster+1
  3007. .%576!     jsr readCluster
  3008. .%577!     bcc +
  3009. .%578!     rts
  3010. .%579!  +  lda #<clusterBuf
  3011. .%580!     ldy #>clusterBuf
  3012. .%581!     sta cbmDataPtr
  3013. .%582!     sty cbmDataPtr+1
  3014. .%583!     lda #0
  3015. .%584!     sta cbmDataLen
  3016. .%585!     lda clusterBlockCount
  3017. .%586!     asl
  3018. .%587!     sta cbmDataLen+1
  3019. .%588!
  3020.  
  3021. Fetch the next cluster number of the file, and adjust the cluster data length
  3022. for the last cluster of the file.
  3023.  
  3024. .%589!     ;**get next cluster
  3025. .%590!     lda cluster
  3026. .%591!     ldy cluster+1
  3027. .%592!     jsr getFatEntry
  3028. .%593!     sta cluster
  3029. .%594!     sty cluster+1
  3030. .%595!     cmp #$ff
  3031. .%596!     bne copyFileClusterData
  3032. .%597!     cpy #$0f
  3033. .%598!     bne copyFileClusterData
  3034. .%599!     lda lenML
  3035. .%600!     sta cbmDataLen
  3036. .%601!     lda #$01
  3037. .%602!     ldx clusterBlockCount
  3038. .%603!     cpx #1
  3039. .%604!     beq +
  3040. .%605!     lda #$03
  3041. .%606!  +  and lenML+1
  3042.  
  3043. The following three lines were added in a last minute panic after realizing
  3044. that if FileLength % ClusterSize == 0, then the last cluster of the file
  3045. contains ClusterSize bytes, not zero bytes.
  3046.  
  3047. .%000!     bne +
  3048. .%000!     ldx lenML
  3049. .%000!     beq copyFileClusterData
  3050. .%607!  +  sta cbmDataLen+1
  3051. .%608!
  3052. .%609!     copyFileClusterData = *
  3053. .%610!     jsr commieOut
  3054. .%611!     rts
  3055. .%612!
  3056.  
  3057. Copy the file data in the MS-DOS cluster buffer to the CBM output file.
  3058.  
  3059. .%613!  cbmDataLimit = $66
  3060. .%614!
  3061. .%615!  commieOut = *  ;( cbmDataPtr, cbmDataLen ) : .CS=err
  3062.  
  3063. If the the logical file number to copy to is 0 ("null device"), then don't
  3064. bother copying anything.
  3065.  
  3066. .%616!     ldx lfn
  3067. .%617!     bne +
  3068. .%618!     clc
  3069. .%619!     rts
  3070.  
  3071. Otherwise, prepare the logical file number for output.
  3072.  
  3073. .%620!  +  jsr kernelChkout
  3074. .%621!     bcc commieOutMore
  3075. .%622!     sta errno
  3076. .%623!     rts
  3077. .%624!
  3078. .%625!     commieOutMore = *
  3079.  
  3080. Process the cluster data in chunks of up to 255 bytes or the number of data
  3081. bytes remaining in the cluster.
  3082.  
  3083. .%626!     lda #255
  3084. .%627!     ldx cbmDataLen+1
  3085. .%628!     bne +
  3086. .%629!     lda cbmDataLen
  3087. .%630!  +  sta cbmDataLimit
  3088. .%631!     ldy #0
  3089. .%632!  -  lda (cbmDataPtr),y
  3090. .%633!     bit transMode
  3091. .%634!     bpl +
  3092.  
  3093. If we have to translate the current ASCII character, look up the PETSCII value
  3094. in the translation table and output that value.  If the translation table
  3095. entry value is $00, then don't output a character (filter out invalid
  3096. character codes).
  3097.  
  3098. .%635!     tax
  3099. .%636!     lda transBuf,x
  3100. .%637!     beq commieNext
  3101. .%638!  +  jsr kernelChrout
  3102. .%639!     commieNext = *
  3103. .%640!     iny
  3104. .%641!     cpy cbmDataLimit
  3105. .%642!     bne -
  3106. .%643!
  3107.  
  3108. Increment the cluster buffer pointer and decrement the cluster buffer character
  3109. count according to the number of bytes just processed, and repeat the above if
  3110. more file data remains in the current cluster.
  3111.  
  3112. .%644!     clc
  3113. .%645!     lda cbmDataPtr
  3114. .%646!     adc cbmDataLimit
  3115. .%647!     sta cbmDataPtr
  3116. .%648!     bcc +
  3117. .%649!     inc cbmDataPtr+1
  3118. .%650!  +  sec
  3119. .%651!     lda cbmDataLen
  3120. .%652!     sbc cbmDataLimit
  3121. .%653!     sta cbmDataLen
  3122. .%654!     bcs +
  3123. .%655!     dec cbmDataLen+1
  3124. .%656!  +  lda cbmDataLen
  3125. .%657!     ora cbmDataLen+1
  3126. .%658!     bne commieOutMore
  3127.  
  3128. If we are finished with the cluster, then clear the CBM Kernal output channel.
  3129.  
  3130. .%659!     jsr kernelClrchn
  3131. .%660!     clc
  3132. .%661!     rts
  3133. .%662!
  3134.  
  3135. The file copying main routine.  Set up for the starting cluster, and call
  3136. the cluster copying routine until end-of-file is reached.  Checks for a
  3137. NULL cluster pointer in the directory entry to handle zero-length files.
  3138.  
  3139. .%663!  copyFile = *  ;( startCluster, lenML, .A=transMode, .X=lfn ) : .CS=err
  3140. .%664!     ldy #$0e
  3141. .%665!     sty $ff00
  3142. .%666!     sta transMode
  3143. .%667!     stx lfn
  3144. .%668!     lda startCluster
  3145. .%669!     ldy startCluster+1
  3146. .%670!     sta cluster
  3147. .%671!     sty cluster+1
  3148. .%672!     jmp +
  3149. .%673!  -  jsr copyFileCluster
  3150. .%674!     bcc +
  3151. .%675!     rts
  3152. .%676!  +  lda cluster
  3153. .%677!     cmp #$ff
  3154. .%678!     bne -
  3155. .%679!     lda cluster+1
  3156. .%680!     cmp #$0f
  3157. .%681!     bne -
  3158. .%682!     clc
  3159. .%683!     rts
  3160. .%684!
  3161.  
  3162. This is the translation table used to convert from ASCII to PETSCII.  You can
  3163. modify it to suit your needs if you wish.  If you cannot reassemble this file,
  3164. then you can sift through the binary file and locate the tabel and change it
  3165. there.  An entry of $00 means the corresponding ASCII character will not be
  3166. translated.  You'll notice that I have set up translations for the following
  3167. ASCII control characters into PETSCII: Backspace, Tab, Linefeed (CR), and
  3168. Formfeed.  I also translate the non-PETSCII characters such as {, |, ~, and _
  3169. according to what they probably would have been if Commodore wasn't so
  3170. concerned with the graphics characters.
  3171.  
  3172. .%685!  transBuf = *
  3173. .%686!         ;0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
  3174. .%687!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$14,$09,$0d,$00,$93,$00,$00,$00 ;0
  3175. .%688!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;1
  3176. .%689!  .byte $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2a,$2b,$2c,$2d,$2e,$2f ;2
  3177. .%690!  .byte $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3a,$3b,$3c,$3d,$3e,$3f ;3
  3178. .%691!  .byte $40,$c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9,$ca,$cb,$cc,$cd,$ce,$cf ;4
  3179. .%692!  .byte $d0,$d1,$d2,$d3,$d4,$d5,$d6,$d7,$d8,$d9,$da,$5b,$5c,$5d,$5e,$5f ;5
  3180. .%693!  .byte $c0,$41,$42,$43,$44,$45,$46,$47,$48,$49,$4a,$4b,$4c,$4d,$4e,$4f ;6
  3181. .%694!  .byte $50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5a,$db,$dc,$dd,$de,$df ;7
  3182. .%695!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;8
  3183. .%696!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;9
  3184. .%697!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;a
  3185. .%698!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;b
  3186. .%699!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;c
  3187. .%700!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;d
  3188. .%701!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;e
  3189. .%702!  .byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;f
  3190. .%703!
  3191.  
  3192. This is where the track cache, etc. are stored.  This section requires 11K of
  3193. storage space but does not increase the length of the binary program file
  3194. since these storage areas are DEFINED rather than allocated with ".buf"
  3195. directives.  The Unix terminology for this type of uninitialized data is "bss".
  3196.  
  3197. .%704!  ;====bss storage====
  3198. .%705!
  3199. .%706!  bss = *
  3200. .%707!  trackbuf   = bss
  3201. .%708!  clusterBuf = trackbuf+4608
  3202. .%709!  fatbuf     = clusterBuf+1024
  3203. .%710!  dirbuf     = fatbuf+1536
  3204. .%711!  end        = dirbuf+4096
  3205.  
  3206. 5. USER-INTERFACE PROGRAM
  3207.  
  3208. This section presents the listing of the user-interface BASIC program.  You
  3209. should be aware that you can easily change some of the defaults to your own
  3210. preferences if you wish.  This program is not listed in the ".%nnn!" format
  3211. that the assembler listing is since you can recover this listing from the
  3212. uuencoded binary program file.  This program should be a little easier to
  3213. follow than the assembler listing since BASIC is a self-commenting language. :-)
  3214.  
  3215. 10 rem little red reader, by craig bruce, 30-sep-92, for c= hacking netmag
  3216. 11 :
  3217.  
  3218. These lines set up the default CBM-DOS and MS-DOS device numbers, taking care
  3219. to disallow them to be the same device.  You can change this to your own drive
  3220. configuration.
  3221.  
  3222. 20 cd=peek(186)  : rem ** default cbm-dos drive **
  3223. 25 dv=9:dt=0  :  rem ** ms-dos drive, type (0=1571,255=1581)
  3224. 26 if dv=cd then dv=8:dt=0 : rem ** alternate ms-dos drive
  3225. 27 :
  3226. 30 print chr$(147);"initializing..." : print
  3227. 40 bank0 : pk=dec("8000")
  3228. 50 if peek(pk+9)=dec("cb") and peek(pk+10)=131 then 60
  3229. 55 print"loading machine language routines..." : bload"lrr.bin",u(cd)
  3230. 60 poke pk+16,dv : poke pk+17,dt : sys pk
  3231.  
  3232. I "dim" the following variables before the arrays to avoid the overhead of
  3233. pushing the arrays around when creating new scalar variables.
  3234.  
  3235. 70 dim t,r,b,i,a$,c,dt$,fl$,il$,x,x$
  3236. 80 dim di$(128),cl(128),sz(128)
  3237. 90 if dt=255 then dt$="1581" :else dt$="1571"
  3238. 100 fl$=chr$(19)+chr$(17)+chr$(17)+chr$(17)+chr$(17)
  3239. 110 il$=fl$:fori=1to19:il$=il$+chr$(17):next
  3240. 120 goto 500
  3241. 130 :
  3242. 131 rem ** load ms-dos directory **
  3243. 140 print"loading directory..." : print
  3244. 150 sys pk : sys pk+3
  3245. 160 dl=0
  3246.  
  3247. The "rreg" instruction returns the return values of the .A, .X, .Y, and .S
  3248. registers from the last "sys" call.  I check the 1-bit of the .S register
  3249. (the Carry flag) for error returns.
  3250.  
  3251. 170 rreg bl,dc,bh,s : e=peek(pk+15)
  3252. 180 if (s and 1) then gosub 380 : return
  3253. 190 print"scanning directory..." : print
  3254. 200 db=bl+256*bh
  3255. 210 if dc=0 then 360
  3256. 220 for dp=db to db+32*(dc-1) step 32
  3257. 230 if peek(dp)=0 or peek(dp)=229 then 350
  3258. 240 if peek(dp+12) and 24 then 350
  3259. 250 dl=dl+1
  3260.  
  3261. This next line is where I set the default selection status, translation type,
  3262. and CBM file type for the MS-DOS files.  You can change these defaults simply
  3263. by overtyping the string in   (  |  |||  |||  ) the "V" locations.
  3264.                                  V  VVV  VVV
  3265. 260 d$=right$(" "+str$(dl),3)+"     asc  seq  " : rem ** default sel/tr/ft **
  3266. 270 a$="" : fori=0to10 : a$=a$+chr$(peek(dp+i)) : next
  3267. 280 a$=left$(a$,8)+"  "+right$(a$,3)
  3268. 290 print dl; a$
  3269. 300 d$=d$+a$+"  "
  3270. 310 cl(dl)=peek(dp+26)+256*peek(dp+27)
  3271. 320 sz=peek(dp+28)+256*peek(dp+29)+65536*peek(dp+30)
  3272. 330 di$(dl)=d$+right$("    "+str$(sz),6)
  3273. 340 sz(dl)=sz
  3274. 350 next dp
  3275. 360 return
  3276. 370 :
  3277. 371 rem ** report ms-dos disk error **
  3278. 380 print chr$(18);"ms-dos disk error #";mid$(str$(e),2);
  3279. 390 print " ($";mid$(hex$(e),3);"), press key.";chr$(146)
  3280. 400 getkey a$ : return
  3281. 410 :
  3282. 411 rem ** screen heading **
  3283. 420 printchr$(147);"ms-dev=";mid$(str$(dv),2);"    ms-type=";dt$;
  3284. 430 print"    cbm-dev=";mid$(str$(cd),2):print
  3285. 440 return
  3286. 450 :
  3287. 451 rem ** screen footing **
  3288. 460 print il$;"d=directory  m=ms-dev  f=cbm-dev q=quit"
  3289. 470 print"t=toggle-column, c=copy-files, +/- page";
  3290. 480 return
  3291. 490 :
  3292. 491 rem ** main routine **
  3293. 500 t=1 : c=0
  3294. 510 r=0
  3295. 520 gosub 420
  3296. 530 print "num  s  trn  typ  filename  ext  length"
  3297. 540 print "---  -  ---  ---  --------  ---  ------"
  3298. 550 gosub 460
  3299. 560 b=t+17 : if b>dl then b=dl
  3300. 570 print fl$;: if t>dl then 590
  3301. 580 for i=t to b : print di$(i) : next
  3302. 590 if dl=0 then print chr$(18);"<no files>";chr$(146)
  3303. 600 if dl=0 then 660
  3304. 610 print left$(il$,r+5);chr$(18);
  3305. 620 on c+1 goto 630,640,650
  3306. 630 print spc(4);mid$(di$(t+r),5,3) : goto 660
  3307. 640 print spc(7);mid$(di$(t+r),8,5) : goto 660
  3308. 650 print spc(12);mid$(di$(t+r),13,5) : goto 660
  3309. 660 getkey a$
  3310.  
  3311. Oh shi^Hoot.  I screwed up the following line in the string after the
  3312. "+chr$(13)+" part.  You'll notice that I have avoided putting cursor control
  3313. characters into the strings everywhere else, but I forgot to do that here.
  3314. The "{stuff}" should be CursorUp, CursorDown, CursorLeft, CursorRight,
  3315. CursorHome, and CursorCLR control characters, respectively.  These characters
  3316. give the index for the "on" statement below.
  3317.  
  3318. 670 i=instr("dmftc+-q "+chr$(13)+"{stuff}",a$)
  3319. 680 print left$(il$,r+5);di$(t+r)
  3320. 690 if i=0 then 600
  3321. 700 onigoto760,1050,1110,950,1150,1000,1020,730,860,860,770,790,810,830,850,500
  3322. 710 stop
  3323. 720 :
  3324. 721 rem ** various menu options **
  3325. 730 print chr$(147);"have an awesome day."
  3326. 740 end
  3327. 760 gosub 420 : gosub 140 : goto 500
  3328. 770 r=r-1 : if r<0 then r=b-t
  3329. 780 goto 600
  3330. 790 r=r+1 : if t+r>b then r=0
  3331. 800 goto 600
  3332. 810 c=c-1 : if c<0 then c=2
  3333. 820 goto 600
  3334. 830 c=c+1 : if c>2 then c=0
  3335. 840 goto 600
  3336. 850 r=0 : c=0 : goto 600
  3337. 860 if dl=0 then 600
  3338. 870 x=t+r : on c+1 gosub 890,910,930
  3339. 880 print left$(il$,r+5);di$(x) : goto 600
  3340. 890 if mid$(di$(x),6,1)=" " then x$="*" :else x$=" "
  3341. 900 mid$(di$(x),6,1)=x$ : return
  3342. 910 if mid$(di$(x),9,1)="a" then x$="bin" :else x$="asc"
  3343. 920 mid$(di$(x),9,3)=x$ : return
  3344. 930 if mid$(di$(x),14,1)="s" then x$="prg" :else x$="seq"
  3345. 940 mid$(di$(x),14,3)=x$ : return
  3346. 950 if dl=0 then 600
  3347. 960 for x=1 to dl
  3348. 970 on c+1 gosub 890,910,930
  3349. 980 next x
  3350. 990 goto 520
  3351. 1000 if b=dl then t=1 : goto 510
  3352. 1010 t=t+18 : goto 510
  3353. 1020 if t=1 then t=dl-(dl-int(dl/18)*18)+1 : goto 510
  3354. 1030 t=t-18 : if t<1 then t=1
  3355. 1040 goto 510
  3356. 1050 print il$;chr$(27);"@";
  3357. 1060 input"ms-dos device number (8-30)";dv
  3358. 1061 if cd=dv then print"ms-dos and cbm-dos devices must be different!":goto1060
  3359. 1070 input"ms-dos device type  (71/81)";x
  3360. 1080 if x=8 or x=81 or x=1581 then dt=255:dt$="1581" :else dt=0:dt$="1571"
  3361. 1090 poke pk+16,dv : poke pk+17,dt : sys pk
  3362. 1100 goto 520
  3363. 1110 print il$;chr$(27);"@";
  3364. 1120 input "cbm-dos device number (0-30)";cd
  3365. 1130 if cd=dv then print"ms-dos and cbm-dos devices must be different!":goto1120
  3366. 1140 goto 520
  3367. 1141 :
  3368. 1142 rem ** copy files **
  3369. 1150 print chr$(147);"copy files":print:print
  3370. 1160 if dl=0 then fc=0 : goto 1190
  3371. 1170 fc=0 : for f=1 to dl : if mid$(di$(f),6,1)="*" then gosub 1200
  3372. 1180 next f
  3373. 1190 print : print"files copied =";fc;" - press key"
  3374. 1191 getkey a$ : goto 520
  3375. 1200 fc=fc+1
  3376. 1210 x$=mid$(di$(f),19,8)+"."+mid$(di$(f),29,3)
  3377. 1220 cf$="":fori=1tolen(x$):if mid$(x$,i,1)<>" " then cf$=cf$+mid$(x$,i,1)
  3378. 1230 next
  3379. 1231 if right$(cf$,1)="." then cf$=left$(cf$,len(cf$)-1)
  3380. 1232 cf$=cf$+","+mid$(di$(f),14,1)
  3381. 1240 print str$(fc);". ";chr$(34);cf$;chr$(34);tab(20);sz(f)"bytes";
  3382. 1245 print tab(35);mid$(di$(f),9,3)
  3383. 1250 cl=cl(f) : lb=sz(f) - int(sz(f)/65536)*65536
  3384.  
  3385. I had to use a DOPEN statement here for disk files because the regular OPEN
  3386. statment does not redirect the DS and DS$ pseudo-variables.  You'll notice
  3387. that the non-disk OPEN statment below has a secondary address of 7.  This is
  3388. to put the printer into lowercase mode if you are outputting directly to it.
  3389. You can replace this with a 5 (or whatever) if you have a special interface
  3390. to an IBM-compatible printer and you want to print directly in ASCII.  In this
  3391. case, you would select the "BIN" translation mode for the file you are routing
  3392. directly to the printer.
  3393.  
  3394. 1260 if cd>=8 then dopen#1,(cf$+",w"),u(cd) :else if cd<>0 then open 1,cd,7
  3395. 1265 if cd<8 then 1288
  3396. 1270 if ds<>63 then 1288
  3397. 1275 x$="y" : print "file exists; overwrite (y/n)";
  3398. 1280 close 1 : input x$ : if x$="n" then fc=fc-1 : return
  3399. 1285 scratch(cf$),u(cd)
  3400. 1286 dopen#1,(cf$+",w"),u(cd)
  3401. 1288 if cd<8 then 1320
  3402. 1300 if ds<20 then 1320
  3403. 1310 print chr$(18)+"cbm disk error: "+ds$ : fc=fc-1 : close1 : return
  3404. 1320 poke pk+19,cl/256 : poke pk+18,cl-peek(pk+19)*256
  3405. 1330 poke pk+21,lb/256 : poke pk+20,lb-peek(pk+21)*256
  3406. 1340 tr=0 : if mid$(di$(f),9,1)="a" then tr=255
  3407. 1346 x=1 : if cd=0 then x=0
  3408. 1350 sys pk+6,tr,x
  3409. 1355 rreg x,x,x,s : e=peek(pk+15)
  3410. 1356 if (s and 1) then gosub 380 : fc=fc-1
  3411. 1360 if cd<>0 and cd<8 then close1
  3412. 1370 if cd>=8 then dclose#1 : if ds>=20 then 1310
  3413. 1380 return
  3414.  
  3415. 6. UUENCODED FILES
  3416.  
  3417. Here are the binary executables in uuencoded form.  The CRC32s of the two
  3418. files are as follows:
  3419.  
  3420.    "lrr.128"    1106058594
  3421.    "lrr.bin"     460671650
  3422.  
  3423. The "lrr.128" file is the main BASIC program and the "lrr.bin" file contains
  3424. the machine lanugage disk-accessing routines.
  3425.  
  3426. begin 640 lrr.128
  3427. M`1Q+'`H`CR!,25143$4@4D5$(%)%041%4BP@0ED@0U)!24<@0E)50T4L(#,P
  3428. M+5-%4"TY,BP@1D]2($,]($A!0TM)3D<@3D5434%'`%$<"P`Z`(`<%`!#1++"
  3429. M*#$X-BD@(#H@CR`J*B!$149!54Q4($-"32U$3U,@1%))5D4@*BH`O!P9`$16
  3430. MLCDZ1%2R,"`@.B`@CR`J*B!-4RU$3U,@1%))5D4L(%194$4@*#`],34W,2PR
  3431. M-34],34X,2D`\AP:`(L@1%:R0T0@IR!$5K(X.D14LC`@.B"/("HJ($%,5$52
  3432. M3D%412!-4RU$3U,@1%))5D4`^!P;`#H`&QT>`)D@QR@Q-#<I.R))3DE424%,
  3433. M25I)3D<N+BXB(#H@F0`R'2@`_@(P(#H@4$NRT2@B.#`P,"(I`%P=,@"+(,(H
  3434. M4$NJ.2FRT2@B0T(B*2"O(,(H4$NJ,3`ILC$S,2"G(#8P`)P=-P"9(DQ/041)
  3435. M3D<@34%#2$E.12!,04Y'54%'12!23U5424Y%4RXN+B(@.B#^$2),4E(N0DE.
  3436. M(BQ5*$-$*0"_'3P`ER!02ZHQ-BQ$5B`Z()<@4$NJ,3<L1%0@.B">(%!+`.,=
  3437. M1@"&(%0L4BQ"+$DL020L0RQ$5"0L1DPD+$E,)"Q8+%@D``(>4`"&($1))"@Q
  3438. M,C@I+$-,*#$R."DL4UHH,3(X*0`J'EH`BR!$5+(R-34@IR!$5"2R(C$U.#$B
  3439. M(#K5($14)+(B,34W,2(`4!YD`$9,)++'*#$Y*:K'*#$W*:K'*#$W*:K'*#$W
  3440. M*:K'*#$W*0!T'FX`24PDLD9,)#J!2;(QI#$Y.DE,)+))3"2JQR@Q-RDZ@@!^
  3441. M'G@`B2`U,#``A!Z"`#H`IAZ#`(\@*BH@3$]!1"!-4RU$3U,@1$E214-43U)9
  3442. M("HJ`,8>C`"9(DQ/041)3D<@1$E214-43U)9+BXN(B`Z()D`V!Z6`)X@4$L@
  3443. M.B">(%!+JC,`X1Z@`$1,LC```!^J`/X)($),+$1#+$)(+%,@.B!%LL(H4$NJ
  3444. M,34I`!H?M`"+("A3(*\@,2D@IR"-(#,X,"`Z((X`.Q^^`)DB4T-!3DY)3D<@
  3445. M1$E214-43U)9+BXN(B`Z()D`3!_(`$1"LD),JC(U-JQ"2`!='](`BR!$0[(P
  3446. M(*<@,S8P`'T?W`"!($10LD1"(*0@1$*J,S*L*$1#JS$I(*D@,S(`G1_F`(L@
  3447. MPBA$4"FR,""P(,(H1%`ILC(R.2"G(#,U,`"W'_``BR#"*$10JC$R*2"O(#(T
  3448. M(*<@,S4P`,,?^@!$3+)$3*HQ``<@!`%$)++)*"(@(JK$*$1,*2PS*:HB("`@
  3449. M("!!4T,@(%-%42`@(B`Z((\@*BH@1$5&055,5"!314PO5%(O1E0@*BH`,B`.
  3450. M`4$DLB(B(#H@@4FR,*0Q,"`Z($$DLD$DJL<HPBA$4*I)*2D@.B""`$X@&`%!
  3451. M)++(*$$D+#@IJB(@("*JR2A!)"PS*0!;("(!F2!$3#L@020`;2`L`40DLD0D
  3452. MJD$DJB(@("(`CB`V`4-,*$1,*;+"*$10JC(V*:HR-3:LPBA$4*HR-RD`NB!`
  3453. M`5-:LL(H1%"J,C@IJC(U-JS"*$10JC(Y*:HV-34S-JS"*$10JC,P*0#;($H!
  3454. M1$DD*$1,*;)$)*K)*"(@("`@(JK$*%-:*2PV*0#I(%0!4UHH1$PILE-:`/(@
  3455. M7@&"($10`/@@:`&.`/X@<@$Z`",A<P&/("HJ(%)%4$]25"!-4RU$3U,@1$E3
  3456. M2R!%4E)/4B`J*@!0(7P!F2#'*#$X*3LB35,M1$]3($1)4TL@15)23U(@(R([
  3457. MRBC$*$4I+#(I.P!](88!F2`B("@D(CO**-(H12DL,RD[(BDL(%!215-3($M%
  3458. M62XB.\<H,30V*0"+(9`!H?D@020@.B".`)$AF@$Z`*PAFP&/("HJ(%-#4D5%
  3459. M3B!(14%$24Y'("HJ`.$AI`&9QR@Q-#<I.R)-4RU$158](CO**,0H1%8I+#(I
  3460. M.R(@("`@35,M5%E013TB.T14)#L``B*N`9DB("`@($-"32U$158](CO**,0H
  3461. M0T0I+#(I.ID`""*X`8X`#B+"`3H`*2+#`8\@*BH@4T-2145.($9/3U1)3D<@
  3462. M*BH`72+,`9D@24PD.R)$/41)4D5#5$]262`@33U-4RU$158@($8]0T)-+41%
  3463. M5B!1/5%5250B`(TBU@&9(E0]5$]'1TQ%+4-/3%5-3BP@0SU#3U!9+49)3$53
  3464. M+"`K+RT@4$%'12([`),BX`&.`)DBZ@$Z`+(BZP&/("HJ($U!24X@4D]55$E.
  3465. M12`J*@#`(O0!5+(Q(#H@0[(P`,@B_@%2LC``TB((`HT@-#(P``(C$@*9(").
  3466. M54T@(%,@(%123B`@5%E0("!&24Q%3D%-12`@15A4("!,14Y'5$@B`#(C'`*9
  3467. M("(M+2T@("T@("TM+2`@+2TM("`M+2TM+2TM+2`@+2TM("`M+2TM+2TB`#PC
  3468. M)@*-(#0V,`!7(S`"0K)4JC$W(#H@BR!"L41,(*<@0K)$3`!P(SH"F2!&3"0[
  3469. M.B"+(%2Q1$P@IR`U.3``C2-$`H$@2;)4(*0@0B`Z()D@1$DD*$DI(#H@@@"V
  3470. M(TX"BR!$3+(P(*<@F2#'*#$X*3LB/$Y/($9)3$53/B([QR@Q-#8I`,<C6`*+
  3471. M($1,LC`@IR`V-C``WR-B`ID@R"A)3"0L4JHU*3O'*#$X*3L`]R-L`I$@0ZHQ
  3472. M((D@-C,P+#8T,"PV-3``&21V`ID@IC0I.\HH1$DD*%2J4BDL-2PS*2`Z((D@
  3473. M-C8P`#LD@`*9(*8W*3O**$1))"A4JE(I+#@L-2D@.B")(#8V,`!?)(H"F2"F
  3474. M,3(I.\HH1$DD*%2J4BDL,3,L-2D@.B")(#8V,`!I))0"H?D@020`D"2>`DFR
  3475. MU"@B1$U&5$,K+5$@(JK'*#$S*:HBD1&='1.3(BQ!)"D`JB2H`ID@R"A)3"0L
  3476. M4JHU*3M$220H5*I2*0"Z)+("BR!)LC`@IR`V,#``!B6\`I%)B3<V,"PQ,#4P
  3477. M+#$Q,3`L.34P+#$Q-3`L,3`P,"PQ,#(P+#<S,"PX-C`L.#8P+#<W,"PW.3`L
  3478. M.#$P+#@S,"PX-3`L-3`P``PEQ@*0`!(ET`(Z`#,ET0*/("HJ(%9!4DE/55,@
  3479. M345.52!/4%1)3TY3("HJ`%<EV@*9(,<H,30W*3LB2$%612!!3B!!5T533TU%
  3480. M($1!62XB`%TEY`*``'<E^`*-(#0R,"`Z((T@,30P(#H@B2`U,#``D24"`U*R
  3481. M4JLQ(#H@BR!2LS`@IR!2LD*K5`";)0P#B2`V,#``M246`U*R4JHQ(#H@BR!4
  3482. MJE*Q0B"G(%*R,`"_)2`#B2`V,#``UR4J`T.R0ZLQ(#H@BR!#LS`@IR!#LC(`
  3483. MX24T`XD@-C`P`/DE/@-#LD.J,2`Z((L@0[$R(*<@0[(P``,F2`.)(#8P,``9
  3484. M)E(#4K(P(#H@0[(P(#H@B2`V,#``*B9<`XL@1$RR,""G(#8P,`!*)F8#6+)4
  3485. MJE(@.B"1($.J,2"-(#@Y,"PY,3`L.3,P`&HF<`.9(,@H24PD+%*J-2D[1$DD
  3486. M*%@I(#H@B2`V,#``E29Z`XL@RBA$220H6"DL-BPQ*;(B("(@IR!8)+(B*B(@
  3487. M.M4@6"2R(B`B`*XFA`/**$1))"A8*2PV+#$ILE@D(#H@C@#=)HX#BR#**$1)
  3488. M)"A8*2PY+#$ILB)!(B"G(%@DLB)"24XB(#K5(%@DLB)!4T,B`/8FF`/**$1)
  3489. M)"A8*2PY+#,ILE@D(#H@C@`F)Z(#BR#**$1))"A8*2PQ-"PQ*;(B4R(@IR!8
  3490. M)+(B4%)'(B`ZU2!8)+(B4T51(@!`)ZP#RBA$220H6"DL,30L,RFR6"0@.B".
  3491. M`%$GM@.+($1,LC`@IR`V,#``8"?``X$@6+(Q(*0@1$P`>"?*`Y$@0ZHQ((T@
  3492. M.#DP+#DQ,"PY,S``@"?4`X(@6`"*)]X#B2`U,C``HR?H`XL@0K)$3""G(%2R
  3493. M,2`Z((D@-3$P`+8G\@-4LE2J,3@@.B")(#4Q,`#B)_P#BR!4LC$@IR!4LD1,
  3494. MJRA$3*NU*$1,K3$X*:PQ."FJ,2`Z((D@-3$P`/LG!@14LE2K,3@@.B"+(%2S
  3495. M,2"G(%2R,0`%*!`$B2`U,3``&B@:!)D@24PD.\<H,C<I.R)`(CL`0"@D!(4B
  3496. M35,M1$]3($1%5DE#12!.54U"15(@*#@M,S`I(CM$5@"%*"4$BR!#1+)$5B"G
  3497. M()DB35,M1$]3($%.1"!#0DTM1$]3($1%5DE#15,@35535"!"12!$249&15)%
  3498. M3E0A(CJ),3`V,`"J*"X$A2)-4RU$3U,@1$5624-%(%194$4@("@W,2\X,2DB
  3499. M.U@`ZR@X!(L@6+(X(+`@6+(X,2"P(%BR,34X,2"G($14LC(U-3I$5"2R(C$U
  3500. M.#$B(#K5($14LC`Z1%0DLB(Q-3<Q(@`.*4($ER!02ZHQ-BQ$5B`Z()<@4$NJ
  3501. M,3<L1%0@.B">(%!+`!@I3`2)(#4R,``M*58$F2!)3"0[QR@R-RD[(D`B.P!5
  3502. M*6`$A2`B0T)-+41/4R!$159)0T4@3E5-0D52("@P+3,P*2([0T0`FBEJ!(L@
  3503. M0T2R1%8@IR"9(DU3+41/4R!!3D0@0T)-+41/4R!$159)0T53($U54U0@0D4@
  3504. M1$E&1D5214Y4(2(ZB3$Q,C``I"ET!(D@-3(P`*HI=00Z`,$I=@2/("HJ($-/
  3505. M4%D@1DE,15,@*BH`WRE^!)D@QR@Q-#<I.R)#3U!9($9)3$53(CJ9.ID`^BF(
  3506. M!(L@1$RR,""G($9#LC`@.B")(#$Q.3``+RJ2!$9#LC`@.B"!($:R,2"D($1,
  3507. M(#H@BR#**$1))"A&*2PV+#$ILB(J(B"G((T@,3(P,``W*IP$@B!&`&,JI@29
  3508. M(#H@F2)&24Q%4R!#3U!)140@/2([1D,[(B`M(%!215-3($M%62(`=2JG!*'Y
  3509. M($$D(#H@B2`U,C``@2JP!$9#LD9#JC$`JBJZ!%@DLLHH1$DD*$8I+#$Y+#@I
  3510. MJB(N(JK**$1))"A&*2PR.2PS*0#E*L0$0T8DLB(B.H%)LC&DPRA8)"DZBR#*
  3511. M*%@D+$DL,2FSL2(@(B"G($-&)+)#1B2JRBA8)"Q)+#$I`.LJS@2"`!0KSP2+
  3512. M(,DH0T8D+#$ILB(N(B"G($-&)++(*$-&)"S#*$-&)"FK,2D`,RO0!$-&)+)#
  3513. M1B2J(BPBJLHH1$DD*$8I+#$T+#$I`&<KV`29(,0H1D,I.R(N("([QR@S-"D[
  3514. M0T8D.\<H,S0I.Z,R,"D[4UHH1BDB0EE415,B.P"`*]T$F2"C,S4I.\HH1$DD
  3515. M*$8I+#DL,RD`KROB!$-,LD-,*$8I(#H@3$*R4UHH1BD@JR"U*%-:*$8IK38U
  3516. M-3,V*:PV-34S-@#I*^P$BR!#1+&R.""G(/X-(S$L*$-&)*HB+%<B*2Q5*$-$
  3517. M*2`ZU2"+($-$L[$P(*<@GR`Q+$-$+#<`^ROQ!(L@0T2S.""G(#$R.#@`#RSV
  3518. M!(L@1%.SL38S(*<@,3(X.``^+/L$6"2R(EDB(#H@F2`B1DE,12!%6$E35%,[
  3519. M($]615)74DE412`H62].*2([`&8L``6@(#$@.B"%(%@D(#H@BR!8)+(B3B(@
  3520. MIR!&0[)&0ZLQ(#H@C@!W+`4%\BA#1B0I+%4H0T0I`)$L!@7^#2,Q+"A#1B2J
  3521. M(BQ7(BDL52A#1"D`HRP(!8L@0T2S.""G(#$S,C``MBP4!8L@1%.S,C`@IR`Q
  3522. M,S(P`.PL'@69(,<H,3@IJB)#0DT@1$E32R!%4E)/4CH@(JI$4R0@.B!&0[)&
  3523. M0ZLQ(#H@H#$@.B".`!DM*`67(%!+JC$Y+$-,K3(U-B`Z()<@4$NJ,3@L0TRK
  3524. MPBA02ZHQ.2FL,C4V`$8M,@67(%!+JC(Q+$Q"K3(U-B`Z()<@4$NJ,C`L3$*K
  3525. MPBA02ZHR,2FL,C4V`&XM/`544K(P(#H@BR#**$1))"A&*2PY+#$ILB)!(B"G
  3526. M(%12LC(U-0"%+4(%6+(Q(#H@BR!#1+(P(*<@6+(P`)4M1@6>(%!+JC8L5%(L
  3527. M6`"Q+4L%_@D@6"Q8+%@L4R`Z($6RPBA02ZHQ-2D`T2U,!8L@*%,@KR`Q*2"G
  3528. M((T@,S@P(#H@1D.R1D.K,0#I+5`%BR!#1+.Q,""O($-$LS@@IR"@,0`.+EH%
  3529. IBR!#1+&R.""G(/X/(S$@.B"+($13L;(R,""G(#$S,3``%"YD!8X`````
  3530. `
  3531. end
  3532.  
  3533. begin 640 lrr.bin
  3534. M`(!,68%,3H),#H3+@P``````````````````````````````````````2*D`
  3535. MA="M$(`@L?^I;R"3_ZE5(*C_)-`P#ZDP(*C_:""H_R30,`(88*D%C0^`.&"M
  3536. M`-U)$(T`W6"I""P-W/#[8"!@@*X,W"!7@(I@J1H@*("0`6`@KO\DT##.&"!'
  3537. M_RP-W"!7@"!H@(T/@"D/R0*P):``(&B`F2*`R,`&D/48J0@@*("0`6"I`2P1
  3538. M@#`"J00@J/\@KO]@2(HI`0H*"@HL$8`0`DD0("B`D`%@:""H_ZD!(*C_J0D@
  3539. MJ/\@KO]X&"!'_RP-W"!7@*DXH(6%`H0#J0"%!"P1@#`#(!.!("N!L`GF!*4$
  3540. MR0F0ZQA88*DXA0*F!!BIA7TB@84#8``($`8.!`P""B!H@(T/@"D/R0*0`6"B
  3541. M`J``J0@L#=SP^ZT`W4D0C0#=K0S<D0+(T.GF`\K0Y&"I#HT`_ZG_C2"`C2&`
  3542. M&&#-((#0$.PA@-`+B)@*&&F%J*DX&&"-((".(8"$!2"^@)`9K0^`*0_)"_`"
  3543. M.&`@<X"M(("N(8"D!9#;8*T@@*XA@*0%3&B!HO_H..D2L/J($/<8:1+(J(I@
  3544. M(*Z!H@#`"9`(2)CI":AHH@'(8(8+(,"!A0B&"80*I0BF":0*(&B!D`%@A0R$
  3545. M#:("H`"Q#)$&R+$,D0;(T/3F#>8'RM#MY@JE"LD*D!*I`84*Y@FE"<D"D`:I
  3546. M`(4)Y@C&"]"[&&`XZ0*P`8BN%H#@`?`'"H0')@>D!QAM'("0`<BB.(8&HI>&
  3547. M!ZX6@$S3@:D.C0#_J0"@`"#`@2!H@9`!8(4"A`.@#;$"C1:`R0.0!ZD\C0^`
  3548. M.&"@$+$"R0+0\:`6L0*-%X#)!+#FH!&Q`HT9@,F!L-M*2DI*C1B`H!.Q`HT:
  3549. M@,BQ`HT;@*`8L0+)"="_H!JQ`LD"T+>@#K$"R0'0KZT7@`H8:0&-'8`8;1B`
  3550. MC1R`K1J`K!N`..T<@+`!B(T>@(P?@*T6@,D"T`9.'X!N'H"I.*";A0:$!ZD!
  3551. MH`"N%X`@TX&0`6"I.*"AA0:$!ZT=@*``KAB`(-.!D`%@J3B@H:X9@!A@A06$
  3552. M`T8#:H4$I@,*)@,8902%`HIE`X4#&*4":3B%`J4#:9N%`Z`"L0*9!@"($/BE
  3553. M!2D!T`BE!RD/J*4&8*4'H@1&"&K*T/JD"&"E9*1E("6"D`%@J3B@EX5@A&&I
  3554. M`(5BK1:`"H5CI62D92`E@X5DA&7)_]`@P`_0'*T4@(5BJ0&N%H#@`?`"J0,M
  3555. M%8#0!:X4@/`"A6,@OH-@I@_0`AA@(,G_D`2-#X!@J?^F8]`"I6*%9J``L6`D
  3556. M#A`&JKTXA/`#(-+_R,1FT.P8I6!E9H5@D`+F83BE8N5FA6*P`L9CI6(%8]#$
  3557. M(,S_&&"@#HP`_X4.A@^M$H"L$X"%9(1E3"J$(&^#D`%@I63)_]#TI67)#]#N
  3558. M&&```````````!0)#0"3`````````````````````````"`A(B,D)28G*"DJ
  3559. M*RPM+B\P,3(S-#4V-S@Y.CL\/3X_0,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7
  3560. MV-G:6UQ=7E_`04)#1$5&1TA)2DM,34Y/4%%24U155E=865K;W-W>WP``````
  3561. M````````````````````````````````````````````````````````````
  3562. M````````````````````````````````````````````````````````````
  3563. A````````````````````````````````````````````````
  3564. `
  3565. end
  3566.  
  3567. 7. BIBLIOGRAPHY
  3568.  
  3569. The following works were consulted in creating this article:
  3570.  
  3571. [1] Jim Butterfield, "Jim Butterfield's Complete C128 Memory Map",
  3572.     _The_Transactor_, Volume 7, Issue 01, July 1986 (A Must!).
  3573.  
  3574. [2] Commodore Business Machines, _Commodore_1571_Disk_Drive_User's_Guide_,
  3575.     CBM, 1985.
  3576.  
  3577. [3] Some program called "msdos-to-128" included with "cs-dos" by
  3578.     M. G-something.  Originally published in COMPUTE!'s Gazzette, I think.
  3579.  
  3580. [4] Commodore Business Machines, _Commodore_128_Programmer's_Reference_Guide_,
  3581.     Bantam Books, 1986.
  3582.  
  3583. [5] _The_Transactor_, Volume 4, Issue 05 ("The Reference Issue"), May 1983.
  3584.  
  3585. =============================================================================
  3586. Next Issue:       
  3587.  
  3588. Learning Machine Language - Part 5
  3589.  
  3590. The SPACE INVASION is continued with the design and implementation of the
  3591. player and alien animation along with a look at device scanning for the 
  3592. 1351 mouse, joystick and keyboard.
  3593.  
  3594. The 1351 Mouse Demystified
  3595.  
  3596. Finally! - After 2 delays, this article will explain how the 1351 mouse
  3597. works as well as provide a easy to use interface in machine language for
  3598. both basic and machine language programmers.  An example program will be
  3599. given to illustrate both the 1351 mouse and the multi-tasking system.
  3600.  
  3601. Multi-tasking on the C=128
  3602.  
  3603. A rudimentary multi-tasking system will be implemented for tasks to run
  3604. con-currently with each other. While intended for machine language programmers
  3605. some discussion of how to use this within basic will be given so that more than
  3606. one basic / ml program can be run at a time. An example program will be given to
  3607. illustrate both the 1351 mouse and the multi-tasking system.
  3608.  
  3609. Stretching sprites
  3610.  
  3611. You might have heard that it is possible to expand sprites to more than twice
  3612. their original size. But there is no need to expand all of them equally. This
  3613. article will examine on how to expand them 2,3 or more multiples of their 
  3614. original size.
  3615.  
  3616. LITTLE RED WRITER: MS-DOS file writer for the 128 and 1571/81 drives.
  3617.  
  3618. This article will extend the Little Red Reader program to be able to write
  3619. Commodore-DOS files to an MS-DOS disk.
  3620. =============================================================================
  3621.  
  3622.