*8-bit Sound & Fury

Apple II Sound & Music Software

home | software | hardware | programming | documentation | audio files | links
[ PROGRAMMING ] One of the Apple II's most endearing features is its ease of programming. Although quite limited by modern standards, the Apple II's built-in AppleSoft BASIC and Mini-Assembler can be used to create some very interesting and useful programs. Of course more advanced languages and tools are available (see below), but since this page is geared toward the beginner we'll try to stick to the basics here. [TOP]

Apple II Programming Examples


A Simple Tone Routine

Not sure where this came from so I can't properly credit it, though I'm sure I'm not giving away any trade secrets by publishing it here. Basically this just pokes in a Machine Language tone generator from AppleSoft which can, in turn, be called from the same AppleSoft program:
    10 FOR L = 770 TO 790: READ V: POKE L,V: NEXT L
    20 DATA 173,48,192,136,208,5,206,1,3,240,9
    30 DATA 202,208,245,174,0,3,76,2,3,96
Now we have the tone generating routine in memory all we have to do is tell it what note to play and for how long. This is done like so:
    40 POKE 768,F: POKE 769,D: CALL 770
In the above line the variable F stands for Frequency and D stands for Duration. Either variable can be any integer between 0 and 255. Note duration can be determined [more-or-less] by dividing, so that if a whole note is equal to a duration of 216, then a half note would be 108 and a quarter note would be 54, and so on. Tone frequency is fairly approximate, but you can use the chart below to get "close enough for rock'n'roll".
NOTE
OCTAVE 1
OCTAVE 2
OCTAVE 3
C
255
128
64
C# / Db
241
121
60
D
227
114
57
D# / Eb
214
108
54
E
202
102
51
F
191
96
48
F# / Gb
180
91
45
G
170
85
43
G# / Ab
161
81
40
A
152
76
38
A# / Bb
143
72
36
B
135
68
34
 
Of course playing square-wave tones on the Apple II's internal speaker can be less than exciting, but it is possible to make some interesting technoesque bleeps with a little imagination and some rudimentary programming knowledge... run with it![TOP]


Using DAC522 from AppleSoft

DAC522.8C is the stand-alone version of DAC522 included on the Sound Editor diskette. Using DAC522.8C from within your own BASIC program is fairly painless, even for the novice hacker.
First off, you need to load DAC522.8C into memory, which you might accomplish with the following line of code:
    10 PRINT CHR$ (4) "BLOAD DAC522.8C"
We also need an audio file in memory for DAC522 to play back. In this case we'll BLOAD the file named MYSAMPLE into memory at address $2000, which is normally the start of HiRes Graphics screen 1.
    20 PRINT CHR$ (4) "BLOAD MYSAMPLE, A$2000"
Now all we need to do is to provide DAC522 with the start and end bytes of the sample, which is a little more involved but hardly rocket surgery. The following is transcribed from an email in which Michael Mahon patiently explained it all to me:
You need to BLOAD each sound file at its own unique (non-overlapping, of course) address. When you want to play a sound, POKE its begin address into locations 6 and 7, and its end address into locations 8 and 9. Then call the PLAY routine at $8C00.

Of course all addresses need to be converted to decimal for BASIC to POKE and CALL them (no problem with 6,7,8,and 9--they're already in both hex and decimal! ;-), and since the addresses POKEd into low memory are two-byte addresses, they need to be split into their high and low bytes, and POKEd into memory low-byte first.

For example, say that you want to play a sound BLOADed at $8000 with a length of 600 bytes (decimal). Then the start address is 32768 (decimal) and the end address is 32768+600, or 33368.

The high byte of the start address is INT(32768/256), or 128, and the low byte of the start address is 32768-256*INT(32768/256), or 0.

So you would need to POKE 6,0: POKE 7,128 to set up the start address.

Similarly, you would POKE 8,88: POKE 9,130 to set up the end address.

Then to play the sound, you call $8C00 by executing CALL 35840 (that's 8*4096 + 12*256 + 0*16 + 0, if you see what I mean).

Of course, all of this can be done by a few BASIC statements that you call with a GOSUB after setting up variables for the start and end addresses (I used SA and EN in SOUND.EDITOR for these variables, and another GOSUB subroutine to do the 2-byte POKEs, to save space and make the program structure more logical. -MJM

There you have it... simplicity itself! So here is a complete listing of a program that will do nothing less than load and play a 2K audio file:
    110 PRINT CHR$ (4) "BLOAD DAC522.8C"
    120 PRINT CHR$ (4) "BLOAD MYSAMPLE, A$2000"
    130 SA = 8192 : REM START ADDRESS
    140 EN = 10240 : REM END ADDRESS
    150 POKE 6, SA - 256 * INT (SA/256)
    160 POKE 7, INT (SA/256)
    170 POKE 8, EN - 256 * INT (EA/256)
    180 POKE 9, INT (EN/256)
    190 CALL 35840
    200 END
Of course, you'll probably want to do more than play one sample and quit, but hopefully this will show just how easy it is to get going... [TOP]

Apple II Programming Tools

Program Writer

Beagle Brothers' Program Writer is essentially just a text editor for AppleSoft programs. Anyone who's gone much beyond PRINT "HELLO WORLD" has undoubtedly discovered the limitations of AppleSoft's built-in editor. Program Writer actually makes writing code fun, and can drastically simplify revisions of existing code.[TOP]

Program Writer
Program Writer Docs


S-C Assembler

Bob Sander-Cederloff's S-C Assembler is an excellent tool for those wanting to explore the fascinating world of 6502 Assembly Language. In addition to being very easy to use, S-C Assembler comes with a wealth of supporting information contained in the monthly Assembly Lines magazine/diskettes which have now been made available to all.[TOP]