Advent of Code 2023: CBASIC Compiler
After my post on Turbo Pascal et al, someone suggested that I try using the Digital Research CBASIC compiler – so I have done!

The CBASIC dialect seems like a mashup of ideas from other BASICs, FORTRAN and Pascal. For example, line numbers are only required if they’re going to be used as a label and there’s a WHILE loop available. It’s still true to its BASIC roots as it remains painful to nest IF..THEN..ELSEs, which provides lots of “encouragement” to make use GOTOs. The manual is adequate, although the examples lack context. The absence of a section describing the compiler error message codes is annoying (perhaps that’s somewhere else). It’s still better than most Microsoft documentation!
Adding this compiler into my benchmark table, I now have:
| | Turbo Pascal 3.01A | FTL Modula-2 1.30 | Aztec C 1.06D | CBASIC80 2,0 |
| Primes less than 1000 | 13 seconds | 17 seconds | 35 seconds | 38 seconds |
| Day 1, part 1 (Trebuchet?!) | 18 seconds | 10 seconds | 16 seconds | 24 seconds |
| Day 1, part 2 (Trebuchet?!) | 47 seconds | 178 seconds | 107 seconds | 99 seconds |
| Day 15, part 1 (Lens Library) | 57 seconds | N/A | 30 seconds | N/A |
Advent of Code 2023 repository
Prime number generators / other programs for the RC2014/RC2040
Postscript – 4th February 2024
I noticed that the output for the primes up to 10,000 was wrong, as it stated there were two more primes than there should be (1,231 instead of 1,229). Multiple re-writes of the algorithm improved the speed a little, but not the problem. Replacing the square root check with a (high) hard-coded value solved the issue. Hmmm.
This code:
FOR I% = 1 to 10000
PRINT "Square root of ";I%;" is ";SQR(I%)
NEXT I%
illustrates that the problem lies with the inbuilt SQR routine (it has the same problem if the variable is explicitly typed as a REAL). It’s … more of a random number generator!

Postscript – 6th February 2024
It looks as if the problem may be in the Z80 emulation code on the RC2040. Running identical code with the same CBASIC compiler on a RC2014 works perfectly (and faster – 1,000 primes are generated in 22 seconds using identical CBASIC code).
Having had a quick rummage in the Z80 emulator code used by the RC2024, I think I can see where the problem may lie. Time to get the emulator source code and pico toolchain installed on one of my Pi’s I think …