Wednesday, November 23, 2011

TUTORIAL #3 - PGM "Clear Screen Subroutine"






I will be going through the subroutine PGM line-by-line and explaining what each line of code is doing and how it affects the outcome of the program.



The subroutine PGM is used to Clear the screen of all text and graphics then open graphics page 1.  It uses a number of internal switches to accomplish the clearing of the screen display.



The Apple II has the ability to display color graphic images on the monitor through a process known as Memory Mapped Output.  Various circuits scan specific areas of RAM to determine what should be displayed on the screen.  These circuits convert memory information into images containing pixels or dots that are either turned on or off at particular screen positions.



It is possible to setup specific graphic modes by turning on or off internal Soft Switches.  The soft switches, which reside in reserved memory locations -16304 through -16297 ($C050 through $C057) can let you display Hi-Res graphics page 1 without erasing its previous contents.



                POKE -16304,0                   SETS GRAPHICS MODE

                POKE -16297,0                   SETS HI-RES MODE

                POKE -16300,0                   SELECTS HI-RES PAGE 1



Hi-Res page 2 can be displayed with the following commands:



POKE -16304,0                   SETS GRAPHICS MODE

                POKE -16297,0                   SETS HI-RES MODE

                POKE -16299,0                   SELECTS HI-RES PAGE 2



The two Hi-Res screens reside at memory locations 8192-16383 ($2000 - $3FFF) for page 1, and at 16384 – 24575 ($4000 - $5FFF) for page 2.  These locations are permanently set.



To be able to clear the page 1 screen of all graphics you are basically setting each pixel to zero or writing a zero to each HEX value from $2000 through $3FFF.



There are four soft switches in the Apple computer that control the different types of graphics modes.



Switch 1 – Sets the mode to either GRAPHICS or TEXT

Switch 2 – Sets the mode to either FULL SCREEN or MIXED TEXT & GRAPHICS

Switch 3 – Sets the mode to either PAGE 1 GRAPHICS DISPLAY or PAGE 2 GRAPHICS DISPLAY

Switch 4 – Sets the mode to either LO-RES or HI-RES graphics.



By turning these switches on or off you can change the type of graphics mode used for your game.



65     *********************************************



This is just a comment line to separate each subroutine and helps to space out each section of code.



66     PGM                    LDA   GRAPHICS



If you remember from tutorial #1 I defined a number of variables with certain memory registers and preset values.  Since I set the variable GRAPHICS to equal $C050 it tells the APPLE computer to set the display to graphics mode and not text mode.  By loading the accumulator with this value you are telling the system to set the computer to GRAPHICS mode. When the accumulator is stored with the HEX value $C050 the program knows to display the graphics mode as opposed to the text mode.





67                                  LDA   HIRES



If you remember from tutorial #1 I defined a number of variables with certain memory registers and preset values.  When the accumulator is stored with the HEX value $C057 the program knows to display the HI-RES graphics mode as opposed to the LO-Res graphics mode.





68                                  LDA   PAGE1



If you remember from tutorial #1 I defined a number of variables with certain memory registers and preset values.  When the accumulator is stored with the HEX value $C053 the program knows to display the page 1 MIXED GRAPHICS MODE with TEXT on the bottom four lines of the screen.





69                                  LDA   #$00



Line 69 is used to “[L]oa[D] the [A]ccumulator” or register A with the value equal to zero!  So, you are setting the value of A to equal the HEX value of 0.



70                                  STA   LOW



Line 70 is used to “[ST]ore the [A]ccumulator” or take the value that has been stored in register A and place it in variable LOW, which in this case equals zero!   The variable LOW is going to be used for the LOW byte of the PAGE 1 screen bytes.  Since the screen is made up of byte from $2000 through $3FFF, the HIGH byte would represent the 20 through 3F and the LOW byte would represent the 00 through FF respectively.



71                                  LDA   #$20



Line 71 is used to “[L]oa[D] the [A]ccumulator” or register A with the value of HEX $20.  This is establishing the HIGH byte of the first pixel in the upper-left corner of the screen for PAGE 1 graphics display.  The counter till increment by 1 to progress through each pixel until it gets all the way to $3FFF or the very last pixel in the lower-right corner.



72                                  STA   HIGH



Line 72 is used to “[ST]ore the [A]ccumulator” or take the value that has been stored in register A and place it in variable HIGH, which in this case equal HEX value $20. This is the HIGH byte of the upper left most memory region for the PAGE 1 graphics screen.  HEX $2000 is the first byte of the screen memory address.  The HIGH variable will increment up through 3F where it will finally check for the value of 64 in which case it completes the subroutine function and returns.






73     CLR1                    LDY   #$00



Line 73 is used to “[L]oa[D] register [Y]” or register Y with the value equal to zero!  So, you are setting the value of Y to equal the HEX value of $00, which equals decimal value of 0.  Also, line 73 has a header titled CLR1, which allows it to be called from a separate statement as a sub-subroutine within the subroutine PGM.  In this case it is used with a loop counter to help clear the screen by placing zero values within all the PAGE 1 graphics memory regions.



74                                  LDA   #$00



Line 74 is used to “[L]oa[D] the [A]ccumulator” or register A with the value equal to zero!  So, you are setting the value of A to equal the HEX value of $00, which equals decimal value of 0.  When you are loading the accumulator with the value of zero you are normally getting ready using it to clear or reset a variable within the subroutine.



75     CLR                      STA   (LOW),Y



Line 75 is used to “[ST]ore the [A]ccumulator” or take the value that has been stored in register A and place it in arrow (LOW) starting at variable array zero since you earlier set the register for Y to equal 0!   Also, line 75 has a header titled CLR, which allows it to be called from a separate statement as a sub-subroutine within the subroutine PGM.  In this case it is used with a loop counter to help clear the screen by placing zero values within all the PAGE 1 graphics byte memory regions.



76                                  INY



Line 76 is used to “[IN]crement register [Y]” or take the value that has been stored in register Y and add 1 to it.



77                                  BNE CLR



Line 77 is used to “[B]ranch on [N]ot [E]qual”, which means that as long as the value in register y is not equal to CLR then it will continue to run through the subroutine.  Once the value of A is equal to XX then it will bypass call the subroutine CLR.



78                                  INC   HIGH



Line 78 is used to “[INC]rement” the variable HIGH by 1 or take the value that has been stored variable HIGH and add 1 to it.



79                                  LDA   HIGH



Line 79 is used to “[L]oa[D] the [A]ccumulator” or register A with the value equal to what has been stored in variable HIGH!  So, you are setting the value of A to equal the decimal value of HIGH, which in this case has just been increased by 1.



80                                  CMP  #$40



Once the value of HIGH is loaded into register A, Line 80 is used to “[C]o[MP]are” what is in register A to the HEX value of $40 or the decimal equivalent of 64.  The value of 64 is the highest value of the HIGH BYTE for the last graphical memory screen region.  Remember that the PAGE 1 graphics screen memory regions are from $2000 to $3FFF and out of the HEX value of $2000 you have a HIGH byte which equals 20 and a LOW byte which equals 00.  So, you can see that this subroutine has a counter that starts at 20



81                                  BLT   CLR1



Line 81 is used to “[B]ranch on [L]ess [T]han”, which means that as long as the value in register A is less than [<] 64 it will continue to call the subroutine with the header CLR1.  Once the value of A is greater than [>] 64 it will bypass line 81 and continue to line 82.   What this part of the subroutine is doing is going from memory screen address to address and putting a ZERO value in it so it will display a blank byte in that memory address.  It does this over and over to each byte filling them each with a zero which in turn fill the entire screen with blank memory addresses or an all black screen.



82                                  RTS



Line 82 is used to “[R]e[T]urn from [S]ubroutine”, which means that everything in the PGM subroutine has completed by filling the entire PAGE 1 graphics display pixels with zeros and will be returned back the statement line which called it.  In this case, since the subroutine BEGIN on line 40 called to this subroutine, it will return back to the end of the same line and continue on to the next code line.

As always, please email me any questions you might have.

ultima_revisited@yahoo.com

Joe





 






No comments:

Post a Comment