BBC BASIC
General >> Announcements >> BBC BASIC for SDL 2.0 v0.17a released
http://bbcbasic.conforums.com/index.cgi?board=announcements&action=display&num=1493635456

BBC BASIC for SDL 2.0 v0.17a released
Post by Richard Russell on May 1st, 2017, 10:44am

I've updated BBCSDL, the free cross-platform edition of BBC BASIC, to version 0.17a. The various editions may be downloaded as follows:The Android edition will also run on the Amazon Fire TV Stick. The Raspberry Pi edition requires a RPi 2 or RPi 3 running Raspbian Jessie.

The main changes in this version are as follows:
I still need volunteers to contribute to the documentation, help viewer, addin utilities and libraries.

Richard.

Re: BBC BASIC for SDL 2.0 v0.17a released
Post by Richard Russell on May 2nd, 2017, 11:39am

on May 1st, 2017, 10:44am, Richard Russell wrote:
Fixed a cache-coherency problem when using the inline ARM assembler (Android and Raspberry Pi editions). I will post separately on this issue.

BBC BASIC's integral assembler unavoidably involves writing code to 'data' memory and then executing that code. This causes a particular issue for modern processors, because typically 'data' and 'code' are cached separately.

x86 CPUs make specific provision for this situation (the so-called SMC or Self Modifying Code condition) so that when you store data in memory the associated instruction cache line is automatically flushed. This has both good and bad consequences: it means you don't need to worry about the instruction cache containing 'stale' data, but it can severely affect execution speed if you allow your assembler code to write to memory in the vicinity of the code (you will sometimes see 'guard bands' or 'gaps' declared in DIM statements to ensure sufficient separation).

The ARM CPU, in keeping with its RISC credentials, makes no specific provision for this situation. Whilst this means that writing to code memory won't have an effect on execution speed, crucially it can mean that if you try to run code immediately after having assembled it, you actually execute something completely different that happened to be in the instruction cache previously. The inevitable consequence is a catastrophic crash.

I've seen this happen on a number of occasions, so I've now modified BBCSDL v0.17a so that the ARM assembler automatically flushes the instruction cache, guaranteeing that when you run the program it really is the assembled code that gets executed. But there may still be a problem when the 'machine code' comes from somewhere other than the BBC BASIC assembler (e.g. from DATA statements or a file). I've seen programs that 'poke' code into memory using conventional indirection operators, for example:

Code:
      FOR addr% = start% TO finish% STEP 4        READ code%        !addr% = code%      NEXT addr%      CALL start% 

This may crash when executed, because the instruction cache is 'stale', and the v0.17a mod doesn't help. So I would strongly recommend that code of this sort be adapted so that it takes advantage of the modification I have made to the assembler, for example:

Code:
      FOR addr% = start% TO finish% STEP 4        READ code%        P% = addr% : [OPT 0 : EQUD code% : ]      NEXT addr%      CALL start% 

This won't be quite as fast, but that shouldn't be important (it will usually be code that is run, just once, during initialisation). It will however guarantee cache coherency.

Richard.