IPS(Interpreter for Process Structures) for mbed
Dependencies: ConfigFile FATFileSystem mbed
main.cpp
00001 // main.cpp 2015/5/23 00002 #include <new> 00003 #include "mbed.h" 00004 #include "BaseIPS.h" 00005 #include "VideoRAM.h" 00006 #include "VirtualRAM.h" 00007 #include "IPS-Mmbed_bin.h" 00008 #include "mbedAPI.h" 00009 #include "ConfigFile.h" 00010 #include "SDFileSystem.h" 00011 00012 const char* CFG_FILE = "/local/ips.cfg"; 00013 00014 #if defined(TARGET_MBED_LPC1768) 00015 #if defined(TOOLCHAIN_GCC)||defined(TOOLCHAIN_GCC_ARM) 00016 SDFileSystem local(MBED_SPI0, "local"); // mosi, miso, sclk, cs, name 00017 #else 00018 LocalFileSystem local("local"); 00019 #endif 00020 #else 00021 SDFileSystem local(D11, D12, D13, D4, "local"); // mosi, miso, sclk, cs, name 00022 #endif 00023 00024 RawSerial pc(USBTX,USBRX); 00025 00026 template<class SERIAL_T> 00027 class myips : public BaseIPS { 00028 VideoRAM<SERIAL_T> vram; 00029 VirtualRAM mem; 00030 SERIAL_T& _pc; 00031 mbedAPI mbedapi; 00032 Timer t20ms; 00033 public: 00034 myips(SERIAL_T& pc): vram(pc),_pc(pc),mbedapi(*this) { 00035 t20ms.reset(); 00036 t20ms.start(); 00037 } 00038 virtual uint8_t mem_peek(uint16_t a) { 00039 return mem.peek(a); 00040 } 00041 virtual void mem_poke(uint16_t a, uint8_t b) { 00042 mem.poke(a, b); 00043 vram.vpoke(a, b); 00044 } 00045 virtual void* file_open(const char* filename, const char* mode) { 00046 if (filename[0] == '/') { 00047 return fopen(filename, mode); 00048 } 00049 char path[128]; 00050 snprintf(path, sizeof(path), "/local/%s", filename); 00051 return fopen(path, mode); 00052 } 00053 virtual int file_getc(void* handle) { 00054 int c = fgetc((FILE*)handle); 00055 return c == EOF ? (-1) : c; 00056 } 00057 virtual void file_putc(int c, void* handle) { 00058 fputc(c, (FILE*)handle); 00059 } 00060 virtual bool file_close(void* handle) { 00061 return fclose((FILE*)handle) == 0 ? true : false; 00062 } 00063 virtual bool test_20ms() { 00064 if (t20ms.read_ms() >= 20) { 00065 t20ms.reset(); 00066 return true; 00067 } 00068 return false; 00069 } 00070 00071 virtual void do_io() { 00072 if (_pc.readable()) { 00073 int c = _pc.getc(); 00074 if (c == '\n' || c == '\r') { 00075 if (input_ptr != peek(a_PI)) { 00076 pokeB(READYFLAG, 1); 00077 poke(a_PE, input_ptr - 1); 00078 } 00079 else if (c == 0x08) // BS 00080 if (input_ptr > 0) { 00081 input_ptr--; 00082 } 00083 } else { 00084 if (input_ptr <= TVE) { 00085 pokeB(input_ptr++, c); 00086 } 00087 } 00088 } 00089 } 00090 virtual void usercode(uint16_t cpc) { 00091 switch(cpc) { 00092 case 80: break; // c_sleepifidle(void) 00093 case 96: mbedapi.code(); break; 00094 default: error("code(#%x) not implemented!", cpc); 00095 } 00096 } 00097 }; 00098 00099 void no_memory () { 00100 perror("Failed to allocate memory!"); 00101 } 00102 00103 int main() { 00104 std::set_new_handler(no_memory); 00105 00106 char* image = NULL; 00107 char* cmd = NULL; 00108 if (CFG_FILE) { 00109 ConfigFile cfg; 00110 if(cfg.read(const_cast<char*>(CFG_FILE))) { 00111 char buf[128]; 00112 if (cfg.getValue(const_cast<char*>("image"), buf, sizeof(buf))) { 00113 static char buf2[128]; 00114 snprintf(buf2, sizeof(buf2), "%s", buf); 00115 image = buf2; 00116 } 00117 if (cfg.getValue(const_cast<char*>("file"), buf, sizeof(buf))) { 00118 static char buf2[128]; 00119 snprintf(buf2, sizeof(buf2), "\" %s \" READ", buf); 00120 cmd = buf2; 00121 } 00122 if (cfg.getValue(const_cast<char*>("cmd"), buf, sizeof(buf))) { 00123 cmd = buf; 00124 } 00125 } 00126 } 00127 00128 myips<RawSerial> ips(pc); 00129 if (image != NULL) { 00130 void* fh = ips.file_open(image, "rb"); 00131 if (fh == NULL) { 00132 pc.printf("file open error [%s]\n", image); 00133 image = NULL; 00134 } else { 00135 for(int i = 0; i <= 65535; i++) { 00136 int c = ips.file_getc(fh); 00137 if (c == (-1)) { 00138 break; 00139 } 00140 ips.pokeB(i, c); 00141 } 00142 ips.file_close(fh); 00143 } 00144 } 00145 if (image == NULL) { 00146 for(size_t i = 0; i < sizeof(IPS_Mmbed_bin); i++) { 00147 ips.pokeB(i, IPS_Mmbed_bin[i]); 00148 } 00149 } 00150 if (cmd) { 00151 ips.command(cmd); 00152 } 00153 ips.emulator(); 00154 std::exit(1); 00155 } 00156
Generated on Fri Jul 15 2022 06:25:55 by 1.7.2