HiSoft Pascal Primer cover: a square, a triangle and a circle drawn and filled by a HiSoft Pascal program on the Tatung Einstein
← Back to Modules
module
18

Sound

Introduction

The Einstein has a sound chip with sixteen registers, and HiSoft Pascal gives you one procedure to reach it: PSG(R,V) puts the value V into register R. There is nothing more - no "play a note" - so this section is about which registers to use for a note, and the one register you must be careful with.

Three Registers For A Note

The chip has three channels; channel A is enough for a tune. A note on channel A needs three things.

The pitch is a number called the period - the bigger it is, the lower the note - and the note's frequency is 2,000,000 ÷ (16 × period). Middle C, 261 Hz, is period 478. A period can be bigger than 255, so it goes in two registers: the remainder after dividing by 256 in register 0, the rest in register 1:

PSG(0,P MOD 256);
PSG(1,P DIV 256);

The volume is register 8: 0 is silent, 15 the loudest.

The switch is register 7, which turns the channels on. For channel A's tone, the value is 126:

PSG(7,126);

Register 7 Is Not Only For Sound

Register 7 does a second job: its top bits set up the part of the chip that reads the keyboard. 126 leaves them as they should be. Other values that also switch tone A on do not - 62, for instance - and then the keyboard stops working: not just in your program, but at the 0: prompt afterwards. The only way back is to restart the Einstein. Use 126.

Silence

A note does not stop when the program does. Whatever the chip was playing when your program ended, it goes on playing at the 0: prompt. End every program that makes a sound with PSG(8,0).

The Code

A scale, from middle C up:

10 PROGRAM SCALE;
20 VAR I,J:INTEGER;
30 N:ARRAY[1..8] OF INTEGER;
40 PROCEDURE TONE(P:INTEGER);
50 BEGIN
60 PSG(0,P MOD 256);
70 PSG(1,P DIV 256)
80 END;
90 BEGIN
100 N[1]:=478; N[2]:=426; N[3]:=379;
110 N[4]:=358; N[5]:=319; N[6]:=284;
120 N[7]:=253; N[8]:=239;
130 PSG(7,126);
140 PSG(8,12);
150 FOR I:=1 TO 8 DO
160 BEGIN
170 TONE(N[I]);
180 FOR J:=1 TO 400 DO
190 END;
200 PSG(8,0)
210 END.

Line 180 is an empty loop that holds each note for about a quarter of a second.

Starting from

SCALE.ASC saved from XBAS; HPEIN SCALE. In MAME: sound must be on - the command in S2 leaves it on.

What you should see

Nothing on the screen - but you hear eight notes going up, C D E F G A B C, about two seconds in all, and then silence.

Change One Thing

  • Take out line 200 (and the ; at the end of line 190). What do you hear after the program ends?
  • Change line 180's 400 to 1600. How long is each note now?
  • Change line 130 to PSG(7,62);. The scale still plays - but what happens when you then type DIR? (Save everything first: you will have to restart the Einstein.)

Exercises

18.1 Play the scale downwards.

18.2 Make a siren: a note that slides up from period 400 to 200 and back, over and over, until a key is pressed (S15).

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
No soundRegister 7 not set to 126, or register 8 left at 0.
A note goes on after the programNo PSG(8,0) at the end.
The keyboard is dead, even at 0:Register 7 was given a value that turns off the keyboard. Restart the Einstein, and use 126.
The note is wrongOnly register 0 was set: a period over 255 needs register 1 as well.

Summary

PSG(R,V) writes a register of the sound chip. For a note on channel A: the period in registers 0 and 1 (frequency 2,000,000 ÷ 16 ÷ period), the volume in register 8, and 126 in register 7 - no other value, or the keyboard goes dead. Finish with PSG(8,0).

Next

S19, Reaching The Machine - PEEK, POKE, the I/O ports and machine code.

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.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.