Avr Sim Description 28
Avr Sim Description 28
avr_sim
3 Code execution
3.1 Hex code generation
The hex code to be executed is generated by the integrated gavrasm command line assembler: it as-
sembles the source code and produces a hex code file in Intel hex format and with the
extension .hex.
If assembling was successful the generated hex file is read and its content is written word-wise to
the flash array aFlash. The hex code is read from that array.
3.2 Code decoding and execution
Starting from the flash address 0000, the hex code there is read from the array by the procedure Ex-
ecuteStep. This decodes the instruction words as follows:
1. If the code is $FFFF, an uninitiated part of the flash memory is accessed and an error mes-
sage is processed.
2. The upper six bits of the code are then isolated and serve as a pre-selector for the instruc-
tions:
◦ A zero stands for the multiple instructions NOP or for MOVW/MULS/MULSU/
FMUL/FMULSU Rd,Rr.
◦ A one stands for the instruction CPC Rd,Rr, a two stands for SBC, a three for LSL or
ADD, and so on until 11.
◦ The following combinations use two bits of the upper six and address 8-bit constant in-
structions, where the upper two bits of the constant modify the lower two bits of the six
bits:
▪ 12..15 stand for CPI Rd,K,
▪ 16..19 are for SBCI Rd,K,
▪ 20..23 stand for SUBI Rd,K,
▪ 24..27 are for ANDI Rd,k, and
▪ 28..31 stand for ORI Rd,K.
◦ The combinations 32..35 and 40..43 address various LD/ST and LDD/STD instructions.
◦ The combinations in between, 36..39, address a very large variety of instructions, which
have to sub-decoded.
◦ 44..59 encode I/O, relative jumps/calls and load instructions.
◦ 60 and 61 encode relative jump instructions,
◦ 62 and 63 stand for bit manipulations in registers.
The codes that address multiple instructions (e.g. 0) use special code to recognize the different in-
structions.
The flags that instructions set or clear are held are performed by the procedure ChangeSreg, which
expects the flag’s abbreviation (“C”, “Z”, etc.) and an “S” (for set) or a “C” (for clear) as second pa-
rameter.
Instructions that manipulate register content read and write to the 32 registers in the aReg byte ar-
ray.
Instructions manipulating ports, such as OUT or ST/STS instructions pointing to ports are per-
formed by the procedure ChangePort. That ensures that changes to the hardware are recognized
and the respective hardware units get aware of any changes made.
As most of the AVR instructions are single clock cycle, the default of advancing the clock cycle
counter is one. One time, or in case of more cycles several times, the routine IncClock is called and
advances timers accordingly, according to the number of consumed clock cycles. The distance from
the current to the next executed instruction, normally +1 but different in relative jump instructions,
is held in the variable nPcAdd. That ensures that the jump leads to the correct flash location ad-
dress.
4.1 I/O-ports
All I/O port registers are handled in the unit avr_ports.
This unit has been changed in many versions to improve its
functioning, because the display of the I/O ports is rather so-
phisticated. Two cases have to be handled:
1. Program execution changes I/O port registers, so those
changes have to be reflected in the display.
2. A user click onto I/O register bits changes I/O regis-
ters, which has to be reflected in the display.
The main changes to I/O registers all might affect the PIN bits.
If the DDRn bit in a port is set, the PINn bit has to follow the
respective PORTn bit. If the DDRn bit in a port is clear and if the PORTn bit is set, the PINn bit is
in an active Pull-Up state and reads as one. The user has then to be able to click on the PIN bit to
make the input pin low. This can also trigger INTn and PCINTn interrupts, if so selected.
The ports are displayed using a TStringGrid component. This allows to comfortably change text and
background colors of the displayed strings by applying the TStringGrid.PrepareCanvas and .Draw-
Cell methods. That was exactly what was needed for this kind of display.
In the first version of avr_sim the user action of clicking into a cell of TStringGrid was programmed
as a reaction to the TStringGrid.SelectCell event. This turned out to be instable, because the compo-
nent itself sometimes initiates this event (e.g. when drawing the component for the first time), so I
had to add diverse flags to control that and to get this stable and correct. In version 2.6 of avr_sim I
changed that to the TStringGrid.MouseUp event, after I learned how to translate the mouse position
to the column and row positions of the StringGrid. Much better and much more stable than the Se-
lectCell method.
A lot of work changed this unit in version 2.6: all user actions to the string-grid component did not
work correct. Note that the PCINT-programming has changed considerably to get this corrected.
Minor changes were also made to the TC unit to get user clicks on TINn bits correct.
This I/O port unit provides the following published procedures:
• PortReset: This sets all port variables (namely their addresses) to their default value (ad-
dresses: $FFFF) and is called on every start-up (on form initiation, on start of the project file
reading). It also calls PortRestart to clear the complete port register array as well as the port
name associations.
• PortRestart: This clears all port registers to their default and is called on every restart of the
simulation.
• UnmarkAll: This sets all previous port values to the current values.
• UnmarkLast: The last change to a port’s value is copied to the previous port value (called on
every manual step to avoid lengthy copy operations).
• ToggleIOPort: If a write access occurs on a PIN port register (by program execution only,
not by user action), the bits that are one toggle the bits in the respective PORT register, if the
device provides this toggle feature. This procedure is called by the ChangePort procedure in
the avr_sim_u1 unit.
• ChangeIOPort: This procedure handles all write operations to the PORTn, DDRn and PINn
port registers. The major function of the procedure is to reflect changes to the PORTn and
DDRn port registers, which change the PINn register accordingly.
• CountPortsPinsInts: This counts the number of available ports, pins and ints/pcints. It is
called by the main unit when preparing the project window.
• DisplayPort: The currently selected port is displayed in this procedure.
• DisplayActivePort: If the port is not fixed, this procedure switches the port to be displayed
and displays this port.
• MarkInt, UnMarkInt: This procedure marks/unmarks the currently executed INTn pin and
displays the background color of INTn.
• MarkPcInt, UnmarkPcInt: This procedure marks/unmarks the currently executed PCINTn
pin.
• SetIsc: This procedure sets the ISC bits of the INTn and displays their background color.
• SetIntCR: Changes to INTn’s interrupt control register are processed in this procedure.
• SetPcMsk: Changes to the PCINT’s interrupt mask register are handled here.
• SetPciCR: Changes to the PCINT’s interrupt control register are processed here.
• StartInt: An INTn has been initiated and the clock cycle counter is started.
• StartPcInt: A PCINTn is started here by setting the clock cycle counter.
• TickInt, TickPcInt: The clock cycle counters of INTs and PCINTs are down-counted and it is
checked whether the INT or PCINT condition is still valid.
4.2 Timers/Counters
TBD
4.4 AD converter
TBD
4.5 EEPROM
When starting simulation or when restarting simulation, the content of the .eep file is read to the re-
spective byte storage space. When EEPROM content is read it is taken from this byte storage. If
new EEPROM content is written, the respective procedure with setting the address in
EEARH:EEARL and the data register in port EEDR and finally setting the EEPROM-write-enable
bit is followed. The timing of the EEPROM write process uses a clock-specific counter, which en-
ables to display the write process in a progress bar display.
The interrupt on completion, when enabled, is displayed like any other of the interrupts (green if en-
abled, yellow if requested, red if executed).
In single steps, the complete EEPROM content is compared with the content one step before. If
there are any changes in content, the display window is updated. In run/go mode, this comparison
and update is only made when the run/go mode is finished.
4.6 SRAM
The complete SRAM content is set to 0xFF when simulation starts or when a simulation restart is
selected. Content changes are recognized by comparing the complete data before and after each
step, in run/go mode only after the simulation is stopped.