I did something similar in 6502 assembly a long while back and it fit into less than a kilobyte of code..