Contents / Part 3 / Previous chapter / Next chapter / Index


28 Teletext control codes and MODE 7

MODE 7 is a Teletext compatible display mode which is very economical in its use of memory. It can provide a full colour text display with limited, but full colour, graphics. This mode is strongly recommended for applications which do not require very fine graphic detail.

MODE 7 uses the standard Teletext control codes to change colours rather than the BBC BASIC COLOUR and DRAW statements. It cannot be over emphasized that MODE 7 requires different codes and statements from those available in modes 0 to 6. The MODE 7 display consists of 25 lines of 40 characters. Each line will normally consist of white letters and numbers (text) on a black background. It the user wishes to change the colour of the text or the background then a control code must be sent to the screen with a PRINT or VDU statement. By way of explanation let us examine one typical line of characters on a MODE 7 screen.

The screen display consists of 25 lines (numbered 0 to 24) each containing up to 40 characters (0 to 39) columns

Let us examine a typical line and see what is displayed on the screen.

To get this on the screen you will need to type

>MODE 7

>PRINT "ABC";CHR$(129);"DEF"; CHR$(130); "GHI";CHR$(132);"JKL"

You will see that there is a space on the screen between the letters ABC and the letters DEF. That space is in fact occupied by an invisible "control code". The control code 'appears' on the screen as a space but it effect everything to its right. The control code at position 3 is code number 129 and that has the effect of turning all letters, and numbers that follow into RED. The code at position 7 is number 130 which produces green alpha-numerics (letters and numbers).

Here is a list of a few more control codes

129 Alphanumeric red

130 Alphanumeric green

131 Alphanumeric yellow

132 Alphanumeric blue

133 Alphanumeric magenta

134 Alphanumeric cyan

135 Alphanumeric white

To change the colour of the text

To get these codes on the screen you have to type PRINT CHR$(X) just before the text you want to alter, and where X stands for is the code you want. Often you will wish to put the codes in between two words and you do that by placing both the words and the CHR$(X) in one long PRINT statement thus

10 MODE 7

20 PRINT "WHITE";CHR$(131);"YELLOW"; CHR$(135);"AND BACK TO WHITE"

To make things flash

Another code 136 makes everything that follows on that line flash. Try

10 MODE 7

20 PRINT "HELLO";CHR$(136);"FLASHER!"

It must be emphasized that every line starts off in white, non-flashing, normal height, black background and so on. If you want to print a whole page in red letters then every line must start with control code 129.

You should by now understand how to put the control codes into a PRINT statement and so we can see what other effects are available.

To change the background colour requires 3 control codes. Suppose that you want blue letters on a yellow background then you must use the following sequence of control codes

131 yellow alphanumeric

157 new background

132 blue alphanumeric

10 MODE 7

20 PRINT CHR$(131);CHR$(157);CHR$

(132);"BLUE LETTERS ON YELLOW"

As you will gather to change the background colour you must first select letters of the desired colour, then declare a new background and then reselect the colour of the letter. Note that you cannot have a flashing background, so the following sequence.

136 flash

131 yellow alphanumeric

137 new background

132 blue alphanumeric

will produce flashing blue letters on a steady yellow background.

10 MODE 7

20 PRINT CHR$(136);CHR$(131);CHR$

(157);CHR$(132);"BLUE LETTERS ON YELLOW"

Flash is turned off with control code 137.

To produce double height characters

It is possible to write characters with double their normal height using control code 141. Obviously this takes up two of the normal display lines. What is not so obvious is that you must therefore print exactly the same text on two successive lines. Try the following;

10 MODE 7

20 PRINT CHR$(141);"THIS IS DOUBLE HEIGHT"

As you will see it only produces the top half of the letters. Add line 30 and it works properly.

10 MODE 7

20 PRINT CHR$(141);"THIS IS DOUBLE HEIGHT"

30 PRINT CHR$(141);"THIS IS DOUBLE HEIGHT"

Of course you can have (for example) double height, flashing red letters on a white background

141 Double height

157 new background

129 red alphanumeric

136 flashing

10 MODE 7

20 PRINT CHR$(141);CHR$(157);CHR$(129);

CHR$(136); "THE LOT"

30 PRINT CHR$(141);CHR$(157);CHR$(129);

CHR$(136); "THE LOT"

Double height is turned off with control code 140.

As you have came to appreciate it can be quite tedious, typing in CHR$(129) every time you want a Teletext control code. To make things easier it is possible to use the red User Defined Function Keys in combination with the SHIFT key to generate these special codes. While pressing SHIFT the function keys normally produce the codes shown in the table overleaf.

f0 128 No effect
f1 129 Alphanumeric red
f2 130 Alphanumeric green
f3 131 Alphanumeric yellow
f4 132 Alphanumeric blue
f5 133 Alphanumeric magenta
f6 134 Alphanumeric cyan
f7 135 Alphanumeric white
f8 136 Alphanumeric flash on
f9 137 Alphanumeric flash off

As you will see f0 is act to produce code 128 and the other keys produce higher numbers. 128 is said to be the "base address" for the keys. The base address can be altered with *FX 226 if you wish. See page 439 for more details.

Once you have a Teletext control code on the screen you can use the editing keys (e.g. COPY) to copy it into another string. This can be very useful.

Graphics

In addition to displaying coloured letters it is possible in MODE 7 to do a certain amount of work with graphics. -The graphics available in Teletext mode are certainly less easy to use than in other modes but with a little patience some very good effects can be achieved. Another set of control codes are used to change lower case letters into small graphic shapes. The shapes are all based on a two by three grid, the same total size as a large letter.

If you want to use those graphic shapes instead of lower case letters then you must precede them with one of the following control codes:

145 red graphics

146 green graphics

147 yellow graphics

148 blue graphics

149 magenta graphics

150 cyan graphics

151 white graphics

Note that lower case letters will still show as letters in the same colour that you have selected for the graphics. Thus

10 PRINT CHR$(145);"ABCdefGHIjkL"

will show the following on the screen in red

The full list of graphic shapes are given on pages 488 and 489).

Graphics codes

It is possible to calculate the code for any particular graphics shape in the following way. Each of the six cells contributes a certain number to the total code.

and in additionyou should add in 32 + 128 (i.e. 160). For example the ASCII code for

is 2 + 8 + 16 + 32 + 128=186.

Making a large shape

Elsewhere in the guide it shows how to use user-defined characters to draw a space ship (see page 173). By way of comparison a similar but cruder space ship can be made in Teletext Mode. Here is the design and the code number for each graphic character

250 245

255 255

191 239

To make these display as graphics characters each line must be preceded by (for example) code 146 (green graphics). So the following codes must be printed on the screen

146, 250, 245

146, 255, 255

146, 191, 239

These codes can be sent using PRINT CHR$( ) so long as care is taken to get each in the correct place, e.g.

10 MODE 7

20 X=20

30 Y=10

40 PRINT TAB(X,Y);CHR$(146);CHR$(154);

CHR$(250);CHR$(245)

50 PRINT TAB(X,Y+1);CHR$(146);CHR$(154);

CHR$(255);CHR$(255)

60 PRINT TAB(X,Y+2>;CHR$(146);CHR$(154); CHR$(191);CHR$(239)

Instead of using PRINT TAB(X,Y) it is probably easier in this case to use ASCII codes to move the cursor down one line

(code 10) and back four spaces (code 8 four times). It is

probably also easier to use the VDU statement rather than PRINT CHR$( ). If these two things are done then the program becomes

5 MODE 7

6 PRINT TAB(20,10);

10 VDU 146,154,250,245

20 VDU 10,8,8,8,8

30 VDU 146,154,255,255

40 VDU 10,8,8,8,8

50 VDU 146,154,191,239

100 PRINT

A complete list of the Teletext control codes is given on pages 486 to 489.

Teletext graphics codes for the more adventurous

As you will have realised, the statements MOVE and DRAW are not available in the Teletext mode (MODE 7). If you wish to draw lines in this mode you will need to use a suitable procedure - so here is one. It uses a look-up table, called S%, to remember the numbers corresponding to each of the 6 pixels (picture elements), in a Teletext graphics character. The look-up table must be set up at the beginning of the program:

10 DIM S% 7

20 !S%=&08040201

30 S%!4=&4010

Next a row of teletext control codes must be written down the left hand side of the screen to turn every line into a graphics display

40 PROCGR

and the associated procedure is

200 DEF PROCGR

210 LOCAL Y%

220 VDU 12

230 FOR Y%=0 TO 23

240 VDU 10,13,&97

250 NEXT

260 ENDPROC

The main program follows - in this case to plot a "sin curve":

50 FOR X=0 TO 77 STEP 0.25

60 PROCPLOT(X,35+35*SIN(X/10))

70 NEXT

80 END

and lastly here is the procedure to plot the point

300 DEF PROCPLOT(X%,Y%)

310 LOCAL C%,A%

330 VDU 31,X% DIV2+1, 24-Y% DIV3

340 C%=S%?((X% AND 1)+(2-Y%MOD3) *2)

350 A%=135

360 VDU (USR &FFF4 AND &FF00) DIV256 OR C% OR 128

370 ENDPROC

There is no need to know how it works but here is an explanation in case you are interested.

The X and Y co-ordinates are put into X% and Y% and then line 330 moves the text cursor to the position X%, Y%.

Line 340 uses the look-up table (S%) to calculate the "value" (in terms of ASCII code), of the selected pixel at X%, Y%.

Setting A%=135 and jumping to the subroutine at &FFF4 returns the ASCII code of the character which includes the spot X%, Y%. See the example on page 434 which explains this

OSBYTE call for a similar example. The character read from the screen is then ORed with the new pixel (C%) and written with the VDU statement.

The &97 in line 240 produces white graphics dots. Other values will give other colours. For example &91 would draw a red graph.

Two improvements could be made; first we could test for illegal values of X% and Y% with

320 IF X%<0 OR X%>77 OR Y%<0 OR Y%>77 THEN ENDPROC

and secondly we could remember the position of the cursor before we entered the procedure and restore the cursor to that position at the end of the procedure. See page 434 for a similar routine.

Notice that no reference need be made to actual memory co-ordinates and this is VITAL if your programs are to work via the 'Tube'. You may not think it important now but you will find that it is sensible to write programs using the machine code calls provided and not to get in the habit of addressing the memory directly.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001