SIDin #3 — Ripping BASIC based tune: Sheba and Testcard (Stefano Tognon, 2003)

author Stefano Tognon (ice00), SIDin magazine
date 2003-02-07
article SIDin issue 3 (version 1.00, 7 February 2003), pp. 17–43: 'Ripping BASIC based tune' — sections The Arrival of the Queen of Sheba (with the full rip source, pp. 17–28), Testcard (the BASIC listing, pp. 28–33), Machine code rip (pp. 34–43), Conclusion. Only the prose and the player routine are summarised; the note/duration data tables and the BASIC listing are not
created 2026-08-30 · updated 2026-08-30

How Tognon turned two BASIC programs from Commodore's Introduction to BASIC course — Andrew Colin's The Arrival of the Queen of Sheba (part 2, Appendix A) and the Testcard demo (part 1) — into PSID files. A BASIC-driven tune has no interrupt-driven play routine, which is exactly what a .sid needs; the article shows what has to be rewritten and prints the smallest three-voice player in the wiki.

Key claims#

  • Why: "there are many old games that where use the BASIC even for generating the music"; the BASIC programs are Andrew Colin's and Commodore's — the Testcard code is reproduced because the books do not print it, the Sheba BASIC is omitted because they do.
  • Sheba ("to show what the machine code can do with all the 3 Sid voices"): BASIC only sets things up — it POKEs the machine-code player from DATA (rows 20–280), copies the voice parameters and generates a frequency table (300–430), sets the SID registers per voice (500–565), then decodes each voice's note-and-duration data into memory, calling the routine meanwhile "so music can start while the BASIC program is still working".
  • The rip: run it in an emulator, disassemble the player in the monitor, "pick up all the memory blocks wrote by the BASIC program and store in a file", paste player and data into a source, move BASIC's SID initialisation into the PSID init routine and set "the right IRQ timing as done by the player". Doing it in the monitor alone would also work, but code low and data high in memory would give "a big file (without a relocation of the player code)".
  • The player (the complete source is printed): per voice a 7-byte record instr — control register, pointer to a list of note numbers, pointer to a list of durations, a 16-bit counter — and a contr block: an on/off byte, a speed byte (8) and two zero bytes that head the frequency table. Each interrupt, for X = 0, 7, 14: skip a stopped voice (control 0); count the 16-bit counter up and return until it overflows; then look up the note in the table BASIC generated (.byte 76,4 … 16-bit values), write $D400/$D401, read the duration and subtract the speed byte from the counter that many times (the next note comes after speed × duration interrupts), advance both pointers, write $00 and then the control byte to $D404,X — gate off, gate on. A note value of 0 stops the voice. Init clears the 25 SID registers, sets attack/decay 25 on all three voices, $D418 = 79, pulse-width low bytes 200/60/60, cutoff high byte 50, $D417 = 0. Voice 1 plays with control 65 ($41, pulse), voices 2 and 3 with 17 ($11, triangle). "No special effect are used for each instrument: however the sound is pretty good." (sid-player-routine, adsr-envelope)
  • PSID header by hand: PSID, version $0200, data offset $7C, load address $0000 (CBM format — the .byte $01,$08 load address stays in the data), init and play addresses, one song, $FFFF speed words, the 32-byte strings "Arrival of the queen of Sheba" / "Andrew Colin" / "1982-83 Commodore Inc"; the header sits under .ifdef sid, so the same source also builds a .prg with a BASIC stub SYS 2061 that hooks $0314/$0315 and chains to the Kernal IRQ at $EA31 (sid-format).
  • Testcard — "probably one of the first demo ever done. It has sprites animation and music, all created by a BASIC program": two voices, a simple sound, "nothing comparable to Sheba", because here the machine code "manage only pitch generation, while duration is done using TI variable by the BASIC program". There is no IRQ routine, "the main point needed for a rip … all the players that have not a full C64 environment emulation must use a IRQ calling for generating the sound. This is exactly what a standard PSID file use in the play section." The way out came in an e-mail from "Warren P.": convert the TI values into data "read by a machine code routine so it knows to wait X number of cycles before progressing on to the next note" — "definitely not very easy". Tognon's solution: an IRQ routine that "simulate[s]" the TI timing — the Sheba routine, "a minor version" of which suits Testcard, with its speed parameter "set as a TI timing"; the Testcard PSID declares 4 songs, "Testcard" / "Andrew Colin" / "1982 Commodore Inc".
  • Conclusion: "there are many old BASIC games that are attending to be ripped: check the Sid Hunt at http://lala.c64.org/sid-hunt". Tognon had used the Sheba routine years earlier "for experimenting Sid music (creating IMHO an horrible tune, but with good remembrance for me)" and credits the machine code hidden in the book with sending him into computer science, "a choice that is more that 20 years I'm follow!"

Practical takeaways#

  • A BASIC tune is ripped by rewriting: the SID POKEs become the init routine, the timing loop or TI polling becomes an IRQ player. HVSC's later way of keeping such programs is the RSID BASIC flag (sid-format); the magazine's next issue continues with "Ripping BASIC program into RSID" (#4, pp. 12–22, per its contents page; not summarised here).
  • Colin's routine is a good first player to read: three 7-byte voice records, a frequency table, a counter per voice, gate off then on per note, nothing else — compare the hubbard-player's 8-byte instruments and note bytes.

Notable quotes#

"All the players that have not a full C64 environment emulation must use a IRQ calling for generating the sound."

Relevance to the wiki#

The PSID/RSID distinction from a ripper's side for sid-format and hvsc; the 1982 minimal player for sid-player-routine; a pointer to the Sid Hunt list for hvsc.

To verify#

  • (unverified) "Warren P." is not expanded in the article; the hint's author is presumably an HVSC crew member.
  • (unverified) $D418 = 79 is $4F: volume 15 with the band-pass bit set; the article does not comment on the filter setting.

Pages touched#

sid-format · sid-player-routine · hvsc · sidin

source file wiki/summaries/s-sidin03-ripping-basic-tune.md · 2 unverified · graph