Screen (logical device)

From C64-Wiki
Jump to navigationJump to search

The screen is one of four devices that are handled like "peripheral" devices, even though most of their "action" takes place in the KERNAL and internal hardware of the C-64 itself (the other three being the keyboard, the datassette, and the RS232 interface).

The text screen has device number 3, but since it's the default output device right after cold start (i.e. power-on or reset), programmers rarely need to address the screen as a logical device and hence use the device number; just use PRINT from BASIC programs, or call CHROUT from machine language programs. But to illustrate the principle, here is a snippet of BASIC code that opens a logical file to device #3, i.e. the screen, and sends some output to that file:

10 open 3,3
20 print#3,"Output to screen as device #3"
30 close 3
Output to screen as device #3


Opening a logical file to device #3 can also be used to read data from the screen, starting at the full screen editor's text cursor position.

Internal workings[edit | edit source]

The hardware involved is the VIC-II in its text mode: A chunk of 1000 bytes within the current VIC bank is used to hold a matrix of 40 columns by 25 rows of screen character codes, while another area, permanently allocated to 55296-56295/$D800–DBE7, holds an individual color code for each of those 1000 characters. The VIC-II is set up (through the four most significant bits in 53272/$D018) to display the corresponding 1000 characters in the video signal sent to the physical monitor or TV.

When a character is sent to the screen through KERNAL's system of logical devices, the CPU is sent to 59158/$E716; the beginning of a large routine with, along with a host of subroutines, handles everything from accepting a PETSCII character code in the accumulator, through scrolling the screen and moving existing on-screen characters, to placing the correct screen character code at the correct location, in the correct color.

For KERNAL to be able to do all this, it needs to know the location of the 1000 bytes in RAM, used to hold the screen character codes – it doesn't automatically deduce this from the current VIC bank and the contents of address 53272/$D018: Instead, KERNAL refers to address 648, which holds the high-byte (8 most significant bits) of the 16-bit starting address. The default value is 4, giving the well-known start address for the character RAM area of 4 × 256 = 1024.