Compare with most of the other emulators, μSim does not provide all MCU features and peripherals. This simulator design as a minimalistic system, and based on the requirements, it can extend with additional peripherals and features.
Apart from the instruction decoder, this system simulates 1024 words of flash memory, 68 bytes of SRAM, two 8 bit GPIO ports, and register map similar to PIC16F84 MCU. Some of the common MCU peripherals and features such as interrupts, timers, and UART systems are not integrated into this simulator.
This article provides step-by-step instructions on how to setup μSim on PC and launch it on the Arduino Uno platform with a simple PIC program.Clone and compile latest μSim source
The latest μSim source is available at GitHub. To get the source clone the above repository with the following command:
git clone https://github.com/dilshan/usim.git
After obtaining the copy, create bin and obj directory structure inside the usim directory as below:
│ ├── hexconv
│ └── usim
The above structure can create using the terminal with the following set of commands:
After obtaining the code, issue the following commands to compile the μSim.
If the compilation is successful, the μSim PC version and hexconv utility may need to create at /bin directory.Flash Arduino with μSim
In Arduino platforms, μSim uses an SD card to load the PIC program(s). To implement this setup, use a commonly available micro SD card module, which comes with level-shifter.
After performing the above steps launch the Arduino IDE and open "arduinousim" file. In the latest codebase, this source file is located at the "/src/arduinousim" directory.
From the Arduino IDE, select "Arduino Uno" as the board type and compile the source code. Following the compiling, upload it to the Arduino board.Preparing a test program
To test the above setup, we need a test program which compiled into PIC MCUs. These test programs can build using any PIC mid-range series compatible compiler, which includes Microchip's XC8, mikroC Pro for PIC, SDCC, etc. In this article, we use Microchip's XC8 and MPLAB IDE to create the test program. This compiler and IDE are freely available to download at a microchip.com software download center.
After installing the MPLAB and XC8 follow the given steps to create the test program (or click here to download the MPLAB test project):
- Open MPLAB IDE and select File > New Project...
- Choose "Microchip Embedded" as a category. Select the "Standalone Project" from the projects list and press the Next button.
- Select PIC16F84 or PIC16F84A as a device and press the Next button.
- Select XC8 as the compile and continue the wizard.
- Specify "ledtest" as the project name and select the appropriate directory to save the project.
- Press Finish, to create the project.
- Under the "Source Files" create main.c file and enter the source code listed in here.
- Click Production > Build Main Project to build the test code.
After the above steps, locate the hex file generated by the compiler and convert it to bin file using hexconv utility.
The hexconv utility used in the above command is a part of the simulator and located at the bin directory of the usim project.
After obtaining the bin file, copy it to the root of the SD card and rename it as usimboot.bin. usimboot.bin is the file name that is used by the Arduino version of μSim to load the flash memory content.Setting up the test circuit
To test the above PIC program wire the Arduino with the circuit layout given below.
As the final step, insert the SD card into the card module and press the RESET button on the Arduino board to launch the PIC program into the μSim. If everything is properly set, you need to get an LED pattern which is defined in the PIC program.Conclusion
As mention at the beginning of this article, the μSim simulator is not a comprehensive PIC MCU implementation. This simulator is designed as a core component, and it must extend based on the actual project requirements. To achieve this μSim is written with minimum system dependencies and with less complexity. To avoid resource overflows/overruns the source code is organized with a few functions and data types. This article provides only the initial guidance to set up the μSim and its test environment.
The main objective of this project is to provide a framework to build virtual MCUs for small-scale embedded systems. More details of this project are available at the documentation at GitHub Wiki.