IPS(Interpreter for Process Structures) for mbed

Dependencies:   ConfigFile FATFileSystem mbed

IPS port from linux/unix version.

mbed_blinky.ips

0 VAR led1
" LED1 " DigitalOut led1 !
: main
    ANFANG
    1 JA?
      1 led1 @ write
      200 wait_ms
      0 led1 @ write
      200 wait_ms
    DANN/NOCHMAL
;
main
Revision:
1:e74530ad6b9e
Child:
2:908338b1151a
diff -r 25fe9b2fe195 -r e74530ad6b9e main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 13 18:39:01 2015 +0900
@@ -0,0 +1,124 @@
+// main.cpp 2015/5/13
+#include "mbed.h"
+#include "BaseIPS.h"
+#include "VideoRAM.h"
+#include "VirtualRAM.h"
+#include "SDFileSystem.h"
+#include "ConfigFile.h"
+#include <string>
+
+#define CFG_FILE "/local/ips.cfg"
+
+#ifndef MBED_SPI0
+#define MBED_SPI0 D11, D12, D13, D4
+#endif
+SDFileSystem local(MBED_SPI0, "local");  //  mosi, miso, sclk, cs, name
+
+RawSerial pc(USBTX,USBRX);
+
+class myips : public BaseIPS {
+    VideoRAM vram;
+    VirtualRAM mem;
+    RawSerial& _pc;
+    Timer t20ms;
+public:
+    myips(RawSerial& pc): vram(pc),_pc(pc) {
+        t20ms.reset();
+        t20ms.start();
+    }
+     virtual u8 peekB(u16 a) {
+        return mem.peek(a);
+    }
+    virtual void pokeB(u16 a, u8 b) {
+        mem.poke(a, b);
+        vram.vpoke(a, b);
+    }
+    virtual void* file_open(const char* filename, const char* mode) {
+        string path = string("/local/") + filename;
+        return fopen(path.c_str(), mode);
+    }
+    virtual int file_getc(void* handle) {
+        MBED_ASSERT(handle);
+        int c = fgetc((FILE*)handle);
+        return c == EOF ? (-1) : c;
+    }
+    virtual void file_putc(int c, void* handle) {
+        MBED_ASSERT(handle);
+        fputc(c, (FILE*)handle);
+    }
+    virtual bool file_close(void* handle) {
+        MBED_ASSERT(handle);
+        if (fclose((FILE*)handle) == 0) {
+            return true;
+        }
+        return false;
+    }
+
+    virtual bool test_20ms() {
+        if (t20ms.read_ms() >= 20) {
+            t20ms.reset();
+            return true;
+        }
+        return false;
+    }
+
+    virtual void do_io() {
+        if (_pc.readable()) {
+            int c = _pc.getc();
+            if (c == '\n' || c == '\r') {
+                if (input_ptr != peek(a_PI)) {
+                   pokeB(READYFLAG, 1);
+                   poke(a_PE, input_ptr - 1);
+                }
+            else if (c == 0x08) // BS
+                if (input_ptr > 0) {
+                    input_ptr--;
+                }
+            } else {
+                if (input_ptr <= TVE) {
+                    pokeB(input_ptr++, c);
+                }
+            }
+        }
+    }
+};
+
+int main() {
+    string image = "IPS-Mu.bin";
+    string file;
+    string cmd;
+    ConfigFile cfg;
+    if(cfg.read(CFG_FILE)) {
+        char buf[128];
+        if (cfg.getValue("image", buf, sizeof(buf))) {
+            image = buf;
+        }
+        if (cfg.getValue("file", buf, sizeof(buf))) {
+            file = buf;
+            cmd = string("\" ") + file + string(" \" READ");
+        }
+        if (cfg.getValue("cmd", buf, sizeof(buf))) {
+            cmd = buf;
+        }
+    }
+
+    myips* ips = new myips(pc);
+    void* fh = ips->file_open(image.c_str(), "rb");
+    MBED_ASSERT(fh);
+    if (fh) {
+        for(int i = 0; i <= 65535; i++) {
+            int c = ips->file_getc(fh);
+            if (c == (-1)) {
+                break;
+            }
+            ips->pokeB(i, c);
+        }
+        ips->file_close(fh);
+    }
+    if (cmd != "") {
+        ips->command(cmd.c_str());
+    }
+    ips->emulator();
+    /* NOTREACHED */
+}
+