satoshinm / pill_6502

emulated 8-bit 6502 CPU and 6850 ACIA for STM32F103 blue pill ⛺
GNU General Public License v3.0
36 stars 9 forks source link

Improve performance of 6502 emulation #1

Open satoshinm opened 6 years ago

satoshinm commented 6 years ago

Executing 6502 instructions on systick every 1 ms gives about a 1 / (1 ms) = 1 kHz clock rate; it ought to be possible to improve this, probably by moving off systick and step6502(), instead using exec6502(ticks), but note the USB device also requires periodic attention (currently polled with while (1) usbd_poll(usbd_dev)).

satoshinm commented 6 years ago

First attempt, this crashes with SIGSEGV:

@@ -202,7 +200,13 @@ int main(void)

        reset6502();

-       while (1)
+       while (1) {
                usbd_poll(usbd_dev);
+
+               if (!paused) {
+                       step6502();
+                       gpio_toggle(GPIOC, GPIO13);
+               }
+       }

when writing to the ACIA data port, which writes to the USB CDC-ACM - is it not fully initialized yet? The ACIA has a flag we could set/clear when ready to receive written data, but how to know when it is ready?

satoshinm commented 6 years ago

Enhanced ^T command, currently computes it runs at about 3.5 kHz (starting point before optimization) - as expected from the 1 ms systick timer + cycle count of average instruction:

131658 ticks
37665 instructions
3483 Hz

132244 ticks
37832 instructions
3508 Hz

132325 ticks
37855 instructions
3521 Hz

132405 ticks
37877 instructions
3636 Hz

132479 ticks
37899 instructions
3363 Hz
spacerace commented 6 years ago

If i got that right, you're executing triggered by SysTick. I would suggest let the emulator just run free in main()'s for(;;) { }. The USB and other stuff can be done in ISRs.

Maybe an OS (RTOS) could be useful for this job, but also could eat up resources.

Here's how to overclock BluePill and others to 128MHz: https://github.com/spacerace/HY-MiniSTM32V/tree/master/makefile_overclock https://github.com/spacerace/HY-MiniSTM32V/tree/master/keil_overclock