Topics / Index / Download source / Deutsch

simbase: Simulate a Microprocessor

Content (automatic)

Goal of "simatmel"

The goal is not so much to simulate all different types of atmel processors. "simatmel" just simulates atmega32.
But simatmel has a built in BASIC interpreter, which helps to examine the program flow and the data in memory and in the registers.

"simatmel" is 2007-03 in an alpha-version state. This means: If it is useful, then be happy, if not, tell me. (post to avrfreaks.net) As I program it in my spare time, I will not response quickly.

Example of a debug session


From the DOS Prompt go to the directory of the project. Then start simatmel

cd C:\home\cc\avr\qqtsfloa
simatmel qqtsfloa.lst
File QQTSFLOA.log does exist, want to [d]elete or [a]ppend ?
 Press one : [da]d
Memory data found from 0=0H   ends at 12757=31D5H
Reading QQTSFLOA.hex
BASIC ERROR:
end
BASIC Program held at line 0, error code 1
Cmd :bp __addsf3
Cmd :e
Executing ... hold by breakpoint
Stopped cy=2496, pc=25C5
Cmd [e] :p f18
2.000000
Cmd :p f22
0.000000
Cmd :bp addret
Cmd :e
Executing ... hold by breakpoint
Stopped cy=2544, pc=2655
Cmd [e] :p f22
0.000000
Cmd :





Installation

There is no installation, Just have "simatmel.exe" somewhere in your execution path (copy it to C:\WINDOWS)

Processor Types

The "simulation" program family for microprocessors are build from 2 main source files : Both simulators do not simulate correctly all instructions, but only those which I needed.
See there are two Microsoft developper workspaces: C:\home\cc\util\mdos50\simatmel\simatmel.dsw and C:\home\cc\util\mdos50\sim84\sim84.dsw.

Used Files

Input files:

name.bas : The BASIC program which is interpreted at start and after each cont command.
name.lst : The avr-gcc compiler / linker output
name.map : The avr-gcc compiler / linker output
name.hex : The avr-gcc compiler / linker output or a hex file from the prommer for people who have lost their source files.
name.kom : Where hand written komments about a hex file are saved.

Output files:

name.jrn : The Journal file is created where all commands are logged. This file acts as a recorder. Its content might be used to be cut&paste into the basic program.
name.log : this file may be analysed to see the history.
SIMBASE.DBG : The debugging output.  for the simatmel hacker (for Jurgen Herbert)

Commands


Command lines can be formed from a key word and optional parameters. Multiple commands per line are allowed and may be separated by colons [:]. The syntax is similar as basic, with some modifications. The key words are :

Invocation of simatmel

The invocation is performed from the DOS command line with the file name. Example: sim84 filename
This tries to load

There are generated two logging files:

Commands are entered at the command line prompt. Each command line is finished by the [Enter] key. Commands are executed immediatedly. Then the prompt for the next command line is displayed again. The command line prompt is: Cmd :
There is a preset default command, which is displayed in the command prompt. This default command depends on the command history. e.g. after a single step command "ss" another "ss" is preset, which is displayed in brackets.
Example:
Cmd :ss
0 0=3000 movlw 0 ; 100 w=0H
100 w=0H

Cmd [ss] :di 0 2
000=3000H movlw 00 ;Start (Reset)
001=0086H movwf 06 ; PORTB ;To PORTB
002=2807H goto 007
Cmd :ss
1 1=0086 movwf 6 ; 6 portb=0H
100 w=0H
6 portb=0H
1 tmr0=1H

Cmd [ss] :
2 2=2807 goto 7H ; pc=6H 1 tmr0=2H

Cmd [ss] :di 0 2
000=3000H movlw 00 ;Start (Reset)
001=0086H movwf 06 ; PORTB ;To PORTB
002=2807H goto 007
Cmd :

General: set - set options

The set radix command determines if entered constants (even numeric values in expressions) are treated decimale or hex.

General: history h History command

Command history or hi opens the logging in a extra editor window.
During a session lots of information goes to the logging file :

BASIC: Automatic start of the BASIC program

The basic program is automatically loaded from the .bas file and started, when simatmel is invoked. If this is not wished, then the very first line of the basic program should be the stop statement. (then it can be continued with the cont statement) or with end.

BASIC: run - Start the BASIC program from start

BASIC: system - Finish all debugging and close the application.

BASIC: stop cont end - stop the BASIC program so it cannot be continued

During the execution of a BASIC program the stop command stops the execution. The execution may be resumed by the cont command. Or the execution of the next basic command can be done by the bss (Basic single step) command.

After the end command no continue is possible

BASIC: gosub label return - Continue a stopped program

gosub - calls a subroutine
label - gives a name to the subroutine
return - goes back to the line after the gosub command
Example:
n = 1 : gosub pnt
n = 2 : gosub pnt
stop
label pnt
print "n=";n
return

BASIC: function - Call a BASIC subroutine from commandline

The function keys at the keybard are mapped to the functions "function f1" to "function f12", with shift to the functions "function s1" to "function s12", with control to the functions "function c1" to "function c12", with Alt to the functions "function a1" to "function a12"
But any label can be started with the function command:

The list of function key names is:
Key 2 is 6BH 107 name=k (k) at 20949000 msec 0
13BH=          f1
13CH=          f2  13DH=          f3  13EH=          f4  13FH=          f5
140H=          f6  141H=          f7  142H=          f8  143H=          f9
144H=         f10  185H=         f11  186H=         f12  15EH=          c1
15FH=          c2  160H=          c3  161H=          c4  162H=          c5
163H=          c6  164H=          c7  165H=          c8  166H=          c9
167H=         c10  189H=         c11  18AH=         c12  168H=          a1
169H=          a2  16AH=          a3  16BH=          a4  16CH=          a5
16DH=          a6  16EH=          a7  16FH=          a8  170H=          a9
171H=         a10  18BH=         a11  18CH=         a12  15EH=          c1
15FH=          c2  160H=          c3  161H=          c4  162H=          c5
163H=          c6  164H=          c7  165H=          c8  166H=          c9
167H=         c10  189H=         c11  18AH=         c12  00DH=       enter
150H=        down  148H=          up  14BH=        left  14DH=       right
009H=         tab  008H=     backtab  151H=    pagedown  149H=      pageup
147H=        home  14FH=         end  152H=      insert  153H=      delete
176H=   cpagedown  184H=     cpageup  177H=       chome  175H=        cend
192H=     cinsert  193H=     cdelete  1A1H=   apagedown  199H=     apageup
197H=       ahome  19FH=        aend  1A2H=     ainsert  1A3H=     adelete
01BH=       abort


BASIC: label and goto

The program flow can be altered by the BASIC command goto, which requires a label somewhere else.
Example

if b >= 24 then  goto M0
a = 2
goto M1
label M0
a = 3
label M1

BASIC: sym - Display all Variables

The command sym displays all basic labels (target processor labels are listet with the llab command)

BASIC: Variables are defined by its type

A variable is a place holder. In the simulator the processor internal variables can be accessed by their names: Example how pc and cy are used as variables:
Cmd :print pc,cy
0  0
Cmd :ss
0 0000__vectors__vector_default__eeprom_end C014 rjmp $00000015
rjmp $0015

Cmd [ss] :ss
2 0015 2411 eor r1,r1 ;
     PRE  sram[1,0001] aliasr1alias r[1,01] =0,00
          sram[1,0001] aliasr1alias r[1,01] =0,00
     POST sram[1,0001] aliasr1alias r[1,01] =0,00
          sram[95,005F] aliassreg Status Registeralias io[63,3F] =2,02

Cmd [ss] :print pc,cy
22  3
Cmd :

Other variables for user usage may be defined by the statements All variables can be displayed with the sym command.
Examples:

Cmd :uint8_t t1
Cmd :sint32_t t2
Cmd :double tr
Cmd :t1 = 203
Cmd :t2 = -t1
Cmd :tr = 8.343+2
Cmd :sym
                  t1=uint8_t  [0] =203
                  t2=sint32_t  [0] =-203
                  tr=double  [0] =10.34300
Cmd :


Other variables are read in from the map file one example is "main" which is the addresse of the function main()
All these target labels can be shown by the "llab" command.

Expression

Expressions calculate a value (and its type) They can be used as arguments for a command or for an assignment.
See ../lq/expressn.htm
For expressions constants can be used. There are different types. A good method to see the size of a constant (or an exression) is to print it convered to hex. e.g. enter p hex$(1.0) will display and log 3FF0000000000000
negative constants do not exist. The - sign will be interpreted as a unary operator which then returns sint8_t, sint16_t or sint32_t

There are three different types of variables:
Labels read from the .map file. e.g. main
BASIC defined variables (There is no DIM command, but int8_t, uint8_t int16_t uint16_t int32_t uint32_t float double text to create a variable)
Predefined variables. For the Atmel simulator simatmel there are
uint8_t : r0, r1 .. r31, io0, io1, io2, ubbrh, adcl, adch, adcsr, adcmux, adcsr, ubbrl, ucsrb, ucsra, udr, spcr, spsr, pind, ddrd, portd, pinc, ddrc, portc, pinb, ddrb, portb, pina, ddra, porta, eecr, eedr, eearl, eearh, wdtcr, assr, ocr2, tcnt2, tccr2, icr1l, icr1h, ocr1bl, ocr1bh, ocr1al, ocr1ah, tcnt1l, tcnt1h, tccr1b, tccr1a, sfior, ocdr, tcnt0, tccr0, mcucsr, mcucr, twcr, spmcr, tifr, timsk, gifr, gimskl, gimskh, spl, sph, sreg, m0
uint16_t : pc(=Program counter), w0 (=r1:r0) w2(=r3:r2)...w24, X; Y, Z, sp, adc, tcnt1
uint32_t : cy (=cycle counter)
float: a set of 4 registers: f0, f2 .. f18
double : us, f (=microseconds, frequency in MHz) (define the frquency first, then single step through target code, then print us.

Even if variables as "tcnt1l" or "tcnt1" do exist, the timer and other I/O hardware is not simulated

Examples for expressions, used with the print command:

float x
x = 9.3
p x, x+3, hex$(x)

Example:

Cmd :t1 = 88
Cmd :di t1+40
080=0000H undefined

because t1 is 88 the expression t1+40 is 128, which is hexadecimale 80:

Example:

Cmd :double c1msec
Cmd :c1msec = f/1000.0
Cmd :p c1msec
10000.000000


f  is the processor frequency, then the amount of cycles for 1 millisecond can be calculated.

Example

print hex$(r16),hex$(34),hex$(5*16+6),hex$(100*200*200)
00 22 00000056 003D0900

Operators are interpreted in respect to their priority: Mixed expressions

float















BASIC: Assignment of expression to a variable

destvar = expression : Fill a variable with a computed value
There are 3 types of variables which can be filled:
BASIC defined variables, which have been defined by one of the data type commands: int8_t int16_t int32_t uint8_t uint16_t uint32_t float double text
Processor defined registers, for the atmel these are r0 r1 ... r31 pc cy
Example :
pina = 3
pinb = pina * 2 + 0x1e
r 5:6
5 pina=3H 6 pinb=24H
print pina;pinb
3
36
See Expression

BASIC: com - set a comment

useful when disassembling. Komments are written in a file when finished


BASIC: Command : print p and lprint lp

The print command can be used to display the value of any expression. The lprint command outputs to the log file.
Example : Cmd :r28=45
Cmd :print r30;r28;r30
1  45  1
Cmd :p r28*r28
2025

BASIC: Definition  text : String Variables

String variables may be used to store character strings. The default length is 127 characters.

Example :

text txtv1 ' define a text variable with length 80
text txtv2[4] ' define a text variable with maximum length 4
text txterg
'
txtv1 = "abcdefg" ' assign a text string
txtv2 = "123456789"
txterg = txtv1 + txtv2
'
print "txtv1= ";txtv1;" txtv2= ";txtv2;" txterg= ";txterg
sym ' show all symbols
when executed the follow is displayed: txtv1= abcdefg txtv2= 1234 txterg= abcdefg1234
txtv1=text [0]*128 =[abcdefg]
txtv2=text [0]*5 =[1234]
txterg=text [0]*128 =[abcdefg1234]
x=uword [0] =7
y=uword [0] =9
z=uword [0] =63

Target: d Dump a piece of memory d from to

The d command expects 2 arguments: The start addresse and the end addresse.
Example

Target: l lasm List Target Programs Assembler Code

The lasm or l command lists a single or multiple lines of targets assempler code, as it has beenread out from the assembler listing. The l command does not work, if  the simulator works on  a HEX or BIN file.  (see also #di_com)
Example 1: Cmd :l e,12

00000e b50c in rtmpl,TCNT1L ; Read 16 Bit Timer
00000f b51d in rtmph,TCNT1H
000010 1b02 sub rtmpl,rt1sl ; and subtract start value
000011 0b13 sbc rtmph,rt1sh ; rtiml,rtimh = elapsed time
Cmd :

Target: di Disassemble and com comments

The command dissassemble from to
di from to
Disassemble target processors program memory. The address expressions are decimale, if want to disassemble hex addresses, then use 0x: or set r 16

The command :
com addrs sets a comment to an address.
Example: Cmd :com 0x35
Comment for 35H ? here is the comment test text
ERROR :Komments changed, want to write back filename.kom ?
Press [Y]es [N]o Yes
Cmd :di 0x30 0x38
030=009BH movwf 1B
031=189BH btfsc 1B,1
032=151BH bsf 1B,2
033=3004H movlw 04
034=00A4H movwf 24
035=122BH bcf 2B,4 ;here is the comment test text
036=1005H bcf 05,0 ; PORTA
037=21F4H call 1F4
038=20C8H call 0C8

Target r : Display registers

Example :
Cmd :set r 16
set radix hex
Cmd :r4=10-2
Cmd :r 0 7
00 = 00H=  0 ;  00H=  0 ;  00H=  0 ;  00H=  0
04 = 0EH= 14 ;  00H=  0 ;  00H=  0 ;  00H=  0
Cmd :set r 10
set radix dec
Cmd :r4=10-2
Cmd :r 0 7
00 = 00H=  0 ;  00H=  0 ;  00H=  0 ;  00H=  0
04 = 08H=  8 ;  00H=  0 ;  00H=  0 ;  00H=  0
Cmd :r 32 35
20 = 00H=  0 ;  00H=  0 ;  00H=  0 ;  00H=  0
Cmd :r 96 99
60 = 00H=  0 ;  00H=  0 ;  00H=  0 ;  00H=  0
Cmd :

Target: listlabel or llab : List Labels

The command llab shows all labels which have been read from the .map file.
The command llab *bit* shows all that labels which consist the string "bit". 

Target: listlabelvalue or vlab : List Labels of Value

The command vlab expression shows all labels which have been read from the .map file and have this value.

Target: Command silentstep and singlestep ss- execute a single instruction

This command executes one instruction of the simulated mikroprocessor. The microprocessors registers are updated.
Example : Execute until a condition is met uint8_t sc

print "Starting"

label To200
if cy > 200 then goto At200
silentstep
goto To200


label At200
print "I am at cycle 200"
end

Target: b Breakpoints

Breakpoints stop the execution e of the target processors code, not the BASIC execition.
bp addrs : Set a breakpoint (with no conditions, no code) at address (or address expression).
b
breakpoint List all breakpoints.
b n,var condition expr,action : Set Breakpoint number n. When the condition is met (var condition expr) then the action is executed.
n: Up to NBreakPoint = 20 breakpoints can be set. The number reaches from 0 to 19 The condition must
var == expr : The expression is calculated when the breakpoint is set. During execution after each step the breakpoint condition is checked.
action: The command which has to be executed each time the breakpoint is reached. Examples are: b 5,pc==0x15
Here a breakpoint at condition variable pc equals 21 (hex:15) is set.

b 5,pc==loop
Here the label is used instead the adress as numerical value.

b 5,pc==loop:print hex$(17),hex$(16)
Here the label is used instead the adress as numerical value. bp - PC Breakpoints: the bp command is a shortcut to set a pc breakpoint. Remember to use 0x if the address is HEX

Example:
Cmd :bp 0xe
Cmd :b
Breakpoints are :
0: pc == 14 EH loop:
Cmd :e
Executing ... haltet by breakpoint
Stopped cy=14, pc=E
Cmd :

Target: t trace - execute a program and display each step

The trace can be stopped by the Esc key.

Target: execute e - execute a program fast

The execution can be stopped by the Esc key.
The execution stops when the program reaches a breakpoint or a break condition.

Examples of BASIC program snippets

Hints

See also