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

Committer:
earlz
Date:
Fri Apr 12 04:12:59 2013 +0000
Revision:
0:dac6a7d43ed2
Initial import. Upstream changeset 29:8dd470477c0b / tip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
earlz 0:dac6a7d43ed2 1 /**
earlz 0:dac6a7d43ed2 2 <Copyright Header>
earlz 0:dac6a7d43ed2 3 Copyright (c) 2013 Jordan "Earlz" Earls <http://Earlz.net>
earlz 0:dac6a7d43ed2 4 All rights reserved.
earlz 0:dac6a7d43ed2 5
earlz 0:dac6a7d43ed2 6 Redistribution and use in source and binary forms, with or without
earlz 0:dac6a7d43ed2 7 modification, are permitted provided that the following conditions
earlz 0:dac6a7d43ed2 8 are met:
earlz 0:dac6a7d43ed2 9
earlz 0:dac6a7d43ed2 10 1. Redistributions of source code must retain the above copyright
earlz 0:dac6a7d43ed2 11 notice, this list of conditions and the following disclaimer.
earlz 0:dac6a7d43ed2 12 2. Redistributions in binary form must reproduce the above copyright
earlz 0:dac6a7d43ed2 13 notice, this list of conditions and the following disclaimer in the
earlz 0:dac6a7d43ed2 14 documentation and/or other materials provided with the distribution.
earlz 0:dac6a7d43ed2 15 3. The name of the author may not be used to endorse or promote products
earlz 0:dac6a7d43ed2 16 derived from this software without specific prior written permission.
earlz 0:dac6a7d43ed2 17
earlz 0:dac6a7d43ed2 18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
earlz 0:dac6a7d43ed2 19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
earlz 0:dac6a7d43ed2 20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
earlz 0:dac6a7d43ed2 21 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
earlz 0:dac6a7d43ed2 22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
earlz 0:dac6a7d43ed2 23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
earlz 0:dac6a7d43ed2 24 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
earlz 0:dac6a7d43ed2 25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
earlz 0:dac6a7d43ed2 26 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
earlz 0:dac6a7d43ed2 27 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
earlz 0:dac6a7d43ed2 28 </Copyright Header>
earlz 0:dac6a7d43ed2 29 **/
earlz 0:dac6a7d43ed2 30
earlz 0:dac6a7d43ed2 31 #ifndef LIGHTVM_H
earlz 0:dac6a7d43ed2 32 #define LIGHTVM_H
earlz 0:dac6a7d43ed2 33
earlz 0:dac6a7d43ed2 34 #ifdef __cplusplus
earlz 0:dac6a7d43ed2 35 extern "C" {
earlz 0:dac6a7d43ed2 36 #endif
earlz 0:dac6a7d43ed2 37
earlz 0:dac6a7d43ed2 38 //standard includes required
earlz 0:dac6a7d43ed2 39
earlz 0:dac6a7d43ed2 40 #include <stdint.h>
earlz 0:dac6a7d43ed2 41 #include <stdlib.h>
earlz 0:dac6a7d43ed2 42
earlz 0:dac6a7d43ed2 43 //project specific includes
earlz 0:dac6a7d43ed2 44
earlz 0:dac6a7d43ed2 45 #include "config.h"
earlz 0:dac6a7d43ed2 46
earlz 0:dac6a7d43ed2 47 //"convenience" typedefs
earlz 0:dac6a7d43ed2 48 #if (__INT_FAST16_MAX__ > 0xFFFFFFFF) //if it'd take it to 64 bit, manually use next best thing: 32bit
earlz 0:dac6a7d43ed2 49 typedef uint32_t reg16_t;
earlz 0:dac6a7d43ed2 50 #else
earlz 0:dac6a7d43ed2 51 typedef uint_least16_t reg16_t;
earlz 0:dac6a7d43ed2 52 #endif
earlz 0:dac6a7d43ed2 53
earlz 0:dac6a7d43ed2 54 #ifdef USE_OPCACHE
earlz 0:dac6a7d43ed2 55 typedef
earlz 0:dac6a7d43ed2 56 #if OPTIMAL_OPCACHE_SIZE==1
earlz 0:dac6a7d43ed2 57 uint8_t
earlz 0:dac6a7d43ed2 58 #elif OPTIMAL_OPCACHE_SIZE==2
earlz 0:dac6a7d43ed2 59 uint16_t
earlz 0:dac6a7d43ed2 60 #elif OPTIMAL_OPCACHE_SIZE==4
earlz 0:dac6a7d43ed2 61 uint32_t
earlz 0:dac6a7d43ed2 62 #else
earlz 0:dac6a7d43ed2 63 #error "OPTIMAL_OPCACHE_SIZE must be 1, 2, or 4"
earlz 0:dac6a7d43ed2 64 #endif
earlz 0:dac6a7d43ed2 65 uint_opcache_t;
earlz 0:dac6a7d43ed2 66 #endif
earlz 0:dac6a7d43ed2 67
earlz 0:dac6a7d43ed2 68
earlz 0:dac6a7d43ed2 69
earlz 0:dac6a7d43ed2 70
earlz 0:dac6a7d43ed2 71 //constants
earlz 0:dac6a7d43ed2 72 #define REGISTER_IP 15
earlz 0:dac6a7d43ed2 73 #define REGISTER_SP 14
earlz 0:dac6a7d43ed2 74 #define REGISTER_TR 13
earlz 0:dac6a7d43ed2 75 #define REGISTER_CR 12
earlz 0:dac6a7d43ed2 76
earlz 0:dac6a7d43ed2 77 //enumerations
earlz 0:dac6a7d43ed2 78
earlz 0:dac6a7d43ed2 79 typedef enum
earlz 0:dac6a7d43ed2 80 {
earlz 0:dac6a7d43ed2 81 None=0,
earlz 0:dac6a7d43ed2 82 InvalidOpcode=1
earlz 0:dac6a7d43ed2 83
earlz 0:dac6a7d43ed2 84 }lightvm_error_t;
earlz 0:dac6a7d43ed2 85
earlz 0:dac6a7d43ed2 86 //structures
earlz 0:dac6a7d43ed2 87
earlz 0:dac6a7d43ed2 88 typedef struct
earlz 0:dac6a7d43ed2 89 {
earlz 0:dac6a7d43ed2 90 uint_fast16_t begin, end; //beginning and ending addresses of memory
earlz 0:dac6a7d43ed2 91 void *memory;
earlz 0:dac6a7d43ed2 92 void *next;
earlz 0:dac6a7d43ed2 93 }extended_memory_node;
earlz 0:dac6a7d43ed2 94
earlz 0:dac6a7d43ed2 95 typedef struct
earlz 0:dac6a7d43ed2 96 {
earlz 0:dac6a7d43ed2 97 uint8_t *memory; //primary memory
earlz 0:dac6a7d43ed2 98 uint_fast16_t memorysize; //primary memory size
earlz 0:dac6a7d43ed2 99
earlz 0:dac6a7d43ed2 100 reg16_t *ip;
earlz 0:dac6a7d43ed2 101 reg16_t *sp;
earlz 0:dac6a7d43ed2 102 reg16_t *tr;
earlz 0:dac6a7d43ed2 103 reg16_t *cr;
earlz 0:dac6a7d43ed2 104 reg16_t immdreg; //temporary invisible register
earlz 0:dac6a7d43ed2 105
earlz 0:dac6a7d43ed2 106 #ifdef INCLUDE_ERROR_MESSAGES
earlz 0:dac6a7d43ed2 107 const char *error;
earlz 0:dac6a7d43ed2 108 #endif
earlz 0:dac6a7d43ed2 109 lightvm_error_t errorcode;
earlz 0:dac6a7d43ed2 110 extended_memory_node* extended;
earlz 0:dac6a7d43ed2 111 }lightvm_state;
earlz 0:dac6a7d43ed2 112
earlz 0:dac6a7d43ed2 113
earlz 0:dac6a7d43ed2 114 typedef struct
earlz 0:dac6a7d43ed2 115 {
earlz 0:dac6a7d43ed2 116 unsigned char byteops:1;
earlz 0:dac6a7d43ed2 117 unsigned char arg1_indirect:1;
earlz 0:dac6a7d43ed2 118 unsigned char arg2_indirect:1;
earlz 0:dac6a7d43ed2 119 unsigned char arg_immediate:1;
earlz 0:dac6a7d43ed2 120 unsigned char op:4;
earlz 0:dac6a7d43ed2 121
earlz 0:dac6a7d43ed2 122 }primary_opcode_t;
earlz 0:dac6a7d43ed2 123
earlz 0:dac6a7d43ed2 124
earlz 0:dac6a7d43ed2 125 //prototypes
earlz 0:dac6a7d43ed2 126
earlz 0:dac6a7d43ed2 127 int lightvm_init(lightvm_state* state);
earlz 0:dac6a7d43ed2 128 reg16_t *lightvm_register(lightvm_state* state, unsigned int reg);
earlz 0:dac6a7d43ed2 129 uint8_t lightvm_next_op_byte(lightvm_state* s);
earlz 0:dac6a7d43ed2 130 uint16_t lightvm_next_op_word(lightvm_state* s);
earlz 0:dac6a7d43ed2 131
earlz 0:dac6a7d43ed2 132 uint16_t *parse_16bit_arg1(lightvm_state* s, uint16_t opcode);
earlz 0:dac6a7d43ed2 133 uint16_t *parse_16bit_arg2(lightvm_state* s, uint16_t opcode);
earlz 0:dac6a7d43ed2 134 int lightvm_step_8bit(lightvm_state* s, uint8_t* arg1, uint8_t* arg2);
earlz 0:dac6a7d43ed2 135 int lightvm_step_16bit(lightvm_state* s, uint16_t op, uint16_t* arg1, uint16_t* arg2);
earlz 0:dac6a7d43ed2 136 int branch_short(lightvm_state* s, unsigned int primary, uint8_t arg);
earlz 0:dac6a7d43ed2 137
earlz 0:dac6a7d43ed2 138 void lightvm_push(lightvm_state* s, uint16_t value);
earlz 0:dac6a7d43ed2 139 uint16_t lightvm_pop(lightvm_state* s);
earlz 0:dac6a7d43ed2 140 uint16_t lightvm_peek(lightvm_state *s);
earlz 0:dac6a7d43ed2 141
earlz 0:dac6a7d43ed2 142 //opcodes
earlz 0:dac6a7d43ed2 143
earlz 0:dac6a7d43ed2 144 void op16_single_arg(lightvm_state* s, uint16_t op, uint16_t* arg2);
earlz 0:dac6a7d43ed2 145
earlz 0:dac6a7d43ed2 146 #ifdef __cplusplus
earlz 0:dac6a7d43ed2 147 }
earlz 0:dac6a7d43ed2 148 #endif
earlz 0:dac6a7d43ed2 149
earlz 0:dac6a7d43ed2 150 #endif