Difference between revisions of "Simple graphics language"
From ScienceZero
(→A simple graphics language for microcontrollers) |
(→Commands) |
||
Line 71: | Line 71: | ||
Stack manipulation: | Stack manipulation: | ||
− | |||
drop ( n -- ) | drop ( n -- ) | ||
+ | dup ( n -- n n ) | ||
+ | swap ( a b -- b a ) | ||
over ( a b -- a b a ) | over ( a b -- a b a ) | ||
− | rot ( a b c -- c b a ) | + | rot ( a b c -- b c a ) |
+ | -rot ( a b c -- c a b ) | ||
>r ( n -- ) | >r ( n -- ) | ||
r> ( -- n ) | r> ( -- n ) |
Revision as of 22:35, 18 April 2011
Contents
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 setcolour ( colour -- ) readcolour ( -- colour ) setpos ( x y -- ) readpos ( -- x y ) load ( address -- data ) store ( data address -- ) _ ( -- n )( Special opcodes used by the compiler ) 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 ) - ( n n -- n ) * ( n n -- n ) / ( n n -- n ) lsl ( n n -- n ) lsr ( n n -- n ) and ( n n -- n ) or ( n n -- n ) eor ( n n -- n ) not ( n -- n ) Stack manipulation: drop ( n -- ) dup ( n -- n n ) swap ( a b -- b a ) over ( a b -- a b a ) rot ( a b c -- b c a ) -rot ( a b c -- c a b ) >r ( n -- ) r> ( -- n )
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