Difference between revisions of "Virtual machine"

From ScienceZero
Jump to: navigation, search
(Machine instructions)
(Machine instructions)
Line 85: Line 85:
 
             D *RESERVED*   
 
             D *RESERVED*   
 
             E *RESERVED*   
 
             E *RESERVED*   
             F *RESERVED*  
+
             F *RESERVED*
 +
   
 
     D OPR2 #imm4
 
     D OPR2 #imm4
 
             0 ADD          - Addition                ( n n -- n )
 
             0 ADD          - Addition                ( n n -- n )
Line 103: Line 104:
 
             E INC          - Increase                ( n -- n )
 
             E INC          - Increase                ( n -- n )
 
             F DEC          - Decrease                ( n -- n )
 
             F DEC          - Decrease                ( n -- n )
 +
 
     E OPR3 #imm4
 
     E OPR3 #imm4
 
             0 DUP          - Duplicate top of stack                              ( a -- a a )
 
             0 DUP          - Duplicate top of stack                              ( a -- a a )
Line 110: Line 112:
 
             4 ROT          - Rotate the top 3 items                              ( a b c -- b c a )
 
             4 ROT          - Rotate the top 3 items                              ( a b c -- b c a )
 
             5 -ROT          - Rotate the top 3 items the other way                ( a b c -- c a b )
 
             5 -ROT          - Rotate the top 3 items the other way                ( a b c -- c a b )
             6 R            - Move top of data stack to the return stack
+
             6 R            - Move the top of the return stack to the data stack ( r -- n )
             7 >R            - Move the top of the return stack to the data stack
+
             7 >R            - Move top of data stack to the return stack         ( n -- r )
             8 @R            - Copy the top of the return stack to the data stack
+
             8 @R            - Copy the top of the return stack to the data stack ( -- n )
 
             9 LD32          - Load a 32 bit value from memory                    ( address -- data )
 
             9 LD32          - Load a 32 bit value from memory                    ( address -- data )
 
             A ST32          - Store a 32 bit value to memory                      ( data address -- )
 
             A ST32          - Store a 32 bit value to memory                      ( data address -- )
Line 120: Line 122:
 
             E ST8          - Store a 8 bit value to memory                      ( data address -- )
 
             E ST8          - Store a 8 bit value to memory                      ( data address -- )
 
             F NOP          - No-operation.                                      ( -- )
 
             F NOP          - No-operation.                                      ( -- )
 +
 
     F OPR4 #imm4
 
     F OPR4 #imm4
             0 FOR          - Start a loop of a fixed number of iterations ( n -- r r )
+
             0 FOR          - Start a loop of a fixed number of iterations       ( n -- r r )
             1 NEXT          - Decrease loop counter and exit if 0         ( r r -- r r | r r -- )
+
             1 NEXT          - Decrease loop counter and exit if 0                 ( r r -- r r | r r -- )
             2 DO            - Start a loop                                 ( -- r )
+
             2 DO            - Start a loop                                       ( -- r )
             3 WHILE         
+
             3 WHILE        - If not 0 then loop                                  ( n -- )
             4 UNTIL
+
             4 UNTIL         - if 0 then loop                                      ( n -- )
             5 AGAIN        - Loop                                         ( r -- )
+
             5 AGAIN        - Loop                                               ( r -- )
             6 RP            - Read return pointer                         ( rp -- address )
+
             6 RP            - Read return pointer                                 ( rp -- address )
             7 >RP          - Write return pointer                         ( address -- rp )
+
             7 >RP          - Write return pointer                               ( address -- rp )
             8 FLAG          - No-operation for 0, else return -1           ( a -- flag )  
+
             8 FLAG          - No-operation for 0, else return -1                 ( n -- flag )  
             9 NFLAG        - Not Flag, (if 0 else -1 endif)
+
             9 NFLAG        - Not Flag, (if 0 else -1 endif)                      ( n -- /flag )
             A IF            - If false then skip until ELSE or ENDIF       ( n -- )
+
             A IF            - If false then skip until ELSE or ENDIF             ( n -- )
             B ELSE          - Skip until ENDIF on the same level
+
             B ELSE          - Skip until ENDIF on the same level                 ( -- )
             C ENDIF        - No-operation
+
             C ENDIF        - No-operation                                       ( -- )
             D JUMP          - Jump to address TOP                     ( address -- )
+
             D JUMP          - Jump to address TOP                                 ( address -- )
             E CALL          - Calls a function at address TOP         ( address -- r r )
+
             E CALL          - Calls a function at address TOP                     ( address -- r r )
             F RETURN        - Return from function                   ( r r -- )
+
             F RETURN        - Return from function                               ( r r -- )
  
 
==Function call==
 
==Function call==

Revision as of 09:44, 11 April 2014

Functions

 :  Define a new function
 ;  End definition
 (  Start comment
 )  End comment

 Example, a function called inc that increases a value by 1
   : inc 1 add ;

 Write numbers directly without using LDC/LDN/LDE since they are implied
 After being defined a function can be used just like any other instruction


Stacks

The virtual machine has two stacks, one for data and one for return addresses and variables. When writing down the stack on one line, the top of the stack is to the right on the line.

A stack with four items, 7 is the topmost item.
 1 4 2 7 

Performing an ADD instruction will result in this stack:
 1 4 9

2 and 7 are discarded and the sum 9 is written back to the stack.

Stack diagrams

 A stack diagram shows how many items a function/instruction take from the stack and put back
 Show what is taken off the stack then what is put back on the stack separated by --
 n n -- n

 Example, a function called mul that takes two numbers off the stack and writes one number back
   : mul ( n n -- n ) 0 rot for over add next nip ;

 Use n for general numbers but name the items if it makes sense
 For example a function that calculates power from voltage and current
   : power ( volt ampere -- watt ) mul ;

Registers

The registers and stack cells are 32 bit each, the registers are internal to the virtual machine The registers will be put on the return stach when a function is called so the machine can return safely and continue where it left off.


Registers

 PC Program counter        - Points to the next instruction
 DP Data stack pointer     - Points to the next free cell on the data stack
 RP Return stack pointer   - Points to the next free cell on the return stack
 LP Local variable pointer - Points to the first local variable or local array element on the return stack
                             This is also used when unwinding a stack frame

Machine instructions

Each instruction is one byte (8 bits). The 4 most significant bits is the type of instruction and the 4 least significant bits is the data.

 7      0
 ttttdddd

When viewed in hexadecimal the first digit is the type and the second is the data. So the byte 0xDA is the OR instruction

Type     Data
   0 LDC #imm4             - Load 4 bit immediate constant
   1 LDN #imm4             - Load 4 bit immediate negative constant
   2 LDE #imm4             - Load extend as TOP = TOP:imm4
   3 LSL #imm4             - Logical shift left by n + 1
   4 DIM #imm4             - Reserve n + 1 elements on the return stack
   5 LDL #imm4             - Load local variable from the return stack
   6 STL #imm4             - Store local variable on the return stack
   7 SYS #'aaaa'           - Call system function as TOP:imm4
   8 LEA #'aaaa'           - Load effective address as TOP:imm4
   9 JUMP #'aaaa'          - Jump as TOP:imm4
   A CALL #'aaaa'          - Call function as TOP:imm4
   B OPR0 *RESERVED*       - Reserved for future expansion
   C OPR1 #imm4
           0 ADD.          - 32 bit IEEE 754 floating-point operation
           1 SUB.
           2 MUL.
           3 DIV.
           4 SQRT.
           5 TOF.          - Convert from integer to floating-point
           6 TOI.          - Convert from floating-point to integer 
           7 *RESERVED*    - Reserved for future expansion of floating-point
           8 *RESERVED*   
           9 *RESERVED*   
           A *RESERVED*   
           B *RESERVED*   
           C *RESERVED*   
           D *RESERVED*   
           E *RESERVED*   
           F *RESERVED*
   
   D OPR2 #imm4
           0 ADD           - Addition                 ( n n -- n )
           1 SUB           - Subtraction              ( n n -- n )
           2 MUL           - Multiplication           ( n n -- n )
           3 DIV           - Unsigned division        ( n n -- n )
           4 SDIV          - Signed division          ( n n -- n )  
           5 LSL           - Logical shift left       ( n n -- n )
           6 LSR           - Logical shift right      ( n n -- n )
           7 ASR           - Arithmetic shift right   ( n n -- n )
           8 ROR           - Rotate right             ( n n -- n )
           9 AND           - Bitwise AND              ( n n -- n )
           A OR            - Bitwise OR               ( n n -- n )
           B EOR           - Bitwise Exclusive OR     ( n n -- n )
           C NOT           - Bitvise invert           ( n -- n )
           D NEG           - Negative                 ( n -- n )
           E INC           - Increase                 ( n -- n )
           F DEC           - Decrease                 ( n -- n )

   E OPR3 #imm4
           0 DUP           - Duplicate top of stack                              ( a -- a a )
           1 DROP          - Drop top of stack                                   ( a b -- a )
           2 SWAP          - Swap the two topmost items                          ( a b -- b a )
           3 OVER          - Copy the second item                                ( a b -- a b a )
           4 ROT           - Rotate the top 3 items                              ( a b c -- b c a )
           5 -ROT          - Rotate the top 3 items the other way                ( a b c -- c a b )
           6 R             - Move the top of the return stack to the data stack  ( r -- n )
           7 >R            - Move top of data stack to the return stack          ( n -- r )
           8 @R            - Copy the top of the return stack to the data stack  ( -- n )
           9 LD32          - Load a 32 bit value from memory                     ( address -- data )
           A ST32          - Store a 32 bit value to memory                      ( data address -- )
           B LD16          - Load a 16 bit value from memory                     ( address -- data )
           C ST16          - Store a 16 bit value to memory                      ( data address -- )
           D LD8           - Load a 8 bit value from memory                      ( address -- data )
           E ST8           - Store a 8 bit value to memory                       ( data address -- )
           F NOP           - No-operation.                                       ( -- )

   F OPR4 #imm4
           0 FOR           - Start a loop of a fixed number of iterations        ( n -- r r )
           1 NEXT          - Decrease loop counter and exit if 0                 ( r r -- r r | r r -- )
           2 DO            - Start a loop                                        ( -- r )
           3 WHILE         - If not 0 then loop                                  ( n -- )
           4 UNTIL         - if 0 then loop                                      ( n -- )
           5 AGAIN         - Loop                                                ( r -- )
           6 RP            - Read return pointer                                 ( rp -- address )
           7 >RP           - Write return pointer                                ( address -- rp )
           8 FLAG          - No-operation for 0, else return -1                  ( n -- flag ) 
           9 NFLAG         - Not Flag, (if 0 else -1 endif)                      ( n -- /flag )
           A IF            - If false then skip until ELSE or ENDIF              ( n -- )
           B ELSE          - Skip until ENDIF on the same level                  ( -- )
           C ENDIF         - No-operation                                        ( -- )
           D JUMP          - Jump to address TOP                                 ( address -- )
           E CALL          - Calls a function at address TOP                     ( address -- r r )
           F RETURN        - Return from function                                ( r r -- )

Function call

Function call stack frame
Showing the top of the return stack after a function call is taken, before the function has executed any instructions
          V0      V1      V2  <--  Local variables on the return stack accessed with the LDL and STL instructions
  PC LP <empty> <empty> <empty> ...
            \    
             \     
              The new LP points to the first local variable for the function
              RP points to the first free cell
  
  DIM #2 will cause the next two cells to be filled with 0, RETURN will delete any variables and return to after the function call
          V0      V1      V2 
  PC LP    0       0    <empty> ...
            \               \                
             \               RP is advanced by two to the first free cell
              LP still points to the same position but the two first variables now contains 0