RTR logo

R. T. RUSSELL

BBC BASIC (86) Manual



VDU Emulation

Introduction

The VDU commands on the BBC Micro perform a number of important actions, particularly involving the screen display and graphics. Although not strictly part of BASIC, most of these commands have been emulated by BBCBASIC(86).

When the default output stream is selected (*OPT 0), all the console output is passed to a software emulator of the BBC Micro's VDU driver. In this mode, the VDU codes perform a function which is as similar as possible to those of the BBC Micro. Inevitably, the difference in hardware imposes limitations on this emulation. The effect of the various VDU codes is described below, with particular emphasis on the differences between the emulation and the BBC Micro.

The VDU statement takes a list of numeric arguments (constants or variables) and sends their least significant bytes as characters to the currently selected output stream.

A 16 bit value (word) can be sent if the value is followed by a semi-colon. It is sent as a pair of characters, the least significant byte first.

See the keyword VDU for other uses of the VDU command.


VDU Code Summary

NumberMeaning
0 Null - it does nothing.
1 Send the next character to the printer ONLY.
2 Enable the printer.
3 Disable the printer.
4 Write text at the text cursor position.
5 Write text at the graphics cursor position.
6 Enable output to the screen.
7 Bell - make a short 'beep'.
8 Move the text cursor backwards one character.
9 Move the text cursor forwards one character.
10 Move the text cursor down one line.
11 Move the text cursor up one line.
12 Clear the text area - identical to CLS.
13 Move the text cursor to the start of the current line.
14 Enable the auto-paging mode.
15 Disable the auto-paging mode.
16 Clear the graphics area - identical to CLG.
17 Define a text colour - identical to COLOUR.
18 Define a graphics colour - identical to GCOL.
19 Select a colour palette.
20 Restore the default logical colours.
21 Disable output to the screen.
22 Select the screen mode - identical to MODE.
23 Program characters and control the cursor.
24 Define a graphics window.
25 Identical to PLOT.
26 Restore the default text and graphics windows.
27 Send the next character to the screen.
28 Define a text window.
29 Set the graphics origin.
30 Home the text cursor to the top left of the screen.
31 Move the text cursor - identical to TAB(x,y).
127 Backspace and delete.


Description of VDU Codes

VDU 0

Does nothing. In other words, it is ignored.

VDU 1

Provided the printer has been enabled (with VDU 2), the next character (byte) is sent to the printer and not to the screen. If the printer is not enabled, the character is 'ditched'. Any 8-bit value (0 to 255) can be sent.

VDU 1 provides a simple way of sending control characters to the printer (see VDU 2). It works whether the VDU has been disabled with the VDU 21 command or not.

VDU 2

VDU 2 enables the printer. It causes all subsequent output to be sent to both the screen and the printer.

The only control characters sent to the printer are BS (8), HT (9), LF (10), VT (11), FF (12) and CR (13). Other control characters are NOT sent to the printer.

Bytes which are parameters for VDU commands are not sent to the printer. For example,

VDU 17,65
does not send 'A' to the printer. VDU 1 must be used to send control characters to the printer.

VDU 3

VDU 3 disables the printer. It cancels the effect of VDU 2 so that subsequent output to the screen is not also sent to the printer.

Typing ^P has the effect of alternately selecting VDU 2 and VDU 3 (it toggles the printer on and off).

VDU 4

Text at Text Cursor

VDU 4 causes text to be written at the text cursor position in the normal way. This is the default mode.

The characters are 'opaque' and they can be positioned only at text coordinates within the text window. The text within the window scrolls as necessary.

User defined characters may only be written at the text cursor position if a CGA mode (mode < 8) is selected.

VDU 5

Text at Graphics Cursor

VDU 5 causes text to be written at the graphics cursor position. It works only in the graphics modes.

As explained in the Colours and Graphics section, if an EGA or VGA mode is selected (mode > 7) user defined characters may only be written at the graphics cursor position. If a CGA mode is selected, user defined characters may be written at either the graphics or text cursor position.

The characters have a 'transparent' background and they are plotted according to the current foreground GCOL rules and colour. VDU 127 (DEL) is an exception; it backspaces and deletes just as it does in the normal text mode, using the current background GCOL rules and colour.

In the graphics modes, characters may be positioned at any graphics coordinate within the graphics window. The top left of the character cell is the reference point. Characters are clipped to the limits of the graphics window if necessary. No scrolling takes place.

VDU 6

VDU 6 enables output to the VDU screen. It cancels the effect of VDU 21.

VDU 7

VDU 7 causes a short 'beep' from the speaker.

VDU 8

VDU 8 moves the text cursor one character to the left. If the cursor was at the start of a line, it moves to the end of the previous line (right edge of the text window). If it was also at the top line of the text window, the window scrolls down (except in the VDU 5 mode).

If a text window has been defined, the cursor is constrained to remain within the window.

VDU 9

VDU 9 moves the text cursor one character to the right. If the cursor was at the end of a line, it moves to the start of the next line (left edge of the text window). If the cursor was also at the bottom of the text window, the window scrolls up (except in the VDU 5 mode).

If a text window has been defined, the cursor is constrained to remain within the window.

VDU 10

VDU 10 moves the text cursor down one line. If the cursor was on the bottom line of the text window, the window scrolls up (except in VDU 5 mode). Scrolling is inhibited if <Ctrl> and <Shift> are held down together.

If a text window has been defined, the cursor is constrained to remain within the window.

VDU 11

VDU 11 moves the text cursor up one line. If the cursor was on the top line of the text window, the window scrolls down (except in VDU 5 mode).

If a text window has been defined, the cursor is constrained to remain within the window.

VDU 12

VDU 12 clears the text window to the current text background colour and moves the text cursor to the top left corner of the text window; it is identical to CLS.

VDU 13

VDU 13 moves the text cursor to the left edge of the text window, but does not move it vertically.

If a text window has been defined, the cursor is constrained to remain within the window.

VDU 14

VDU 14 enables auto-paging mode. Scrolling will stop after each page. When the text window has been filled, output is paused until <Shift> is pressed.

VDU 15

VDU 15 disables auto-paging mode. This is the default condition.

VDU 16

VDU 16 only works in graphics modes. It clears the graphics window using the current background GCOL action and colour. It does not move the graphics cursor. VDU 16 is identical to CLG.

VDU 17

VDU 17 is identical to COLOUR. The next byte determines the text colour. See COLOUR for more details. The example below sets the text colour to red in the text mode and magenta in any of the 4 colour graphics modes. (Assuming that VDU 19 has not been used to change the default logical to physical colour assignments.)
VDU 17,1

VDU 18

VDU 18 is identical to GCOL. It takes the next two characters as the plotting mode and graphics colour respectively. Thus,
VDU 18,mode,colour
and
GCOL mode,colour
have the same effect. See the Graphics and Colours section for more details.

VDU 19

The Palette

In BBCBASIC(86) colours are allocated by number and, depending upon the chosen display mode, up to 16 logical colours are available.

You can (with certain limitations) set the actual colour displayed by a colour number to any one of the physical colours available. You could, if you wished, set all the colour numbers to the same physical colour. Because you can change the actual colour displayed by a colour number to any one of the physical colours available to you, these numbers are called logical colours.

It may help you to visualise the logical colours as an artist's palette with up to 16 numbered areas where colours may be placed. You could place any of the colours available to you in any one of the areas on the palette. Once the colours were in place on the palette, you could 'paint by numbers'.

As explained in the Graphics and Colours section, the number of logical colours available depends upon the chosen display mode.

Physical Colours

The physical colours are also referred to by numbers. However, a physical colour number always refers to the same colour. Depending upon the graphics adapter fitted to your computer, up to 64 physical colours are available. If a VGA is fitted, an 'analogue' colour palette is also available.

Palettes Available

As explained in the Graphics and Colours section, two palettes are available. The normal palette provides maximum compatibility with the BBC Micro and the enhanced palette provides access to all the colours available with an EGA or VGA.

Setting the Palette

VDU 19 sets the physical to logical colour mapping (the palette). It takes the next five characters as parameters. The first parameter is the logical colour to be set and the second is the physical colour that it is to be set to. The next three parameters are used by the VGA enhanced palette. If a CGA or an EGA is used, they must still be present, but they are ignored (set them to 0).
VDU 19,logical,physical,0,0,0

Colours Available

The colours available depend on the graphics adapter fitted and the type of display connected to it.

Normal Palette - CGA Modes

The colours available (depending on the mode) are listed below.
Colour Number  Physical Colour
0Black (normal background)
1Red
2Green
3Yellow (Brown)
4Blue
5Magenta (blue-red)
6Cyan (blue-green)
7White (normal foreground)
8Intensified Black (grey)
9Intensified Red
10Intensified Green
11Intensified Yellow
12Intensified Blue
13Intensified Magenta
14Intensified Cyan
15Intensified White
It is not possible to provide a true palette when a CGA is used because of limitations imposed by the hardware. The facilities available depend upon the mode used.

CGA 2-Colour Mode

Mode 0

Most EGA cards do not emulate the (peculiar) CGA MODE 0 characteristic. Consequently, the action of the VDU 19 command differs between a CGA and an EGA in MODE 0. Some EGA cards come with a software utility which improves their CGA emulation.

With a CGA, the foreground colour (logical colour 1) can be set to any colour between 0 and 15.

VDU 19,1,physical,0,0,0
and the background colour (logical colour 0) cannot be changed from black. Thus,
VDU 19,0,physical,0,0,0
has no effect.

With an EGA, the palette for mode 0 behaves in a peculiar manner. Instead of altering the foreground colour, setting logical colour 1 alters the background colour and the foreground colour is set to cyan.

CGA 4-Colour Modes

Modes 1, 2, 4 and 5

In Modes 1, 2, 4 and 5, the logical foreground colours can be set to one of two different palettes and the logical background colour can be set to any physical colour between 0 and 15.

If the logical colour number is greater than 0 (a foreground logical colour), the VDU 19 command selects which of two foreground colour palettes will be used depending on the value of the physical colour number. It is not possible to change the foreground colours independently and logical colour numbers between 1 and 3 have the same effect.

The physical colour number selects which of the two palettes are to be used for the foreground colours. If the physical colour number is 0, 1, 2 or 3, then palette No 1 is selected. If the physical colour number is 4, 5, 6 or 7, then palette No 2 is selected. The colours of the two palettes are listed below.

 Palette 1Palette 2
Logical Colour  Physical Colour  Physical Colour
1RedMagenta
2GreenCyan
3YellowWhite
The first example below selects palette No 1 and the second selects palette No 2.
VDU 19,1,2,0,0,0

VDU 19,3,4,0,0,0
By specifying a logical colour of 0, the background/border colour can be set to any colour between 0 and 15. The example below sets logical colour 0 to blue.
VDU 19,0,4,0,0,0

Normal Palette - EGA/VGA Colour Modes

Modes 8, 9, 10, 12,13, 16 and 18

In the EGA and VGA colour graphics modes 16 physical colours are available with the normal palette. These colours are the same as those available in the CGA modes.

For example,

VDU 19,1,4,0,0,0
would set logical colour 1 to blue.

Normal Palette - Text Only Modes

Modes 3, 6, 11 and 14

You cannot change the physical to logical colour number mapping with the normal palette in the text-only modes (modes 3, 6, 11 and 14).

Enhanced Palette

If an EGA or VGA is available, you may use the enhanced palette in any of the colour display modes.

Enhanced Palette - Graphics Modes

Modes 0, 1, 2, 4, 5, 8, 9, 10, 12, 13, 15, 16 and 18

If bit 7 of the logical colour number is set (values 128 to 143), then the enhanced palette is used and the physical colour is taken to be the IBM colour value.

Unfortunately, this is not as straightforward as it might seem. The actual colours depend upon the mode in use, whether the monitor has 4 or 6 inputs and how it is adjusted.

A 6 input monitor has 2 blue, 2 green and 2 red inputs. A 4 input monitor has a blue, green, red and intensity input. The former is capable of displaying 64 colours and the latter 16 colours. If the monitor is truly IBM compatible, it will only act as a 6 input monitor in mode 16. In all other modes, it will act as a 4 input monitor. This restriction is imposed by the monitor and not by the graphics adapter.

The colour number on a 6 input monitor (in mode 16) is made up as shown below.

Not Used Not Used Red' Green' Blue' Red Green Blue
MSBLSB

Thus, colour 18 would give you a combination of Green' and Green; in other words, bright green, and colour 17 would give you a combination of Green' and Blue.

The colour number on a 4 input monitor (or an IBM compatible 6 input monitor in modes other than 16) is made up in a similar manner as shown below.

Not Used Not Used Not used Intensity Not Used Red Green Blue
MSBLSB

With a 4 input monitor, the colour 18 will still give bright green (Intensity and Green), but colour 17 gives bright blue (Intensity and Blue).

Consequently, the colours displayed on a 4 input monitor are only the same as those displayed on a 6 input monitor for physical colours 0 to 7 (plus some other odd numbers). Thus, you need to know the type of monitor you are programming for.

For example,

VDU 19,129,27,0,0,0
would set logical colour 1 to bright cyan with either a 4 or 6 input monitor (27 is binary 00011011),

and

VDU 19,130,26,0,0,0
would set logical colour 2 to a combination of Green', Green and Blue on a 6 input monitor and bright green on a 4 input monitor (26 is binary 00011010).

Whilst a 4 input monitor only displays 16 colours, the colour numbers in the enhanced palette differ from the normal palette because the normal palette numbers have been translated to maximise compatibility with the BBC Micro.

The enhanced (IBM) colour numbers are not contiguous. With a 4 input VDU, colours 8 to 15, 32 to 39 and 40 to 47 are the same as the 'normal' colours 0 to 7 and colours 24 to 31, 48 to 55 and 56 to 63 are the same as the intensified colours 16 to 23. On the other hand, the enhanced palette colours are in ascending order of luminance and they display as a grey scale on a colour-input-compatible monochrome monitor.

Enhanced Palette - Text Only Modes

Modes 3, 6, 11 and 14

If bit 7 of the logical colour number is set (values 128 to 143), then the enhanced palette is used and the physical colour is taken to be the IBM colour value.

For example, the following command will set logical colour 1 to green.

VDU 19,129,2,0,0,0
The text enhanced palette is the same as the graphics enhanced palette.

Enhanced Palette - VGA

The VGA enhanced palette has two modes. The first offers identical features to the EGA enhanced palette and it is set in an identical manner.

The alternative mode provides an 'analogue' palette by allowing you to set the Red, Green and Blue intensities to any value between 0 and 63. The format of the VGA 'analogue' colour palette command is shown below.

VDU 19,logical,-1,red,green,blue
As before, bit 7 of the logical colour needs to be set in order to indicate that an enhanced palette is to be used. In addition, the 'physical' colour is set to -1 to indicate that the following 3 numbers specify the Red, Green and Blue intensities respectively. These may be set to any value between 0 and 63.

The following example sets colour 0 to a dull blue.

VDU 19,128,-1,0,0,10

Border Colour

With most colour adapters it is possible to change the border colour independently in the CGA text modes 3 and 6, all the EGA modes and the VGA mode. Only the enhanced palette is available.

The border is specified by using -1 as the logical colour number.

The following example sets the border colour to green.

VDU 19,-1,2,0,0,0

VDU 20

Default Colour Setting

The VDU 20 command performs two distinct actions: Most EGA cards do not emulate the (peculiar) CGA mode 0 characteristics and the VDU 20 command acts in an apparently unusual manner in this mode if an EGA or VGA is used. This is because the EGA/VGA default palette for mode 0 is cyan for logical colour 1 and white for logical colour 0.

Some EGA cards come with a software utility which improves their CGA emulation.

VDU 21

VDU 21 disables the VDU until a VDU 6 is received. All VDU commands except 1 and 6 are ignored. If the printer is enabled, VDU commands 8 to 13 will still be sent to the printer.

VDU 22

VDU 22 is identical to MODE, except that MODE zeros the value of COUNT whereas VDU 22 does not. The mode is set according to the value of the byte following the VDU 22 command. The example below sets mode 3.
VDU 22,3
VDU 22 also resets all the screen driver variables (colour, palette, windows, cursor position, graphics origin, etc). In particular, VDU 22 performs the actions of VDU 4, VDU 12, VDU 20 and VDU 26.

See the Graphics and Colours section or the keyword MODE for further details.

VDU 23

User Defined Characters

Characters from &80 to &FF (128 to 255) may be programmed using the VDU 23 command. The programmable characters are only available in the graphics modes (0, 1, 2, 4, 5, 8, 9, 10, 12, 13, 15, 16 and 18).

In the EGA and VGA modes (8, 9, 10, 12, 13, 15, 16 and 18), user defined characters programmed with VDU 23 are active only in the VDU 5 mode (plot character at graphics cursor position). In VDU 4 mode, the standard ROM font is used for characters 128 to 255.

The format of the VDU 23 command is;

VDU 23,char_no,r1,r2,r3,r4,r5,r6,r7,r8
'Char_no' is the character number to be programmed and 'r1' to 'r8' are the values of the bytes which define the character, top to bottom. For example, the character illustrated below is defined as character 130 by the following VDU 23 command.
VDU 23,130,24,36,66,255,24,24,24,255

. . . * * . . .     r1 = 24
. . * . . * . .     r2 = 36
. * . . . . * .     r3 = 66
* * * * * * * *     r4 = 255
. . . * * . . .     r5 = 24
. . . * * . . .     r6 = 24
. . . * * . . .     r7 = 24
* * * * * * * *     r8 = 255
Because of hardware limitations, characters up to &7F (127) are not re-programmable.

Since user defined characters occupy a 'cell' which is 8 pixels by 8 pixels, the number of rows of user defined text which may be displayed on the screen depends upon the vertical resolution of the display mode in use.

In all modes other than modes 16 and 18, 25 lines of 'VDU 5' text may be displayed. In mode 16, 43 lines of 'VDU 5' text may be displayed. In mode 18, 60 lines of 'VDU 5' text may be displayed.

Enable/Disable Cursor

The text cursor may be disabled and enabled using the VDU 23 command as shown below.
VDU 23,1,0;0;0;0; Disable cursor

VDU 23,1,1;0;0;0; Enable cursor

Programming the 6845 CRT Controller

UNLESS YOU UNDERSTAND HOW THE 6845 CONTROLLER WORKS, YOU SHOULD NOT USE THIS COMMAND

The VDU 23 command may be used to write to the control registers of the 6845 CRT controller. The command

VDU 23,0,reg,value,0;0;0;
writes 'value' to register number 'reg' of the 6845 CRT controller.

VDU 24

In the graphics modes, VDU 24 defines a graphics window. The following 4 words (pairs of bytes) are the X & Y coordinates of the bottom left corner of the window and the X & Y coordinates of the top right corner of the window, in that order. The coordinates are with respect to the current graphics origin.

If any of the edges of the new graphics window would be off the screen, the command is ignored. It is particularly easy to select invalid window limits if the graphics origin has been moved. It is advisable, therefore, to precede a VDU 24 command with a VDU 26 command to reset the windows and the graphics origin.

The following example defines a graphics window with the bottom left corner at 200,100 (X,Y) and the top right corner at 500,300 (X,Y).

VDU 24,200;100;500;300;

VDU 25

VDU 25 is identical to the PLOT command. It is ignored in the text only modes. See the the Graphics and Colours section or the keyword PLOT for more details.
VDU 25,mode,x_coord;y_coord;
The following example draws a line in the current foreground colour to the point 350,525 (X,Y).
VDU 25,5,350;525;

VDU 26

VDU 26 resets the text and graphics windows to their default positions (filling the whole screen), homes the text cursor to the top left of the screen (0,0), resets the graphics origin to the bottom left of the screen (0,0) and homes the graphics cursor to the graphics origin.

VDU 27

VDU 27 sends the next byte to the screen without interpreting it as a control character. It allows graphics characters corresponding to VDU 0 to VDU 31 and VDU 127 to be displayed. It acts for characters sent to the screen in a similar manner to the way VDU 1 does for characters sent to the printer.

VDU 28

VDU 28 defines a text window. The following 4 bytes are the X & Y coordinates of the bottom left corner of the window and the X & Y coordinates of the top right corner of the window, in that order. The coordinates are with respect to the text origin (top left) and are measured in 'character positions'.

If the text cursor is outside the new window, it is moved to the new home position (top left of the window). If it is inside the new window, it is not moved.

If any of the edges of the new text window would be off the screen, the command is ignored.

The following example defines a text window with the bottom left corner at 0,15 (X across, Y down) and the top right corner at 30,3 (X across, Y down).

VDU 28,0,15,30,3

VDU 29

In the graphics modes, VDU 29 moves the graphics origin to the coordinates specified by the following two words (pairs of bytes). The first word specifies the X coordinate of the new origin and the second specifies the Y coordinate. Subsequent graphics commands operate with respect to this origin.

The following example sets the graphics origin to the centre of the screen (except for modes 16 and 18).

VDU 29,640;400; 

VDU 30

In VDU 4 mode, VDU 30 homes the text cursor to the top left corner of the text window. In VDU 5 mode, VDU 30 homes the graphics cursor to the top left corner of the graphics window.

VDU 31

VDU 31 is identical to PRINT TAB(x,y). It positions the text cursor according to the following two bytes. The coordinates are measured in character positions with respect to the top left of the current text window. The example below positions the text cursor 15 characters in and 10 lines down in the current text window.
VDU 31,15,10
See the keyword TAB for further details.

VDU 127

Delete the character to the left of the cursor and backspace the cursor and all the characters on the line to the right of the cursor. This has the same effect as the sequence backspace-space-backspace (VDU 8,32,8) when in VDU 4 mode.

Left CONTENTS

CONTINUE Right


Best viewed with Any Browser Valid HTML 3.2!
© Doug Mounter and Richard Russell