This commit is contained in:
Alvin-Zilverstand committed 2026-10-08 01:02:30 +02:00
1 parent 96cd1ac97d
commit 6dfe517094
33 files changed
+9970 -3177

No files matched your search

+47 -20
View File
@@ -40,7 +40,9 @@ The kernel-only `bob_snapshot(int *descriptor, int operation)` is trap 7.
Operation 0 saves; 1 restores. The four-word descriptor contains pointers to
the 192-word names table, then eight-word length, kind and used tables. File
contents occupy the fixed `0xF000..0xFFFF` region. Descriptors can be on the
kernel stack; tables must be disjoint and below `0x9000`. The host validates
kernel stack; tables must be disjoint and below `0xA000` in bob16 mode. In
bob32 mode they may be in low kernel RAM or in the loaded kernel image, which
now resides above `0x10000`. The host validates
metadata, payload checksum and complete file length before committing a restore.
Returns 0 on success, 1 when restored binaries were omitted because the kernel
changed, -1 for storage errors, -2 for invalid data and -3 for invalid arguments.
@@ -52,14 +54,16 @@ user-facing save/restore workflow.
| --- | --- |
| `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 |
| `void memcpy(void *dst, void *src, int count)` | Copy count words forward; source and destination must not overlap |
| `void memset(void *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.
not host bytes. The bob16 heap reserves 8,192 words from 0xC000; bob32 reserves
4,096 words from 0xD000 so the enlarged kernel and compatibility-program stack
fit below it. The heap is shared with loaded programs. There is no free or reset
operation.
## RAM filesystem
@@ -67,9 +71,13 @@ The heap is shared with loaded programs. There is no free or reset operation.
`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.
kind 1 is machine code, kind 2 is resident bob32 C code, and kind 3 is a packed
native B32K v2 image. Its copy appends a zero word, which is not part of length.
Names hold at most 23 characters. bob16 retains eight fixed 511-word slots;
bob32 packs files into a shared 4,096-word content region. Text files remain
limited to 511 words; resident programs and native images can use the remaining
shared capacity (up to 4,095 words per file). Valid slot indices are 0..7.
These are internal kernel APIs.
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
@@ -89,27 +97,46 @@ kernel stack reserved at `0xE600..0xEFFF`. Input buffers are `0x0100..0x01FF`.
## 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.
`bob_run(entry)` invokes emulator trap 6. In bob16 mode valid entries are
0xA000..0xBDFF. In bob32 mode kind-1 legacy entries are 0xC200..0xCFFF and
kind-2 native entries are 0x20000..0xEFFFF when the caller explicitly supplies
a wide entry. The shell places legacy programs at 0xC200 in bob32 mode to avoid
overwriting the bob32 kernel image, and native bob32 files at 0x20000. The entry executes
with r6 at the top of its protected stack region, r5=0, and r7 pointing to the
reserved return sentinel. In bob32 mode kind-1 apps use `0xC200..0xCFFF` because
the expanded kernel occupies the old bob16 program window. Compatibility apps
retain access to the shared `0xD000..0xDFFF` heap. 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.
their application and stack regions; bob16 and compatibility applications may
also write the shared heap. 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.
The resident compiler uses variable words at 0x9800..0x980F in bob16 mode and
0xF0000..0xF000F in bob32 mode; string data is inline in the program image.
In bob16 its files can contain at most 511 code/data words; bob32 compiler output
uses the available packed filesystem capacity. Console and arithmetic 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.
In bob32 mode, `import32 HOST_PATH NAME` validates and packs a B32K v2 image
into a guest RAM file; `run NAME` loads and executes it. B32S version 2
snapshots preserve packed files; version 1 fixed-slot snapshots remain readable
and are converted to the packed layout on restore. `run32 HOST_PATH` remains
available for direct host-path launches.
Native C applications can include `bob_string.h` for small app-side string
and parsing helpers. It provides bounded integer formatting, status-returning
decimal/hex parsing, and in-place quoted tokenization without depending on the
kernel's private runtime functions.
+4 -1
View File
@@ -2,11 +2,14 @@
#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_puts(const char *text);
void bob_gets(char *buffer, int capacity);
void bob_halt(void);
int bob_getc(void);
int bob_run(int entry);
int bob_run_image(const char *path);
int bob_import_image(int *descriptor);
int bob_run_native(const int *descriptor);
int bob_snapshot(int *descriptor, int operation);
int bob_key(void);
int bob_terminal(void);
+125
View File
@@ -0,0 +1,125 @@
#ifndef BOB_STRING_H
#define BOB_STRING_H
/* Small word-addressed string helpers for native bob applications. */
int bob_strlen(const char *text) {
int length; length = 0;
while (text[length]) length++;
return length;
}
char *bob_strcpy(char *destination, const char *source) {
int i; i = 0;
while (source[i]) { destination[i] = source[i]; i++; }
destination[i] = 0;
return destination;
}
int bob_strcmp(const char *left, const char *right) {
int i; i = 0;
while (left[i] && left[i] == right[i]) i++;
return left[i] - right[i];
}
char *bob_strcat(char *destination, const char *source) {
int i; int j;
i = bob_strlen(destination); j = 0;
while (source[j]) destination[i++] = source[j++];
destination[i] = 0;
return destination;
}
char *bob_strchr(const char *text, int character) {
while (*text && *text != character) text++;
if (*text == character) return (char *)text;
return 0;
}
int bob_isspace(int character) {
return character == ' ' || character == '\t' || character == '\n' ||
character == '\r' || character == '\v' || character == '\f';
}
/* Decimal and 0x-prefixed signed integers. valid is cleared on any error. */
int bob_parse_int(const char *text, int *valid) {
int negative; int base; int digit; int digits;
unsigned int value; unsigned int limit; unsigned int radix;
const char *cursor;
*valid = 0; cursor = text; negative = 0; base = 10; digits = 0; value = 0;
while (bob_isspace(*cursor)) cursor++;
if (*cursor == '+' || *cursor == '-') {
negative = *cursor == '-'; cursor++;
}
if (cursor[0] == '0' && (cursor[1] == 'x' || cursor[1] == 'X')) {
base = 16; cursor += 2;
}
radix = (unsigned int)base;
limit = negative ? 0x80000000U : 0x7fffffffU;
while (*cursor) {
if (bob_isspace(*cursor)) break;
digit = *cursor;
if (digit >= '0' && digit <= '9') 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 || value > limit / radix ||
(value == limit / radix && (unsigned int)digit > limit % radix)) return 0;
value = value * radix + (unsigned int)digit;
digits++; cursor++;
}
while (bob_isspace(*cursor)) cursor++;
if (!digits || *cursor) return 0;
*valid = 1;
if (!negative) return (int)value;
if (value == 0x80000000U) return -2147483647 - 1;
return -(int)value;
}
/* Returns the character count, or -1 when the destination is too small. */
int bob_format_int(int value, char *destination, int capacity) {
char reverse[11];
unsigned int magnitude; int negative; int count; int i;
if (capacity < 1) return -1;
negative = value < 0;
magnitude = negative ? 0U - (unsigned int)value : (unsigned int)value;
count = 0;
do {
reverse[count++] = (char)('0' + magnitude % 10U);
magnitude /= 10U;
} while (magnitude);
if (count + negative + 1 > capacity) return -1;
i = 0;
if (negative) destination[i++] = '-';
while (count) destination[i++] = reverse[--count];
destination[i] = 0;
return i;
}
/* In-place shell-like token reader: whitespace separates, quotes group, and
backslash quotes the next character. Returns 1/token, 0/end, or -1/bad quote. */
int bob_token_next(char **cursor, char **token) {
char *scan; char *write; int quote; int character;
if (!cursor || !token || !*cursor) return -1;
scan = *cursor;
while (bob_isspace(*scan)) scan++;
if (!*scan) { *cursor = scan; *token = 0; return 0; }
write = scan; *token = write; quote = 0;
while (*scan) {
character = *scan;
if (quote) {
if (character == quote) { quote = 0; scan++; continue; }
if (character == '\\' && scan[1]) scan++;
} else {
if (bob_isspace(character)) break;
if (character == '"' || character == '\'') { quote = character; scan++; continue; }
if (character == '\\' && scan[1]) scan++;
}
*write++ = *scan++;
}
if (quote) { *token = 0; *cursor = scan; return -1; }
while (bob_isspace(*scan)) scan++;
*write = 0; *cursor = scan;
return 1;
}
#endif
+733 -40
View File
File diff suppressed because it is too large. Load diff
+102 -7
View File
@@ -8,9 +8,17 @@ void file_snapshot(int operation) {
descriptor[0] = (int)file_names; descriptor[1] = (int)file_lengths;
descriptor[2] = (int)file_kinds; descriptor[3] = (int)file_used;
result = bob_snapshot(descriptor, operation);
#ifdef BOBC_WIDE
if (result == -1) println("Storage unavailable. Check bob-files.b32 location/permissions (or BOB16_STORAGE).");
#else
if (result == -1) println("Storage unavailable. Check bob-files.b16 location/permissions (or BOB16_STORAGE).");
#endif
else if (result < 0) println("Invalid saved files; RAM files unchanged.");
#ifdef BOBC_WIDE
else if (operation == 0) println("Files saved to bob-files.b32 (or BOB16_STORAGE).");
#else
else if (operation == 0) println("Files saved to bob-files.b16 (or BOB16_STORAGE).");
#endif
else {
println("Files restored; previous RAM files replaced.");
if (result == 1) println("Kernel changed: old programs omitted. Compile restored C sources again.");
@@ -20,7 +28,16 @@ void file_snapshot(int operation) {
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_content(int slot) {
#ifdef BOBC_WIDE
int i; int offset;
offset = 0;
for (i = 0; i < slot; i++) if (file_used[i]) offset += file_lengths[i] + 1;
return (int *)0xf000 + offset;
#else
return (int *)0xf000 + slot * FILE_WORDS;
#endif
}
int file_find(char *name) {
int i;
for (i = 0; i < FILE_COUNT; i++)
@@ -44,16 +61,79 @@ int file_slot(char *name) {
}
return -1;
}
int file_write(char *name, int *data, int length, int kind) {
int file_prepare_native(char *name) {
int i; int count; int length;
length = strlen(name);
if (!length || length >= NAME_WORDS || file_find(name) >= 0) return -1;
for (i = 0; i < length; i++) if (name[i] < 33 || name[i] > 126) return -1;
count = 0;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i]) {
if (i != count) {
memcpy(file_name(count), file_name(i), NAME_WORDS);
file_lengths[count] = file_lengths[i]; file_kinds[count] = file_kinds[i]; file_used[count] = 1;
file_used[i] = 0;
}
count++;
}
if (count == FILE_COUNT) return -1;
memset(file_name(count), 0, NAME_WORDS); memcpy(file_name(count), name, length);
file_lengths[count] = 0; file_kinds[count] = 3; file_used[count] = 1;
return count;
}
int file_write(char *name, void *data, int length, int kind) {
#ifdef BOBC_WIDE
int slot; int i; int used; int available; int targetOffset; int sourceSlot;
if (length < 0 || length >= FILE_WORDS || (!kind && length >= TEXT_WORDS)) return -1;
if (!kind) for (i = 0; i < length; i++) if (!((int *)data)[i] || ((int *)data)[i] > 255) return -1;
int oldSlot = file_find(name);
slot = file_slot(name);
if (slot < 0) return -1;
sourceSlot = -1;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && data == file_content(i)) sourceSlot = i;
used = 0;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) used += file_lengths[i] + 1;
available = FILE_WORDS - used;
if (length + 1 > available) {
if (oldSlot < 0) { file_used[slot] = 0; file_lengths[slot] = 0; file_kinds[slot] = 0; memset(file_name(slot), 0, NAME_WORDS); }
return -1;
}
if (sourceSlot == slot) return slot;
/* Compact first so variable-sized files never leave unusable gaps. */
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) {
int *source; int *destination; int j;
source = (int *)0xf000; destination = (int *)0xf000;
for (j = 0; j < i; j++) if (file_used[j] && j != slot) destination += file_lengths[j] + 1;
for (j = 0; j < i; j++) if (file_used[j]) source += file_lengths[j] + 1;
if (source != destination && destination < source)
for (j = 0; j <= file_lengths[i]; j++) destination[j] = source[j];
}
/* Insert the rewritten target at its slot-ordered offset. */
targetOffset = 0;
for (i = 0; i < slot; i++) if (file_used[i] && i != slot) targetOffset += file_lengths[i] + 1;
{ int total = 0;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) total += file_lengths[i] + 1;
for (i = total - 1; i >= targetOffset; i--) ((int *)0xf000)[i + length + 1] = ((int *)0xf000)[i];
if (sourceSlot >= 0) {
int sourceOffset = 0;
for (i = 0; i < sourceSlot; i++) if (file_used[i] && i != slot) sourceOffset += file_lengths[i] + 1;
if (sourceSlot > slot) sourceOffset += length + 1;
data = (int *)0xf000 + sourceOffset;
}
memcpy((int *)0xf000 + targetOffset, data, length);
((int *)0xf000)[targetOffset + length] = 0;
file_lengths[slot] = length; file_kinds[slot] = kind;
}
return slot;
#else
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;
file_lengths[slot] = length; file_kinds[slot] = kind;
return slot;
#endif
}
void file_list(void) {
int i; int count;
@@ -61,13 +141,19 @@ void file_list(void) {
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)");
if (file_kinds[i] == 3) println(" words (native bob32 image)");
else if (file_kinds[i]) println(" words (program)");
else println(" chars (text)");
count++;
}
}
if (!count) println("No files.");
print("Used "); print_dec(count); println("/8 slots; limit 511 words per file. delete frees a slot.");
print("Used "); print_dec(count);
#ifdef BOBC_WIDE
println("/8 slots; shared 4096-word storage. Text/resident files max 511; native apps use remaining space.");
#else
println("/8 slots; limit 511 words per file. delete frees a slot.");
#endif
}
void file_manage(char *operation, char *name, char *destination) {
int slot; int target;
@@ -75,9 +161,18 @@ void file_manage(char *operation, char *name, char *destination) {
slot = file_find(name);
if (slot < 0) { println("File not found. Use ls to see names."); return; }
if (strcmp(operation, "delete") == 0) {
#ifdef BOBC_WIDE
int sourceOffset; int destinationOffset; int i; int j;
sourceOffset = 0; destinationOffset = 0;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i]) {
if (i == slot) { sourceOffset += file_lengths[i] + 1; continue; }
if (sourceOffset != destinationOffset)
for (j = 0; j <= file_lengths[i]; j++) ((int *)0xf000)[destinationOffset + j] = ((int *)0xf000)[sourceOffset + j];
sourceOffset += file_lengths[i] + 1; destinationOffset += file_lengths[i] + 1;
}
#endif
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) {
+4
View File
@@ -44,7 +44,11 @@ void input_complete(void) {
count = input_cursor - start; matches = 0;
if (input_cursor != input_length) return;
if (!start) {
#ifdef BOBC_WIDE
words = "help echo clear mem halt list ls dir read write edit load run run32 cc go copy rename delete alloc peek poke save restore";
#else
words = "help echo clear mem halt list ls dir read write edit load run cc go copy rename delete alloc peek poke save restore";
#endif
while (*words) {
length = 0;
while (*words && *words != ' ') candidate[length++] = *words++;
+4
View File
@@ -9,7 +9,11 @@
int main(void) {
int length; char *sample;
println("bob!");
#ifdef BOBC_WIDE
println("bob32 OS: help COMMAND for usage; go bob.c runs the example.");
#else
println("bob16 OS: help COMMAND for usage; go bob.c runs the example.");
#endif
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);
+20 -2
View File
@@ -1,5 +1,10 @@
int heap_used;
#ifdef BOBC_WIDE
int bit_masks[32] = {(-2147483647-1),1073741824,536870912,268435456,134217728,67108864,33554432,16777216,
8388608,4194304,2097152,1048576,524288,262144,131072,65536,32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
#else
int bit_masks[16] = {-32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
#endif
int bit_weights[4] = {8,4,2,1};
int strlen(char *text) { int n; n = 0; while (text[n]) n++; return n; }
@@ -8,12 +13,21 @@ int strcmp(char *a, char *b) {
while (a[i] && a[i] == b[i]) i++;
return a[i] - b[i];
}
void memcpy(int *dst, int *src, int count) {
#ifdef BOBC_WIDE
void memcpy(void *dst, void *src, int count) {
int i; for (i = 0; i < count; i++) ((int *)dst)[i] = ((int *)src)[i];
}
void memset(void *dst, int value, int count) {
int i; for (i = 0; i < count; i++) ((int *)dst)[i] = value;
}
#else
void word_copy(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) {
void word_fill(int *dst, int value, int count) {
int i; for (i = 0; i < count; i++) dst[i] = value;
}
#endif
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) {
@@ -31,7 +45,11 @@ void print_dec(int number) {
void print_hex(int number) {
int nibble; int bit; int digit;
print("0x");
#ifdef BOBC_WIDE
for (nibble = 0; nibble < 8; nibble++) {
#else
for (nibble = 0; nibble < 4; nibble++) {
#endif
digit = 0;
for (bit = 0; bit < 4; bit++) {
if (number & bit_masks[nibble * 4 + bit]) digit = digit + bit_weights[bit];
+29 -4
View File
@@ -1,17 +1,42 @@
#ifndef BOB_RUNTIME_H
#define BOB_RUNTIME_H
#include "bob.h"
#if BOBC_WIDE
#define HEAP_BASE 0xd000
#define HEAP_WORDS 4096
#define PROGRAM_BASE 0xc200
#define PROGRAM_WORDS 3584
#else
#define HEAP_BASE 0xc000
#define HEAP_WORDS 8192
#define PROGRAM_BASE 0x9000
#define PROGRAM_WORDS 12288
#define PROGRAM_BASE 0xa000
#define PROGRAM_WORDS 8192
#endif
#define WIDE_PROGRAM_BASE 0x20000
#define WIDE_VARIABLE_BASE 0xf0000
#define FILE_COUNT 8
#define TEXT_WORDS 512
#if BOBC_WIDE
#define FILE_WORDS 4096
#else
#define FILE_WORDS 512
#endif
#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);
int file_find(char *name);
int file_prepare_native(char *name);
char *file_name(int slot);
int *file_content(int slot);
#if BOBC_WIDE
void memcpy(void *dst, void *src, int count);
void memset(void *dst, int value, int count);
#else
void word_copy(int *dst, int *src, int count);
void word_fill(int *dst, int value, int count);
#define memcpy(dst, src, count) word_copy((int *)(dst), (int *)(src), (count))
#define memset(dst, value, count) word_fill((int *)(dst), (value), (count))
#endif
void print(char *text);
void println(char *text);
void print_dec(int number);
+117 -17
View File
@@ -80,13 +80,13 @@ 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;
return strcmp(edit_buffer, (char *)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; }
if (editor_length + delta >= TEXT_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) {
@@ -195,10 +195,36 @@ void load_words(char *name) {
if (slot < 0) println("Cannot create program file.");
else println("Loaded.");
}
void run_file(char *name) {
void run_file(char *name, char *app_arguments) {
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; }
if (slot < 0 || !file_kinds[slot]) { println("Program not found. Use cc, load, or import32 first."); return; }
#ifdef BOBC_WIDE
if (file_kinds[slot] == 3) {
int descriptor[4];
descriptor[0] = (int)file_content(slot); descriptor[1] = file_lengths[slot];
descriptor[2] = (int)app_arguments;
descriptor[3] = (int)name;
status = bob_run_native(descriptor);
if (status == -4) println("Invalid or unsupported native bob32 image.");
else if (status == -5) println("Not enough emulator memory to load this app.");
else 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'); }
return;
}
if (file_kinds[slot] == 2) {
program = (int *)WIDE_PROGRAM_BASE;
memset(program, 0, file_lengths[slot] + 1);
memcpy(program, file_content(slot), file_lengths[slot]);
memset((int *)WIDE_VARIABLE_BASE, 0, 16);
status = bob_run(WIDE_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'); }
return;
}
#endif
program = (int *)PROGRAM_BASE;
memset(program, 0, PROGRAM_WORDS);
memcpy(program, file_content(slot), file_lengths[slot]);
@@ -207,19 +233,65 @@ void run_file(char *name) {
else if (status == -3) println("Program timed out; shell restored.");
else { print("Exit "); print_dec(status); bob_putc('\n'); }
}
void import_wide_image(char *path, char *name) {
int slot; int descriptor[3]; int result; int i; int used;
slot = file_prepare_native(name);
if (slot < 0) { println("Cannot import: invalid name, duplicate file, or all 8 slots are used."); return; }
used = 0; for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) used += file_lengths[i] + 1;
descriptor[0] = (int)path; descriptor[1] = (int)file_content(slot); descriptor[2] = FILE_WORDS - used;
result = bob_import_image(descriptor);
if (result < 0) {
file_used[slot] = 0; file_lengths[slot] = 0; file_kinds[slot] = 0; memset(file_name(slot), 0, NAME_WORDS);
if (result == -1) println("Cannot read host app image path.");
else if (result == -5) println("Not enough RAM filesystem space for this app image.");
else println("Invalid, truncated, corrupt, or unsupported B32K app image.");
return;
}
file_content(slot)[result] = 0; file_lengths[slot] = result; file_kinds[slot] = 3;
print("Imported native app as "); print(name); print(". Run it with: run "); println(name);
}
#ifdef BOBC_WIDE
void run_wide_image(char *path) {
int status;
status = bob_run_image(path);
if (status == -5) println("Cannot read B32 app image.");
else if (status == -4) println("Invalid B32 app image or unsupported address range.");
else 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'); }
}
#endif
void memory_status(void) {
print("Heap "); print_hex(HEAP_BASE); print(".."); print_hex(0xdfff);
print("Heap "); print_hex(HEAP_BASE); print(".."); print_hex(HEAP_BASE + HEAP_WORDS - 1);
print("; free "); print_dec(HEAP_WORDS - heap_used); println(" words");
#ifdef BOBC_WIDE
print("bob16 programs 0x0000A000..0x0000BDFF; bob32 legacy programs "); print_hex(PROGRAM_BASE); print("..0x0000CFFF; native bob32 programs "); print_hex(WIDE_PROGRAM_BASE); print("..0x000EFFFF"); println("");
#else
print("Programs "); print_hex(PROGRAM_BASE); print(".."); print_hex(0xbfff); println("");
#endif
#ifdef BOBC_WIDE
println("Files: 8 slots, 4096 words total; text files up to 511 words; native apps share remaining space.");
#else
println("Files: 8 slots, 511 words each; stack above 0xE600.");
#endif
}
void command_help(char *name) {
if (strcmp(name, "save") == 0 || strcmp(name, "restore") == 0) {
println("save: snapshot all files to bob-files.b16 in the working directory (or BOB16_STORAGE). restore yes: replace RAM files with that snapshot. Save current work first. Changed kernels restore text only; recompile programs."); return;
#ifdef BOBC_WIDE
println("save: snapshot all files to bob-files.b32 in the working directory (or BOB16_STORAGE). restore yes: replace RAM files with that snapshot. Save current work first. Changed kernels restore text only; recompile programs.");
#else
println("save: snapshot all files to bob-files.b16 in the working directory (or BOB16_STORAGE). restore yes: replace RAM files with that snapshot. Save current work first. Changed kernels restore text only; recompile programs.");
#endif
return;
}
if (strcmp(name, "edit") == 0) println("edit NAME: type/arrows; Ctrl+O saves, Ctrl+X exits, Ctrl+Z undo/redo. Unsaved exit asks Y/N. Redirected input uses the line editor; see C_OS.md.");
else if (strcmp(name, "cc") == 0 || strcmp(name, "go") == 0)
else if (strcmp(name, "cc") == 0 || strcmp(name, "go") == 0) {
#ifdef BOBC_WIDE
println("cc SOURCE [OUTPUT]: compile a native 32-bit C program; output defaults to app. Code can use available RAM filesystem space. go also runs it. 32-bit int; ?:, do/while, arrays; one main; no includes.");
#else
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.");
#endif
}
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)
@@ -227,18 +299,27 @@ void command_help(char *name) {
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.");
#ifdef BOBC_WIDE
else if (strcmp(name, "run32") == 0) println("run32 PATH: load and run a B32K v2 application image in the bob32 OS. Example: run32 build/wide-app.b32. Compile with bobcc --wide-app.");
else if (strcmp(name, "import32") == 0) println("import32 HOST_PATH GUEST_NAME: import a B32K v2 image into RAM files; then run GUEST_NAME and save to keep it in B32S.");
#endif
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.");
println("peek ADDRESS | poke ADDRESS VALUE: word addresses, hex 0x0000..0xFFFF or signed decimal. Example: poke 0xD000 98. Writes allowed only in heap RAM. 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.");
println("alloc WORDS: allocate/zero 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. Unsaved RAM files and allocations are lost; use save 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) {
#ifdef BOBC_WIDE
#define SHELL_COMMAND_BODY shell_command_normal
#else
#define SHELL_COMMAND_BODY shell_command
#endif
void SHELL_COMMAND_BODY(char *line) {
char *command; char *name; char *text; int value; int address; int *pointer;
arguments = line; command = next_arg();
if (!*command) return;
@@ -250,6 +331,10 @@ void shell_command(char *line) {
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]");
#ifdef BOBC_WIDE
println("run32 PATH: run a native bob32 B32K v2 application image.");
println("import32 HOST_PATH GUEST_NAME: import once, then run the guest filename.");
#endif
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.");
@@ -276,23 +361,23 @@ void shell_command(char *line) {
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 println((char *)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. Names: 1..23 printable characters; 8 slots. Use ls or help write.");
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, "run") == 0) { name = next_arg(); run_file(name, remaining_args()); }
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);
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. Use 1..8192 words; mem shows available space."); return; }
if (!number_ok) { println("Invalid allocation size. Use a positive word count; mem shows available space."); return; }
pointer = alloc(value);
if (!pointer) println("Allocation failed. Use a positive size within free space; see mem. No free command.");
else { print_hex((int)pointer); bob_putc('\n'); }
@@ -303,11 +388,26 @@ void shell_command(char *line) {
if (strcmp(command, "poke") == 0) {
value = parse_number(next_arg());
if (!number_ok) { println("Invalid value. Use -32768..65535 or 0x0000..0xFFFF."); return; }
if (address >= 0 || address < -16384 || address >= -8192) {
println("poke is limited to 0xC000..0xDFFF."); return;
}
#ifdef BOBC_WIDE
if (address < 0xd000 || address > 0xdfff) { println("poke is limited to heap RAM."); return; }
#else
if (address >= 0 || address < -16384 || address >= -8192) { println("poke is limited to 0xC000..0xDFFF."); return; }
#endif
*pointer = value;
}
print_hex(address); print(": "); print_hex(*pointer); print(" ("); print_dec(*pointer); println(")");
} else println("Unknown command. Type help.");
}
#ifdef BOBC_WIDE
void shell_command(char *line) {
if(line[0]=='r' && line[1]=='u' && line[2]=='n' && line[3]=='3' && line[4]=='2' && (!line[5] || line[5]==' ')) {
arguments=line+5;char *path=next_arg();int slot;
if(!*path)println("Usage: run32 PATH [ARG ...]");
else {slot=file_find(path);if(slot>=0 && file_kinds[slot]==3)run_file(path,remaining_args());else if(*remaining_args())println("Arguments require an imported native app.");else run_wide_image(path);}
} else if(line[0]=='i' && line[1]=='m' && line[2]=='p' && line[3]=='o' && line[4]=='r' && line[5]=='t' && line[6]=='3' && line[7]=='2' && (!line[8] || line[8]==' ')) {
arguments=line+8;char *path=next_arg();char *name=next_arg();
if(!*path || !*name || *next_arg())println("Usage: import32 HOST_PATH GUEST_NAME");
else import_wide_image(path,name);
} else shell_command_normal(line);
}
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef BOB_STDBOOL_H
#define BOB_STDBOOL_H
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef BOB_STDDEF_H
#define BOB_STDDEF_H
typedef unsigned int size_t;
typedef int ptrdiff_t;
#ifndef NULL
#define NULL ((void *)0)
#endif
#endif