Spectrum BASIC to SAM BASIC translation

Play it once, SAM. For old times' sake.
Post Reply
User avatar
flatduckrecords
Manic Miner
Posts: 795
Joined: Thu May 07, 2020 11:47 am
Location: Oban, Scotland
Contact:

Spectrum BASIC to SAM BASIC translation

Post by flatduckrecords »

The MGT disk format is compatible between SAM and ZX Spectrum PlusD; and SAMDOS will recognise and enumerate the various Spectrum files: CODE files (including SCREEN$) that originated on Spectrum can be loaded from a PlusD disk no problem.

Is there an elegant way to load "zxbasic" files from disk into SAM's memory?

On the SAMDOS system disk, among various utilities, MGT provided a Spectrum BASIC translator. Unfortunately it can only load from tape.



I took a look at the disassembly of the program and it seems straightforward enough: the BASIC data block is loaded into SAM memory at the correct memory page and offset as indicated by SAM's PROGP and PROG system variables (i.e. where the SAM ROM would expect a SAM BASIC program to be).



Once that's loaded, a translation subroutine (at 0x410E) is called that modifies the tokenised BASIC in-place so that the Spectrum keywords are swapped out for their SAM equivalents. Another subroutine (at 0x40BA) deals with the other system values (pointers to the BASIC variables area etc). It's not foolproof but it works really well for simple BASIC programs.

What I would like to do is load from disk rather than tape, then manually CALL the translation routine directly. SAMDOS won't allow us to simply LOAD "zxbasic" CODE, however, and it complains with "wrong file type".

What you *can* do is load raw disk sectors into memory, but of course you need to know which sectors are used by the file you want to open (and deal with any file-headers in the data):

Code: Select all

REM SAMDOS
READ AT disk, sector, track, address
MasterDOS allows you to specify how many sectors to load:

Code: Select all

REM MasterDOS
READ AT disk, sector, track, address, count
That sort-of works but it's a bit of a kludge. Have I missed something, or am I just going to have to roll my sleeves up and learn about the DOS hook codes and the UIFA?

It seems like something that would certainly have been covered in the pages of Format given the crossover of SAM and PlusD owners in the readership…but if it was I haven't come across it yet!
User avatar
Stefan
Manic Miner
Posts: 822
Joined: Mon Nov 13, 2017 9:51 pm
Location: Belgium
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by Stefan »

As an even hackier work-around you could change the file type in the directory from 1 zxbasic to 19 SAM code.
User avatar
flatduckrecords
Manic Miner
Posts: 795
Joined: Thu May 07, 2020 11:47 am
Location: Oban, Scotland
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by flatduckrecords »

Aha! Thanks @Stefan I'll give that a go.
User avatar
Stefan
Manic Miner
Posts: 822
Joined: Mon Nov 13, 2017 9:51 pm
Location: Belgium
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by Stefan »

But... with samdos I can just:

Code: Select all

load "whatever" line 9999
To load the spectrum basic file, merge "whatever" will probably also work. The file is complete gibberish since the tokens are different, so how much has been mangled is anybody's guess.
User avatar
flatduckrecords
Manic Miner
Posts: 795
Joined: Thu May 07, 2020 11:47 am
Location: Oban, Scotland
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by flatduckrecords »

Haha! Thanks @Stefan that works!

The LOAD/MERGE operation stalls at the end but you can clear it with the NMI button.

That will do nicely, cheers!
User avatar
Stefan
Manic Miner
Posts: 822
Joined: Mon Nov 13, 2017 9:51 pm
Location: Belgium
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by Stefan »

And in case that does not work, reading sectors is way easier than trying to load a file using hook codes.

I no longer remember which bytes you need, but you get the starting track and sector from the directory entry (on the first four tracks). From there on, the last two bytes of each sector point to the next track and sector.
User avatar
1024MAK
Bugaboo
Posts: 3129
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Spectrum BASIC to SAM BASIC translation

Post by 1024MAK »

If you have a Speccy, can’t you just SAVE the BASIC program as a CODE file?

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
flatduckrecords
Manic Miner
Posts: 795
Joined: Thu May 07, 2020 11:47 am
Location: Oban, Scotland
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by flatduckrecords »

Thanks @1024MAK yeah that crossed my mind as well, but as I’ve already got a bunch of programs saved I was motivated to make it work at the SAM end. And since SAMDOS has native support for the files I was curious why SAM BASIC has none at all! (And curiouser still why I couldn’t find any articles or utilities that would do it for me!)

The method described by @Stefan works well though. Even tricky listings from Outlet with loads of control characters and embedded machine code routines. (They won’t run, but they are transliterated appropriately!)
dvduk
Drutt
Posts: 36
Joined: Sat Apr 08, 2023 6:29 pm

Re: Spectrum BASIC to SAM BASIC translation

Post by dvduk »

Bit of a conveluted way - but if you could turn the spectrum basic listing natively into a LLIST generated file - you could use KEYIN to turn them into basic lines on SAM.
Can also be found at SamstersVideos - which does occasionally update when I have time...
User avatar
flatduckrecords
Manic Miner
Posts: 795
Joined: Thu May 07, 2020 11:47 am
Location: Oban, Scotland
Contact:

Re: Spectrum BASIC to SAM BASIC translation

Post by flatduckrecords »

Thanks @dvduk, cool idea. The PlusD supports "opentype" files that you can attach to a stream:

Code: Select all

REM Spectrum PlusD
OPEN #4; D1 "filename" OUT
LLIST #4
CLOSE #*4
However it seems to LIST or LLIST with tokens enabled, I'm not sure how to expand* it out to ASCII characters.

Over on SAM, MasterDOS can attach the opentype file to an input stream. I've not really used KEYIN before but I think we can get the stream data into a variable, then "KEYIN a$" — is that the general idea? That would also be neat for importing other BASICs that do support plain-text save files.

* Talking about the PlusD and printer settings? Now it *really* sounds like an article in Format magazine!
dvduk
Drutt
Posts: 36
Joined: Sat Apr 08, 2023 6:29 pm

Re: Spectrum BASIC to SAM BASIC translation

Post by dvduk »

May be possible to use something like BetaBasic on the Spectrum to allow the output as extended characters but i'm not sure.

I certainly know the redirection of listings works on SAM Basic as I used that trick many years to convert them to ASCII format - but not to sure how to do it on the Spectrum.

Yep, use Keyin on the result - may have to use a bit of stringslicing as I think the line terminator would be included, to use as a marker.
Can also be found at SamstersVideos - which does occasionally update when I have time...
User avatar
g0blinish
Manic Miner
Posts: 287
Joined: Sun Jun 17, 2018 2:54 pm

Re: Spectrum BASIC to SAM BASIC translation

Post by g0blinish »

Post Reply