See an introductory vidéo here : http://dai.ly/nesUQE
Please consult the Wiki for more information.
This is raw coding... no time to really optimize the code. Sorry