After I saw the suggestion to add the forty-two melody as an easter egg somewhere in Action 53 volume 3, I got to thinking whether more substantial bytebeat compositions such as "Crowd" by Kragen might be doable. Is it possible to compute each sample of "Crowd" in exactly 224 cycles on the MOS 6502? Or are all the 32-bit shifts and bitwise ops too complex?

Is it possible to compute each sample of "Crowd" in exactly 224 cycles on the MOS 6502?

What's your estimate for a "naive" implementation? (Why does it have to be exactly 224? I'd imagine it would still be musically recognizable at a range of speeds.)

tepples wrote:

Or is this a moot point because of undefined behavior involving t>>(4-(1^7&(t>>19)))?

Since when does "undefined" mean "unimplementable"?

(Just in case my tone is unclear, I'm not trying to be dismissive, just not ready to dive in and solve it myself right now. I think this is an interesting idea, so I'll be glad to hear if you come up with anything here.)

Is it possible to compute each sample of "Crowd" in exactly 224 cycles on the MOS 6502?

What's your estimate for a "naive" implementation? (Why does it have to be exactly 224? I'd imagine it would still be musically recognizable at a range of speeds.)

I was mostly trying to rule out jitter-fests due to implementations that aren't constant time, such as an inc/bne implementation of t++ or a looping implementation of the term with the variable shift amount. A constant time implementation of t++ alone would take 34 cycles:

Or is this a moot point because of undefined behavior involving t>>(4-(1^7&(t>>19)))?

Since when does "undefined" mean "unimplementable"?

"Crowd" is defined by a C program, but C doesn't define what negative shift amounts or shift amounts greater than the size in bits of the left operand do. At t == 0x200000 samples (around 43 minutes 41 seconds), for instance, this particular sub-expression reduces to

However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

The nice thing about casting shifted 32 bit numbers into 8 bits is that you don't have to bit shift all 4 bytes. Crowd also doesn't use multiplication which was the most expensive part of the forty-two melody.

That's not relevant to this. If you want to play the specific song, do what the specific implementation does. It doesn't matter whether it's a well formed C question. If the song plays, the compiler had a way to implement it.

tepples wrote:

C doesn't define what negative shift amounts or shift amounts greater than the size in bits of the left operand do.

I believe Intel implemented SHL/SHR to take only an unsigned parameter, bitmasked to the available shift amounts (i.e. -1=31, -2=30, etc). That's what I'd expect would happen here, but it's easy to check.

Anyhow, if the computation is unfeasible as-is, there's always lookup tables. If you're just looking for a way to fill unused ROM space, you could just precompute the whole function for as long as the space allows. ;P

Is it possible to compute each sample of "Crowd" in exactly 224 cycles on the MOS 6502?

So... I got bored on the toilet, and I think the answer turns out to be yes.

This uses only 222 cycles, but you can add a NOP in there somewhere probably. The code isn't fancy, it's just not a whole lot of computation when you restrict it to just the bits you need.

I'm not entirely sure it's correct, because I don't want to listen to it for ~150 hours to compare, and also the loss of 1 bit of precision means some "quiet" sounds are lost in the mix, but... seems pretty close so far?

(Somebody please check my work.)

Edit: Realized it doesn't use the upper bits, so it actually loops after about 8.5 minutes. After listening to the whole thing (and hearing/correcting a single character typo in the 6th >>19 sound phase), I'm pretty sure this is it.

Edit: Or... well I guess some of the "negative" shifts use the upper bits? Maybe 18 hours in or something three of the eight >>19 phases will start to sound a little different?

One could test it in a 6502 simulator by logging $4011 writes and comparing the output to that from a modified crowd.c that adds (...)>>1. Does NSFPlay support non-returning play?

Yeah, but it'd probably take longer to rig up and execute that test than it took to write in the first place. Kinda exceeds my interest level at this point.

tepples wrote:

May I use this as the test for $4011 in 240p Test Suite for NES?

I didn't put an explicit license on the code, but yes, please do whatever you like with the code. Copy it, modify it, etc. I'm don't really care about being attributed or not, though I guess the original work is CC-BY so you might be obliged to attribute at least Kragen.

I guess some of the "negative" shifts use the upper bits? Maybe 18 hours in or something three of the eight >>19 phases will start to sound a little different?

So, this long phase stuff is really only the top 3 bits, and they are being shifted to the low 1-3 bits of output and ORed with it, so their actual impact on the sound is quite minimal. (This implementation discards the low bit of output too, so it's only the top 2 bits.)

Using a debugger it's easy to set the top 2 bits of "t" and listen, and it only affects 2 of the 8 mid-range phases (about 1 minute each of the ~8 minute "loop"). They don't really sound that different. The negative shifts are more like a surrogate for a 0 term than an actual useful result.

Originally I was considering that the calculation could be simplified by shifting the output early (>> 1) instead of at the end (and also remember only 7 of the bits matter). In particular the terms (t << 1) >> 1 and (t >> 7) >> 1 can just be directly read as single bytes from the 4-byte accumulator. I think this would lose precision on the lowest bit of output from (t>>1) + (t>>7), but would mostly sound the same. (Similarly the 8 mid-phase shift operations can each be customized with an extra >> 1.) Also if you want to ignore the long phase entirely (since it has minimal effect anyway), you can just treat the high byte of "t" as a permanent 0 and skip the extra load/add/store in its increment.

Ultimately I thought it was best to keep it as accurate as I could, but if you need extra cycles, these are options.

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum