home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / Atari800 / pokey11.txt < prev    next >
Text File  |  1997-04-13  |  12KB  |  278 lines

  1.                        Atari POKEY Chip Simulator V1.1
  2.                        ===============================
  3.                                 by Ron Fries
  4.                                   23 Sep 96
  5.  
  6. The POKEY Chip Simulator is designed to emulate the functionality of the
  7. Atari POKEY Chip Hardware through 'C' Sourcecode.  I have seen very good
  8. results.  The simulator is able to produce sounds which are essentially
  9. identical to the original Atari, including the exact distortions and
  10. pitches. 
  11.  
  12. The simulator is designed to run in a 32-bit environment.  Though it can
  13. compiled and run in a 16-bit environment, it is slow.  
  14.  
  15.  
  16. Features:
  17. ---------
  18.  
  19. Version 1.1 of the 'POKEY' simulator supports the following functions:
  20.  
  21. 1) All polynomial sound generators: 
  22.    a) 4-bit poly - actual bit pattern determined from sampled sound
  23.    b) 5-bit poly - actual bit pattern determined from sampled sound
  24.    c) 17-bit poly - simulated random bit pattern
  25.    d) 9-bit poly - derived from simulated 17-bit poly
  26.    
  27. 2) Full support of all 'Divide by N' counter clocks:
  28.    a) 1.79 MHz (high limited to playback sample rate)
  29.    b) 64 KHz (high limited to playback sample rate)
  30.    c) 15 KHz
  31.  
  32. 3) Full support of all 'Divide by N' resolutions:
  33.    a) 8-bit - single channel
  34.    b) 16-bit - double channel
  35.  
  36. 4) Full support of all distortions 
  37.    a) 5-bit poly, then 17-bit poly
  38.    b) 5-bit poly only
  39.    c) 5-bit poly, then 4-bit poly
  40.    d) 17-bit poly only
  41.    e) no poly counters (pure tone)
  42.    f) 5-bit poly only
  43.  
  44. 5) Full support of volume control 
  45.  
  46. 6) Full support of all pitches - distortions will vary exactly as the
  47.    original Atari based on different pitches
  48.  
  49. 7) Accurate pitch generation
  50.  
  51. 8) Support of any playback sample rate (e.g. 22050)
  52.  
  53.  
  54. The 'POKEY' simulator does not currently support the following functions:
  55.  
  56. 1) High pass filters
  57.  
  58.  
  59. Though I don't believe adding support for the High-Pass filters is very
  60. complicated, I decided not to add support right now because I don't
  61. believe this feature is used much.  I'm also not sure how much impact it
  62. would have on performance.
  63.  
  64. You'll notice in the code there are two separate versions.  The original
  65. process function, now called Pokey_process_2(), is the non-optimized 
  66. version.  I've left it in for reference.  The other function,
  67. Pokey_process(), is optimized.  In my tests using the 'Maximize Speed' 
  68. option of the compiler, I've seen very good performance.  On my 486DX2-66, 
  69. it typically takes about 0.15 seconds to produce about 3 seconds of audio.  
  70. These times were calculated under WIN95.
  71.  
  72. One of the unique features of the optimized version is that the processing
  73. time will vary based on the frequency.  Since the routine only calculates
  74. new output values when a change is sensed, the lower frequencies (which 
  75. change less frequently) will require less processing time.
  76.  
  77.  
  78. Differences Between the Simulator and the Actual POKEY Chip:
  79. ------------------------------------------------------------  
  80.  
  81. The biggest difference between the simulator and the original hardware is 
  82. that the simulator emulates an 'ideal' POKEY chip.  All output from the 
  83. simulator is a based on a precise square wave, whereas the output from the
  84. original chip has decay.  Though the output is slightly different, I
  85. don't believe this difference is easily discernible.
  86.  
  87. Another slight difference is the 17-bit/9-bit poly.  Since the polynomial
  88. is large (2^17 bits), I choose to create the sample using a random number
  89. generator rather than a table.  I don't believe this difference is 
  90. significant.
  91.  
  92. There are also a few differences which are introduced by aliasing.  This is
  93. a direct result of using an output sampling rate which is not identical to
  94. the original sound rate.  It is most evident with high frequencies. 
  95.  
  96. A final difference is the lack of support for the High-Pass Filter 
  97. functionality.  I plan to add this in a future release if necessary.
  98.  
  99.  
  100. Sample/Test Application:
  101. ------------------------
  102.  
  103. The test program I've distributed is a 16-bit DOS application created with 
  104. the Borland 'C' compiler.  The only reason I used 16-bit was because I 
  105. already had a set of working SB drivers in 16-bit.  Since the test system
  106. is dedicated to generating sounds, the performance in 16-bit is more than
  107. adequate.
  108.  
  109.  
  110. POKEY11.C
  111. ==========
  112.  
  113. The POKEY11.C file is the heart of the POKEY Sound Emulation program.  
  114. Although the routines in the file must work together, no other files are
  115. modules are required for operation.  A header file, 'POKEY11.H', has 
  116. been included for use in other modules, and provides the necessary 
  117. function prototypes.  I've attempted to make the routines as portable as
  118. possible, so the file should compile on almost any compiler with little
  119. or no modification.  
  120.  
  121. I have made some attempts at optimizing the routines, though I am sure
  122. more optimization can be done.  They are currently only available in 'C'.
  123. I'll be happy to convert them to assembly language if desired.  Please feel 
  124. free to send me e-mail at rfries@tcmail.frco.com.
  125.  
  126. The routines are easy to use.  Detailed descriptions on the function calls   
  127. are listed below.
  128.  
  129. The POKEY11.C module can be compiled in a 32-bit or 16-bit environment.
  130. Since these routines are optimized for 32-bit use, the code will default
  131. to 32-bit.  To compile in 16-bits, use a command line option to define
  132. the variable COMP16.
  133.  
  134.  
  135. GENERAL OVERVIEW
  136. ----------------
  137.  
  138. On start-up of the system, a single call should be made to Pokey_sound_init.  
  139. This routine will prepare the structures for sound output.  This routine
  140. can be called again if necessary during warm-start or other reset.
  141.  
  142. Once in the main loop, there are two other functions that will be used.  
  143. Whenever the system needs to write to either the AUDC or AUDF values,
  144. a call should be made to the Update_pokey_sound routine.  This routine will 
  145. take care of updating the internal registers.  It will pre-calculate several
  146. values to help with optimization.
  147.  
  148. The only other routine that is called is the Pokey_process function.  This 
  149. function will fill a audio buffer with a specified number of bytes.  This
  150. function should be called whenever a new audio buffer is required.
  151.  
  152. For best results, I recommend using at least two output buffers.  Using this
  153. scheme, the sound card can be playing one buffer while the system is filling
  154. the other.
  155.  
  156.  
  157. DETAILED FUNCTION DESCRIPTIONS
  158. ------------------------------
  159.  
  160. Pokey_sound_init(uint32 freq17, uint16 playback_freq)
  161. --------------------------------------------------------
  162.  
  163. This function initializes the structures used by the PokeySound routines.
  164. This function takes two parameters: the main clock frequency and the 
  165. playback frequency.  
  166.  
  167. The main clock frequency is the frequency of the 1.79MHz source clock.  
  168. To provide exact results, freq17 should be set equal to 1789790 Hz.  As an 
  169. alternative, freq17 can be set to an approximate frequency of 1787520 Hz.  
  170. Using this approximate frequency will reduce aliasing and thus produce a 
  171. clearer output signal.
  172.  
  173. A constant has been defined for both of these values for your convenience.
  174. The names are FREQ_17_EXACT and FREQ_17_APPROX.
  175.  
  176. The playback frequency is the frequency of the sound playback (the frequency 
  177. used by the sound card).  For best results, the playback frequency should 
  178. be an even division of the main clock frequency.  Since most of the sounds
  179. will be generated using the 64kHz clock, I also recommend making the 
  180. playback frequency an even division of the 64kHz clock.
  181.  
  182. The 64kHz clock is exactly equal to the main clock divided by 28.  For
  183. the playback frequency, I recommend one of the following values:
  184.  
  185. 1) FREQ_17_APPROX / (28*1), which is equal to 63840.  Of course, most sound 
  186.    cards can't reproduce this frequency.
  187.  
  188. 2) FREQ_17_APPROX / (28*2), which is equal to 31920.  All of the newer cards
  189.    will support this frequency.  
  190.  
  191. 3) FREQ_17_APPROX / (28*3), which is equal to 21280.  All of the SB 
  192.    compatibles should support this frequency.
  193.  
  194. 4) FREQ_17_APPROX / (28*4), which is equal to 15960.  This may be the
  195.    best choice, as it offers good sound reproduction with good performance.
  196.   
  197. Of course, these options also assume you are using the approximate
  198. frequency for the main clock as well.  Any of these choices will offer the
  199. best results when the main 64kHz clock is used, reasonable results when the
  200. 15kHz clock is selected, and marginal results when the 1.79MHz clock is
  201. selected (the only way to produce good results in all cases is to set the
  202. playback frequency to 1.79MHz!)
  203.  
  204. Feel free to experiment to find other alternatives as well.
  205.  
  206. This function has no return value (void).
  207.  
  208.  
  209. Update_pokey_sound (uint16 addr, uint8 val)
  210. -----------------------------------------
  211.  
  212. This function should be called each time an AUDC, AUDF or AUDCTL value
  213. changes.  This function takes two parameters: the address to change and
  214. the new value.  The address should be one of the following values:
  215.  
  216.                   Addr     Description
  217.                  ------    -----------
  218.                  0xd200       AUDF1
  219.                  0xd201       AUDC1
  220.                  0xd202       AUDF2
  221.                  0xd203       AUDC2
  222.                  0xd204       AUDF3
  223.                  0xd205       AUDC3
  224.                  0xd206       AUDF4
  225.                  0xd207       AUDC4
  226.                  0xd208       AUDCTL
  227.  
  228. Any values outside of this range will be ignored.
  229.  
  230. The routine pre-calculates several values that are needed by the 
  231. processing function.  This is done to optimize performance.
  232.  
  233. This function has no return value (void).
  234.  
  235.  
  236. Pokey_process (unsigned char *buffer, uint16 n)
  237. ---------------------------------------------
  238.  
  239. This function calculates and fills a buffer with unsigned 8-bit mono audio.
  240. This function takes two parameters: a pointer to the buffer to fill and
  241. the size of the buffer (limited to 65535).  This function fills the 
  242. buffer based on the requested size and returns.  It automatically
  243. updates the pointers for the next call, so subsequent calls to this function
  244. will provide a continuous stream of data.
  245.  
  246. The size of the buffer that is needed depends on the playback frequency.
  247. It is best to keep the buffer as small as possible to maximize response time
  248. to changes in the sound.  Of course, the minimum size is dependent on
  249. system and emulator performance.
  250.  
  251. Selecting the correct buffer size is a careful balance.  Selecting a buffer
  252. size that is too small will produce noticeable clicks in the output, though
  253. selecting a size that is too large will cause a poor response time and 
  254. possible delays in the system when the new buffer is filled.
  255.  
  256. This function has no return value (void).
  257.  
  258.  
  259. License Information and Copyright Notice
  260. ========================================
  261.  
  262. PokeySound is Copyright(c) 1996 by Ron Fries
  263.  
  264. This library is free software; you can redistribute it and/or modify it under 
  265. the terms of version 2 of the GNU Library General Public License as published 
  266. by the Free Software Foundation.
  267.  
  268. This library is distributed in the hope that it will be useful, but WITHOUT 
  269. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  270. FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for more 
  271. details.
  272.  
  273. To obtain a copy of the GNU Library General Public License, write to the Free 
  274. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  275.  
  276. Any permitted reproduction of these routines, in whole or in part, must bear 
  277. this legend.  
  278.