10

I'm starting to explore RP2040/Raspberry Pi Pico, however, I was wondering about something: is there a "proper" emulator/simulator for RP2040?

From earlier Atmel experience, Atmel Studio allows access to a simulator: you write your C code for a given chip, and can then run the code in the simulator (very, veeery slowly), and do (say) C code tracepoint printout with cycle-leve accuracy; in addition, you can "stimulate" an GPIO input pin with a representation of, say, a UART serial signal, and then see what sort of effect this has on registers. Specifically, I remember this about it:

https://www.microchip.com/content/dam/mchp/documents/parked-documents/AVR-Simulator-UserGuide-DS50003042A.pdf

The simulator is based on software models of devices derived directly from the hardware designs, and are thus cycle-accurate per the real devices

Basically, that is what I'd want, but for RP2040.

So far, I've found this:

... which I don't think provide the kind of functionality like Atmel Simulator.

I found this interesting comment, however:

https://hackaday.com/2021/06/03/gdbdiff-diff-ing-a-real-rp2040-mcu-against-an-emulated-mcu/

No need to write an emulator, it’s 2021 and all the hardware is open hardware!

Just take the VHDL and launch it in Verilator!

What a wonderful world!

My sarcasm detector is broken, so I'm not sure if this is a joke, however https://www.veripool.org/verilator/ is a thing.

So I tried to find HDL sources for RP2040, and there is this blog post:

https://www.raspberrypi.com/news/the-journey-to-raspberry-silicon/

So, apparently the HDL description of the chip was made in Verilog; and there is this screenshot in this blog post, captioned "“ell” from the phrase “Hello World” from core0 of RP2040 in a simulator"

RP2040 simulator

Yes, that is exactly the kind of analysis I'd want!

However, I wasn't really able to find the Verilog source of RP2040 - and I guess, it is probably not open.

But barring that - are there any other options for cycle-accurate simulation of RP2040, that could work with an arbitrary .uf2/.elf/.hex executable, and where I could also "stimulate" pins with (say) a digital capture (think .vcd file) of (say) an UART signal?

sdbbs
  • 237
  • 3
  • 8
  • 1
    I too would love to find an emulator/simulator. I'd like to start an open source C++ async library for it but with Sparkfun (they are 1 mile from my house) being out of stock for a long time of actual boards, I have not yet gotten the actual hardware. – Michel May 21 '22 at 15:13

2 Answers2

9

Author of rp2040js and founder of wokwi.com.

As far as I know, the Verilog source code of the RP2040 is not open, and is not likely to even be open. The chip contains a lot of 3rd party IP, such as the ARM Cortex-M0+ core, some of the peripherals (the datasheet says that both UART and SPI are based on ARM Primecell), so even if Raspberry Pi wanted, they probably don't have the rights to release the Verilog code of these parts.

In addition, Verilog level model is usually not very useful for simulating the entire chip. The simulation would be very slow, orders of magnitudes slower compared to the actual chip. Synthesizing the verilog model and running it on a capable FPGA chip will probably give you a better performance (if I recall correctly, Raspberry Pi got 40MHz or so when they ran it on FPGA), but that would also be expensive and require some non trivial learning curve to set it up.

I believe that rp2040js is the best bet for you at moment. The simulation is not exactly cycle-accurate (since we don't simulate things like bus contentions, arbitration, etc.), but it's pretty close, and probably a bit more optimistic compared with reality.

Most of the peripherals are there (even though not all the features are present in all of the peripherals), and most notably - the unique PIO peripheral, which is implemented in cycle accurate and is nearly feature complete.

Your program runs in the simulator instruction-by-instruction, so you can do things like single stepping, dumping the registers and poking various memory addresses, and stimulating GPIO pins / reading the results.

You can learn what rp2040js is capable of by watching the free Raspberry Pi Pico and RP2040 - The Deep Dive course. The course uses Wokwi for teaching about the internals of ARM, writing ARM assembly, interacting directly with the peripherals through the registers, debugging your program with GDB, and creating your own PIO state machines.

Here are some projects where you can see of these capabilities:

You asked about using rp2040js outside of a webpage. There are two examples in the repo, using Node.js to run the simulation from the command line: simulating a hex file, and simulating stock MicroPython firmware).

Finally, if you are really interested in all the gory details, you can watch the live stream where I coded the simulator from scratch, for more than 70 hours of questionable entertainment.

urish
  • 191
  • 1
  • 4
  • 2
    I have yet to try it but it looks like you did an amazing job on this simulator! If only I had 70 hours of spare time :D – ebruchez Feb 21 '23 at 18:13
0

Whilst the following project looks to be abandoned 2 years ago, it does look pretty comprehensive...

docs: https://rp2040pio-docs.readthedocs.io/en/latest/index.html

code: https://github.com/soundpaint/rp2040pio

I haven't used it persoanlly. It looks like a fairly steep path getting started. It is Java based and requires building from source :(

Here are some pretty pictures of it in action...

enter image description here

enter image description here

Greg Woods
  • 181
  • 4