Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPU Emulation and locking to a specific clock speed

If you had read my other question, you'll know I've spent this weekend putting together a 6502 CPU emulator as a programming exercise.

The CPU emulator is mostly complete, and seems to be fairly accurate from my limited testing, however it is running incredibly fast, and I want to throttle it down to the actual clock speed of the machine.

My current test loop is this:

    // Just loop infinitely.     while (1 == 1)     {                         CPU.ClockCyclesBeforeNext--;          if (CPU.ClockCyclesBeforeNext <= 0)         {             // Find out how many clock cycles this instruction will take             CPU.ClockCyclesBeforeNext = CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].CpuCycles;              // Run the instruction             CPU.ExecuteInstruction(CPU.Memory[CPU.PC]);              // Debugging Info             CPU.DumpDebug();             Console.WriteLine(CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].ArgumentLength);              // Move to next instruction             CPU.PC += 1 + CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].ArgumentLength;                                                 }     } 

As you can tell, each opcode takes a specific amount of time to complete, so I do not run the next instruction until I count down the CPU Cycle clock. This provides proper timing between opcodes, its just that the entire thing runs way to fast.

The targeted CPU speed is 1.79mhz, however I'd like whatever solution to the clock issue to keep the speed at 1.79mhz even as I add complexity, so I don't have to adjust it up.

Any ideas?

like image 658
FlySwat Avatar asked Sep 21 '08 22:09

FlySwat


People also ask

Can you control CPU clock speed?

Computer CPUs that are running too fast or too hot can have their speed turned down through a technique called "underclocking." Depending on the motherboard and CPU, the system BIOS can feature special controls that adjust the motherboard's Front Side Bus speed and the processor's multiplier.

Why does a CPU run at a particular clock speed?

A computer's processor clock speed determines how quickly the central processing unit (CPU) can retrieve and interpret instructions. This helps your computer complete more tasks by getting them done faster. Clock speeds are measured in gigahertz (GHz), with a higher number equating to higher clock speed.

What happens if the CPU is made to run faster than its clock speed?

If the clock tells the CPU to execute instructions too quickly, the processing will not be completed before the next instruction is carried out. If the CPU cannot keep up with the pace of the clock, the data is corrupted . CPUs can also overheat if they are forced to work faster than they were designed to work.

Does CPU clock speed actually matter?

Why Does Clock Speed Matter? CPU clock speed is a good indicator of your processor's performance. Though applications like video editing and streaming are known to rely on multi-core performance, many new video games still benchmark best on CPUs with the highest clock speed.


2 Answers

I wrote a Z80 emulator many years ago, and to do cycle accurate execution, I divided the clock rate into a number of small blocks and had the core execute that many clock cycles. In my case, I tied it to the frame rate of the game system I was emulating. Each opcode knew how many cycles it took to execute and the core would keep running opcodes until the specified number of cycles had been executed. I had an outer run loop that would run the cpu core, and run other parts of the emulated system and then sleep until the start time of the next iteration.

EDIT: Adding example of run loop.

int execute_run_loop( int cycles ) {     int n = 0;     while( n < cycles )     {         /* Returns number of cycles executed */         n += execute_next_opcode();     }      return n; } 

Hope this helps.

like image 110
Jason Fritcher Avatar answered Sep 22 '22 00:09

Jason Fritcher


Take a look at the original quicktime documentation for inspiration.

It was written a long time ago, when displaying video meant just swapping still frames at high enough speed, but the Apple guys decided they needed a full time-management framework. The design at first looks overengineered, but it let them deal with widely different speed requirements and keep them tightly synchronized.

you're fortunate that 6502 has deterministic time behaviour, the exact time each instruction takes is well documented; but it's not constant. some instructions take 2 cycles, other 3. Just like frames in QuickTime, a video doesn't have a 'frames per second' parameter, each frame tells how long it wants to be in screen.

Since modern CPU's are so non-deterministic, and multitasking OS's can even freeze for a few miliseconds (virtual memory!), you should keep a tab if you're behind schedule, or if you can take a few microseconds nap.

like image 36
Javier Avatar answered Sep 23 '22 00:09

Javier