Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
8bit_chiptune:sn76489 [2019/11/19 15:46]
admin [1. Conventions]
8bit_chiptune:sn76489 [2019/11/19 16:16] (current)
admin [8. Game Gear stereo extension]
Line 71: Line 71:
  
  
-Channel   Volume registers   Tone/noise registers\\ +   Channel   Volume registers   Tone/noise registers 
-0 (%00)        Vol0                 Tone0\\ +   0 (%00)           Vol0           Tone0 
-1 (%01)        Vol1                 Tone1\\ +   1 (%01)           Vol1           Tone1 
-2 (%10)        Vol2                 Tone2\\ +   2 (%10)           Vol2           Tone2 
-3 (%11)        Vol3                 Noise\\+   3 (%11)           Vol3           Noise
  
  
Line 132: Line 132:
 (spacing added for clarity, hopefully):\\ (spacing added for clarity, hopefully):\\
  
-1. %1 00 0 1110      Latch, channel 0, tone, data %1110\\+**1.** %1 00 0 1110      Latch, channel 0, tone, data %1110\\
 %0 0  001111      Data %001111\\ %0 0  001111      Data %001111\\
 Set channel 0 tone to %0011111110 = 0xfe (440Hz @ 35976545Hz clock)\\ Set channel 0 tone to %0011111110 = 0xfe (440Hz @ 35976545Hz clock)\\
  
-2. %1 01 1 1111      Latch, channel 1, volume, data %1111\\+**2.** %1 01 1 1111      Latch, channel 1, volume, data %1111\\
 Set channel 1 volume to %1111 = 0xf (silent)\\ Set channel 1 volume to %1111 = 0xf (silent)\\
  
-3. %1 10 1 1111      Latch, channel 2, volume, data %1111\\+**3.** %1 10 1 1111      Latch, channel 2, volume, data %1111\\
 %0 0  000000      Data %000000\\ %0 0  000000      Data %000000\\
 Set channel 2 volume to %1111 = 0xf (silent)\\ Set channel 2 volume to %1111 = 0xf (silent)\\
Line 145: Line 145:
 **The data byte is NOT ignored.**\\ **The data byte is NOT ignored.**\\
  
-4. %1 11 0 0101      Latch, channel 3, noise, data %0101\\+**4.** %1 11 0 0101      Latch, channel 3, noise, data %0101\\
 Set noise register to %101 (white noise, medium shift rate)\\ Set noise register to %101 (white noise, medium shift rate)\\
  
-5. %1 11 0 0101      Latch, channel 3, noise, data %0101\\+**5.** %1 11 0 0101      Latch, channel 3, noise, data %0101\\
 %0 0  000100      Data %000100\\ %0 0  000100      Data %000100\\
 Set noise register to %101 (white noise, medium shift rate)\\ Set noise register to %101 (white noise, medium shift rate)\\
Line 154: Line 154:
 ** The data byte is NOT ignored.** ** The data byte is NOT ignored.**
  
-Numbers 1, 2 and 4 above are the same as is described in the various +Numbers 1, 2 and 4 above are the same as is described in the various existing docs.
-existing docs.+
  
 Number 3 IS USED - when pausing between text boxes in SMS Alex Kidd in Number 3 IS USED - when pausing between text boxes in SMS Alex Kidd in
Line 171: Line 170:
 Many games also produce the above two unusual behaviours but not Many games also produce the above two unusual behaviours but not
 repeatedly (often when a SFX is first played, for example). repeatedly (often when a SFX is first played, for example).
- 
  
 Also of note is that the tone registers update immediately when a byte Also of note is that the tone registers update immediately when a byte
Line 182: Line 180:
   %0 0  111111    %1111111111   %0 0  111111    %1111111111
  
-There were a couple of ways to handle SN76489 writes before: +There were a couple of ways to handle SN76489 writes before:\\ 
- +**1.** Latch only the tone registers, as above, and leave them latched when other types of data (volume, noise) are written. This gives a "squawk" effect on SMS Micro Machines' title screen, which drowns out the "eek".
-1. Latch only the tone registers, as above, and leave them latched when +
-   other types of data (volume, noise) are written. This gives a +
-   "squawk" effect on SMS Micro Machines' title screen, which drowns out +
-   the "eek"+
- +
-2. Latch tone registers as above, and "unlatch" when other types of +
-   data are written. When a data byte is written with it unlatched, the +
-   data is discarded. This fixes the "squawk" but leaves the "eek".+
  
 +**2.** Latch tone registers as above, and "unlatch" when other types of data are written. When a data byte is written with it unlatched, the data is discarded. This fixes the "squawk" but leaves the "eek".
  
-5. How the SN76489 makes sound +---- 
-==============================+==== 5. How the SN76489 makes sound ====
  
 This is already well documented, but I'll repeat it again with This is already well documented, but I'll repeat it again with
Line 214: Line 205:
 decremented, it is zero, the following happens: decremented, it is zero, the following happens:
  
-5.1 Tone channels +=== 5.1 Tone channels ===
-=================+
  
 The counter is reset to the value currently in the corresponding The counter is reset to the value currently in the corresponding
Line 237: Line 227:
 This is often used for sample playback on the SN76489. This is often used for sample playback on the SN76489.
  
-5.2 Noise channel +=== 5.2 Noise channel ===
-=================+
  
 The counter is reset according to the low 2 bits of the noise register The counter is reset according to the low 2 bits of the noise register
Line 254: Line 243:
 shift register" (LFSR), which can generate noise or act as a divider. shift register" (LFSR), which can generate noise or act as a divider.
  
-5.2.1 The Linear Feedback Shift Register +=== 5.2.1 The Linear Feedback Shift Register ===
-========================================+
  
 The LFSR is an array of either 15 or 16 bits, depending on the chip The LFSR is an array of either 15 or 16 bits, depending on the chip
Line 282: Line 270:
 For white noise (Noise register bit 2 = 1): For white noise (Noise register bit 2 = 1):
  
-  For the SMS (1 and 2), Genesis and Game Gear, the tapped bits are +For the SMS (1 and 2), Genesis and Game Gear, the tapped bits are bits 0 and 3. For the SC-3000H, the tapped bits are bits 1 and 2. For the BBC Micro, using John Kortink's data, the tapped bits are 15, 2 and 0.
-  bits 0 and 3. For the SC-3000H, the tapped bits are bits 1 and 2. For +
-  the BBC Micro, using John Kortink's data, the tapped bits are 15, 2 +
-  and 0.+
  
-  I would like to confirm the bit pattern for other systems, please +I would like to confirm the bit pattern for other systems, please contact me if you can help by running/coding homebrew code on a real system and sampling the sound.
-  contact me if you can help by running/coding homebrew code on a real +
-  system and sampling the sound.+
  
-  Example (SMS/GG):+Example (SMS/GG):
  
          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Line 307: Line 290:
 For "periodic noise" (Noise register bit 2 = 0): For "periodic noise" (Noise register bit 2 = 0):
  
-  For the SMS, GG and Genesis, only bit 0 is tapped, ie. the output bit +For the SMS, GG and Genesis, only bit 0 is tapped, ie. the output bit is also the input bit. The effect of this is to output the contents of the shift register in a 16-bit long loop.
-  is also the input bit. The effect of this is to output the contents of +
-  the shift register in a 16-bit long loop.+
  
-  For the BBC Micro, bit 1 is tapped, giving a 15-bit long loop.+For the BBC Micro, bit 1 is tapped, giving a 15-bit long loop.
  
-  Other systems need investigation.+Other systems need investigation.
  
          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Line 321: Line 302:
    +----------------------------------------------------+    +----------------------------------------------------+
  
-   Note that this "periodic noise", as it is called in the original +Note that this "periodic noise", as it is called in the original chip's documentation, is in fact not periodic noise as it is defined elsewhere (white noise with a configurable periodicity); it is a duty cycle modifier. For this reason, throughout this document it is always referred to with quotes.
-   chip's documentation, is in fact not periodic noise as it is +
-   defined elsewhere (white noise with a configurable periodicity); it +
-   is a duty cycle modifier. For this reason, throughout this document +
-   it is always referred to with quotes.+
  
 When the noise register is written to, the shift register is reset, When the noise register is written to, the shift register is reset,
Line 332: Line 309:
 important as it affects the sound of white noise. important as it affects the sound of white noise.
  
-5.2.2 The 15-bit shift register +=== 5.2.2 The 15-bit shift register ===
-===============================+
  
 The values given above are for using a 16-bit shift register. However, The values given above are for using a 16-bit shift register. However,
Line 349: Line 325:
  
  
-5.2.3 An implementation of the noise shift register +=== 5.2.3 An implementation of the noise shift register ===
-===================================================+
  
 The method of generating white noise is the most fundamentally incorrect The method of generating white noise is the most fundamentally incorrect
Line 356: Line 331:
 most common method is to do something like: most common method is to do something like:
  
-#define WHITE_NOISE_FEEDBACK 0xf037   ; or some other value +   #define WHITE_NOISE_FEEDBACK 0xf037   ; or some other value 
-#define PERIODIC_NOISE_FEEDBACK 0x8000 +   #define PERIODIC_NOISE_FEEDBACK 0x8000 
-... +   ... 
-    ShiftRegister>>=1; +    
-    ShiftRegister^=(WhiteNoise?WHITE_NOISE_FEEDBACK:PERIODIC_FEEDBACK); +   ShiftRegister>>=1; 
-    Output=ShiftRegister&1;+   ShiftRegister^=(WhiteNoise?WHITE_NOISE_FEEDBACK:PERIODIC_FEEDBACK); 
 +   Output=ShiftRegister&1;
  
 With the correct choice of periodic feedback parameter (depending on With the correct choice of periodic feedback parameter (depending on
Line 386: Line 362:
 parity(): parity():
  
-int parity(int val) {+   int parity(int val) {
     val^=val>>8;     val^=val>>8;
     val^=val>>4;     val^=val>>4;
Line 392: Line 368:
     val^=val>>1;     val^=val>>1;
     return val&1;     return val&1;
-};+   };
  
 Thanks go to Dave (finaldave) for coming up with this. You may get Thanks go to Dave (finaldave) for coming up with this. You may get
Line 399: Line 375:
 built-in parity checking instructions/flags. built-in parity checking instructions/flags.
  
-5.3 Volume/attenuation +=== 5.3 Volume/attenuation ===
-======================+
  
 The mixer then multiplies each channel's output by the corresponding The mixer then multiplies each channel's output by the corresponding
Line 414: Line 389:
 is defined as is defined as
  
-    power 1 +       power 1 
-log ------- +   log ------- 
-    power 2+       power 2
  
 Whether it's positive or negative depends on which way around you put Whether it's positive or negative depends on which way around you put
Line 443: Line 418:
                       (voltage 2)                       (voltage 2)
  
-Rearranging,+Rearranging,\\
  
     voltage 1     (decibels / 20)     voltage 1     (decibels / 20)
Line 453: Line 428:
 build an output table, for example: build an output table, for example:
  
-int volume_table[16]={ +   int volume_table[16]={ 
-  32767, 26028, 20675, 16422, 13045, 10362,  8231,  6568, +    32767, 26028, 20675, 16422, 13045, 10362,  8231,  6568, 
-   5193,  4125,  3277,  2603,  2067,  1642,  1304,     0 +    5193,  4125,  3277,  2603,  2067,  1642,  1304,     0 
-};+   };
  
 These correspond to volume register values 0x0 to 0xf, in that order. These correspond to volume register values 0x0 to 0xf, in that order.
Line 468: Line 443:
 example. example.
  
- +---- 
-6. The imperfect SN76489 +==== 6. The imperfect SN76489 ====
-========================+
  
 Real components aren't perfect. The output of the SN76489 in its Real components aren't perfect. The output of the SN76489 in its
Line 492: Line 466:
 waves from this: waves from this:
  
----------+         +---------+         +---------+         +---------+ +   +---------+         +---------+         +---------+         +---------+ 
-         |                                                 | +             |                                                 | 
-         |                                                 | +             |                                                 | 
-- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- +   +- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- - - - -|- 
-         |                                                 | +             |                                                 | 
-         |                                                 | +             |                                                 | 
-         +---------+         +---------+         +---------+         +--+             +---------+         +---------+         +---------+         +--
  
 to something like this: to something like this:
  
--___               +-___               +-___               +-___ +    +-___               +-___               +-___               +-___ 
-    -----+            -----+            -----+            -----+ +        -----+            -----+            -----+            -----+ 
-         |                                                 | +             |                                                 | 
-_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ +   +_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|_ 
-         |                                                 | +             |                                                 | 
-         |                                                 | +             |                                                 | 
-         | ___-----+         | ___-----+         | ___-----+         | _ +             | ___-----+         | ___-----+         | ___-----+         | _ 
-         +-                  +-                  +-                  +-+             +-                  +-                  +-                  +-
  
 If the tone register value is large enough, they will decay close to If the tone register value is large enough, they will decay close to
Line 523: Line 497:
  
  
-----+    +----+    +----+    +----+    +----+    +----+    +----+    +-- +   +----+    +----+    +----+    +----+    +----+    +----+    +----+    +-- 
-- - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- +   + - - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- - | - -|- 
-    +----+    +----+    +----+    +----+    +----+    +----+    +----++         +----+    +----+    +----+    +----+    +----+    +----+    +----+
  
 Perfect output (assuming output bit pattern of 10100101): Perfect output (assuming output bit pattern of 10100101):
  
----------+         +---------+                   +---------+         +-- +   +---------+         +---------+                   +---------+         +-- 
-         |                                                   | +             |                                                   | 
-         |                                                   | +             |                                                   | 
-- - - - -+---------+- - - - -+-------------------+- - - - -+---------+-+   +- - - - -+---------+- - - - -+-------------------+- - - - -+---------+-
  
 Actual output: Actual output:
  
--___               +-___                         +-___               +-_ +   +-___               +-___                         +-___               +-_ 
-    -----+            -----+                      -----+         | +        -----+            -----+                      -----+         | 
-         |                                                   | +             |                                                   | 
-- - - - -|-___-----+- - - - -|-___---------------+- - - - -|-___-----+- +   +- - - - -|-___-----+- - - - -|-___---------------+- - - - -|-___-----+- 
-         +-                  +-                            +-+             +-                  +-                            +-
  
  
-The empirical effects are: +The empirical effects are:\\ 
-- The sound of tones is changed very slightly +- The sound of tones is changed very slightly\\ 
-- Noise sounds a bit louder +- Noise sounds a bit louder\\ 
-- Voices sound slightly different+- Voices sound slightly different\\
  
 Some games were programmed with the SN76489 considered to be a perfect Some games were programmed with the SN76489 considered to be a perfect
Line 552: Line 526:
  
  
-6.1 Noise volume +=== 6.1 Noise volume ===
-================+
  
 As previously described, the noise output is either 0 or +1, multiplied As previously described, the noise output is either 0 or +1, multiplied
Line 568: Line 541:
 noise volume when mixing the channels together. noise volume when mixing the channels together.
  
- +---- 
-7. Playing samples on the SN76489 +==== 7. Playing samples on the SN76489 ====
-=================================+
  
 This is for the reference of those wishing to put sample playback in This is for the reference of those wishing to put sample playback in
Line 577: Line 549:
 It is possible to play samples in two ways: It is possible to play samples in two ways:
  
-1. Pulse Code Modulation+=== 1. Pulse Code Modulation ===
  
-   This is the usual way to store, process and output waves. The data is +This is the usual way to store, process and output waves. The data is in the form of voltages (corresponding to the desired speaker position which in turn goves corresponding pressure waves in the air) which are stored digitally, often as 16-bit or 8-bit signed numbers.
-   in the form of voltages (corresponding to the desired speaker +
-   position which in turn goves corresponding pressure waves in the air) +
-   which are stored digitally, often as 16-bit or 8-bit signed numbers.+
  
-   On the SN76489, this is done by:+On the SN76489, this is done by:\\ 
 +1. Setting all 3 tone channels to frequency 0x000\\ 
 +2. At rapid, closely timed intervals, setting the output volume of all 3 to values stored in ROM.\\
  
-   1Setting all 3 tone channels to frequency 0x000 +In other words, the volume setting is used as a 4-bit DACAll three tone channels are usually used together to get maximum volume.
-   2. At rapid, closely timed intervals, setting the output volume of +
-      all 3 to values stored in ROM.+
  
-   In other words, the volume setting is used as a 4-bit DAC. All three +The problem is that the output levels of the SN76489 are not linearly scaled:
-   tone channels are usually used together to get maximum volume. +
- +
-   The problem is that the output levels of the SN76489 are not linearly +
-   scaled:+
  
                                  | Linear                                  | Linear
    | ||||| | | |  |      |                             | SN76489    | ||||| | | |  |      |                             | SN76489
  
-   The source wave could be prepared with this in mind, using some +The source wave could be prepared with this in mind, using some specialised software, but I am not aware of any consumer-level wave editor capable of doing this. However, ignoring this and outputting "normal" linear 4-bit data will generally sound good, but significantly quieter than it would be on a linear scale.
-   specialised software, but I am not aware of any consumer-level wave +
-   editor capable of doing this. However, ignoring this and outputting +
-   "normal" linear 4-bit data will generally sound good, but +
-   significantly quieter than it would be on a linear scale.+
  
-   The quality depends on the rate at which data is sent to the chip; on +The quality depends on the rate at which data is sent to the chip; on most systems, the limit is more likely to be memory space than CPU speed (8kHz 4-bit audio will fit 4.1 seconds into 16KB).
-   most systems, the limit is more likely to be memory space than CPU +
-   speed (8kHz 4-bit audio will fit 4.1 seconds into 16KB).+
  
-   One could use the combined logarithmic volume levels of the three +One could use the combined logarithmic volume levels of the three tone channels to give more output levels, but this additional complexity may not be worth the effort and the space these 12-bit samples would take up; it seems not to be found in existing software.
-   tone channels to give more output levels, but this additional +
-   complexity may not be worth the effort and the space these 12-bit +
-   samples would take up; it seems not to be found in existing software.+
  
-2. Pulse Width Modulation+=== 2. Pulse Width Modulation === 
 +This works by outputting pulses at constant volume whose pattern gives the effect of different frequencies and volumes. This is also how samples are played on PC internal speakers and some CD players.
  
-   This works by outputting pulses at constant volume whose pattern +On the SN76489, this is done by:\\ 
-   gives the effect of different frequencies and volumes. This is also +1. Setting all 3 tone channels to frequency 0x000\\ 
-   how samples are played on PC internal speakers and some CD players. +2. At rapid, closely timed intervals, setting the output volume of all 3 to either 0xf (off) or 0x0 (full) depending on valuues stored in ROM.\\
-   On the SN76489, this is done by:+
  
-   1. Setting all 3 tone channels to frequency 0x000 +All three channels are used to get maximum volume.
-   2. At rapid, closely timed intervals, setting the output volume of +
-      all 3 to either 0xf (off) or 0x0 (full) depending on valuues +
-      stored in ROM.+
  
-   All three channels are used to get maximum volume.+This is equivalent to dithering the sound to one bit per sample instead of however many bits per sample are in the input data. Thus, an 8kHz 8-bit sample can be output as a 64kHz 1-bit sample and it will sound much the same. It is somewhat dependent on the output frequency being above the range of hearing.
  
-   This is equivalent to dithering the sound to one bit per sample +The advantage of this is that it allows for a sample based on a linear PCM scale to be output accurately on the SN76489, (allowing for louder sounds) and it can potentially output any bitdepth source audio. The disadvantage is that, with a limited output rate, one is forced to trade off between the bitdepth and sampling rate of the input sample: with a maximum output rate of 20kHzfor example, one may choose a 6.67kHz 3-bit source sample5kHz 4-bit source sample, etcThis can be severely limiting for the qaulity.
-   instead of however many bits per sample are in the input data. Thus, +
-   an 8kHz 8-bit sample can be output as 64kHz 1-bit sample and it +
-   will sound much the sameIt is somewhat dependent on the output +
-   frequency being above the range of hearing.+
  
-   The advantage of this is that it allows for a sample based on a +On the Master SystemPWM is not very good quality (often the sound is unintelligiblefor example Alex Kidd: the Lost Stars' "Find/I'the Miracle Ball" and Shooting Gallery's "Perfect")but it is the loudest way to play samples.
-   linear PCM scale to be output accurately on the SN76489, (allowing +
-   for louder sounds) and it can potentially output any bitdepth source +
-   audio. The disadvantage is thatwith a limited output rate, one is +
-   forced to trade off between the bitdepth and sampling rate of the +
-   input sample: with a maximum output rate of 20kHzfor example, one +
-   may choose a 6.67kHz 3-bit source sample, a 5kHz 4-bit source sample, +
-   etc. This can be severely limiting for the qaulity.+
  
-   On the Master System, PWM is not very good quality (often the sound +---- 
-   is unintelligible, for example Alex Kidd: the Lost Stars' "Find/I'+==== 8. Game Gear stereo extension ====
-   the Miracle Ball" and Shooting Gallery's "Perfect"), but it is the +
-   loudest way to play samples. +
- +
- +
-8. Game Gear stereo extension +
-=============================+
  
 When a byte is written to port 0x06 on the Game Gear, the SN76489 When a byte is written to port 0x06 on the Game Gear, the SN76489
Line 669: Line 605:
 outputs to rhe left side only, etc. outputs to rhe left side only, etc.
  
- +----
-Maxim +
-maxim@mwos.cjb.net +
-27/4/2005 +
- +
-Updated 6/6/2002: +
-Clarification that SN76489 tones update immediately after latch byte. +
-Use of 2-stage volume writes found. +
- +
-Updated 22/8/2002: +
-Charles MacDonald sampled GG and Genesis noise for me, it's the same +
-bit pattern as SMS noise. +
- +
-Updated 20/10/2002: +
-Fixed some typos. +
- +
-Updated 21/3/2003: +
-Added SC-3000H noise feedback pattern, thanks to Charles MacDonald for +
-getting the data for me. +
- +
-Updated 21/4/2003: +
-Charles MacDonald sampled SMS1 noise, it's the same bit pattern as the +
-SMS2, GG and Genesis. +
- +
-Updated 27/4/2005: +
-Added sections on 15-bit shift registers and volume/attenuation. Most  +
-sections tweaked, clarified, corrected and extended. +
- +