I'm not sure I quite follow this. You say CycleDelay is set to 256 but I don't see it ever get used. Also there is LastWait but it is never initialized.
Anyway, not sure if it helps but I used your code in Delphi and got this "W 1128292 lw 0 t 1128292" as the outcome.