The CRTC Interface Essential aspects of display page 80-column by 25-row text

advertisement
The CRTC Interface
Essential aspects of display page
and cursor control for standard
80-column by 25-row text
Problem background
• For proper display of text that applications wish
to write to the STDOUT device-file, our ‘os630’
program will need to reposition the CRT cursor
• This will involve directly programming certain
registers in the CRT Controller (i.e., hardware)
• For cooperation with messages that our ‘trackldr’
boot-loader displays (it uses int-0x10), we also
need to set some variable-values located in the
ROM-BIOS DATA AREA (i.e., software)
• This lesson will explore these two issues
Hardware registers
• The CRT Controller is a peripheral chip
• It implements 25 standard CRTC registers
• Access to these registers is accomplished
via a multiplexing scheme that uses just
two I/O port-addresses:
address-port: 0x03D4
data-port:
0x03D5
The relevant CRTC registers
• We are only concerned with 6 (of the 25)
standard CRTC registers:
• 0x0A: CURSOR_START
• 0x0B: CURSOR_END
• 0x0C: START_ADDRESS_HI
• 0x0D: START_ADDRESS_LO
• 0x0E: CURSOR_LOCATION_HI
• 0x0F: CURSOR_LOCATION_LO
8x16 character box
Cursor_start = 12
Cursor_end = 13
scanline 0
scanline 1
scanline 2
scanline 3
scanline 4
scanline 5
scanline 6
scanline 7
scanline 8
scanline 9
scanline 10
scanline 11
scanline 12
scanline 13
scanline 14
scanline 15
CURSOR START/END
7
6
Index 0x0A:
5
disable
4
3
2
1
0
starting_scanline
CURSOR_START REGISTER
7
Index 0x0B:
6
5
skew
4
3
2
1
ending_scanline
CURSOR_END REGISTER
0
Organization of VRAM
4KB
Page 7
4KB
Page 6
4KB
Page 5
4KB
Page 4
4KB
Page 3
4KB
Page 2
4KB
Page 1
4KB
Page 0
Base_Address = 0xB8000
Changing the visual page
7
6
5
4
3
2
1
START_ADDRESS_HI
register-index 0x0C
Programming example:
0
7
6
5
4
3
2
1
START_ADDRESS_LO
register-index 0x0D
// switches display to vram page 5
// the offset to visual page 5 (in words) is 0x2800 (= 5 * 2048)
mov
dx, #0x03D4
// port-address in register DX
mov
ax, #0x280C
// value=0x28, register=0x0C
out
dx, ax
// write value to CRTC register
mov
ax, #0x000D
// value=0x00, register=0x0D
out
dx, ax
// write value to CRTC register
0
Moving the CRT’s cursor
7
6
5
4
3
2
1
CURSOR_LOCATION_HI
register-index 0x0E
Programming example:
mov
mov
imul
add
mov
mov
out
mov
mov
out
0
7
6
5
4
3
2
1
0
CURSOR_LOCATION_LO
register-index 0x0F
// moves cursor to row 4, column 9, on page 0
dx, #0x03D4
bx, #4
bx, #80
bx, #9
ah, bh
al, #0x0E
dx, ax
ah, bl
al, #0x0F
dx, ax
// port-address in register DX
// row-number
// times cells-per-row
// plus column-number
// offset’s MSB
// CURSOR_HI index
// write value to CRTC register
// offset’s LSB
// CURSOR_LO index
// write value to CRTC register
Scrolling the screen
• Here’s a code-fragment that will scroll the
contents of vram page 0 up by one line:
mov
mov
mov
mov
mov
cld
mov
rep
movsw
mov
mov
rep
stosw
ax, 0xB800
ds, ax
es, ax
di, #0
si, #160
cx, #1920
// address vram page 0
// with DS register
// also ES register
// destination is top line
// source is one line lower
// do forward copying
// 24 times 80
ax, #0x0720
cx, #80
// blank character/color
// characters on bottom line
Linux uses hardware scrolling
• The value of the CRT START_ADDRESS
is reprogrammed, to change the region of
visible vram by one line (i.e., add #80)
• So instead of subdividing vram into eight
4KB pages, the entire 32KB vram is one
continuous page, but only partially visible
• To scroll up by one line, Linux adds #80 to
the value of the CRT_START_ADDRESS
The ROM-BIOS variables
• Several variables in the ROM-BIOS DATA
AREA are used by the VIDEO ROM-BIOS
routins (i.e., int-0x10) to keep track of the
current visisible page and of the positions
of the cursors on each of the eight pages
• The locations of these variables are part of
the IBM-PC BIOS standard, and as such
are widely documented
Standard addresses
0x449 (byte) video mode-number
0x44A (word) number of columns on screen
0x44C (word) current page-size (in bytes)
0x44E (word) current page-address
0x450 (byte array) cursor-positions (col,row)
0x460 (cursor type) (END, START)
0x462 (byte) current page-number
0x463 (word) CRTC i/o port-address
Real-mode INT-0x10 services
• 0x00: set_display_mode
• 0x01: set_cursor_type
• 0x02: set_cursor_position
• 0x03: get_cursor_position_and_type
• 0x05: select_new_video_page
• 0x06: scroll_current_page_up
• 0x07: scroll_current_page_down
• 0x08: read_char_and_attrib_from_screen
• 0x09: write_char_and_attrib_to_screen
• 0x0A: write char_only_to_screen
• 0x0E: write_teletype_to_active_page
• 0x0F: return_video_status
• 0x13: write_string
NOTE: These ROM-BIOS services are not available in protected-mode
Cursor-movement demo
• To illustrate reprogramming of the six CRT
controller registers, we wrote ‘arrows.s’
• It lets the user control the cursor position
and visible page by using arrow-keys
• It also changes the height of the cursor
• An unusual feature (not recommended) is
its use of “polled mode” keyboard deviceprogramming (instead of “interrupt-driven”)
Polling the status-register
• The keyboard interrupts are masked
• The keyboard controller’s status-register is
read and reread in a tight loop until bit #0
is set, indicating that a key was pressed
and thus the output-buffer is now “full”
• Then the output-buffer register is read by
the CPU to get the key’s “scancode”
• For arrow-keys, the cursor is moved
• Other keys are ignored (except ESCAPE)
Disadvantage of polling
• Almost all of the CPU’s time is consumed
by continually reading the status-register
• So this would not be a good design to use
in writing a multitasking operating system
• On the other hand, for single-tasking it has
the advantage of not requiring a interrupt
service routine to be written, so the demo
code can be shorter and simpler
Another noteworthy feature
• The ‘arrows.s’ demo uses a ‘jump-table’ to
efficiently dispatch control to appropriate
subroutines, based on a variable’s value
• This is a similar programming situation to
using a ‘switch’ statement in C/C++
• The jump-table avoids the long chain of
‘compare-and-branch’ statements for all
the various possible cases that can occur
In-Class exercise
• To insure your mastery of the jump-table
concept, and cement your grasp of how
the CRTC START_ADDRESS registers
are programmed, try modifying the demo
to incorporate these additional actions:
when one of the function-keys F1-F7 is
pressed, the display is switched to the
correspondingly numbered display-page
Download