/* BOB-16 written by misterbob you are free to use and modify this but PLEASE credit me (at least retain the name in some way) ** read all of the docs before using as they contain some important tidbits ** **** INSTRUCTION SEMANTICS **** (instructions are in order of opcode, nop is 0, add is 1, etc.) (instructions with a * change condition codes) NOP: does nothing other than increment pc *ADD (normal normal): r[dst] = r[src1] + r[src2] ex. add r0 r1 r2 *ADD (normal immediate): r[dst] = r[src1] + imm[4] ex. add r0 r1 4 *ADD (in place normal): r[dst] += r[src1] ex. add r0 r1 *ADD (in place immediate): r[dst] += imm[7] ex. add r0 4 *AND (normal normal): r[dst] = r[src1] & r[src2] ex. and r0 r1 r2 *AND (normal immediate): r[dst] = r[src1] & imm[4] ex. and r0 r1 4 *AND (in place normal): r[dst] &= r[src1] ex. and r0 r1 *AND (in place immediate): r[dst] &= imm[7] ex. and r0 4 *NOT (normal): r[dst] = !r[src] ex. not r0 r1 *NOT (immediate): !r[dst] ex. not r1 *LD: r[dst] <= memory[pc + imm[9]] ex. ld r0 15 *LDI: r[dst] <= memory[memory[pc + imm[9]] ex. ldi r0 15 *LDR: r[dst] <= memory[r[src] + imm[6]] ex. ldr r0 r1 15 ST: r[src] => memory[pc + imm[9]] ex. st r0 15 STI: r[src] => memory[memory[pc + imm[9]] ex. sti r0 15 STR: r[src] => memory[r[dst] + imm[6]] ex. str r0 r1 15 BR: if condition codes match any of the nzp flags, pc += imm[9] ex. br np 50 (jumps to incremented pc + 50 if condition codes n or p are 1) JMP: pc = r[src] ex. jmp r0 JSR: r[7] = pc, pc += imm[11] ex. jsr 50 JSRR: r[7] = pc, pc = r[src] ex. jsrr r0 *LEA: r[src] = pc ex. lea r0 RET: pc = r[7] ex. ret TRAP (vector 0): halts program TRAP (vector 1): prints ascii character in r[0] TRAP (vector 2): r[7] = pc, pc = r[0], prints null terminated ascii string TRAP (vector 3): r[7] = pc, pc = r[0], gets user inputted string with max length stored in r[1] and stores it sequentially at address in r[0] ex. trap 0 **** REGISTERS **** r0...r7: general purpose registers (r0 is usually used as an argument for trap calls, r7 is used for return addresses) accumulator: register inside alu that stores results of arithmetic and logical operations ir (instruction register): stores the current instruction the cpu is executing pc (program counter): stores the memory address of the NEXT instruction to execute (gets incremented during fetch phase of execution, so keep in mind anything using pc-relative addressing will be using the incremented pc) cc: 3 bit flags which consist of n (negative), z (zero), and p (positive), are changed whenever an instruction modifies a general purpose register **** NOTES **** any immediate values in assembly will clamp to be within bounds, e.g. for ADD normal immediate, if you put 60 as the immediate value it will clamp to 15 any unused bits in a machine code instruction will be set to 0 strings are NOT packed!!! pc is *always* incremented at the start of execution, so instructions that change or use pc such as jmp will use the incremented pc as the base sext stands for sign extend and not anything else you MUST include trap 0 to halt the program somewhere or else it'll crash condition codes are set after instructions with *, so if the result of an instruction with an * is: negative -> n bit is set zero -> z bit is set positive -> p bit is set */ #include #include #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif /* Kernel disk exposed to the guest boot ROM through trap 4. */ static uint32_t bootDisk[0x10000]; static bool cpu32; static bool application32; static uint32_t applicationArgc; static uint32_t applicationArgv; static bool applicationEntryArguments; static bool filesystemRegistered; static uint32_t filesystemNames; static uint32_t filesystemLengths; static uint32_t filesystemKinds; static uint32_t filesystemUsed; enum { GRAPHICS_WIDTH=80, GRAPHICS_HEIGHT=25, GRAPHICS_CELLS=GRAPHICS_WIDTH*GRAPHICS_HEIGHT }; enum { SYSTEM_API_VERSION=1, SYSTEM_DEVICE_CELL_FRAMEBUFFER=1, SYSTEM_INPUT_KEYBOARD=1, SYSTEM_INPUT_MOUSE=2 }; enum { WIDE_FILE_BASE=0x1e000, WIDE_FILE_WORDS=8192 }; static unsigned char graphicsCharacters[GRAPHICS_CELLS]; static unsigned char graphicsColors[GRAPHICS_CELLS]; static bool graphicsActive; static int terminalAvailable(void); static void graphicsReset(void) { for(unsigned i=0;i=GRAPHICS_WIDTH || y>=GRAPHICS_HEIGHT)return; unsigned index=(unsigned)y*GRAPHICS_WIDTH+(unsigned)x; graphicsCharacters[index]=character?character:' ';graphicsColors[index]=color&15; } static void graphicsPresent(void) { int ansi=terminalAvailable(),lastColor=-1; if(ansi)printf("\033[2J\033[H"); for(int y=0;y>16); if(!delta)return 0; *type=OS_EVENT_MOUSE_WHEEL;*value=delta; } else if(buttons!=*buttonState) { *type=(buttons&~*buttonState)?OS_EVENT_MOUSE_BUTTON_DOWN:OS_EVENT_MOUSE_BUTTON_UP; *buttonState=buttons; } else return 0; return 1; } return 0; } static int terminalEvent(int *type,int *value,int *x,int *y) { fflush(stdout); #ifdef _WIN32 if(terminalAvailable()) { HANDLE input=GetStdHandle(STD_INPUT_HANDLE);DWORD mode,count;INPUT_RECORD record; if(!GetConsoleMode(input,&mode))return 0; DWORD originalMode=mode; mode=(mode&~ENABLE_PROCESSED_INPUT)|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS; if(!SetConsoleMode(input,mode))return 0; if(!rawInputStarted){originalInputMode=originalMode;rawInputStarted=true;atexit(restoreInputMode);} while(ReadConsoleInputA(input,&record,1,&count) && count) { if(record.EventType==KEY_EVENT) { KEY_EVENT_RECORD key=record.Event.KeyEvent; if(terminalEventDecode(1,key.bKeyDown,(unsigned char)key.uChar.AsciiChar, key.wVirtualKeyCode,0,0,0,&terminalMouseButtons,type,value,x,y))return 1; } if(record.EventType==MOUSE_EVENT) { MOUSE_EVENT_RECORD mouse=record.Event.MouseEvent; unsigned buttons=mouse.dwButtonState; int moving=mouse.dwEventFlags==MOUSE_MOVED; if(terminalEventDecode(2,moving,(int)mouse.dwEventFlags,0, mouse.dwMousePosition.X,mouse.dwMousePosition.Y,buttons, &terminalMouseButtons,type,value,x,y))return 1; } } return 0; } #endif int key=getchar();if(key==EOF)return 0;if(key=='\r')key='\n'; return terminalEventDecode(1,1,key,0,0,0,0,&terminalMouseButtons,type,value,x,y); } static int terminalEventPoll(int *type,int *value,int *x,int *y) { #ifdef _WIN32 if(terminalAvailable()) { HANDLE input=GetStdHandle(STD_INPUT_HANDLE);DWORD mode,count;INPUT_RECORD record; if(!GetConsoleMode(input,&mode))return 0; DWORD originalMode=mode; mode=(mode&~ENABLE_PROCESSED_INPUT)|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS; if(!SetConsoleMode(input,mode))return 0; if(!rawInputStarted){originalInputMode=originalMode;rawInputStarted=true;atexit(restoreInputMode);} for(;;) { if(!PeekConsoleInputA(input,&record,1,&count)||!count)return 0; if(!ReadConsoleInputA(input,&record,1,&count)||!count)return 0; if(record.EventType==KEY_EVENT) { KEY_EVENT_RECORD key=record.Event.KeyEvent; if(terminalEventDecode(1,key.bKeyDown,(unsigned char)key.uChar.AsciiChar, key.wVirtualKeyCode,0,0,0,&terminalMouseButtons,type,value,x,y))return 1; } else if(record.EventType==MOUSE_EVENT) { MOUSE_EVENT_RECORD mouse=record.Event.MouseEvent; unsigned buttons=mouse.dwButtonState; int moving=mouse.dwEventFlags==MOUSE_MOVED; if(terminalEventDecode(2,moving,(int)mouse.dwEventFlags,0, mouse.dwMousePosition.X,mouse.dwMousePosition.Y,buttons, &terminalMouseButtons,type,value,x,y))return 1; } } } #else (void)type;(void)value;(void)x;(void)y; #endif return 0; } typedef uint32_t ureg_t; typedef uint16_t instruction_t; typedef int32_t reg_t; static reg_t wordValue(uint32_t value) { return cpu32?(int32_t)value:(int16_t)(uint16_t)value; } static reg_t wordAdd(reg_t a,reg_t b) { return wordValue((uint32_t)a+(uint32_t)b); } typedef enum INSTRUCTION_E { NOP, ADD, AND, NOT, LD, LDI, LDR, ST, STI, STR, BR, JMP, JSR, LEA, RET, TRAP } INSTRUCTION; typedef enum TRAP_VECTOR_E { HALT, PUTC, PUTS, GETS } TRAP_VECTOR; typedef enum CONDITION_CODES_E { N, Z, P } CONDITION_CODES; typedef struct alu_t { reg_t accumulator; } alu_t; struct cpu_t { reg_t regFile[8]; instruction_t ir; ureg_t pc; bool cc[3]; alu_t alu; } cpu; struct ram_t { int32_t memory[0x10000]; ureg_t mar; reg_t mdr; } ram; static int32_t memoryRead(ureg_t address); static void memoryWrite(ureg_t address,int32_t value); #include "storage.c" enum { MEMORY_PAGE_SHIFT=12, MEMORY_PAGE_WORDS=1<used) { if(!create)return NULL; page->words=allocateSparsePage(); if(!page->words)return NULL; page->number=number;page->used=1;return page; } if(page->number==number)return page; } machineFault();return NULL; } static void memoryPageInstall(uint32_t number,int32_t *words) { uint32_t slot=(number*2654435761u)%MEMORY_PAGE_SLOTS; for(unsigned probe=0;probeused)continue; page->number=number;page->words=words;page->used=1;return; } machineFault(); } static int32_t memoryRead(ureg_t address) { if(address<0x10000)return ram.memory[address]; SparseMemoryPage *page=memoryPage(address>>MEMORY_PAGE_SHIFT,0); return page?page->words[address&(MEMORY_PAGE_WORDS-1)]:0; } static void memoryWrite(ureg_t address,int32_t value) { if(address<0x10000){ram.memory[address]=value;return;} SparseMemoryPage *page=memoryPage(address>>MEMORY_PAGE_SHIFT,1); if(!page)machineFault(); page->words[address&(MEMORY_PAGE_WORDS-1)]=value; } static void memoryReset(void) { for(unsigned i=0;i=0x20&&sparseMemory[i].number<0x100)used++; *count=used; SavedSparsePage *saved=used?calloc(used,sizeof(*saved)):NULL; if(used&&!saved)return NULL; for(unsigned i=0;i=0x20&&sparseMemory[i].number<0x100) { saved[out].number=sparseMemory[i].number; saved[out].words=malloc(MEMORY_PAGE_WORDS*sizeof(int32_t)); if(!saved[out].words) { for(unsigned j=0;j=0x20&&sparseMemory[i].number<0x100) { free(sparseMemory[i].words);sparseMemory[i].used=0;sparseMemory[i].words=NULL; } for(unsigned i=0;i0xf000-4))return -1; if(programMode && (request<0x20000 || request>0xffff9))return -1; uint32_t op=(uint32_t)memoryRead(request); if(!programMode) { if(op!=0 || !cpu32)return -1; filesystemNames=(uint32_t)memoryRead(request+1); filesystemLengths=(uint32_t)memoryRead(request+2); filesystemKinds=(uint32_t)memoryRead(request+3); filesystemUsed=(uint32_t)memoryRead(request+4); uint64_t imageEnd=(uint64_t)bootOrigin+bootWords; if(imageEnd>WIDE_FILE_BASE)return -1; if(filesystemNamesimageEnd || filesystemLengthsimageEnd || filesystemKindsimageEnd || filesystemUsedimageEnd)return -1; filesystemRegistered=true;return 0; } if(!application32 || !filesystemRegistered)return -1; if((op>>16)!=1)return -4; op&=0xffffu; uint32_t a=(uint32_t)memoryRead(request+1),b=(uint32_t)memoryRead(request+2),c=(uint32_t)memoryRead(request+3),d=(uint32_t)memoryRead(request+4); uint32_t e=(uint32_t)memoryRead(request+5),f=(uint32_t)memoryRead(request+6); #define APP_RANGE(p,n) ((p)>=0x20000u && (uint64_t)(p)+(uint64_t)(n)<=0x100000u) char name[24];unsigned nameLength=0; if((op>=2 && op<=4) || op==8 || op==9 || op==10) { if(!APP_RANGE(a,1))return -1; while(nameLength126)return -1; name[nameLength++]=(char)ch; } if(!nameLength || nameLength==sizeof(name)-1 || nameLength>=24 || (uint32_t)memoryRead(a+nameLength)!=0)return -1; name[nameLength]=0; } int slot=-1; if((op>=2 && op<=4) || op==8 || op==9 || op==10)for(int i=0;i<8;i++) { if(!memoryRead(filesystemUsed+i))continue; unsigned j=0;while(j<23 && (uint32_t)memoryRead(filesystemNames+i*24+j)==(unsigned char)name[j] && name[j])j++; if(j==nameLength && !name[j]){slot=i;break;} } if(op==1) { uint32_t index=a,buffer=b,capacity=c,lengthAddress=d; if(index>=8)return 0; if(!APP_RANGE(buffer,capacity) || !capacity || !APP_RANGE(lengthAddress,1))return -1; if(!memoryRead(filesystemUsed+index))return 0; unsigned n=0;while(n<23 && memoryRead(filesystemNames+index*24+n))n++; if(capacity<=n)return -2; for(unsigned i=0;i2)) || (!kind && length>=512) || !APP_RANGE(buffer,length))return -1; if(slot<0)for(int i=0;i<8;i++)if(!memoryRead(filesystemUsed+i)){slot=i;break;} if(slot<0)return -1; uint32_t total=length+1; for(int i=0;i<8;i++)if(i!=slot && memoryRead(filesystemUsed+i))total+=(uint32_t)memoryRead(filesystemLengths+i)+1; if(total>WIDE_FILE_WORDS)return -1; if(!kind)for(uint32_t i=0;i255)return -1;} int32_t packed[WIDE_FILE_WORDS];uint32_t out=0;int newLengths[8],newKinds[8],newUsed[8];char newNames[8][24]; for(int i=0;i<8;i++) { newUsed[i]=i==slot?1:(int)memoryRead(filesystemUsed+i); if(!newUsed[i]){newLengths[i]=0;newKinds[i]=0;memset(newNames[i],0,24);continue;} uint32_t oldName=filesystemNames+i*24;for(int j=0;j<24;j++)newNames[i][j]=(char)memoryRead(oldName+j); if(i==slot){for(int j=0;j<24;j++)newNames[i][j]=j<(int)nameLength?name[j]:0;newLengths[i]=(int)length;newKinds[i]=(int)kind; for(uint32_t j=0;j126)return -1; name[nameLength++]=(char)ch; } if(!nameLength||nameLength>=sizeof(name)-1||!APP_RANGE(nameAddress+nameLength,1)||memoryRead(nameAddress+nameLength))return -1; name[nameLength]=0; if(argumentAddress) { while(argumentLength255)return -1; arguments[argumentLength++]=(char)ch; if(!ch)break; } if(!argumentLength||arguments[argumentLength-1])return -1; } else arguments[argumentLength++]=0; for(int i=0;i<8;i++)if(memoryRead(filesystemUsed+i)) { unsigned j=0;while(j<23&&(uint32_t)memoryRead(filesystemNames+i*24+j)==(unsigned char)name[j]&&name[j])j++; if(j==nameLength&&!name[j]){target=i;break;} } if(target<0)return -1; kind=(int)memoryRead(filesystemKinds+target); if(kind!=3)return -2; for(int i=0;i>32)); return 2; } if(op==16) { graphicsReset();graphicsActive=true;return 0; } if(op==17) { if(!graphicsActive || !APP_RANGE(a,b) || b<2)return -1; memoryWrite(a,GRAPHICS_WIDTH);memoryWrite(a+1,GRAPHICS_HEIGHT);return 2; } if(op==18) { if(!graphicsActive || (int32_t)a<0 || (int32_t)b<0 || a>=GRAPHICS_WIDTH || b>=GRAPHICS_HEIGHT || c>255 || d>15)return -1; graphicsPixel((int)a,(int)b,(unsigned char)c,(unsigned char)d);return 0; } if(op==19) { int x=(int32_t)a,y=(int32_t)b,w=(int32_t)c,h=(int32_t)d; if(!graphicsActive || w<=0 || h<=0 || w>GRAPHICS_WIDTH*2 || h>GRAPHICS_HEIGHT*2 || e>255 || f>15)return -1; int64_t endX=(int64_t)x+w,endY=(int64_t)y+h; int startX=x<0?0:x,startY=y<0?0:y; int clippedX=endX>GRAPHICS_WIDTH?GRAPHICS_WIDTH:(int)endX; int clippedY=endY>GRAPHICS_HEIGHT?GRAPHICS_HEIGHT:(int)endY; for(int py=startY;py=GRAPHICS_WIDTH || x1<0 || x1>=GRAPHICS_WIDTH || y0<0 || y0>=GRAPHICS_HEIGHT || y1<0 || y1>=GRAPHICS_HEIGHT || e>255 || f>15)return -1; int dx=abs(x1-x0),sx=x0=dy){error+=dy;x0+=sx;}if(twice<=dx){error+=dx;y0+=sy;}} return 0; } if(op==21) { int x=(int32_t)a,y=(int32_t)b,startX=x;uint32_t text=c,color=d; if(!graphicsActive || (int32_t)x<0 || x>=GRAPHICS_WIDTH || y<0 || y>=GRAPHICS_HEIGHT || !APP_RANGE(text,1) || color>15)return -1; for(unsigned i=0;i=GRAPHICS_HEIGHT)break;continue;} graphicsPixel(x++,y,(unsigned char)(ch<=255?ch:'?'),(unsigned char)color); if(x>=GRAPHICS_WIDTH)break; } return 0; } if(op==22) { int x=(int32_t)a,y=(int32_t)b,w=(int32_t)c,h=(int32_t)d;uint32_t bitmap=e,color=f; if(!graphicsActive || x<0 || y<0 || x>=GRAPHICS_WIDTH || y>=GRAPHICS_HEIGHT || w<=0 || h<=0 || w>GRAPHICS_WIDTH-x || h>GRAPHICS_HEIGHT-y || (uint64_t)w*h>GRAPHICS_CELLS || !APP_RANGE(bitmap,(uint64_t)w*h) || color>15)return -1; for(int py=0;py255)return -1; if(ch)graphicsPixel(x+px,y+py,(unsigned char)ch,(unsigned char)color); } return 0; } if(op==27) { int x=(int32_t)a,y=(int32_t)b,w=(int32_t)c,h=(int32_t)d;uint32_t bitmap=e; if(!graphicsActive || x<0 || y<0 || x>=GRAPHICS_WIDTH || y>=GRAPHICS_HEIGHT || w<=0 || h<=0 || w>GRAPHICS_WIDTH-x || h>GRAPHICS_HEIGHT-y || (uint64_t)w*h>GRAPHICS_CELLS || !APP_RANGE(bitmap,(uint64_t)w*h))return -1; for(int i=0;i0xfffu)return -1; for(int py=0;py>8)&15u; if(ch)graphicsPixel(x+px,y+py,(unsigned char)ch,(unsigned char)color); } return 0; } if(op==23) { if(!graphicsActive || a>255 || b>15)return -1; for(unsigned i=0;i=8 || (application32 ? (entry < 0x20000 || entry >= 0xf0000) : (entry < legacyBase || entry >= legacyEnd))) return -1; struct cpu_t saved = cpu; ureg_t savedMar = ram.mar; reg_t savedMdr = ram.mdr; volatile int status = -2; legacyProgram32=savedCpu32&&!application32; programMode = true; programDone = false; bool savedApplicationEntryArguments=applicationEntryArguments; unsigned frame=programDepth++; if (setjmp(programFaultStack[frame]) == 0) { if(!application32)cpu32=false; cpu.pc = addressValue(entry); cpu.regFile[6] = wordValue(application32?0x100000u:(savedCpu32?0xd000u:0xc000u)); cpu.regFile[5] = 0; cpu.regFile[7] = 0x80; if(application32 && applicationEntryArguments) { cpu.regFile[0]=wordValue(applicationArgc);cpu.regFile[1]=wordValue(applicationArgv); } applicationEntryArguments=false; unsigned cycles = 0; while (!programDone && cpu.pc != 0x80 && cycles < 5000000) { cpuCycle(); ++cycles; } status = cycles == 5000000 ? -3 : (int32_t)(uint32_t)cpu.regFile[0]; } programDepth--; programMode = savedProgramMode; if(application32 && graphicsActive)graphicsClose(); if(savedCpu32 && !application32 && bootWideHeader) { uint64_t imageEnd=(uint64_t)bootOrigin+bootWords; uint32_t restoreStart=bootOrigin>legacyBase?bootOrigin:legacyBase; uint32_t restoreEnd=imageEnd16)machineFault(); unsigned bits=(uint16_t)val & ((1u< 0; } void fetch() { ram.mar = cpu.pc; currentInstructionAddress=(uint32_t)cpu.pc; cpu.pc = addressValue(cpu.pc+1); ram.mdr = memoryRead(ram.mar); if(cpu32 && ((uint32_t)ram.mdr & 0xffff0000u)) { fprintf(stderr,"Reserved upper bits in bob32 instruction at %04X\n",ram.mar);machineFault(); } cpu.ir = (uint16_t)ram.mdr; currentInstruction=cpu.ir; } void evalAddress() { int opcode = (cpu.ir >> 12) & 0xF; if (opcode == LD || opcode == ST || opcode == LEA || opcode == BR) { ram.mar = addressValue(cpu.pc + sext(cpu.ir & 0x1FF, 9)); } else if (opcode == LDI || opcode == STI) { ram.mar = addressValue(cpu.pc + sext(cpu.ir & 0x1FF, 9)); ram.mdr = memoryRead(ram.mar); ram.mar = addressValue((uint32_t)ram.mdr); } else if (opcode == LDR || opcode == STR) { ram.mar = addressValue((uint32_t)cpu.regFile[(cpu.ir >> 6) & 0x7] + (uint32_t)sext(cpu.ir & 0x3F, 6)); } } void fetchOperands() { int opcode = (cpu.ir >> 12) & 0xF; if (opcode == LD || opcode == LDI || opcode == LDR) { ram.mdr = memoryRead(ram.mar); } } void execute() { switch ((cpu.ir >> 12) & 0xF) { case NOP: if(cpu.ir & 0x0fff)machineFault(); break; case ADD: switch ((cpu.ir >> 7) & 0x3) { case 0: if (cpu.ir & 0b1) { printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1)); machineFault(); } cpu.alu.accumulator = wordAdd(cpu.regFile[(cpu.ir >> 4) & 0x7],cpu.regFile[(cpu.ir >> 1) & 0x7]); break; case 1: cpu.alu.accumulator = wordAdd(cpu.regFile[(cpu.ir >> 4) & 0x7],sext(cpu.ir & 0xF, 4)); break; case 2: 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 = wordAdd(cpu.regFile[(cpu.ir >> 9) & 0x7],cpu.regFile[(cpu.ir >> 4) & 0x7]); break; case 3: cpu.alu.accumulator = wordAdd(cpu.regFile[(cpu.ir >> 9) & 0x7],sext(cpu.ir & 0x7F, 7)); break; } break; case AND: switch ((cpu.ir >> 7) & 0x3) { case 0: if (cpu.ir & 0x1) { 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]; break; case 1: cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 4) & 0x7] & sext(cpu.ir & 0xF, 4); break; case 2: 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]; break; case 3: cpu.alu.accumulator = cpu.regFile[(cpu.ir >> 9) & 0x7] & sext(cpu.ir & 0x7F, 7); break; } break; case NOT: if (cpu.ir & ((cpu.ir & 0x100)?0xff:0x1f)) { printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1)); machineFault(); } if (cpu.ir & 0x100) { cpu.alu.accumulator = ~cpu.regFile[(cpu.ir >> 9) & 0x7]; } else { cpu.alu.accumulator = ~cpu.regFile[(cpu.ir >> 5) & 0x7]; } break; case LD: break; case LDI: break; case LDR: break; case ST: break; case STI: break; case STR: break; case BR: if ( (cpu.cc[N] && ((cpu.ir >> 11) & 1)) || (cpu.cc[Z] && ((cpu.ir >> 10) & 1)) || (cpu.cc[P] && ((cpu.ir >> 9) & 1)) ) { cpu.pc = addressValue(cpu.pc + sext(cpu.ir & 0x1FF, 9)); } break; case JMP: if (cpu.ir & 0x1FF) { printf("bad instruction: %X\n at memory address %X\n", cpu.ir, (uint16_t)(cpu.pc - 1)); machineFault(); } cpu.pc = addressValue((uint32_t)cpu.regFile[(cpu.ir >> 9) & 0x7]); break; case JSR: { if(!(cpu.ir & 0x800) && (cpu.ir & 0xff))machineFault(); ureg_t target = addressValue((uint32_t)cpu.regFile[(cpu.ir >> 8) & 0x7]); cpu.regFile[7] = wordValue(cpu.pc); if (cpu.ir & 0x800) { cpu.pc = addressValue(cpu.pc + sext(cpu.ir & 0x7FF, 11)); } else { cpu.pc = target; } break; } case LEA: break; case RET: if(cpu.ir & 0x0fff)machineFault(); cpu.pc = addressValue((uint32_t)cpu.regFile[7]); break; case TRAP: if (cpu.ir & 0xFF) { 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: { uint64_t remaining = (uint64_t)UINT32_MAX+1; cpu.regFile[7] = wordValue(cpu.pc); cpu.pc = addressValue((uint32_t)cpu.regFile[0]); while (true) { if (!remaining--) machineFault(); reg_t character=wordValue((uint32_t)memoryRead(cpu.pc)); if (character == 0) break; cpu.regFile[0] = character; putchar(cpu.regFile[0] & 0xFF); if(cpu32 && cpu.pc==UINT32_MAX)machineFault(); cpu.pc=addressValue(cpu.pc+1); } putchar('\n'); cpu.pc = addressValue((uint32_t)cpu.regFile[7]); break; } case GETS: { if(cpu32 && ((uint32_t)cpu.regFile[1] & 0xffff0000u))machineFault(); int n = (uint16_t)cpu.regFile[1]; cpu.regFile[7] = wordValue(cpu.pc); uint32_t address = addressValue((uint32_t)cpu.regFile[0]); uint64_t end = (uint64_t)address + (unsigned)n; uint32_t legacyBase=legacyProgram32?0xc200u:0xa000u; uint32_t legacyEnd=0xe000u; int guestRange=application32?(address>=0x20000 && end<=0x100000):((address>=legacyBase && end<=legacyEnd) || address>=0x10000); if (n < 2 || end > (uint64_t)UINT32_MAX+1 || (programMode && !guestRange)) { 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'; size_t length=strlen(buf); for (size_t i = 0; i <= length; i++) { memoryWrite(address + (uint32_t)i,buf[i] & 0xFF); } free(buf); cpu.pc = addressValue((uint32_t)cpu.regFile[7]); break; } case 4: /* disk_read(dst=r0, word_offset=r1, count=r2) */ { if(cpu32 && (((uint32_t)cpu.regFile[1] | (uint32_t)cpu.regFile[2]) & 0xffff0000u)) { fprintf(stderr,"Invalid boot disk argument\n");machineFault(); } uint32_t dst = addressValue((uint32_t)cpu.regFile[0]); unsigned offset = (uint16_t)cpu.regFile[1]; unsigned count = (uint16_t)cpu.regFile[2]; uint64_t end=(uint64_t)dst+count; if (programMode || !bootMode || offset + count > bootWords || dst < 0x100 || end > (uint64_t)UINT32_MAX+1 || (dst < 0xf000 && end > 0xf000)) { fprintf(stderr, "Invalid boot disk read\n"); machineFault(); } for (unsigned i = 0; i < count; ++i) memoryWrite(dst + i,wordValue(bootDisk[offset + i])); cpu.regFile[0] = wordValue(count); break; } case 5: cpu.regFile[0] = (int16_t)getchar(); break; case 6: if(programMode){cpu.regFile[0]=-1;break;} application32=cpu32 && ((uint32_t)cpu.regFile[0]>=0x20000u && (uint32_t)cpu.regFile[0]<0xf0000u); if(cpu32 && ((uint32_t)cpu.regFile[0]&0xffff0000u) && ((uint32_t)cpu.regFile[0]<0x20000u || (uint32_t)cpu.regFile[0]>=0xf0000u))cpu.regFile[0]=-2; else cpu.regFile[0] = wordValue((uint32_t)runProgram(cpu32?(uint32_t)cpu.regFile[0]:(uint16_t)cpu.regFile[0])); application32=false; break; case 7: if(programMode)machineFault(); cpu.regFile[0]=wordValue((uint32_t)snapshotService(cpu32?(uint32_t)cpu.regFile[0]:(uint16_t)cpu.regFile[0],cpu.regFile[1])); break; case 8: cpu.regFile[0]=(int16_t)terminalKey(); break; case 9: cpu.regFile[0]=(int16_t)terminalAvailable(); break; case 10: case 11: cpu.regFile[0]=((cpu.ir>>8)&15)==10?80:25; #ifdef _WIN32 { CONSOLE_SCREEN_BUFFER_INFO info; if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info))cpu.regFile[0]=((cpu.ir>>8)&15)==10?info.dwSize.X:info.srWindow.Bottom-info.srWindow.Top+1; } #endif break; case 12: if(programMode || !cpu32)cpu.regFile[0]=-4; else cpu.regFile[0]=wordValue((uint32_t)runWideImageFromGuest((uint32_t)cpu.regFile[0])); break; case 13: if(!cpu32)cpu.regFile[0]=-4; else cpu.regFile[0]=wordValue((uint32_t)runWidePackedFromGuest((uint32_t)cpu.regFile[0],NULL)); break; case 14: if(programMode || !cpu32)cpu.regFile[0]=-4; else cpu.regFile[0]=wordValue((uint32_t)importWideImageFromGuest((uint32_t)cpu.regFile[0])); break; case 15: if(!cpu32)cpu.regFile[0]=-4; else cpu.regFile[0]=wordValue((uint32_t)filesystemService((uint32_t)cpu.regFile[0])); break; default: machineFault(); } break; } } void storeResult() { int opcode = (cpu.ir >> 12) & 0xF; int dest = (cpu.ir >> 9) & 0x7; if (opcode == LD || opcode == LDI || opcode == LDR) { cpu.regFile[dest] = wordValue((uint32_t)ram.mdr); updateCC(cpu.regFile[dest]); } else if (opcode == ST || opcode == STI || opcode == STR) { uint32_t storeBase=application32?0x20000u:(legacyProgram32?0xc200u:0xa000u); uint32_t storeEnd=application32?0x100000u:0xe000u; bool allowedStore=ram.mar>=storeBase && ram.mar=0x9800u && ram.mar<0x9810u) allowedStore=true; if (programMode && !allowedStore) { if (getenv("BOB16_TRACE_FAULT")) fprintf(stderr,"Rejected store to 0x%08X (valid 0x%08X..0x%08X%s)\n",ram.mar,storeBase,storeEnd-1,(!application32&&!legacyProgram32?" or 0x00009800..0x0000980F":"")); machineFault(); } memoryWrite(ram.mar,wordValue((uint32_t)cpu.regFile[dest])); } else if (opcode == ADD || opcode == AND || opcode == NOT) { cpu.regFile[dest] = wordValue((uint32_t)cpu.alu.accumulator); updateCC(cpu.regFile[dest]); } else if (opcode == LEA) { cpu.regFile[dest] = wordValue(ram.mar); updateCC(cpu.regFile[dest]); } } void cpuCycle() { fetch(); evalAddress(); fetchOperands(); execute(); storeResult(); } int parseReg(const char* s) { if (!s) { //string doesn't exist return -1; } if (strlen(s) != 2 || (s[0] != 'R' && s[0] != 'r')) { //incorrect format return -2; } if (s[1] < '0' || s[1] > '7') { //out of bounds return -3; } return s[1] - '0'; } static int parseImmediate(const char *text,int *value) { const char *p=text;if(*p=='+' || *p=='-')p++; if(!*p)return 0; for(;*p;p++)if(*p<'0' || *p>'9')return 0; errno=0;char *end;long parsed=strtol(text,&end,10); if(errno==ERANGE || *end || parsedINT_MAX)return 0; *value=(int)parsed;return 1; } static int readAssemblyLine(FILE *file,char line[257]) { size_t length=0;int ch,readAny=0,tooLong=0; while((ch=fgetc(file))!=EOF) { readAny=1; if(ch=='\n')break; if(ch==0) { while((ch=fgetc(file))!=EOF && ch!='\n'){} line[0]=0;return -2; } if(length<256)line[length++]=(char)ch; else tooLong=1; } if(ferror(file)){line[0]=0;return -3;} if(!readAny && ch==EOF)return 0; if(length && line[length-1]=='\r')length--; line[length]=0; if(tooLong || length>255)return -1; return 1; } static int assembleSource(FILE *file); int assemble() { memset(ram.memory, 0, sizeof(ram.memory)); FILE* file; char fileName[256]; setbuf(stdout, NULL); printf("Enter source assembly file name: \n"); if (!fgets(fileName, 256, stdin)) return -8; if(!strchr(fileName,'\n')) { int next=fgetc(stdin); if(next=='\r')next=fgetc(stdin); if(next!=EOF && next!='\n') { while(next!=EOF && next!='\n')next=fgetc(stdin); printf("Source file name exceeds 255 characters!\n");return -1; } } fileName[strcspn(fileName, "\r\n")] = '\0'; file = fopen(fileName, "r"); if (!file) { printf("Source file doesn't exist!\n"); return -1; } int result=assembleSource(file); if(fclose(file) && !result){fprintf(stderr,"Cannot close assembly source\n");result=-1;} if(!result)printf("Starting execution!\n"); return result; } static int assembleSource(FILE *file) { uint32_t instructionCount = 0; assembledOrigin=0; int lineCount = 0; char line[257]; int lineStatus; while ((lineStatus=readAssemblyLine(file,line))>0) { uint32_t instruction = 0; size_t n = strspn(line, " \t\r"); char* lineTrimmed = line + n; if (lineTrimmed[0] == '\0' || lineTrimmed[0] == ';') { lineCount++; continue; } char* tokens[4] = { NULL, NULL, NULL, NULL }; int tokenCount = 0; char* token = strtok(lineTrimmed, " \t\r\n"); while (token != NULL) { if (tokenCount == 4) { printf("Too many tokens on line %d!\n", lineCount); return -2; } tokens[tokenCount] = token; tokenCount++; token = strtok(NULL, " \t\r\n"); } if (tokenCount == 0) { lineCount++; continue; } if(strcmp(tokens[0],".org")==0) { char *end;errno=0;unsigned long long value=tokenCount==2?strtoull(tokens[1],&end,0):0; if(!cpu32 || tokenCount!=2 || tokens[1][0]=='-' || end==tokens[1] || *end || errno==ERANGE || value>UINT32_MAX || value(cpu32?UINT32_MAX:0xffffu)) { printf("Wrong token on line %d!\n", lineCount); return -3; } instruction=(uint32_t)value; } else if (strcmp(tokens[0], ".stringz") == 0) { if (tokenCount != 2) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } size_t length=strlen(tokens[1]); uint64_t assemblyLimit=instructionCount>=0x10000?(uint64_t)UINT32_MAX+1:0x10000; if((uint64_t)instructionCount+length+1>assemblyLimit){printf("Too many instructions!\n");return -7;} for (size_t i = 0; i < length; i++) { memoryWrite(instructionCount, tokens[1][i] & 0x00FF); instructionCount++; } memoryWrite(instructionCount, 0); instructionCount++; lineCount++;continue; } else if (strcmp(tokens[0], "add") == 0) { instruction += (1 << 12); if (tokenCount == 4) { //normal int dst = parseReg(tokens[1]); int src0 = parseReg(tokens[2]); int src1 = parseReg(tokens[3]); if (dst < 0 || src0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (dst << 9); instruction += (src0 << 4); if (src1 < 0) { //imm instruction += (1 << 7); int imm; if(!parseImmediate(tokens[3],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 7) { imm = 7; } if (imm < -8) { imm = -8; } instruction += imm & 0xF; } else { //normal instruction += (src1 << 1); } } else if (tokenCount == 3) { //in place int dst = parseReg(tokens[1]); int src = parseReg(tokens[2]); if (dst < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (dst << 9); if (src < 0) { //imm instruction += (3 << 7); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 63) { imm = 63; } if (imm < -64) { imm = -64; } instruction += imm & 0x7F; } else { //normal instruction += (2 << 7); instruction += (src << 4); } } else { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } } else if (strcmp(tokens[0], "and") == 0) { instruction += (2 << 12); if (tokenCount == 4) { //normal int r0 = parseReg(tokens[1]); int r1 = parseReg(tokens[2]); int r2 = parseReg(tokens[3]); if (r0 < 0 || r1 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); instruction += (r1 << 4); if (r2 < 0) { //imm instruction += (1 << 7); int imm; if(!parseImmediate(tokens[3],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 7) { imm = 7; } if (imm < -8) { imm = -8; } instruction += imm & 0xF; } else { //normal instruction += (r2 << 1); } } else if (tokenCount == 3) { //in place int r0 = parseReg(tokens[1]); int r1 = parseReg(tokens[2]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); if (r1 < 0) { //imm instruction += (3 << 7); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 63) { imm = 63; } if (imm < -64) { imm = -64; } instruction += imm & 0x7F; } else { //normal instruction += (2 << 7); instruction += (r1 << 4); } } else { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } } else if (strcmp(tokens[0], "not") == 0) { instruction += (3 << 12); if(tokenCount!=2 && tokenCount!=3) { printf("Wrong amount of tokens on line %d!\n",lineCount);return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); if(tokenCount==2) { instruction += (1 << 8); } else { int r1=parseReg(tokens[2]); if(r1<0) { printf("Wrong tokens on line %d!\n",lineCount);return -3; } instruction += (r1 << 5); } } else if (strcmp(tokens[0], "ld") == 0) { instruction += (4 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "ldi") == 0) { instruction += (5 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "ldr") == 0) { instruction += (6 << 12); if (tokenCount != 4) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); int r1 = parseReg(tokens[2]); if (r0 < 0 || r1 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); instruction += (r1 << 6); int imm; if(!parseImmediate(tokens[3],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 31) { imm = 31; } if (imm < -32) { imm = -32; } instruction += (imm & 0x3F); } else if (strcmp(tokens[0], "st") == 0) { instruction += (7 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "sti") == 0) { instruction += (8 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "str") == 0) { instruction += (9 << 12); if (tokenCount != 4) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); int r1 = parseReg(tokens[2]); if (r0 < 0 || r1 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); instruction += (r1 << 6); int imm; if(!parseImmediate(tokens[3],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 31) { imm = 31; } if (imm < -32) { imm = -32; } instruction += (imm & 0x3F); } else if (strcmp(tokens[0], "br") == 0) { instruction += (10 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } size_t l = strlen(tokens[1]); if (l > 3 || l == 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } int conditionCodes = 0; for (size_t i = 0; i < l; i++) { if (tokens[1][i] == 'n') { conditionCodes |= 0b100; } else if (tokens[1][i] == 'z') { conditionCodes |= 0b010; } else if (tokens[1][i] == 'p') { conditionCodes |= 0b001; } else { printf("Invalid branch condition on line %d!\n",lineCount);return -3; } } instruction += ((conditionCodes & 0b111) << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "jmp") == 0) { instruction += (11 << 12); if (tokenCount != 2) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += ((r0 & 0x7) << 9); } else if (strcmp(tokens[0], "jsr") == 0) { instruction += (12 << 12); instruction += (1 << 11); if (tokenCount != 2) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int imm; if(!parseImmediate(tokens[1],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 1023) { imm = 1023; } if (imm < -1024) { imm = -1024; } instruction += (imm & 0x7FF); } else if (strcmp(tokens[0], "jsrr") == 0) { instruction += (12 << 12); if (tokenCount != 2) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 8); } else if (strcmp(tokens[0], "lea") == 0) { instruction += (13 << 12); if (tokenCount != 3) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int r0 = parseReg(tokens[1]); if (r0 < 0) { printf("Wrong tokens on line %d!\n", lineCount); return -3; } instruction += (r0 << 9); int imm; if(!parseImmediate(tokens[2],&imm)){printf("Invalid decimal operand on line %d!\n",lineCount);return -3;} if (imm > 255) { imm = 255; } if (imm < -256) { imm = -256; } instruction += (imm & 0x1FF); } else if (strcmp(tokens[0], "ret") == 0) { instruction += (14 << 12); if (tokenCount != 1) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } } else if (strcmp(tokens[0], "trap") == 0) { instruction += (15 << 12); if (tokenCount != 2) { printf("Wrong amount of tokens on line %d!\n", lineCount); return -4; } int vector; if(!parseImmediate(tokens[1],&vector)){printf("Invalid trap vector on line %d!\n",lineCount);return -5;} if (vector < 0 || vector > 11) { printf("Bad trap vector on line %d!\n", lineCount); return -5; } instruction += ((vector & 0xF) << 8); } else { printf("Unkown opcode on line %d!\n", lineCount); return -6; } if (instructionCount == UINT32_MAX) { printf("Too many instructions!\n"); return -7; } memoryWrite(instructionCount++, instruction); lineCount++; } if(lineStatus<0) { if(lineStatus==-1)printf("Source line %d exceeds 255 characters!\n",lineCount); else if(lineStatus==-2)printf("Source line %d contains a zero byte!\n",lineCount); else fprintf(stderr,"Cannot read assembly source\n"); return -2; } if(ferror(file)){fprintf(stderr,"Cannot read assembly source\n");return -1;} assembledWords=instructionCount-assembledOrigin; return 0; } static void bootImage(const char *path) { filesystemRegistered=false; graphicsActive=false;graphicsReset(); 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) && memcmp(magic,"B32K",4))) { fprintf(stderr, "Not a bob16/bob32 kernel image\n"); exit(1); } cpu32=!memcmp(magic,"B32K",4); unsigned version=readWord(file),headerSize=0; uint32_t origin,entry,words,checksum; if(cpu32 && version==2) { headerSize=readWord(file);origin=readDword(file);entry=readDword(file); words=readDword(file);checksum=readDword(file); } else { origin=readWord(file);entry=readWord(file);words=readWord(file);checksum=readWord(file); if(cpu32)checksum|=(uint32_t)readWord(file)<<16; } uint64_t end=(uint64_t)origin+words; int validV1=version==1 && words && origin>=0x100 && end<=0xa000 && entry>=origin && entry=origin && entry=0xf000)return -4; while(length255)return -4; path[length++]=(char)character; } if(length==sizeof(path)-1 || pathAddress+length>=0xf000)return -4; path[length]=0; FILE *file=fopen(path,"rb");if(!file)return -5; unsigned char header[24];int valid=fread(header,1,sizeof(header),file)==sizeof(header); if(valid && (memcmp(header,"B32K",4) || header[4]!=2 || header[5]!=0 || header[6]!=24 || header[7]!=0))valid=0; uint32_t origin=0,entry=0,count=0,checksum=0; if(valid) { for(unsigned i=0;i<4;i++) { origin|=(uint32_t)header[8+i]<<(8*i);entry|=(uint32_t)header[12+i]<<(8*i); count|=(uint32_t)header[16+i]<<(8*i);checksum|=(uint32_t)header[20+i]<<(8*i); } uint64_t end=(uint64_t)origin+count; if(!count || count>0x10000 || origin<0x20000 || end>0x100000 || entry=end)valid=0; } uint32_t *words=NULL;uint32_t sum=0; if(valid) {words=malloc((size_t)count*sizeof(*words));if(!words)valid=0;} if(valid)for(uint32_t i=0;i>MEMORY_PAGE_SHIFT,lastPage=(origin+count-1)>>MEMORY_PAGE_SHIFT;unsigned neededPages=0,freePages=0; for(uint32_t page=firstPage;page<=lastPage;page++)if(!memoryPage(page,0))neededPages++; for(unsigned i=0;ifreePages){free(words);return -5;} typedef struct {uint32_t number;int32_t *words;} PendingPage; PendingPage *pending=neededPages?calloc(neededPages,sizeof(*pending)):NULL; if(neededPages && !pending){free(words);return -5;} unsigned pendingCount=0; for(uint32_t page=firstPage;page<=lastPage;page++)if(!memoryPage(page,0)) { pending[pendingCount].number=page; pending[pendingCount].words=allocateSparsePage(); if(!pending[pendingCount].words) { for(unsigned i=0;i0xf000-4)return -4; uint32_t address=(uint32_t)memoryRead(descriptor),length=(uint32_t)memoryRead(descriptor+1); uint32_t argumentAddress=(uint32_t)memoryRead(descriptor+2); uint32_t programNameAddress=(uint32_t)memoryRead(descriptor+3); const char *arguments=NULL; char argumentBuffer[128]; if(argumentAddress) { if(argumentAddress<0x100 || argumentAddress>=0xf000)return -4; unsigned i=0; while(i255)return -4; argumentBuffer[i++]=(char)c; if(!c)break; } if(!i || argumentBuffer[i-1])return -4; arguments=argumentBuffer; } char programNameBuffer[128]; if(programNameAddress) { if(programNameAddress<0x100 || programNameAddress>=0xf000)return -4; unsigned i=0; while(i255)return -4; programNameBuffer[i++]=(char)c; if(!c)break; } if(!i || programNameBuffer[i-1])return -4; } else programNameBuffer[0]=0; int oldStorage=address>=0xf000&&address<0x10000&&length<=4096&&address+length<=0x10000; int wideStorage=address>=WIDE_FILE_BASE&&address0x10000 || origin<0x20000 || (uint64_t)origin+count>0x100000 || entry=origin+count)return -4; uint32_t sum=0;for(uint32_t i=0;i>MEMORY_PAGE_SHIFT,lastPage=(origin+count-1)>>MEMORY_PAGE_SHIFT;unsigned neededPages=0,freePages=0; for(uint32_t page=firstPage;page<=lastPage;page++)if(!memoryPage(page,0))neededPages++; for(unsigned i=0;i>MEMORY_PAGE_SHIFT; int needsArgumentPage=!memoryPage(argumentPage,0) && (argumentPagelastPage); if(neededPages+(unsigned)needsArgumentPage>freePages)return -5; typedef struct {uint32_t number;int32_t *words;} PendingPage; PendingPage *pending=neededPages?calloc(neededPages,sizeof(*pending)):NULL; if(neededPages && !pending)return -5; unsigned pendingCount=0; for(uint32_t page=firstPage;page<=lastPage;page++)if(!memoryPage(page,0)) { pending[pendingCount].number=page;pending[pendingCount].words=allocateSparsePage(); if(!pending[pendingCount].words) {for(unsigned i=0;i0xf000-3)return -4; uint32_t pathAddress=(uint32_t)memoryRead(descriptor),destination=(uint32_t)memoryRead(descriptor+1),capacity=(uint32_t)memoryRead(descriptor+2); if(pathAddress<0x100 || pathAddress>=0xf000)return -4; int oldStorage=destination>=0xf000&&destination<0x10000&&capacity<=0x10000-destination; int wideStorage=destination>=WIDE_FILE_BASE&&destination255)return -4;path[pathLength++]=(char)c;} if(!pathLength || pathLength==sizeof(path)-1)return -4; path[pathLength]=0; FILE *file=fopen(path,"rb");if(!file)return -1; unsigned char header[24];int valid=fread(header,1,sizeof(header),file)==sizeof(header); if(valid && (memcmp(header,"B32K",4)||header[4]!=2||header[5]!=0||header[6]!=24||header[7]!=0))valid=0; uint32_t origin=0,entry=0,count=0,checksum=0; if(valid)for(unsigned i=0;i<4;i++){origin|=(uint32_t)header[8+i]<<(8*i);entry|=(uint32_t)header[12+i]<<(8*i);count|=(uint32_t)header[16+i]<<(8*i);checksum|=(uint32_t)header[20+i]<<(8*i);} if(valid && (!count || count>0x10000 || origin<0x20000 || (uint64_t)origin+count>0x100000 || entry=origin+count))valid=0; if(valid && count+7>capacity){fclose(file);return -5;} uint32_t sum=0;memoryWrite(destination,0x4233324b);memoryWrite(destination+1,0x00180002);memoryWrite(destination+2,origin);memoryWrite(destination+3,entry);memoryWrite(destination+4,count);memoryWrite(destination+5,checksum); for(uint32_t i=0;valid && i'9')return 0; errno=0;char *end;unsigned long value=strtoul(text,&end,10); if(errno==ERANGE || *end || !value)return 0; *limit=value;return 1; } int main(int argc, char** argv) { memoryReset(); #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],"--asm32")==0) { cpu32=true; FILE *assembly=fopen(argv[2],"rb"); if(!assembly){perror(argv[2]);return 1;} int result=assembleSource(assembly); if(fclose(assembly) && !result){fprintf(stderr,"Cannot close assembly source\n");return 1;} if(result){fprintf(stderr,"B32 assembly failed (%d)\n",result);return 1;} assembledWide=true; cpu.pc=assembledOrigin; if(assembledWords<=0x10000) { bootOrigin=assembledOrigin;bootWords=assembledWords;bootWideHeader=true; for(uint32_t i=0;i= 3 && strcmp(argv[1], "--boot") == 0) { if (argc == 5 && strcmp(argv[3], "--max-cycles") == 0) { if(!parseCycleLimit(argv[4],&maxCycles)){fprintf(stderr,"--max-cycles requires a positive decimal integer within unsigned long range\n");return 1;} } 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 image.b16/b32 [--max-cycles N] | --asm32 source.basm]\n", argv[0]); return 1; } int assembleReturn = -1; while (assembleReturn < 0) { assembleReturn = assemble(); if (assembleReturn == -8) return 1; } } uint32_t identityWords[0x8d00]; uint64_t bootEnd=(uint64_t)bootOrigin+bootWords; for(unsigned i=0;i<0x8d00;i++) { unsigned address=0x300+i; identityWords[i]=bootMode?((uint64_t)address>=bootOrigin && (uint64_t)address= maxCycles) { fprintf(stderr, "CPU cycle limit reached\n"); return 2; } cpuCycle(); } return 0; }