The Limitation of the Beeper
The simple beeper (Part 28) can only play one square wave tone at a time. The AY-3-8910 (or compatible) Programmable Sound Generator is a dedicated chip that provides three independent tone channels, a noise generator, and volume control.
Communicating with the AY Chip
The AY chip uses a few fixed I/O ports for communication. The Z80 talks to it using a two-step process:
- Select Register: Write the address (0-15) of the internal register you want to modify.
- Write Data: Write the actual data value to that register.
Generic AY Port Example:
Port | Function |
---|---|
Control Port (A) | Selects the internal register address. |
Data Port (B) | Writes the data to the currently selected register. |
Example: Setting Channel A Volume to Full
AY_PORT_A EQU 0FFFDH ; Example: Control Port Address
AY_PORT_B EQU FFFDH ; Example: Data Port Address (Often the same address!)
SET_VOLUME:
; 1. Select the Channel A Volume Register (Register 8)
LD A, 8 ; A ← Register 8 address
OUT (AY_PORT_A), A ; Send register address to the chip
; 2. Write the volume level (15 = Max Volume, 16 = Envelope Control)
LD A, 0FH ; A ← Volume level 15 (Binary 1111)
OUT (AY_PORT_B), A ; Write the data to the chip
RET
Register Map Overview
To play music, you manipulate these key internal registers:
Register (Index) | Purpose | Data Written |
---|---|---|
R0/R1, R2/R3, R4/R5 | Tone Period (High/Low bytes) | Controls the frequency (pitch) of Channels A, B, and C. |
R7 | Mixer Control | Determines which channels play Tone and which play Noise. |
R8, R9, R10 | Amplitude/Volume | Controls the volume (0-15) of Channels A, B, and C. |
Playing a Simple Tone
To play a note on Channel A, you must write the 16-bit Tone Period value across two registers (R0 and R1), and then un-mute the channel in the Mixer Register (R7).
Optimization: Complex music is almost always driven by a Tracker or Player routine that reads a sequence of note data from a data block in memory (your music data) and updates the AY registers every V-Blank Interrupt (VBI) cycle. This ensures the music is perfectly timed and synchronized with the screen update.