Sorry, I was writing based on memory. Looking at your code again, a better place for the VSYNC would be after the line that contains "BTN=="