static type_u find_method(type_u phandle, const char *name) { forth_push((type_u)name); forth_push(strlen(name)); forth_push(phandle); forth_eval("find-method"); if (forth_pop()) return forth_pop(); return 0; }
int socket(int domain, int type, int proto, char *mac_addr) { const char mac_prop_name[] = "local-mac-address"; type_u phandle; uint8_t *prop_addr; int prop_len; int fd; /* search free file descriptor (and skip stdio handlers) */ for (fd = 3; fd < FILEIO_MAX; ++fd) { if (fd_array[fd].type == FILEIO_TYPE_EMPTY) { break; } } if (fd == FILEIO_MAX) { puts("Can not open socket, file descriptor list is full"); return -2; } /* Assume that obp-tftp package is the current one, so * my-parent is the NIC node that we are interested in */ forth_eval("my-parent ?dup IF ihandle>phandle THEN"); phandle = forth_pop(); if (phandle == 0) { puts("Can not open socket, no parent instance"); return -1; } fd_array[fd].read_xt = find_method(phandle, "read"); if (!fd_array[fd].read_xt) { puts("Can not open socket, no 'read' method"); return -1; } fd_array[fd].write_xt = find_method(phandle, "write"); if (!fd_array[fd].write_xt) { puts("Can not open socket, no 'write' method"); return -1; } /* Read MAC address from device */ forth_push((unsigned long)mac_prop_name); forth_push(strlen(mac_prop_name)); forth_push(phandle); forth_eval("get-property"); if (forth_pop()) return -1; prop_len = forth_pop(); prop_addr = (uint8_t *)forth_pop(); memcpy(mac_addr, &prop_addr[prop_len - 6], 6); fd_array[fd].type = FILEIO_TYPE_SOCKET; return fd; }
/** * Call Forth code to try to claim the memory region */ int elf_forth_claim(void *addr, long size) { forth_push((long)addr); forth_push(size); forth_eval("elf-claim-segment"); return forth_pop(); }
long SLOF_dma_map_in(void *virt, long size, int cacheable) { forth_push((long)virt); forth_push(size); forth_push(cacheable); forth_eval("dma-map-in"); return forth_pop(); }
static double pop(struct forth* f) { return forth_pop(f); }
void test_colon(void) { test_setup(); printf("\n<<<<<<<<<<<< TEST ':' <<<<<<<<<<<< \n\n"); printf("ctx->rsp: %p\n", ctx_rsp); printf("ctx->psp: %p\n", ctx_psp); printf("ctx->vars: %p\n", ctx_vars); printf("ctx->regs: %p\n", ctx_regs); // Vars int i, n; fcell_xt* var[50] = {0}; i = 0; n = 15; for (int j = 0; j < n; j++) var[j] = forth_alloc_var(); fword_t *entry_colon = dict_find(1, ":"); printf("colon: `:` %p \n", entry_colon); TEST_CHECK_(entry_colon != NULL, "Expected non-null `:` word, got "CELL_FMT"", entry_colon); printf("colon cfa: `:` %p\n", dict_cfa(entry_colon)); printf("xt_colon: `:` %p -> %p \n", xt_colon, *xt_colon); int idx_interpret = i; *var[i++] = (fcell_xt) dict_cfa(dict_find(7, "docolon")); *var[i++] = (fcell_xt) dict_cfa(dict_find(9, "interpret")); *var[i++] = dict_cfa(dict_find(4, "semi")); (void)idx_interpret; ctx_vars->tib_str = " : a 0x99 ;"; ctx_vars->tib_len = strlen(ctx_vars->tib_str); ctx_vars->tib_idx = 0; forth_eval(var[0]); forth_flush_tob(); dict_print(); // Try running new word! fword_t *entry_a = dict_find(1, "a"); TEST_CHECK_(entry_a != NULL, "Expected non-null `:` word, got %p for %s", entry_a, "entry_a"); printf("entry_a: %p\n", entry_a); fcell_xt * cfa_a = (fcell_xt *) dict_cfa(entry_a); TEST_CHECK_(cfa_a != NULL, "Expected non-null `:` word, got "CELL_FMT" for %s", cfa_a, "cfa_a"); printf("cfa_aa: %p\n", cfa_a); fword_t *semicolon = dict_find(1, ";"); printf("semicolon: %p\n", semicolon); fcell_t expi; fcell_t expl; fcell_t cnt; fcell_t x; fcell_t err; // Good Case ctx_vars->tib_str = "a 1 +"; ctx_vars->tib_len = 5; ctx_vars->tib_idx = 0; forth_eval(var[0]); forth_flush_tob(); expi = 154; expl = 1; cnt = forth_count(); TEST_CHECK_(cnt == expl, "Expected "CELL_FMT", got "CELL_FMT"", expl, cnt); x = forth_pop(); TEST_CHECK_(x == expi, "Expected "CELL_FMT", got "CELL_FMT"", expi, x); err = forth_errno(); TEST_CHECK_(err == FW_OK, "Expected "CELL_FMT", got "CELL_FMT"", FW_OK, err); // Bad Case ctx_vars->tib_str = "b 1 +"; ctx_vars->tib_len = 5; ctx_vars->tib_idx = 0; forth_eval(var[0]); forth_flush_tob(); expi = 0; expl = 0; cnt = forth_count(); TEST_CHECK_(cnt == expl, "Expected "CELL_FMT", got "CELL_FMT"", expl, cnt); err = forth_errno(); TEST_CHECK_(err == FW_ERR_NOWORD, "Expected "CELL_FMT", got "CELL_FMT"", FW_ERR_NOWORD, err); }
void test_interpreter(void) { test_setup(); dict_print(); fcell_t dlen = ctx_vars->tob_idx; printf("TOB SZ: %s\n", ctx_vars->tob_str); for (fcell_t i = 0; i < dlen; i++) { putchar(ctx_vars->tob_str[i]); } printf("TOB DONE\n"); printf("ctx->rsp: %p\n", ctx_rsp); printf("ctx->psp: %p\n", ctx_psp); printf("ctx->vars: %p\n", ctx_vars); printf("ctx->regs: %p\n", ctx_regs); // Vars int i, n; fcell_xt* var[50] = {0}; i = 0; n = 4; for (int j = 0; j < n; j++) var[j] = forth_alloc_var(); // Colons *var[i++] = (fcell_xt) dict_cfa(dict_find(7, "docolon")); *var[i++] = (fcell_xt) dict_cfa(dict_find(9, "interpret")); *var[i++] = dict_cfa(dict_find(4, "semi")); ctx_vars->tib_str = "7 2 +"; ctx_vars->tib_len = 5; ctx_vars->tib_idx = 0; forth_eval(var[0]); forth_flush_tob(); print_psp_info(); int cnt = forth_count(); fcell_t x = forth_pop(); TEST_CHECK_(1 == cnt, "Expected "CELL_FMT", got "CELL_FMT"", 1, cnt); print_stack(); printf("... stack done\n"); TEST_CHECK_(x == 9, "Expected "CELL_FMT", got "CELL_FMT"", 9, x); x = forth_pop(); cnt = forth_count(); TEST_CHECK_(0 == cnt, "Expected "CELL_FMT", got "CELL_FMT"", 0, cnt); TEST_CHECK_(forth_errno() == FW_ERR_STACKUNDERFLOW, "Expected "CELL_FMT", got "CELL_FMT"", forth_errno(), FW_ERR_STACKUNDERFLOW); forth_clear(); TEST_CHECK_(forth_errno() == FW_OK, "Expected "CELL_FMT", got "CELL_FMT"", forth_errno(), FW_OK); printf(" >>>>>>>>>>>>>> basic test \n\n\n"); }
void *SLOF_alloc_mem(long size) { forth_push(size); forth_eval("alloc-mem"); return (void *)forth_pop(); }
void *SLOF_dma_alloc(long size) { forth_push(size); forth_eval("dma-alloc"); return (void *)forth_pop(); }
/** * get msec-timer value * access to HW register * overrun will occur if boot exceeds 1.193 hours (49 days) * * @param - * @return actual timer value in ms as 32bit */ uint32_t SLOF_GetTimer(void) { forth_eval("get-msecs"); return (uint32_t) forth_pop(); }