Skip to content

Commit

Permalink
Full text display working; CPU functions almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestKatsch committed May 6, 2013
1 parent d2a5bed commit 4b6ef73
Show file tree
Hide file tree
Showing 10 changed files with 1,499 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dum
Dum OS emulator
===

dum
This is a full emulator for an OS based on the stack-based language Dum.
125 changes: 125 additions & 0 deletions assets/scripts/cpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

var STACK_UNDERFLOW=0;

var ERR_LUT=["stack underflow"];

var I={
"__push_int":[0x00],
"__nop":[0x01],
"__0branch":[0x02],
"__copy":[0x03]
};

var MAX_CORE_TIME=10; // in milliseconds

var Core=function(id) {
this.id=id;
this.stack=[];
this.return_pointer=[];
this.memory=[];
this.rom=[0x00,0xff,0x03];
this.next_push=false;
this.program=[];
this.position=-1;
this.start_address=0;
this.kdebug=function(t) {
log.kdebug("core #"+this.id+": "+t);
};
this.kernel=function(t) {
log.kernel("core #"+this.id+": "+t);
};
this.boot=function() {
console.log("Starting core "+this.id)
this.program=this.rom;
this.run(0);
};
this.push=function(n) {
this.kdebug("push 0x"+n.toString(16));
this.stack.push(n);
};
this.error=function(error_code) {
log.error("core #"+this.id+": "+ERR_LUT[error_code]);
this.program=[]; // eventually search for proper error
this.position=-1;
throw "DumError: "+ERR_LUT[error_code];
};
this.pop=function() {
if(this.stack.length <= 0) {
this.error(STACK_UNDERFLOW);
return;
}
var n=this.stack.pop();
this.kdebug("pop 0x"+n.toString(16));
return(n);
};
this.run=function(l) {
this.position=l;
this.return_pointer.push(l);
};
this.get_instruction=function(ins) {
return(0xff & ins);
};
// this.get_arg=function(ins) {
// return (0xfffffff & (ins>>8));
// };
this.run_builtin=function(ins) {
i=this.get_instruction(ins);
if(i == I["__nop"]) {
return;
} else if(i == I["__copy"]) {
var to=this.pop();
var from=this.pop();
console.log(from,to);
} else if(i == I["__push_int"]) {
this.next_push=true;
}
};
this.run_instruction=function(l) {
try {
var i=this.program[l];
if(this.next_push == true) {
this.push(i);
this.next_push=false;
} else {
this.run_builtin(i);
}
} catch(e) {
if(!e.startsWith("DumError"))
throw e;
}
};
this.run_next=function() {
if(this.position == -1)
return;
var start_time=new Date().getTime();
while(true) {
if((this.run_instruction(this.position) == false) || (this.position >= this.program.length-1)) {
this.position=-1;
this.kernel("waiting");
return;
}
this.position+=1;
if((new Date().getTime()-start_time) >= MAX_CORE_TIME)
break;
}
};
};

var cores=[];

var CORE_NUMBER=1;

function cpu_init() {
for(var i=0;i<CORE_NUMBER;i++) {
var core=new Core(i);
core.boot();
cores.push(core);
}
loaded("cpu");
}

function cpu_update() {
for(var i=0;i<cores.length;i++) {
cores[i].run_next();
}
}
80 changes: 80 additions & 0 deletions assets/scripts/display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

var INVERSE=0;

var display={
context:null,
content:[

],
dirty:true,
height:0,
font_size:[7,10],
width:0
};

function display_init() {
display.context=$("#display").get(0).getContext("2d");
$(window).resize(display_resize);
display_resize();
loaded("display");
}

function display_resize() {
display.width=$(window).width();
display.height=$(window).height();
display.context.canvas.width=display.width;
display.context.canvas.height=display.height;
display.dirty=true;
}

function display_clear() {
display.context.fillStyle="#000";
display.context.fillRect(0,0,display.context.canvas.width,display.context.canvas.height);
}

function setPixel(id,x,y,r,g,b,a) {
if(x >= id.width)
return;
if(y >= id.height)
return;
var index=(x+y*id.width)*4;
id.data[index+0]=r;
id.data[index+1]=g;
id.data[index+2]=b;
id.data[index+3]=a;
}

function display_glyph(id,glyph) {
var g=FONT[glyph[0]]
if(!(glyph[0] in FONT))
g=FONT["undefined"]
for(var y=0;y<g.length;y++) {
var col=g[y];
for(var x=0;x<col.length;x++) {
var mul=1;
var x_pixel=(x+(glyph[1]*display.font_size[0]))*mul+mul;
var y_pixel=(y+(glyph[2]*display.font_size[1]))*mul+mul;
if(col[x] != " ")
setPixel(id,x_pixel,y_pixel,255,255,255,255);
}
}
}

function display_draw() {
var id=display.context.createImageData(display.width,display.height);
for(var i=0;i<display.content.length;i++) {
display_glyph(id,display.content[i]);
}
display.context.putImageData(id,0,0);
}

function display_redraw() {
display_clear();
display_draw();
// display.dirty=false;
}

function display_update() {
if(display.dirty)
display_redraw();
}
Loading

0 comments on commit 4b6ef73

Please sign in to comment.