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

+181 -34
View File
@@ -110,6 +110,34 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#ifdef _WIN32
#include <windows.h>
#endif
/* Kernel disk exposed to the guest boot ROM through trap 4. */
static uint16_t bootDisk[0x10000];
static uint16_t bootWords;
static bool bootMode;
static bool programMode;
static bool programDone;
static jmp_buf programFault;
static void machineFault(void) {
if (programMode) longjmp(programFault, 1);
exit(1);
}
static unsigned readWord(FILE *file) {
int lo = fgetc(file), hi = fgetc(file);
if (lo == EOF || hi == EOF) {
fprintf(stderr, "Truncated bob16 kernel image\n");
exit(1);
}
return (unsigned)lo | ((unsigned)hi << 8);
}
static void bootImage(const char *path);
typedef uint16_t ureg_t;
typedef int16_t reg_t;
@@ -164,6 +192,35 @@ struct ram_t {
reg_t mdr;
} ram;
void cpuCycle(void);
static int runProgram(uint16_t entry) {
if (programMode || entry < 0x9000 || entry >= 0xbe00) return -1;
struct cpu_t saved = cpu;
ureg_t savedMar = ram.mar;
reg_t savedMdr = ram.mdr;
volatile int status = -2;
programMode = true;
programDone = false;
if (setjmp(programFault) == 0) {
cpu.pc = entry;
cpu.regFile[6] = (int16_t)0xc000;
cpu.regFile[5] = 0;
cpu.regFile[7] = 0x80;
unsigned cycles = 0;
while (!programDone && cpu.pc != 0x80 && cycles < 5000000) {
cpuCycle();
++cycles;
}
status = cycles == 5000000 ? -3 : cpu.regFile[0];
}
programMode = false;
cpu = saved;
ram.mar = savedMar;
ram.mdr = savedMdr;
return status;
}
int sext(int16_t val, uint16_t length) {
int n = 16 - length;
return (int16_t)((val << n)) >> n;
@@ -210,8 +267,8 @@ void execute() {
switch ((cpu.ir >> 7) & 0x3) {
case 0:
if (cpu.ir & 0b1) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 4) & 0x7] + cpu.regFile[(cpu.ir >> 1) & 0x7];
@@ -221,8 +278,8 @@ void execute() {
break;
case 2:
if (cpu.ir & 0xF) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 9) & 0x7] + cpu.regFile[(cpu.ir >> 4) & 0x7];
@@ -237,8 +294,8 @@ void execute() {
switch ((cpu.ir >> 7) & 0x3) {
case 0:
if (cpu.ir & 0x1) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 4) & 0x7] & cpu.regFile[(cpu.ir >> 1) & 0x7];
@@ -247,9 +304,9 @@ void execute() {
cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 4) & 0x7] & sext(cpu.ir & 0xF, 4);
break;
case 2:
if (cpu.ir & 0x1F) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
if (cpu.ir & 0xF) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 9) & 0x7] & cpu.regFile[(cpu.ir >> 4) & 0x7];
@@ -262,8 +319,8 @@ void execute() {
break;
case NOT:
if (cpu.ir & 0x1F) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
if (cpu.ir & 0x100) {
@@ -297,21 +354,24 @@ void execute() {
break;
case JMP:
if (cpu.ir & 0x1FF) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
cpu.pc = cpu.regFile[(cpu.ir >> 9) & 0x7];
break;
case JSR:
{
ureg_t target = (ureg_t)cpu.regFile[(cpu.ir >> 8) & 0x7];
cpu.regFile[7] = cpu.pc;
if (!(cpu.ir & 0x800)) {
if (cpu.ir & 0x800) {
cpu.pc += sext(cpu.ir & 0x7FF, 11);
} else {
cpu.pc = cpu.regFile[(cpu.ir >> 8) & 0x7];
cpu.pc = target;
}
break;
}
case LEA:
break;
case RET:
@@ -319,22 +379,27 @@ void execute() {
break;
case TRAP:
if (cpu.ir & 0xFF) {
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, ram.memory[cpu.pc - 1]);
exit(-1);
printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1));
machineFault();
}
switch ((cpu.ir >> 8) & 0xF) {
case HALT:
if (programMode) { programDone = true; break; }
exit(0);
break;
case PUTC:
putchar(cpu.regFile[0] & 0xFF);
break;
case PUTS:
{
unsigned remaining = 0x10000;
cpu.regFile[7] = cpu.pc;
cpu.pc = cpu.regFile[0];
while (cpu.ir != 0) {
if (!remaining--) machineFault();
cpu.ir = ram.memory[cpu.pc];
if (cpu.ir == 0) break;
cpu.regFile[0] = cpu.ir;
putchar(cpu.regFile[0] & 0xFF);
cpu.pc++;
@@ -344,17 +409,49 @@ void execute() {
cpu.pc = cpu.regFile[7];
break;
}
case GETS:
int n = cpu.regFile[1];
{
int n = (uint16_t)cpu.regFile[1];
cpu.regFile[7] = cpu.pc;
char buf[n];
fgets(buf, n, stdin);
for (int i = 0; i < strlen(buf); i++) {
ram.memory[cpu.regFile[0] + i] = buf[i] & 0xFF;
unsigned address = (uint16_t)cpu.regFile[0];
if (n < 2 || address + (unsigned)n > 0x10000 ||
(programMode && (address < 0x9000 || address + (unsigned)n > 0xe000))) {
fprintf(stderr, "Invalid input buffer\n"); machineFault();
}
char *buf = calloc((size_t)n, 1);
if (!buf) machineFault();
if (!fgets(buf, n, stdin)) buf[0] = '\0';
for (size_t i = 0; i <= strlen(buf); i++) {
ram.memory[address + i] = buf[i] & 0xFF;
}
free(buf);
cpu.pc = cpu.regFile[7];
break;
}
case 4: /* disk_read(dst=r0, word_offset=r1, count=r2) */
{
unsigned dst = (uint16_t)cpu.regFile[0];
unsigned offset = (uint16_t)cpu.regFile[1];
unsigned count = (uint16_t)cpu.regFile[2];
if (programMode || !bootMode || offset + count > bootWords ||
dst < 0x100 || dst + count > 0xf000) {
fprintf(stderr, "Invalid boot disk read\n"); machineFault();
}
for (unsigned i = 0; i < count; ++i)
ram.memory[dst + i] = (int16_t)bootDisk[offset + i];
cpu.regFile[0] = (int16_t)count;
break;
}
case 5:
cpu.regFile[0] = (int16_t)getchar();
break;
case 6:
cpu.regFile[0] = (int16_t)runProgram((uint16_t)cpu.regFile[0]);
break;
default:
machineFault();
}
break;
@@ -368,6 +465,7 @@ void storeResult() {
cpu.regFile[dest] = ram.mdr;
updateCC(cpu.regFile[dest]);
} else if (opcode == ST || opcode == STI || opcode == STR) {
if (programMode && (ram.mar < 0x9000 || ram.mar >= 0xe000)) machineFault();
ram.memory[ram.mar] = cpu.regFile[dest];
} else if (opcode == ADD || opcode == AND || opcode == NOT) {
cpu.regFile[dest] = cpu.alu.accumulator;
@@ -392,7 +490,7 @@ int parseReg(const char* s) {
return -1;
}
if (strlen(s) != 2 || s[0] != 'R' && s[0] != 'r') {
if (strlen(s) != 2 || (s[0] != 'R' && s[0] != 'r')) {
//incorrect format
return -2;
}
@@ -413,7 +511,7 @@ int assemble() {
setbuf(stdout, NULL);
printf("Enter source assembly file name: \n");
fgets(fileName, 256, stdin);
if (!fgets(fileName, 256, stdin)) return -8;
fileName[strcspn(fileName, "\n")] = '\0';
file = fopen(fileName, "r");
if (!file) {
@@ -470,7 +568,7 @@ int assemble() {
return -4;
}
for (int i = 0; i < strlen(tokens[1]); i++) {
for (size_t i = 0; i < strlen(tokens[1]); i++) {
ram.memory[instructionCount] = tokens[1][i] & 0x00FF;
instructionCount++;
}
@@ -489,14 +587,14 @@ int assemble() {
int dst = parseReg(tokens[1]);
int src0 = parseReg(tokens[2]);
int src1 = parseReg(tokens[3]);
if (dst < 0 || src1 < 0) {
if (dst < 0 || src0 < 0) {
printf("Wrong tokens on line %d!\n", lineCount);
return -3;
}
instruction += (dst << 9);
instruction += (src0 << 4);
if (src0 < 0) { //imm
if (src1 < 0) { //imm
instruction += (1 << 7);
int imm = atoi(tokens[3]);
if (imm > 7) {
@@ -522,7 +620,7 @@ int assemble() {
instruction += (dst << 9);
if (src < 0) { //imm
instruction += (4 << 7);
instruction += (3 << 7);
int imm = atoi(tokens[2]);
if (imm > 63) {
imm = 63;
@@ -534,7 +632,7 @@ int assemble() {
instruction += imm & 0x7F;
} else { //normal
instruction += (3 << 7);
instruction += (2 << 7);
instruction += (src << 4);
}
@@ -581,9 +679,9 @@ int assemble() {
return -3;
}
instruction += (r0 << 7);
instruction += (r0 << 9);
if (r1 < 0) { //imm
instruction += (4 << 10);
instruction += (3 << 7);
int imm = atoi(tokens[2]);
if (imm > 63) {
imm = 63;
@@ -595,7 +693,7 @@ int assemble() {
instruction += imm & 0x7F;
} else { //normal
instruction += (3 << 7);
instruction += (2 << 7);
instruction += (r1 << 4);
}
@@ -790,7 +888,7 @@ int assemble() {
}
int conditionCodes = 0;
for (int i = 0; i < l; i++) {
for (size_t i = 0; i < l; i++) {
if (tokens[1][i] == 'n') {
conditionCodes |= 0b100;
} else if (tokens[1][i] == 'z') {
@@ -857,7 +955,7 @@ int assemble() {
return -4;
}
int r0 = parseReg(tokens[2]);
int r0 = parseReg(tokens[1]);
if (r0 < 0) {
printf("Wrong tokens on line %d!\n", lineCount);
return -3;
@@ -931,16 +1029,65 @@ int assemble() {
}
printf("Starting execution!\n");
fclose(file);
return 0;
}
static void bootImage(const char *path) {
FILE *file = fopen(path, "rb");
if (!file) { perror(path); exit(1); }
char magic[4];
if (fread(magic, 1, 4, file) != 4 || memcmp(magic, "B16K", 4)) {
fprintf(stderr, "Not a bob16 kernel image\n"); exit(1);
}
unsigned version = readWord(file), origin = readWord(file);
unsigned entry = readWord(file), words = readWord(file), checksum = readWord(file);
if (version != 1 || !words || origin < 0x100 || origin + words > 0x9000 ||
entry < origin || entry >= origin + words) {
fprintf(stderr, "Invalid bob16 kernel header\n"); exit(1);
}
unsigned sum = 0;
for (unsigned i = 0; i < words; ++i) { bootDisk[i] = readWord(file); sum += bootDisk[i]; }
if ((sum & 0xffff) != checksum || fgetc(file) != EOF) {
fprintf(stderr, "Invalid kernel checksum or length\n"); exit(1);
}
fclose(file);
bootWords = (uint16_t)words;
bootMode = true;
memset(&cpu, 0, sizeof(cpu));
memset(&ram, 0, sizeof(ram));
/* Real guest instructions: load disk parameters, read kernel, jump to entry. */
uint16_t rom[] = {0x4007, 0x4207, 0x4407, 0xf400, 0x4606, 0xb600,
0xf000, 0, (uint16_t)origin, 0, (uint16_t)words, (uint16_t)entry};
for (unsigned i = 0; i < sizeof(rom) / sizeof(rom[0]); ++i) ram.memory[i] = (int16_t)rom[i];
updateCC(0);
}
int main(int argc, char** argv) {
#ifdef _WIN32
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
if (GetConsoleMode(console, &mode)) SetConsoleMode(console, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
unsigned long maxCycles = 0;
if (argc == 2 && strcmp(argv[1], "--os") == 0) {
bootImage("build/kernel.b16");
} else if (argc >= 3 && strcmp(argv[1], "--boot") == 0) {
if (argc == 5 && strcmp(argv[3], "--max-cycles") == 0) maxCycles = strtoul(argv[4], NULL, 10);
else if (argc != 3) { fprintf(stderr, "Usage: %s --boot kernel.b16 [--max-cycles N]\n", argv[0]); return 1; }
bootImage(argv[2]);
} else {
if (argc != 1) { fprintf(stderr, "Usage: %s [--os | --boot kernel.b16 [--max-cycles N]]\n", argv[0]); return 1; }
int assembleReturn = -1;
while (assembleReturn < 0) {
assembleReturn = assemble();
if (assembleReturn == -8) return 1;
}
}
unsigned long cycles = 0;
while (true) {
if (maxCycles && cycles++ >= maxCycles) { fprintf(stderr, "CPU cycle limit reached\n"); return 2; }
cpuCycle();
}