Click a key, or just type a,s,d,f,g,h,j,k and w,e,r,t,y all play. (This is only a broswer preview); the real program runs as RISC-V inside RARS.
A playable piano simulated entirely in RISC-V assembly — keyboard input mapped to MIDI notes, an automated "Twinkle Twinkle Little Star," and a hidden surprise for anyone who types the right four letters.
Built for EC351 with Elias Abuamer and Mohammed Almoldy, this project simulates a playable piano using nothing but the RISC-V instruction set, run inside the RARS simulator. Every key press is read raw, matched against a note table, and turned into sound through RARS's built-in MIDI syscall — no audio library, no abstraction layer underneath.
The interesting part isn't the piano itself — it's that something as fluid as music has to be expressed entirely through branches, lookups, and register juggling. Thirteen notes from C5 to C6, a 42-note melody played from two parallel arrays, and a four-key Easter egg tracked by a tiny hand-rolled state machine.
RARS exposes a single syscall for audio — load four registers and call it. No instrument files, no buffers, just a number for pitch, a number for how long, and a number for how loud.
| Register | Value | Meaning |
|---|---|---|
| a7 | 31 | Sound system call |
| a0 | 72–84 | MIDI note number (C5 to C6) |
| a1 | ms | Duration the note plays for |
| a2 | 0 | Instrument — piano |
| a3 | 0–127 | Volume / velocity |
la t2, notes slli t3, t1, 2 # index * 4 bytes add t2, t2, t3 lw a0, 0(t2) # MIDI note number li a1, 1000 # duration, ms li a2, 0 # instrument: piano li a3, 127 # volume li a7, 31 # sound syscall ecall
delay:
li t4, 150000
delay_loop:
addi t4, t4, -1
bnez t4, delay_loop
ret
Type r → i → c → k in that order and the program quietly switches melodies. Register s7 holds the current position in the sequence — any wrong key resets it back to zero.
check_k: li t1, 3 beq s7, t1, play_rickroll j cont
Verified by ear, mostly. Each key was checked against its expected pitch, notes were played back to back to confirm the loop doesn't drop or stall, and the melody and Easter-egg paths were both run end to end before falling back to the main loop.
Pitch accuracy — each of the 13 keys plays its correct MIDI note.
Sequencing — multiple notes and melodies play cleanly back to back.
Clean exit — 'q' terminates the program via syscall 10 every time.