I play with Arduino Micro. I write my programs in directly in machine code, in binary editors (yes, I know that there are more easy ways to program an Arduino, I do it for fun and learning). Often my programs work, but if not, then I would like to run them on an emulator to understand what's exactly going on under the hood. Is there any Arduino emulator that accepts hex files with compiled program and can execute it? I already looked at tools mentioned at Can I program for Arduino without having a real board? , but as far as I see, they are simulators rather than emulators and I can't use compiled binary programs in them.
-
Are you sure you are not writing Opcode? As machine code is literally typing in numbers. For example, there are no "labels" let alone "goto labels". When writing in machine code, generally all branches need to be calculated to either a relative or absolute memory address.– st2000Commented Dec 15, 2018 at 14:53
-
Yes, I am sure that I am not writing opcodes, I am writing in machine code - this is why I mentioned that I use binary editor (I tried to be clear about it).– user983447Commented Dec 15, 2018 at 15:30
-
Ok, understood. Going on, I answered your question generically. But I see you were asking about the ATmega32U4. If I were Microchip, I wouldn't publish how my processor worked. That's intellectual property. So it would be surprising to find a working software emulator available for this specific processor. As I send in the answer, most developers simply work with the actual processor using its JTAG interface.– st2000Commented Dec 15, 2018 at 15:35
-
Hint: Search github, here is an example of a simulator that you could continue working on: github.com/buserror/simavr– Mikael PatelCommented Dec 15, 2018 at 16:17
2 Answers
Disclaimer: I have no affiliation with Wokwi (based on opensource AVR8js engine) except that I have learnt a lot from it.
I just tried it on the free Wokwi Arduino simulator(my favourite Arduino simulator for learning) and it worked in a blink!
Here is what I did to load the hex file into the Arduino simulator
- Go to the Arduino IDE and open the basic blink example
- Change the ON time delay to 100ms (to make sure, the hex file is really loaded into the Arduino simulator and to verify it's working.
- Once the modified example is compiled go to the
%temp%
folder - Search for
*.hex
files and find the one which is just the latest one (this is the only way I found to grab the hex file needed) - Go to Wokwi Arduino Simulator with any Arduino example
- Click on the editor tab in the left
- Press function key
F1
- Select the option
Load HEX file and START simulation
- Done - the simulation should run like expected.
I have created a short GIF and I hope you like it (lazy to show step by step)
-
1
-
Hello @starCat I have been following this Simulator for my Arduino learning but not involved in developing. I have got a lot of help as training and tutorials form Wokwi while I learn and develop other projects. Commented May 14, 2021 at 11:34
-
1I have seen this Simulator growing from simple blink to complex projects and is closely knowing all the updates happening. Let me know if I should do something Commented May 14, 2021 at 11:35
There are many different processors used in making many different types of Arduino boards. So there is no one way to write Arduino machine code.
There are emulator for simpler processors available for the asking. But, while they work, it is not believe they claim to be totally accurate.
At one point in processor development history, ICE machines were widely used. This is where a real processor is modified to allow access to internal controls and registers. (This actually sounds like the answer to your question.) However, as processors grew in speed and complexity, this approach grew out of favor.
Currently, most processors are built with extra internal circuitry and interface to break program execution for debugging purposes. JTAG is one such industry standard / example.
So, while the question has merit, the common practice is likely not to run code on an emulator, but to run it on the processor using the JTAG interface and an IDE running on a host-computer / work-station.
-
The in-circuit-emulators were very expensive and never 100% accurate (for example different timing for external signals), while the JTAG is already in the microcontroller for free.– JotCommented Dec 15, 2018 at 15:35
-
Good points @Jot. And I can attest to the timing difficulties.– st2000Commented Dec 15, 2018 at 15:37