Beginner's question about loading programs on ZX spectrum

General software. From trouble with the Banyan Tree to OCP Art Studio, post any general software chat here. Could include game challenges...
User avatar
yang
Berk
Posts: 14
Joined: Fri Jun 22, 2018 8:10 am
Location: Slovenia

Beginner's question about loading programs on ZX spectrum

Post by yang » Wed Jun 27, 2018 7:57 am

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 ?
0 x

User avatar
PeteProdge
Manic Miner
Posts: 403
Joined: Mon Nov 13, 2017 9:03 am

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

Post by PeteProdge » Wed Jun 27, 2018 8:16 am

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.
0 x

User avatar
Pegaz
Microbot
Posts: 156
Joined: Mon Nov 13, 2017 1:44 pm

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

Post by Pegaz » Wed Jun 27, 2018 9:12 am

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?
0 x

User avatar
Ast A. Moore
Manic Miner
Posts: 719
Joined: Mon Nov 13, 2017 3:16 pm

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

Post by Ast A. Moore » Wed Jun 27, 2018 9:56 am

yang wrote:
Wed Jun 27, 2018 7: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.
3 x
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.

User avatar
ZXDunny
Dizzy
Posts: 83
Joined: Tue Nov 14, 2017 3:45 pm

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

Post by ZXDunny » Wed Jun 27, 2018 10:03 am

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.
2 x

User avatar
Ast A. Moore
Manic Miner
Posts: 719
Joined: Mon Nov 13, 2017 3:16 pm

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

Post by Ast A. Moore » Wed Jun 27, 2018 10:25 am

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.
1 x
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.

hikoki
Manic Miner
Posts: 275
Joined: Thu Nov 16, 2017 10:54 am

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

Post by hikoki » Wed Jun 27, 2018 12:45 pm

It is explained on these videos. Automatic translation of subtitles in english seems quite understandable.


0 x

Joefish
Manic Miner
Posts: 315
Joined: Tue Nov 14, 2017 10:26 am

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

Post by Joefish » Wed Jun 27, 2018 12:56 pm

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.
1 x

User avatar
ZXDunny
Dizzy
Posts: 83
Joined: Tue Nov 14, 2017 3:45 pm

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

Post by ZXDunny » Wed Jun 27, 2018 3:52 pm

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.
1 x

User avatar
PeteProdge
Manic Miner
Posts: 403
Joined: Mon Nov 13, 2017 9:03 am

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

Post by PeteProdge » Wed Jun 27, 2018 3:55 pm

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?
0 x

Post Reply