Commodore's port... (column) Joel Swank.
pt . . . commodore's port . . . co
Expanding the Vic 20 Screen
Most Vic owners know that the Vic displays information on a screen that has 23 lines of 22 characters each. Routines in the Vic KERNAL (control program) take care of formatting output to match this screen size. But the Vic is not limited to this screen configuration. In fact, the 6560 VIC (Video Interface Chip) TV controller IC (Integrated Circuit) can display a wide variety of other screen formats. After the 6502 microprocessor IC that controls all operations in the VIC, the 6560 is the most important. It generates the color display and sound that are the most impressive features of the Vic.
The Vic uses a technique called memory-mapped video for displaying information on a TV. This technique is common among low cost computers. With memory-mapped video, a section of the computer memory or RAM (Random Access Memory) serves a dual purpose. It appears to the microprocessor as normal memory, but is also used by the video interface to determine what is displayed on the screen. Each byte of memory determines what pattern of dots is displayed in each area, or character, of the screen.
The video controller reads this section of memory every thirtieth of a second and displays it on the TV. So, whenever the microprocessor alters a byte of this memory, the change is immediately visible on the screen. This special section of memory is called the screen buffer. Each byte in the screen buffer contains the number, called the screen code, of the character that is to be displayed in one part of the screen.
If the display is to have color there must also be a section of memory that determines the color of the characters on the screen. This is called the color buffer. Each byte of the color buffer determines the color of one character. So, each character on the screen must have one byte that determines its pattern of dots and one byte that determines its color.
Table 1 is a list of the screen codes for each Vic character from page 141 of the Vic user's manual. Table 2 is a list of the Vic color codes from page 144 of the user's manual.
To place a character directly onto the screen, first look up the screen code of the character in Table 1 and the location of the desired spot on the screen in the top half of Figure 1. Then POKE the screen code into the desired location. For example, to place an asterisk on the third character of the tenth row, type POKE 7880,42.
If you do this on an empty screen you will not see the character. This is because the color code in the color buffer is still set to white, the screen color. Now find the location in the color buffer of the desired spot on the screen from the second half of Figure 1, and the code for the desired color from Table 2, and POKE the color code into the color buffer: POKE 38600,2. Since 2 is the code for red, a red asterisk should now appear on row 10, character 3.
This technique can be used to place any character of any color anywhere on the screen.
Mapping The Display
The details of how the computer memory is used to map the display vary from one video controller to another. Many controllers have a fixed format and a fixed character set. The Vic 6560 controller has registers that allow the computer to change the way the 6560 displays memory. This adds a great deal of flexibility not found in simpler video controllers.
Table 3 gives a summary of the registers in the 6560. These registers control the size and format of the Vic screen, the color of the screen and border, and the location in memory of the screen buffer and color buffer. They also control other functions of the 6560 not normally found in a video controller, such as sound, game paddle inputs, and light pen. A complete description of all registers in the 6560 is given in the Vic programmer's reference guide, starting on page 212.
I will now explain and give some examples of using five of these registers to change the size and location of the Vic screen. Vic addresses will be given in decimal and hexadecimal for convenience. Hexadecimal numbers will be denoted with a leading dollar sign ($ ).
The first two registers in the 6560 are the horizontal and vertical centering registers, located at 36864 ($9000) and 36865 ($9001). These are used to center the Vic screen within the border. Location 36864 is the horizontal control register. Increasing the value in this register moves the screen to the right, while decreasing the value moves the screen to the left.
The high order bit of this register is used for another function (interlace scan), so it should always be off. This means that the valid range for horizontal centering is 1 to 127.
Location 36865 controls the vertical position of the Vic screen within the border. Increasing the value in this register causes the screen to move down, and decreasing the value causes the screen to move up. The valid range of this register is 0 to 255. You can experiment with these registers by POKEing different values into them and watching the change in screen location. Pressing the STOP/RUN and RESTORE keys will restore them to the default values.
The next two registers in the 6560 control the line length and the number of lines on the screen. Location 36866 ($9002) controls the number of characters per line. Only the low order seven bits of this register are used for line length. Bit 7 is part of the screen buffer address and should not be changed.
This should allow from one to 127 characters per line, but the 6560 will accept no more than 29 characters per line. Location 36867 ($9003) controls the number of lines on the screen. This value is kept in bits 1-6. Bits 0 and 7 are used for other functions. Six its allow from one to 63 lines on the screen.
The Vic screen can be from one to 63 lines or from one to 29 characters. Just how much of the screen is actually visible at one time depends on the TV being used. Some TVs can barely display all of the standard Vic screen. Others display the screen with a wide border. My Sony can display a maximum of about 30 lines of 24 characters.
Some TVs have vertical and horizontal size adjustments on the back that can be used to increase the amount of picture displayed. If these controls are external they can easily be adjusted. If they are internal, you can only change them by operating the TV with the back off, a dangerous proposition. Operating the TV with the back off should be left to experienced TV service personnel. The high voltages found in a TV can destroy the delicate circuits in the Vic. I have seen ICs actually explode when subjected to such voltages.
An expanded screen is not compatible with the Vic KERNAL. The KERNAL works properly only with the standard 22 X 23 screen. This means that PRINT statements and the cursor movement keys will not work properly. Things must be displayed by POKEing screen codes into the screen buffer, and color codes into the color buffer. Also, if you expand or move the screen, the maps in Figure 1 are no longer valid, and must be redrawn to match the new screen configuration.
The screen buffer normally resides at location 7680 ($1E00) through location 8185 ($1FF9). Vic memory ends at location 8192 ($1FFF), leavng only six bytes for expansion. To expand the screen more than six bytes, the screen buffer must be relocated. But it is possible to expand the screen and use only these six bytes.
Stopwatch Program
Listing 1 is a short Basic program that makes use of these six bytes to display a stopwatch. It enlarges the screen by one line to 24 lines, but does not change the line length. Leaving the line length the same allows the KERNAL to be used to display information on the top 23 lines of the screen.
Since memory ends at location 8191, only the first six bytes of the new line are usable. The other 16 bytes are displayed from the Vic ROMs and cannot be changed. These 16 characters are blanked by storing the screen color (white) in their color buffer locations starting at location 37888 ($9400).
The first six characters of the last line are used to display a four-digit stop-watch. The Vic's jiffy clock is used to keep time. The function keys also print their functions on the screen each time that they are pressed.
As the program runs and keys are pressed, the screen behaves normally. Lines scroll up just as they should, except that the line with the stopwatch is never used. KERNAL does not know about this line, and PRINT statements can never access it.
The only way to display anything on the last line is to POKE screen codes directly into the screen buffer. This means that the normal ASCII characters must be converted into screen codes. For numeric digits this is easy, since their ASCII code is the same as their screen code and can be converted by the Basic ASC function. Table 1 can be used to convert other characters from ASCII to screen codes.
Expanding The Screen Further
Expanding the screen further requires that the screen buffer be moved. Moving the screen buffer means telling the 6560 the new screen buffer address and also notifying the KERNAL and Basic of the move. Listing 2 is a Basic program that moves the screen buffer to location 7168 ($1C00).
Basic is informed about the new location by POKEing 28 into locations 52 and 56 and executing a Basic CLR command. This tells Basic not to use any memory above memory page 28 ($1C). The KERNAL is notified by POKEing 28 into location 646, and calling the KERNAL subroutine at 58648 ($E518) to initialize KERNAL pointers. This subroutine also stores the new screen buffer location in the 6560 registers.
The new number of lines must be inserted into the 6560 register at 36867 ($9003) and must be stored in bits 1 through 6 of this register, not using bits 0 and 7. To do this, the number of lines must be doubled. So to tell the 6560 26 lines, 52 must be POKEd into this register.
In this program the screen is expanded by three lines to 26 lines, and a full time of day clock is displayed in the three extra lines. Before this program is RUN the Vic jiffy clock must be set by entering the current time into the variable T1$. Twenty-four hour clock time must be entered. For example to set the current time to 6:33 p.m., TI "183300' would be entered. A full six digits must always be entered.
The program updates the clock display as it waits for input. When a key is pressed, its ASCII equivalent is displayed on the normal Vic screen. Once again the extra lines at the bottom are not affected by printing on the normal Vic screen.
Depending on the TV being used, all of the three extra lines may or may not be visible. The screen can be centered by pressing the cursor up/down key. When it is pressed, the program scrolls the entire screen up or down by incrementing or decrementing the vertical centering register in the 6560.
These three extra lines on the screen could be used for a wide variety of other things. A program could put status information there or a trace of where the program is executing. Any desired information can be displayed totally independently of what is happening on the standard screen.
It is also possible to expand the width of the screen by increasing the value in the line length register at location 36866 ($9002), but doing this makes Basic PRINT statements totally unusable. All data to be displayed anywhere on the screen must be POKEd into the screen buffer. Expanding the screen this way is most useful for graphic displays such as video games.
The Vic has great flexibility in its screen format. There are many more possibilities than the simple examples given here. You can use this flexibility to add a personal touch to your applications and tailor the display to the specific needs of each application. Using the techniques presented here, you can make your programs easier and more pleasant to use.
Table: A list of the screen code for each Vic character.
Table: Table 2. The color codes for the Vic character colors.
Table: The location and use of the 16 registers used to communicate with the 6560 Video Interface Chip.
Table: Listing 1. Stopwatch program. A stopwatch is displayed on the expanded Vic screen.
Table: Listing 2. Time of day clock program. The Vic screen is expanded by three lines and relocated so that a digital clock can be displayed.
Photo: Figure 1. Maps of the Vic screen buffer and color buffer. Codes from Table 1 are POKEd into the page 1 of this figure to place characters on the screen. Color codes from Table 2 are POKEd into locations on page 2 of this figure to set the color of each character.