Adventure Helpline Issue 49 Contents Animator 1

HEWSON'S HELPLINE


Mr Hewson

Inside the 128

Hewson looks at the new RAM

AS I WRITE Sinclair has announced its new machine. I have known about it for some time, in common with a number of other software companies in the UK. However, we have all been bound by agreements not to breathe a word about it to the public.

Now the gag is off I thought you might like to know a bit more about using the machine for writing programs. The principle advantages are the extra memory which is available, the second screen memory and the new sound chip.

The extra RAM memory is a little difficult to get at, even when writing in machine code. This is because the total of 128K of RAM is divided into eight banks of 16K but the Z80 microprocessor can only address 64K of memory at a time, and that includes the 16K of ROM at the bottom of the address space.

In the new computer there are ten possible banks of 16K to be addressed, two banks of ROM and eight banks of RAM. The bottom 16K of address space is always used by one or other of the two ROMs, the next 16K can only be used by RAM bank number five, the next by RAM bank number two and the top 16K by any one of the eight RAM banks. Thus it is possible, although not particularly sensible, to have either of RAM bank numbers five and two accessible to the Z80 at different places in the memory simultaneously.

What would be more useful would be to have any one of the eight RAM banks appearing at any one of the three possible positions above the ROM, or, better still, appearing instead of one of the ROMs. Instead we are stuck with shuffling data back and forth via the top 16K of the memory map.

A new facility which will be of interest to machine code programmers is the second bank of RAM which may be used to hold the screen display. In the Spectrum the screen always resides at address 16348 - hex 4000. In the new machine this part of memory is in RAM bank number five. However, the new machine also supports the use of RAM bank number seven for screen memory.

Unfortunately RAM bank number seven may only appear at the top of memory as explained above. This does suggest the possibility of building a picture off-screen and switching it in more or less instantaneously, rather than copying it laboriously to 16384.

Those readers who are well-informed about machine-code programming will know that, because the hardware needs to get at the screen memory to maintain the screen picture 50 times per second, programs run more slowly when placed in the lowest RAM bank. This problem affects only one of three banks of RAM in the ordinary Spectrum but four out of the eight banks in the 128.

The new sound chip is a great blessing because it allows the programmer to set up some effects and then go away and get on with executing some other code rather than, as with the Spectrum, forever having to go back and toggle the loudspeaker at the correct moment. The effect transmitted through the TV speaker is also rather better than the old telephone-in-a-card board-box generated by the Spectrum.

Of course the price to pay for better sound is the extra work involved in setting up the registers with channel, amplitude, tone and envelope data. The manufacturers have identified a n entry point in the second ROM by which the machine code programmer can access the music system provided by the new Basic Play command. That is similar in principal to the way in which RST 16 can be used in the ordinary Spectrum ROM to use the facilities of the Print command from machine code.


Numbers which won't add up

THERE'S ONE TOPIC which appears in my postbag with the same kind of regularity as those infernal competition offers from Reader's Digest. Every mortal has his cross to bear, and the rounding error seems to be mine.

On this particular occasion it is John Jennings of Staines who is suffering from rounding errors. He clearly did not read my article in the May 1982 issue of Sinclair User when I first wrote about the problem! He includes a copy of the program which is giving him trouble and I have listed it in table one. The screen display when the program is executed is shown in table two.

Table 1. John Jennings' test program
demonstrating the effect of rounding errors

10 LET X=3
20 FOR I=1 TO 10
30 LET X=X-0.5
40 PRINT X
50 IF X=2.5 THEN PRINT "Two point five"
60 IF X=-1 THEN PRINT "Minus one"
70 NEXT I
The output from the program listed
in Table 1

2.5
Two point five
2
1.5
1
0.5
2.3283064E-10
-0.5
-1
-1.5
-2

John writes: I typed this test program into the Spectrum Plus demonstration model at a local branch of W H Smith and got the same result as with my own machine.

I really want to know whether there is a fault in my machine or whether this is a quirk of the Spectrum.

It is not a fault with the machine and it is only a quirk of the Spectrum in the sense that the computer tries in a rather half-hearted fashion to hide a problem with the nature of arithmetic from ordinary users. The problem is related to the fact that it is impossible to express some fractions exactly without rounding errors.

For example, we all know the following statements are incorrect:

1/3 = 0.3
1/3 = 0.33
1/3 = 0.333

No matter how many trailing threes we place at the end of the number the result is still incorrect, even if only slightly. We are so familiar with this example that we no longer consider it worthy of comment or investigation.

In this regard we are no different from the ancient Romans who no doubt knew that their system of numerals was cumbersome but were happy to persist with it.

Unfortunately, because the decimal representation serves us well in everyday life we somehow assume that the inaccuracy in the number one-third is a property of the number rather than the method by which we choose to represent it. In fact it is easy to show that it is the method of representation which is at fault, not the number itself.

If we had been created with six fingers on each hand rather than five then we would presumably count in groups of 12. In maths jargon we would count to base 12. When counting to base 12 the following statement is true:

1/3 = 0.4

That is easy to understand if you remember that the column following the decimal point when counting in base 12 is the twelfths column, and four-twelfths equals one-third.

However, counting to base 12 is not immune to problems. The fraction one-fifth, for example, which is equal to 0.2 in decimal can only be exactly represented in base 12 by 0.24972497 ... followed by endless repetition of the four digits 2497.

No matter what base you choose there will always be some fractions which cannot be represented exactly. There are even some numbers - such as pi, the ratio of the circumference of a circle to its diameter - which cannot be exactly represented in any base.

Of course those inexactitudes do not normally matter because we are happy to accept a number which is approximately correct as exact The trick is to build in acceptance of tiny but irrelevant differences. In particular you should not, as John Jennings has done in line 50 and 60 of his program, demand equality in any comparison.

The program illustrates how the Spectrum disguises the problem of rounding errors in is printing routine except for numbers which are very close to zero. Thus we do not know if there are rounding errors in the calculation of 2, 1.5, 1, 0.5, -0.5, -1, -1.5 and -2 because the print routine rounds off the numbers to the first half dozen or so significant figures, discarding any rounding error as it does so.

But the number zero has no leading significant figures by definition, so the Spectrum has no way of knowing where truth ends and rounding error begins.

Hence it prints what it has stored away, which is a number which is not quite zero but is smaller than one divided by a thousand million. The poor old computer, because it isn't human, doesn't realise that John Jennings is going to be very perplexed by this result.



How random is random?

THIS MONTH'S LOOK at the system variables focuses on the SEED variable stored at address 23670. This number is used by the RND function to kick off the next random number calculation.

Creating random numbers in a computer is a funny business because strictly speaking it cannot be done at all. A computer is a machine which executes a series of stored instructions and given that the starting point is the same and the instructions are the same it will always obtain the same result. A random element is not permitted to creep in.

The usual way of getting round this problem is to use a sequence of instructions, or algorithm, which when fed a number, chops it around and rearranges its component parts rather like shuffling a pack of cards. ln the jargon of the trade this is called 'hashing.'

The aim is to create a pseudo random number and a seed number. The seed can then be fed through the same algorithm to produce the next random number. Sometimes the seed for the next calculation is the preceding random number. Mathematicians have had fun in the past devising a number of hashing algorithms and then proving that the sequence of numbers created by them are more or less random.

The Spectrum random number generator uses the calculation:

Random number=(75*(SEED+1)-1)/65536

You can prove that by choosing some whole number as a seed and then comparing the result of the calculation with:

10 RANDOMIZE SEED
20 PRINT RND

To finish off the exercise keep an eye on the SEED system variable at address 23670. You will see that, as its name suggests, it is this location which is used to store the seed for the next calculation.



Adventure Helpline Issue 49 Contents Animator 1

Sinclair User
April 1986