Transcript 投影片 1

ceForth for Arduino
2012 FIG Taiwan Conference
February 16, 2012
Chen-Hanson Ting
Summary









A Native FORTH System for Arduino
The Fundamental Problem
A Solution
FORTH Virtual Machine
FORTH Dictionary
ceForth_328 Sketch
ceForth Metacompiler
Compiling to RAM Memory
Demomonstrations
A Native FORTH for Arduino



A native FORTH system which can be
loaded as a Arduino Sketch.
Must be written in C (Arduino Process)
and does not require separated AVR
programmer.
Attractive to beginning Arduino users.
Arduino Uno with AVRISP mkll
Arduino Uno Alone
The Fundamental Problem




ATmega328P is a microcontroller of Harvard
Architecture with separated program and data
space.
C is a programming language of Harvard
Architecture with hidden program space.
FORTH is a programming language of
Princeton Architecture, with unified program
and data space.
How to force a Princeton Architecture on a
Harvard Architecture computer?
A Solution



A FORTH Virtual Machine programmed
in C.
A FORTH dictionary constructed by F#
and imported into the C program as a
data array.
The dictionary can be extended into the
RAM space so FORTH can grow.
FORTH Virtual Machine



33 Pseudo instructions as byte codes.
Pseudo instructions can be stored either
in program or data memory.
A Finite State Machine to execute
pseudo instructions.
FORTH Virtual Machine



A unified memory model spanning both
program and data space.
Program and data are mapped to
different areas in the memory space.
FORTH dictionary is initially loaded in
program memory, but can be extended
into data memory.
Finite State Machine
void setup()
{
clock = 0; phase = 0; P = 0; IP = 0;
S = stack; R = rack; top = 0;}
void loop()
{
phase = clock & 3;
switch(phase) {
case 0: fetch_decode(); break;
case 1: execute(I1); break;
case 2: execute(I2); break;
case 3: jump(); break;
}
clock += 1;
}
FORTH Dictionary




FORTH commands are records linked into a
dictionary.
A command record has a link field, a name
field and a code field.
A primitive command has pseudo instructions
in code field.
A compound command has a token list in
code field.
Primitive Commands
void
void
void
void
void
void
void
void
void
void
drop(void) { pop; }
dup(void) { *++S = top; }
swap(void) { w = top; top = *S; *S = w; }
over(void) { push S[-1]; }
zless(void) { top = (top & 0X8000) LOGICAL ; }
andd(void) { top &= *S--; }
orr(void) { top |= *S--; }
xorr(void) { top ^= *S--; }
uplus(void) { *S += top; top = LOWER(*S, top) ; }
nop(void) { jump(); }
Compound Commands

Compound commands are produced by
ceForth_328 metacompiler as lists of
execution addresses.
ceForth_328 Sketch




Pseudo instructions coded in C.
Finite State Machine coded in C.
A dictionary imported as a data array.
The dictionary is produced by
ceForth_328 metacompiler.
ceForth_328 Memory Space






0000-00FF
0100-02FF
0300-031F
0320-087F
0880-08FF
0900-1FFF
ATmega328 registers
Data space assigned by C
FORTH variables
Free space for dictionary
TIB/ATmega328 stack
Dictionary in flash memory
cefMETA328 Metacompiler
All files compiled by cefMETA328.fex under F#
 cefMETA328.f
Metacompiler
 cefASM328.f
Assembler
 cefKERN328.f
Kernel commands
 cEF328.f
Compound commands
 cefSIM328.f
Simulator
Compiling to RAM Memory




ATmega328 has only 2 KB of RAM memory.
Only small applications can be compiled.
The application code cannot be saved to the
flash memory.
It is still very useful with the PEEK and POKE
capability.
It is a very good tool to learn about the
ATmega328 Microcontroller.
Demonstrations




Compile ceForth328.pde under Arduino
0022 system.
Upload ceForth328 to Arduino
HyperTerminal interaction
Demonstrations



Blink LED
Tone generator
Servo motors