Mouse In A Hole, the platform game the course builds in Xtal BASIC 4.2, running on the Tatung Einstein: the mouse mid-jump between platforms, two cats and the cheese
← Back to Courses
module
30

Noise

Introduction

The game is silent, and games in 1984 were not. This section gives it four sounds: a squeak for each piece of cheese, a blip for a jump, a hiss for being caught and a tune for getting home.

There is a catch, and it decides how every sound is made. A sound that makes the program wait while it plays makes the game stop while it plays. Three of the four have to happen while the game carries on.

The Sound Chip

The Einstein's sound comes from a chip of its own, which Tatung's manual calls the Programmable Sound Generator. It has three channels, so it can play three notes at once, and a hiss generator for noises that are not notes. Everything it does is set by sixteen numbers held inside it, called registers, numbered 0 to 15.

PSG puts a number into one of them:

PSG 8,15

puts 15 into register 8. That is all PSG does. What you hear depends on which registers hold what, and the chip carries on making that sound until you change them - after the statement has finished, after the program has finished.

The game needs these, all for the first channel, channel A:

RegisterWhat it holds
0 and 1The pitch - the fine tune and coarse tune. The bigger the number, the lower the note. The game only needs register 0, with 1 at 0
6The pitch of the hiss. Smaller is higher
7Which channels are switched on, and whether each plays a note or the hiss. 126 is "a note on channel A", 119 is "the hiss on channel A", 127 is everything off
8Channel A's volume, 0 to 15 - or 16, which means "let the envelope decide"
11 and 12How long the envelope takes
13The envelope's shape. Putting 0 here starts it

Register 7 needs a warning. Only the values from 64 to 127 are safe in it, because part of that register is not about sound at all - the machine uses it for the keyboard. The three values above are all you need.

A Sound That Dies Away

The envelope is the chip's own way of changing the volume. Give channel A volume 16 and the envelope takes charge of it; put 0 in register 13 and the volume starts at full and falls, step by step, to nothing, all by itself.

That last part is what the game needs. The chip does the fading, not the program - the PSG statements are over in a moment, and the game carries on while the sound dies away.

Here is a short note at pitch P, dying away:

1290 REM -- A SHORT NOTE AT PITCH P, DYING AWAY --
1310 PSG 0,P:PSG 1,0:PSG 11,0:PSG 12,4:PSG 8,16:PSG 7,126:PSG 13,0
1320 RETURN

Set the pitch, set the envelope's length (4 in register 12 gives a little over a tenth of a second), hand the volume to the envelope, switch on a note on channel A, and start it. The pitch comes first: set it last and you hear a moment of the previous sound's pitch before the new one.

The cheese squeaks at pitch 40, high:

440 GOSUB 500:GOSUB 560:P=40:GOSUB 1310

and the jump blips at 120, lower:

320 IF J THEN VY=15:VX=SGN(VX)*4:G=0:P=120:GOSUB 1310:RETURN

Noise

Being caught is not a note. It is a hiss, from the same envelope, a good deal longer - 20 in register 12, about two thirds of a second:

1330 REM -- A HISS, DYING AWAY --
1340 PSG 6,20:PSG 11,0:PSG 12,20:PSG 8,16:PSG 7,119:PSG 13,0
1350 RETURN

The differences from the note are register 6, which sets how high the hiss is, and 119 in register 7, which gives channel A the hiss instead of a note.

650 GOSUB 1340:LIVES=LIVES-1:TCOL 11,1:PRINT @33,2;LIVES

There is only one envelope, shared by all three channels. Start the squeak while the hiss is still dying away and the hiss is cut short. In this game that never matters.

A Tune

For getting home, and for losing, there is MUSIC, which plays a tune written as letters:

MUSIC "C1E1G1+C1R1+C5R"

The letters are notes, starting from middle C. A digit after a note sets how long it lasts - 1 is very short, 5 is a crotchet, 9 a semibreve - + before a note puts it an octave higher, and R is a rest. End the string with R, or the last note goes on sounding after the tune has finished.

TEMPO sets the speed, from 0, slowest, to 7, fastest. BASIC starts at 4.

210 SPRITE OFF 0:PRINT @17,4;"HOME!":TEMPO 6:MUSIC "C1E1G1+C1R1+C5R":TEMPO 4
200 PRINT @15,4;"GAME OVER":TEMPO 5:MUSIC "G2E2C6R":TEMPO 4:GOTO 220

Each line puts TEMPO back to 4 afterwards, because the machine does not: the speed you leave it at is still in force when the program is next run.

MUSIC does make the program wait until the tune is finished. That is why it is only used when the game is over, and there is nothing left to wait for.

BEEP, from the manual, is the simplest sound of all - one fixed note, and BEEP 5 makes it five times as long. It makes the program wait too, which is why the game does not use it.

The Code

Add lines 1290 to 1350, and change lines 200, 210, 320, 440 and 650.

200 PRINT @15,4;"GAME OVER":TEMPO 5:MUSIC "G2E2C6R":TEMPO 4:GOTO 220
210 SPRITE OFF 0:PRINT @17,4;"HOME!":TEMPO 6:MUSIC "C1E1G1+C1R1+C5R":TEMPO 4
320 IF J THEN VY=15:VX=SGN(VX)*4:G=0:P=120:GOSUB 1310:RETURN
440 GOSUB 500:GOSUB 560:P=40:GOSUB 1310
650 GOSUB 1340:LIVES=LIVES-1:TCOL 11,1:PRINT @33,2;LIVES
1290 REM -- A SHORT NOTE AT PITCH P, DYING AWAY --
1310 PSG 0,P:PSG 1,0:PSG 11,0:PSG 12,4:PSG 8,16:PSG 7,126:PSG 13,0
1320 RETURN
1330 REM -- A HISS, DYING AWAY --
1340 PSG 6,20:PSG 11,0:PSG 12,20:PSG 8,16:PSG 7,119:PSG 13,0
1350 RETURN

Starting from

LOAD "MOUSE12" from S29.

What you should hear

  • A low blip each time the mouse jumps.
  • A high squeak for each piece of cheese.
  • A hiss when a cat catches the mouse.
  • A rising tune when the mouse gets home, and a falling one when the game is lost.

The game does not pause for the blip, the squeak or the hiss.

In MAME, sound is on unless you have turned it off; if you hear nothing, check the volume on your own computer first.

Save this. SAVE "MOUSE13".

Change One Thing

  • Change P=40 in line 440 to P=20, then to P=200. Which way does the squeak go?
  • Change PSG 12,4 in line 1310 to PSG 12,40. What happens to the squeak and the blip - and does the game slow down while they play?
  • Take :TEMPO 4 off the end of line 210, win a game, and then lose one. What has happened to the losing tune?

Exercises

30.1 Make a different squeak for the last piece of cheese, so the player knows the hole is open.

30.2 Write a longer tune for getting home. Keep it to one MUSIC statement ending in R.

30.3 Make the hiss for being caught shorter and higher.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
No sound at allRegister 7 is not 126 or 119, or register 8 is 0. Check the order and the numbers in line 1310.
A note that never stopsRegister 8 got a volume (0-15) instead of 16, so the envelope is not in charge. PSG 8,0 silences it.
The last note of a tune goes on after the tuneThe MUSIC string does not end in R.
A tune plays at the wrong speedTEMPO was left changed by an earlier run. It is not put back by RUN.
The keyboard behaves oddly after a PSG 7A value below 64 in register 7. Use only 126, 119 or 127.
The game stops dead for a moment on a soundA MUSIC or BEEP in the game loop. Use the envelope.

Summary

PSG sets the sound chip's registers, and the chip keeps making whatever sound they describe. With volume 16 and the envelope started from register 13, a sound dies away on its own while the program carries on - which is how a moving game makes a noise. Register 7 chooses a note or the hiss. MUSIC plays a tune from letters, and waits until it has finished; TEMPO sets its speed and stays set.

Next

S31, Faster - where the time in the game loop goes, and how to get some back.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.