// File: lhartmann[P]orangepi-i2s-steppers/cnt.cpp
// Function: step()
uint32_t step() {
	uint32_t out = 0;
	
	// If motion has completed, prepare new move
	bool done = throttle.done();
	for (int s=0; s<NUM_STEPPERS; ++s) done = done && stepper[s].done();
	
	if (done) {
		// Reverse direction
		if (++forward == 16) forward = 0;
		
		// Move Duration (samples), considering max-speed.
		// Beware of this expression, RATE*MOVE may overflow.
		uint32_t duration = RATE * 1. * MOVE/MAXSPD;
		
		// Prepare the speed controllers
		for (int s=0; s<NUM_STEPPERS; ++s)
			stepper[s].prepare(MOVE/(s+1), duration);
		
		// Prepare the acceleration controller
		throttle.prepare(duration, ACCEL, ACCEL/100, ACCEL/100);
	}
	
	// Render movement
	out += forward*DIR_MASK;
	if (throttle.step()) {
		for (int s=0; s<NUM_STEPPERS; ++s) {
			if (stepper[s].step()) {
				out |= STEP_MASK<<s;
			}
		}
	}
	
	// Encode for I2S output:
	//   Apply selective transport delay (31 LSBs delayed by 1 clock)
	out = td(out);
	//   Fix word clock phase by shifting bits around.
	//   Hardware will delay 1 MSB by 1 clock, resyncing the word.
	out = (out<<1) | (out>>31); 
	
	return out;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x0, -0x4(%rbp)
leaq 0x2e3e(%rip), %rdi      # 0x4044
callq 0x1460
andb $0x1, %al
movb %al, -0x5(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $0x4, -0xc(%rbp)
jge 0x1255
xorl %eax, %eax
testb $0x1, -0x5(%rbp)
movb %al, -0x19(%rbp)
je 0x1242
movslq -0xc(%rbp), %rax
leaq 0x2e3d(%rip), %rdi      # 0x4070
imulq $0x14, %rax, %rax
addq %rax, %rdi
callq 0x1480
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movb %al, -0x5(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1217
testb $0x1, -0x5(%rbp)
je 0x12e3
movl 0x2ddb(%rip), %eax      # 0x4040
addl $0x1, %eax
movl %eax, 0x2dd2(%rip)      # 0x4040
cmpl $0x10, %eax
jne 0x127d
movl $0x0, 0x2dc3(%rip)      # 0x4040
movl $0xea600, -0x10(%rbp)   # imm = 0xEA600
movl $0x0, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x12c6
movslq -0x14(%rbp), %rax
leaq 0x2dd4(%rip), %rdi      # 0x4070
imulq $0x14, %rax, %rax
addq %rax, %rdi
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl $0x27100, %eax          # imm = 0x27100
cltd
idivl %ecx
movl %eax, %esi
movl -0x10(%rbp), %edx
callq 0x14a0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x128b
movl -0x10(%rbp), %esi
leaq 0x2d74(%rip), %rdi      # 0x4044
movl $0x5dc00, %edx          # imm = 0x5DC00
movl $0xf00, %r8d            # imm = 0xF00
movl %r8d, %ecx
callq 0x14f0
movl 0x2d57(%rip), %eax      # 0x4040
shll $0x10, %eax
addl -0x4(%rbp), %eax
movl %eax, -0x4(%rbp)
leaq 0x2d4b(%rip), %rdi      # 0x4044
callq 0x15c0
testb $0x1, %al
jne 0x1304
jmp 0x134d
movl $0x0, -0x18(%rbp)
cmpl $0x4, -0x18(%rbp)
jge 0x134b
movslq -0x18(%rbp), %rax
leaq 0x2d54(%rip), %rdi      # 0x4070
imulq $0x14, %rax, %rax
addq %rax, %rdi
callq 0x1690
testb $0x1, %al
jne 0x132e
jmp 0x133e
movl -0x18(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
orl -0x4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1340
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x130b
jmp 0x134d
movl -0x4(%rbp), %esi
leaq 0x2d69(%rip), %rdi      # 0x40c0
callq 0x16f0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
shll %eax
movl -0x4(%rbp), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)