I finally achieve it!! The ZX Spectrum BASIC compiler does really work! 😉
Yes, my first home computer was the ZX Spectrum 48K, as you surely have already guessed and it left an impressive mark on my soul.

As a child, one of my dreams was to write a compiler for this machine; to have the power of transform the slow ZX Spectrum BASIC to Machine Code, and to be able to develop games or other interesting programs without having to deal with assembler language.

And so the years passed, my ZX Spectrum grew old and disappeared… 🙁 but not that dream of making up my own Spectrum BASIC Compiler. Due to my lack of knowledge at first (I studied Computer Sciences at the university) and the lack of time later it was not possible to me to make it until today (and yes, I’m a bit thrilled). 🙂

If you ever wanted your BASIC programs to be fast, I encourage you to give it a try.

Brief Information

Ok, If you’ve reached here, you are probably interested. Read this, please:

  • What is it exactly?
    It’s an SDK entirely written in python (using PLY as LALR parser) which runs on your PC.
  • What does it do?
    You write a program in a BASIC dialect using your preferred text editor, and this program translates it into ZX Spectrum (Z80) machine code.
  • Which platforms does it run on?
    It runs on every platform where python does. This includes, of course, Linux, Windows and Mac. It only requires you to have installed the python interpreter in your computer. For Windows there’s also a .MSI installer version which does not requires python, since it is already bundled with the compiler
  • How do I transfer the program to my Speccy?
    There are several ways. This tool allows you to create .bin and .tzx files. To test your program you can use a ZX Spectrum emulator (there are *tons* of them on the internet). You can also convert your TZX program to .MP3 or .WAV and record it on a CD or cassette, to load it as you always did! 😉
  • Where can I find out more?
    There are two main sites: The Wiki (in English) and the forum.
  • Where can I ask for help?
    At the forum created for that, and also at the Speccy forum.
  • Where can I download it?
    Go to the download page to get the most recent version.

For more info, please visit the Wiki F.A.Q section. I will put some tutorials there in a short period of time. The compiler keeps developing and extending, until I reach my next goal…

  1. […] ZX BASIC compiler (Spectrum): BASIC to machine code crosscompiler written in Python (Link) […]

    Like or Dislike: Thumb up 0 Thumb down 0

  2. Muchas gracias es un buen programa, oye con que programa creo los ficheros bas

    Like or Dislike: Thumb up 0 Thumb down 0

  3. Great!


    Like or Dislike: Thumb up 0 Thumb down 0

  4. Hola, Jorge

    Lo primero, muchísimas gracias a tí. Esto me anima mucho. He puesto el enlace al juego en tu comentario.

    Respecto a lo que comentas, el tamaño del heap debería modificarse desde la línea de comandos, pero me olvidé de implementarlo. 😳 Lo pondré en la versión 1.2.3 (a punto de salir). De momento, si te corre prisa, puedes usar la versión python del compilador y editar la linea #111 del archivo backend__init__.py donde se define el valor por defecto, y hacerlo más grande.

    También puedes poner estas cosa en el foro http://www.boriel.com/forum 😉 ya que allí están empezando a entrar otros usuarios, y creo que sería buena idea crear una comunidad en torno al compilador.

    De nuevo, muchas gracias!

    Like or Dislike: Thumb up 0 Thumb down 0

  5. Primero: muchisimas gracias! Estoy flipando con Tu compilador. Tanto que ya he hecho un juego (Star Trader’s Luck. Esta en WOS pero no puedo poner enlace porque estoy en un movil) y algunos programillas cutres para probar varias cosillas (UDGs, scroll en basic, etc…)

    Estoy haciendo un juego de aventura tipo elige Tu propia aventura para el concurso y tengo un problema: creo que me quedo sin memoria para el array de strings que almacena los parrafos. llega un momento en el que el texto no se almacena.

    Lei en El foro sobre el heap que tiene 4kb, pero la solucion de usar –org no tiene efecto.

    Hay otra forma de aumentar esta memoria? la parte logica del juego es muy reducida

    Like or Dislike: Thumb up 0 Thumb down 0

  6. Thanks, LT.
    I was about to ask you to put these bugs in the forum, but I see you’ve done so already.

    Will fix them as soon as possible.

    Like or Dislike: Thumb up 0 Thumb down 0

  7. Actually, while I’m here… 🙂

    –array-base=1 seems to be broken in the latest dev build. If you use that parameter it is impossible to set the very first entry in the array (position 1) although subsequent positions are okay. A compiler warning is generated but the code crashes when it runs.

    10 DIM p$(2)
    20 LET p$(1) = “A”
    30 LET p$(2) = “B”

    The compiler generates a warning for line 20 (but not line 10) when given –array-base=1. The code works okay if line 20 is removed.

    Like or Dislike: Thumb up 0 Thumb down 0

  8. Hi Boriel, just been messing around with the compiler a little with an old BASIC game I had lying around and I’m having a little trouble with the INK and PAPER commands.

    In ZX Basic you can use INK 8 and PAPER 8 when printing to specify that the character should be printed in ‘whatever colour the screen currently is’, but this doesn’t seem to be supported at the minute by the compiler.

    PRINT AT 0,0; INK 8; PAPER 8; “X”

    … would be red/blue if position 0,0 was already red/blue, green/yellow if position 0,0 was already green/yellow, etc.

    The code for setting ink and paper in the compiler appears to do an AND 7; on the passed parameter so it always gets printed as ‘black/black’.

    Is there a way around this? I’m using the very latest dev version, btw. 🙂

    Thanks for all your hard work!

    Like or Dislike: Thumb up 0 Thumb down 0

  9. Paul (#13):

    You variable declaration looks right. ❓
    Maybe it is a compiler bug. Put this question into the forum if you want to track it.

    Documentation about DIM has been written into the Wiki, here.

    Like or Dislike: Thumb up 0 Thumb down 0

  10. Kiwi (#14):

    Thanks a lot ❗ I will keep in touch with you for help in integrating ZX BASIC into Tommy Gun.

    At this moment, Tommy Gun supports syntax highlight and can compile programs with ZX Basic already. So they form a very good tándem.

    Like or Dislike: Thumb up 0 Thumb down 0

  11. Great work!
    I have had a play with the demo programs using TommyGun and its great.
    Any help you need with TommyGun support just ask I will be happy to help.
    TommyGun supports syntax highlighting for Basic already be the way.

    Like or Dislike: Thumb up 0 Thumb down 0

  12. Well done for all the hard work! This is awesome.

    Unfortunately, apart from the examples, very difficult to use because of lack of instructions and documentation. I got a few small simple programs to work, but anything bigger and I get stuck – no documentation on keywords.

    I am of course, converting Sinclair Basic programs to ZX BASIC.

    Current example:
    DIM p$(3,2) fails – and there’s no documentation on DIM!


    Like or Dislike: Thumb up 0 Thumb down 0

  13. LCD (#11):

    First of all, thank you for all your comments. For commenting about bugs or improvements, the forum is the best place.

    The documentation is still mostly missing, due to my lack of time. I will document it ASAP.

    Regarding to your suggestions:

    • The ZXBASIC alredy has an extended Poke
    • I usually use VIM on Linux and Notepad++ on Win32. So a syntax highlight file for those editors would be nice
    • Variables are defined with _variablename at the ASM level. To load a variable into a register, you should do something like LD HL, (_variablename)

    To see the ASM code the compiler generates, use the -A flag. Eg. zxb -A program.bas

    Hope I’ve helped you with my explanation. 🙄

    Like or Dislike: Thumb up 0 Thumb down 0

  14. Great tool, but it is a bit hard to use, because there is no readme with language details (the online reference is a bit incomplete, there is no description about memory, variable or label pointers, if they are included), no “what’s new”, no editor… And the compiled code seems to be slower than HiSoft Basic. I also do not like the FreeBasic syntax.
    But that’s enough negative, I’m still impressed by it. The most positive aspect is the inclusion of InLine ASM (but missing the possiblity to load a Basic Variable to a Register?: LD HL,@variable for example, but maybe I’m missing somewhat), extended POKE, Language extension. At least I want to write a Editor for it later, with included help for the syntax.
    This Compiler is extremly promising and with some improvments it could be THE tool for developing games for the Spectrum.
    Boriel, you are great, please continue you great work.

    Like or Dislike: Thumb up 0 Thumb down 0

  15. Ola,

    Looks like good stuff. I’m looking myself for a simpler python tool: one that would translate labelled basic code to line-numbered basic code. I’m writing a Basic app for a TI99 and managing line numbers is a nightmare…!

    So I’m planning to write a python translator that would take non-numbered BASIC listings and replace “LABEL FOO” by “100 REM LABEL FOO” and “GOTO FOO” or “GOSUB FOO” by “GOTO 100” or “GOSUB 100”.

    If you’ve heard of such a tool, let me know :-).


    Like or Dislike: Thumb up 0 Thumb down 0

  16. Juan: Son necesarios dos retraces porque el primero sólo garantiza que se refresca la pantalla desde el punto donde esté en ese momento, que es desconocido, hasta el final de la pantalla.

    Como puede haber aún una zona de la pantalla (la superior) sin refrescar, es necesario un segundo retrace para garantizar que se refresca toda.

    Tienes una explicación mejor en el foro de speccy.

    También he abierto un foro para el compilador que es más útil (y cómodo) que escribir las dudas aquí. 💡

    Like or Dislike: Thumb up 0 Thumb down 0

  17. Hola, Boriel.

    Muchísimas gracias por la nueva versión (el IN me hará falta para leer el teclado, je, je). Me alegro de que este proyecto siga vivito y coleando.

    Por cierto, tengo una duda sobre waitretrace: ¿por qué se hace halt dos veces en vez de una?

    Like or Dislike: Thumb up 0 Thumb down 0

  18. Juan (#6): Muchas gracias! 🙂

    Ojo, que la ultimísima versión aún no la he subido, y compila los comandos IN y OUT. También mira la Wiki pues allí iré colocando la ayuda.

    Like or Dislike: Thumb up 0 Thumb down 0

  19. Hola, Boriel.

    Muchas gracias por este estupendo compilador. Acabo de hacer unas pruebas y los tzx resultantes son rapidísimos. Voy a ver si me animo a hacer un pequeño juego con movimiento suave (a nivel de pixel) en zxbasic…

    Like or Dislike: Thumb up 0 Thumb down 0

  20. Impressive.

    I loved my old Spectrum and the thought of listening to the loading sound as an MP3 really made me happy (wierd I know).

    Keep up th good work. I work on parsers and compilers for the JVM myself.

    Like or Dislike: Thumb up 0 Thumb down 0

  21. Hi, XavSnap

    I haven’t changed the text font! Just the layout. Which part do you see different? (right sidebar text color is now green, by te way).

    Like or Dislike: Thumb up 0 Thumb down 0

  22. ZxBasic allready exist for ZX81 basic txt files.
    The Text font is different and can make confusions!

    Like or Dislike: Thumb up 0 Thumb down 0

  23. Hi!
    Jose could you contact me.

    Like or Dislike: Thumb up 0 Thumb down 0

  24. […] The ZX Basic Compiler […]

    Like or Dislike: Thumb up 0 Thumb down 0