Working with the Pokey v0.005 by Josh McCormick Copyright 1999 by Arcade Gameshop Corporation This document is Open Content (OC); your contributions are both invited and welcomed. You many redistribute it and modify it under the terms of the OpenContent License (OPL) v1.0. See http://arcade.gameshop.com/opl.html for details. Have improvements? Send them to "jmccorm@galstar.com". --------------------------------------------------------------------------- CREDITS: Oct 07, 1999 - Clay Cowgill: Excellent Pokey input advice and code. --------------------------------------------------------------------------- INTRODUCTION TO THE POKEY ------------------------- The Pokey "POts and KEYs" is a multi-purpose chip that was borrowed from Atari's line of home computers. Here's how "Mapping the Atari" (an excellent reference book to the Atari 8-bit computer hardware) describes the Pokey: POKEY is a digital I/O chip that controls the audio frequency and control registers, frequency dividers, poly noise counters, pot (paddle) controllers, the random number generator, keyboard scan, serial port I/O, and the IRQ interrupts. If you are wanting to experiment with the Pokey (especially sounds), your best choice for a development tool may be an actual Atari home computer (available *cheap* on eBay). Second to that would be an Atari 8-bit home computer emulator. SIDE TOPIC: PURCHASING AN ATARI 8-BIT HOME COMPUTER. If you don't know much about the Atari line of products, your *safest bets* are the 800XL and the 130XE. They have plenty of memory and built-in BASIC, but they also suffer from mushy keyboards. Your *best bets* is an Atari 800 (not XL) _if_ it has at least 48k and you get a BASIC cartridge for it. The Atari 1200XL is also a great choice _if_ you get a BASIC cartridge. Avoid the 400 (membrane keyboard from hell), and the 600XL (an 800XL w/16k). Avoid the ST, TT (16-bit and 32-bit hardware). If you come across an "Assmebler Editor" cartridge and manual, snag it. They're not too common these days. Whichever model you get, be sure a power supply is included in the deal. And if you don't get a TV/GAME switchbox, you'll have to pick one up at your local Radio Shack. One major difference between the Atari Home Computer and the Tempest hardware is that the home computer only has one Pokey chip, and it is located at $D200 (53760 decimal). Keep this in mind when going back and forth between references and actual code. SIDE TOPIC: UPGRADING AN ATARI 8-BIT HOME COMPUTER TO DUAL POKEYS It is possible to upgrade an Atari 8-bit home computer to have a second Pokey chip at $D210. The "Pokey Stereo Sound" upgrade at http://aegis.mcs.kent.edu/~clisowsk/8bit/pokey.html covers this procedure. Chances are you WON'T need to do this, since a single Pokey is good enough to model with. SIDE TOPIC: PURCHASING A POKEY CHIP Information taken from Gregg Woodcock's Tempest Repair FAQ. Various places that deal in Atari 8-bit home computers will sell these. One such site ( http://www.myatari.com ) caries these for $5 apiece, $8 shipping, $20 minimum order (before shipping). Possible (unverified) other sources: > Best Electronics > 408.243.6950 (Atari POKEYs $5+$? S/H+$4.75 COD; $12 min) > American Techna-Vision > 510.352.3787 (Atari POKEYs $5; #AK675) INITIALIZING THE POKEY ---------------------- If you want to be able to play any sounds, the first thing you will need to do with the Pokey is to initialize it. This involves setting the Pokey control register ($60CF for Pokey #1, $60DF for Pokey #2) to "3", zeroing out the frequency and control for each channel. [Code taken directly from Tempest source code.] CDBA A9:07 LDA:imm #07 ; Enable sound and FAST ; potentiometer scanning. CDBC 8D:CF 60 STA:abs $60CF ; Init Pokey #1 CDBF 8D:DF 60 STA:abs $60DF ; Init Pokey #2 CDC2 A2:07 LDX:imm #07 ; Zero out the volume/distortion CDC4 A9:00 LDA:imm #00 ; and pitch for all channels CDC6 9D:C0 60 STA:abs,x $60C0,X ; on both Pokeys. CDC9 9D:D0 60 STA:abs,x $60D0,X ; CDCC 95:C0 STA:zp,x Zp RAM 00C0 CDCE 95:D0 STA:zp,x Zp RAM 00D0 CDD0 CA: DEX:imp X=X-1 CDD1 10:F3 BPL:rel Branch->$CDC6 You'll also want to initialize the control channel to a neutral state: CDD3 A9:00 LDA:imm #00 ; Zero out AUDCTL for pokey #1. CDD5 8D:C8 60 STA:abs $60C8 ; CDD8 A9:00 LDA:imm #00 ; Zero out AUDCTL for pokey #2. CDDA 8D:D8 60 STA:abs $60D8 ; MAKING SOUNDS WITH THE POKEY ---------------------------- Each Pokey chip has four voices. Each voice has a register that controls frequency, and a register that controls both distortion and volume. Here's how the registers map out. AUDIO FREQUENCY CHANNEL REGISTERS (write) $60C0 - Audio Frequency Channel #1 (AUDF1-1) for Pokey #1 $60C2 - Audio Frequency Channel #2 (AUDF2-1) for Pokey #1 $60C4 - Audio Frequency Channel #3 (AUDF3-1) for Pokey #1 $60C6 - Audio Frequency Channel #4 (AUDF4-1) for Pokey #1 $60D0 - Audio Frequency Channel #5 (AUDF1-2) for Pokey #2 $60D2 - Audio Frequency Channel #6 (AUDF2-2) for Pokey #2 $60D4 - Audio Frequency Channel #7 (AUDF3-2) for Pokey #2 $60D6 - Audio Frequency Channel #8 (AUDF4-2) for Pokey #2 "Mapping the Atari" description of AUDFn-n registers: This is actually a number (N) used in a "divide by N circuit"; for every N pulses coming in (as set by the POKEY clock AUDCTL-1 and AUDCTL-2), one pulse goes out. As N gets larger, output pulses will decrease, and thus the sound produced will be a lower note. N can be in the range from one to 256; POKEY adds one to the value in the AUDFn-n register. My description of the AUDFn-n registers: The number you put in here will control the frequency of the tone (or noise) generated. Small numbers create high pitched noises. Large numbers create low pitched noises. Tone frequencies can also be modified by each channel's control register, and each Pokey's master control register. AUDIO CONTROL CHANNEL REGISTERS (write) $60C1 - Audio Control Channel #1 (AUDC1-1) for Pokey #1 $60C3 - Audio Control Channel #2 (AUDC2-1) for Pokey #1 $60C5 - Audio Control Channel #3 (AUDC3-1) for Pokey #1 $60C7 - Audio Control Channel #4 (AUDC4-1) for Pokey #1 $60D1 - Audio Control Channel #5 (AUDC1-2) for Pokey #2 $60D3 - Audio Control Channel #6 (AUDC2-2) for Pokey #2 $60D5 - Audio Control Channel #7 (AUDC3-2) for Pokey #2 $60D7 - Audio Control Channel #8 (AUDC4-2) for Pokey #2 "Mapping the Atari" description of AUDCn-n registers: Each AUDF register has an associated control register which sets volume and distortion levels. The bit assignment is: BIT: 7 6 5 4 3 2 1 0 ---------- ------- ----------- Distortion Volume Volume (noise) Only level 0 0 0 0 0 0 0 0 Lowest 0 0 1 0 0 0 1 etc. to: etc. to: 1 1 1 1 1 1 1 1 Highest (forced output) The values for the distortion bits are as follows. The first process is to divide the clock value by the frequency, then mark the output using the polys in the order below. Finally, the result is divided by two. BIT: 7 6 5 0 0 0 five bit, then 17 bit, polys 0 0 1 five bit polys only 0 1 0 five bit, then four bit, polys 0 1 1 five bit poly only 1 0 0 17 bit poly only 1 0 1 no poly counters (pure tone) 1 1 0 four bit poly only 1 1 1 no poly counters (pure tone) In general, the tones become more regular (a recognizible droning becomes apparent) with fewer and lower value polys masking the output. This is all the more obvious at low frequency ranges. POKE with 160 (#A0) or 224 (#E0) plus the volume for pure tones. My description of AUDCn-n registers: Bits 0-3 are straightforward. They control the volume level for each channel. NOTE: Do not exceed a combined volume level of 32 on the four channels. Bit 4 is the D/A bit. What happens when bit 4 is set is that the frequency register is completely ignored, as well as the distortion bits in this register. Instead, the volume bits (0-3) are used to directly feed a D/A converter. If you set the volume to "F", the speaker is going to cave out to its maximum. If you set the volume to "0", the speaker will relax. This allows you to directly work the speaker however you want, and the primary reason for this would be to play digitized sounds. It should be possible to combine channels for an even higher resolution waveform. The Atari home computer game of "Whomper Stomper" gives a fair demonstration of this. NOTE: If you are going to play digitized sounds, you're going to either need a very tight loop of code with consistant timing, or you're going to have to work some timing interrupts. And nothing will chew up memory faster than storing digitized sounds. Bits 5-7 are the distortion bits. In short, play around with these. You'll get anything from pure tones, to hissing sounds, to purring sounds. Play until you find something you want to use. [NOTE: I'll work on getting together a description of the sounds created with the various bits.] MASTER AUDIO CONTROL (write) $60C8 - Master Audio Control #1 (AUDCTL-1) for Pokey #1 $60D8 - Master Audio Control #1 (AUDCTL-1) for Pokey #1 "Mapping the Atari" description of AUDCTL-n registers: The bit assignment is: Bit Description 7 Makes the 17 bit poly counter into nine bit poly (see below) 6 Clock channel one with 1.79MHz 5 Clock channel three with 1.79Mhz 4 Join channels two and one (16 bit) 3 Join channels four and three (16 bit) 2 Insert high pass filter into channel one, clocked by channel two 1 Insert high pass filter into channel three, clock by channel four 0 Switch main clock baser from 64KHz to 15KHz Poly (polynomial) counters are used as a source of random pulses for noise generation. There are three polys: four, five and 17 bits long. The shorter polys create repeatable sound patterns, while the longer poly has no apparent repetition. Therefore, setting BIT 7 above, making the 17-bit into a nine-pit poly will make the pattern in the distortion more evident. You chose which poly(s) to use by setting the high three bits in the AUDCn-n registers. The 17-bit poly is also used in the generation of random numbers. The clock bits allow the user to speed up or slow down the clock timers, respectively, making higher or lower frequency franges possible. Setting the channels to the 1.79MHz will produce a much higher sound, the 64Khz clock will be lower, and the 15KHz clock the lowest. The clock is also used when setting the frequency for the AUDFn-n timers. Two bits (three and four) allow the user to combine channels one and two or three and four for what amounts to a nine octave range instead of the usual five. Here's an example from De Re Atari of this increased range, which uses two paddles to change the frequency: the right paddle makes coarse adjustements, the left paddle makes fine adjustments: [EDITOR'S NOTE: The address for the Pokey is different for the Atari home computer than the Tempest arcade game. Convert as needed.] 10 SOUND 0,0,0,0:POKE 53768,80:REM Set clock and join 1&2 channels 20 POKE 53761,160:POKE 53763,168:REM Turn of channel 1. Set 2 to pure tone. 30 POKE 53760,PADDLE(0):POKE 53762,PADDLE(1):GOTO 30 High pass filters allow only frequencies higher than the clock value to pass through. These are mostly used for special effects. Try: 10 SOUND 0,0,0,0:POKE 53768,4:REM High pass filter on Channel 1 20 POKE 53761,168:POKE 53765.168:REM Pure tones 30 POKE 53760,254:POKE 54764,127:REM Sound values 40 GOTO 40 See the excellent chapter on sound in "De Re Atari": it is the best explanation of sound functions in the Atari available. See also the "Hardware Manual" for complete details. My description of the AUDCTL-n registers: In general, these are used for controlling pitch on a much larger scale (either chip-wide frequency adjustments or combining two channels), or filtering. Most everyday programming doesn't need to touch these, but you'll probably be able to pull some good sound effects by working these. READING INPUTS WITH THE POKEY ----------------------------- Each Pokey chip can handle up to eight analog/paddle inputs. Of course, this also means that it can handle eight digital inputs. To grab these values, you need to do a small bit of work. The first is to write (any value) to the POTGO ($60CB for Pokey #1, $60DB for Pokey #2) register. This will reset the POT registers to 0, discharge the capacitors, and start rescanning the potenionmeters. Then, you'll need to read the ALLPOT ($60C8 for Pokey #1, $60D8 for Pokey #2) register. This bitmap contains the ready state of the POTn registers (POT #7 is at the 7th bit, POT #0 is at the 0th bit). If the bit is 0, the corresponding register's value is valid. If the bit is 1, the register is in the middle of counting, and is not valid. Normally, this is a very slow process. On the Atari home computer, it takes 228 scan lines before the data is safe to be read. What we want to do is enable fast pot scanning, which is done by writing #07 into $60Cf and $60Df. This slightly decreases the accuracy of the scan, but it makes it available in only 2 scan lines. Just what we want. If you'll look in the sound initialization routines under the sound section above, you'll see that the very first thing Tempest does when it initializes the sound hardware is to set the Pokey into fast scan mode. Onto the POTn-n registers. Under normal scanning mode, they should store a value between 0 (closed circuit) and 228 (open circuit). It is *believed* to still be the same for fast scanning mode. The locations of these registers and the hardware that is believed to be attached is as follows: 60C0 - Encoder Wheel 60D0 - D/E2 Switch #2 60C1 - Encoder Wheel 60D1 - D/E2 Switch #3 60C2 - Encoder Wheel 60D2 - D/E2 Switch #4 60C3 - Encoder Wheel 60D3 - Zapper 60C4 - Cocktail Detect 60D4 - Fire 60C5 - D/E2 Switch #1 60D5 - Player 1 Start 60C6 - [unused] 60D6 - Player 2 Start 60C7 - [unused] 60D7 - [unused] You may have noticed that these are the exact same addresses you use to WRITE to the sound register. That is correct. A write operation to the Pokey is used for sound. A READ operation is used to get input. NOTE: Clay Cowgill points out that if you are just reading binary inputs, there is no need to check the status of all the pots individualls. The ALLPOT register will give you the status of all the inputs on that Pokey in one read.