A project to implement a console using the Mbed using VGA for video output and a PS/2 keyboard for the input. The eventual goal is to also include tools for managing SD cards, and a semi-self-hosting programming environment.
Dependencies: PS2_MbedConsole fastlib SDFileSystem vga640x480g_mbedconsole lightvm mbed
MbedConsole is a cool little project to have a self-contained computer all on an Mbed. So far it has VGA and PS/2 support and can stand alone without a computer powering it. Next planned features are SD card support and a lightweight programmable VM complete with a file editor and self-hosted assembler.
You can view additional details about it at http://earlz.net/tags/mbedconsole
Diff: plEarlz.cpp
- Revision:
- 5:367397a82ddc
- Parent:
- 4:b44c27404035
- Child:
- 6:a4dff59ef214
--- a/plEarlz.cpp Wed Sep 19 05:05:31 2012 +0000 +++ b/plEarlz.cpp Thu Sep 20 04:43:50 2012 +0000 @@ -28,6 +28,7 @@ #define MAXSTACK 64 #define MAXCALLS 32 #define MAXLINELENGTH 128 +#define DICTIONARYSTEP 4 //how much memory to reserve when doing a reallocation for resizing int pl_stackpos=0; int pl_stack[MAXSTACK]; @@ -49,6 +50,27 @@ Word, Quote }; +typedef void (*BuiltinFunction)(void); +typedef struct +{ + char name[12]; + enum Type{ + Constant, + ConstantString, + Builtin, + Function + } type; + union valueunion{ + int intvalue; + BuiltinFunction builtin; + char *string; + } value; +} WordKey; + + +int pl_dictionary_count=0; +int pl_dictionary_size=0; +WordKey *pl_dictionary; //todo: a hash table would be much faster void pl_push(int val) { @@ -73,24 +95,89 @@ return pl_stack[pl_stackpos]; } +WordKey *pl_lookup(char* name) +{ + for(int i=0;i<pl_dictionary_count;i++) + { + if(strlcmp(name, pl_dictionary[i].name, 12)==0) + { + return &pl_dictionary[i]; + } + } + return NULL; +} + +int pl_addword() +{ + if(pl_dictionary_size==0){ + pl_dictionary=(WordKey*)malloc(sizeof(WordKey)*12); + pl_dictionary_size=12; + return 0; + } + if(pl_dictionary_size<=pl_dictionary_count) + { + void* tmp=realloc(pl_dictionary, pl_dictionary_size+sizeof(WordKey)*DICTIONARYSTEP); + if(tmp==NULL) + { + vputs("Out of memory!! Epic Fail!!"); + return -1; + } + pl_dictionary=(WordKey*)tmp; + pl_dictionary_size+=sizeof(WordKey)*DICTIONARYSTEP; + } + return pl_dictionary_count++; +} + void execute_word(char *word, WordType type) { if(type==Number){ pl_push(atoi(word)); }else{ - if(word[1]==0 && word[0]=='.') + if(word[1]==0) //check the quick builtins first { - int tmp=pl_pop(); - sprintf( word, "%i\n", tmp); - vputs(word); + if(word[0]=='.'){ + int tmp=pl_pop(); + sprintf( word, "%i\n", tmp); + vputs(word); + return; + }else if(word[0]==';') + { + //...? + }else if(word[0]=='+'){ + pl_push(pl_pop()+pl_pop()); + }else if(word[0]=='/'){ + pl_push(pl_pop()/pl_pop()); + }else if(word[0]=='-'){ + pl_push(pl_pop()-pl_pop()); + }else if(word[0]=='!'){ + pl_push(pl_pop()-pl_pop()); + }else if(word[0]=='%'){ + pl_push(pl_pop()%pl_pop()); + }else if(word[0]=='*'){ + pl_push(pl_pop()*pl_pop()); + }else if(word[0]=='!'){ + int tmp=pl_pop(); + int* tmp2=(int*)pl_pop(); + *tmp2=tmp; + }else if(word[0]=='?'){ + pl_push(*(int*)pl_pop()); + }else if(word[0]=='='){ + pl_push(pl_pop() == pl_pop()); + }else if(word[0]=='`'){ + pl_push(pl_pop()>0); + }else if(word[0]=='$'){ + //print string (address on stack + } + }else{ vputs("I don't know the word "); vputs(word); vputs("\n"); } } +} -} + void parse_line(char *line) { @@ -102,7 +189,7 @@ for(int i=0;i<len;i++) { char c=line[i]; - if(is_whitespace(c)) + if(is_whitespace(c) || c=='\\') { if(word[0]!=0) { @@ -112,6 +199,9 @@ } wordpos=0; type=Unknown; + if(c=='\\'){ + return; //rest is a comment + } }else if(is_numeric(c)){ if(type==Unknown){ type=Number;