From the web.
Introduction
To play a MIDI note in the RARS (RISC-V Assembler and Runtime System)
simulator, use environment call (ecall) 33 (synchronous MIDI out)
or 31 (asynchronous MIDI out).
MIDI ecall Parameters
- a0: Pitch (note number from 0 to 127, e.g., 60 for Middle C)
- a1: Duration in milliseconds (e.g., 500 for half a second)
- a2: Instrument (patch number from 0 to 127, e.g.,
0 for Acoustic Grand Piano)
- a3: Volume (velocity from 0 to 127, e.g., 100)
- a7: System call number (31 for async, 33 for sync)
Common MIDI Instruments (0 – 127)
RARS uses standard General MIDI instrument mappings. Some standout presets include:
Piano & Keyboards (0–7)
- 0: Acoustic Grand Piano
- 1: Bright Acoustic Piano
- 4: Electric Piano 1
- 6: Harpsichord
Chromatic Percussion (8–15)
- 11: Vibraphone
- 12: Marimba
- 13: Xylophone
Guitar (24–31)
- 24: Acoustic Guitar (nylon)
- 25: Acoustic Guitar (steel)
- 27: Electric Guitar (clean)
Bass (32–39)
- 32: Acoustic Bass
- 33: Electric Bass (finger)
Strings & Orchestral (40–55)
- 40: Violin
- 42: Cello
- 48: String Ensemble 1
- 52: Choir Aahs
Brass & Winds (56–79)
- 56: Trumpet
- 57: Trombone
- 65: Alto Sax
- 73: Flute
Synth Leads & Pads (80–103)
- 80: Square Lead
- 81: Sawtooth Lead
- 88: New Age Pad
Example: Playing a Single Note
This short program plays Middle C for 500 milliseconds
using the piano instrument before exiting cleanly.
.text
.global main
main:
li a0, 60 # Pitch: 60 = Middle C
li a1, 500 # Duration: 500 ms
li a2, 0 # Instrument: 0 = Grand Piano
li a3, 100 # Volume: 100 velocity
li a7, 33 # Syscall 33 = MidiOutSync (waits for tone to finish)
ecall
# Exit program
li a7, 10
ecall
...
Example: Code Snippet
This short snippet plays middle C (60) for 500 milliseconds using
an Acoustic Grand Piano (0) at maximum volume (127):
.data
.text
main:
li a0, 60 # Pitch: Middle C (60)
li a1, 500 # Duration: 500 ms
li a2, 0 # Instrument: Acoustic Grand Piano (0)
li a3, 127 # Volume: Max (127)
li a7, 31 # Syscall 31: MIDI out async
ecall
# Optional: Sleep (ecall 32) to let the note ring/finish
li a0, 500
li a7, 32
ecall
# Exit program
li a7, 10
ecall
Links
Music Player in RISC-V