Topics / Index /
Download source
/ Deutsch
simbase: Simulate a Microprocessor
Content (automatic)
- #content : Content (automatic)
- #goal : Goal of "simatmel"
- #example : Example of a debug session
- #installation : Installation
- #types : Processor Types
- #files : Used Files
- #Input_files : Input files:
- #Output_files : Output files:
- #commands : Commands
- #invocation : Invocation of simatmel
- #immediate : There are generated two logging files:
- #set : General: set- set options
- #history : General: historyh History command
- #start : BASIC: Automatic start of the BASIC program
- #run : BASIC: run- Start the BASIC program from start
- #system : BASIC: system- Finish all debugging and close the application.
- #end : BASIC: stop cont end - stop the BASICprogram so it cannot be continued
- #return : BASIC: gosub label return - Continue astopped program
- #function : BASIC: function- Call a BASIC subroutine from commandline
- #goto : BASIC: label and goto
- #sym : BASIC: sym - Display all Variables
- #valiables : BASIC: Variables are defined by its type
- #expression : Expression
- #expression : Examples for expressions, used with the print command:
- #expression : Example:
- #expression : Example:
- #expression : Example
- #let : BASIC: Assignment of expression to a variable
- #com : BASIC: com - set a comment
- #print : BASIC: Command : print p andlprintlp
- #text : BASIC: Definition text : StringVariables
- #text : Example :
- #dump : Target: dDump a piece of memory d from to
- #lasm : Target: l lasmList Target Programs Assembler Code
- #di_com : Target: diDisassemble and com comments
- #r : Target r: Display registers
- #listlabel : Target: listlabel or llab : List Labels
- #listlabelvalue : Target: listlabelvalue or vlab : ListLabelsof Value
- #ss : Target: Command silentstep and singlestepss- execute a single instruction
- #bp : Target: bBreakpoints
- #trace : Target: t trace - execute a program and displayeach step
- #execute : Target: execute e - execute a program fast
- #hints : Hints
- #seealso : See also
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 :
- One, simbase.cpp, which does the dialogs and some
simulation.
- A second source which contains the processor specific routines,
especially the single step.
The second file may be :
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 :
- General commands
- -help Starts the Internet Explorer
to display this page. The internet exploere is started by an external
batch file C:\extends\ie.bat or ~/home/cc/linux/ie
- -restart Restarts the
microprocessor and the BASIC program. The BASIC program is reread from
file.
- +history hi Display the logging
in notepad.
- system : Leave the simulator, Quit
- BASIC commands
- +uint8_t uword ulong schar sword sint32_t
double
text Create variables
- +sym
show all BASIC symbols and its
content
- +goto
BASIC jump to a +label
- +stop halts the basic execution.
The execution can be resumed by the cont command
- +cont continues a halted BASIC
program
- +bss single steps through a halted
BASIC program
- +run Restarts the execution
of the
basic program from memory
- +gosub
a call to a part of BASIC
program.
- +return
Return from a
gusub-Subroutine
- -end stop the basic program. It
cannot be restarted.
- -tron and troff Start stop
the BASIC tracing: All commands are logged, while they are executed.
- +label Mark a point in the BASIC
program. A goto or a call can
branch to this point
- -list List Basic code
List Target Programs Listing as produced by assembler
from and to are HEX values.
- +p print lprint : print
(displays) multiple variables
- +destvar = expression
Assignment of expression to a variable
- d from to : dump a piece of RAM (Atmel:
0..31=R 32..95=I/O 96..=SRAM)
- Target Code commands: The target commands are common for all CPU
types.
- Settings
Invocation of simatmel
The invocation is performed from the DOS command line with the file
name. Example:
sim84 filename
This tries to load
-
- filename.lst, if this listing file of gpasm exists.
- filename.hex - The HEX File definition and filename.kom - the
comment file.
- filename.bas - The BASIC file which is executed at start.
There are generated two logging files:
- filename.jrn : This file
may be copied to the end
(or into an apropriate part of) filename.bas -
Then all the commands which have been entered are
executed at next start again. (remove the last line "system")
- filename.log : All
display output is written to this log file.
The history (short hi) command invokes
the notepad editor to display it.
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.
- set radix hex or set r h or set r 16
- Decimale constants are :
- 1.0e3 = 1000 because the . implies decimale constant
- 0d13 = 13 because 0d switches to decimale
- hex constants are
- abc = 2748 because the radix is set to hex
- 10 = 16
- 0x10 = 16 because the prefix 0x
- set radix dec
or set
r d or set r 10
- Decimale constants are :
- 1.0e3 = 1000 because the . implies decimale constant
- 10 = 10
- hex constants are
- 0xabc = 2748 because the leading 0x switches to hex
- 10h = 16 because the trailing h switches to 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 :
- Execution of BASIC program in tron
mode
- output of BASIC command lprint
- The processor state after each singlestep
instruction.
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:
- A 8 bit register name: indir, tmr0, pcl, status, fsr, porta,
portb, grey, eedata, eeadr, pclath, intcon, gp0c, gp0d, gp0e, gp0F,
gp10 ... gp7f, option, trisa, trisb, grey, eecon1, eecon2, w, wdt.
- A 16 bit register name: pc, prescale.
- A 32 bit register name: cy.
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
- uint8_t name : ' Value range 0 to 255
- sint8_t name Value range -128 to 127
- uint16_t name Value range 0 to 65535
- sint16_t name Value range -32768 to 32767
- uint32_t name Value range 0 to 4294967295
- sint32_t name Value range -2147483648 to 2147483647
(ca. 2e9)
- float name Value range
+-340.28232635E+36 (See ../lq/float.htm)
- double name up to +- 89.8846567431157694E306
(MDOS50) about 15 significant digits
- text name Value can be a text string of up to 127
chracters
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
- uint8_t are values from 0 to 255 e.g. : 63 , 129
- uint16_t are values from 256 to 65535 e.g. : 500, 20000
- uint32_t are values from 65536 to 2^32-1 e.g. : 1000000
- float are either numbers with a dot and a trailing f, or decimale
or hex values with a trailung .f e.g. : 3.14f 0x3f800000.f
- double are all values which have a decimale point e.g. 1.0
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:
- The negation bit inversion -x ~x and the logical not !
- The multiplication division remainder * / %
- The addition subtraction + -
- The shift operators << >>
- The comparison < > <= >=
- The comparison == !=
- The bit and &
- The bit or |
- The bit xor ^
- The locigal and &&
- The logical or ||
- The decision ? : : Example: a = b < c ? b : c
- The least priority have the paranthesis ( ) the comma , -
they are calculated after all other operators
Mixed expressions
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
- Define a float array and fill
some values
float vw[3][4]
vw[1][2] = -15.675
vw[2][3] = 24.231
p "float print"
p vw[1][2], vw[2][3]
p "define text"
text txa.10 ' this text variable can hold 10 characters
p "symbols are:"
sym
p "text defined"
txa = "dies ist "
p txa
- Demonstrate how a variable is created and overwritten
Hints
- If debug an assembler program, then create dummy entry point, so
you can use their addresses for Breakpoints.
- Have a function key definition BASIC file as an include file, so
you can use the same function key in any project.
- After you have manually entered some useful commands, look in the
journal file. Cut and paste them into your BASIC program as a function.
The history works as a macro-recorder. Even after exit by the system
command the history is in the .jrn file.
- When you set the radix to 16, then you easy can enter hexadecimal
program addresses, but then all basic variables will be interpreted
hexadecial as well, if then are not preceded by 0d
- Avoid variable names which start with as a,b,c,d,e,f - when set
radix hex, then these are valid numbers. as well aaaa or ffff
- I write code for the simatmel-BASIC. While I enter line by line
in the Editor (I prefere pn on Windows) I copy & paste then from
the editor into the simatmel command window. So I can check if they
wark in he way I want.
- Even if variables as "tcnt1l" or "tcnt1" do exist, the timer and
other I/O hardware is not simulated.
See also