Skip to content

Instantly share code, notes, and snippets.

@bzikarsky
Created December 6, 2011 07:17
Show Gist options
  • Save bzikarsky/1437167 to your computer and use it in GitHub Desktop.
Save bzikarsky/1437167 to your computer and use it in GitHub Desktop.
Solution to canyoucrackit#2
String.prototype.lpad = function(length, padString) {
var str = this;
if (padString == undefined) {
padString = ' ';
}
while (str.length < length) {
str = padString + str;
}
return str;
}
Number.prototype.toHex = function(zeroFill) {
if (zeroFill == undefined) {
zeroFill = 2;
}
return "0x" + this.toString(16).lpad(zeroFill, '0');
}
var VM = {
MEM_SEGMENT_SIZE: 0x10,
mem: [],
cpu: {
ip: 0x00,
r0: 0x00,
r1: 0x00,
r2: 0x00,
r3: 0x00,
cs: 0x00,
ds: 0x10,
fl: 0x00,
firmware: [0xd2ab1f05, 0xda13f110]
},
interrupted: null,
cycles: 0,
lastIP: null,
observers: {},
ops: {
// jumpn r1
0x00: function(op) {
this.r('ip', this.r(op));
this._notifyOp("JMPN %r", [op]);
},
// jumpf r2:r1
0x01: function(op) {
var op2 = this.next();
this.r('ip', this.r(op));
this.r('cs', op2);
this._notifyOp("JMPF %r %i", [op, op2]);
},
// movr0 r1, r2
0x02: function(op) {
var op2 = this.next();
this.r(op, this.r(op2));
this._notifyOp("MOVR0 %r %r", [op, op2]);
},
// movr1 r1, imm
0x03: function(op) {
var op2 = this.next();
this.r(op, op2);
this._notifyOp("MOVR1 %r %i", [op, op2]);
},
// movm0 r1, [ds:r2]
0x04: function(op) {
var op2 = this.next();
this.r(op, this.m([this.r('ds'), this.r(op2)]));
this._notifyOp("MOVM0 %r [ds:%r]", [op, op2]);
},
// movm1 [ds:r1], r2
0x05: function(op) {
var op2 = this.next();
this.m([this.r('ds'), this.r(op)], this.r(op2));
this._notifyOp("MOVM1 [ds:%r] %r", [op, op2]);
},
// add0 r1, r2
0x06: function(op) {
var op2 = this.next();
this.r(op, this.r(op) + this.r(op2));
this._notifyOp("ADD0 %r %r", [op, op2]);
},
// add1 r1, imm
0x07: function(op) {
var op2 = this.next();
this.r(op, this.r(op) + op2);
this._notifyOp("ADD1 %r %i", [op, op2]);
},
// xor0 r1, r2
0x08: function(op) {
var op2 = this.next();
this.r(op, this.r(op) ^ this.r(op2));
this._notifyOp("XOR0 %r %r", [op, op2]);
},
// xor1 r1, imm
0x09: function(op) {
var op2 = this.next();
this.r(op, this.r(op) ^ op2);
this._notifyOp("XOR1 %r %i", [op, op2]);
},
// cmp0 r1, r2
0x0a: function(op) {
var op2 = this.next();
this.r('fl', this._cmp(this.r(op), this.r(op2)));
this._notifyOp("CMP0 %r %r", [op, op2]);
},
// cmp1 r1, imm
0x0b: function(op) {
var op2 = this.next();
this.r('fl', this._cmp(this.r(op), op2));
this._notifyOp("CMP1 %r %i", [op, op2]);
},
// jmpen r1
0x0c: function(op) {
if (this.r('fl') == 0x00) {
this.r('ip', this.r(op));
this._notifyOp("JMPEN %r", [op]);
} else {
this._notifyOp("NOP (JMPEN)", []);
}
},
// jmpef r2:r1
0x0d: function(op) {
if (this.r('fl') == 0x00) {
var op2 = this.next();
this.r('ip', this.r(op));
this.r('cs', op2);
this._notifyOp("JMPEF %r %i", op, op2);
} else {
this._notifyOp("NOP (JMPEF)");
}
},
// hlt
0x0e: function(op) {
this.interrupt("HALT");
this._notifyOp("HLT");
},
// hlt
0x0f: function(op) {
this.interrupt("HALT");
this._notifyOp("HLT");
}
},
r: function(r, v) {
var trans = {4: 'cs', 5: 'ds'};
r = trans[r] != undefined ? trans[r] : r;
if (typeof r == "number") {
r = "r" + r;
}
if (this.cpu[r] == undefined) {
throw "Invalid register " + r;
}
if (v != undefined) {
this.notify("REG_WRITE", {register: r, value: v});
this.cpu[r] = v;
} else {
this.notify("REG_READ", {register: r});
return this.cpu[r];
}
},
m: function(adr, v) {
if (typeof adr != "number") {
adr = (adr[0] << 4) + adr[1];
}
if (this.mem[adr] == undefined) {
throw new "Memory access violation at " + adr.toHex();
}
if (v != undefined) {
this.notify("MEM_WRITE", {idx: adr, value: v});
this.mem[adr] = v;
} else {
this.notify("MEM_READ", {idx: adr});
return this.mem[adr];
}
},
_cmp: function(v1, v2) {
if (v1 == v2) {
return 0x00;
}
return (v1 > v2) ? 0x01 : 0xff;
},
next: function() {
return this.m([this.cpu.cs, this.cpu.ip++]);
},
exec: function(limitCycles) {
var lim = limitCycles ? this.cycles + limitCycles : -1;
while (this.interrupted == null && (lim == -1 || this.cycles < lim)) {
this.lastIP = this.cpu['ip'];
var byt = this.next();
var opc = byt >> 4;
var op1 = byt & 0x0F;
if (!this.ops[opc]) {
throw "Unknown opcode " + opx.toHex();
}
this.ops[opc].call(this, op1);
this.cycles++;
}
return this.interrupted != null;
},
_notifyOp: function(format, args) {
var data = {
ip: this.lastIP,
cycle: this.cycles+1,
nextip: this.cpu['ip'],
message: this._format(format, args)
};
this.notify("OP", data);
},
_format: function(format, args) {
args = args || [];
for (var i=0;i<args.length; i++) {
var reg = /%([a-z])/;
var match = format.match(reg);
var replace = "@undef@";
switch (match[1]) {
case 'r': replace = 'r' + args[i]; break;
case 'i': replace = args[i].toHex(); break;
}
format = format.replace(reg, replace);
}
return format;
},
interrupt: function(type) {
this.interrupted = type;
this.notify("INTERRUPT", {interrupt: type});
},
initMemory: function(mem, offset) {
if (offset == undefined) {
offset = 0;
}
for (var i=0; i<mem.length; i++) {
this.mem[offset + i] = mem[i];
}
},
initCPU: function(reg) {
for (var key in reg) {
this.cpu[key] = reg[key];
}
},
reset: function() {
this.interrupted = null;
this.cycles = 0;
this.lastIP = null;
},
init: function(cpu, mem) {
this.reset();
this.initCPU(cpu);
this.initMemory(mem);
},
addObserver: function(e, cb) {
if (this.observers[e] == undefined) {
this.observers[e] = [];
}
this.observers[e].push(cb);
},
notify: function(e, data) {
if (this.observers[e] == undefined) {
return;
}
for (var i=0; i<this.observers[e].length; i++) {
this.observers[e][i](data, e);
}
}
};
var VM_MEM = [
0x31, 0x04, 0x33, 0xaa, 0x40, 0x02, 0x80, 0x03, 0x52, 0x00, 0x72, 0x01, 0x73, 0x01, 0xb2, 0x50,
0x30, 0x14, 0xc0, 0x01, 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x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8, 0xcb, 0xd8, 0xd1,
0x8b, 0xd5, 0x82, 0xd9, 0x9a, 0xf1, 0x92, 0xab, 0xe8, 0xa6, 0xd6, 0xd0, 0x8c, 0xaa, 0xd2, 0x94,
0xcf, 0x45, 0x46, 0x67, 0x20, 0x7d, 0x44, 0x14, 0x6b, 0x45, 0x6d, 0x54, 0x03, 0x17, 0x60, 0x62,
0x55, 0x5a, 0x4a, 0x66, 0x61, 0x11, 0x57, 0x68, 0x75, 0x05, 0x62, 0x36, 0x7d, 0x02, 0x10, 0x4b,
0x08, 0x22, 0x42, 0x32, 0xba, 0xe2, 0xb9, 0xe2, 0xd6, 0xb9, 0xff, 0xc3, 0xe9, 0x8a, 0x8f, 0xc1,
0x8f, 0xe1, 0xb8, 0xa4, 0x96, 0xf1, 0x8f, 0x81, 0xb1, 0x8d, 0x89, 0xcc, 0xd4, 0x78, 0x76, 0x61,
0x72, 0x3e, 0x37, 0x23, 0x56, 0x73, 0x71, 0x79, 0x63, 0x7c, 0x08, 0x11, 0x20, 0x69, 0x7a, 0x14,
0x68, 0x05, 0x21, 0x1e, 0x32, 0x27, 0x59, 0xb7, 0xcf, 0xab, 0xdd, 0xd5, 0xcc, 0x97, 0x93, 0xf2,
0xe7, 0xc0, 0xeb, 0xff, 0xe9, 0xa3, 0xbf, 0xa1, 0xab, 0x8b, 0xbb, 0x9e, 0x9e, 0x8c, 0xa0, 0xc1,
0x9b, 0x5a, 0x2f, 0x2f, 0x4e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
var VM_CPU = {
ip: 0x00,
r0: 0x00,
r1: 0x00,
r2: 0x00,
r3: 0x00,
cs: 0x00,
ds: 0x10,
fl: 0x00,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment