Cornell University ECE4760
Language Interpreters
for Atmel Mega644 microcontrollers
Introduction
It is useful to be able to write small programs in a scripting language. This example of an interpreter is based on a C program written by Adam Dunkels, originally to implement, in his words: A really tiny and stupid BASIC interpreter, written in a few hours for the fun of it. (see http://www.sics.se/~adam/ubasic/).
I hacked it to be more of a tiny and stupid, block-oriented,
bracket-delimited, language and ported it to the Mega644 in WINAVR/GCC. I also
made a very simple interactive download utility so that source code (see example
below) can be written on a PC editor, then sent to the microcontroller as a
text file, terminated with the @
sign.
# test program ;
print 'starting demo';
peek 3+32, c ; # look at pinb ;
while c=255 {
print 'press button';
delay(200);
peek 3+32, c ;
} ;
poke 7+32, 255 ;
for i= 0 to 10 {
peek 3+32,c ;
poke 8+32,i ;
print 'i=',i ;
for j=10 to 13 {print 'i-j=', i-j;};
if c=255 {print 'press button';};
if c<255 {
print 'button pushed';
delay(500);
if c=254 {print 'button0';};
}
else {
print 'no button' ;
};
delay(500);
};
print 'done' ;
end;
@
Files
The first pass of modification just uses a string embedded in the C interpreter
as source. You need to download the following files. The source string and
main
are in use-ubasic.c
.
These files are modified versions of the programs on Adam Dunkel's site.
Program structure
The following table shows the syntax of the existing statements. You can easily add your own statements.
+,
-, *, /, %, &, |
), variables and parentheses. The mod operator
is %
.expr1>expr2
or expr1<expr2
or expr1=expr2
. a-z
. The more formal BNF notation for the language is:
<variable> ::= <letter>
<number>
::= 0 to 32768
<variable_value> ::= contents of <variable>
<relation> ::= <expression> ("<" | ">" | "=") <expression>
<expression> ::= <term> {( "+" | "-" | "&" | "|"
) <term>}
<term> ::= <factor>
{( "*" | "/" | "%"
) <factor>}
<factor> ::= <number> | <variable_value> | "(" <expression>
")"
<statement_sequence> ::= <statement> { <statement> }
<statement> ::=
<assignment_statement> | <if_statement> |
<while_statement> | <print_statement> | <for_statement> | <peek_statement> |
<poke_statement> | <delay_statement> | <comment statement>
<comment statement>
::= "#" [<any characters
except ";">]
";"
<assignment_statement> ::= <variable> "=" <expression>
";"
<peek_statement>
::= peek <expression> ","
<variable>
";"
<poke_statement>
::= poke <expression> ","
<expression>
";"
<delay_statement>
::= delay <expression>
";"
<if_statement> ::=
if <relation> "{" <statement_sequence> "}"
[ else "{" <statement_sequence> "}" ] ";"
<while_statement> ::=
while <relation>
"{" <statement_sequence> "}"
";"<for_statement>
::=
for <variable> "=" <expression> to <expression>
"{" <statement_sequence> "}"
";"All statments are terminated with a semicolon. Spaces, tabs, newlines are all ignored. All program constructs must be lower-case.
statement |
example |
---|---|
for var = expr to expr { statement; statment; ... } ; |
for i=1 to 10 { j=j+1; print j; } ; |
while relation { statement; statment; ... } ; |
while i<10 { i=i+1; print 'i=',i; } ; |
|
if c<255 {print c; delay(500);} |
print expr, 'string', ... ; |
print 'hi there', i+1; |
peek address, var ; |
peek 3+32, x; # peek address, variable; |
poke address, expr ; |
poke 40, i+2; # poke address, value |
delay expr ; |
delay 500; # milliseconds ; |
# this is a comment statement ; |
# comment ; |
var = expr ; |
a = (i+1)/j; # assignment ; |
Performance
A for-loop executes in about 100 microSec, and a 2-level nested for-loop in 200 microSec. A 16-bit add, subtract, multiply, or divide takes 200 microSecs.
Interactive version.
A simple interactive interface was built on top of the intrepeter so that
a program can be downloaded from
a PC using hyperterm's sendfile feature, then
executed or saved in eeprom. The source code sent from the PC must end with
an at sign @
. The revised interperter files are here.
There
are six commands:
Command | meaning |
---|---|
help |
Prints the other 5 commands. |
down |
Sets download of program from PC using hyperterm transfer>send
text file the text file is terminated by an @ symbol. Prints download
complete when it finishes. |
run |
Executes the program. |
load |
Gets a program from eeprom. Prints load complete when
it finishes. |
save |
Puts a program into eeprom. Prints save complete when
it finishes. |
list |
Prints the program text. |
Copyright Cornell University September 1, 2011