From 1c63a67d9893bc49a92e3b9e0b3ccd7e050889b9 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Mon, 31 Jan 2022 18:31:40 -0800 Subject: [PATCH] Improve sim, runs without error. --- ueforth/Makefile | 2 +- ueforth/esp32/builtins.h | 4 -- ueforth/esp32/options.h | 8 ++- ueforth/esp32/print-builtins.cpp | 9 ++-- ueforth/esp32/sim_main.cpp | 87 ++++++++++++++++++++++---------- 5 files changed, 70 insertions(+), 40 deletions(-) diff --git a/ueforth/Makefile b/ueforth/Makefile index 1cfc609..af5885c 100644 --- a/ueforth/Makefile +++ b/ueforth/Makefile @@ -329,7 +329,7 @@ $(ESP32_SIM)/Esp32forth-sim: \ common/interp.h \ $(GEN)/esp32_boot.h \ $(GEN)/esp32_sim_opcodes.h | $(ESP32_SIM) - $(CXX) $(CFLAGS) -g $< -o $@ + $(CXX) $(CFLAGS) $< -o $@ strip $(STRIP_ARGS) $@ # ---- ESP32 ---- diff --git a/ueforth/esp32/builtins.h b/ueforth/esp32/builtins.h index b4b07aa..a59bf2c 100644 --- a/ueforth/esp32/builtins.h +++ b/ueforth/esp32/builtins.h @@ -62,8 +62,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size); OPTIONAL_SPI_FLASH_SUPPORT \ USER_WORDS -#ifndef SIM_PRINT_ONLY - #define REQUIRED_MEMORY_SUPPORT \ Y(MALLOC, SET malloc(n0)) \ Y(SYSFREE, free(a0); DROP) \ @@ -86,8 +84,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size); X("Serial.write", SERIAL_WRITE, n0 = Serial.write(b1, n0); NIP) \ X("Serial.flush", SERIAL_FLUSH, Serial.flush()) -#endif - #define REQUIRED_ARDUINO_GPIO_SUPPORT \ Y(pinMode, pinMode(n1, n0); DROPn(2)) \ Y(digitalWrite, digitalWrite(n1, n0); DROPn(2)) \ diff --git a/ueforth/esp32/options.h b/ueforth/esp32/options.h index 9ce3569..1718129 100644 --- a/ueforth/esp32/options.h +++ b/ueforth/esp32/options.h @@ -38,7 +38,9 @@ // RMT support designed around v2.0.1 toolchain. // While ESP32 also has RMT, for now only include for // ESP32-S2 and ESP32-C3. -#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(SIM_PRINT_ONLY) +#if defined(CONFIG_IDF_TARGET_ESP32S2) || \ + defined(CONFIG_IDF_TARGET_ESP32C3) || \ + defined(SIM_PRINT_ONLY) # define ENABLE_RMT_SUPPORT #endif @@ -57,7 +59,9 @@ // built the serial library, so check if its enabled as well. #if defined(BOARD_HAS_PSRAM) || defined(SIM_PRINT_ONLY) # define ENABLE_CAMERA_SUPPORT -# if (defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)) || defined(SIM_PRINT_ONLY) +# if (defined(CONFIG_BT_ENABLED) && \ + defined(CONFIG_BLUEDROID_ENABLED)) || \ + defined(SIM_PRINT_ONLY) # define ENABLE_SERIAL_BLUETOOTH_SUPPORT # endif #endif diff --git a/ueforth/esp32/print-builtins.cpp b/ueforth/esp32/print-builtins.cpp index 71d4bbf..c91d457 100644 --- a/ueforth/esp32/print-builtins.cpp +++ b/ueforth/esp32/print-builtins.cpp @@ -17,19 +17,18 @@ #include #define SIM_PRINT_ONLY +#define ENABLE_OLED_SUPPORT #include "esp32/options.h" #define FLOATING_POINT_LIST #define USER_WORDS -#define REQUIRED_MEMORY_SUPPORT -#define REQUIRED_SYSTEM_SUPPORT -#define REQUIRED_SERIAL_SUPPORT #include "builtins.h" #define Y(name, code) X(#name, name, code) int main() { - printf("#define PLATFORM_MOCK_OPCODE_LIST \\\n"); -#define X(str, name, code) printf(" X(\"%s\", %s, ) \\\n", str, #name); + printf("#define PLATFORM_SIMULATED_OPCODE_LIST \\\n"); +#define X(str, name, code) \ + printf(" X(\"%s\", %s, DUP; sp = simulated(sp, STR_%s); DROP) \\\n", str, #name, #name); PLATFORM_OPCODE_LIST #undef X printf("\n"); diff --git a/ueforth/esp32/sim_main.cpp b/ueforth/esp32/sim_main.cpp index 6a5e10a..6de111c 100644 --- a/ueforth/esp32/sim_main.cpp +++ b/ueforth/esp32/sim_main.cpp @@ -17,43 +17,74 @@ #include "common/opcodes.h" #include "common/floats.h" #include "common/calling.h" -#include -#include + +static cell_t *simulated(cell_t *sp, const char *op); #define PLATFORM_OPCODE_LIST \ FLOATING_POINT_LIST \ - REQUIRED_MEMORY_SUPPORT \ - REQUIRED_SYSTEM_SUPPORT \ - REQUIRED_SERIAL_SUPPORT \ - PLATFORM_MOCK_OPCODE_LIST - -#define REQUIRED_MEMORY_SUPPORT \ - Y(MALLOC, SET malloc(n0)) \ - Y(SYSFREE, free(a0); DROP) \ - Y(REALLOC, SET realloc(a1, n0); NIP) \ - Y(heap_caps_malloc, SET malloc(n1); NIP) \ - Y(heap_caps_free, free(a0); DROP) \ - Y(heap_caps_realloc, \ - tos = (cell_t) realloc(a2, n1); NIPn(2)) - -#define REQUIRED_SYSTEM_SUPPORT \ - X("MS-TICKS", MS_TICKS, PUSH time(0) * 1000) \ - X("RAW-YIELD", RAW_YIELD, ) \ - Y(TERMINATE, exit(n0)) - -#define REQUIRED_SERIAL_SUPPORT \ - X("Serial.begin", SERIAL_BEGIN, DROP) \ - X("Serial.end", SERIAL_END, ) \ - X("Serial.available", SERIAL_AVAILABLE, PUSH -1) \ - X("Serial.readBytes", SERIAL_READ_BYTES, n0 = read(0, b1, n0); NIP) \ - X("Serial.write", SERIAL_WRITE, n0 = write(1, b1, n0); NIP) \ - X("Serial.flush", SERIAL_FLUSH, ) + PLATFORM_SIMULATED_OPCODE_LIST #include "gen/esp32_sim_opcodes.h" + +#define X(str, name, code) static const char *STR_ ## name = str; +PLATFORM_SIMULATED_OPCODE_LIST +#undef X + #include "common/core.h" #include "common/interp.h" #include "gen/esp32_boot.h" #include "esp32/main.cpp" +#include +#include +#include + +static cell_t *simulated(cell_t *sp, const char *op) { + if (op == STR_MALLOC) { + *sp = (cell_t) malloc(*sp); + return sp; + } else if (op == STR_SYSFREE) { + free((void*) *sp--); + return sp; + } else if (op == STR_SERIAL_BEGIN) { + --sp; + return sp; + } else if (op == STR_SERIAL_READ_BYTES) { + sp[-1] = read(0, (void *) sp[-1], sp[0]); --sp; + return sp; + } else if (op == STR_SERIAL_WRITE) { + sp[-1] = write(1, (void *) sp[-1], sp[0]); --sp; + return sp; + } else if (op == STR_MS_TICKS) { + struct timespec tm; + clock_gettime(CLOCK_MONOTONIC, &tm); + *++sp = tm.tv_sec * 1000 + tm.tv_nsec / 1000000; + return sp; + } else if (op == STR_RAW_YIELD) { + return sp; + } else if (op == STR_SPIFFS_BEGIN) { + sp -= 2; *sp = 0; + return sp; + } else if (op == STR_pinMode) { + sp -= 2; + return sp; + } else if (op == STR_digitalWrite) { + sp -= 2; + return sp; + } else if (op == STR_gpio_install_isr_service) { + --sp; + *sp = 0; + return sp; + } else if (op == STR_SERIAL_AVAILABLE) { + *++sp = 1; + return sp; + } else if (op == STR_TERMINATE) { + exit(*sp); + return sp; + } else { + fprintf(stderr, "MISSING SIM OPCODE: %s\n", op); + return sp; + } +} int main(int argc, char *argv[]) { setup();