BBC Basic for the Spectrum Next (Part 1)
Since the last update, I’ve added a substantial number of new features to BBC Basic for Next
- The built-in Z80 assembler now supports the additional Z80N opcodes
- Realtime clock support (if fitted)
- Three additional video modes
- Line, circle, and filled triangle graphics primitives via PLOT and LINE commands
- VDU command now works
- SOUND command now works
- File I/O via esxDOS
- Improvements to the BASIC editor
In addition to these features, there have been a number of bug fixes and optimisations, including a handful of quick Z80N optimisations in the core BASIC interpreter code.
Sadly I’ve had to abandon using my favourite emulator ZEsarUX for the time being, as there are a number of hardware features it does not currently support, as of version 9.2, including ULA hardware scrolling and relative paging for 16K screen banks. I’m currently testing this on CSpect, and verifying it on my MiSTer Next core. I will provide feedback to the author; much as I’ve come to appreciate CSpect, it’s a bit of a resource hog on my old MacBook Pro.
Text
The VDU command is unique to BBC Basic and provides a quick way to output a list of characters or control codes by ASCII value.
VDU n == PRINT CHR$(n);
In order to take full advantage of this, BBC Basic for Next has implemented the following control codes:
- VDU 8: Backspace
- VDU 9: Advance cursor one space
- VDU 10: Line feed
- VDU 12: CLS
- VDU 13: Carriage return
- VDU 17, c: COLOUR c
- VDU 18, m, c: GCOL m, c
- VDU 19, c, r, g, b: COLOUR c, r, g, b
- VDU 22, n: MODE n
- VDU 30: Home cursor
- VDU 31, x, y: TAB(x, y)
Graphics
I’ve added Next specific Layer 2 graphics modes, in addition to the standard Spectrum 48K/128K mode:
- Mode 0: 256 x 192 (32 x 24) – ULA, as per standard Sinclair Spectrum 48K/128K
- Mode 1: 256 x 192 (32 x 24) – 256 colours per pixel
- Mode 2: 320 x 256 (40 x 32) – 256 colours per pixel
- Mode 3: 640 x 256 (80 x 32) – 16 colours per pixel
There were a number of challenges porting my standard library graphics primitives to BBC Basic:
- They needed to be upgraded to 16-bit coordinates to cope with the higher screen resolutions
- All self-modifying code had to be removed as the intention is to run this code within a ROM
- The library routines are written for performance, and make no attempt to clip at the screen boundary
- The filled shape routines use a buffer to store one set of line coordinates to simplify the logic
- The graphics primitives need to work across all screen modes
These changes took a week or so to perfect. There were some performance trade-offs, but I’m happy with the end result.
Disk I/O
BBC Basic for Next can now save and load programs. I use the exsDOS system built into the Next to achieve this. The BASIC statements LOAD, SAVE and CHAIN work as per their BBC Basic counterparts. In addition, the following star commands are supported:
- *CAT (or *.): List the contents of a directory
- *DIR (or *CD): Change directory
- *DRIVE: Change drive
- *MKDIR and *RMDIR: Create and remove directories
- *ERASE (or *DELETE): Delete files
- *LOAD and *SAVE: Load and save blocks of memory
I ran into some issues initially trying to debug these in ZEsarUX – as of version 9.2 it does not appear to enable esxDOS if you compile and run it through DeZog. To overcome this, I switched to using the CSpect emulator, which enabled me to run esxDOS commands in debug mode.
Sound
The SOUND statement supports the 3 standard Spectrum 128K AY channels. Up to 5 notes can be queued in each channel at any one time, with the SOUND command only blocking when trying to insert into a full queue. The interrupt driven player is tuned to the same pitch as BBC Basic running on the Model B and Electron. There is currently no support for ENVELOPE, or the additional Next AY chips.
The editor
The editor now has full line-editing capabilities, and text can be copied from other parts of the screen using a COPY mode. The keyboard handling routines have been upgraded to support debounce and key repeat. And the cursor is now a hardware sprite, rather than an attribute block.
What next
There are not many things left to implement. There are a handful of bugs to fix and a similar number of incomplete features. Once those have been addressed, I’ll probably release this as a version 1.00.
And then the fun begins, as there are quite a few Next features that I’d like to incorporate into the BASIC, the main one being sprites. This will be interesting; the current BBC Basic IV token table is full. Adding them as ‘*’ commands is not ideal, as they only accept constants as parameters. To add any extra keywords to the interpreter whilst maintaining backwards compatibility, I will need to borrow a trick from Basic V. I expect this will continue to be a work-in-progress for a while.
I would also like this to be distributed as a ROM file, rather than a NEX file. This will involve rejigging the code so that it runs in 2x16K banks, like the 128K Spectrum. And whilst I’m doing that, I may do a 128K Spectrum spin-off version.
The latest build is available here in my github account; this code is distributed under the terms of a zlib license as requested by R.T.Russell. This includes ‘nex’ executable files that can be run in CSpect, on a MiSTer, or on a Next (or compatible).
And as ever, I’ll be posting regular updates on Twitter as @breakintoprogram.