Getting Started in BBC BASIC with Owlet
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.
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 :)
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.
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 -
Sounds about right to me. Now let's go have some fun with it!"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."
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!
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.
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 2To make best use of the BBC Micro's limited RAM, each graphics mode is a trade-off between screen resolution and colour palette:
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.
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:
If you draw another line it will start at the end of the previous line:
Above we're using a FOR loop with GCOL to set colors 0-15 in order.
GCOL 0, colourThe 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:
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:
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:
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK