544 Classic 8-bit era Programmable Sound Generator AY-3-8913

544 : Classic 8-bit era Programmable Sound Generator AY-3-8913

Design render
  • Author: ReJ aka Renaldas Zioma
  • Description: The AY-3-8913 is a 3-voice programmable sound generator (PSG) chip from General Instruments. The AY-3-8913 is a smaller variant of AY-3-8910 or its analog YM2149.
  • GitHub repository
  • Clock: 2000000 Hz

How it works

This Verilog implementation is a replica of the classical AY-3-8913 programmable sound generator. With roughly a 1500 logic gates this design fits on a single tile of the TinyTapeout.

The goals of this project

  1. closely replicate the behavior and eventually the complete design of the original AY-3-891x with builtin DACs
  2. provide a readable and well documented code for educational and hardware preservation purposes
  3. leverage the modern fabrication process

A significant effort was put into a thorough test suite for regression testing and validation against the original chip behavior.

Chip technical capabilities

  • 3 square wave tone generators
  • A single white noise generator
  • A single envelope generator able to produce 10 different shapes
  • Chip is capable to produce a range of waves from a 30 Hz to 125 kHz, defined by 12-bit registers.
  • 16 different volume levels

Registers The behavior of the AY-3-891x is defined by 14 registers.

Register Bits used Function Description
0 xxxxxxxx Channel A Tone 8-bit fine frequency
1 ....xxxx ---//--- 4-bit coarse frequency
2 xxxxxxxx Channel B Tone 8-bit fine frequency
3 ....xxxx ---//--- 4-bit coarse frequency
4 xxxxxxxx Channel C Tone 8-bit fine frequency
5 ....xxxx ---//--- 4-bit coarse frequency
6 ...xxxxx Noise 5-bit noise frequency
7 ..CBACBA Mixer Tone and/or Noise per channel
8 ...xxxxx Channel A Volume Envelope enable or 4-bit amplitude
9 ...xxxxx Channel B Volume Envelope enable or 4-bit amplitude
10 ...xxxxx Channel C Volume Envelope enable or 4-bit amplitude
11 xxxxxxxx Envelope 8-bit fine frequency
12 xxxxxxxx ---//--- 8-bit coarse frequency
13 ....xxxx Envelope Shape 4-bit shape control

Square wave tone generators Square waves are produced by counting down the 12-bit counters. Counter counts up from 0. Once the corresponsding register value is reached, counter is reset and the output bit of the channel is flipped producing square waves.

Noise generator Noise is produced with 17-bit Linear-feedback Shift Register (LFSR) that flips the output bit pseudo randomly. The shift rate of the LFSR register is controller by the 5-bit counter.

Envelope The envelope shape is controlled with 4-bit register, but can take only 10 distinct patterns. The speed of the envelope is controlled with 16-bit counter. Only a single envelope is produced that can be shared by any combination of the channels.

Volume Each of the three AY-3-891x channels have dedicated DAC that converts 16 levels of volume to analog output. Volume levels are 3 dB apart in AY-3-891x.

Historical use of the AY-3-891x

The AY-3-891x family of programmable sound generators was introduced by General Instrument in 1978. Soon Yamaha Corporation licensed and released a very similar chip under YM2149 name.

Both variants of the AY-3-891x and YM2149 were broadly used in home computers, game consoles and arcade machines in the early 80ies.

The AY-3-891x chip family competed with the similar Texas Instruments SN76489.

The original pinout of the AY-3-8913

The AY-3-8913 was a 24-pin package release of the AY-3-8910 with a number of internal pins left simply unconnected. The goal of AY-3-8913 was to reduce complexity for the designer and reduce the foot print on the PCB. Otherwise the functionality of the chip is identical to AY-3-8910 and AY-3-8912.

           ,--._.--.
    GND ---|1    24|<-- /cs*
   BDIR -->|2    23|<--  a8*
    BC1 -->|3    22|<-- /a9*
    DA7 <->|4    21|<-- /RESET
    DA6 <->|5    20|<-- CLOCK
    DA5 <->|6    19|--- GND
    DA4 <->|7    18|--> CHANNEL C OUT
    DA3 <->|8    17|--> CHANNEL A OUT
    DA2 <->|9    16|    not connected
    DA1 <->|10   15|--> CHANNEL B OUT
    DA0 <->|11   14|<-- test*
  test* <--|12   13|<-- VCC
           `-------'
    * -- omitted from this Verilog implementation

Difference from the original hardware

This Verilog implementation is a completely digital and synchronous design that differs from the original AY-3-8913 design which incorporated internal DACs and analog outputs.

Audio signal output While the original chip had no summation The module provides two alternative outputs for the generated audio signal:

  1. digital 8-bit audio output suitable for external Digital to Analog Converter (DAC)
  2. pseudo analog output through Pulse Width Modulation (PWM)

Master output channel In contrast to the original chip which had only separate channel outputs, this implementation also provides an optional summation of the channels into a single master output.

No DC offset This implementation produces output 0/1 waveforms without DC offset.

No /A8, A9 and /CS pins The combination of /A8, A9 and /CS pins orginially were intended to select a specific sound chip out the larger array of devices connected to the same bus. In this implementation this mechanism is omitted for simplicity, /A8, A9 and /CS are considered to be tied low and chip behaves as always enabled.

Synchronous reset and single phase clock The original design employed 2 phases of the clock and asynchronous reset mechanism for operation of the registers.

To make it easier to synthesize and test on FPGAs this implementation uses single clock phase and synchronous reset for registers.

The reverse engineered AY-3-891x

This implementation would not be possible without the reverse engineered schematics and analysis based on decapped AY-3-8910 and AY-3-8914 chips.

Explain how your project works

How to test

Summary of commands to communicate with the chip

The AY-3-8913 is programmed by updating its internal registers via the data bus. Below is a short summary of the communication protocol of AY-3-891x. Please consult AY-3-891x Technical Manual for more information.

BDIR BC1 Bus state description
0 0 Bus is inactive
0 1 (Not implemented)
1 0 Write bus value to the previously latched register #
1 1 Latch bus value as the destination register #

Latch register address First, put the destination register adress on the bus of the chip and latch it by pulling both BDIR and BC1 pins high.

Write data to register Put the desired value on the bus of the chip. Pull BC1 pin low while keeping BDIR pin high to write the value of the bus to the latched register address.

Inactivate bus by pulling both BDIR and BC1 pins low.

Register Format Description Parameters
0,2,4 ffffffff A/B/C tone period f - low bits
1,3,5 0000FFFF ---//--- F - high bits
6 000fffff Noise period f - noise period
7 00CBAcba Noise / tone per channel CBA - noise off,
cba - tone off
8,9,10 000Evvvv A/B/C volume E - envelope on,
v - volume level
11 ffffffff Envelope period f - low bits
12 FFFFFFFF ---//--- F - high bits
13 0000caAh Envelope Shape c - continue, a - attack, A - alternate, h - hold

Note frequency

Use the following formula to calculate the 12-bit period value for a particular note:

$ tone period_{cycles} = clock_{frequency} / (16_{cycles} * note_{frequency}) $

For example 12-bit period that plays 440 Hz note on a chip clocked at 2 MHz would be:

$ tone period_{cycles} = 2000000 Hz / (16_{cycles} * 440 Hz) = 284 = 11C_{hex} $

An example to play a note at a maximum volume

BDIR BC1 DA7..DA0 Explanation
1 1 xxxx0000 Latch tone A coarse register address $0 = 0000_{bin}$
1 0 xxxx0001 Write high 4-bits of the 440 Hz note $1 = 0001_{bin}$
1 1 xxxx0001 Latch tone A fine register address $1_{dec} = 0001_{bin}$
1 0 00011100 Write low 8-bits of the note $1C_{hex} = 00011100_{bin}$
1 1 xxxx1000 Latch channel A volume register address $8 = 1000_{bin}$
1 0 xxx01111 Write maximum volume level $15_{dec} = 1111_{bin}$ with the envelope disabled
Timing diagram

CLK   ____      ____      ____      ____      ____      ____         
   __/    `____/    `____/    `____/    `____/    `____/    `____ ...
    |         |         |         |         |         |
    |         |         |         |         |         |

BDIR ______    ______    ______    ______    ______    ______
   _/      `__/      `__/      `__/      `__/      `__/      `__

BC1  _______             _______            ________
   _/       `___________/       `__________/        `___________

DA7..DA0_____  ________  ________  ________  ________  ________
   _/  0000  `/xxxx0001`/  0001  `/00011100`/  1000  `/xxx01111`
      latch     write     latch      write    latch     

Externally configurable clock divider

SEL1 SEL0 Description Clock frequency
0 0 Standard mode, clock divided by 8 1.7 .. 2.0 MHz
1 1 -----//----- 1.7 .. 2.0 MHz
0 1 New mode for TT05, no clock divider 250 .. 500 kHZ
1 0 New mode for TT05, clock div. 128 25 .. 50 MHz
SEL1 SEL0 Formula to calculate the 12-bit tone period value for a note
0 0 $clock_{frequency} / (16_{cycles} * note_{frequency})$
1 1 -----//-----
0 1 $clock_{frequency} / (2_{cycles} * note_{frequency})$
1 0 $clock_{frequency} / (128_{cycles} * note_{frequency})$

External hardware

The data bus of the AY-3-8913 chip has to be connected to microcontroller and receive a regular stream of commands. The AY-3-8913 produces audio output and has to be connected to a speaker. There are several ways how the overall schematics can be established.

8-bit parallel output via DAC One option is to connect off the shelf data parallel Digital to Analog Converter (DAC) for example Digilent R2R Pmod to the output pins and route the resulting analog audio to piezo speaker or amplifier.

uController             AY-3-8913  
,---------.            ,---._.---. 
|         |    2 Mhz ->|CLK  SEL0|<-- 0
|    GPIOx|----------->|BC1  SEL1|<-- 0
|    GPIOx|----------->|BDI      |         ,----------.
|    GPIOx|----------->|DA0  OUT0|-------->|LSB       |
|    GPIOx|----------->|DA1  OUT1|-------->|          |  
|    GPIOx|----------->|DA2  OUT2|-------->|   pDAC   |  Headphones
|    GPIOx|----------->|DA3  OUT3|-------->|    or    |      or    
|    GPIOx|----------->|DA4  OUT4|-------->| RESISTOR |    Buzzer    
|    GPIOx|----------->|DA5  OUT5|-------->|  ladder  |         /|
|    GPIOx|----------->|DA6  OUT6|-------->|          |     .--/ |
|    GPIOx|----------->|DA7  OUT7|-------->|MSB       |-----|    |
`---------'            `---------'         `----------'     `--` |
                                                             |  `|
                                                             |
                                                        GND ---  

AUDIO OUT through RC filter Another option is to use the Pulse Width Modulated (PWM) AUDIO OUT pin that combines 4 channels with the Resistor-Capacitor based low-pass filter or better the Operation Amplifier (Op-amp) & Capacitor based integrator:

uController             AY-3-8913
,---------.            ,---._.---. 
|         |    2 Mhz ->|CLK  SEL0|<-- 0
|    GPIOx|----------->|BC1  SEL1|<-- 0
|    GPIOx|----------->|BDIR     |
|    GPIOx|----------->|DA0      |
|    GPIOx|----------->|DA1      |
|    GPIOx|----------->|DA2      |          C1
|    GPIOx|----------->|DA3      |     ,----||----.
|    GPIOx|----------->|DA4      |     |          | 
|    GPIOx|----------->|DA5      |     |  Op-amp  |        Speaker     
|    GPIOx|----------->|DA6 AUDIO|     |   |X     |            /|
|    GPIOx|----------->|DA7  OUT |-----+---|-X    |   C2   .--/ |
`---------'            `---------'         |  }---+---||---|    |
                                        ,--|+/             `--` |
                                        |  |/               |  `|
                                        |                   |
                                   GND ---             GND ---  

Separate channels through the Op-amp The third option is to externally combine 4 channels with the Operational Amplifier and low-pass filter:

uController             AY-3-8913
,---------.            ,---._.---. 
|         |    2 Mhz ->|CLK  SEL0|<-- 0
|    GPIOx|----------->|BC1  SEL1|<-- 0
|    GPIOx|----------->|BDIR     |
|    GPIOx|----------->|DA0      |
|    GPIOx|----------->|DA1      |
|    GPIOx|----------->|DA2      |           C1
|    GPIOx|----------->|DA3      |      ,----||----.
|    GPIOx|----------->|DA4      |      |          | 
|    GPIOx|----------->|DA5     A|---.  |  Op-amp  |        Speaker
|    GPIOx|----------->|DA6     B|---+  |   |X     |            /|
|    GPIOx|----------->|DA7     C|---+--+---|-X    |   C2   .--/ |
`---------'            `---------'          |  }---+---||---|    |
                                         ,--|+/             `--` |
                                         |  |/               |  `|
                                         |                   |
                                    GND ---             GND ---  

IO

#InputOutputBidirectional
0DA0 - multiplexed data/address bus LSBaudio out (PWM)(in) **BC1** bus control
1DA1 - multiplexed data/address busdigita audio LSB(in) **BDIR** bus direction
2DA2 - multiplexed data/address busdigita audio (in) **SEL0** clock divider
3DA3 - multiplexed data/address busdigita audio (in) **SEL1** clock divider
4DA4 - multiplexed data/address busdigita audio (out) channel A (PWM)
5DA5 - multiplexed data/address busdigita audio (out) channel B (PWM)
6DA6 - multiplexed data/address busdigita audio (out) channel C (PWM)
7DA7 - multiplexed data/address bus MSBdigita audio MSB(out) AUDIO OUT master (PWM)

Chip location

Controller Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux Mux tt_um_chip_rom (Chip ROM) tt_um_factory_test (TinyTapeout Factory Test) tt_um_MichaelBell_tinyQV (TinyQV Risc-V SoC) tt_um_urish_silife_max (Game of Life 8x32 (siLife)) tt_um_vc32_cpu (VC 16-bit CPU) tt_um_tinytapeout_logo_screensaver (VGA Screensaver with Tiny Tapeout Logo) tt_um_htfab_rotfpga2 (ROTFPGA v2a) tt_um_htfab_latch_test (Latch test) tt_um_no_time_for_squares_tommythorn (No Time For Squares, IHP edition) tt_um_tommythorn_maxbw (Asynchronous Multiplier) tt_um_urish_simon (Simon Says memory game) tt_um_htfab_rotfpga2_ff (ROTFPGA v2b) tt_um_meiniKi_ttihp_fazyrv_exotiny (FazyRV-ExoTiny) tt_um_rebeccargb_hardware_utf8 (Hardware UTF Encoder/Decoder) tt_um_rebeccargb_universal_decoder (Universal Binary to Segment Decoder) tt_um_algofoogle_raybox_zero (raybox-zero TTIHP0p2 edition) tt_um_z2a_rgb_mixer (RGB Mixer demo) tt_um_vga_clock (VGA clock) tt_um_frequency_counter (Frequency counter) tt_um_brandonramos_VGA_Pong_with_NES_Controllers (VGA Pong with NES Controllers) tt_um_demosiine_sda (DemoSiine) tt_um_toivoh_demo_deluxe (Sequential Shadows Deluxe [TT08 demo competition]) tt_um_edwintorok (Rounding error) tt_um_2048_vga_game (2048 sliding tile puzzle game (VGA)) tt_um_hpretl_spi (SPI Test) tt_um_top_mole99 (One Sprite Pony) tt_um_urish_spell (SPELL) tt_um_rebeccargb_vga_pride (VGA Pride) tt_um_autosel (I2C EEPROM Project Selection) tt_um_a1k0n_nyancat (VGA Nyan Cat) tt_um_a1k0n_vgadonut (VGA donut) tt_um_rebeccargb_colorbars (Color Bars) tt_um_crispy_vga (Crispy VGA) tt_um_kbeckmann_flame (Flame demo) tt_um_jamesrosssharp_1bitam (1bit_am_sdr) tt_um_simon_cipher (simon_cipher) tt_um_htfab_bouncy_capsule (Bouncy Capsule) tt_um_phansel_laplace_lut (Experiment Number Six: Laplace LUT) tt_um_kianv_bare_metal (KianV RISC-V RV32E Baremetal SoC) tt_um_calonso88_rsa (8 bit RSA encryption) tt_um_silice (Warp) tt_um_rejunity_vga_test01 (VGA Drop (audio/visual demo)) tt_um_a1k0n_demo (Demo by a1k0n) tt_um_MichaelBell_canon (TT08 Pachelbel's Canon demo) tt_um_htfab_caterpillar (Simon's Caterpillar) tt_um_ravenslofty_chess (Chess) tt_um_fountaincoder_top_V2 (maddihp) tt_um_tomkeddie_a (VGA Experiments in Tennis) tt_um_MichaelBell_mandelbrot (VGA Mandelbrot) tt_um_MichaelBell_rle_vga (RLE Video Player) tt_um_jayjaywong12 (mulmul) tt_um_wokwi_392873974467527681 (PILIPINASLASALLE) tt_um_froith_goldcrest (Goldcrest RISC-V) tt_um_dvxf_dj8v (DJ8 8-bit CPU) tt_um_hpretl_minilogix (Minilogix) tt_um_tomkeddie_b (Transmit UART) tt_um_joerdsonsilva_modem (Multimode Modem) tt_um_oled_frequency_counter (Frequency Counter SSD1306 OLED) tt_um_stochastic_addmultiply_CL123abc (Stochastic Multiplier, Adder and Self-Multiplier) tt_um_QIF_8bit (8 Bit Digital QIF) tt_um_toivoh_retro_console (Retro Console) tt_um_cejmu (CEJMU Beers and Adders) tt_um_rejunity_sn76489 (Classic 8-bit era Programmable Sound Generator SN76489) tt_um_dlmiles_tt05_i2c_bert (I2C BERT) tt_um_dlmiles_muldiv8 (MULDIV unit (8-bit signed/unsigned)) tt_um_dlmiles_loopback (IHP loopback tile with input skew measurement) tt_um_dlmiles_bad_synchronizer (Example of Bad Synchronizer) tt_um_wokwi_407306064811090945 (DDR throughput and flop aperature test) tt_um_urish_giant_ringosc (Giant Ring Oscillator (3853 inverters)) tt_um_digital_clock_example (Digital Desk Clock v2.0) tt_um_rejunity_z80 (Zilog Z80) tt_um_rejunity_ay8913 (Classic 8-bit era Programmable Sound Generator AY-3-8913) tt_um_rtfb_collatz (Collatz conjecture brute-forcer) tt_um_ccattuto_conway (Conway's Game of Life on UART and VGA) tt_um_snow (Snow) tt_um_calonso88_74181 (8-bit ALU based on 2x 74181) tt_um_rejunity_vga_logo (VGA Tiny Logo (1 tile)) tt_um_NicklausThompson_SkyKing (SkyKing Demo) tt_um_htfab_cells (Cell mux) tt_um_htfab_pg_1x1 (Power gating test (1x1)) tt_um_htfab_pg_1x2 (Power gating test (1x2)) tt_um_dlmiles_ringosc_5inv (Ring Oscillator (5 inverter)) tt_um_devinatkin_pulse_width_counter (Pulse Width Counter) tt_um_algofoogle_vga_fun_wrapper (TTIHP VGA FUN!) tt_um_cfib_demo (cfib Demoscene Entry) tt_um_vga_glyph_mode (Glyph Mode) tt_um_favoritohjs_scroller (VGA Scroller) tt_um_pulse_generator (TTL Pulse Generator) tt_um_rajum_iterativeMAC (Iterative MAC) tt_um_algofoogle_tinyvga_fun_wrapper (TTIHP TinyVGA FUN!) tt_um_urish_sram_test (SRAM (1024x8) test) tt_um_one_bit_puf_wrapper (One Bit PUF) tt_um_multi_bit_puf_wrapper (One Bit PUF) tt_um_gray_sobel (Gray scale and Sobel filter) tt_um_rebeccargb_intercal_alu (INTERCAL ALU)