Systems - ZX-Basic Compiler: extensible cross-compiler.

Info Guide #11
ZX-Basic Compiler
 ZX-Basic Compiler,
 surprisingly simple
extensible cross-compiler

Paulo Silva (Nitrofurano/Conscience) 

   My hobby is developing different
things to support the retro scene. There's also
nostalgia and the opportunity to finally get there
to computers that were unlucky with full─
valuable support during their release. More
I'm always amazed at how modern prog is─
frames are slow on new computers, although
we could do even more interesting things
on the old hardware (which we grew up on), this
with all its limitations.
   That's why I wanted to take part in
competitions like MSXDev, CSSCGC(Crap game
compo), etc., and CSSCGC is especially important─
I was interested because most of them there─
their, so to speak, “games” were written using
Boriel's ZX-Basic Compiler, where you can use─ 
use a syntax very similar to everything
the famous Sinclair ZX Spectrum Basic.
   I tried to write a few exp─
rmental pieces of code (http://
nitrofurano.altervista.org/retrocoding/
zxspectrum ), thensent some to─
game throws on CSSCGC (probably 5-6 per
3 years, I don’t remember exactly - but they are given in
listhttp://www.boriel.com/wiki/en/
index.php/ZX_BASIC:Released_Programs).

   Quick start - just pick up a piece
code in a text editor, compile
its console command like

zxb.py -tBa helloworld.zxb

(which creates a .tap file - see
http://www.boriel.com/wiki/en/index.php/
ZX_BASIC:Zxb#Command_Line_Options ).

   ZX-Basic Compiler was quite surprising with its
compatibility: it is all written in Python
(except for assembly libraries forZ80, is─
used during cross-compilation) - I personally
I work with it under GNU/Linux. At the same time we can─
but create your own libraries and use─
install a built-in assembler that opens
the possibility that the original developer─
tchik, Jose' Rodriguez (boriel), and not pre─
intended: compile for any other
hardware on the Z80 processor. And that’s all I need
surprised, not only on the ZX-Basic forum─
worked before this, and also Haroldo Pinheiro
(haroldoop) enjoyed itthis is for development─ 
ki for Sega Master System and posted
their programs (http://www.smspower.org/
Homebrew/Index?q=$:Platform=SMS ).

   I am active on the ZX-Basic forum
(http://www.boriel.com/forum/
zx-basic-compiler/ ) and enter the information into
ZX-Basic wiki (http://www.boriel.com/
wiki/en/index.php/ZX_BASIC ) to separate the─
fight even more and try to help about─
project that I like so much.
   I started working on all sorts of hardware on the Z80
(http://www.boriel.com/wiki/en/index.php/
ZX_BASIC:Released_Programs#OTHER_HARDWARE)
and collect information on them that can
help for the project (http://
www.boriel.com/wiki/en/index.php/
ZX_BASIC:Other_architectures ) - I started with
MSX (I need to know how videos work─ 
modes on chips V9958 and V9990 ), Mattel
Aquarius and consoles like Colecovision and 
SG-1000. 
   The support for these pieces of hardware is quite tricky,
After all, it’s not enough that libraries need to be made
for their architectural features (video─
processors, sound processors, if any
yes, memory layout, I/O ports
etc.), this is still the case in most cases
need to be compiled into a ROM image file, why
ZX-Basic Compiler was completely wrong─ 
isolated, so I had to talk about it at the beginning
allocate grams for each variable specifically─
address.
   Since it was clear that every com─
The computer or set-top box has its own format le─
nts or ROMs, I'm a GNU/Linux user
I wrote a simple bash script that
could "translate" the compiled binary─
nickname in the launched image, and then at the end
launch the appropriate emulator to
check the result.

   When I saw what Jim Bagley wrote
PacManicMinerMan for arcade machine 
PacMan (in cross assembler, most likely in 
Pasmo), I decided to do something similar to 
ZX-Basic Compiler. I fiddled for a long time until 
Jim didn't explain to me what awatchdog is, oh 
which I never suspected!:D - then
I got excited to support the arch even more─
of these cars on the Z80 (http://www.boriel.com/
wiki/en/index.php/
ZX_BASIC:Released_Programs#Arcade_Systems
- now there are probably about 30 of them there).

   Among other things, there was a ZX board
Evo, which was clearly easier because 
its main firmware implements a ZX clone
Spectrum. Libraries for ATM video modes 
Turbo 2+ simply click modes, palette and 
memory.

   For example,
zxatmshadow(%11101010,1)
sets the mode320x200, this is implemented─
is in the library with the following code:

sub zxatmshadow(tvl as ubyte,tst as byte):
 asm
 ld a,(ix+7)
 and $01
 cp 0
 jp z,zxatmshadowclose
 zxatmshadowset:
 halt
;- Enable shadow ports and access to the palette: 
;- ld a,%10101011 ;;- 6912 screen,turbo 
;- ld a,%10100011 ;;- 6912 screen,noturbo 
;- ld hl,$2a53 ;;- out (c),a:ret in TR-DOS 
;- push hl 
;- jp $3d2f ;;- nop:ret in TR-DOS 
   ld a,(ix+5)
   ld bc,$bd77
   jp zxatmshadowoutdosend
  zxatmshadowoutdos:
   ld hl,$2a53;;- out (c),a:ret в TR-DOS
   push hl
   jp $3d2f;;- nop:ret в TR-DOS
  zxatmshadowoutdosend:
   call zxatmshadowoutdos
   jp zxatmshadowend
  zxatmshadowclose:
; Выключить теневые порты. 
;;- ld a,%10101011 ;;- 6912 screen,turbo 
;;- ld a,%10100011 ;;- 6912 screen,noturbo 
   ld a,(ix+5)
   ld bc,$ff77
   out (c),a
  zxatmshadowend:
 end asm
end sub

   Запись  12-битного цвета rgb (типа#C55
- Indian Red,в цвет номер3 ) будет выгля─
деть как
  zxatmpalettergb(3,$C55)
(но  значение  цвета  обрежется  до  6 бит
из-за ограничений железа)

sub zxatmpalettergb(tad as ubyte,
tvl as uinteger):'- формат: $RGB 12bit
 asm
   ld e,(ix+7)
   ld d,(ix+6)
   ld a,d
   and %10001000
   rr a
   rr a
   rr a
   ld b,a
   ld a,d
   and %00000100
   rl a
   rl a
   rl a
   or b
   ld b,a
   ld a,d
   and %01000000
   rl a
   or b
   ld b,a
   ld a,e
   and %00000100
   rl a
   rl a
   rl a
   rl a
   or b
   ld b,a
   ld a,e
   and %00001000
   rr a
   rr a
   or b
   ld b,a
   ld a,(ix+5)
   and %00001000
   or %00000001
   xor $FF
   ld c,a
   ld a,(ix+5)
   and %00000111
   out (c),a
   ld a,$ff
   ld c,a
   ld a,b
   xor $FF
   out (c),a
 end asm
end sub

   Причём,разумеется,библиотеки тоже можно
писать  на бейсике, или на смеси ассеблера
с бейсиком, как удобно программисту  и как
он умеет, и в зависимости от того, что де─
лает код и какая нужна скорость.
   Библиотеки  вставляются  через include
в главный модуль на ZX-Basic, например:

#include "library/zxatmshadow.zxbasic"

   И  вся  программакомпилируется  через
bash-скрипт, например:

rm exampleO1.scl
cp library/boot_zmakebas.bas boot.bas
zmakebas -r -o boot.B boot.bas
rm boot.bas
mctrd cscl exampleO1.scl
mctrd add boot.B exampleO1.scl -b -a 10
zxb.py --org=28672 exampleO1.zxbasic
mv exampleO1.bin exec1.C
mctrd add exec1.C exampleO1.scl
rm *.B *.C
wine /opt/wine/emulators/UnrealSpeccy/
               unreal.exe exampleO1.scl &

   Этот скрипт подготавливает  образ диска
со скомпилированной программой (и всем,что
ей нужно), а потом автоматически запускает
эмулятор с этим диском.

   Для токенизации бейсик-загрузчика испо─
льзуется  zmakebas. Файлboot_zmakebas.bas
выглядит так:

10 clear 28671:border 0:cls
20 randomize usr 15619:rem:load"a:exec1"
code 28672
30 randomize usr 28672

   Mctrd is a console utility for co─
building images.scl or .trd and copying
files in them.  Everything else is commands
bash. Even if you are unfamiliar with Unix and 
bash, such scripts can be easily written─ 
available for other operating systems.

   Slideshow loader for IVP'2014 ATM
Turbo/ZX Evo gfx compo, which I did, 
works a little differently: the whole main part─
bot makes tokenized BASIC from
zmakebas, and the compiled BASIC from 
ZX-Basic Compiler only changes the screen 
mode and displays the picture. Slideshow is used─
It takes regular 4-bit .bmp files.

  -boot.bas (tokenized in zmakebas ):

10 clear 28671:border 0:cls
11 randomize usr 15619:rem:load"a:amtm"
code 32768
20 let amnt=0:for i=0 to 2:
letamnt=(amnt*10)+(peek(32768+i)-48):
next i
21 let dltm=0:for i=4 to 7:
let dltm=(dltm*10)+(peek(32768+i)-48):
next i
22 randomize usr 15619:rem:load"a:setscO"
code 28672
30 for l=1 to amnt
31 let w$=str$(1000+l):let w$=w$(2 to)
32 out 65527,255:out 32765,24+0
33 randomize usr 15619:rem:load"a:"+w$
code 32768
34 randomize usr 28672
35 pause dltm
36 next l
37 goto 30

  - компилирующий bash-скрипт.sh:

rm slideshow_ipv2014_evolutiongfxcompo.scl
zmakebas-r -o boot.B boot.bas
mctrd cscl
  slideshow_ipv2014_evolutiongfxcompo.scl
mctrd add boot.B
  slideshow_ipv2014_evolutiongfxcompo.scl
  -b -a 10
# zxb.py -t --org=28672 setscreenO.zxbasic
zxb.py --org=28672 setscreenO.zxbasic
mv setscreenO.bin setscO.C
mctrd add setscO.C
  slideshow_ipv2014_evolutiongfxcompo.scl
cp amtm.txt amtm.C
mctrd add amtm.C
  slideshow_ipv2014_evolutiongfxcompo.scl
rm *.B *.C
a=1;for i in pics/*.bmp;do new=$(printf
 "%03d.C" ${a});cp ${i} ${new};let a=a+1;
 done
for i in *.C;do mctrd add $i
 slideshow_ipv2014_evolutiongfxcompo.scl;
 done
rm *.B *.C
xpeccy
  slideshow_ipv2014_evolutiongfxcompo.scl
#wine /opt/wine/emulators/UnrealSpeccy/ 
   unreal.exe
   slideshow_ipv2014_evolutiongfxcompo.scl

   (чтобы  скопировать  картинки  на образ
диска.scl,  используется  цикл  "a=1; for
i in pics/*.bmp;  do new=$(printf "%03d.C"
${a});cp ${i} ${new};let a=a+1; done", это
картинки в 4-битном .bmp-формате.)

  Одна из будущих целей Boriel's ZX Basic
Compiler- распространить  его  на  другие 
процессорные  архитектуры, кроме Z80: на 
6502, 8080, 6809, 6800и т.п. Это бы  сде─
made it a real multi-purpose cross-
compiler. The task is not easy, since it
uses a lot of registerIX,a architecture 
type6502and8080do not have such a register. 
Maybe someone would like to make branches
for this or even implements multiarchitem─
texture compilation?

Share your thoughts about the article