Skip to content

Commit

Permalink
Fix DVI rounds towards zero
Browse files Browse the repository at this point in the history
  • Loading branch information
niligulmohar committed Aug 22, 2012
1 parent f14b336 commit 4a00f48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/cpu.js
Expand Up @@ -22,6 +22,14 @@ root.getSigned = function(word) {
return word-sign;
};

var roundTowardsZero = function (number) {
if (number > 0) {
return Math.floor(number);
} else {
return Math.ceil(number);
}
};

var opcodes = [
//assembler directives
{id: 'DAT', cost: 0, args: Infinity},
Expand Down Expand Up @@ -346,7 +354,8 @@ CPU.prototype = {
this.set('ex', 0x0000);
} else {
this.set('ex', (root.getSigned(bVal) << 16) / root.getSigned(aVal));
this.set(insn.b, Math.floor(root.getSigned(bVal) / root.getSigned(aVal)));
var quotient = root.getSigned(bVal) / root.getSigned(aVal);
this.set(insn.b, roundTowardsZero(quotient));
}
break;

Expand Down
14 changes: 7 additions & 7 deletions test/cpu_test.js
Expand Up @@ -356,13 +356,13 @@ module.exports = {
assert.equal(cpu.get("ex"), 0x4000);
},

// 'DVI negative number by nonzero should round quotitient towards zero': function () {
// // -0x7FFF / 2 == -0x3fff.8
// var cpu = runOnCpu("SET a, 0x8001",
// "DVI a, 0x2");
// assert.equal(cpu.get("a"), 0xc001);
// assert.equal(cpu.get("ex"), 0x8000);
// },
'DVI negative number by nonzero should round quotitient towards zero': function () {
// -0x7FFF / 2 == -0x3fff.8
var cpu = runOnCpu("SET a, 0x8001",
"DVI a, 0x2");
assert.equal(cpu.get("a"), 0xc001);
assert.equal(cpu.get("ex"), 0x8000);
},

'DVI with no next word values should take three cycles to perform': function () {
var cpu = runOnCpu("DVI a, 1");
Expand Down

0 comments on commit 4a00f48

Please sign in to comment.