Feature: The BBC Micro Bot

Richard Ashbery has been looking at the BBC Micro Bot website, and in particular he’s been doing so with Iris, the RISC OS Developments web browser project. After spending a little time with the two, he sent a quick review to RISCOSitory, but I’ve decided to build on what he wrote with a brief feature.

Let’s start with Richard’s review:

Clever, clever, clever – The Retro Scene is Going Strong

The BBC Micro Bot site is still up and running and more very smart coding continues to be added to the site despite a 280 character limit. Incredible what can be done on an 8-bit emulator. With a JavaScript browser like Iris and a fast Raspberry Pi4 the site can be displayed with its numerous animations almost as quickly as a PC Windows browser like Edge.

Where it slows down is in the Owlet BBC BASIC Editor although Iris will display the page and shows the code if a picture is clicked. If the program is relatively short, you can even select, copy and paste it to your selected editor but this can take a long time. A Windows browser is obviously much better for this.

For background, created by Dominic Pajak BBC Micro Bot is an online emulation of a BBC Micro, based on emulators by Matt Godbolt and Chris Evans (no, not that one, or any of those more widely famous ones). It’s designed specifically to cater for programs that fit in the length of a tweet – a short comment posted on Twitter, which has a 280 character limit. Therefore, any programs written for it need to be able to fit inside that limit.

This all works from Twitter itself, and you can see many programs run through it by visiting BBC Micro Bot Twitter account. If you have an account yourself you can write a program in a tweet. Begin that tweet with @bbcmicrobot to direct it at the bot, and the bot will run your program, and reply with the results.

Typically, your program will be run for 30 seconds (if it doesn’t end before that time) and a three second video of the result is recorded as an animated GIF, which is what you get in reply, but because some programs may take longer to produce meaningful output, you can add an emoji to the end of your tweet to change that behaviour – 🎬 (Unicode 1f3ac) will run the bot for the equivalent of three hours on a real BBC Micro before recording the three second video, and 🚀 (1f680) will run the program for the same amount of time, and then take a screenshot instead of a video.

Now, 280 characters is a very small amount of space in which to squeeze your program – remember that the BBC Model B had 32kB of RAM, with the amount usable for programs varying according to the screen mode in use, but it was always a lot more than a you have in a tweet. It’s possible to get around this to some extent, and you’ll see this if you look at some of the tweets that are directed at the bot.

One method – the simplest of all – is to compact your program as much as possible; don’t bother with line numbers, avoid unnecessary spaces, and so on. You can also abbreviate keywords, such as using P. instead of PRINT. These are the same tricks you might be inclined to use if you were programming a real BBC Micro – or even a RISC OS computer, although less so in the latter case because there is even more memory available, and readable code is more maintainable code!

Another trick is to make use of the fact that BBC BASIC is tokenised, with keywords replaced by a value that fits in a single byte. Because of this, you can replace commands completely with a character that represents its tokenised value in a tweet. For example, the tokenised value used for PRINT is &1F, so the Unicode character U+001F (or ñ) can be used in its place, which is an extra character saved over using the P. abbreviation. The editor mentioned above – which was put together by Dominic and Matt – helps with this; there’s a ‘shrink’ button that replaces all BASIC keywords in this way.

For the really advanced, another trick is to use base 2048 encoding. This gives you even more space to play with, because it has the effect of squeezing a program into a smaller number of characters – but the flip side is that your program is no longer human readable, and instead looks like complete gibberish; but they can still be run by the bot.

A very good example of that is a program tweeted in July by P Malin, to draw an astronomical image. The base 2048 encoded program as tweeted looks a bit (subject to copying and rendering errors) like this:

ຈࡘ௩ۄ૮ਪརښϮଢƆܤҾɚχࡓଯҴԻզઈৱೠÞѥԊৡޙШɞಜuςറऔڤࠄঊಛࠕƅƂ۴ՌȠ६ಸԚҏಡƑՌȡൽਓӶДԒҺਠബॳԍӔϽજຟՌȠআಚʤٻ๑Ԟڤબॠ౫ϣФզՃڂૹൽਔΚѿਡՃਲઊʎదԺҏߥछޑbΦཡผЭബՃڂਯ৯ਓΜЭտಥڄॵɢਸԚϗछஜധബشƔԛҏ۸ૐՌȠࠕՐ૦ϗڴ८ޓЩρનદƅତչոȡԶՊԜҐঘछȿࠊદથۈϟଟ૧ઑ੪ৰסծմۉԝۉҫʪཎƋϗప൞ոमআਗӘդاԜਟञϭཐβϏҰƗضݭɏƼΜϽࡧҺୟ൙ɂਣښНԓԤۆॵɦψζץչඡבਢبകԢҐഞઆฯ൘८൚ߡݿຕԞଗટங૬Λ୫ࡘ৭ޕڈવન૮Ϡಟઆ෬੦ʐయqއԒ൲Ɉݦʒ೩øЬוࢲסDݾ൷Νݿڐһඈഓ

That all fits in a tweet, but in the editor it expands to a 380 character program:

ë1
ï5
ãG=³(-64)¸400
t=³(99):q=³(99)
u=³(1280):v=³(1024)
A=³(1)*3
R=90/(1+³(200))
Q=1+R*(.5+³(1)/2)
a=1+3*³(1)^2
M=1
ç³(9)<4Q=R:t=0:q=0:A=0:M=¯/3:a=1
C=(1+3*³(1)^2)*R*R
ãi=0¸C
S=-ª³(1)
T=i*M
U=S*R*µT
V=S*Q*›T
T=S*A
X=U*›T+V*µT
Y=V*›T-U*µT
D=(X*X+Y*Y)/(R*R+Q*Q)
Z=99*((2.7^-D)+.1)
Z=Z*(³(1)-.5)^3
y=Y*›t+Z*µt
Z=Z*›t-Y*µt
x=u+X*›q+y*µq
y=v-X*µq+y*›q
P=°x,y)+a
çP>3P=3
æ0,P
ð69,x,y
í,

Decoded, so that the tokens are replaced by the actual keywords, that becomes larger program still at 463 characters, but one that’s more recognisably BBC BASIC:

MODE1
VDU5
FORG=RND(-64)TO400
t=RND(99):q=RND(99)
u=RND(1280):v=RND(1024)
A=RND(1)*3
R=90/(1+RND(200))
Q=1+R*(.5+RND(1)/2)
a=1+3*RND(1)^2
M=1
IFRND(9)<4Q=R:t=0:q=0:A=0:M=PI/3:a=1
C=(1+3*RND(1)^2)*R*R
FORi=0TOC
S=-LNRND(1)
T=i*M
U=S*R*SINT
V=S*Q*COST
T=S*A
X=U*COST+V*SINT
Y=V*COST-U*SINT
D=(X*X+Y*Y)/(R*R+Q*Q)
Z=99*((2.7^-D)+.1)
Z=Z*(RND(1)-.5)^3
y=Y*COSt+Z*SINt
Z=Z*COSt-Y*SINt
x=u+X*COSq+y*SINq
y=v-X*SINq+y*COSq
P=POINT(x,y)+a
IFP>3P=3
GCOL0,P
PLOT69,x,y
NEXT,

And the result of running it should look something like this:

An astronomical scene, drawn by P Malin's base 2048 encoded BBC BASIC program
An astronomical scene, drawn by P Malin’s base 2048 encoded BBC BASIC program.

If you have a Mastodon account, the bot also is now running from there as @bbcmicrobot@mastodon.me.uk. Mastodon is an interesting alternative to Twitter, with a number of similarities and a number of differences – but a key one for the bot is that ‘toots’ (the Mastodon equivalent to tweets) have a longer limit, allowing for longer mini programs than are possible on Twitter (though it should be noted that the base 2048 trick isn’t enabled for this incarnation).

Related posts