Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed
Embedded Systems, 6502 breadboard computer · Lecture 28 of 29 · 18:58
28 of 29: Computer noises
Study guide
What this lecture covers
The simplest way to make a digital computer produce sound is to toggle a pin on and off at an audio frequency, which is exactly a square wave. The lecture uses the 6522 VIA's timer to generate that square wave in hardware, adds a BEEP instruction to Microsoft BASIC that sets the timer's frequency and duration, and then amplifies the output with an LM386 chip to drive a small speaker.
This follows the earlier videos that add custom instructions to BASIC and builds on the same parameter-parsing techniques. By the end, BASIC programs on the breadboard computer can play notes and simple tunes through a speaker.
Key ideas
- Square wave from a timer: toggling a single output bit at a fixed rate produces a square wave, and adjusting the rate changes the pitch.
- 6522 timer 1, free-run mode: setting both control bits in the auxiliary control register makes timer 1 continuously reload itself and automatically invert the PB7 output each time it reaches zero.
- Counter registers set frequency: writing a 16-bit value to the timer 1 counter registers sets how many clock cycles pass between each toggle, and starts the timer.
- BEEP instruction: parses a 16-bit parameter with
formula eval/make int, writes it into the timer counter, and sets the auxiliary control register bits to start the square wave; a value of zero stops it. - Amplification: the VIA can't drive a speaker directly, so an LM386 audio amplifier chip, wired per its data sheet's minimal application circuit, boosts the square wave enough to drive an 8-ohm speaker.
- Duration parameter: a second
BEEPparameter (0-255) drives a nested delay loop (an outer and inner loop) so a beep can play for a controlled length of time before turning off automatically. - Zero-duration special case: passing a delay of zero is treated as "leave the sound on" rather than an instant on/off.
- Playing tunes: with note and duration arrays and a loop calling
BEEP, BASIC programs can play sequences of notes, as demonstrated with a 78-note tune.
Walkthrough
Introduction to sound (0:00)
Generating a square wave by toggling a bit is presented as the simplest way to make a computer produce sound, using the existing 6522 VIA already wired into the computer.
VIA timer configuration (0:35)
The 6522 data sheet shows timer 1's counter can auto-reload and invert output on pin PB7 each time it reaches zero. Since PB7 isn't used by the LCD wiring, it's free to drive the square wave. The auxiliary control register's bits 6 and 7 are set to put timer 1 in free-run mode with PB7 output enabled.
Programming the beep (2:37)
A new BEEP instruction is added to BASIC in a new sound.s file, following the pattern from earlier custom-instruction videos. It parses a 16-bit integer parameter, writes it to the timer counter registers at $6004/$6005, and sets the auxiliary control register to start the square wave; a zero parameter branches to a routine that silences the timer instead.
Implementation and testing (5:03)
After flashing the ROM, BEEP 100 produces a visible square wave on an oscilloscope connected to PB7, with the period matching the parameter divided by the 1 MHz clock.
Frequency analysis (8:36)
Calculating the counter value for a target frequency (such as 440 Hz, the musical note A) shows the square wave's period is measured rising-edge to rising-edge, so the counter value needs to be halved to get the intended frequency.
Audio amplification (9:50)
Since the VIA can only source a few milliamps, an LM386 audio amplifier is wired using the minimal circuit from its data sheet, with a potentiometer as volume control on the input and a capacitor/resistor output filter, to drive a small speaker audibly.
Adding duration control (12:05)
A second BEEP parameter sets a delay duration using a nested loop (up to 255 outer iterations, each running 255 inner iterations) so the beep automatically stops after a controlled time. Edge cases are handled: a zero frequency still delays in silence, and a zero duration leaves the tone on instead of instantly toggling it off.
Playing music in BASIC (16:17)
With BEEP freq, duration working, a BASIC program loads note and duration values into two 78-element arrays from DATA statements and loops through them calling BEEP for each, playing a short tune.
Before you watch
- Watch the earlier videos on adding custom instructions to Microsoft BASIC and parsing instruction parameters, since this lecture reuses that pattern for
BEEP. - Be familiar with the 6522 VIA's registers from the LCD interfacing videos in this course.
- Basic understanding of what a square wave is and how frequency relates to period is useful.
Check your understanding
- How does setting the auxiliary control register's bits 6 and 7 turn timer 1 into a square-wave generator on PB7?
- Why does the counter value for a target frequency need to be divided by two?
- What does the LM386 amplifier add that the VIA alone can't provide?
- How does the nested delay loop translate the second
BEEPparameter into a beep duration? - Why does a zero-duration parameter leave the sound on instead of turning it off immediately?
Chapters
- 0:00 Introduction to sound
- 0:35 VIA timer configuration
- 2:37 Programming the beep
- 5:03 Implementation and testing
- 8:36 Frequency analysis
- 9:50 Audio amplification
- 12:05 Adding duration control
- 16:17 Playing music in BASIC
From the YouTube description
I show the simplest way to get a computer to make noise—amplifying a square wave. I add a BEEP instruction to MSBASIC that lets you generate a beep at a particular frequency and use it to make some very rudimentary music.
Support these videos on Patreon: https://www.patreon.com/beneater or https://eater.net/support for other ways to support.
------------------
Social media:
Website: https://www.eater.net
Twitter: https://x.com/beneater
Patreon: https://patreon.com/beneater
Reddit: https://www.reddit.com/r/beneater
Special thanks to these supporters for making this video possible:
Adrien Friggeri, Aleksey Smolenchuk, aliceitc, Anthony Weems, anula, Axel Hanikel, Ben, Ben Cochran, Ben Kamens, Benjamin D. Williams, Benjamin Elder, Benjamin Keil, Benji Bromberg, Bill Cooksey, Binh Tran, Bradley Stach, Brian Haug, Carl Fooks, Carsten Schwender, Chad Fertig, Chai, Chris Anders, Chris Lajoie, Craig Hawco, criis, Cristi Cobzarenco, Daniel Tang, Dave Westwood, David Clark, David Cox, David Dawkins, David House, David Sastre Medina, David Turner, Dean Winger, Deep Kalra, DemoniacDeath, Dennis Henderson, Derek Chandler, Dilip Gowda, Dirk Sperling, Dmitry Guyvoronsky, Dustin Schoenbrun, Dzevad Trumic, Emilio Mendoza, Eric Dynowski, Erik Chancy, Ethan Sifferman, Eugene Bulkin, Evan Serrano, Evan Thayer, Eveli László, garmata, George Harris, George Miroshnykov, Glen Jarvis, Glenn Davidson, Gregory Burns, GusGold, Henry Lerner, Hovis Biddle, Ingo Eble, Ingram Leedy, Isaac Parker, Jack McKinney, James Capuder, James Chacon, Jason DeStefano, Jason Grim, Jason Thorpe, JavaXP, Jaxon Ketterman, Jefferson Hunt, jemmons, Jesse Miller, Jim Kelly, Jim Knowler, Jim Rees, Joe Beda, Joel, John Haugabook, John Rivoire, Jon Dugan, Jonn Miller, Josh Smith, Justin Berry, Justin Williams, Kai Wells, Kefen, Kennard Smith, Kenneth Christensen, Kristian Høy Horsberg, Kyle Kellogg, Lambda GPU Workstations, Larry Scherr, László Bácsi, Lithou, Marcel Wysocki, Mark Day, Mark Frankford, melvin2001, MICHAEL SLASS, Michael Tedder, Michael Timbrook, mikebad, Miles Macchiaroli, Mlok Karel, Nate Welch, Nicholas Counts, Nicholas Moresco, Nick Chapman, NormalLuser, Olivier HUBER, Örn Arnarson, Owen Arnett, Pat Nakajima, Paul Heller, Paul Pluzhnikov, Pete Dietl, Philip Hofstetter, Ponytail Bob, ProgrammerDor, Randal Masutani, Randy True, raoulvp, real_huitz, Richard Wagoner, Robert Diaz, Robert Walsh, Robey Pointer, Roland Munsil, Ryan, Ryan Morrison, Sachin Chitale, Sagnik Bhattacharya, Sam Sturgis, Scott Holmes, Sean Patrick O’Brien, Sebastian Kopeć, SergeantBiggs, Sergey Kruk, Sergey Ten, SonOfSofaman, sorek.uk, spookybassoon, Stanley Benoit, Stefan Nesinger, Stéphane Dumont, Stephen Kovalcik, Stephen Riley, Steve Jones, Steve Harris, This person's name is too hard to pronounce, Thomas Eriksen, Tim Oriol, Tim Sanders, Tim Walkowski, Tom, Tom Smith, tremors08, Trevor Johnston, Trey Webb, tryonlinux, Tyler Latham, Vaida Narušė, Victor Kazakov, Warren Miller, Wraithan McCarroll
← 27 of 29: How input buffering works · 29 of 29: Sound synth with a 6502 →
