Simple graphics language

From ScienceZero
Revision as of 06:21, 19 April 2011 by Bjoern (Talk | contribs) (Commands)

Jump to: navigation, search

A simple graphics language for microcontrollers

We start with a very simple reverse polish notation interpreter with a preprocessor stage

Sample code:

to circle 360 for 1 forward 1 right next end

Registers:

0 X (nn.ff)
1 Y
2 Angle
3 Scale
4 Colour
5 Pen up/down
6 Nested If counter
7

Stack diagrams:

( things read from the stack -- things written on the stack )
x - x position
y - y position
n - number
Example:
  ( n n -- n )
  This means that two numbers were read from the stack, then one number was written back.
  For example a simple addition reads two numbers and writes back the result.

Commands

Define function:
  To < function name > < code > End
 
Absolute commands
  _             ( -- n )( Special opcodes used by the compiler )
  setcolour     ( colour -- )
  readcolour    ( -- colour )
  setpos        ( x y -- )
  readpos       ( -- x y )
  load          ( address -- data )
  store         ( data address -- )

Relative commands:
  penup         ( -- )
  pendown       ( -- )
  forward       ( length -- )
  backward      ( length -- )
  left          ( angle -- )
  right         ( angle -- )
 
Loops:
  for           ( n -- )
  next          ( -- )
  do            ( -- )
  again         ( -- )

Conditionals:
  if            ( n -- )
  else
  endif

Data processing:
  +             ( n n -- n )        ( to +  >r 0 r> - - end )
  -             ( n n -- n )        ( fundamental )
  *             ( n n -- n )
  /             ( n n -- n )
  lsl           ( n n -- n )        ( to lsl for dup + next end )
  lsr           ( n n -- n )
  and           ( n n -- n )        ( fundamental )
  or            ( n n -- n )        ( to or over not and + end )
  eor           ( n n -- n )        ( to eor over over or -rot and not and end )
  not           ( n -- n )          ( to not >r -1 r> - end )

Stack manipulation:
 drop           ( n -- )           ( to drop if endif end )
 dup            ( n -- n n )       ( fundamental )
 swap           ( a b -- b a )     ( to swap dup >r >r dup r> eor dup >r eor r> r> eor end )
 over           ( a b -- a b a )   ( to over >r dup r> swap end ) 
 rot            ( a b c -- b c a ) ( to rot >r swap r> swap end ) 
 -rot           ( a b c -- c a b ) ( to -rot swap >r swap r> end )
 >r             ( n -- )           ( fundamental )
 r>             ( -- n )           ( fundamental )

Optimal stack reordering in Forth

The dictionary

The dictionary used to convert a command into an opcode. It is a string like "SetcolourReadcolourSetposReadpos" that is searched linearly from start to end. Any character that is not a lower case "a-z" marks the start of a new word. The first compiler pass adds all functions to the end of this table.

The function jump table

This table contains the address of each function, in the same order as the functions appear in the dictionary. It is filled in during the second pass when the code position is known.

Interpreter

do
  read opcode
  if number then add number to the data stack
  if hardwired command then execute
  if function then look up the address in the function jump table and call it
again
Opcodes:
0 load 0
1 load next byte on to the stack
2 load the next two bytes
3 load the next three bytes
4 load the next four bytes
5-15 reserved
16-255 commands