Close

Song Storage

A project log for Attiny10 Chiptunes

Chiptunes and simple musical games on a 36 cent chip. Some assembly required.

legionlabsLegionlabs 03/20/2023 at 03:180 Comments

The problem of playing the notes solved, we need to store what notes to play somewhere. Normally I’d use EEPROM, but the attiny10 doesn’t have any. Thankfully, the chip can fully address (read and write!) the entirety of it’s program flash, which is a generous 1kb. That 1kb is organized into 64 pages each containing 8 words, which themselves contain 2 bytes.

Our program is quite small, occupying about 194 bytes (depending mildly on the revision) – yes, I could trim it down a little further, but it’s small enough for now: if we define a format where the output compare unit values (16 bits) are stored in a first word, and the note duration in a second word, we’ve got enough space for 207 notes, sufficient for several songs! I could also use only a single byte for note duration, but I left myself some space in the format in case I need to add some new features in later, and don't want to rewrite the songs.

Sadly the (excellent) datasheet is a bit light on the details of actually reading the flash. So I wrote a program to figure it out for me.

First, I used the Z-pointer to store the 16-bit flash address. This is really just a pair of 8-bit registers that together work more or less like a 16-bit one. When I point those to flash memory, I can just load ‘whatever’ is stored in that address and it should work fine. However, I could not work out the addressing scheme from the datasheet.

So instead I loaded a value not present anywhere else in memory at a fixed flash address (0x0150) using an org directive. Then I iterated through all possible Z-pointers, comparing the value read from memory until it matched the value I was looking for. I set a breakpoint to halt the CPU when that happened, and read the value of the Z-index. With those 2 values (the address 0x0150, and the Z-index that addresses that), and the memory structure, it was pretty easy to figure out the addressing scheme.

Discussions