a2_assembly_language by Unknown

a2_assembly_language by Unknown

Author:Unknown
Language: eng
Format: epub


108

Using Sound in Your Programs / 109

publication called Apple Assembly Line. Those programs, and some of the explanatory text, come from the February 1981 issue of Apple Assembly Line.

The speaker hardware in the Apple is very simple. A flip-flop, which is a device that repeatedly alternates between two states (e.g. ON and OFF), controls the current that is supplied to the coil of the speaker. The flip-flop is connected in such a way, that it reverses the flow of current through the speaker's coil. This is important, because the direction of the current flow determines whether the cone of the speaker is pulled in or out. If we "toggle" the flip-flop and cause it to continuously reverse the flow of current through the speaker, we can cause the speaker to produce audible sound. The rate at which we toggle, determines the frequency of the sound. And, by changing the toggling of the speaker dynamically, it is possible to produce some very complex sounds. The toggling of the speaker is accomplished by accessing location $C030 with any of the load, store or BIT instructions.

How to generate a simple tone

To generate a simple tone, it is only necessary to toggle the speaker at a rate that is low enough so that it falls within the range of 20 to 20,000 Hertz (cycles per second), which is the range of signals that the human ear can detect. The program SIMPLE TONE ROUTINE generates a tone burst of 128 cycles (this is equal to 256 half-cycles). Each half cycle here consists of 1288 Apple clock pulses. Since the internal Apple clock frequency is about 1 MHz, the frequency of sound that is produced is about 338 Hz.

The program starts out by setting the Y-register, which is used as a half-cycle counter, to zero (line 1210). In line 1220, the X-register, which is used as a delay counter, is also set to zero. The sound producing section of code starts with LOOP 1 online 1230, where the speaker is toggled. After toggling the speaker, the program waits, while LOOP2 is executed (lines 1240 and 1250). LOOP2 is only used to produce a time delay, which will be equal to the amount of time it takes to decrement the X-register to zero. After the delay, the Y-register is decremented (line 1260) and the speaker is toggled once more (line 1270). This continues until 256 half-cycles (128 cycles) are completed.

Figuring out the frequency

At this point, you might be curious how the frequency is figured out. This is done by determining the time taken up by each half cycle, doubling it and then taking its inverse. Let's go through a sample calculation.

To start with, we have to add up all of the 6502 cycles for each instruction in the sound producing loop (lines 1230 to 1270). To avoid confusion between the 6502 cycles and the cycles of the sound producing loop, each half cycle will be referred to as a pulse. Thus, there are two pulses per sound producing cycle (Hz).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.