public void tick(object sender, EventArgs e) { if (System.Threading.Interlocked.Read(ref cpuBusy) == 0) //Sync timer and CPU { System.Threading.Interlocked.Increment(ref cpuBusy); if (breakpoints.Contains(registers.select[REG.PC].value)) { Console.WriteLine("[*] Breakpoint at 0x{0:X} reached.\nGoing to Step-by-step mode\n", registers.select[REG.PC].value); debugMode = CPUDebugMode.Verbose; mainTimer.Stop(); } if (debugMode == CPUDebugMode.Verbose) { registers.printState(); } var instruction = new Instruction(memory.readWord(registers.select[REG.PC].value)); executeInstruction(instruction); foreach (Action <CPU> action in actions) { action(this); } System.Threading.Interlocked.Decrement(ref cpuBusy); } }
System.Collections.Generic.List <Action <CPU> > actions; //debug - actions to perform at the end of every tick public CPU() { memory = new Memory(); registers = new CPURegisters(); breakpoints = new System.Collections.Generic.List <ushort>(); actions = new System.Collections.Generic.List <Action <CPU> >(); debugMode = CPUDebugMode.None; mainTimer = new System.Timers.Timer(); mainTimer.Elapsed += tick; cpuBusy = 0; }