Page 1 of 1

Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 8:57 am
by yang
Hello,

As I used to load the programs in the 1980s to the ZX spectrum 48k through the MC Cassette player, I never understood what is required and what is happening technically (in bits and bytes) to successfully load the program. And why are the rainbow lines visible, when loading the program? Could someone point me to an URL/thread with the explanation or explain the procedure to me, please ?

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 9:16 am
by PeteProdge
When you load a program in, the Spectrum's memory is getting filled up with instructions and information.

The bigger games take longer to load, so things like 16K games are quick to run.

As computer memory is stored in binary - ones and zeroes - the cassette tape sends these out as sounds. A low-pitch blip is a 1, a high-pitch blip is a 0. And this happens very fast. You get a feel for this when you see a loading screen appear. All the emptiness usually sounds like 'eeeeee...', big blocks are like 'oorrrr'.

When all the instructions and information is loaded, the computer jumps to a specific part of memory to run the whole thing.

I think I've got that all correct, in layman's terms.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 10:12 am
by Pegaz
What about border stripes?
ZX81 does not have them, so who has the idea to add them and how exactly are they generated while loading the program?

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 10:56 am
by Ast A. Moore
yang wrote: Wed Jun 27, 2018 8:57 am Could someone point me to an URL/thread with the explanation or explain the procedure to me, please ?
This is the ROM tape loading routine. Virtually all commercial loaders used it either directly, or created custom variations of it.

To explain how it works, I’d need to know your level of knowledge of programming in general, but here’s the gist of it.

1. Data in computers is represented as only two (binary) digits: zero and one. Eight binary digits (bit) are combined into larger units—bytes.
2. On tape, each bit is represented as a pulse of different duration. (A pulse here is simply a change in voltage over time—a wave.) As the magnetic tape passes the tape head, it induces electric current in it. Changes in current over time create changes in voltage as well, which is what the tape recorder will present on its output, amplified.
3. On the Spectrum, the shorter pulses represent a zero and longer pulses represent a one. Here’s a waveform from a real Spectrum:

Image

4. The tape loading routine doesn’t care about the polarity of the signal (i.e. whether it’s falling or rising); it only cares about the time between each zero crossing (i.e. every point the thin dark wavy blue line crosses the straight horizontal lighter blue line in the picture above). A sort of a Morse code, if you will.
5. A timer is set and is incremented at a more or less constant rate. The EAR port is then sampled continuously. Each time the signal changes polarity (crosses the zero horizontal line), the value read from the port will change from 0 to 1 or 1 to 0. The counterintuitive part that sometimes throws people off is that we don’t care whether it changes from one value to the other. We only care how much time has passed since the last change. When a change is detected, we look at our timer value, compare it to a predetermined threshold value, and decide whether the pulse we have detected is a one (long) or a zero (short).
6. We do this eight times to form a byte, keeping the intermediate results in a CPU register. When eight bits (a full byte) have been accumulated, we copy the byte from the register into RAM.
7. Rinse and repeat.

The colored stripes in the border are just a visual indicator for the end user. They are not essential to the loading process itself. Each time we detect a one or a zero, we change the border color accordingly, so the stripes more or less represent the bits coming in from the tape. We don’t have to stick to just two colors, though. We could either cycle through them or even change them randomly. Again, this is just a visual gimmick and nothing more.

Some loaders did away with that completely. Others, Academy, for instance, changed the color of a few attributes of the display file. Yet others presented you with a countdown timer, a scrolling text with instructions, etc.

The timing of the pulses (their duration) was often altered as well—typically shortened—to speed up the loading process. Many publishers did not store the data on tape “as is,” and instead encrypted it. The loader would then decrypt it on the fly, but the “mechanics” of fetching data from tape remained the same.

The only unavoidable part of the loading process is the sound itself—you can’t mute it, as the Spectrum’s ULA has essentially one single pin for driving the MIC and EAR ports and the internal speaker.

Hope this clarifies things a little.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 11:03 am
by ZXDunny
Border stripes are not actually essential to get your game loaded - they're a visual representation of the sound coming in, so you can see that all is well.

Bytes in memory (the smallest unit that can be stored) is made of a collection of 8 bits. Each bit represents a power of 2 - 1, 2, 4, 8, 16, 32, 64 and 128. If all bits are 1, then those numbers added together make 255 - the largest number you can store in a byte.

As has been mentioned, all data is loaded as bits (1s and 0s). In order to make those one and zeros, we actually have to send two at a time, in pairs. We make these by flipping the input from on to off rapidly, which is what makes the sound - it's literally a vibration. Each pair makes a bit. a 1 is a long period between flips of the input signal, a 0 is a short flip.

So for 1: ----------------______________
So for 0: -------______

If you see what I mean.

When loading, if the signal is high (flipped ON) we set the border to say, yellow. If it's flipped OFF (for the second part of the tone above) we change the border to blue. As to why we get the stripes and not flashing border colours of blue and yellow:

The display is a constantly running "beam" that moves across the screen from left to right, top to bottom, 50 times per second. So it's very, very fast. As the beam does this scanning, it constantly looks either at the state of the border colour, or the screen memory to see what it should draw. As these tones are a fair bit shorter than the time it takes for the beam to move from one side of the screen to the other, the border colour appears to change in short bursts, creating runs of colour that resemble stripes in the border.

You can see this effect by switching the border colour yourself in BASIC:

10 BORDER 1: BORDER 2: BORDER 3: BORDER 4: BORDER 5: BORDER 6: BORDER 7: PAUSE 1: GO TO 10

And you'll see coloured stripes - the difference being that BASIC cannot change the border colour as fast as the tape loader can, so the stripes appear much fatter.

And that's about it.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 11:25 am
by Ast A. Moore
A small addendum (or a bit of trivia) to my previous post: tape loading errors.

These can be detected in two different ways—while loading a block of data and after the block has loaded. The first type is relying on the same timer we use for detecting the pulse duration. If the change (zero crossing) occurs before the timer reaches a certain threshold, we consider it a zero; if it happens after the threshold is exceeded, we consider it a one. If however, the timer “overflows”—i.e. reaches a third value that is even higher (typically, because the timer is a single-byte vale, it just resets to zero after being incremented from 255), we consider that a tape loading error. In other words, we expected to receive a one or a zero pulse, but were unable to detect either.

The other way is to maintain a simple running value after loading each byte. Since we know exactly what data should reside on the tape, we can precalculate something known as the checksum for it. There are different methods of doing that, the most common being XORing each byte sequentially with the previous one. At the end of a block load, we compare this precalculated value with the value we’ve obtained by actually XORing bytes as we read them from the tape. If they don’t match, again, we consider that a tape loading error. This method is not foolproof, but for most cases it’s good enough.

Some loaders dealt with tape loading errors in very non–user friendly ways: for example, the computer would hang or reboot. Others (by yours truly, for example), allowed you to rewind the tape and simply reload the offending block with no ill-effect.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 1:45 pm
by hikoki
It is explained on these videos. Automatic translation of subtitles in english seems quite understandable.
https://youtu.be/6hXnd4z_STg

https://youtu.be/vQNYnPVoRXQ

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 1:56 pm
by Joefish
The coloured lines are just there to show you that the Spectrum is responding and reading the data signal from the tape, so you have some confidence that things are working correctly. The loading is done by code programmed into the Spectrum's ROM. It's fairly easy to copy the function into memory (RAM) and fiddle with it to speed it up or alter the colours, then run your own version.

As in Ast A. Moore's diagram, the tape signal is a series of pulses, and the width of each pulse signals a 1 or a 0 bit. Eight bits make a byte, 1024 bytes make 1kB of data, etc. The screen is 6.75kB so it needs to load 55296 bits for a loading screen.

After each pulse, the border colour is swapped between blue and yellow. It does this faster than the TV can re-draw the picture, so you see stripes of colour from where the TV started drawing a line of the border in yellow, and the Spectrum switched it to blue before the TV had finished.

You can see a similar effect of stripes in the border from BASIC - even BASIC can swap the border colour faster than the TV can redraw the whole screen.

10 PAUSE 1 : BORDER 1 : BORDER 6 : BORDER 1 : BORDER 6 : BORDER 1: BORDER 6 : BORDER 1 : GOTO 10

The reason you can hear the noise from the tape is that the Spectrum's audio in and out sockets (EAR, MIC) and the speaker are all partially linked up inside for various circuit-design reasons.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 4:52 pm
by ZXDunny
I first saw the loading sequence of a ZX81 at school and was warned by the teacher not to watch as it could affect your brain.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 4:55 pm
by PeteProdge
Hypothetically speaking, if you had a video of a MIA game being loaded yet it had no sound in the recording, would it be possible for some expert to run something that could take all those loading 'stripes' and decipher it into the actual data? Or is it truly lost?

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 5:00 pm
by Seven.FFF
I think it could be reconstructed, maybe even quite easily*.

Richard T Russell recovered the missing chroma info from the dot crawl on monochrome recordings of unfiltered TV programs, 10 years ago using a program he wrote in BBC BASIC.

Admittedly he did write the spec for BBC BASIC and worked in BBC Engineering R&D half his life, but still, it gives an idea of what is possible :D

*depending on the video source. And how much was cut off in the overscan area.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 5:43 pm
by Ralf
Reconstruct a program from loading border stripes? I'm afraid it's theoretically possible but wouldn't work correctly in practice.

Any video recording, traditional or digital, consists of frames. There are 30, 40, 50 or whatever any other number of frames recorded per second.
It works perfectly for human eye giving an illusion of movement but I'm afraid it will miss information being displayed between frames. You will have big holes of unknown data in your program.

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 10:08 pm
by 4thRock
From the PAL signal specs (if we trust Wikipedia https://en.wikipedia.org/wiki/PAL) we get:
» Total time for each line 64.000 μs

From somewhere on the Web, I got:
» normal loading pulse 0.244 milliseconds

PAL only has 576 active lines, out of 625.
So you don't record for 3136μs » 3.13600 milliseconds and there's data loss.

Please correct this if I'm wrong :lol:

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 11:11 pm
by ZXDunny
Yes, it's not possible. There is a period of "flyback" where the TV's beam resets to the top-left corner of the screen after it has reached the bottom-right - any data that is transmitted during that period is lost. Also there are no stripes drawn to the paper section of the TV screen (pixel/attr data there) so you might not be able to distinguish between 1s and 0s.

Of course, you could analyse the sounds coming from the internal beeper - but then you might as well just play the tape :)

Re: Beginner's question about loading programs on ZX spectrum

Posted: Wed Jun 27, 2018 11:14 pm
by Seven.FFF
Yes, I concur. I realised this just after I posted, and put in that qualification as an edit. But it's not just overscan, the v.flyback is a showstopper. Oh well.