Compare commits
21 Commits
b115744991
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
b50883b3ed
|
|||
|
1d16c23fc5
|
|||
|
29ad4c2835
|
|||
|
d8fad1765e
|
|||
|
d73ace71fb
|
|||
|
9c242187ea
|
|||
|
75ddd75ab9
|
|||
|
47b6a1eb82
|
|||
|
53b4cff56a
|
|||
|
278a524bbb
|
|||
|
efcc54ed4b
|
|||
|
0d49827035
|
|||
|
fcb533ff24
|
|||
|
23451c8e85
|
|||
|
fcbc810db8
|
|||
|
dbf4eb5d0e
|
|||
|
19c8608c76
|
|||
|
1c0c6daf4f
|
|||
|
3c292cfc18
|
|||
|
dcc88bc8ca
|
|||
|
f033187c6c
|
@@ -1,19 +1,23 @@
|
|||||||
CC = gcc
|
CC = clang
|
||||||
CFLAGS = -Wall -Wextra -g -std=c11 -D_POSIX_C_SOURCE=200809L
|
CFLAGS = -Wall -Wextra -g -std=c11
|
||||||
LDFLAGS =
|
COMMON_SRCS = forth_core.c forth_dict.c forth_words.c forth_interp.c main.c
|
||||||
SRCS = forth_core.c forth_dict.c forth_words.c forth_interp.c main.c
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
LINUX_SRCS = $(COMMON_SRCS) platform_linux.c
|
||||||
|
KERNEL_SRCS = $(COMMON_SRCS) platform_kernel.c kernel_entry.S font8x8.c
|
||||||
|
KERNEL_LD = kernel.ld
|
||||||
|
|
||||||
TARGET = forth
|
TARGET = forth
|
||||||
|
KERNEL_TARGET = forth.kernel
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(OBJS)
|
$(TARGET): $(LINUX_SRCS) forth.h
|
||||||
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
|
$(CC) $(CFLAGS) -static -nostdlib -ffreestanding -fno-stack-protector -o $@ $(LINUX_SRCS)
|
||||||
|
|
||||||
%.o: %.c forth.h
|
$(KERNEL_TARGET): $(KERNEL_SRCS) forth.h $(KERNEL_LD)
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -ffreestanding -nostdlib -fno-stack-protector -mno-red-zone -fno-pie -no-pie -o $@ $(KERNEL_SRCS) -T$(KERNEL_LD) -Wl,--build-id=none
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS) $(TARGET)
|
rm -f $(TARGET) $(KERNEL_TARGET)
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
const uint8_t font8x8[96][8] = {
|
||||||
|
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 32 ' ' */
|
||||||
|
{0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x00}, /* 33 '!' */
|
||||||
|
{0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00}, /* 34 '"' */
|
||||||
|
{0x66,0x66,0xFF,0x66,0xFF,0x66,0x66,0x00}, /* 35 '#' */
|
||||||
|
{0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00}, /* 36 '$' */
|
||||||
|
{0x62,0x66,0x0C,0x18,0x30,0x66,0x46,0x00}, /* 37 '%' */
|
||||||
|
{0x3C,0x66,0x3C,0x38,0x67,0x66,0x3F,0x00}, /* 38 '&' */
|
||||||
|
{0x06,0x0C,0x18,0x00,0x00,0x00,0x00,0x00}, /* 39 ''' */
|
||||||
|
{0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00}, /* 40 '(' */
|
||||||
|
{0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00}, /* 41 ')' */
|
||||||
|
{0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00}, /* 42 '*' */
|
||||||
|
{0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00}, /* 43 '+' */
|
||||||
|
{0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30}, /* 44 ',' */
|
||||||
|
{0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00}, /* 45 '-' */
|
||||||
|
{0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00}, /* 46 '.' */
|
||||||
|
{0x00,0x03,0x06,0x0C,0x18,0x30,0x60,0x00}, /* 47 '/' */
|
||||||
|
{0x3C,0x66,0x6E,0x76,0x66,0x66,0x3C,0x00}, /* 48 '0' */
|
||||||
|
{0x18,0x18,0x38,0x18,0x18,0x18,0x7E,0x00}, /* 49 '1' */
|
||||||
|
{0x3C,0x66,0x06,0x0C,0x30,0x60,0x7E,0x00}, /* 50 '2' */
|
||||||
|
{0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00}, /* 51 '3' */
|
||||||
|
{0x06,0x0E,0x1E,0x66,0x7F,0x06,0x06,0x00}, /* 52 '4' */
|
||||||
|
{0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00}, /* 53 '5' */
|
||||||
|
{0x3C,0x60,0x60,0x7C,0x66,0x66,0x3C,0x00}, /* 54 '6' */
|
||||||
|
{0x7E,0x06,0x0C,0x18,0x30,0x30,0x30,0x00}, /* 55 '7' */
|
||||||
|
{0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00}, /* 56 '8' */
|
||||||
|
{0x3C,0x66,0x66,0x3E,0x06,0x06,0x3C,0x00}, /* 57 '9' */
|
||||||
|
{0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00}, /* 58 ':' */
|
||||||
|
{0x00,0x00,0x18,0x00,0x00,0x18,0x18,0x30}, /* 59 ';' */
|
||||||
|
{0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00}, /* 60 '<' */
|
||||||
|
{0x00,0x00,0x7E,0x00,0x7E,0x00,0x00,0x00}, /* 61 '=' */
|
||||||
|
{0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00}, /* 62 '>' */
|
||||||
|
{0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00}, /* 63 '?' */
|
||||||
|
{0x3C,0x66,0x6E,0x6E,0x60,0x62,0x3C,0x00}, /* 64 '@' */
|
||||||
|
{0x18,0x3C,0x66,0x7E,0x66,0x66,0x66,0x00}, /* 65 'A' */
|
||||||
|
{0x7C,0x66,0x66,0x7C,0x66,0x66,0x7C,0x00}, /* 66 'B' */
|
||||||
|
{0x3C,0x66,0x60,0x60,0x60,0x66,0x3C,0x00}, /* 67 'C' */
|
||||||
|
{0x78,0x6C,0x66,0x66,0x66,0x6C,0x78,0x00}, /* 68 'D' */
|
||||||
|
{0x7E,0x60,0x60,0x78,0x60,0x60,0x7E,0x00}, /* 69 'E' */
|
||||||
|
{0x7E,0x60,0x60,0x78,0x60,0x60,0x60,0x00}, /* 70 'F' */
|
||||||
|
{0x3C,0x66,0x60,0x6E,0x66,0x66,0x3C,0x00}, /* 71 'G' */
|
||||||
|
{0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00}, /* 72 'H' */
|
||||||
|
{0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00}, /* 73 'I' */
|
||||||
|
{0x1E,0x0C,0x0C,0x0C,0x0C,0x6C,0x38,0x00}, /* 74 'J' */
|
||||||
|
{0x66,0x6C,0x78,0x70,0x78,0x6C,0x66,0x00}, /* 75 'K' */
|
||||||
|
{0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00}, /* 76 'L' */
|
||||||
|
{0x63,0x77,0x7F,0x6B,0x63,0x63,0x63,0x00}, /* 77 'M' */
|
||||||
|
{0x66,0x76,0x7E,0x7E,0x6E,0x66,0x66,0x00}, /* 78 'N' */
|
||||||
|
{0x3C,0x66,0x66,0x66,0x66,0x66,0x3C,0x00}, /* 79 'O' */
|
||||||
|
{0x7C,0x66,0x66,0x7C,0x60,0x60,0x60,0x00}, /* 80 'P' */
|
||||||
|
{0x3C,0x66,0x66,0x66,0x66,0x3C,0x0E,0x00}, /* 81 'Q' */
|
||||||
|
{0x7C,0x66,0x66,0x7C,0x78,0x6C,0x66,0x00}, /* 82 'R' */
|
||||||
|
{0x3C,0x66,0x60,0x3C,0x06,0x66,0x3C,0x00}, /* 83 'S' */
|
||||||
|
{0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, /* 84 'T' */
|
||||||
|
{0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x00}, /* 85 'U' */
|
||||||
|
{0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00}, /* 86 'V' */
|
||||||
|
{0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00}, /* 87 'W' */
|
||||||
|
{0x66,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00}, /* 88 'X' */
|
||||||
|
{0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00}, /* 89 'Y' */
|
||||||
|
{0x7E,0x06,0x0C,0x18,0x30,0x60,0x7E,0x00}, /* 90 'Z' */
|
||||||
|
{0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00}, /* 91 '[' */
|
||||||
|
{0x00,0x60,0x30,0x18,0x0C,0x06,0x03,0x00}, /* 92 '\' */
|
||||||
|
{0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00}, /* 93 ']' */
|
||||||
|
{0x18,0x3C,0x66,0x00,0x00,0x00,0x00,0x00}, /* 94 '^' */
|
||||||
|
{0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00}, /* 95 '_' */
|
||||||
|
{0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00}, /* 96 '`' */
|
||||||
|
{0x00,0x00,0x3C,0x06,0x3E,0x66,0x3E,0x00}, /* 97 'a' */
|
||||||
|
{0x00,0x60,0x60,0x7C,0x66,0x66,0x7C,0x00}, /* 98 'b' */
|
||||||
|
{0x00,0x00,0x3C,0x60,0x60,0x60,0x3C,0x00}, /* 99 'c' */
|
||||||
|
{0x00,0x06,0x06,0x3E,0x66,0x66,0x3E,0x00}, /* 100 'd' */
|
||||||
|
{0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00}, /* 101 'e' */
|
||||||
|
{0x00,0x0E,0x18,0x3E,0x18,0x18,0x18,0x00}, /* 102 'f' */
|
||||||
|
{0x00,0x00,0x3E,0x66,0x66,0x3E,0x06,0x7C}, /* 103 'g' */
|
||||||
|
{0x00,0x60,0x60,0x7C,0x66,0x66,0x66,0x00}, /* 104 'h' */
|
||||||
|
{0x00,0x18,0x00,0x38,0x18,0x18,0x3C,0x00}, /* 105 'i' */
|
||||||
|
{0x00,0x06,0x00,0x06,0x06,0x06,0x06,0x3C}, /* 106 'j' */
|
||||||
|
{0x00,0x60,0x60,0x6C,0x78,0x6C,0x66,0x00}, /* 107 'k' */
|
||||||
|
{0x00,0x38,0x18,0x18,0x18,0x18,0x3C,0x00}, /* 108 'l' */
|
||||||
|
{0x00,0x00,0x66,0x7F,0x6B,0x63,0x63,0x00}, /* 109 'm' */
|
||||||
|
{0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00}, /* 110 'n' */
|
||||||
|
{0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00}, /* 111 'o' */
|
||||||
|
{0x00,0x00,0x7C,0x66,0x66,0x7C,0x60,0x60}, /* 112 'p' */
|
||||||
|
{0x00,0x00,0x3E,0x66,0x66,0x3E,0x06,0x06}, /* 113 'q' */
|
||||||
|
{0x00,0x00,0x7C,0x66,0x60,0x60,0x60,0x00}, /* 114 'r' */
|
||||||
|
{0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00}, /* 115 's' */
|
||||||
|
{0x00,0x18,0x18,0x7E,0x18,0x18,0x0C,0x00}, /* 116 't' */
|
||||||
|
{0x00,0x00,0x66,0x66,0x66,0x66,0x3E,0x00}, /* 117 'u' */
|
||||||
|
{0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00}, /* 118 'v' */
|
||||||
|
{0x00,0x00,0x63,0x6B,0x7F,0x3E,0x36,0x00}, /* 119 'w' */
|
||||||
|
{0x00,0x00,0x66,0x3C,0x18,0x3C,0x66,0x00}, /* 120 'x' */
|
||||||
|
{0x00,0x00,0x66,0x66,0x66,0x3E,0x0C,0x78}, /* 121 'y' */
|
||||||
|
{0x00,0x00,0x7E,0x0C,0x18,0x30,0x7E,0x00}, /* 122 'z' */
|
||||||
|
{0x0C,0x18,0x18,0x30,0x18,0x18,0x0C,0x00}, /* 123 '{' */
|
||||||
|
{0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, /* 124 '|' */
|
||||||
|
{0x30,0x18,0x18,0x0C,0x18,0x18,0x30,0x00}, /* 125 '}' */
|
||||||
|
{0x00,0x00,0x66,0x3C,0x00,0x00,0x00,0x00}, /* 126 '~' */
|
||||||
|
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 127 DEL */
|
||||||
|
};
|
||||||
@@ -1,16 +1,69 @@
|
|||||||
#ifndef FORTH_H
|
#ifndef FORTH_H
|
||||||
#define FORTH_H
|
#define FORTH_H
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <inttypes.h>
|
#include <stddef.h>
|
||||||
#include <sys/types.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
// Configuration (all hard limits removed)
|
// Configuration (all hard limits removed)
|
||||||
#define MAX_NAME_LEN 31
|
#define MAX_NAME_LEN 31
|
||||||
|
#define FORTH_EOF (-1)
|
||||||
|
|
||||||
|
/* Platform interface -- must be provided by the target platform layer. */
|
||||||
|
void forth_putchar(char c);
|
||||||
|
int forth_getchar(void);
|
||||||
|
void forth_printf(const char* fmt, ...);
|
||||||
|
void forth_fflush(void);
|
||||||
|
void forth_panic(void);
|
||||||
|
|
||||||
|
/* Platform string-to-number (base 10 is required). */
|
||||||
|
int64_t forth_strtoll(const char* str, char** endptr, int base);
|
||||||
|
|
||||||
|
/* Portable character classification. */
|
||||||
|
static inline int forth_isspace(int c) {
|
||||||
|
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Portable string operations. */
|
||||||
|
static inline size_t forth_strlen(const char* s) {
|
||||||
|
size_t n = 0;
|
||||||
|
while (s[n]) n++;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void forth_memcpy(void* dst, const void* src, size_t n) {
|
||||||
|
char* d = dst;
|
||||||
|
const char* s = src;
|
||||||
|
while (n--) *d++ = *s++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int forth_strcmp(const char* a, const char* b) {
|
||||||
|
while (*a && *a == *b) { a++; b++; }
|
||||||
|
return (unsigned char)*a - (unsigned char)*b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Static memory sizes (override before including forth.h) */
|
||||||
|
#ifndef FORTH_DATA_STACK_SIZE
|
||||||
|
#define FORTH_DATA_STACK_SIZE 128
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_RET_STACK_SIZE
|
||||||
|
#define FORTH_RET_STACK_SIZE 128
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_COMPILE_STACK_SIZE
|
||||||
|
#define FORTH_COMPILE_STACK_SIZE 64
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_COMPILE_BUF_SIZE
|
||||||
|
#define FORTH_COMPILE_BUF_SIZE 256
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_USER_MEM_CELLS
|
||||||
|
#define FORTH_USER_MEM_CELLS (1024 * 1024)
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_MAX_WORDS
|
||||||
|
#define FORTH_MAX_WORDS 256
|
||||||
|
#endif
|
||||||
|
#ifndef FORTH_MAX_WORD_BODY_CELLS
|
||||||
|
#define FORTH_MAX_WORD_BODY_CELLS 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
#define F_IMMEDIATE (1 << 7)
|
#define F_IMMEDIATE (1 << 7)
|
||||||
#define F_HIDDEN (1 << 6)
|
#define F_HIDDEN (1 << 6)
|
||||||
@@ -69,6 +122,9 @@ extern Word* w_branch;
|
|||||||
extern Word* w_zbranch;
|
extern Word* w_zbranch;
|
||||||
extern Word* w_dot_quote_inner;
|
extern Word* w_dot_quote_inner;
|
||||||
|
|
||||||
|
// Entry point
|
||||||
|
void forth_run(void);
|
||||||
|
|
||||||
// Core function prototypes
|
// Core function prototypes
|
||||||
void data_push(int64_t val);
|
void data_push(int64_t val);
|
||||||
int64_t data_pop(void);
|
int64_t data_pop(void);
|
||||||
@@ -88,8 +144,11 @@ void inner_interpreter(void);
|
|||||||
void process_token(const char* token);
|
void process_token(const char* token);
|
||||||
void outer_interpreter(void);
|
void outer_interpreter(void);
|
||||||
|
|
||||||
// Ensure compile_buf has at least 'needed' free cells
|
// Ensure compile_buf has at least 'needed' free cells (returns 0 on overflow)
|
||||||
void ensure_compile_cap(int32_t needed);
|
int ensure_compile_cap(int32_t needed);
|
||||||
|
|
||||||
|
Word* forth_alloc_word(void);
|
||||||
|
Cell* forth_alloc_body(int32_t cells);
|
||||||
|
|
||||||
// Stack ops
|
// Stack ops
|
||||||
void do_dup(Word* w);
|
void do_dup(Word* w);
|
||||||
@@ -145,6 +204,14 @@ void do_dot_quote(Word* w);
|
|||||||
void do_dot_quote_inner(Word* w);
|
void do_dot_quote_inner(Word* w);
|
||||||
void do_words(Word* w);
|
void do_words(Word* w);
|
||||||
|
|
||||||
|
// Framebuffer
|
||||||
|
void do_fb_addr(Word* w);
|
||||||
|
void do_fb_width(Word* w);
|
||||||
|
void do_fb_height(Word* w);
|
||||||
|
void do_fb_pitch(Word* w);
|
||||||
|
void do_fb_bpp(Word* w);
|
||||||
|
void do_fb_plot(Word* w);
|
||||||
|
|
||||||
// Memory
|
// Memory
|
||||||
void do_fetch(Word* w);
|
void do_fetch(Word* w);
|
||||||
void do_store(Word* w);
|
void do_store(Word* w);
|
||||||
@@ -157,6 +224,7 @@ void do_do_var(Word* w);
|
|||||||
void do_do_const(Word* w);
|
void do_do_const(Word* w);
|
||||||
void do_here(Word* w);
|
void do_here(Word* w);
|
||||||
void do_allot(Word* w);
|
void do_allot(Word* w);
|
||||||
|
void do_comma(Word* w);
|
||||||
|
|
||||||
// Return stack
|
// Return stack
|
||||||
void do_to_r(Word* w);
|
void do_to_r(Word* w);
|
||||||
|
|||||||
+81
-53
@@ -1,34 +1,61 @@
|
|||||||
#include "forth.h"
|
#include "forth.h"
|
||||||
|
|
||||||
// Dynamic storage
|
// Static storage for bare-metal portability
|
||||||
int64_t *data_stack = NULL;
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
|
static int64_t data_stack_storage[FORTH_DATA_STACK_SIZE];
|
||||||
|
int64_t *data_stack = data_stack_storage;
|
||||||
|
#else
|
||||||
|
int64_t *data_stack;
|
||||||
|
#endif
|
||||||
int32_t data_sp = -1;
|
int32_t data_sp = -1;
|
||||||
int32_t data_cap = 0;
|
int32_t data_cap = FORTH_DATA_STACK_SIZE;
|
||||||
|
|
||||||
Cell *ret_stack = NULL;
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
|
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
|
||||||
|
Cell *ret_stack = ret_stack_storage;
|
||||||
|
#else
|
||||||
|
Cell *ret_stack;
|
||||||
|
#endif
|
||||||
int32_t rp = -1;
|
int32_t rp = -1;
|
||||||
int32_t ret_cap = 0;
|
int32_t ret_cap = FORTH_RET_STACK_SIZE;
|
||||||
Cell* ip = NULL;
|
Cell* ip = NULL;
|
||||||
|
|
||||||
Word* dict_head = NULL;
|
Word* dict_head = NULL;
|
||||||
|
|
||||||
int state = 0;
|
int state = 0;
|
||||||
Cell *compile_buf = NULL;
|
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
|
static Cell compile_buf_storage[FORTH_COMPILE_BUF_SIZE];
|
||||||
|
Cell *compile_buf = compile_buf_storage;
|
||||||
|
#else
|
||||||
|
Cell *compile_buf;
|
||||||
|
#endif
|
||||||
int32_t compile_idx = 0;
|
int32_t compile_idx = 0;
|
||||||
int32_t compile_cap = 0;
|
int32_t compile_cap = FORTH_COMPILE_BUF_SIZE;
|
||||||
char compiling_name[MAX_NAME_LEN + 1] = {0};
|
char compiling_name[MAX_NAME_LEN + 1] = {0};
|
||||||
|
|
||||||
char* input_buf = NULL;
|
char* input_buf = NULL;
|
||||||
size_t input_buf_cap = 0;
|
size_t input_buf_cap = 0;
|
||||||
char* input_ptr = NULL;
|
char* input_ptr = NULL;
|
||||||
|
|
||||||
int64_t *compile_stack = NULL;
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
|
static int64_t compile_stack_storage[FORTH_COMPILE_STACK_SIZE];
|
||||||
|
int64_t *compile_stack = compile_stack_storage;
|
||||||
|
#else
|
||||||
|
int64_t *compile_stack;
|
||||||
|
#endif
|
||||||
int32_t compile_sp = -1;
|
int32_t compile_sp = -1;
|
||||||
int32_t compile_stack_cap = 0;
|
int32_t compile_stack_cap = FORTH_COMPILE_STACK_SIZE;
|
||||||
|
|
||||||
Cell *user_mem = NULL;
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
int64_t user_mem_size = 0;
|
static Cell user_mem_storage[FORTH_USER_MEM_CELLS];
|
||||||
Cell* here = NULL;
|
Cell *user_mem = user_mem_storage;
|
||||||
|
Cell* here = user_mem_storage;
|
||||||
|
#else
|
||||||
|
Cell *user_mem;
|
||||||
|
Cell* here;
|
||||||
|
#endif
|
||||||
|
int64_t user_mem_size = FORTH_USER_MEM_CELLS;
|
||||||
|
|
||||||
Word* w_exit = NULL;
|
Word* w_exit = NULL;
|
||||||
Word* w_docolon = NULL;
|
Word* w_docolon = NULL;
|
||||||
@@ -37,93 +64,94 @@ Word* w_branch = NULL;
|
|||||||
Word* w_zbranch = NULL;
|
Word* w_zbranch = NULL;
|
||||||
Word* w_dot_quote_inner = NULL;
|
Word* w_dot_quote_inner = NULL;
|
||||||
|
|
||||||
|
static Word word_pool[FORTH_MAX_WORDS];
|
||||||
|
static int32_t word_pool_used = 0;
|
||||||
|
|
||||||
|
static Cell word_body_storage[FORTH_MAX_WORD_BODY_CELLS];
|
||||||
|
static int32_t word_body_used = 0;
|
||||||
|
|
||||||
|
Word* forth_alloc_word(void) {
|
||||||
|
if (word_pool_used >= FORTH_MAX_WORDS) {
|
||||||
|
forth_printf("Dictionary full\n");
|
||||||
|
forth_panic();
|
||||||
|
}
|
||||||
|
return &word_pool[word_pool_used++];
|
||||||
|
}
|
||||||
|
|
||||||
|
Cell* forth_alloc_body(int32_t cells) {
|
||||||
|
if (word_body_used + cells > FORTH_MAX_WORD_BODY_CELLS) {
|
||||||
|
forth_printf("Word body pool exhausted\n");
|
||||||
|
forth_panic();
|
||||||
|
}
|
||||||
|
Cell* p = &word_body_storage[word_body_used];
|
||||||
|
word_body_used += cells;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Data stack ----------
|
// ---------- Data stack ----------
|
||||||
void data_push(int64_t val) {
|
void data_push(int64_t val) {
|
||||||
if (data_sp + 1 >= data_cap) {
|
if (data_sp + 1 >= data_cap) {
|
||||||
int32_t new_cap = data_cap ? data_cap * 2 : 128;
|
forth_printf("Data stack overflow\n");
|
||||||
int64_t *tmp = realloc(data_stack, new_cap * sizeof(int64_t));
|
return;
|
||||||
if (!tmp) {
|
|
||||||
fprintf(stderr, "Out of memory\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
data_stack = tmp;
|
|
||||||
data_cap = new_cap;
|
|
||||||
}
|
}
|
||||||
data_stack[++data_sp] = val;
|
data_stack[++data_sp] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t data_pop(void) {
|
int64_t data_pop(void) {
|
||||||
if (data_sp < 0) {
|
if (data_sp < 0) {
|
||||||
fprintf(stderr, "Data stack underflow\n");
|
forth_printf("Data stack underflow\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return data_stack[data_sp--];
|
return data_stack[data_sp--];
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- Return stack (holds Cell values) ----------
|
// ---------- Return stack (holds Cell values) ----------
|
||||||
static void ensure_ret_stack(void) {
|
|
||||||
if (rp + 1 >= ret_cap) {
|
|
||||||
int32_t new_cap = ret_cap ? ret_cap * 2 : 128;
|
|
||||||
Cell *tmp = realloc(ret_stack, new_cap * sizeof(Cell));
|
|
||||||
if (!tmp) {
|
|
||||||
fprintf(stderr, "Out of memory\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
ret_stack = tmp;
|
|
||||||
ret_cap = new_cap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ret_push_ip(Cell* val) {
|
void ret_push_ip(Cell* val) {
|
||||||
ensure_ret_stack();
|
if (rp + 1 >= ret_cap) {
|
||||||
|
forth_printf("Return stack overflow\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
rp++;
|
rp++;
|
||||||
ret_stack[rp].ptr = val; // store pointer in the 'ptr' member
|
ret_stack[rp].ptr = val; // store pointer in the 'ptr' member
|
||||||
}
|
}
|
||||||
|
|
||||||
Cell* ret_pop_ip(void) {
|
Cell* ret_pop_ip(void) {
|
||||||
if (rp < 0) {
|
if (rp < 0) {
|
||||||
fprintf(stderr, "Return stack underflow\n");
|
forth_printf("Return stack underflow\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return ret_stack[rp--].ptr;
|
return ret_stack[rp--].ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ret_push_num(int64_t val) {
|
void ret_push_num(int64_t val) {
|
||||||
ensure_ret_stack();
|
if (rp + 1 >= ret_cap) {
|
||||||
|
forth_printf("Return stack overflow\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
rp++;
|
rp++;
|
||||||
ret_stack[rp].num = val;
|
ret_stack[rp].num = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t ret_pop_num(void) {
|
int64_t ret_pop_num(void) {
|
||||||
if (rp < 0) {
|
if (rp < 0) {
|
||||||
fprintf(stderr, "Return stack underflow (num)\n");
|
forth_printf("Return stack underflow (num)\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return ret_stack[rp--].num;
|
return ret_stack[rp--].num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- Compile stack (indices) ----------
|
// ---------- Compile stack (indices) ----------
|
||||||
static void ensure_compile_stack(void) {
|
|
||||||
if (compile_sp + 1 >= compile_stack_cap) {
|
|
||||||
int32_t new_cap = compile_stack_cap ? compile_stack_cap * 2 : 64;
|
|
||||||
int64_t *tmp = realloc(compile_stack, new_cap * sizeof(int64_t));
|
|
||||||
if (!tmp) {
|
|
||||||
fprintf(stderr, "Out of memory\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
compile_stack = tmp;
|
|
||||||
compile_stack_cap = new_cap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void compile_push(int64_t idx) {
|
void compile_push(int64_t idx) {
|
||||||
ensure_compile_stack();
|
if (compile_sp + 1 >= compile_stack_cap) {
|
||||||
|
forth_printf("Compile stack overflow\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
compile_stack[++compile_sp] = idx;
|
compile_stack[++compile_sp] = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t compile_pop(void) {
|
int64_t compile_pop(void) {
|
||||||
if (compile_sp < 0) {
|
if (compile_sp < 0) {
|
||||||
fprintf(stderr, "Compile stack underflow\n");
|
forth_printf("Compile stack underflow\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return compile_stack[compile_sp--];
|
return compile_stack[compile_sp--];
|
||||||
|
|||||||
+5
-9
@@ -1,18 +1,14 @@
|
|||||||
#include "forth.h"
|
#include "forth.h"
|
||||||
|
|
||||||
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags) {
|
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags) {
|
||||||
Word* w = malloc(sizeof(Word));
|
Word* w = forth_alloc_word();
|
||||||
if (!w) {
|
|
||||||
fprintf(stderr, "Out of memory\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
w->prev = dict_head;
|
w->prev = dict_head;
|
||||||
dict_head = w;
|
dict_head = w;
|
||||||
|
|
||||||
size_t len = strlen(name);
|
size_t len = forth_strlen(name);
|
||||||
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
||||||
w->flags = flags | (uint8_t)len;
|
w->flags = flags | (uint8_t)len;
|
||||||
memcpy(w->name, name, len);
|
forth_memcpy(w->name, name, len);
|
||||||
w->name[len] = '\0';
|
w->name[len] = '\0';
|
||||||
w->code = code;
|
w->code = code;
|
||||||
w->body = NULL;
|
w->body = NULL;
|
||||||
@@ -22,14 +18,14 @@ Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags) {
|
|||||||
Word* lookup_word(const char* name) {
|
Word* lookup_word(const char* name) {
|
||||||
for (Word* w = dict_head; w != NULL; w = w->prev) {
|
for (Word* w = dict_head; w != NULL; w = w->prev) {
|
||||||
if (w->flags & (1 << 6)) continue;
|
if (w->flags & (1 << 6)) continue;
|
||||||
if (strcmp(w->name, name) == 0) return w;
|
if (forth_strcmp(w->name, name) == 0) return w;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Word* lookup_word_internal(const char* name) {
|
Word* lookup_word_internal(const char* name) {
|
||||||
for (Word* w = dict_head; w != NULL; w = w->prev) {
|
for (Word* w = dict_head; w != NULL; w = w->prev) {
|
||||||
if (strcmp(w->name, name) == 0) return w;
|
if (forth_strcmp(w->name, name) == 0) return w;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-26
@@ -3,12 +3,12 @@
|
|||||||
// Input tokenizer (unchanged)
|
// Input tokenizer (unchanged)
|
||||||
char* next_token(void) {
|
char* next_token(void) {
|
||||||
if (input_ptr == NULL) return NULL;
|
if (input_ptr == NULL) return NULL;
|
||||||
while (*input_ptr != '\0' && isspace((unsigned char)*input_ptr)) {
|
while (*input_ptr != '\0' && forth_isspace((unsigned char)*input_ptr)) {
|
||||||
input_ptr++;
|
input_ptr++;
|
||||||
}
|
}
|
||||||
if (*input_ptr == '\0') return NULL;
|
if (*input_ptr == '\0') return NULL;
|
||||||
char* start = input_ptr;
|
char* start = input_ptr;
|
||||||
while (*input_ptr != '\0' && !isspace((unsigned char)*input_ptr)) {
|
while (*input_ptr != '\0' && !forth_isspace((unsigned char)*input_ptr)) {
|
||||||
input_ptr++;
|
input_ptr++;
|
||||||
}
|
}
|
||||||
if (*input_ptr != '\0') {
|
if (*input_ptr != '\0') {
|
||||||
@@ -19,17 +19,12 @@ char* next_token(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure compile_buf has room for at least 'needed' more cells
|
// Ensure compile_buf has room for at least 'needed' more cells
|
||||||
void ensure_compile_cap(int32_t needed) {
|
int ensure_compile_cap(int32_t needed) {
|
||||||
while (compile_idx + needed > compile_cap) {
|
if (compile_idx + needed > compile_cap) {
|
||||||
int32_t new_cap = compile_cap ? compile_cap * 2 : 256;
|
forth_printf("Compile buffer overflow\n");
|
||||||
Cell *tmp = realloc(compile_buf, new_cap * sizeof(Cell));
|
return 0;
|
||||||
if (!tmp) {
|
|
||||||
fprintf(stderr, "Out of memory\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
compile_buf = tmp;
|
|
||||||
compile_cap = new_cap;
|
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inner interpreter (unchanged)
|
// Inner interpreter (unchanged)
|
||||||
@@ -62,47 +57,56 @@ void process_token(const char* token) {
|
|||||||
w->code(w);
|
w->code(w);
|
||||||
}
|
}
|
||||||
} else { // Compile normal word
|
} else { // Compile normal word
|
||||||
ensure_compile_cap(1);
|
if (!ensure_compile_cap(1)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w};
|
compile_buf[compile_idx++] = (Cell){.word = w};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { // Try to parse as number
|
} else { // Try to parse as number
|
||||||
char* end;
|
char* end;
|
||||||
long long v = strtoll(token, &end, 10);
|
int64_t v = forth_strtoll(token, &end, 10);
|
||||||
if (end != token && *end == '\0') {
|
if (end != token && *end == '\0') {
|
||||||
if (state == 0) {
|
if (state == 0) {
|
||||||
data_push((int64_t)v);
|
data_push(v);
|
||||||
} else { // Compile lit + number
|
} else { // Compile lit + number
|
||||||
if (!w_lit) {
|
if (!w_lit) {
|
||||||
fprintf(stderr, "Fatal: lit word not found\n");
|
forth_printf("Fatal: lit word not found\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_lit};
|
compile_buf[compile_idx++] = (Cell){.word = w_lit};
|
||||||
compile_buf[compile_idx++] = (Cell){.num = (int64_t)v};
|
compile_buf[compile_idx++] = (Cell){.num = (int64_t)v};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Unknown word: '%s'\n", token);
|
forth_printf("Unknown word: '%s'\n", token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void outer_interpreter(void) {
|
void outer_interpreter(void) {
|
||||||
|
static char line_buf[256];
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("ok ");
|
forth_printf("ok ");
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
ssize_t n = getline(&input_buf, &input_buf_cap, stdin);
|
size_t i = 0;
|
||||||
if (n < 0) {
|
int c;
|
||||||
|
while (i < sizeof(line_buf) - 1) {
|
||||||
|
c = forth_getchar();
|
||||||
|
if (c == FORTH_EOF || c == '\n' || c == '\r') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
line_buf[i++] = (char)c;
|
||||||
|
}
|
||||||
|
line_buf[i] = '\0';
|
||||||
|
if (i == 0 && c == FORTH_EOF) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (n > 0 && input_buf[n - 1] == '\n') {
|
input_buf = line_buf;
|
||||||
input_buf[n - 1] = '\0';
|
input_buf_cap = sizeof(line_buf);
|
||||||
}
|
|
||||||
input_ptr = input_buf;
|
input_ptr = input_buf;
|
||||||
char* tok;
|
char* tok;
|
||||||
while ((tok = next_token()) != NULL) {
|
while ((tok = next_token()) != NULL) {
|
||||||
process_token(tok);
|
process_token(tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("\n");
|
forth_printf("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
+93
-81
@@ -98,7 +98,7 @@ void do_div(Word* w) {
|
|||||||
int64_t b = data_pop();
|
int64_t b = data_pop();
|
||||||
int64_t a = data_pop();
|
int64_t a = data_pop();
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
fprintf(stderr, "Division by zero\n");
|
forth_printf("Division by zero\n");
|
||||||
data_push(a);
|
data_push(a);
|
||||||
data_push(b);
|
data_push(b);
|
||||||
return;
|
return;
|
||||||
@@ -112,7 +112,7 @@ void do_mod(Word* w) {
|
|||||||
int64_t b = data_pop();
|
int64_t b = data_pop();
|
||||||
int64_t a = data_pop();
|
int64_t a = data_pop();
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
fprintf(stderr, "Modulo by zero\n");
|
forth_printf("Modulo by zero\n");
|
||||||
data_push(a);
|
data_push(a);
|
||||||
data_push(b);
|
data_push(b);
|
||||||
return;
|
return;
|
||||||
@@ -126,7 +126,7 @@ void do_slash_mod(Word* w) {
|
|||||||
int64_t b = data_pop();
|
int64_t b = data_pop();
|
||||||
int64_t a = data_pop();
|
int64_t a = data_pop();
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
fprintf(stderr, "Modulo by zero\n");
|
forth_printf("Modulo by zero\n");
|
||||||
data_push(a);
|
data_push(a);
|
||||||
data_push(b);
|
data_push(b);
|
||||||
return;
|
return;
|
||||||
@@ -309,55 +309,55 @@ void do_zero_gt(Word* w) {
|
|||||||
void do_dot(Word* w) {
|
void do_dot(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (data_sp < 0) return;
|
if (data_sp < 0) return;
|
||||||
printf("%" PRId64 " ", data_pop());
|
forth_printf("%lld ", (long long)data_pop());
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_cr(Word* w) {
|
void do_cr(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
printf("\n");
|
forth_printf("\n");
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_emit(Word* w) {
|
void do_emit(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (data_sp < 0) return;
|
if (data_sp < 0) return;
|
||||||
putchar((char)data_pop());
|
forth_putchar((char)data_pop());
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_key(Word* w) {
|
void do_key(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
int c = getchar();
|
int c = forth_getchar();
|
||||||
data_push(c == EOF ? -1 : c);
|
data_push(c == FORTH_EOF ? -1 : c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_dot_quote(Word* w) {
|
void do_dot_quote(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state == 0) {
|
if (state == 0) {
|
||||||
// Interpret mode: print immediately
|
// Interpret mode: print immediately
|
||||||
if (input_ptr == NULL) { fprintf(stderr, "Missing string\n"); return; }
|
if (input_ptr == NULL) { forth_printf("Missing string\n"); return; }
|
||||||
while (*input_ptr && isspace((unsigned char)*input_ptr)) input_ptr++;
|
while (*input_ptr && forth_isspace((unsigned char)*input_ptr)) input_ptr++;
|
||||||
if (*input_ptr != '"') { fprintf(stderr, "Expected \" to start string\n"); return; }
|
if (*input_ptr != '"') { forth_printf("Expected \" to start string\n"); return; }
|
||||||
input_ptr++;
|
input_ptr++;
|
||||||
char* start = input_ptr;
|
char* start = input_ptr;
|
||||||
while (*input_ptr && *input_ptr != '"') input_ptr++;
|
while (*input_ptr && *input_ptr != '"') input_ptr++;
|
||||||
if (*input_ptr != '"') { fprintf(stderr, "Unterminated string\n"); return; }
|
if (*input_ptr != '"') { forth_printf("Unterminated string\n"); return; }
|
||||||
while (start < input_ptr) putchar(*start++);
|
while (start < input_ptr) forth_putchar(*start++);
|
||||||
input_ptr++;
|
input_ptr++;
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
} else {
|
} else {
|
||||||
// Compile mode: compile string for runtime
|
// Compile mode: compile string for runtime
|
||||||
if (input_ptr == NULL) { fprintf(stderr, "Missing string\n"); return; }
|
if (input_ptr == NULL) { forth_printf("Missing string\n"); return; }
|
||||||
while (*input_ptr && isspace((unsigned char)*input_ptr)) input_ptr++;
|
while (*input_ptr && forth_isspace((unsigned char)*input_ptr)) input_ptr++;
|
||||||
if (*input_ptr != '"') { fprintf(stderr, "Expected \" to start string\n"); return; }
|
if (*input_ptr != '"') { forth_printf("Expected \" to start string\n"); return; }
|
||||||
input_ptr++;
|
input_ptr++;
|
||||||
char* start = input_ptr;
|
char* start = input_ptr;
|
||||||
while (*input_ptr && *input_ptr != '"') input_ptr++;
|
while (*input_ptr && *input_ptr != '"') input_ptr++;
|
||||||
if (*input_ptr != '"') { fprintf(stderr, "Unterminated string\n"); return; }
|
if (*input_ptr != '"') { forth_printf("Unterminated string\n"); return; }
|
||||||
size_t len = input_ptr - start;
|
size_t len = input_ptr - start;
|
||||||
if (!w_dot_quote_inner) { fprintf(stderr, "Fatal: do_dot_quote_inner not found\n"); return; }
|
if (!w_dot_quote_inner) { forth_printf("Fatal: do_dot_quote_inner not found\n"); return; }
|
||||||
ensure_compile_cap(2 + (int32_t)len);
|
if (!ensure_compile_cap(2 + (int32_t)len)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_dot_quote_inner};
|
compile_buf[compile_idx++] = (Cell){.word = w_dot_quote_inner};
|
||||||
compile_buf[compile_idx++] = (Cell){.num = (int64_t)len};
|
compile_buf[compile_idx++] = (Cell){.num = (int64_t)len};
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
@@ -372,21 +372,21 @@ void do_dot_quote_inner(Word* w) {
|
|||||||
int64_t len = ip->num;
|
int64_t len = ip->num;
|
||||||
ip++;
|
ip++;
|
||||||
for (int64_t i = 0; i < len; i++) {
|
for (int64_t i = 0; i < len; i++) {
|
||||||
putchar((char)ip->num);
|
forth_putchar((char)ip->num);
|
||||||
ip++;
|
ip++;
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_words(Word* w) {
|
void do_words(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
printf("Dictionary words:\n");
|
forth_printf("Dictionary words:\n");
|
||||||
for (Word* cur = dict_head; cur != NULL; cur = cur->prev) {
|
for (Word* cur = dict_head; cur != NULL; cur = cur->prev) {
|
||||||
if (cur->flags & (1 << 6)) continue;
|
if (cur->flags & (1 << 6)) continue;
|
||||||
printf("%s ", cur->name);
|
forth_printf("%s ", cur->name);
|
||||||
}
|
}
|
||||||
printf("\n");
|
forth_printf("\n");
|
||||||
fflush(stdout);
|
forth_fflush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory operations
|
// Memory operations
|
||||||
@@ -394,7 +394,7 @@ void do_fetch(Word* w) {
|
|||||||
(void)w;
|
(void)w;
|
||||||
int64_t addr = data_pop();
|
int64_t addr = data_pop();
|
||||||
if (addr < 0 || addr >= user_mem_size) {
|
if (addr < 0 || addr >= user_mem_size) {
|
||||||
fprintf(stderr, "Address out of bounds\n");
|
forth_printf("Address out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
data_push(user_mem[addr].num);
|
data_push(user_mem[addr].num);
|
||||||
@@ -405,7 +405,7 @@ void do_store(Word* w) {
|
|||||||
int64_t addr = data_pop();
|
int64_t addr = data_pop();
|
||||||
int64_t val = data_pop();
|
int64_t val = data_pop();
|
||||||
if (addr < 0 || addr >= user_mem_size) {
|
if (addr < 0 || addr >= user_mem_size) {
|
||||||
fprintf(stderr, "Address out of bounds\n");
|
forth_printf("Address out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user_mem[addr].num = val;
|
user_mem[addr].num = val;
|
||||||
@@ -416,7 +416,7 @@ void do_plus_store(Word* w) {
|
|||||||
int64_t addr = data_pop();
|
int64_t addr = data_pop();
|
||||||
int64_t val = data_pop();
|
int64_t val = data_pop();
|
||||||
if (addr < 0 || addr >= user_mem_size) {
|
if (addr < 0 || addr >= user_mem_size) {
|
||||||
fprintf(stderr, "Address out of bounds\n");
|
forth_printf("Address out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user_mem[addr].num += val;
|
user_mem[addr].num += val;
|
||||||
@@ -427,7 +427,7 @@ void do_cfetch(Word* w) {
|
|||||||
int64_t addr = data_pop(); // byte offset
|
int64_t addr = data_pop(); // byte offset
|
||||||
int64_t max_byte = user_mem_size * (int64_t)sizeof(Cell);
|
int64_t max_byte = user_mem_size * (int64_t)sizeof(Cell);
|
||||||
if (addr < 0 || addr >= max_byte) {
|
if (addr < 0 || addr >= max_byte) {
|
||||||
fprintf(stderr, "Address out of bounds\n");
|
forth_printf("Address out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t* base = (uint8_t*)user_mem;
|
uint8_t* base = (uint8_t*)user_mem;
|
||||||
@@ -440,7 +440,7 @@ void do_cstore(Word* w) {
|
|||||||
int64_t val = data_pop();
|
int64_t val = data_pop();
|
||||||
int64_t max_byte = user_mem_size * (int64_t)sizeof(Cell);
|
int64_t max_byte = user_mem_size * (int64_t)sizeof(Cell);
|
||||||
if (addr < 0 || addr >= max_byte) {
|
if (addr < 0 || addr >= max_byte) {
|
||||||
fprintf(stderr, "Address out of bounds\n");
|
forth_printf("Address out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t* base = (uint8_t*)user_mem;
|
uint8_t* base = (uint8_t*)user_mem;
|
||||||
@@ -456,22 +456,34 @@ void do_here(Word* w) {
|
|||||||
void do_allot(Word* w) {
|
void do_allot(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
int64_t n = data_pop();
|
int64_t n = data_pop();
|
||||||
if (here + n > user_mem + user_mem_size) {
|
int64_t idx = here - user_mem;
|
||||||
fprintf(stderr, "User memory overflow\n");
|
if (idx + n < 0 || idx + n > user_mem_size) {
|
||||||
|
forth_printf("User memory out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
here += n;
|
here += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_comma(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
int64_t val = data_pop();
|
||||||
|
if (here + 1 > user_mem + user_mem_size) {
|
||||||
|
forth_printf("User memory overflow\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
here->num = val;
|
||||||
|
here++;
|
||||||
|
}
|
||||||
|
|
||||||
// Variable and constant
|
// Variable and constant
|
||||||
void do_variable(Word* w) {
|
void do_variable(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
char* name = next_token();
|
char* name = next_token();
|
||||||
if (!name) { fprintf(stderr, "VARIABLE expects a name\n"); return; }
|
if (!name) { forth_printf("VARIABLE expects a name\n"); return; }
|
||||||
|
|
||||||
// allocate one cell in user memory for the variable's data
|
// allocate one cell in user memory for the variable's data
|
||||||
if (here + 1 > user_mem + user_mem_size) {
|
if (here + 1 > user_mem + user_mem_size) {
|
||||||
fprintf(stderr, "User memory overflow\n");
|
forth_printf("User memory overflow\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Cell* var_cell = here; // address of the data cell
|
Cell* var_cell = here; // address of the data cell
|
||||||
@@ -479,15 +491,14 @@ void do_variable(Word* w) {
|
|||||||
here++;
|
here++;
|
||||||
|
|
||||||
// create dictionary entry
|
// create dictionary entry
|
||||||
Word* new_w = malloc(sizeof(Word));
|
Word* new_w = forth_alloc_word();
|
||||||
if (!new_w) { printf("Out of memory\n"); exit(1); }
|
|
||||||
new_w->prev = dict_head;
|
new_w->prev = dict_head;
|
||||||
dict_head = new_w;
|
dict_head = new_w;
|
||||||
|
|
||||||
size_t len = strlen(name);
|
size_t len = forth_strlen(name);
|
||||||
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
||||||
new_w->flags = (uint8_t)len;
|
new_w->flags = (uint8_t)len;
|
||||||
memcpy(new_w->name, name, len);
|
forth_memcpy(new_w->name, name, len);
|
||||||
new_w->name[len] = '\0';
|
new_w->name[len] = '\0';
|
||||||
new_w->code = do_do_var;
|
new_w->code = do_do_var;
|
||||||
new_w->body = var_cell; // body points directly to the data cell in user_mem
|
new_w->body = var_cell; // body points directly to the data cell in user_mem
|
||||||
@@ -497,11 +508,11 @@ void do_constant(Word* w) {
|
|||||||
(void)w;
|
(void)w;
|
||||||
int64_t val = data_pop();
|
int64_t val = data_pop();
|
||||||
char* name = next_token();
|
char* name = next_token();
|
||||||
if (!name) { fprintf(stderr, "CONSTANT expects a name\n"); data_push(val); return; }
|
if (!name) { forth_printf("CONSTANT expects a name\n"); data_push(val); return; }
|
||||||
|
|
||||||
// allocate a cell in user memory to hold the constant value
|
// allocate a cell in user memory to hold the constant value
|
||||||
if (here + 1 > user_mem + user_mem_size) {
|
if (here + 1 > user_mem + user_mem_size) {
|
||||||
fprintf(stderr, "User memory overflow\n");
|
forth_printf("User memory overflow\n");
|
||||||
data_push(val); // restore the value (optional)
|
data_push(val); // restore the value (optional)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -509,15 +520,14 @@ void do_constant(Word* w) {
|
|||||||
val_cell->num = val;
|
val_cell->num = val;
|
||||||
here++;
|
here++;
|
||||||
|
|
||||||
Word* new_w = malloc(sizeof(Word));
|
Word* new_w = forth_alloc_word();
|
||||||
if (!new_w) { printf("Out of memory\n"); exit(1); }
|
|
||||||
new_w->prev = dict_head;
|
new_w->prev = dict_head;
|
||||||
dict_head = new_w;
|
dict_head = new_w;
|
||||||
|
|
||||||
size_t len = strlen(name);
|
size_t len = forth_strlen(name);
|
||||||
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
||||||
new_w->flags = (uint8_t)len;
|
new_w->flags = (uint8_t)len;
|
||||||
memcpy(new_w->name, name, len);
|
forth_memcpy(new_w->name, name, len);
|
||||||
new_w->name[len] = '\0';
|
new_w->name[len] = '\0';
|
||||||
new_w->code = do_do_const;
|
new_w->code = do_do_const;
|
||||||
new_w->body = val_cell; // body points to the cell that holds the value
|
new_w->body = val_cell; // body points to the cell that holds the value
|
||||||
@@ -547,7 +557,7 @@ void do_r_from(Word* w) {
|
|||||||
|
|
||||||
void do_r_fetch(Word* w) {
|
void do_r_fetch(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (rp < 0) { fprintf(stderr, "Return stack underflow\n"); return; }
|
if (rp < 0) { forth_printf("Return stack underflow\n"); return; }
|
||||||
data_push(ret_stack[rp].num);
|
data_push(ret_stack[rp].num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,10 +582,10 @@ void do_lit(Word* w) {
|
|||||||
void do_colon(Word* w) {
|
void do_colon(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
char* name = next_token();
|
char* name = next_token();
|
||||||
if (!name) { fprintf(stderr, "':' expects a name\n"); return; }
|
if (!name) { forth_printf("':' expects a name\n"); return; }
|
||||||
size_t len = strlen(name);
|
size_t len = forth_strlen(name);
|
||||||
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
||||||
memcpy(compiling_name, name, len);
|
forth_memcpy(compiling_name, name, len);
|
||||||
compiling_name[len] = '\0';
|
compiling_name[len] = '\0';
|
||||||
state = 1;
|
state = 1;
|
||||||
compile_idx = 0;
|
compile_idx = 0;
|
||||||
@@ -584,27 +594,27 @@ void do_colon(Word* w) {
|
|||||||
|
|
||||||
void do_semicolon(Word* w) {
|
void do_semicolon(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "';' only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("';' only valid in compile mode\n"); return; }
|
||||||
if (!w_exit) { fprintf(stderr, "Fatal: exit word not found\n"); return; }
|
if (!w_exit) { forth_printf("Fatal: exit word not found\n"); return; }
|
||||||
|
|
||||||
ensure_compile_cap(1);
|
if (!ensure_compile_cap(1)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_exit};
|
compile_buf[compile_idx++] = (Cell){.word = w_exit};
|
||||||
|
|
||||||
// Create body copy of compiled cells
|
// Create body copy of compiled cells
|
||||||
Cell* body_copy = malloc(compile_idx * sizeof(Cell));
|
Cell* body_copy = forth_alloc_body(compile_idx);
|
||||||
if (!body_copy) { printf("Out of memory\n"); exit(1); }
|
if (!body_copy) { forth_printf("Out of memory\n"); return; }
|
||||||
memcpy(body_copy, compile_buf, compile_idx * sizeof(Cell));
|
forth_memcpy(body_copy, compile_buf, compile_idx * sizeof(Cell));
|
||||||
|
|
||||||
// Create new word entry
|
// Create new word entry
|
||||||
Word* new_w = malloc(sizeof(Word));
|
Word* new_w = forth_alloc_word();
|
||||||
if (!new_w) { printf("Out of memory\n"); free(body_copy); exit(1); }
|
if (!new_w) { forth_printf("Out of memory\n"); return; }
|
||||||
new_w->prev = dict_head;
|
new_w->prev = dict_head;
|
||||||
dict_head = new_w;
|
dict_head = new_w;
|
||||||
|
|
||||||
size_t len = strlen(compiling_name);
|
size_t len = forth_strlen(compiling_name);
|
||||||
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
|
||||||
new_w->flags = (uint8_t)len; // no hidden, no immediate
|
new_w->flags = (uint8_t)len; // no hidden, no immediate
|
||||||
memcpy(new_w->name, compiling_name, len);
|
forth_memcpy(new_w->name, compiling_name, len);
|
||||||
new_w->name[len] = '\0';
|
new_w->name[len] = '\0';
|
||||||
new_w->code = do_docolon;
|
new_w->code = do_docolon;
|
||||||
new_w->body = body_copy;
|
new_w->body = body_copy;
|
||||||
@@ -632,9 +642,9 @@ void do_zero_branch(Word* w) {
|
|||||||
// Control flow using compile stack (indices)
|
// Control flow using compile stack (indices)
|
||||||
void do_if(Word* w) {
|
void do_if(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "IF only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("IF only valid in compile mode\n"); return; }
|
||||||
if (!w_zbranch) { fprintf(stderr, "Fatal: 0branch not found\n"); return; }
|
if (!w_zbranch) { forth_printf("Fatal: 0branch not found\n"); return; }
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
||||||
// compile_push current index (where the offset will be placed)
|
// compile_push current index (where the offset will be placed)
|
||||||
compile_push(compile_idx);
|
compile_push(compile_idx);
|
||||||
@@ -643,7 +653,7 @@ void do_if(Word* w) {
|
|||||||
|
|
||||||
void do_then(Word* w) {
|
void do_then(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "THEN only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("THEN only valid in compile mode\n"); return; }
|
||||||
int64_t offset_idx = compile_pop();
|
int64_t offset_idx = compile_pop();
|
||||||
if (offset_idx < 0) return;
|
if (offset_idx < 0) return;
|
||||||
compile_buf[offset_idx].num = compile_idx - offset_idx;
|
compile_buf[offset_idx].num = compile_idx - offset_idx;
|
||||||
@@ -651,12 +661,12 @@ void do_then(Word* w) {
|
|||||||
|
|
||||||
void do_else(Word* w) {
|
void do_else(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "ELSE only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("ELSE only valid in compile mode\n"); return; }
|
||||||
int64_t if_offset_idx = compile_pop();
|
int64_t if_offset_idx = compile_pop();
|
||||||
if (if_offset_idx < 0) return;
|
if (if_offset_idx < 0) return;
|
||||||
|
|
||||||
if (!w_branch) { fprintf(stderr, "Fatal: branch not found\n"); return; }
|
if (!w_branch) { forth_printf("Fatal: branch not found\n"); return; }
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
|
|
||||||
// resolve IF offset to skip the ELSE branch
|
// resolve IF offset to skip the ELSE branch
|
||||||
compile_buf[if_offset_idx].num = (compile_idx + 2) - if_offset_idx;
|
compile_buf[if_offset_idx].num = (compile_idx + 2) - if_offset_idx;
|
||||||
@@ -669,30 +679,31 @@ void do_else(Word* w) {
|
|||||||
|
|
||||||
void do_begin(Word* w) {
|
void do_begin(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "BEGIN only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("BEGIN only valid in compile mode\n"); return; }
|
||||||
compile_push(compile_idx);
|
compile_push(compile_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_until(Word* w) {
|
void do_until(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "UNTIL only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("UNTIL only valid in compile mode\n"); return; }
|
||||||
int64_t begin_idx = compile_pop();
|
int64_t begin_idx = compile_pop();
|
||||||
if (begin_idx < 0) return;
|
if (begin_idx < 0) return;
|
||||||
|
|
||||||
if (!w_zbranch) { fprintf(stderr, "Fatal: 0branch not found\n"); return; }
|
if (!w_zbranch) { forth_printf("Fatal: 0branch not found\n"); return; }
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
||||||
compile_buf[compile_idx++] = (Cell){.num = begin_idx - compile_idx};
|
compile_buf[compile_idx] = (Cell){.num = begin_idx - compile_idx};
|
||||||
|
compile_idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_while(Word* w) {
|
void do_while(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "WHILE only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("WHILE only valid in compile mode\n"); return; }
|
||||||
int64_t begin_idx = compile_pop();
|
int64_t begin_idx = compile_pop();
|
||||||
if (begin_idx < 0) return;
|
if (begin_idx < 0) return;
|
||||||
|
|
||||||
if (!w_zbranch) { fprintf(stderr, "Fatal: 0branch not found\n"); return; }
|
if (!w_zbranch) { forth_printf("Fatal: 0branch not found\n"); return; }
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
compile_buf[compile_idx++] = (Cell){.word = w_zbranch};
|
||||||
int64_t while_offset_idx = compile_idx;
|
int64_t while_offset_idx = compile_idx;
|
||||||
compile_idx++; // reserve offset
|
compile_idx++; // reserve offset
|
||||||
@@ -703,16 +714,17 @@ void do_while(Word* w) {
|
|||||||
|
|
||||||
void do_repeat(Word* w) {
|
void do_repeat(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
if (state != 1) { fprintf(stderr, "REPEAT only valid in compile mode\n"); return; }
|
if (state != 1) { forth_printf("REPEAT only valid in compile mode\n"); return; }
|
||||||
int64_t begin_idx = compile_pop();
|
int64_t begin_idx = compile_pop();
|
||||||
if (begin_idx < 0) return;
|
if (begin_idx < 0) return;
|
||||||
int64_t while_offset_idx = compile_pop();
|
int64_t while_offset_idx = compile_pop();
|
||||||
if (while_offset_idx < 0) return;
|
if (while_offset_idx < 0) return;
|
||||||
|
|
||||||
if (!w_branch) { fprintf(stderr, "Fatal: branch not found\n"); return; }
|
if (!w_branch) { forth_printf("Fatal: branch not found\n"); return; }
|
||||||
ensure_compile_cap(2);
|
if (!ensure_compile_cap(2)) return;
|
||||||
compile_buf[compile_idx++] = (Cell){.word = w_branch};
|
compile_buf[compile_idx++] = (Cell){.word = w_branch};
|
||||||
compile_buf[compile_idx++] = (Cell){.num = begin_idx - compile_idx};
|
compile_buf[compile_idx] = (Cell){.num = begin_idx - compile_idx};
|
||||||
|
compile_idx++;
|
||||||
|
|
||||||
compile_buf[while_offset_idx].num = compile_idx - while_offset_idx;
|
compile_buf[while_offset_idx].num = compile_idx - while_offset_idx;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
ENTRY(_start)
|
||||||
|
|
||||||
|
SECTIONS
|
||||||
|
{
|
||||||
|
. = 0x100000;
|
||||||
|
|
||||||
|
.text : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(.multiboot2)
|
||||||
|
*(.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
.rodata : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(.rodata)
|
||||||
|
}
|
||||||
|
|
||||||
|
.data : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
.bss : ALIGN(4K)
|
||||||
|
{
|
||||||
|
__bss_start = .;
|
||||||
|
*(.bss)
|
||||||
|
*(COMMON)
|
||||||
|
__bss_end = .;
|
||||||
|
}
|
||||||
|
|
||||||
|
.note : ALIGN(4)
|
||||||
|
{
|
||||||
|
*(.note.Xen)
|
||||||
|
}
|
||||||
|
}
|
||||||
+184
@@ -0,0 +1,184 @@
|
|||||||
|
# Multiboot2 header and x86_64 long-mode bootstrap
|
||||||
|
|
||||||
|
.section .multiboot2, "a"
|
||||||
|
.align 8
|
||||||
|
multiboot_header:
|
||||||
|
.long 0xe85250d6
|
||||||
|
.long 0
|
||||||
|
.long multiboot_header_end - multiboot_header
|
||||||
|
.long -(0xe85250d6 + 0 + (multiboot_header_end - multiboot_header))
|
||||||
|
|
||||||
|
/* Framebuffer tag: type=5, size=20, width=height=0 (any), depth=32.
|
||||||
|
* Layout per multiboot2 spec:
|
||||||
|
* u16 type, u16 flags, u32 size, u32 width, u32 height, u32 depth
|
||||||
|
* Padded to 8-byte alignment. */
|
||||||
|
.align 8
|
||||||
|
.word 5
|
||||||
|
.word 0
|
||||||
|
.long 20
|
||||||
|
.long 0
|
||||||
|
.long 0
|
||||||
|
.long 32
|
||||||
|
.long 0 /* padding to 8-byte alignment */
|
||||||
|
|
||||||
|
.align 8
|
||||||
|
.word 0
|
||||||
|
.word 0
|
||||||
|
.long 8
|
||||||
|
multiboot_header_end:
|
||||||
|
|
||||||
|
.section .bss, "aw", @nobits
|
||||||
|
.align 4096
|
||||||
|
pml4:
|
||||||
|
.space 4096
|
||||||
|
pdpt:
|
||||||
|
.space 4096
|
||||||
|
pd:
|
||||||
|
.space 4096 * 4
|
||||||
|
|
||||||
|
.align 16
|
||||||
|
stack_bottom:
|
||||||
|
.space 16384
|
||||||
|
stack_top:
|
||||||
|
|
||||||
|
.align 8
|
||||||
|
mboot_info_ptr:
|
||||||
|
.space 8
|
||||||
|
|
||||||
|
.section .text
|
||||||
|
.code32
|
||||||
|
.global _start
|
||||||
|
.type _start, @function
|
||||||
|
_start:
|
||||||
|
movl $stack_top, %esp
|
||||||
|
movl %ebx, mboot_info_ptr
|
||||||
|
|
||||||
|
cld
|
||||||
|
movl $__bss_start, %edi
|
||||||
|
movl $__bss_end, %ecx
|
||||||
|
subl %edi, %ecx
|
||||||
|
xorl %eax, %eax
|
||||||
|
rep stosb
|
||||||
|
|
||||||
|
movl $pdpt, %eax
|
||||||
|
orl $0x03, %eax
|
||||||
|
movl %eax, pml4
|
||||||
|
|
||||||
|
/* Populate 4 PDPT entries, each pointing to one of 4 PDs.
|
||||||
|
* This identity-maps the first 4 GiB using 2 MiB pages,
|
||||||
|
* which covers typical framebuffer/MMIO regions. */
|
||||||
|
movl $pd, %eax
|
||||||
|
orl $0x03, %eax
|
||||||
|
movl %eax, pdpt + 0
|
||||||
|
addl $4096, %eax
|
||||||
|
movl %eax, pdpt + 8
|
||||||
|
addl $4096, %eax
|
||||||
|
movl %eax, pdpt + 16
|
||||||
|
addl $4096, %eax
|
||||||
|
movl %eax, pdpt + 24
|
||||||
|
|
||||||
|
movl $0x00000083, %eax
|
||||||
|
movl $pd, %edi
|
||||||
|
movl $2048, %ecx
|
||||||
|
1:
|
||||||
|
movl %eax, (%edi)
|
||||||
|
addl $0x200000, %eax
|
||||||
|
addl $8, %edi
|
||||||
|
loop 1b
|
||||||
|
|
||||||
|
movl $pml4, %eax
|
||||||
|
movl %eax, %cr3
|
||||||
|
|
||||||
|
movl %cr4, %eax
|
||||||
|
orl $0x20, %eax
|
||||||
|
movl %eax, %cr4
|
||||||
|
|
||||||
|
movl $0xC0000080, %ecx
|
||||||
|
rdmsr
|
||||||
|
orl $0x100, %eax
|
||||||
|
wrmsr
|
||||||
|
|
||||||
|
movl %cr0, %eax
|
||||||
|
orl $0x80000000, %eax
|
||||||
|
movl %eax, %cr0
|
||||||
|
|
||||||
|
lgdt gdt64_pointer
|
||||||
|
ljmp $0x08, $long_mode_start
|
||||||
|
|
||||||
|
.code64
|
||||||
|
long_mode_start:
|
||||||
|
/* Load data segment registers with the data descriptor (0x10).
|
||||||
|
* In long mode most of these are ignored for access checks, but
|
||||||
|
* %ss must be a valid (or null) selector so that iretq from an
|
||||||
|
* interrupt can safely reload it. */
|
||||||
|
movw $0x10, %ax
|
||||||
|
movw %ax, %ds
|
||||||
|
movw %ax, %es
|
||||||
|
movw %ax, %fs
|
||||||
|
movw %ax, %gs
|
||||||
|
movw %ax, %ss
|
||||||
|
|
||||||
|
movabsq $stack_top, %rax
|
||||||
|
movq %rax, %rsp
|
||||||
|
/* Ensure 16-byte alignment for the System V AMD64 ABI. */
|
||||||
|
andq $-16, %rsp
|
||||||
|
|
||||||
|
movq mboot_info_ptr, %rdi
|
||||||
|
call kernel_main
|
||||||
|
cli
|
||||||
|
1:
|
||||||
|
hlt
|
||||||
|
jmp 1b
|
||||||
|
|
||||||
|
.code64
|
||||||
|
.global isr_keyboard
|
||||||
|
.align 16
|
||||||
|
isr_keyboard:
|
||||||
|
/* Save all caller-saved registers used by the SysV AMD64 ABI. */
|
||||||
|
pushq %rax
|
||||||
|
pushq %rcx
|
||||||
|
pushq %rdx
|
||||||
|
pushq %rsi
|
||||||
|
pushq %rdi
|
||||||
|
pushq %r8
|
||||||
|
pushq %r9
|
||||||
|
pushq %r10
|
||||||
|
pushq %r11
|
||||||
|
/* On interrupt entry the CPU pushed 5 qwords (40 bytes), and we
|
||||||
|
* pushed 9 more (72 bytes) = 112 bytes total. That leaves %rsp
|
||||||
|
* misaligned by 8 relative to a 16-byte boundary after the
|
||||||
|
* upcoming `call`. Push one dummy qword to realign. */
|
||||||
|
subq $8, %rsp
|
||||||
|
cld
|
||||||
|
call keyboard_handler
|
||||||
|
addq $8, %rsp
|
||||||
|
popq %r11
|
||||||
|
popq %r10
|
||||||
|
popq %r9
|
||||||
|
popq %r8
|
||||||
|
popq %rdi
|
||||||
|
popq %rsi
|
||||||
|
popq %rdx
|
||||||
|
popq %rcx
|
||||||
|
popq %rax
|
||||||
|
iretq
|
||||||
|
|
||||||
|
.section .note.Xen, "a", @note
|
||||||
|
.align 4
|
||||||
|
.long 4
|
||||||
|
.long 4
|
||||||
|
.long 18
|
||||||
|
.asciz "Xen"
|
||||||
|
.align 4
|
||||||
|
.long _start
|
||||||
|
|
||||||
|
.section .rodata
|
||||||
|
.align 8
|
||||||
|
gdt64:
|
||||||
|
.quad 0x0000000000000000 /* 0x00: null descriptor */
|
||||||
|
.quad 0x00209A0000000000 /* 0x08: 64-bit code, DPL=0 */
|
||||||
|
.quad 0x0000920000000000 /* 0x10: data, writable, present */
|
||||||
|
gdt64_end:
|
||||||
|
gdt64_pointer:
|
||||||
|
.word gdt64_end - gdt64 - 1
|
||||||
|
.quad gdt64
|
||||||
@@ -1,13 +1,6 @@
|
|||||||
#include "forth.h"
|
#include "forth.h"
|
||||||
|
|
||||||
int main(void) {
|
void forth_run(void) {
|
||||||
// Allocate user memory
|
|
||||||
user_mem_size = 1024 * 1024; // 1 Mega cells
|
|
||||||
user_mem = calloc((size_t)user_mem_size, sizeof(Cell));
|
|
||||||
if (!user_mem) {
|
|
||||||
fprintf(stderr, "Failed to allocate user memory\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
here = user_mem;
|
here = user_mem;
|
||||||
|
|
||||||
// Hidden words first
|
// Hidden words first
|
||||||
@@ -72,6 +65,14 @@ int main(void) {
|
|||||||
add_primitive(".\"", do_dot_quote, F_IMMEDIATE); // immediate
|
add_primitive(".\"", do_dot_quote, F_IMMEDIATE); // immediate
|
||||||
add_primitive("words", do_words, 0);
|
add_primitive("words", do_words, 0);
|
||||||
|
|
||||||
|
// Framebuffer
|
||||||
|
add_primitive("fb-addr", do_fb_addr, 0);
|
||||||
|
add_primitive("fb-width", do_fb_width, 0);
|
||||||
|
add_primitive("fb-height", do_fb_height, 0);
|
||||||
|
add_primitive("fb-pitch", do_fb_pitch, 0);
|
||||||
|
add_primitive("fb-bpp", do_fb_bpp, 0);
|
||||||
|
add_primitive("fb-plot", do_fb_plot, 0);
|
||||||
|
|
||||||
// Memory
|
// Memory
|
||||||
add_primitive("@", do_fetch, 0);
|
add_primitive("@", do_fetch, 0);
|
||||||
add_primitive("!", do_store, 0);
|
add_primitive("!", do_store, 0);
|
||||||
@@ -82,6 +83,7 @@ int main(void) {
|
|||||||
add_primitive("constant", do_constant, 0);
|
add_primitive("constant", do_constant, 0);
|
||||||
add_primitive("here", do_here, 0);
|
add_primitive("here", do_here, 0);
|
||||||
add_primitive("allot", do_allot, 0);
|
add_primitive("allot", do_allot, 0);
|
||||||
|
add_primitive(",", do_comma, 0);
|
||||||
|
|
||||||
// Return stack
|
// Return stack
|
||||||
add_primitive(">r", do_to_r, 0);
|
add_primitive(">r", do_to_r, 0);
|
||||||
@@ -110,5 +112,4 @@ int main(void) {
|
|||||||
|
|
||||||
// Start outer interpreter
|
// Start outer interpreter
|
||||||
outer_interpreter();
|
outer_interpreter();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,397 @@
|
|||||||
|
#include "forth.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
extern const uint8_t font8x8[96][8];
|
||||||
|
|
||||||
|
struct mboot_tag {
|
||||||
|
uint32_t type;
|
||||||
|
uint32_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mboot_tag_framebuffer {
|
||||||
|
uint32_t type;
|
||||||
|
uint32_t size;
|
||||||
|
uint64_t framebuffer_addr;
|
||||||
|
uint32_t framebuffer_pitch;
|
||||||
|
uint32_t framebuffer_width;
|
||||||
|
uint32_t framebuffer_height;
|
||||||
|
uint8_t framebuffer_bpp;
|
||||||
|
uint8_t framebuffer_type;
|
||||||
|
uint16_t reserved;
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint64_t fb_addr = 0;
|
||||||
|
static uint32_t fb_pitch = 0;
|
||||||
|
static uint32_t fb_width = 0;
|
||||||
|
static uint32_t fb_height = 0;
|
||||||
|
static uint8_t fb_bpp = 0;
|
||||||
|
static int fb_cols = 0;
|
||||||
|
static int fb_rows = 0;
|
||||||
|
static int fb_col = 0;
|
||||||
|
static int fb_row = 0;
|
||||||
|
|
||||||
|
static void fb_draw_pixel(int x, int y, uint32_t color) {
|
||||||
|
if (x < 0 || y < 0 || (uint32_t)x >= fb_width || (uint32_t)y >= fb_height) return;
|
||||||
|
if (!fb_addr) return;
|
||||||
|
if (fb_bpp == 32) {
|
||||||
|
uint32_t* p = (uint32_t*)(fb_addr + y * fb_pitch + x * 4);
|
||||||
|
*p = color;
|
||||||
|
} else if (fb_bpp == 24) {
|
||||||
|
uint8_t* p = (uint8_t*)(fb_addr + y * fb_pitch + x * 3);
|
||||||
|
p[0] = color & 0xFF;
|
||||||
|
p[1] = (color >> 8) & 0xFF;
|
||||||
|
p[2] = (color >> 16) & 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fb_scroll(void) {
|
||||||
|
int line_height = 8;
|
||||||
|
uint32_t bytes_per_pixel = (fb_bpp + 7) / 8;
|
||||||
|
uint32_t copy_bytes = fb_width * bytes_per_pixel;
|
||||||
|
for (uint32_t y = 0; y < fb_height - line_height; y++) {
|
||||||
|
uint8_t* dst = (uint8_t*)(fb_addr + y * fb_pitch);
|
||||||
|
uint8_t* src = (uint8_t*)(fb_addr + (y + line_height) * fb_pitch);
|
||||||
|
for (uint32_t i = 0; i < copy_bytes; i++) {
|
||||||
|
dst[i] = src[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (uint32_t y = fb_height - line_height; y < fb_height; y++) {
|
||||||
|
uint8_t* row = (uint8_t*)(fb_addr + y * fb_pitch);
|
||||||
|
for (uint32_t i = 0; i < copy_bytes; i++) {
|
||||||
|
row[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fb_draw_char(char c) {
|
||||||
|
if (!fb_addr || fb_cols == 0) return;
|
||||||
|
if (c == '\n') {
|
||||||
|
fb_col = 0;
|
||||||
|
fb_row++;
|
||||||
|
} else if (c == '\r') {
|
||||||
|
fb_col = 0;
|
||||||
|
} else if (c == '\b') {
|
||||||
|
if (fb_col > 0) fb_col--;
|
||||||
|
} else {
|
||||||
|
unsigned char ch = (unsigned char)c;
|
||||||
|
if (ch < 32 || ch > 126) ch = '?';
|
||||||
|
const uint8_t* glyph = font8x8[ch - 32];
|
||||||
|
int start_x = fb_col * 8;
|
||||||
|
int start_y = fb_row * 8;
|
||||||
|
for (int dy = 0; dy < 8; dy++) {
|
||||||
|
uint8_t row = glyph[dy];
|
||||||
|
for (int dx = 0; dx < 8; dx++) {
|
||||||
|
if (row & (0x80 >> dx)) {
|
||||||
|
fb_draw_pixel(start_x + dx, start_y + dy, 0xFFFFFF);
|
||||||
|
} else {
|
||||||
|
fb_draw_pixel(start_x + dx, start_y + dy, 0x000000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fb_col++;
|
||||||
|
}
|
||||||
|
if (fb_col >= fb_cols) {
|
||||||
|
fb_col = 0;
|
||||||
|
fb_row++;
|
||||||
|
}
|
||||||
|
if (fb_row >= fb_rows) {
|
||||||
|
fb_scroll();
|
||||||
|
fb_row = fb_rows - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void outb(uint16_t port, uint8_t val) {
|
||||||
|
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t inb(uint16_t port) {
|
||||||
|
uint8_t ret;
|
||||||
|
__asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void io_wait(void) {
|
||||||
|
__asm__ volatile ("outb %0, %1" : : "a"((uint8_t)0), "Nd"((uint16_t)0x80));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void serial_init(void) {
|
||||||
|
outb(0x3F8 + 1, 0x00);
|
||||||
|
outb(0x3F8 + 3, 0x80);
|
||||||
|
outb(0x3F8 + 0, 0x01);
|
||||||
|
outb(0x3F8 + 1, 0x00);
|
||||||
|
outb(0x3F8 + 3, 0x03);
|
||||||
|
outb(0x3F8 + 2, 0xC7);
|
||||||
|
outb(0x3F8 + 4, 0x0B);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Each 64-bit IDT entry is 16 bytes = two uint64_t words.
|
||||||
|
*
|
||||||
|
* Word 0 layout:
|
||||||
|
* [15: 0] offset[15:0]
|
||||||
|
* [31:16] segment selector
|
||||||
|
* [34:32] IST (0 = no IST)
|
||||||
|
* [39:35] reserved (0)
|
||||||
|
* [47:40] type+attr (0x8E = present, DPL=0, 64-bit interrupt gate)
|
||||||
|
* [63:48] offset[31:16]
|
||||||
|
*
|
||||||
|
* Word 1 layout:
|
||||||
|
* [31: 0] offset[63:32]
|
||||||
|
* [63:32] reserved (0)
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
uint64_t low;
|
||||||
|
uint64_t high;
|
||||||
|
} __attribute__((packed)) IdtEntry;
|
||||||
|
|
||||||
|
static IdtEntry idt_entries[256];
|
||||||
|
|
||||||
|
static void idt_set_gate(int vector, uint64_t offset, uint16_t selector, uint8_t type_attr) {
|
||||||
|
uint64_t low =
|
||||||
|
(offset & 0x000000000000FFFFULL) | /* offset[15:0] */
|
||||||
|
((uint64_t)selector << 16) | /* selector */
|
||||||
|
/* bits 34:32 = IST=0, bits 39:35 = 0 */
|
||||||
|
((uint64_t)type_attr << 40) | /* type+attr */
|
||||||
|
((offset & 0x00000000FFFF0000ULL) << 32); /* offset[31:16] */
|
||||||
|
uint64_t high = offset >> 32; /* offset[63:32] */
|
||||||
|
idt_entries[vector].low = low;
|
||||||
|
idt_entries[vector].high = high;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
uint16_t limit;
|
||||||
|
uint64_t base;
|
||||||
|
} __attribute__((packed)) idt_ptr;
|
||||||
|
|
||||||
|
static void pic_remap(void) {
|
||||||
|
uint8_t a1 = inb(0x21);
|
||||||
|
uint8_t a2 = inb(0xA1);
|
||||||
|
|
||||||
|
outb(0x20, 0x11);
|
||||||
|
io_wait();
|
||||||
|
outb(0xA0, 0x11);
|
||||||
|
io_wait();
|
||||||
|
outb(0x21, 0x20);
|
||||||
|
io_wait();
|
||||||
|
outb(0xA1, 0x28);
|
||||||
|
io_wait();
|
||||||
|
outb(0x21, 0x04);
|
||||||
|
io_wait();
|
||||||
|
outb(0xA1, 0x02);
|
||||||
|
io_wait();
|
||||||
|
outb(0x21, 0x01);
|
||||||
|
io_wait();
|
||||||
|
outb(0xA1, 0x01);
|
||||||
|
io_wait();
|
||||||
|
|
||||||
|
outb(0x21, a1);
|
||||||
|
outb(0xA1, a2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pic_unmask_keyboard(void) {
|
||||||
|
uint8_t mask = inb(0x21);
|
||||||
|
mask &= ~(1 << 1);
|
||||||
|
outb(0x21, mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define KEYBUF_SIZE 256
|
||||||
|
static volatile char keybuf[KEYBUF_SIZE];
|
||||||
|
static volatile int keybuf_head = 0;
|
||||||
|
static volatile int keybuf_tail = 0;
|
||||||
|
|
||||||
|
static const char scancode_map[] = {
|
||||||
|
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
|
||||||
|
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
|
||||||
|
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
|
||||||
|
0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,
|
||||||
|
'*', 0, ' ', 0
|
||||||
|
};
|
||||||
|
|
||||||
|
void keyboard_handler(void) {
|
||||||
|
uint8_t scancode = inb(0x60);
|
||||||
|
if (scancode < sizeof(scancode_map)) {
|
||||||
|
char c = scancode_map[scancode];
|
||||||
|
if (c) {
|
||||||
|
int next = (keybuf_tail + 1) % KEYBUF_SIZE;
|
||||||
|
if (next != keybuf_head) {
|
||||||
|
keybuf[keybuf_tail] = c;
|
||||||
|
keybuf_tail = next;
|
||||||
|
forth_putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outb(0x20, 0x20);
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_putchar(char c) {
|
||||||
|
fb_draw_char(c);
|
||||||
|
while ((inb(0x3F8 + 5) & 0x20) == 0) { }
|
||||||
|
outb(0x3F8, (uint8_t)c);
|
||||||
|
}
|
||||||
|
|
||||||
|
int forth_getchar(void) {
|
||||||
|
while (keybuf_head == keybuf_tail) {
|
||||||
|
__asm__ volatile ("pause");
|
||||||
|
}
|
||||||
|
char c = keybuf[keybuf_head];
|
||||||
|
keybuf_head = (keybuf_head + 1) % KEYBUF_SIZE;
|
||||||
|
return (int)c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_fflush(void) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_panic(void) {
|
||||||
|
__asm__ volatile ("cli; hlt");
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t forth_strtoll(const char* str, char** endptr, int base) {
|
||||||
|
const char* s = str;
|
||||||
|
int neg = 0;
|
||||||
|
int64_t val = 0;
|
||||||
|
while (forth_isspace(*s)) s++;
|
||||||
|
if (*s == '-') { neg = 1; s++; }
|
||||||
|
else if (*s == '+') { s++; }
|
||||||
|
while (*s >= '0' && *s <= '9') {
|
||||||
|
val = val * base + (*s - '0');
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
if (endptr) *endptr = (char*)s;
|
||||||
|
return neg ? -val : val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_str(const char* s) {
|
||||||
|
while (*s) forth_putchar(*s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_int(long long v) {
|
||||||
|
char buf[32];
|
||||||
|
int i = 30;
|
||||||
|
int neg = 0;
|
||||||
|
if (v < 0) { neg = 1; v = -v; }
|
||||||
|
buf[31] = '\0';
|
||||||
|
if (v == 0) {
|
||||||
|
buf[i--] = '0';
|
||||||
|
} else {
|
||||||
|
while (v > 0 && i >= 0) {
|
||||||
|
buf[i--] = '0' + (v % 10);
|
||||||
|
v /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (neg) buf[i--] = '-';
|
||||||
|
print_str(&buf[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_printf(const char* fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
while (*fmt) {
|
||||||
|
if (*fmt == '%' && *(fmt + 1)) {
|
||||||
|
fmt++;
|
||||||
|
if (*fmt == 's') {
|
||||||
|
print_str(va_arg(ap, const char*));
|
||||||
|
} else if (*fmt == 'c') {
|
||||||
|
forth_putchar((char)va_arg(ap, int));
|
||||||
|
} else if (*fmt == '%') {
|
||||||
|
forth_putchar('%');
|
||||||
|
} else if (*fmt == 'l') {
|
||||||
|
if (*(fmt + 1) == 'l' && (*(fmt + 2) == 'd' || *(fmt + 2) == 'i' || *(fmt + 2) == 'u')) {
|
||||||
|
fmt += 2;
|
||||||
|
print_int(va_arg(ap, long long));
|
||||||
|
}
|
||||||
|
} else if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
|
||||||
|
print_int(va_arg(ap, int));
|
||||||
|
} else {
|
||||||
|
forth_putchar('%');
|
||||||
|
forth_putchar(*fmt);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
forth_putchar(*fmt);
|
||||||
|
}
|
||||||
|
fmt++;
|
||||||
|
}
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void kernel_main(uint64_t mboot_ptr) {
|
||||||
|
extern void isr_keyboard(void);
|
||||||
|
|
||||||
|
/* Multiboot2 info tag types:
|
||||||
|
* 5 = BIOS boot device, 6 = memory map, 8 = framebuffer info, ...
|
||||||
|
* Note: in the *header* (request side), type 5 means "framebuffer
|
||||||
|
* request", but the bootloader's *response* tag uses type 8. */
|
||||||
|
struct mboot_tag* tag = (struct mboot_tag*)(mboot_ptr + 8);
|
||||||
|
while (tag->type != 0) {
|
||||||
|
if (tag->type == 8) {
|
||||||
|
struct mboot_tag_framebuffer* fb = (struct mboot_tag_framebuffer*)tag;
|
||||||
|
fb_addr = fb->framebuffer_addr;
|
||||||
|
fb_pitch = fb->framebuffer_pitch;
|
||||||
|
fb_width = fb->framebuffer_width;
|
||||||
|
fb_height = fb->framebuffer_height;
|
||||||
|
fb_bpp = fb->framebuffer_bpp;
|
||||||
|
}
|
||||||
|
tag = (struct mboot_tag*)((uint8_t*)tag + ((tag->size + 7) & ~7));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fb_addr && fb_bpp >= 24) {
|
||||||
|
fb_cols = fb_width / 8;
|
||||||
|
fb_rows = fb_height / 8;
|
||||||
|
for (uint32_t y = 0; y < fb_height; y++) {
|
||||||
|
for (uint32_t x = 0; x < fb_width; x++) {
|
||||||
|
fb_draw_pixel(x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serial_init();
|
||||||
|
|
||||||
|
idt_set_gate(0x21, (uint64_t)isr_keyboard, 0x08, 0x8E);
|
||||||
|
idt_ptr.limit = (uint16_t)(sizeof(idt_entries) - 1);
|
||||||
|
idt_ptr.base = (uint64_t)(uintptr_t)idt_entries;
|
||||||
|
__asm__ volatile ("lidt %0" : : "m"(idt_ptr));
|
||||||
|
|
||||||
|
pic_remap();
|
||||||
|
outb(0x21, 0xFF); // mask all IRQs on master
|
||||||
|
outb(0xA1, 0xFF); // mask all IRQs on slave
|
||||||
|
pic_unmask_keyboard();
|
||||||
|
|
||||||
|
__asm__ volatile ("sti");
|
||||||
|
|
||||||
|
forth_printf("Kernel started\n");
|
||||||
|
forth_run();
|
||||||
|
forth_panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_addr(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
data_push((int64_t)fb_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_width(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
data_push((int64_t)fb_width);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_height(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
data_push((int64_t)fb_height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_pitch(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
data_push((int64_t)fb_pitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_bpp(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
data_push((int64_t)fb_bpp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_plot(Word* w) {
|
||||||
|
(void)w;
|
||||||
|
int64_t color = data_pop();
|
||||||
|
int64_t y = data_pop();
|
||||||
|
int64_t x = data_pop();
|
||||||
|
fb_draw_pixel((int)x, (int)y, (uint32_t)color);
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
#include "forth.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
static inline long syscall3(long n, long a1, long a2, long a3) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile ("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(n), "D"(a1), "S"(a2), "d"(a3)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline long syscall1(long n, long a1) {
|
||||||
|
long ret;
|
||||||
|
__asm__ volatile ("syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "a"(n), "D"(a1)
|
||||||
|
: "rcx", "r11", "memory");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_putchar(char c) {
|
||||||
|
char ch = c;
|
||||||
|
syscall3(1, 1, (long)&ch, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_fflush(void) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int forth_getchar(void) {
|
||||||
|
unsigned char c;
|
||||||
|
long n = syscall3(0, 0, (long)&c, 1);
|
||||||
|
if (n <= 0) return FORTH_EOF;
|
||||||
|
return (int)c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_panic(void) {
|
||||||
|
syscall1(60, 1);
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t forth_strtoll(const char* str, char** endptr, int base) {
|
||||||
|
const char* s = str;
|
||||||
|
int neg = 0;
|
||||||
|
int64_t val = 0;
|
||||||
|
while (forth_isspace(*s)) s++;
|
||||||
|
if (*s == '-') { neg = 1; s++; }
|
||||||
|
else if (*s == '+') { s++; }
|
||||||
|
while (*s >= '0' && *s <= '9') {
|
||||||
|
val = val * base + (*s - '0');
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
if (endptr) *endptr = (char*)s;
|
||||||
|
return neg ? -val : val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_str(const char* s) {
|
||||||
|
while (*s) forth_putchar(*s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_int(long long v) {
|
||||||
|
char buf[32];
|
||||||
|
int i = 30;
|
||||||
|
int neg = 0;
|
||||||
|
if (v < 0) { neg = 1; v = -v; }
|
||||||
|
buf[31] = '\0';
|
||||||
|
if (v == 0) {
|
||||||
|
buf[i--] = '0';
|
||||||
|
} else {
|
||||||
|
while (v > 0 && i >= 0) {
|
||||||
|
buf[i--] = '0' + (v % 10);
|
||||||
|
v /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (neg) buf[i--] = '-';
|
||||||
|
print_str(&buf[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void forth_printf(const char* fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
while (*fmt) {
|
||||||
|
if (*fmt == '%' && *(fmt + 1)) {
|
||||||
|
fmt++;
|
||||||
|
if (*fmt == 's') {
|
||||||
|
print_str(va_arg(ap, const char*));
|
||||||
|
} else if (*fmt == 'c') {
|
||||||
|
forth_putchar((char)va_arg(ap, int));
|
||||||
|
} else if (*fmt == '%') {
|
||||||
|
forth_putchar('%');
|
||||||
|
} else if (*fmt == 'l') {
|
||||||
|
if (*(fmt + 1) == 'l' && (*(fmt + 2) == 'd' || *(fmt + 2) == 'i' || *(fmt + 2) == 'u')) {
|
||||||
|
fmt += 2;
|
||||||
|
print_int(va_arg(ap, long long));
|
||||||
|
}
|
||||||
|
} else if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
|
||||||
|
print_int(va_arg(ap, int));
|
||||||
|
} else {
|
||||||
|
forth_putchar('%');
|
||||||
|
forth_putchar(*fmt);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
forth_putchar(*fmt);
|
||||||
|
}
|
||||||
|
fmt++;
|
||||||
|
}
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_fb_addr(Word* w) { (void)w; data_push(0); }
|
||||||
|
void do_fb_width(Word* w) { (void)w; data_push(0); }
|
||||||
|
void do_fb_height(Word* w) { (void)w; data_push(0); }
|
||||||
|
void do_fb_pitch(Word* w) { (void)w; data_push(0); }
|
||||||
|
void do_fb_bpp(Word* w) { (void)w; data_push(0); }
|
||||||
|
void do_fb_plot(Word* w) { (void)w; data_pop(); data_pop(); data_pop(); }
|
||||||
|
|
||||||
|
void _start(void) {
|
||||||
|
forth_run();
|
||||||
|
syscall1(60, 0);
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user