A lightweight VM which allows for easy custom programs to be made on the device without having to worry about linking and PIC code and all of that. BSD licensed. See https://bitbucket.org/earlz/lightvm/ for the upstream general purpose repository

Dependents:   MbedConsole

Files at this revision

API Documentation at this revision

Comitter:
earlz
Date:
Fri Apr 12 04:12:59 2013 +0000
Commit message:
Initial import. Upstream changeset 29:8dd470477c0b / tip

Changed in this revision

16bitops.c Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
lightvm.c Show annotated file Show diff for this revision Revisions of this file
lightvm.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r dac6a7d43ed2 16bitops.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/16bitops.c	Fri Apr 12 04:12:59 2013 +0000
@@ -0,0 +1,95 @@
+/**
+<Copyright Header>
+Copyright (c) 2013 Jordan "Earlz" Earls  <http://Earlz.net>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+  derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+</Copyright Header>
+**/
+
+#include "lightvm.h"
+
+void op16_single_arg(lightvm_state* s, uint16_t op, uint16_t* arg2)
+{
+    unsigned int secondary=op & 0xF000 >> 12;
+    switch(secondary)
+    {
+        case 0:
+        //push
+        lightvm_push(s, *arg2);
+        break;
+        case 1:
+        //pop
+        *arg2 = lightvm_pop(s);
+        break;
+        case 2:
+        //not
+        *arg2 = ~(*arg2);
+        break;
+        case 3:
+        //unused
+        case 4:
+        //unused
+        case 5:
+        //unused
+        case 6:
+        //xcall?
+        case 7:
+        //and.eq
+        *s->tr &= *s->cr == *arg2;
+        break;
+        case 8:
+        //or.eq
+        *s->tr |= *s->cr == *arg2;
+        break;
+        case 9:
+        //and.neq
+        *s->tr &= *s->cr != *arg2;
+        break;
+        case 10:
+        //or.neq
+        *s->tr |= *s->cr != *arg2;
+        break;
+        case 11:
+        //set TR
+        *s->tr = 1;
+        break;
+        case 12:
+        //reset TR
+        *s->tr = 0;
+        break;
+        case 13:
+        //load CR
+        *s->cr = *arg2;
+        break;
+        case 14:
+        //8.bank PROBABLY NOT
+        case 15:
+        //unused
+        default:
+        return;
+    }
+    return;
+}
+
diff -r 000000000000 -r dac6a7d43ed2 config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Fri Apr 12 04:12:59 2013 +0000
@@ -0,0 +1,71 @@
+
+/**
+<Copyright Header>
+Copyright (c) 2013 Jordan "Earlz" Earls  <http://Earlz.net>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+  derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+</Copyright Header>
+*/
+
+#ifndef LIGHTVM_CONFIG_H
+#define LIGHTVM_CONFIG_H
+
+//configuration directives
+
+/*
+  The optimal opcache size in bytes. 
+  This is the maximum amount of bytes that can possibly be read in a single operation
+  
+*/
+//#define OPTIMAL_OPCACHE_SIZE 4 
+
+
+
+//options
+
+/*
+  Use this to ensure that exactly 16-bit registers are used
+*/
+//#define EXACT_REGISTERS
+/*
+  Use this to give the greater world the middle finger and use
+  optimizations that will make the VM environment non-portable.
+  Includes making registers take up exactly 32 bits, rather than 64
+*/
+//#define SCREW_PORTABILITY
+
+//#define ALIGN_OPCACHE
+
+//#define USE_OPCACHE
+
+//Use this to specify that string "plain text" error messages are populated as well as an error code when something goes wrong
+#define INCLUDE_ERROR_MESSAGES
+
+//Specifies IP should be validated to be within our memory block.
+//This makes the VM "safer", but also slower
+//#define VALIDATE_IP_WITHIN_MEMORY
+
+#endif
+
diff -r 000000000000 -r dac6a7d43ed2 lightvm.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lightvm.c	Fri Apr 12 04:12:59 2013 +0000
@@ -0,0 +1,331 @@
+/**
+<Copyright Header>
+Copyright (c) 2013 Jordan "Earlz" Earls  <http://Earlz.net>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+  derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+</Copyright Header>
+**/
+
+#include "lightvm.h"
+#include "stdio.h"
+
+int lightvm_init(lightvm_state* state)
+{
+    if(state->memory==NULL || state->memorysize<64)
+    {
+        return -1;
+    }
+    for(int i=0;i<15;i++)
+    {
+        *lightvm_register(state, i)=0;
+    }
+    state->ip=lightvm_register(state, REGISTER_IP);
+    state->sp=lightvm_register(state, REGISTER_SP);
+    state->tr=lightvm_register(state, REGISTER_TR);
+    state->cr=lightvm_register(state, REGISTER_CR);
+    *state->ip=64; //set to end of register block
+    *state->sp=state->memorysize; //set to end of memory(stack grows downwards)
+   // state->opcache=malloc(sizeof(uint_opcache_t));
+   // state->cachesize=sizeof(uint_fast16_t);
+ 
+    return 0;
+}
+
+int lightvm_destroy(lightvm_state* state)
+{
+    //free(state->opcache);
+    return 0;
+}
+
+uint16_t* memory_word(lightvm_state* s, reg16_t address)
+{
+    return (uint16_t*)&s->memory[address];   
+}
+uint8_t* memory_byte(lightvm_state* s, reg16_t address)
+{
+    return (uint8_t*)&s->memory[address];
+}
+
+reg16_t *lightvm_register(lightvm_state* state, unsigned int reg)
+{
+    if(reg>15)
+    {
+        return NULL;
+    }
+    return (reg16_t*) memory_word(state, reg<<2); //shortcut for reg*4
+}
+
+uint8_t lightvm_next_op_byte(lightvm_state* s)
+{
+    uint8_t tmp=*memory_byte(s, *s->ip);
+    (*s->ip)++;
+    return tmp;
+}
+uint16_t lightvm_next_op_word(lightvm_state* s)
+{
+    uint16_t tmp=*memory_word(s, *s->ip);
+    (*s->ip)+=2;
+    return tmp;
+}
+
+void lightvm_set_error(lightvm_state* s, lightvm_error_t err)
+{
+    s->errorcode=err;
+    #ifdef INCLUDE_ERROR_MESSAGES
+    switch(err)
+    {
+        case None:
+        s->error="No error has occured!?";
+        break;
+        case InvalidOpcode:
+        s->error="Invalid Opcode";
+        break;
+        default:
+        s->error="Unknown error";
+        break;
+    }
+    #endif
+}
+
+int lightvm_step(lightvm_state* s)
+{
+    uint16_t op=lightvm_next_op_word(s);
+    unsigned int primary = (op&0x00FF) >> 8;
+    //bitfields sound nice, but not the most portable things to keep exact to a bit.
+    unsigned int _8bit=(primary&0x80) > 0;
+  //  unsigned int arg1_ptr = op&0x4000 > 0;
+  //  unsigned int arg2_ptr = op&0x2000 > 0;
+  //  unsigned int immediate = op&0x1000 > 0;
+    unsigned int second = op &0x00FF;
+    
+    //gather args
+    
+    
+    if(_8bit && primary>=12 && primary<=15)
+    {
+        //is branch.short
+        return branch_short(s, primary, second);
+    }
+    if(_8bit)
+    {
+    
+    }
+    else
+    {
+        uint16_t *arg1=parse_16bit_arg1(s, op);
+        uint16_t *arg2=parse_16bit_arg2(s, op);
+        lightvm_step_16bit(s, op, arg1, arg2);
+    }
+    return 0;
+}
+
+uint16_t *parse_16bit_arg1(lightvm_state* s, uint16_t op)
+{
+    unsigned int arg1_ptr = (op&0x0040) > 0;
+    unsigned int reg=(op & 0xF000) >> 12;
+    if(arg1_ptr)
+    {
+        return memory_word(s, *lightvm_register(s, reg));
+    }
+    return (uint16_t*)lightvm_register(s, reg);
+}
+uint16_t *parse_16bit_arg2(lightvm_state* s, uint16_t op)
+{
+    unsigned int arg2_ptr = (op&0x0020) > 0;
+    unsigned int isimmediate = (op&0x0010) > 0;
+    unsigned int reg=(op& 0x0F00) >> 8;
+    uint16_t* tmp;
+    if(isimmediate)
+    {
+        uint16_t immd=lightvm_next_op_word(s);
+        s->immdreg=immd;
+        tmp=(uint16_t*)&s->immdreg;
+    }
+    else
+    {
+        tmp=(uint16_t*)lightvm_register(s, reg);
+    }
+    
+    if(arg2_ptr)
+    {
+        return memory_word(s, *tmp);
+    }
+    return tmp;
+}
+
+void lightvm_push(lightvm_state* s, uint16_t value)
+{
+    (*s->sp)-=2;
+    *memory_word(s, *s->sp) = value;
+}
+uint16_t lightvm_pop(lightvm_state* s)
+{
+    uint16_t tmp = *memory_word(s, *s->sp);
+    (*s->sp)+=2;
+    return tmp;
+}
+
+uint16_t lightvm_peek(lightvm_state *s)
+{
+    uint16_t tmp = *memory_word(s, *s->sp);
+    return tmp;
+}
+
+int lightvm_step_8bit(lightvm_state* s, uint8_t* arg1, uint8_t* arg2)
+{
+    return 0;
+}
+
+void lightvm_dump(lightvm_state *s) //for when test-wat isn't enough
+{
+    for(int i=0;i<16;i++)
+    {
+        reg16_t tmp=*lightvm_register(s, i);
+        printf("r%i = %X\n", i, tmp);
+    }
+}
+
+
+int lightvm_step_16bit(lightvm_state* s, uint16_t op, uint16_t* arg1, uint16_t* arg2)
+{
+    unsigned int primary=op & 0x0F00 >> 8;
+    switch(primary)
+    {
+        case 0:
+        //1-arg ops
+            op16_single_arg(s, op, arg2);
+            break;
+        case 1:
+        //xor
+            *arg1=(*arg1) ^ (*arg2);
+            break;
+        case 2:
+        //or
+            *arg1=(*arg1) | (*arg2);
+            break;
+        case 3:
+        //and
+            *arg1=(*arg1) & (*arg2);
+            break;
+        case 4:
+        //unused
+            break;
+        case 5:
+        //mv
+            *arg1=*arg2;
+            break;
+        case 6:
+        //comparison 1-arg
+            break;
+        case 7:
+        //mv.iftrue
+            if(*s->tr)
+            {
+                *arg1=*arg2;
+            }
+            break;
+        case 8:
+        //shl
+            *arg1 = (*arg1) << (*arg2);
+            break;
+        case 9:
+        //shr
+            *arg1 = (*arg1) >> (*arg2);
+            break;
+        case 10:
+        //add
+            *arg1 = (*arg1) + (*arg2);
+            break;
+        case 11:
+        //sub
+            *arg1 = (*arg1) + (*arg2);
+            break;
+        case 12:
+        //push.mv
+            lightvm_push(s, *arg1);
+            *arg1 = *arg2;
+            break;
+        case 13:
+        //push.mv.ifture
+            if(*s->tr)
+            {
+                lightvm_push(s, *arg1);
+                *arg1 = *arg2;
+            }
+            break;
+        case 14:
+        //push.mv.iffalse
+            if(!(*s->tr))
+            {
+                lightvm_push(s, *arg1);
+                *arg1 = *arg2;
+            }
+        case 15:
+        //extensions/unused
+            if((op & 0x00FF) == 0)
+            {
+                lightvm_dump(s);
+            }
+        default:
+        return -1; //shouldn't reach here
+    }
+    return 0;
+}
+
+int branch_short(lightvm_state* s, unsigned int primary, uint8_t arg)
+{
+    unsigned int op = primary & 0x0F;
+    if(op==13 && !*s->tr)
+    {
+        return 0;
+    }
+    if(op==14 && *s->tr)
+    {
+        return 0;
+    }
+    //you can never be too explicit when mixing signed and unsigned values
+    int16_t diff = (int8_t)arg; //make it so it can go +/-256 instead of 128
+    diff=diff << 1;
+    int16_t ip=*s->ip;
+    ip=ip+diff;
+    *s->ip=*(uint16_t*)&ip; //make sure not to "cast" the int16 to uint16. Just copy direct. Assume C compiler is 2's complement
+    return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff -r 000000000000 -r dac6a7d43ed2 lightvm.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lightvm.h	Fri Apr 12 04:12:59 2013 +0000
@@ -0,0 +1,150 @@
+/**
+<Copyright Header>
+Copyright (c) 2013 Jordan "Earlz" Earls  <http://Earlz.net>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+  derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+</Copyright Header>
+**/
+
+#ifndef LIGHTVM_H
+#define LIGHTVM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//standard includes required
+
+#include <stdint.h>
+#include <stdlib.h>
+
+//project specific includes
+
+#include "config.h"
+
+//"convenience" typedefs
+#if (__INT_FAST16_MAX__ > 0xFFFFFFFF) //if it'd take it to 64 bit, manually use next best thing: 32bit
+typedef uint32_t reg16_t;
+#else
+typedef uint_least16_t reg16_t;
+#endif
+
+#ifdef USE_OPCACHE
+typedef 
+#if OPTIMAL_OPCACHE_SIZE==1
+uint8_t
+#elif OPTIMAL_OPCACHE_SIZE==2
+uint16_t
+#elif OPTIMAL_OPCACHE_SIZE==4
+uint32_t
+#else
+#error "OPTIMAL_OPCACHE_SIZE must be 1, 2, or 4"
+#endif
+uint_opcache_t;
+#endif
+
+
+
+
+//constants
+#define REGISTER_IP 15
+#define REGISTER_SP 14
+#define REGISTER_TR 13
+#define REGISTER_CR 12
+
+//enumerations
+
+typedef enum 
+{
+    None=0,
+    InvalidOpcode=1
+    
+}lightvm_error_t;
+
+//structures
+
+typedef struct
+{
+    uint_fast16_t begin, end; //beginning and ending addresses of memory
+    void *memory;
+    void *next;
+}extended_memory_node;
+
+typedef struct
+{
+    uint8_t *memory; //primary memory
+    uint_fast16_t memorysize; //primary memory size
+    
+    reg16_t *ip;
+    reg16_t *sp;
+    reg16_t *tr;
+    reg16_t *cr;
+    reg16_t immdreg; //temporary invisible register
+    
+    #ifdef INCLUDE_ERROR_MESSAGES
+    const char *error;
+    #endif
+    lightvm_error_t errorcode;
+    extended_memory_node* extended;
+}lightvm_state;
+
+
+typedef struct
+{
+    unsigned char byteops:1;
+    unsigned char arg1_indirect:1;
+    unsigned char arg2_indirect:1;
+    unsigned char arg_immediate:1;
+    unsigned char op:4;
+
+}primary_opcode_t;
+
+
+//prototypes
+
+int lightvm_init(lightvm_state* state);
+reg16_t *lightvm_register(lightvm_state* state, unsigned int reg);
+uint8_t lightvm_next_op_byte(lightvm_state* s);
+uint16_t lightvm_next_op_word(lightvm_state* s);
+
+uint16_t *parse_16bit_arg1(lightvm_state* s, uint16_t opcode);
+uint16_t *parse_16bit_arg2(lightvm_state* s, uint16_t opcode);
+int lightvm_step_8bit(lightvm_state* s, uint8_t* arg1, uint8_t* arg2);
+int lightvm_step_16bit(lightvm_state* s, uint16_t op, uint16_t* arg1, uint16_t* arg2);
+int branch_short(lightvm_state* s, unsigned int primary, uint8_t arg);
+
+void lightvm_push(lightvm_state* s, uint16_t value);
+uint16_t lightvm_pop(lightvm_state* s);
+uint16_t lightvm_peek(lightvm_state *s);
+
+//opcodes
+
+void op16_single_arg(lightvm_state* s, uint16_t op, uint16_t* arg2);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif