ceforth for arduino 2012 fig taiwan conference february 16, 2012 chen-hanson ting

18
ceForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Upload: kerry-nichols

Post on 17-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

ceForth for Arduino

2012 FIG Taiwan Conference

February 16, 2012Chen-Hanson Ting

Page 2: 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

Page 3: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 4: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Arduino Uno with AVRISP mkll

Page 5: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Arduino Uno Alone

Page 6: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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?

Page 7: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 8: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 9: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 10: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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; }

Finite State Machine

Page 11: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 12: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Primitive Commandsvoid drop(void) { pop; } void dup(void) { *++S = top; } void swap(void) { w = top; top = *S; *S = w; } void over(void) { push S[-1]; } void zless(void) { top = (top & 0X8000) LOGICAL ; } void andd(void) { top &= *S--; } void orr(void) { top |= *S--; } void xorr(void) { top ^= *S--; } void uplus(void) { *S += top; top = LOWER(*S,

top) ; } void nop(void) { jump(); }

Page 13: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Compound Commands

Compound commands are produced by ceForth_328 metacompiler as lists of execution addresses.

Page 14: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 15: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

ceForth_328 Memory Space

0000-00FF ATmega328 registers 0100-02FF Data space assigned by C 0300-031F FORTH variables 0320-087F Free space for dictionary 0880-08FF TIB/ATmega328 stack 0900-1FFF Dictionary in flash

memory

Page 16: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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

Page 17: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

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.

Page 18: CeForth for Arduino 2012 FIG Taiwan Conference February 16, 2012 Chen-Hanson Ting

Demonstrations Compile ceForth328.pde under

Arduino 0022 system. Upload ceForth328 to Arduino HyperTerminal interaction Demonstrations

Blink LED Tone generator Servo motors