3

I'm trying to implement emulator main loop. Emulator runs at full speed. About 60 Mhz. But I want to make it run at 4.77Mhz. How it's done ?

for (;;)
{
        emu_step(ctx) ;
        uint64_t current = get_gtod_clock_time () ;
        uint64_t elapsed = current - last_time ;
        if (elapsed >= 1000000)
        {
            printf("Cycles: %d\n", get_cycles(ctx)) ;
            set_cycles_zero(ctx, 0) ;
            last_time = get_gtod_clock_time () ;
        }
}

emu_step() is function which executes one instruction. get_cycles(ctx) is sum of executed machine cycles. set_cycles_zero(ctx, 0) sets cycles counter to zero.

1 Answer 1

0

I think this is because emu_step is being executed at every iteration of the for loop, if you moved it inside the if, then it would only be executed when elapsed >= 1000000. In that particular case you could execute it once, independently from the loop, so that you don't have to wait one throttled cycle.

It could look something like

emu_step(ctx) ;

for (;;)
{
        uint64_t current = get_gtod_clock_time () ;
        uint64_t elapsed = current - last_time ;
        if (elapsed >= 1000000)
        {
            emu_step(ctx);
            printf("Cycles: %d\n", get_cycles(ctx)) ;
            set_cycles_zero(ctx, 0) ;
            last_time = get_gtod_clock_time () ;
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.