5

Getting Started in BBC BASIC with Owlet

 3 years ago
source link: https://www.bbcmicrobot.com/learn/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Learning to code in the 1980s meant learning BASIC. No installations, no dependencies, no downloads. Just you, some type-in listings, and an 8-bit computer...

Retrocoding fun with BBC BASIC

BASIC is a fun, easy to learn language that was standard on 80s home computers and inspired a generation to program. The best implementation of the era was BBC BASIC created by Sophie Wilson for the 8-bit BBC Microcomputer found in 80s UK schools. (She later designed the ARM architecture - the most widely used CPU architecture on the planet.)

This article aims to get you running BBC BASIC code in a few minutes :)

1244416219589074947.gif
1229521582802046982.gif
1240071203487965184.gif

The micro renaissance

BBC BASIC had a revival beyond the die-hard retro heads in 2020 thanks to BBC Micro Bot - a Twitter bot that runs your tweet as code and replies with a video. It was featured in Gizmodo, IEEE Spectrum and Xataka, and gained fans in comedian Dara Ó Briain, science writer Ben Goldacre, and Raspberry Pi founder Eben Upton. It's run over 10,000 BASIC programs and a talented, friendly community has grown around it. As a result people have created truly awesome programs in one tweet of a 40 year old language.

1234923162808119297.gif
1228377194210189312.gif
1247226355193982976.gif

Some programs are tiny pieces of art in a minimalist palette and pixels. Others are technically mind blowing - e.g. ray tracers implemented in fewer bytes of BASIC than this paragraph occupies. But these amazing tweet-sized programs often use code size optimizations which make them hard for beginners to follow. So to help I teamed up with Matt Godbolt to develop a simple, intuitive web editor called Owlet. Owlet is embedded in this blog to get you a quick start in BBC BASIC.

Mostly harmless

BASIC has been out of fashion for at least 30 years for some good reasons. However BBC BASIC (1981) addresses shortcomings highlighted by Dijkstra (1975) with newer features like PROC and FN meaning you can structure code and completely avoid GOTO and GOSUB which he considered harmful. With BBC BASIC in mind Eben Upton recently offered a measured modern perspective -

"You learn it, and then you move on. Maybe Python’s a better choice these days because the ladder goes up further, but nothing wrong with BASIC."

Sounds about right to me. Now let's go have some fun with it!

Start coding in BBC BASIC right now

There's a sandbox editor below with a full BBC Micro emulation running next to it (there are several full 6502-based computers with 32KB of RAM embedded in this page thanks to JSbeeb!). Change the message in the PRINT statement and the emulated result will update live as you type. Go on, try it!

BBC Micro Computer 32K | GXR ROM
MODE 2
REPEAT
COLOUR RND(7)
PRINT "Your message here"
UNTIL FALSE
Congratulations if this is your first BBC BASIC program! The same program will run on the original 1980s hardware if you want.

This article aims to get you into coding fun for the classic BBC Microcomputer as quickly as possible. But you're going to want more details on BBC BASIC to progress further. You can find full user guides based on the 1980s text here:

Let's do some graphics

Although the BBC Micro has limited graphics by today's standards it can still look amazing. In this section we'll look at how to approach creating your own.

BBC Micro Computer 32K | GXR ROM
MODE1
Q=120
PRINT"�����������
���"
C$="�@��0� ����"
FORY=0 TO 7
PRINT"";MID$(C$,1+Y*2,2)
MOVE0,400+Y*80
PLOT97,1280,80
GCOL 0,0
FOR X=0TO1280 STEP Q
S=RND(30)+30
Y=380+S*4:W=S

Choosing a graphics mode

Selecting the graphics mode is the first line for many programs and it clears the screen at the same time:

MODE 2 
To make best use of the BBC Micro's limited RAM, each graphics mode is a trade-off between screen resolution and colour palette:
MODE 0
640 x 256
MODE 1
320 x 256
MODE 2
160 x 256
MODE 3
text only
MODE 4
320 x 256
MODE 5
160 x 256
MODE 6
text only
MODE 7
teletext

Try editing the MODE number in the first line of the code below. You'll see the text size and colour palette changes - but the treatment of graphics coordinates always stay the same. Compare the colour results with the mode table above.

BBC Micro Computer 32K | GXR ROM
MODE 2
FOR C = 0 TO 15
COLOUR C
PRINT "Hello ";C
REM Draw a 100x100 unit 
square
DRAW 400,0
DRAW 400,400
DRAW 0,400
DRAW 0,0

Note - the BBC Micro starts in MODE 7. This mode is teletext only, and doesn't support the standard graphics we'll cover below.

Drawing lines

Graphics commands always treat the screen canvas as a 1280 x 1024 grid, with co-ordinates 0,0 being the bottom left and 1279, 1023 being top right. To draw a line, you first use the MOVE command set an origin for the line you want to draw. You then use DRAW to give the end point, and a line will be drawn on the screen:

BBC Micro Computer 32K | GXR ROM
REM Draw from bottom left to 
top right
MODE 2
MOVE 0,0
DRAW 1279,1023
GOTO 70

If you draw another line it will start at the end of the previous line:

BBC Micro Computer 32K | GXR ROM
REM Lines starts from the 
end of the last
MODE 2
FOR colour = 0 TO 16
REM use GCOL to set graphic 
colour
GCOL 0,colour
DRAW RND(1280),RND(1024)
NEXT colour

Above we're using a FOR loop with GCOL to set colors 0-15 in order.

GCOL 0, colour
The numbers of the colors correspond to the numbers you saw in the mode table. Because we're in MODE 2 the numbers give the following colours:
MODE 2
160 x 256

Plotting shapes

To draw shapes we use MOVE to define key points, then use PLOT to draw the shape. The number directly after PLOT defines what shape we draw. You can edit the number assigned to shape in the code below:

BBC Micro Computer 32K | GXR ROM
MODE 2
REM triangle fill: &51
REM rectangle fill: &61
REM circle outline: &91
REM circle fill: &99
shape = &99
GCOL 0,RND(7)
MOVE RND(1280),RND(1024)
PLOT shape, 100,100
GOTO 80

If you were wondering the & symbol just means a hex number in BBC BASIC. You can use decimal instead if you want.

IF (&99 = 145) PRINT "True"

Plot codes

There are many other plot codes which let you do fills or draw circles, rectangles, parallelograms or triangles - here's a quick reference:
Code      Result
-------------------------------------
PLOT 4    MOVE
PLOT 5    DRAW line
PLOT 69   Plot Single Pixel
PLOT 85   Filled Triangle
PLOT 101  Filled Rectangle	(GXR only)
PLOT 145  Circle Outline	(GXR only)
PLOT 153  Filled Circle	(GXR only)
PLOT 197  Ellipse Outline	(GXR only)
PLOT 205  Filled Ellipse	(GXR only)
This table was swiped from Paul Malin who made a very nice page summarizing BBC BASIC graphics commands which has more detail. GXR refers to the Graphics Extension ROM installed in bbcmicrobot. GXR gives the BBC Model B the same graphics command support as the later BBC Master 128 computer.

Pattern fills

Here we put what we learnt together so far by drawing rectangles and triangles to make a scene. We use MODE 1 for higher resolution but fewer colours. We also make use of patterned fills to render a graduated sky effect:

Changing the palette

We used MODE 1 for higher resolution but the trade off is we're limited to four colours:

MODE 1
320 x 256
We can't increase the number of colours displayed on screen simultaneously. However BBC BASIC allows you to configure the palette of colours assigned using the powerful VDU command:
VDU 19,1,4,0,0,0 :REM 1 = blue(4)
VDU 19,2,6,0,0,0 :REM 2 = cyan(6)
Here we change our sunset for a sunrise by swapping red for blue and yellow for cyan. Palette configuration is just one of many useful features possible with the VDU command - in fact all the graphics commands call VDU behind the scenes.

Changing the palette with VDU modifies the colours on the screen almost instantaneously. This enables an animation technique called palette cycling - where the animation is pre-rendered on the screen and then different sections are triggered just using VDU palette assignment:

This old classic version of BBC BASIC doesn't have an explicit WAIT or DELAY command, so instead we used INKEY to specify a delay in centiseconds. Try changing the value assigned to INKEY on line 300 and it will speed up the animation. For example:

z=INKEY(30)

Next steps

This article was a bit of an experiment - the first I've ever seen with live retrocoding sandboxes! Embedding 9x full BBC Microcomputer emulations into a page was asking for trouble. If you read this far I'm glad it worked and hope it's been a useful way to give you a taste of BBC BASIC. Next installment we'll look at code size optimization for BBC Micro Bot.

Until then for further inspiration take a look at the amazing code submitted by bbcmicrobot users at bbcmicrobot.com.

That's all for now. Have fun!

Dominic Pajak is the creator of BBC Micro Bot


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK