我正在学习nand2tetris课程并进行练习,但没有使用软件。相反,我正在尝试用javascript来解决所有问题。目的是建立一个nand门,然后你创建所有的筹码/门。
我为每个门单元测试写过它们然后通过,但是对于我的16位加法器,它们失败了,我不知道为什么。
function Adder() {
this.halfAdder = function(a, b) {
var gate = new Gate();
var sum = gate.XOR(a, b);
var carry = gate.AND(a, b);
return [sum, carry];
};
this.fullAdder = function(a, b, c) {
var gate = new Gate();
var out1 = this.halfAdder(a, b);
var out2 = this.halfAdder(c, out1[0]);
var result = gate.OR(out1[1], out2[1])
return [out2[0], result];
};
this.Adder16 = function(a, b) {
/*
HalfAdder(a=a[0],b=b[0],sum=out[0],carry=carry1);
FullAdder(a=a[1],b=b[1],c=carry1,sum=out[1],carry=carry2);
FullAdder(a=a[2],b=b[2],c=carry2,sum=out[2],carry=carry3);
FullAdder(a=a[3],b=b[3],c=carry3,sum=out[3],carry=carry4);
FullAdder(a=a[4],b=b[4],c=carry4,sum=out[4],carry=carry5);
FullAdder(a=a[5],b=b[5],c=carry5,sum=out[5],carry=carry6);
FullAdder(a=a[6],b=b[6],c=carry6,sum=out[6],carry=carry7);
FullAdder(a=a[7],b=b[7],c=carry7,sum=out[7],carry=carry8);
FullAdder(a=a[8],b=b[8],c=carry8,sum=out[8],carry=carry9);
FullAdder(a=a[9],b=b[9],c=carry9,sum=out[9],carry=carry10);
FullAdder(a=a[10],b=b[10],c=carry10,sum=out[10],carry=carry11);
FullAdder(a=a[11],b=b[11],c=carry11,sum=out[11],carry=carry12);
FullAdder(a=a[12],b=b[12],c=carry12,sum=out[12],carry=carry13);
FullAdder(a=a[13],b=b[13],c=carry13,sum=out[13],carry=carry14);
FullAdder(a=a[14],b=b[14],c=carry14,sum=out[14],carry=carry15);
FullAdder(a=a[15],b=b[15],c=carry15,sum=out[15],carry=carry16);
*/
var sum = [];
var carry;
sum[0] = this.halfAdder(a[0], b[0])[0];
carry = this.halfAdder(a[0], b[0])[1];
for (var i = 1; i <= 15; i++) {
sum[i] = this.fullAdder(a[i], b[i], carry)[0];
carry = this.fullAdder(a[i], b[i], carry)[1];
}
return sum;
}
}
我的大门如下:
function Gate() {
this.NAND = function(a, b) {
if (a == 1 && b == 1) return 0;
else return 1;
};
this.NOT = function(a) {
return this.NAND(a, a);
};
this.AND = function(a, b) {
return this.NOT(this.NAND(a, b));
};
this.OR = function(a, b) {
return this.NOT(this.AND(this.NOT(a), this.NOT(b)));
};
this.XOR = function(a, b) {
return this.OR(this.AND(a, this.NOT(b)), this.AND(this.NOT(a), b));
};
}
我的半加法器和全加器工作,他们通过了课程创建者提供的所有单元测试,但不是我的16位加法器。单元测试是这些:
describe('adder 16 bit tests', function() {
it('adder of a = 0000000000000000 and b = 0000000000000000 should be out = 0000000000000000', function() {
var adder = new Adder();
var a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var out = adder.Adder16(a, b);
var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
assert.deepEqual(result, out);
});
it('adder of a = 0000000000000000 and b = 1111111111111111 should be out = 1111111111111111', function() {
var adder = new Adder();
var a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var b = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var out = adder.Adder16(a, b);
var result = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
assert.deepEqual(result, out);
});
it('adder of a = 1111111111111111 and b = 1111111111111111 should be out = 1111111111111110', function() {
var adder = new Adder();
var a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var b = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var out = adder.Adder16(a, b);
var result = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0];
assert.deepEqual(result, out);
});
it('adder of a = 1010101010101010 and b = 0101010101010101 should be out = 1111111111111111', function() {
var adder = new Adder();
var a = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
var b = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
var out = adder.Adder16(a, b);
var result = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
assert.deepEqual(result, out);
});
it('adder of a = 0011110011000011 and b = 0000111111110000 should be out = 0100110010110011', function() {
var adder = new Adder();
var a = [0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1];
var b = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0];
var out = adder.Adder16(a, b);
var result = [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1];
console.log(out);
assert.deepEqual(result, out);
});
it('adder of a = 0001001000110100 and b = 1001100001110110 should be out = 1010101010101010', function() {
var adder = new Adder();
var a = [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0];
var b = [1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0];
var out = adder.Adder16(a, b);
var result = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
assert.deepEqual(result, out);
});
});
其他门和加法器的单元测试通过。