sim: cpu_sim: Refactor and bugfixes.