## Practicality of bytebeat: Can "Crowd" be optimized?

Moderator: Moderators

tepples
Posts: 21802
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Practicality of bytebeat: Can "Crowd" be optimized?

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?

Code: Select all

``````#include <stdint.h>
static inline void putchar(int c) {
*(uint8_t *)0x4011 = c >> 1;
}

int main(void) {
for (uint32_t t = 0; ; ++t) {
uint8_t out =
((t<<1)^((t<<1)+(t>>7)&t>>12))
| t>>(4-(1^7&(t>>19)))
| t>>7;
putchar(out);
}
}
``````
Or is this a moot point because of undefined behavior involving t>>(4-(1^7&(t>>19)))?

lidnariq
Posts: 8901
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

It doesn't really answer the question, but there are a few bytebeats ported to the 2600: e.g. http://www.acc.umu.se/~tjoppen/files/vcs/genmusic1/

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

tepples wrote: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.)

tepples
Posts: 21802
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

rainwarrior wrote:
tepples wrote: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:

Code: Select all

``````clc
lda #1
sta t_0
lda #0
sta t_8
lda #0
sta t_16
lda #0
sta t_24
``````
Calculating one of the shifted versions takes 32 cycles:

Code: Select all

``````lda t_0
asl a
sta tshl1_0
lda t_8
rol a
sta tshl1_8
lda t_16
rol a
sta tshl2_16
lda t_24
rol a
sta tshl2_24
``````
rainwarrior wrote:
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"?
"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

Code: Select all

``````0x200000>>(4-(1^7&(0x200000>>19)))
0x200000>>(4-(1^7&(4)))
0x200000>>(4-(1^4))
0x200000>>(4-5))
``````
One might consider defining the piece to end at the first undefined behavior. But in C, undefined behavior causes time travel:
The C committee wrote: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).

JRoatch
Formerly 43110
Posts: 389
Joined: Wed Feb 05, 2014 7:01 am
Location: us-east
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

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.

I not sure if the answers to this stackoverflow question helps how t >> -1 is implemented today.

Edit:
Or since it is 43 minutes in, we could just force the sequence to cycle at that point by changing the term to t>>(4-(1^3&(t>>19))) masking to 2 bits instead of 3.

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

tepples wrote:undefined behavior causes time travel
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

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

tepples wrote: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?

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?
Attachments
crowd.zip
source
crowd.nes
Last edited by rainwarrior on Mon Mar 13, 2017 1:11 pm, edited 4 times in total.

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

And just because I wanted to see it, here's my naive forty-two. Takes 237 cycles.

I guess JRoatch already made one of these, but hey you can always use another.
Attachments
fortytwo.s
fortytwo.nes

tepples
Posts: 21802
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

rainwarrior wrote:I'm not entirely sure it's correct, because I don't want to listen to it for ~150 hours to compare
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?
Edit: Realized it doesn't use the upper bits, so it actually loops after about 8.5 minutes.
And after thinking it over, I'm really only concerned about the first 8.5 minutes, given that crowd.ogg linked from Kragen's bytebeat page is only the first 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.
May I use this as the test for \$4011 in 240p Test Suite for NES?

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm
Contact:

### Re: Practicality of bytebeat: Can "Crowd" be optimized?

tepples wrote: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.

rainwarrior
Posts: 7699
Joined: Sun Jan 22, 2012 12:03 pm