Answers.Body: <p>After all the time I finally found a good way of emulating <em>EV3</em> brick. I've tried different ways and this one is the best way I've found:</p>
  <ul>
  <li>Bring <a href="https://github.com/mindboards/ev3sources" rel="nofollow noreferrer">the original</a> or <a href="https://github.com/ev3dev/lms2012-compat" rel="nofollow noreferrer">compat</a> version of EV3 firmware;</li>
  <li>Redirect some stuff to be exported (like motors data, sensors data, LCD buffers, buttons, etc.);</li>
  </ul>
  <blockquote>
  <p>The firmware is written mostly on C lang, so for the step we can just <a href="https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport">export</a> some methods to implement them outside the library</p>
  </blockquote>
  <ul>
  <li>Compile the firmware as a library;</li>
  </ul>
  <blockquote>
  <p>The compilation process of the firmware is done using <a href="https://cmake.org/" rel="nofollow noreferrer">CMake</a> (useful tool for cross-compiling you code with configurable build steps). Change the output of the build to be a library and edit some other stuff (like dependencies that are presented only on *nix machines)</p>
  </blockquote>
  <ul>
  <li>Import the library in an emulator application and configure redirected data.</li>
  </ul>
  <blockquote>
  <p>In my case I used C# with <a href="https://avaloniaui.net/" rel="nofollow noreferrer">Avalonia</a> framework. Marshalled compiled library and implemented the redirected functions.</p>
  </blockquote>
  <p>So I did the steps and got quite a good result (for now motor and sensor ports redirects are still under development):<br />
  <a href="https://i.sstatic.net/Kna4P8AG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Kna4P8AG.png" alt="the emulator picture" /></a></p>
  <p>Small clarifications about the project:</p>
  <ul>
  <li>For now, the EV3 firmware library is only tested on Windows machines, but when I was rewriting it, I assumed it would be cross-platform;</li>
  <li>It can be compiled and tested only targeting <em>x86</em> because of some problems with pointer size. I've tried to deal with it but understood that there is too much work;</li>
  <li>Output/input fuction implementations are still under development;</li>
  <li>There are still some small bugs like I can't rename the brick;</li>
  <li>Communication with the brick is also not done yet. I couldn't find any resources about emulating USB or Bluetooth. So probably only WiFi would be supported.</li>
  </ul>
  <p>For those who are interested <a href="https://github.com/CrackAndDie/Ev3Emulator" rel="nofollow noreferrer">here is the source code</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-09T19:58:50.033Z
Answers.Text: Thanks for the answer! While I can't test it to check its usefulness, it is clear that it works for you, which is great :) However, I guess others who would need this answer would not find it as intuitive as it is for you right now. Could you please take a few more minutes to add some more details to the listed steps?
Answers.UserDisplayName: zovits
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-09T18:22:29.027Z
Answers.LastActivityDate: 2024-09-10T06:42:06.453Z
Answers.LastEditDate: 2024-09-10T06:42:06.453Z
Answers.UserDisplayName: crackanddie
Body: <p>The main goal: emulate the work of a <em>EV3</em> brick so then I would use it as a part of the simulator.</p>
  <p>The problem: I don't know the best way of doing this.</p>
  <p>About a year ago I <a href="https://bricks.stackexchange.com/questions/18359/what-is-the-difference-between-local-memory-for-programs-and-local-memory-for-di">asked</a> about the memory allocations in a <em>Ev3</em> compiled program. Since then I haven't done anything more with this. And now I understand that this is quite a tedious way of emulating brick because I have to write my own &quot;interpreter&quot;.</p>
  <p>For now I have an idea just to run <a href="https://github.com/pybricks/pybricks-micropython/tree/master" rel="nofollow noreferrer">Pybricks</a> or <a href="https://github.com/ev3dev/lms2012-compat/tree/ev3dev-stretch" rel="nofollow noreferrer">lms-2012</a> in a virtual machine with redirected IO (for ports, display, connections, buttons) but I am too witless to do this. For this approach, I would have just rewritten some parts of the original sources so the IO would be redirected to a socket or std. But even this is too hard for me because I am bad at C or ASM.</p>
  <p>Then I found some repos where the emulator is already written: <a href="https://github.com/ArnoVanDerVegt/wheel" rel="nofollow noreferrer">wheel</a>, <a href="https://github.com/AlexGustafsson/ev3-emulator-toolkit?ysclid=lysfs719zm612412197" rel="nofollow noreferrer">emulator toolkit</a>, <a href="https://github.com/InfoSphereAC/RoboSim" rel="nofollow noreferrer">RoboSim</a>:</p>
  <ul>
  <li>As I understood the first one uses a virtual machine (VM) to emulate the <em>EV3</em> but I can't understand how it works and why is there Java used (also not sure that this works with an old LEGO MINDSTORMS Ev3);</li>
  <li>There is also a VM used (also not sure that this works with an old LEGO MINDSTORMS Ev3);</li>
  <li>Probably custom &quot;interpreter&quot; for the LEGO bytecode (also not sure that this works with an old LEGO MINDSTORMS Ev3).</li>
  </ul>
  <p>There is also the <a href="https://www.virtualroboticstoolkit.com/" rel="nofollow noreferrer">Virtual Robotics Toolkit</a> that is not an open-source project and also uses a virtual machine (VM) to emulate <em>Ev3</em>. I couldn't decompile the library that works with the VM because it was compiled natively.</p>
  <p>So for now the main question is: What is the best way to emulate the work of <em>EV3</em> brick without having to write/rewrite a bunch of code?</p>
  <p>Any help is appreciated, thanks!</p>
  
Tags: <ev3><pybricks><emulation>
Title: What is the best way of emulating a Ev3 brick?
UserDisplayName: crackanddie
ThreadText: # What is the best way of emulating a Ev3 brick?
  
  (Asked by: username_0 on 2024-07-19)
  
  The main goal: emulate the work of a *EV3* brick so then I would use it as a part of the simulator.
  
  The problem: I don't know the best way of doing this.
  
  About a year ago I [asked](https://bricks.stackexchange.com/questions/18359/what-is-the-difference-between-local-memory-for-programs-and-local-memory-for-di) about the memory allocations in a *Ev3* compiled program. Since then I haven't done anything more with this. And now I understand that this is quite a tedious way of emulating brick because I have to write my own "interpreter".
  
  For now I have an idea just to run [Pybricks](https://github.com/pybricks/pybricks-micropython/tree/master) or [lms-2012](https://github.com/ev3dev/lms2012-compat/tree/ev3dev-stretch) in a virtual machine with redirected IO (for ports, display, connections, buttons) but I am too witless to do this. For this approach, I would have just rewritten some parts of the original sources so the IO would be redirected to a socket or std. But even this is too hard for me because I am bad at C or ASM.
  
  Then I found some repos where the emulator is already written: [wheel](https://github.com/ArnoVanDerVegt/wheel), [emulator toolkit](https://github.com/AlexGustafsson/ev3-emulator-toolkit?ysclid=lysfs719zm612412197), [RoboSim](https://github.com/InfoSphereAC/RoboSim):
  
  * As I understood the first one uses a virtual machine (VM) to emulate the *EV3* but I can't understand how it works and why is there Java used (also not sure that this works with an old LEGO MINDSTORMS Ev3);
  * There is also a VM used (also not sure that this works with an old LEGO MINDSTORMS Ev3);
  * Probably custom "interpreter" for the LEGO bytecode (also not sure that this works with an old LEGO MINDSTORMS Ev3).
  
  There is also the [Virtual Robotics Toolkit](https://www.virtualroboticstoolkit.com/) that is not an open-source project and also uses a virtual machine (VM) to emulate *Ev3*. I couldn't decompile the library that works with the VM because it was compiled natively.
  
  So for now the main question is: What is the best way to emulate the work of *EV3* brick without having to write/rewrite a bunch of code?
  
  Any help is appreciated, thanks!
  
  **Tags:** ev3, pybricks, emulation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-09-09. Score: 1
  
  After all the time I finally found a good way of emulating *EV3* brick. I've tried different ways and this one is the best way I've found:
  
  * Bring [the original](https://github.com/mindboards/ev3sources) or [compat](https://github.com/ev3dev/lms2012-compat) version of EV3 firmware;
  * Redirect some stuff to be exported (like motors data, sensors data, LCD buffers, buttons, etc.);
  
  > The firmware is written mostly on C lang, so for the step we can just [export](https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport) some methods to implement them outside the library
  
  * Compile the firmware as a library;
  
  > The compilation process of the firmware is done using [CMake](https://cmake.org/) (useful tool for cross-compiling you code with configurable build steps). Change the output of the build to be a library and edit some other stuff (like dependencies that are presented only on \*nix machines)
  
  * Import the library in an emulator application and configure redirected data.
  
  > In my case I used C# with [Avalonia](https://avaloniaui.net/) framework. Marshalled compiled library and implemented the redirected functions.
  
  So I did the steps and got quite a good result (for now motor and sensor ports redirects are still under development):  
  [![the emulator picture](https://i.sstatic.net/Kna4P8AG.png)](https://i.sstatic.net/Kna4P8AG.png)
  
  Small clarifications about the project:
  
  * For now, the EV3 firmware library is only tested on Windows machines, but when I was rewriting it, I assumed it would be cross-platform;
  * It can be compiled and tested only targeting *x86* because of some problems with pointer size. I've tried to deal with it but understood that there is too much work;
  * Output/input fuction implementations are still under development;
  * There are still some small bugs like I can't rename the brick;
  * Communication with the brick is also not done yet. I couldn't find any resources about emulating USB or Bluetooth. So probably only WiFi would be supported.
  
  For those who are interested [here is the source code](https://github.com/CrackAndDie/Ev3Emulator)
  
  > **Comments:**
  > **username_1**: Thanks for the answer! While I can't test it to check its usefulness, it is clear that it works for you, which is great :) However, I guess others who would need this answer would not find it as intuitive as it is for you right now. Could you please take a few more minutes to add some more details to the listed steps?  
  
  ---
  
  