📜 ⬆️ ⬇️

Hey Simulator! Show your language


Writing this material was contributed by the article Why the Verilog language to a microcontroller programmer , which describes the possibility of testing the algorithm (C file) of a microcontroller project in a Verilog project. Those. The Verilog HDL simulator interacts with the Microcontroller's Sich code and tests the implemented algorithm for possible logical errors.

Similar methods for debugging code for microcontrollers have already been implemented by manufacturers. I can’t vouch for everyone, but I can show by the example of the MPLAB SIM simulator from Microchip Technology Inc.


')

Story


Many, many years ago, in Microchip, the MPLAB simulator development team developed the VHDL-like language SCL - Stimulus Control Language, with which you can create a testbench to help you debug the code. However, in connection with the departure from the author's team of the SCL implementation and the “complexity” of the language, it was decided to create a GUI that frees the embedded programmer from understanding the subtleties of a VHDL-like SCL and helps create test signal sequences and data injections into the registers of a simulated microcontroller. Such a GUI wrapper in the MPLAB IDE development environment is called Stimulus .

Secret language
Microchip did not specifically promote SCL and did not provide a language description. In 2006, it was even announced that the next version of MPLAB IDE v8.xx would generally remove support for SCL, but members of forum.microchip.com were against it and SCL was left in the next releases.

Some enthusiasts even started to “reverse” SCL (Stimulus can generate SCL files) and create an SCL tutorial and a repository of examples. Of particular note is the Russian-language work describing SCL, accompanied by several examples (from a simple generator, to emulation of UART and generation of DTMF signals).

Due to the fact that the new generation MPLAB X IDE has been completely rewritten and cross-platform (MPLAB X is based on NetBeans), the SCL implementation has also been rewritten and the current developers have included the “unofficial” SCL User's Guide in the MPLAB X IDE documentation.


GUI Stimulus features


Now Stimulus allows:
A) create synchronous sequences of changes in the states of the outputs or values ​​of the registers (convenient for the formation of repeating sequences:
At the time of the Xn state (pin, register) = XX
During Xm state (pin, register) = YY
The process can be repeated from step i; between repetitions ask a delay ii.

B) create complex effects:
If the condition that (pin, bit or register) becomes (=,! =, <=, <,> = Or>) is satisfied, then through N (ticks, ns, ÎĽs, ms, or cm / s), execute ( one time or with period M) impact (one or more): write value to the register, change pin or bit state, set voltage on analog input.

B) generate signals:
On the pin (pin_yy) with the initial value (HIGH or LOW), output a sequence (the duration of the HIGH and LOW state is set). The start and stop conditions are set (the program starts either by the value of the program counter or at a specified time or when the value on pin_xx == HIGH or LOW).

D) data injection into registers:
From the specified file, take the value and write it to the specified register. The action to produce when: reading from the register occurs (that is, reading the register in the program being debugged) or the program counter will take the fixed value or the condition specified in the file will be fulfilled. For example, you can inject data into the UART from a file of the form:

wait 1 sec // 1
01 54 106 //
02 55
wait 200 ms // 200
“hello” //
rand 1 15 sec // 1 15
32 33 34

Stimulus, in addition to the above, allows you to save the created effects as a SCL file and (!) Load your own SCL scripts.

SCL


Stimulus is quite a powerful tool, but it has significant limitations - it serves mainly for defining simple and deterministic influences; not every behavior model of the “external environment” can be described using the GUI offered in the development environment.
SCL, being a programming language, gives more flexibility: conditional expressions are available (if-elsif-else constructions), cycles, instructions for working with files, etc.

Some SCL features:


That is, SCL can do everything that is available through the Stimulus GUI in MPLAB IDE. But SCL has more opportunities and allows you to create complex dependencies between the values ​​on the outputs and the values ​​of the registers of the microcontroller; simulate interaction with the outer periphery; simulate impacts that are difficult to reproduce in the hardware and identify situations that lead to errors. In addition, the development environment allows you to connect multiple SCL files for parallel execution. This can be useful, for example, to connect to the project typical testbench-s.

Examples


For example, to debug and test a software decoder, you need to generate a Manchester code at the input of the microcontroller. With the use of Stimulus, the task can be accomplished by forming a sequence of changes in the state of the output, but this is too dreary, since each change of bits needs to be registered with the hands, like this:



Now, if you want to change the first bit, then you need to rewrite the entire sequence.

Another thing is to form a sequence using SCL: we write testbench, which takes an input bitstream (not encoded), encodes it and sends it to the input of a simulated microcontroller. For the convenience of changing data, the input bitstream can be taken from an external file. Along the way, our testbench can display informational messages in the simulator window.

Sample SCL file:


 configuration for "pic16f1509" is end configuration; testbench for "pic16f1509" is begin process is file fileVar : text; variable lineVar : line; -- ,    variable status : file_open_status; variable data_in : integer; begin loop file_open(status, fileVar, "dat.txt", read_mode); while endfile(fileVar) == false loop -- ,     readline(fileVar, lineVar); report(" "); RB5 <= '1'; --   (1/2  ) report ("== start bit =="); wait for 500 us; while match(lineVar, "") == false loop --  ,     read(lineVar, data_in); if data_in == 0 then --  0 report ("== 0 =="); RB5 <= '0'; wait for 500 us; RB5 <= '1'; wait for 500 us; elsif data_in == 1 then --  1 report ("== 1 =="); RB5 <= '1'; wait for 500 us; RB5 <= '0'; wait for 500 us; else report(" !"); end if; end loop; -- match InLine end loop; -- endfile RB5 <= '0'; file_close(fileVar); wait for 15 ms; --    end loop; wait; end process; end testbench; 


External data file with a bit sequence:


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 1 1 0 0 0 0 0 0 1 1
1 1 1 0 1 0 1 0 0 1 1 1 0 0 0

In the case of SCL, it is much easier to change the data, the length of the sequence and the temporal parameters of the simulated stream, which makes it easier to check the algorithm, error handling, etc ...



The SCL script can parse the input file and receive external commands. For example, in addition to the bitstream, you can change the parameters of the signals.

An example of setting parameters for a PWM-encoded parcel with a synchro-preamble (commands parse with the SCL script and set the corresponding delays):
sync_bit_hi 2000 us
sync_bit_lo 500 us
pulse_lo 500 us
pulse_hi_0 500 us
pulse_hi_1 1000 us
1 0 0 0 1 1 0 1
wait 2 ms
1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0
wait 3 ms
0 0 0 0 1 1 1 1
1 0 1 0 1 0 1 1
wait 15 ms



The ability to work with external files significantly expands the possibilities of simulation. So, in the “SCL repository” (link at the end of the article) there is an example of obtaining data for simulation from an external application (Input to a Simulation From An External Application). This example simulates a matrix keyboard and an input signal for an ADC (written in VBA in Excel). “Pressing” buttons and voltage changes are recorded in two files, the SCL script reads these files and simulates a change in the logic levels of the I / O ports and the voltage at the ADC input.



Results


The SCL program has labels, addresses and variables of the code being debugged. With or .asm code. The state of the registers, the program counter of the microcontroller, the simulation time are available. SCL allows you to create multiple parallel processes within a single testbench. For example, at one input we form a bit sequence, read data from another pin, process and control the state of other inputs or the state of microcontroller registers.

Thus, SCL helps organize testbench-and to debug the code of the microcontroller. Since SCL is part of a native simulator, SCL scripts are available in one project and in one debug cycle (yes, several SCL scripts can be connected to the project being debugged), breakpoints, stopwatch, stack, logic analyzer, etc., peripherals are being simulated (timer, UART, ADC, etc.), interrupts and it all works in the same development environment.

Some shortcomings of the current SCL implementation should be noted:
- limited debugging capabilities of the SCL code (when connecting to the draft script with an error, we receive only a message like “parser error in line No. xx”). If, when connecting the script, you received a message about duplicating variables, then, most likely, you connected the same testbench a second time

- some simplifications in the current implementation (MPLAB X)

You can read more about SCL here:


Source: https://habr.com/ru/post/262845/


All Articles