top of page

Watermelon Man Assembly

Writer's picture: Isaiah RegachoIsaiah Regacho

Introduction

Studying microcontrollers for the first time in 2016 was an incredible experience. Diving into the CPU architecture and relating the program counter, registers, ALU, and address bus to the sequential logic circuits demystified the connection between programming and digital electronics.


One of the chapters of the course was learning assembly. We were studying the MSP430 from TI which is often mocked for its simplicity compared to the latest and greatest microcontrollers. Each year, all technology decays toward obsolescence, but new students wrestle with the MSP430 assembly language every Fall. As they succeed or fail to turn bits on and off meaningfully, the appreciation for assembly and the C compiler grows.


Music on the MSP430

I knew when I was a student that if something could be turned on and off, it could also be turned on and off at 440 Hz. 440 Hz is audible and the standard tuning for A. If a note could be played, a scale was possible. If a scale was possible, a melody could be played. The roadmap was clear, and the end goal was to play Watermelon Man by Herbie Hancock.


Fast forward a couple of years, a newer version of the MSP430 Music Player was developed. The latest version receives commands from a Python GUI and can play two tones on a single pin. Back to Watermelon Man in Assembly.


The Algo-rhythm

The following section will describe the major steps (pun intended).


Bar 1: Toggle a pin at a given frequency.


The strategy is to pre-calculate how many times we stay inside a loop that does nothing but wait and bide its time. After waiting for half of the period, toggle the bit.


1. Load a register (R8) with the pre-calculated number of loop iterations (R4) for a given note.

2. Toggle the note. (P3.1)

3. Enter the delay loop.

4. Decrement the number of iterations (R8) remaining to perform.

5. If loops remain, repeat from 3.

6. If done, repeat from 1.



Next, we need the clock frequency of the MSP430 -> 1.048 MHz. The cycles for this process are calculated as 3 x R4 + 8. That is 8 cycles of overhead to prepare the note and repeat the whole process and 3 cycles of the delay loop.


We need to calculate how many clock cycles are needed for the half-period.


1048000 Hz/2*440 Hz = 1191 cycles.


Now calculate the times the loop needs to repeat for 2382 cycles.


1191 = 3 x R4 + 8


R4 = (1191 – 8)/3 = 394 = 0x18A


Bar 2: Play a note for four seconds.


If only there was a way to calculate how many times the cycle of note needs to repeat for every second.


440 Hz = 440 cycles /second


Nice.


Remember the algorithm from Step 1 is repeating on the half-cycle to toggle the pin so the result needs to be doubled.


440 Hz * 4 *2 = 3520 = 0xDC0


We need 4 seconds for whole notes at 60 BPM. R5 should be loaded with the number of half-cycles to sustain a note for four seconds.


Bar 3: Play a note for a calculated duration.

With the note half-cycles for four seconds calculated for every note, we need a variety of durations to create an interesting melody. Thankfully rhythms in music are very mathematical with many of them being achieved by dividing by two. As of right now (1:01 AM October 18, 2023), it ain’t got that swing.


The strategy is to divide the precalculated number (whole notes at 60 BPM) to half-notes, quarter-notes, etc. If we have odd durations like tied notes equivalent to the duration of 5 quarter-notes just multiply.



R7 contains the number of divisions by two needed to perform. The MSP430 has limited computational power but it can still shift bits to the right. R5 was preloaded with the number of half-cycles for four seconds. The division halves the duration R7 times.


This base value R5 can be multiplied by adding its value to R6, R9 times.


Bar 4: Putting it all together.


With confusion at an all-time high, lets analyze how to play a note.





1. Push the number of divisions (R7=3) and multiplications (R9=5) onto the stack.


2. Call the desired note to preload the calculated values. (R4=0x18A, R5 =0xDC0)

3. Play the note.


4. R5 (0xDC0) gets shifted R7 (3) times. 0b1101_1100_0000 -> 0b0001_1011_1000

5. R5 (0x1B8), R7(0)

6. R6 is filled with R5 (0x1B8), R9 (5) times.

7. R6 (0x898), R9(0)

8. Prepare the delay loop counter R8 with precalculated value R4 (0x18A)

9. Toggle the output pin.

10. Stay inside delay loop until R8 reaches 0.

11. Decrement R6 each time R8 reaches 0.

12. Repeat playing the note until R6 reaches 0.

13. Return to the original call.


Bar 5: Can we rest?

For the self-proclaimed detail-oriented quick-learners, you may have noticed that toggling the output pin is strange. The value of P3OUT is toggled with R10.


The purpose of this is to provide a way to rest for a precise amount of time. R10 can be toggled to 0x00 so that the output of P3OUT is not toggled at all. Resting can be played with any note, but with R10 cleared.





Bar 6: Capo

This whole project was completed while I was still a student. There must have been something in the coffee. With enough diligence, it is possible to take my program and make it play a different song. I would do other pieces but that means finding sheet music or transcribing something myself. For now, you can purchase the complete .asm file for $2.

6 views0 comments

Recent Posts

See All

Trends in Robotics: Cobots

https://www.linkedin.com/pulse/robotics-trends-robot-cobot-mobot-robotmp Cobots (Collaborative Robots) are robots which are designed...

About: Invigo Robotics

Invigo Robotics is committed to developing robotic technologies as envisioned by Isaiah Regacho. This vision is to remove the black box...

Comments


bottom of page