# What is FM Tone Generation?

Before going deeper into YM2612 details, these Yamaha pages are a good starting point for the general idea of FM tone generation.

- [`FM Tone Generators and the Dawn of Home Music Production`](https://web.archive.org/web/20260724133543/https://usa.yamaha.com/products/contents/music_production/synth_50th/history/chapter002.html)
- [`How FM Tone Generation Works`](https://web.archive.org/web/20260821160530/https://usa.yamaha.com/products/contents/music_production/synth_50th/anecdotes/004.html)



# About YM2612

The YM2612 is a sound chip developed by Yamaha.
It was used in Sega's Mega Drive / Genesis video game console.

This chip has:

- 6 channels of 4-operator FM tone generation
- two interval timers
- a sine-wave low-frequency oscillator (LFO)
- stereo output
- a simple DAC playback path for PCM-style sample playback on channel 6


# YM2612 Chip Information

```
http://www.chipdir.nl/pinusr/ym2612.txt


    +--()--+
GND | 1  24| 0M(CLK)
 D0 | 2  23| Vcc
 D1 | 3  22| A.Vcc
 D2 | 4  21| MOL
 D3 | 5  20| MOR
 D4 | 6  19| A.GND
 D5 | 7  18| A1
 D6 | 8  17| A0
 D7 | 9  16| /RD
  ? |10  15| /WR
/IC |11  14| /CS
GND |12  13| /IRQ
    +------+
```

## Pin summary

- `GND`: digital ground
- `A.GND`: analog ground for the audio output section
- `Vcc`: digital power supply
- `A.Vcc`: analog power supply for the audio output section
- `0M (CLK)`: master clock input

- `MOL`: left audio output pin
- `MOR`: right audio output pin

- `D0-D7`: 8-bit bidirectional data bus
- `A0`: address select pin
- `A1`: address select pin

- `/RD`: read signal (active low)
- `/WR`: write signal (active low)
- `/CS`: chip select signal (active low)
- `/IRQ`: interrupt request signal (active low)
- `/IC`: reset signal (active low)

- `Pin 10`: usually unused in normal applications
  Some references describe it as `NC`, while others describe it as a `TEST` pin.

## FM sound design

Before going deeper into PCM / DAC, it is helpful to first look at how YM2612 makes FM tones.

The main idea is:

- parameters such as `DT`, `MULTI`, `TL`, `AR`, `D1R`, `D2R`, `SL`, and `RR` shape the sound of each operator
- multiple operators can then be combined with different algorithms to make more complex sounds

You can try these interactive pages first:

- [`operator1.html`](../demos/operator1.html)
  Learn how a single operator changes when you adjust `DT`, `MULTI`, `TL`, `AR`, `D1R`, `D2R`, `SL`, and `RR`.
- [`operator2.html`](../demos/operator2.html)
  Try two operators together and see how modulation and algorithm changes affect the tone.
- [`YM2612 synth demo`](../synth/index.html)
  Combine multiple operators and channels to design more complex sounds in a playable demo.

## FM sound chip control

`A0` and `A1` choose which control port is being accessed.
They switch between address/data access and port 0/port 1 access.

- `A1=0, A0=0`: address port 0
- `A1=0, A0=1`: data port 0
- `A1=1, A0=0`: address port 1
- `A1=1, A0=1`: data port 1

## How `chip.write()` maps to YM2612 pins

`void ym2612::write(uint32_t offset, uint8_t data)` is a simplified interface.

- `offset` corresponds to the access selected by `A1` and `A0`
- `data` corresponds to the 8-bit value on `D0-D7`

The mapping is:

- `offset = 0`: `A1=0, A0=0` -> address port 0
- `offset = 1`: `A1=0, A0=1` -> data port 0
- `offset = 2`: `A1=1, A0=0` -> address port 1
- `offset = 3`: `A1=1, A0=1` -> data port 1

Example:

```cpp
chip.write(0, 0x30);
chip.write(1, 0x01);
```

This means:

1. Select register `0x30` on address port 0.
2. Write `0x01` to that register on data port 0.

In other words, software is not changing the sound by magic.
It is sending YM2612-specific control values through these chip pins and writing them into YM2612 registers.

## PCM / DAC on YM2612

The YM2612 is mainly known as a 6-channel FM chip, but it also has a simple DAC playback path.

- `0x2A`: DAC data
- `0x2B`: DAC enable

When DAC playback is enabled, channel 6 can be used for PCM-style sample playback instead of normal FM output.

This is not a separate large PCM engine like later chips.
It is a simpler DAC path where software keeps writing sample bytes to the chip.

In practice, this is important for:

- voice samples
- drum hits
- VGM playback that uses YM2612 DAC streaming

In this repository, the current PCM-related browser support is mainly handled through the VGM playback path.
That path reads VGM DAC/stream commands and forwards the sample bytes to YM2612 DAC register writes.

## Questions

Q: Why are there two types of ground, `GND` and `A.GND`?

A: The chip has both digital and analog sections.
The analog section is used for audio output, so `A.Vcc` and `A.GND` are used for the audio output circuitry behind `MOL` and `MOR`.

Q: Why do we need `/CS`?

A: `/CS` tells the YM2612 that the current bus access is for this chip.
Without it, multiple chips on the same bus could respond at the same time.

Q: Why do we need `/IRQ`?

A: `/IRQ` is mainly used to notify the CPU when an internal timer event occurs.

Q: Why do we need `/RD` and `/WR`?

A: `/RD` and `/WR` are control signals, not data lines.
The actual data is carried on `D0-D7`.

Q: What are `D0-D7` for?

A: `D0-D7` are 8-bit data pins.
They carry digital data between the CPU and the YM2612.

Q: What is `/IC` for?

A: `/IC` is the reset signal.
It initializes the chip and clears its registers.

## References

- http://www.chipdir.nl/pinusr/ym2612.txt
- https://www.vgmpf.com/Wiki/index.php?title=YM2612
- https://gendev.spritesmind.net/forum/viewtopic.php?start=585&t=386


