XSG Application Manual

Overview

XSG (eXtended Sound Generator) is a sound generation device. It is strongly inspired by classic sound chips, such as General Instrument's AY-3-8910 (for the purposes of this reference, we'll call it the PSG), Konami's SCC and Yamaha's OPN family. It features six wave generators with customizable waveforms, two noise generators, two envelope generators and two timers. It also features internal memory for storing waveforms and is capable of stereo sound output.

Features

Description of functions

All functions of XSG are controlled by internal registers. The CPU writes data to XSG internal registers, and XSG generates sound. Sound is generated by the following blocks:

Music generator:
Waveforms having different frequencies are generated for each channel (A, B, C, D, E and F).
Noise generator:
Pseudo-random waveforms having different frequencies are generated for each sound unit (1 for channels A, B and C and 2 for channels D, E and F).
Mixers:
Music and noise output are mixed for the six channels, and further mixed into two stereo channels (left and right). Waveforms can also be inverted for a pseudo-surround effect.
Level control:
Level can be set for each of the six channels. Levels may be controlled by the CPU or by the envelope generators for each sound unit (1 for channels A, B and C and 2 for channels D, E and F).
Envelope generators:
Generates various types of attenuation.
Timers:
Two timers with different resolutions can be useful for music replayers and other applications.
D-A converter:
Sound is output for each of the six channels (A, B, C, D, E and F).

The CPU can read the contents of the internal registers, except when XSG is in write-only mode. This mode is provided to avoid hardware conflicts (for example, if XSG is used to mirror another PSG).

Sound units

XSG is designed to work as two PSGs in tandem. For this reason, there are two noise generators and two envelope generators, one for each sound unit. For maximum compatibility, these units have the exact same features. These sound units are configured from start to work as closely as possible as two PSGs. Thus, each sound channel is set to produce 50% duty-cycle pulse waves and monoaural sound. The equations provided below assume an undivided master clock frequency on the 3.58MHz ballpark, unlike the original PSG and its clones which used half that much.

The register array

Please refer to the register map reference for an overview of the register array. The register array is organized in 3 register banks:

  1. Registers R00 to R0D: used to set parameters for sound unit 1 (sound channels A, B and C);
  2. Registers R10 to R1D: used to set parameters for sound unit 2 (sound channels D, E and F);
  3. Registers R24 to R27 and RF0 to RFE: used to set parameters for features not available in the original PSG.

Unused bits are greyed out. Since they can be used for future features, you should always set them to 0, except when noted otherwise.

Banks 1 and 2 register values usually default to 0 (except when otherwise noted), but for compatibility purposes and best practices, should not be considered as such. You should always initialize these registers by software.

Register bank 1 (channels A, B and C)

Sound unit 1 frequency settings (registers R00 to R05)

Frequencies for each channel are set with the register value TP (Tone Period), which has 12 bits. Since XSG is a 8-bit sound generation device, two registers must be used to set each channel frequencies. The 8 less significant bits are in even-numbered registers and the 4 most significant bits are in odd-numbered registers, according to each sound channel. Thus, R00 and R01 set the frequency for channel A, R02 and R03 for channel B and R04 and R05 for channel C.

The frequency can be calculated by the following equation:

fT = fMaster / 32TP

fMaster is the master clock frequency. TP is the 12-bit value and fT is the final tone frequency.

Noise generator 1 setting (register R06)

The noise generator 1 frequency is set with the register value NP (Noise Period), which has 5 bits. The following equation is used to calculate the noise generator 1 frequency:

fN = fMaster / 32NP

fMaster is the master clock frequency. NP is the 5-bit value and fN is the final noise frequency.

Sound unit 1 mixer setting (register R07)

The mixer is used to combine music and noise components. The combination is determined by bits B0 to B5 of register R07. Sound is output when 0 is written to the register. Thus, when both noise and tone are 0, the output is combined by the mixer. When noise is 0 and tone is 1, only noise is output. When noise is 1 and tone is 0, only the waveform is output. Nothing is output when both bits are 0. Differently from the original PSG, bits B6 and B7 are not used. For maximum compatibility with the MSX platform, B6 always returns 0 when read, while B7 always returns 1. Thus:

Sound unit 1 level control (registers R08 to R0A)

Each level control register's bit B4 is the mode select. If its value is 0, then level is controlled by envelope generator 1. If its value is 1, then level is determined by a 4-bit value in bits B0 to B3. The channel A level is controlled by register R08, while R09 and R0A refer to channels B and C, respectively.

Sound level in XSG is logarithmic, just like the PSG, and this behavior is closer to the way human hearing works. Since 15 is the maximum level, and each level step is √2 times louder or quieter than the previous level, this means level 13 is half as loud as level 15, and so on. Conversely, level 10, for example, is twice as loud as level 8, and so on. The only exception to this is when XSG is in linear amplitude mode. In this mode, each level corresponds approximately to 6.7% (1/15) of full volume.

Envelope generator 1 setting (registers R0B and R0C)

Envelope repetition frequency is set with the register value EP (Envelope Period), which has 16 bits. Since XSG is a 8-bit sound generation device, two registers must be used to set the envelope generator frequency. The less significant bits are in R0B and the most significant bits are in R09. This equation can be used to calculate the frequency:

fE = fMaster / 512EP

fMaster is the master clock frequency. EP is the 16-bit value and fE is the final envelope frequency. The period of the actual frequency fEA used for the generated envelope is 1/32 of the envelope repetition period (1/fE).

Envelope generator 1 shape control (register R0D)

XSG's envelope generators count the envelope clock fEA 32 times for each envelope pattern cycle. The envelope level is determined by the 5-bit output of the counter. The shape of this envelope is created by increasing, decreasing, stopping or repeating this counter, and is controlled by register R0D's bits B0 to B3:

The following table shows a graphical representation of the envelope generator output,

Envelope Generator Output
B7 B6 B5 B4 B3 B2 B1 B0 Envelope Shape
CTN ATT ALT HLD
0 0 x x
0 1 x x
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

Register bank 2 (channels D, E and F)

Register function and layout for sound unit 2 is exactly the same as for sound unit 1. By adding 1016 to each register number, settings for channels D, E and F instead of channels A, B and C can be changed. Equally, noise generator 2 and envelope generator 2 are set instead of noise generator 1 and envelope generator 1. The only exception to this is when mirror mode is set. In this case, R10 to R1D will have the same values of R00 to R0D, and vice versa. You will still be able to change waveform and stereo settings for each channel separately.

Register bank 3 (control set)

The bank 3 registers are used for features new to XSG. For this reason, they all default to 0 and don't need to be initialized by software. This is to ensure XSG can be used for PSG applications with minimal or no modifications.

Timer A (registers R24 and R25)

XSG provides two timers tied to its master clock frequency. Timer A is a high resolution timer (approximately 8.9µs), and it uses 10 bits to set its period (most significant 8 bits are in register R24, and the less significant 2 bits are in register R25. Its frequency is calculated by the following equation:

fTA = fMaster / 32 * (1024 - PTA)

fMaster is the master clock frequency. PTA is the 10 bits period and fTA is the final timer frequency.

Timer B (register R26)

Timer B is a low resolution timer (approximately 143µs), and it uses 8 bits to set its period. Its frequency is calculated by the following equation:

fTB = fMaster / 512 * (256 - PTB)

fMaster is the master clock frequency. PTB is the 8 bits period and fTB is the final timer frequency.

Timer control (register R27)

This register is used to control the timers, according to the following:

Waveform select (registers RF0 to RF6)

Each one of these 7 registers (one for each sound channel, from A to F, plus another two for fine addressing and data I/O) point to a waveform in the 4096 bytes of internal storage. Since each waveform has 32 bytes, up to 128 waveforms can be stored internally. The internal address storage is calculated by the following equation:

AW = 32PW + OW

AW is the internal address. PW is the value of the pointer, and OW is the offset. RF6 works differently, in that it doesn't select a waveform for a specific sound channel. Instead, it selects a waveform that will be written to or read from RF8.

Waveform offset (register RF7)

When needed, you can point to a specific byte inside a given waveform. This register is automatically reset when writing to register RF6.

Waveform data (register RF8)

XSG has an internal 12-bit address pointer. It points to an internal 4096 byte storage area, and its 7 most significant bits map to register RF6, while its 5 less significant bits map to register RF7. When setting the value of RF6, this value is shifted to the most significant 7 bits of the address pointer (effectively multiplying it for 32). The less significant bits are then reset to 0. When writing or reading a value to or from the waveform data register (RF8), the address pointer automatically increments. When it reaches 0FFF16, it wraps around to 0. This way, data can be written or read sequentially to or from RF8, simplifying waveform loading routines. Also, waveform data is 8-bit, unsigned.

In order to achieve compatibility with the PSG, after reset the first 16 bytes of the internal waveform storage area will be filled with FF16, and the following bytes will be at 0, representing a 50% duty-cycle pulse wave in all 6 channels.

Stereo mixer (registers RFA and RFB)

The stereo mixer is used to determine whether a given sound channel will output sound to the left, right or center. RFA controls stereo output for sound channels A, B and C, while RFB controls stereo output for channels D, E and F. Otherwise, they work the same way. Each control bit is used by each channel, as follows:

For compatibility and consistence purposes (similarity to registers R07 and R17), each bit is activated with the value 0. Since the default register value is also 0, sound is output by each channel to both left and right, resulting in monoaural sound by default. If left for a given sound channel is 0 and right is 1, sound will be produced only on the left side. Conversely, if left is 1 and right is 0, sound will be produced only on the right side. A special case happens when both bits are set to 1: instead of muting the output, the waveform is inverted in the right output, giving a pseudo surround effect. These registers values are ignored when XSG is set to monoaural mode.

Operation mode (register RFC)

This register is used to set XSG operation modes:

These bits are all activated by the value 1. Since 0 is the register's default value, they are unset by default.

When XSG is in write-only mode, registers can't be read. With this, it becomes possible to set XSG to the same I/O ports of an existing PSG. This may be useful if you want to use XSG's new features with software written for PSG.

When XSG is in mirror mode, any value written to any Bank 1 register will also be written to the corresponding Bank 2 register. Conversely, any value written to any Bank 2 register will also be written to the corresponding Bank 1 register. This may be useful to play the same notes with different waveforms in full stereo, for example. Effectively, polyphony is sacrificed for the sake of having more complex sounds.

When XSG is in monoaural mode, stereo mixer settings are completely ignored. This may be useful if XSG is connected to a monoaural sound amplifier.

When XSG is in linear amplitude mode, level control is linear instead of logarithmic. This is closer to the way Konami SCC works and may be useful if you want to mimic that particular chip's dynamic response.

Status (register RFD)

This register is used to check for the timer flags. When the timers are running and they overflow, their respective flags are set to 1 in this register. These flags can't be reset to 0 by writing directly to this register; use the timer control register (R27) instead, when needed.

Test (register RFE)

This register is used for device detection and to check the device id and revision.

Test (register R3F)

This register is used to test for the presence of XSG and obtain its revision version.