This commit is contained in:
Alvin-Zilverstand committed 2026-10-06 22:05:14 +02:00
1 parent 74bda7d8b2
commit 472df35261
23 files changed
+32910 -42

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
# Kernel API
Include `runtime.h` in guest kernel source. The host bob16 compiler treats each
int, char and pointer as a single 16-bit word. These are guest routines, not the
host C library. Headers and source are compiled together by `kernel/kernel.c`.
## Console
| Function | Contract |
| --- | --- |
| `void print(char *text)` | Print an unpacked ASCII string up to its zero terminator |
| `void println(char *text)` | Print the string and one newline |
| `void print_dec(int number)` | Print a signed value, including -32768, without a newline |
| `void print_hex(int number)` | Print its raw 16 bits as `0x` and four uppercase hex digits |
| `int read_line(char *buffer, int capacity)` | Read/terminate a line; length on success, -1 for truncation/invalid capacity, -2 for EOF with no characters |
The caller must supply a writable buffer of at least capacity words. At most
capacity-1 characters are stored. Excess characters are consumed until newline,
so they do not leak into the next command. Newline/CR are not stored. Backspace
removes a stored character. A partial final line at EOF is returned normally;
the next read reports EOF. The host terminal supplies line editing and echo.
The low-level `bob_putc(int)` sends the low eight bits to stdout. `bob_getc()`
returns 0..255 or -1 at EOF. `bob_puts()` adds a newline; use print for prompts.
## Strings, memory and allocation
| Function | Contract |
| --- | --- |
| `int strlen(char *text)` | Count words before the zero terminator |
| `int strcmp(char *a, char *b)` | Negative, zero or positive according to the first differing ASCII character |
| `void memcpy(int *dst, int *src, int count)` | Copy count words forward; source and destination must not overlap |
| `void memset(int *dst, int value, int count)` | Fill count words with a 16-bit value |
| `int *alloc(int words)` | Return a zeroed heap block, or null for an invalid size or exhaustion |
String routines require terminated strings. Memory routines require valid
regions; they do not perform bounds checks for kernel callers. Counts are words,
not host bytes. Allocation reserves from 0xC000 upward, with 8,192 total words.
The heap is shared with loaded programs. There is no free or reset operation.
## RAM filesystem
`file_find(name)` returns a slot or -1. `file_name(slot)` and
`file_content(slot)` return addresses into the filesystem buffers.
`file_write(name, data, length, kind)` copies data and creates/replaces a file,
returning its slot or -1 for a bad name, length or a full table. Kind 0 is text;
kind 1 is machine code. Its copy appends a zero word, which is not part of length.
Names hold at most 23 characters and data at most 511 words. Valid slot indices
are 0..7. These routines are internal kernel APIs; callers must check slots.
Failed size/name validation does not overwrite an existing file. Edits start
with the existing file contents in a scratch buffer and commit on :w, :wq or a
single dot line. :q or EOF discards changes since the most recent save. Oversize
changes are rejected without modifying the current buffer.
Resident compilation similarly commits output only after successful parsing and
code generation. Storage is volatile and disappears when bob.exe stops.
## Program execution
`bob_run(entry)` invokes emulator trap 6. Valid entries are 0x9000..0xBDFF.
The shell's run_file copies a binary RAM file to 0x9000 after clearing the
12,288-word program region. Entry 0x9000 executes with r6=0xC000, r5=0, and
r7 pointing to the reserved return sentinel. The shell CPU state is restored
on completion. RET or TRAP 0 ends the program.
Return values -1, -2 and -3 are reserved for invalid entry/nested execution,
program fault, and timeout. Otherwise r0 is the program's exit status. A program
returning one of those reserved values is indistinguishable from that service
status. Five million cycles is the per-run limit. Programs may write only
0x9000..0xDFFF; kernel code, RAM files and the shell stack are protected from
their stores. Program and heap contents themselves are not private or restored.
The resident compiler uses variable words at 0x9800..0x980F and inline string
data in its program image. Its files can contain at most 511 code/data words,
so these variable words do not overlap its code. Its console/helper calls use
the separate program stack. Compiled resident programs depend on addresses in
the current kernel and should not be reused with a different kernel build.
`bob_address(function_name)` is a host-compiler intrinsic that inserts a kernel
function address. `bob_call(entry)` is an unchecked direct call for trusted
kernel code; ordinary shell programs use bob_run for recovery and write checks.
See [the OS guide](../C_OS.md) for commands, examples and C subset limits.
+10
View File
@@ -0,0 +1,10 @@
#ifndef BOB_KERNEL_H
#define BOB_KERNEL_H
/* Guest services. bob16 int, char and pointers occupy one 16-bit word. */
void bob_putc(int character);
void bob_puts(char *text);
void bob_gets(char *buffer, int capacity);
void bob_halt(void);
int bob_getc(void);
int bob_run(int entry);
#endif
+418
View File
@@ -0,0 +1,418 @@
/* Resident recursive-descent compiler. It emits bob16 machine words in RAM. */
#define TOKEN_ID 256
#define TOKEN_NUMBER 257
#define TOKEN_STRING 258
#define TOKEN_EQ 259
#define TOKEN_NE 260
#define TOKEN_LE 261
#define TOKEN_GE 262
#define TOKEN_AND 263
#define TOKEN_OR 264
#define TOKEN_INC 265
#define TOKEN_DEC 266
#define TOKEN_SHL 267
#define TOKEN_SHR 268
#define TOKEN_ADD_ASSIGN 269
#define TOKEN_SUB_ASSIGN 270
#define TOKEN_MUL_ASSIGN 271
#define TOKEN_DIV_ASSIGN 272
#define TOKEN_MOD_ASSIGN 273
#define TOKEN_AND_ASSIGN 274
#define TOKEN_OR_ASSIGN 275
#define TOKEN_XOR_ASSIGN 276
#if BOBC_LEGACY
#define CALL_R3 0xcb00
#else
#define CALL_R3 0xc300
#endif
char *cc_source;
int cc_token;
int cc_value;
char cc_text[64];
int cc_error;
int cc_position;
int cc_code[511];
char cc_names[384];
int cc_variables;
int cc_error_offset;
int cc_depth;
char *cc_begin;
int cc_alpha(int ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; }
int cc_digit(int ch) { return ch >= '0' && ch <= '9'; }
void cc_fail(void) {
if (!cc_error) cc_error_offset = (int)(cc_source - cc_begin);
cc_error = 1;
}
void cc_next(void) {
int ch; int count; int digit; int base;
while (1) {
while (*cc_source == ' ' || *cc_source == '\t' || *cc_source == '\n' || *cc_source == '\r') cc_source++;
if (cc_source[0] == '/' && cc_source[1] == '/') {
while (*cc_source && *cc_source != '\n') cc_source++;
} else if (cc_source[0] == '/' && cc_source[1] == '*') {
cc_source = cc_source + 2;
while (*cc_source && !(cc_source[0] == '*' && cc_source[1] == '/')) cc_source++;
if (!*cc_source) { cc_fail(); cc_token = 0; return; }
cc_source = cc_source + 2;
} else break;
}
ch = *cc_source; cc_text[0] = 0;
if (ch == 0) { cc_token = 0; return; }
if (cc_alpha(ch)) {
count = 0;
while (cc_alpha(*cc_source) || cc_digit(*cc_source)) {
if (count == 63) { cc_fail(); cc_token = 0; return; }
cc_text[count++] = *cc_source++;
}
cc_text[count] = 0; cc_token = TOKEN_ID; return;
}
if (cc_digit(ch)) {
cc_value = 0; count = 0; base = 10;
if (cc_source[0] == '0' && (cc_source[1] == 'x' || cc_source[1] == 'X')) { base = 16; cc_source = cc_source + 2; }
while (1) {
digit = *cc_source;
if (cc_digit(digit)) digit = digit - '0';
else if (base == 16 && digit >= 'a' && digit <= 'f') digit = digit - 'a' + 10;
else if (base == 16 && digit >= 'A' && digit <= 'F') digit = digit - 'A' + 10;
else break;
if ((base == 10 && (cc_value > 3276 || (cc_value == 3276 && digit > 7))) ||
(base == 16 && count == 4)) { cc_fail(); cc_token = 0; return; }
cc_value = cc_value * base + digit; cc_source++; count++;
}
if (!count) cc_fail();
cc_token = TOKEN_NUMBER; return;
}
if (ch == '"' || ch == '\'') {
cc_source++; count = 0;
while (*cc_source && *cc_source != ch) {
digit = *cc_source++;
if (digit == '\\') {
if (!*cc_source) { cc_fail(); break; }
digit = *cc_source++;
if (digit == 'n') digit = '\n';
else if (digit == 't') digit = '\t';
else if (digit != '\\' && digit != '"' && digit != '\'') { cc_fail(); break; }
}
if (count == 63 || digit == 0) { cc_fail(); break; }
cc_text[count++] = digit;
}
if (*cc_source != ch) cc_fail(); else cc_source++;
cc_text[count] = 0;
if (ch == '\'') { if (count != 1) cc_fail(); cc_value = cc_text[0]; cc_token = TOKEN_NUMBER; }
else cc_token = TOKEN_STRING;
return;
}
cc_source++; cc_token = ch;
if (*cc_source == ch) {
if (ch == '&') cc_token = TOKEN_AND;
else if (ch == '|') cc_token = TOKEN_OR;
else if (ch == '+') cc_token = TOKEN_INC;
else if (ch == '-') cc_token = TOKEN_DEC;
else if (ch == '<') cc_token = TOKEN_SHL;
else if (ch == '>') cc_token = TOKEN_SHR;
else if (ch != '=') return;
if (ch != '=') { cc_source++; return; }
}
if (*cc_source == '=') {
if (ch == '=') cc_token = TOKEN_EQ;
else if (ch == '!') cc_token = TOKEN_NE;
else if (ch == '<') cc_token = TOKEN_LE;
else if (ch == '>') cc_token = TOKEN_GE;
else if (ch == '+') cc_token = TOKEN_ADD_ASSIGN;
else if (ch == '-') cc_token = TOKEN_SUB_ASSIGN;
else if (ch == '*') cc_token = TOKEN_MUL_ASSIGN;
else if (ch == '/') cc_token = TOKEN_DIV_ASSIGN;
else if (ch == '%') cc_token = TOKEN_MOD_ASSIGN;
else if (ch == '&') cc_token = TOKEN_AND_ASSIGN;
else if (ch == '|') cc_token = TOKEN_OR_ASSIGN;
else if (ch == '^') cc_token = TOKEN_XOR_ASSIGN;
else return;
cc_source++;
}
}
int cc_is(char *name) { return cc_token == TOKEN_ID && strcmp(cc_text, name) == 0; }
void cc_expect(int token) { if (cc_token != token) { cc_fail(); return; } cc_next(); }
void cg_emit(int word) {
if (cc_position >= 511) { cc_fail(); return; }
cc_code[cc_position++] = word;
}
void cg_literal(int reg, int value) { cg_emit(0x4001 + reg * 512); cg_emit(0xae01); cg_emit(value); }
void cg_push(void) { cg_emit(0x1dff); cg_emit(0x9180); }
void cg_pop(void) { cg_emit(0x6380); cg_emit(0x1d81); }
int cg_jump(int target) {
int patch; patch = cc_position + 2;
cg_literal(3, PROGRAM_BASE + target); cg_emit(0xb600); return patch;
}
void cg_patch(int patch) { if (!cc_error) cc_code[patch] = PROGRAM_BASE + cc_position; }
void cg_call(int address, int args) {
cg_literal(3, address); cg_emit(CALL_R3); cg_emit(0x1d80 + args);
}
int cg_if_zero(void) { cg_emit(0x1080); cg_emit(0xaa04); return cg_jump(0); }
int cc_variable(char *name) {
int i;
for (i = 0; i < cc_variables; i++) if (strcmp(cc_names + i * NAME_WORDS, name) == 0) return i;
return -1;
}
int cc_less(int a, int b) { return a < b; }
int cc_greater(int a, int b) { return a > b; }
int cc_less_equal(int a, int b) { return a <= b; }
int cc_greater_equal(int a, int b) { return a >= b; }
int cc_equal(int a, int b) { return a == b; }
int cc_not_equal(int a, int b) { return a != b; }
int cc_multiply(int a, int b) { return a * b; }
int cc_divide(int a, int b) { return a / b; }
int cc_modulo(int a, int b) { return a % b; }
int cc_negate(int a) { return -a; }
int cc_not(int a) { return !a; }
int cc_bit_or(int a, int b) { return a | b; }
int cc_bit_xor(int a, int b) { return a ^ b; }
int cc_shift_left(int a, int b) { return a << b; }
int cc_shift_right(int a, int b) { return a >> b; }
int cc_truth(int a) { return !!a; }
int cc_loop_depth;
int cc_break_chain[16];
int cc_continue_chain[16];
int cc_precedence(int op) {
if (op == TOKEN_OR) return 1;
if (op == TOKEN_AND) return 2;
if (op == '|') return 3;
if (op == '^') return 4;
if (op == '&') return 5;
if (op == TOKEN_EQ || op == TOKEN_NE) return 6;
if (op == '<' || op == '>' || op == TOKEN_LE || op == TOKEN_GE) return 7;
if (op == TOKEN_SHL || op == TOKEN_SHR) return 8;
if (op == '+' || op == '-') return 9;
if (op == '*' || op == '/' || op == '%') return 10;
return 0;
}
void cc_operation(int op) {
int address;
if (op == '+' || op == '-') {
if (op == '-') { cg_emit(0x3100); cg_emit(0x1181); }
cg_emit(0x1010); return;
}
if (op == '&') { cg_emit(0x2010); return; }
cg_push(); cg_emit(0x1dff); cg_emit(0x9380);
if (op == '*') address = bob_address(cc_multiply);
else if (op == '/') address = bob_address(cc_divide);
else if (op == '%') address = bob_address(cc_modulo);
else if (op == '|') address = bob_address(cc_bit_or);
else if (op == '^') address = bob_address(cc_bit_xor);
else if (op == TOKEN_SHL) address = bob_address(cc_shift_left);
else if (op == TOKEN_SHR) address = bob_address(cc_shift_right);
else if (op == TOKEN_EQ) address = bob_address(cc_equal);
else if (op == TOKEN_NE) address = bob_address(cc_not_equal);
else if (op == '<') address = bob_address(cc_less);
else if (op == '>') address = bob_address(cc_greater);
else if (op == TOKEN_LE) address = bob_address(cc_less_equal);
else address = bob_address(cc_greater_equal);
cg_call(address, 2);
}
void cc_expression(void);
void cc_primary(void) {
int variable; int patch; int start; int count; int op;
if (cc_error) return;
cc_depth++;
if (cc_depth > 32) { cc_fail(); return; }
if (cc_token == TOKEN_NUMBER) { cg_literal(0, cc_value); cc_next(); }
else if (cc_token == TOKEN_STRING) {
count = strlen(cc_text);
/* Inline string data is skipped by an absolute jump. */
patch = cg_jump(0); start = cc_position;
for (variable = 0; variable <= count; variable++) cg_emit(cc_text[variable]);
cg_patch(patch); cg_literal(0, PROGRAM_BASE + start); cc_next();
} else if (cc_token == '(') { cc_next(); cc_expression(); cc_expect(')'); }
else if (cc_token == '-' || cc_token == '!' || cc_token == '+' || cc_token == '~') {
op = cc_token; cc_next(); cc_primary(); cg_push();
if (op == '-') cg_call(bob_address(cc_negate), 1);
else if (op == '!') cg_call(bob_address(cc_not), 1);
else { cg_emit(0x6180); cg_emit(0x1d81); if (op == '~') cg_emit(0x3100); }
} else if (cc_token == TOKEN_INC || cc_token == TOKEN_DEC) {
op = cc_token; cc_next(); variable = cc_variable(cc_text);
if (cc_token != TOKEN_ID || variable < 0) { cc_fail(); return; }
cc_next(); cg_literal(0, 0x9800 + variable); cg_emit(0x6000);
if (op == TOKEN_INC) cg_emit(0x1181); else cg_emit(0x11ff);
cg_literal(1, 0x9800 + variable); cg_emit(0x9040);
} else if (cc_token == TOKEN_ID) {
variable = cc_variable(cc_text);
if (variable < 0) { cc_fail(); return; }
cg_literal(0, 0x9800 + variable); cg_emit(0x6000); cc_next();
if (cc_token == TOKEN_INC || cc_token == TOKEN_DEC) {
op = cc_token; cc_next(); cg_push();
if (op == TOKEN_INC) cg_emit(0x1181); else cg_emit(0x11ff);
cg_literal(1, 0x9800 + variable); cg_emit(0x9040);
cg_emit(0x6180); cg_emit(0x1d81);
}
} else cc_fail();
cc_depth--;
}
void cc_binary(int minimum) {
int op; int level; int shortcut; int end;
cc_primary(); level = cc_precedence(cc_token);
while (!cc_error && level >= minimum && level) {
op = cc_token; cc_next();
if (op == TOKEN_AND || op == TOKEN_OR) {
cg_emit(0x1080);
if (op == TOKEN_AND) cg_emit(0xaa04); else cg_emit(0xa404);
shortcut = cg_jump(0); cc_binary(level + 1);
cg_push(); cg_call(bob_address(cc_truth), 1); end = cg_jump(0);
cg_patch(shortcut); cg_literal(0, op == TOKEN_OR); cg_patch(end);
} else {
cg_push(); cc_binary(level + 1); cg_pop(); cc_operation(op);
}
level = cc_precedence(cc_token);
}
}
void cc_expression(void) { cc_binary(1); }
void cc_patch_chain(int chain, int target) {
int next;
while (chain >= 0 && !cc_error) {
next = cc_code[chain]; cc_code[chain] = PROGRAM_BASE + target; chain = next;
}
}
void cc_simple(void) {
char name[24]; int variable; int address; int op;
if (cc_token == TOKEN_INC || cc_token == TOKEN_DEC) { cc_primary(); return; }
if (cc_token != TOKEN_ID || strlen(cc_text) >= NAME_WORDS) { cc_fail(); return; }
memcpy(name, cc_text, strlen(cc_text) + 1); cc_next(); op = cc_token;
if (op == TOKEN_INC || op == TOKEN_DEC) {
variable = cc_variable(name); if (variable < 0) { cc_fail(); return; }
cc_next(); cg_literal(0, 0x9800 + variable); cg_emit(0x6000);
if (op == TOKEN_INC) cg_emit(0x1181); else cg_emit(0x11ff);
cg_literal(1, 0x9800 + variable); cg_emit(0x9040);
} else if (op == '=' || (op >= TOKEN_ADD_ASSIGN && op <= TOKEN_XOR_ASSIGN)) {
variable = cc_variable(name); if (variable < 0) { cc_fail(); return; }
if (op != '=') { cg_literal(0, 0x9800 + variable); cg_emit(0x6000); cg_push(); }
cc_next(); cc_expression();
if (op != '=') {
cg_pop();
if (op == TOKEN_ADD_ASSIGN) op = '+';
else if (op == TOKEN_SUB_ASSIGN) op = '-';
else if (op == TOKEN_MUL_ASSIGN) op = '*';
else if (op == TOKEN_DIV_ASSIGN) op = '/';
else if (op == TOKEN_MOD_ASSIGN) op = '%';
else if (op == TOKEN_AND_ASSIGN) op = '&';
else if (op == TOKEN_OR_ASSIGN) op = '|';
else op = '^';
cc_operation(op);
}
cg_literal(1, 0x9800 + variable); cg_emit(0x9040);
} else if (op == '(') {
address = 0;
if (strcmp(name, "print") == 0) address = bob_address(print);
else if (strcmp(name, "println") == 0) address = bob_address(println);
else if (strcmp(name, "print_dec") == 0) address = bob_address(print_dec);
else if (strcmp(name, "print_hex") == 0) address = bob_address(print_hex);
else if (strcmp(name, "bob_putc") == 0) address = 1;
if (!address) { cc_fail(); return; }
cc_next(); cc_expression(); cc_expect(')');
if (address == 1) cg_emit(0xf100);
else { cg_push(); cg_call(address, 1); }
} else cc_fail();
}
void cc_statement(void) {
int variable; int start; int patch; int end; int step; int body;
if (cc_error) return;
cc_depth++;
if (cc_depth > 32) { cc_fail(); return; }
if (cc_token == '{') {
cc_next();
while (!cc_error && cc_token && cc_token != '}') cc_statement();
cc_expect('}');
} else if (cc_is("int")) {
cc_next();
if (cc_token != TOKEN_ID || strlen(cc_text) >= NAME_WORDS || cc_variables == 16 || cc_variable(cc_text) >= 0) { cc_fail(); return; }
variable = cc_variables++; memcpy(cc_names + variable * NAME_WORDS, cc_text, strlen(cc_text) + 1);
cc_next();
if (cc_token == '=') { cc_next(); cc_expression(); }
else cg_literal(0, 0);
cg_literal(1, 0x9800 + variable); cg_emit(0x9040); cc_expect(';');
} else if (cc_is("return")) {
cc_next();
if (cc_token == ';') cg_literal(0, 0); else cc_expression();
cg_emit(0x1cd0); cg_emit(0x6b80); cg_emit(0x1d81); cg_emit(0x6f80); cg_emit(0x1d81); cg_emit(0xe000);
cc_expect(';');
} else if (cc_is("break") || cc_is("continue")) {
variable = cc_is("continue");
if (!cc_loop_depth) { cc_fail(); return; }
cc_next(); patch = cg_jump(0);
if (variable) {
cc_code[patch] = cc_continue_chain[cc_loop_depth - 1]; cc_continue_chain[cc_loop_depth - 1] = patch;
} else {
cc_code[patch] = cc_break_chain[cc_loop_depth - 1]; cc_break_chain[cc_loop_depth - 1] = patch;
}
cc_expect(';');
} else if (cc_is("for")) {
if (cc_loop_depth == 16) { cc_fail(); return; }
cc_next(); cc_expect('(');
if (cc_token == ';') cc_next(); else cc_statement();
start = cc_position;
if (cc_token == ';') cg_literal(0, 1); else cc_expression();
cc_expect(';'); patch = cg_if_zero(); body = cg_jump(0); step = cc_position;
if (cc_token != ')') cc_simple();
cc_expect(')'); cg_jump(start); cg_patch(body);
cc_break_chain[cc_loop_depth] = -1; cc_continue_chain[cc_loop_depth] = -1; cc_loop_depth++;
cc_statement(); cc_loop_depth--; cg_jump(step); cg_patch(patch);
cc_patch_chain(cc_break_chain[cc_loop_depth], cc_position);
cc_patch_chain(cc_continue_chain[cc_loop_depth], step);
} else if (cc_is("if") || cc_is("while")) {
variable = cc_is("while"); cc_next(); cc_expect('(');
start = cc_position; cc_expression(); cc_expect(')');
patch = cg_if_zero();
if (variable) {
if (cc_loop_depth == 16) { cc_fail(); return; }
cc_break_chain[cc_loop_depth] = -1; cc_continue_chain[cc_loop_depth] = -1; cc_loop_depth++;
}
cc_statement();
if (variable) {
cc_loop_depth--; cg_jump(start); cg_patch(patch);
cc_patch_chain(cc_break_chain[cc_loop_depth], cc_position);
cc_patch_chain(cc_continue_chain[cc_loop_depth], start);
}
else if (cc_is("else")) { end = cg_jump(0); cg_patch(patch); cc_next(); cc_statement(); cg_patch(end); }
else cg_patch(patch);
} else if (cc_token == TOKEN_ID || cc_token == TOKEN_INC || cc_token == TOKEN_DEC) {
cc_simple(); cc_expect(';');
} else if (cc_token == ';') cc_next();
else cc_fail();
cc_depth--;
}
int compile_command(char *source_name, char *output_name) {
int slot; int result; int i; int line; int column;
slot = file_find(source_name);
if (slot < 0 || file_kinds[slot]) { println("C source file not found."); return 0; }
if (!*output_name || strlen(output_name) >= NAME_WORDS) { println("Usage: cc SOURCE OUTPUT"); return 0; }
if (strcmp(source_name, output_name) == 0) { println("Output must differ from source; choose another name."); return 0; }
cc_source = file_content(slot); cc_begin = cc_source;
cc_error = 0; cc_position = 0; cc_variables = 0; cc_error_offset = 0; cc_depth = 0; cc_loop_depth = 0;
memset(cc_names, 0, 384); cc_next();
if (!cc_is("int")) cc_fail(); else cc_next();
if (!cc_is("main")) cc_fail(); else cc_next();
cc_expect('('); if (cc_is("void")) cc_next(); cc_expect(')');
if (cc_token != '{') cc_fail();
/* Preserve the return address and caller frame; runProgram provides a stack. */
cg_emit(0x1dff); cg_emit(0x9f80); cg_emit(0x1dff); cg_emit(0x9b80); cg_emit(0x1ae0);
cc_statement();
if (cc_token) cc_fail();
cg_literal(0, 0);
cg_emit(0x1cd0); cg_emit(0x6b80); cg_emit(0x1d81); cg_emit(0x6f80); cg_emit(0x1d81); cg_emit(0xe000);
if (cc_error) {
print("C compile error near character "); print_dec(cc_error_offset);
println(". Unsupported syntax or source/code/nesting limit exceeded.");
line = 1; column = 1;
for (i = 0; i < cc_error_offset; i++) {
if (cc_begin[i] == '\n') { line++; column = 1; }
else column++;
}
print(source_name); print(": line "); print_dec(line);
print(", column "); print_dec(column); println(". Use edit to fix the source.");
return 0;
}
result = file_write(output_name, cc_code, cc_position, 1);
if (result < 0) println("Cannot save compiled program (max 511 words).");
else { print("Compiled "); print_dec(cc_position); println(" words."); }
return result >= 0;
}
+81
View File
@@ -0,0 +1,81 @@
char file_names[192];
int file_lengths[8];
int file_kinds[8];
int file_used[8];
char *file_name(int slot) { return file_names + slot * NAME_WORDS; }
/* The upper 4096 words are reserved for file contents, above the kernel stack.
Supervised programs cannot write this region. */
int *file_content(int slot) { return (int *)0xf000 + slot * FILE_WORDS; }
int file_find(char *name) {
int i;
for (i = 0; i < FILE_COUNT; i++)
if (file_used[i] && strcmp(file_name(i), name) == 0) return i;
return -1;
}
int file_slot(char *name) {
int slot; int length;
length = strlen(name);
if (length == 0 || length >= NAME_WORDS) return -1;
slot = file_find(name);
if (slot >= 0) return slot;
for (slot = 0; slot < FILE_COUNT; slot++) {
if (!file_used[slot]) {
memset(file_name(slot), 0, NAME_WORDS);
memcpy(file_name(slot), name, length);
file_used[slot] = 1;
return slot;
}
}
return -1;
}
int file_write(char *name, int *data, int length, int kind) {
int slot;
if (length < 0 || length >= FILE_WORDS) return -1;
slot = file_slot(name);
if (slot < 0) return -1;
memcpy(file_content(slot), data, length);
file_content(slot)[length] = 0;
file_lengths[slot] = length;
file_kinds[slot] = kind;
return slot;
}
void file_list(void) {
int i; int count;
count = 0;
for (i = 0; i < FILE_COUNT; i++) {
if (file_used[i]) {
print(file_name(i)); print(" "); print_dec(file_lengths[i]);
if (file_kinds[i]) println(" words (program)");
else println(" chars (text)");
count++;
}
}
if (!count) println("No files.");
}
void file_manage(char *operation, char *name, char *destination) {
int slot; int target;
if (!*name) { println("Usage: copy OLD NEW | rename OLD NEW | delete NAME"); return; }
slot = file_find(name);
if (slot < 0) { println("File not found. Use ls to see names."); return; }
if (strcmp(operation, "delete") == 0) {
file_used[slot] = 0; file_lengths[slot] = 0; file_kinds[slot] = 0;
memset(file_name(slot), 0, NAME_WORDS);
memset(file_content(slot), 0, FILE_WORDS);
println("Deleted."); return;
}
if (!*destination || strlen(destination) >= NAME_WORDS) {
println("Destination needs 1..23 characters."); return;
}
if (file_find(destination) >= 0) {
println("Destination exists; choose another name or delete it first."); return;
}
if (strcmp(operation, "rename") == 0) {
memset(file_name(slot), 0, NAME_WORDS);
memcpy(file_name(slot), destination, strlen(destination));
println("Renamed."); return;
}
target = file_write(destination, file_content(slot), file_lengths[slot], file_kinds[slot]);
if (target < 0) println("File slots full (8). Delete a file and retry.");
else println("Copied.");
}
+22
View File
@@ -0,0 +1,22 @@
#include "runtime.h"
#include "runtime.c"
#include "files.c"
#include "compiler.c"
#include "shell.c"
int main(void) {
int length; char *sample;
println("bob!");
println("bob16 OS: help COMMAND for usage; go bob.c runs the example.");
println("Files are in RAM; halt exits. Type help to get started.");
sample = "int main(void) { println(\"bob!\"); return 0; }";
file_write("bob.c", sample, strlen(sample), 0);
while (1) {
print("bob> ");
length = read_line(command_line, 128);
if (length == -2) { bob_putc('\n'); return 0; }
if (length < 0) println("Command too long.");
else shell_command(command_line);
}
return 0;
}
+69
View File
@@ -0,0 +1,69 @@
int heap_used;
int bit_masks[16] = {-32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
int bit_weights[4] = {8,4,2,1};
int strlen(char *text) { int n; n = 0; while (text[n]) n++; return n; }
int strcmp(char *a, char *b) {
int i; i = 0;
while (a[i] && a[i] == b[i]) i++;
return a[i] - b[i];
}
void memcpy(int *dst, int *src, int count) {
int i; for (i = 0; i < count; i++) dst[i] = src[i];
}
void memset(int *dst, int value, int count) {
int i; for (i = 0; i < count; i++) dst[i] = value;
}
void print(char *text) { int i; i = 0; while (text[i]) { bob_putc(text[i]); i++; } }
void println(char *text) { print(text); bob_putc('\n'); }
void decimal_negative(int number) {
int quotient; int digit;
quotient = number / 10;
digit = -(number - quotient * 10);
if (quotient) decimal_negative(quotient);
bob_putc('0' + digit);
}
void print_dec(int number) {
if (number < 0) bob_putc('-');
else number = -number;
decimal_negative(number);
}
void print_hex(int number) {
int nibble; int bit; int digit;
print("0x");
for (nibble = 0; nibble < 4; nibble++) {
digit = 0;
for (bit = 0; bit < 4; bit++) {
if (number & bit_masks[nibble * 4 + bit]) digit = digit + bit_weights[bit];
}
if (digit < 10) bob_putc('0' + digit);
else bob_putc('A' + digit - 10);
}
}
/* Consume the complete line, even when the destination fills. -2 means EOF. */
int read_line(char *buffer, int capacity) {
int length; int character; int truncated;
if (capacity < 1) return -1;
length = 0; truncated = 0;
while (1) {
character = bob_getc();
if (character == -1 || character == '\n') break;
if (character == '\b' || character == 127) { if (length) length--; }
else if (character != '\r') {
if (length < capacity - 1) buffer[length++] = character;
else truncated = 1;
}
}
buffer[length] = 0;
if (truncated) return -1;
if (character == -1 && length == 0) return -2;
return length;
}
int *alloc(int words) {
int *address;
if (words < 1 || words > HEAP_WORDS - heap_used) return 0;
address = (int *)(HEAP_BASE + heap_used);
heap_used = heap_used + words;
memset(address, 0, words);
return address;
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef BOB_RUNTIME_H
#define BOB_RUNTIME_H
#include "bob.h"
#define HEAP_BASE 0xc000
#define HEAP_WORDS 8192
#define PROGRAM_BASE 0x9000
#define PROGRAM_WORDS 12288
#define FILE_COUNT 8
#define FILE_WORDS 512
#define NAME_WORDS 24
int strlen(char *text);
int strcmp(char *a, char *b);
void memcpy(int *dst, int *src, int count);
void memset(int *dst, int value, int count);
void print(char *text);
void println(char *text);
void print_dec(int number);
void print_hex(int number);
int read_line(char *buffer, int capacity);
int *alloc(int words);
int bob_address(int function);
int bob_call(int entry);
#endif
+299
View File
@@ -0,0 +1,299 @@
char command_line[128];
char edit_line[128];
char edit_buffer[512];
char edit_undo[512];
int editor_length;
int editor_undo_length;
int editor_has_undo;
char *arguments;
int program_length;
char *next_arg(void) {
char *start;
while (*arguments == ' ') arguments++;
start = arguments;
while (*arguments && *arguments != ' ') arguments++;
if (*arguments) { *arguments = 0; arguments++; }
return start;
}
char *remaining_args(void) { while (*arguments == ' ') arguments++; return arguments; }
int number_ok;
int parse_number(char *text) {
int negative; int base; int digit; int number; int digits; int limit;
number_ok = 0; negative = 0; base = 10; number = 0; digits = 0;
if (*text == '-') { negative = 1; text++; }
if (text[0] == '0' && (text[1] == 'x' || text[1] == 'X')) { base = 16; text = text + 2; }
while (*text) {
digit = *text;
if (digit >= '0' && digit <= '9') digit = digit - '0';
else if (digit >= 'a' && digit <= 'f') digit = digit - 'a' + 10;
else if (digit >= 'A' && digit <= 'F') digit = digit - 'A' + 10;
else return 0;
if (digit >= base) return 0;
if (base == 16) {
if (digits == 4) return 0;
number = number * 16 + digit;
} else {
limit = 7; if (negative) limit = 8;
if (number < -3276 || (number == -3276 && digit > limit)) return 0;
number = number * 10 - digit;
}
digits++; text++;
}
if (!digits) return 0;
number_ok = 1;
if (base == 16) { if (negative) return -number; return number; }
if (negative) return number;
return -number;
}
int editor_line(int line) {
int i; int current;
if (line < 1) return -1;
current = 1; i = 0;
while (i < editor_length && current < line) {
if (edit_buffer[i] == '\n') current++;
i++;
}
if (current == line) return i;
return -1;
}
int editor_end(int position) {
while (position < editor_length && edit_buffer[position] != '\n') position++;
if (position < editor_length) position++;
return position;
}
void editor_show(void) {
int i; int line;
i = 0; line = 1;
while (i < editor_length) {
print_dec(line); print(": ");
while (i < editor_length && edit_buffer[i] != '\n') bob_putc(edit_buffer[i++]);
if (i < editor_length) i++;
bob_putc('\n'); line++;
}
if (!editor_length) println("(empty)");
}
int editor_changed(char *name) {
int slot;
slot = file_find(name);
if (slot < 0) return editor_length != 0;
return strcmp(edit_buffer, file_content(slot)) != 0;
}
int editor_replace(int start, int end, char *text, int newline) {
int added; int delta; int i;
added = strlen(text) + newline;
delta = added - (end - start);
if (editor_length + delta >= FILE_WORDS) { println("File too large; change rejected."); return 0; }
memcpy(edit_undo, edit_buffer, editor_length + 1);
editor_undo_length = editor_length; editor_has_undo = 1;
if (delta > 0) {
for (i = editor_length; i >= end; i--) edit_buffer[i + delta] = edit_buffer[i];
} else {
for (i = end; i <= editor_length; i++) edit_buffer[i + delta] = edit_buffer[i];
}
memcpy(edit_buffer + start, text, added - newline);
if (newline) edit_buffer[start + added - 1] = '\n';
editor_length = editor_length + delta;
return 1;
}
int editor_save(char *name) {
if (file_write(name, edit_buffer, editor_length, 0) < 0) { println("Cannot save file."); return 0; }
println("Saved."); return 1;
}
void editor_append_line(char *text) {
char joined[129]; int length;
length = strlen(text);
if (editor_length && edit_buffer[editor_length - 1] != '\n') {
joined[0] = '\n'; memcpy(joined + 1, text, length + 1);
editor_replace(editor_length, editor_length, joined, 1);
} else editor_replace(editor_length, editor_length, text, 1);
}
void edit_file(char *name) {
int n; int slot; int line; int start; int end; int i; int temporary; char *command; char *text;
if (!*name || strlen(name) >= NAME_WORDS) { println("Invalid file name."); return; }
slot = file_find(name);
if (slot >= 0 && file_kinds[slot]) { println("Cannot edit a binary program."); return; }
editor_length = 0; editor_has_undo = 0;
if (slot >= 0) {
editor_length = file_lengths[slot];
memcpy(edit_buffer, file_content(slot), editor_length);
}
edit_buffer[editor_length] = 0;
println("Editor: text appends; :p lists; :i N TEXT inserts; :r N TEXT replaces.");
println(":d N deletes; :u undo/redo; :w saves; :q quits; :q! discards; . saves/quits.");
println(":p N shows one line. :q protects unsaved edits; :wq also saves/quits.");
editor_show();
while (1) {
print("edit> ");
n = read_line(edit_line, 128);
if (n == -2) { println("Edit cancelled at EOF."); return; }
if (n < 0) { println("Line too long; change rejected."); continue; }
if (strcmp(edit_line, ".") == 0 || strcmp(edit_line, ":wq") == 0) {
if (editor_save(name)) return;
} else if (strcmp(edit_line, ":q!") == 0) { println("Discarded unsaved changes."); return; }
else if (strcmp(edit_line, ":q") == 0) {
if (editor_changed(name)) println("Unsaved changes. Use :wq to save or :q! to discard.");
else return;
}
else if (strcmp(edit_line, ":p") == 0) editor_show();
else if (strcmp(edit_line, ":w") == 0) editor_save(name);
else if (strcmp(edit_line, ":u") == 0) {
if (!editor_has_undo) println("Nothing to undo.");
else {
end = editor_length; if (editor_undo_length > end) end = editor_undo_length;
for (i = 0; i <= end; i++) {
temporary = edit_buffer[i]; edit_buffer[i] = edit_undo[i]; edit_undo[i] = temporary;
}
temporary = editor_length; editor_length = editor_undo_length; editor_undo_length = temporary;
println("Undo/redo applied. Use :p to inspect.");
}
} else if (edit_line[0] == ':') {
arguments = edit_line; command = next_arg();
if (strcmp(command, ":p") == 0) {
line = parse_number(next_arg()); start = editor_line(line);
if (!number_ok || start < 0 || start == editor_length || *next_arg()) {
println("Usage: :p LINE (existing line number)"); continue;
}
print_dec(line); print(": "); end = editor_end(start);
for (i = start; i < end; i++) bob_putc(edit_buffer[i]);
if (end && edit_buffer[end - 1] != '\n') bob_putc('\n');
continue;
}
if (strcmp(command, ":a") == 0) { editor_append_line(remaining_args()); continue; }
if (strcmp(command, ":i") && strcmp(command, ":r") && strcmp(command, ":d")) {
println("Unknown editor command."); continue;
}
line = parse_number(next_arg()); text = remaining_args();
start = editor_line(line);
if (!number_ok || start < 0 || (start == editor_length && strcmp(command, ":i"))) {
println("Invalid line number."); continue;
}
end = start;
if (strcmp(command, ":i")) end = editor_end(start);
if (strcmp(command, ":d") == 0) editor_replace(start, end, "", 0);
else editor_replace(start, end, text, 1);
} else {
editor_append_line(edit_line);
}
}
}
void load_words(char *name) {
int slot; int count; int value; int words[511]; char *token;
count = 0;
while (1) {
token = next_arg(); if (!*token) break;
value = parse_number(token);
if (!number_ok || count == 511) { println("Invalid machine words."); return; }
words[count++] = value;
}
if (!count) { println("Usage: load name 0xWORD ... (finish with RET 0xE000)"); return; }
slot = file_write(name, words, count, 1);
if (slot < 0) println("Cannot create program file.");
else println("Loaded.");
}
void run_file(char *name) {
int slot; int status; int *program;
slot = file_find(name);
if (slot < 0 || !file_kinds[slot]) { println("Program not found. Use cc or load first."); return; }
program = (int *)PROGRAM_BASE;
memset(program, 0, PROGRAM_WORDS);
memcpy(program, file_content(slot), file_lengths[slot]);
program_length = file_lengths[slot];
status = bob_run(PROGRAM_BASE);
if (status == -2) println("Program fault; shell restored.");
else if (status == -3) println("Program timed out; shell restored.");
else { print("Exit "); print_dec(status); bob_putc('\n'); }
}
void memory_status(void) {
print("Heap "); print_hex(HEAP_BASE); print(".."); print_hex(0xdfff);
print("; free "); print_dec(HEAP_WORDS - heap_used); println(" words");
print("Programs "); print_hex(PROGRAM_BASE); print(".."); print_hex(0xbfff); println("");
println("Files: 8 slots, 511 words each; stack above 0xE000.");
}
void command_help(char *name) {
if (strcmp(name, "edit") == 0) println("edit NAME: open text; ordinary lines append. :p or :p N views; :i N TEXT inserts; :r N TEXT replaces; :d N deletes; :u undo/redo; :w saves; :q quits safely; :q! discards; . saves/quits.");
else if (strcmp(name, "cc") == 0 || strcmp(name, "go") == 0)
println("cc SOURCE [OUTPUT]: compile C; output defaults to app. go SOURCE [OUTPUT] also runs on success. Example: go bob.c. One int main(void); no includes. Use edit SOURCE to fix errors.");
else if (strcmp(name, "write") == 0) println("write NAME TEXT: create/replace a text file. Example: write note bob! Names: 1..23 characters without spaces; 8 file slots, 511 words each. Use edit for multiple lines.");
else if (strcmp(name, "read") == 0) println("read NAME: print text. Example: read bob.c. Use ls to see names; run executes program files.");
else if (strcmp(name, "ls") == 0 || strcmp(name, "dir") == 0 || strcmp(name, "list") == 0)
println("ls / dir / list: show names, lengths and text/program kinds. Files are in RAM and lost on exit.");
else if (strcmp(name, "copy") == 0 || strcmp(name, "rename") == 0 || strcmp(name, "delete") == 0)
println("copy OLD NEW | rename OLD NEW | delete NAME. Example: copy bob.c backup.c. Destinations must be new. Copy needs a free slot; rename does not. Delete is permanent.");
else if (strcmp(name, "run") == 0) println("run NAME: execute a compiled/loaded program. Example: cc bob.c bob, then run bob. Exit shows return value; faults/timeouts restore the shell.");
else if (strcmp(name, "load") == 0) println("load NAME 0xWORD ...: store machine words. Example: load empty 0x2180 0xE000, then run empty. RET 0xE000 returns; use cc for C source.");
else if (strcmp(name, "peek") == 0 || strcmp(name, "poke") == 0)
println("peek ADDRESS | poke ADDRESS VALUE: word addresses, hex 0x0000..0xFFFF or signed decimal. Example: poke 0xC000 98. Writes allowed only at 0xC000..0xDFFF. peek also shows signed value.");
else if (strcmp(name, "alloc") == 0 || strcmp(name, "mem") == 0)
println("alloc WORDS: allocate/zero 1..8192 heap words, print start address; no free. Example: alloc 16. mem shows remaining heap and memory regions.");
else if (strcmp(name, "echo") == 0) println("echo TEXT: print text. Example: echo bob!");
else if (strcmp(name, "clear") == 0) println("clear: clear an ANSI-capable terminal; does not remove files.");
else if (strcmp(name, "halt") == 0) println("halt: stop emulator. RAM files and allocations are lost; read important files before quitting.");
else if (strcmp(name, "help") == 0) println("help: list commands. help COMMAND: show usage and an example. Try help edit or help go.");
else println("No help for that command. Type help for available names.");
}
void shell_command(char *line) {
char *command; char *name; char *text; int value; int address; int *pointer;
arguments = line; command = next_arg();
if (!*command) return;
if (strcmp(command, "help") == 0) {
name = next_arg();
if (*name) { command_help(name); return; }
println("help | echo TEXT | clear | mem | halt");
println("peek ADDRESS | poke ADDRESS VALUE | alloc WORDS");
println("list / ls / dir | read NAME | write NAME TEXT | edit NAME");
println("copy OLD NEW | rename OLD NEW | delete NAME (permanent in RAM)");
println("load NAME 0xWORD ... | run NAME | cc SOURCE [OUTPUT] | go SOURCE [OUTPUT]");
println("cc/go default output: app. Try go bob.c to compile and print bob!");
println("Use hex addresses. poke is limited to heap RAM.");
println("Type help COMMAND for usage/examples. Try help edit or help go.");
} else if (strcmp(command, "echo") == 0) println(remaining_args());
else if (strcmp(command, "clear") == 0) print("\033[2J\033[H");
else if (strcmp(command, "mem") == 0) memory_status();
else if (strcmp(command, "halt") == 0) bob_halt();
else if (strcmp(command, "list") == 0 || strcmp(command, "ls") == 0 || strcmp(command, "dir") == 0) file_list();
else if (strcmp(command, "copy") == 0 || strcmp(command, "rename") == 0 || strcmp(command, "delete") == 0) {
name = next_arg(); text = next_arg();
if (*next_arg() || (strcmp(command, "delete") == 0 && *text)) {
println("Usage: copy OLD NEW | rename OLD NEW | delete NAME"); return;
}
file_manage(command, name, text);
} else if (strcmp(command, "read") == 0) {
value = file_find(next_arg());
if (value < 0) println("File not found.");
else if (file_kinds[value]) println("Binary program; use run.");
else println(file_content(value));
} else if (strcmp(command, "write") == 0) {
name = next_arg(); text = remaining_args();
if (file_write(name, text, strlen(text), 0) < 0) println("Cannot write file.");
else println("Saved.");
} else if (strcmp(command, "edit") == 0) edit_file(next_arg());
else if (strcmp(command, "load") == 0) { name = next_arg(); load_words(name); }
else if (strcmp(command, "run") == 0) run_file(next_arg());
else if (strcmp(command, "cc") == 0 || strcmp(command, "go") == 0) {
name = next_arg(); text = next_arg();
if (!*name || *next_arg()) { println("Usage: cc SOURCE [OUTPUT] | go SOURCE [OUTPUT]"); return; }
if (!*text) text = "app";
value = compile_command(name, text);
if (value && strcmp(command, "go") == 0) run_file(text);
} else if (strcmp(command, "alloc") == 0) {
value = parse_number(next_arg());
if (!number_ok) { println("Invalid allocation size."); return; }
pointer = alloc(value);
if (!pointer) println("Allocation failed.");
else { print_hex((int)pointer); bob_putc('\n'); }
} else if (strcmp(command, "peek") == 0 || strcmp(command, "poke") == 0) {
address = parse_number(next_arg());
if (!number_ok) { println("Invalid address."); return; }
pointer = (int *)address;
if (strcmp(command, "poke") == 0) {
value = parse_number(next_arg());
if (!number_ok) { println("Invalid value."); return; }
if (address >= 0 || address < -16384 || address >= -8192) {
println("poke is limited to 0xC000..0xDFFF."); return;
}
*pointer = value;
}
print_hex(address); print(": "); print_hex(*pointer); print(" ("); print_dec(*pointer); println(")");
} else println("Unknown command. Type help.");
}