how is load""code autostart implemented?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

how is load""code autostart implemented?

Post by 777 »

cant see anything on google about it. any help would be appreciated.
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
TheMartian
Microbot
Posts: 100
Joined: Wed Feb 03, 2021 5:18 am

Re: how is load""code autostart implemented?

Post by TheMartian »

If I'm correct, I think that *in general* you can't.

Say, imagine you LOAD "" CODE a block at $8000, you need to call USR 32768.

However, if you're loading over the system variables area, you can overwrite the variables that contain the immediate command being executed.

Say you save with:

SAVE "block" CODE 23296,30000: RANDOMIZE USR 28000

When you LOAD "" CODE, you load the program, but also the system variables and the SAVE:RANDOMIZE commands. When the LOAD routine returns, it's like you just returned from the SAVE routine, and go into the RANDOMIZE USR.

Does this answer your question?
User avatar
777
Manic Miner
Posts: 512
Joined: Fri Jun 26, 2020 11:23 am
Location: sw uk

Re: how is load""code autostart implemented?

Post by 777 »

i think so. thank you
i started programming the spectrum when i was 8 :-

1 plot rnd*255,rnd*175
2 goto 1
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: how is load""code autostart implemented?

Post by Bedazzle »

TheMartian wrote: Wed Nov 09, 2022 3:01 pm If I'm correct, I think that *in general* you can't.
In general, you can.
Let's examine Eric and the floaters loader

Image

TZX itself contains these files:

Image

So, by CLEAR it is changing where "safe" area of memory starts. It means, Basic do not touch anything after that address.
Additionally, stack is moved there.
Code block loaded from address 32722 overlaps stack area, and after loading is finished, just jumps to address, that was loaded with code block.

Here goes hex dump of beginning of file:

Image

You can see $9FD0 address, and if we examine what is there in memory...

Image

Yeah, this is decryptor. When it finishes, execution jumps to $8000, or 32768 decimal, what is equal to address, where is original game entry point.
Last edited by Bedazzle on Wed Nov 09, 2022 4:10 pm, edited 2 times in total.
TheMartian
Microbot
Posts: 100
Joined: Wed Feb 03, 2021 5:18 am

Re: how is load""code autostart implemented?

Post by TheMartian »

Oh yes I missed that case, sorry. Overwriting the stack so the return from LOAD "" CODE goes to the game.
sn3j
Manic Miner
Posts: 501
Joined: Mon Oct 31, 2022 12:29 am
Location: Germany

Re: how is load""code autostart implemented?

Post by sn3j »

TheMartian wrote: Wed Nov 09, 2022 3:01 pm When you LOAD "" CODE, you load the program, but also the system variables and the SAVE:RANDOMIZE commands. When the LOAD routine returns, it's like you just returned from the SAVE routine, and go into the RANDOMIZE USR.
I remember this back then, when one of my tapes wore out I tried to load the code somewhere else in memory and save it back to a fresh tape. Then, after franctically fiddling with my 'new'-but-just-reused tape and the recorder's audio, it finally loaded but just refused to run. Quite mind-boggling for me as I had no idea of the concept. :dance
POKE 23614,10: STOP      1..0 hold, SS/m/n colors, b/spc toggle
User avatar
patters
Manic Miner
Posts: 471
Joined: Thu Apr 11, 2019 1:06 am

Re: how is load""code autostart implemented?

Post by patters »

Interesting stuff. I remember now that I had one such tape and I used to wonder what kind of sorcery had been used :)
User avatar
Pobulous
Dynamite Dan
Posts: 1366
Joined: Wed Nov 15, 2017 12:51 pm

Re: how is load""code autostart implemented?

Post by Pobulous »

Easiest way is to do something like

10 SAVE "GAME" CODE 16384,49152
20 RANDOMIZE USR whatever

When you load the code back in it will resume from line 20.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: how is load""code autostart implemented?

Post by Ast A. Moore »

I explained it here.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
sn3j
Manic Miner
Posts: 501
Joined: Mon Oct 31, 2022 12:29 am
Location: Germany

Re: how is load""code autostart implemented?

Post by sn3j »

One of those games was Pogo.
I remember it because it was packaged with the ZX 48K here in Germany.
It featured a small Basic program that loaded a huge code chunk from 16384 to 65535.
This took around 5 minutes to load and you were praying to the recorder that it got the audio right.
If something was tripped up you mostly wouldn't notice until the last bit was read in and you were left with an error screen.
I had no idea how to hack this. And tape to tape copying used to detoriate the signal further.
The cheap cassette players that you could get hold of with your pocket money used to wear out tapes quite easily.

https://spectrumcomputing.co.uk/entry/3 ... ctrum/Pogo
POKE 23614,10: STOP      1..0 hold, SS/m/n colors, b/spc toggle
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: how is load""code autostart implemented?

Post by Dr beep »

sn3j wrote: Sun Nov 20, 2022 2:53 pm One of those games was Pogo.
I remember it because it was packaged with the ZX 48K here in Germany.
It featured a small Basic program that loaded a huge code chunk from 16384 to 65535.
This took around 5 minutes to load and you were praying to the recorder that it got the audio right.
If something was tripped up you mostly wouldn't notice until the last bit was read in and you were left with an error screen.
I had no idea how to hack this. And tape to tape copying used to detoriate the signal further.
The cheap cassette players that you could get hold of with your pocket money used to wear out tapes quite easily.

https://spectrumcomputing.co.uk/entry/3 ... ctrum/Pogo
How to hack POGO, POLE POSITION and A VIEW TO A KILL
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: how is load""code autostart implemented?

Post by Bedazzle »

Dr beep wrote: Sun Nov 27, 2022 8:36 pm How to hack POGO, POLE POSITION and A VIEW TO A KILL
Back in time real jedi did this from machine code

loading game file to address 25000
save "first" code from 25000 with length 40536
(screen can be saved separately to 6912, and crunched)

load game code again, but to an address 0 (ROM)
save "second" code from 40536 with length 8616

then these data blocks can be easily loaded and explored with any disassembler, like MONS or STS.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: how is load""code autostart implemented?

Post by Dr beep »

Bedazzle wrote: Mon Nov 28, 2022 9:49 am Back in time real jedi did this from machine code

loading game file to address 25000
save "first" code from 25000 with length 40536
(screen can be saved separately to 6912, and crunched)

load game code again, but to an address 0 (ROM)
save "second" code from 40536 with length 8616

then these data blocks can be easily loaded and explored with any disassembler, like MONS or STS.
I see a few flaws here
1) loading first blocks could cause basic to make code run which is not loaded (second block)
2) second block could overload stackpointer and make it run as well.
3) still knowledge needed to make it loadable

My program only needs pressing space and the use of a taperecorder.
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: how is load""code autostart implemented?

Post by spider »

CODE 23552, length

Can calc length although these days I cheat and truncate it with ZXBE, with care.

Here:

Tape contains loader in Basic:

Code: Select all

10 LOAD "" CODE
And the 'Basic' in the code:

Code: Select all

10 REM
20 REM BORING TEST PROGRAM
30 REM
40 POKE (RND*767),RND*127
50 IF INKEY$<>"" THEN CLS
60 GOTO 40
Download here > https://ufile.io/ppzxz0en (should stay "alive" for about 30 days) long enough for what it is.
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: how is load""code autostart implemented?

Post by Bedazzle »

Dr beep wrote: Mon Nov 28, 2022 11:59 am I see a few flaws here
Seems, you missed the whole stuff about machine code, and disassemblers.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: how is load""code autostart implemented?

Post by Dr beep »

Bedazzle wrote: Mon Nov 28, 2022 4:42 pm Seems, you missed the whole stuff about machine code, and disassemblers.
No I did not. You still need knowledge to add those together.
My "48 Kraak" does the whole job and copyable with a copyprogram that can handle larger blocks.
User avatar
chupocro
Drutt
Posts: 5
Joined: Sat Jul 22, 2023 9:24 pm
Location: Hrvatska

Re: how is load""code autostart implemented?

Post by chupocro »

There was another way of autostarting the CODE block which was not mentioned. While loading the data would overwrite ERRSP system variable and then an error was caused intentionally by the code stored on tape being one byte shorter that it should be so the parity byte was missing.
chupocro
F3 CD AF 0D CD 52 00 3B 3B E1 11 12 00 19 06 0B 7E EE
7E D7 23 10 F9 FB C9 1C 07 5E 3D 16 0B 0E 11 1D 0C 11
Post Reply