Skip to content

Commit ff03ec8

Browse files
committed
test(instruction): add ADD, SUB and WDR unit tests
1 parent 755a7f9 commit ff03ec8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/cpu/instruction.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,41 @@ describe('avrInstruction', () => {
8080
expect(cpu.data[SREG]).toEqual(SREG_H | SREG_Z | SREG_C);
8181
});
8282

83+
it('should execute `ADD r0, r1` instruction when result overflows', () => {
84+
loadProgram('ADD r0, r1');
85+
cpu.data[r0] = 11;
86+
cpu.data[r1] = 245;
87+
avrInstruction(cpu);
88+
expect(cpu.pc).toEqual(1);
89+
expect(cpu.cycles).toEqual(1);
90+
expect(cpu.data[r0]).toEqual(0);
91+
expect(cpu.data[SREG]).toEqual(SREG_H | SREG_Z | SREG_C);
92+
});
93+
94+
it('should execute `ADD r0, r1` instruction when carry is on', () => {
95+
loadProgram('ADD r0, r1');
96+
cpu.data[r0] = 11;
97+
cpu.data[r1] = 244;
98+
cpu.data[SREG] = SREG_C;
99+
avrInstruction(cpu);
100+
expect(cpu.pc).toEqual(1);
101+
expect(cpu.cycles).toEqual(1);
102+
expect(cpu.data[r0]).toEqual(255);
103+
expect(cpu.data[SREG]).toEqual(SREG_S | SREG_N);
104+
});
105+
106+
it('should execute `ADD r0, r1` instruction when carry is on and the result overflows', () => {
107+
loadProgram('ADD r0, r1');
108+
cpu.data[r0] = 11;
109+
cpu.data[r1] = 245;
110+
cpu.data[SREG] = SREG_C;
111+
avrInstruction(cpu);
112+
expect(cpu.pc).toEqual(1);
113+
expect(cpu.cycles).toEqual(1);
114+
expect(cpu.data[r0]).toEqual(0);
115+
expect(cpu.data[SREG]).toEqual(SREG_H | SREG_Z | SREG_C);
116+
});
117+
83118
it('should execute `BCLR 2` instruction', () => {
84119
loadProgram('BCLR 2');
85120
cpu.data[SREG] = 0xff;
@@ -991,6 +1026,17 @@ describe('avrInstruction', () => {
9911026
expect(cpu.data[Z]).toEqual(0x50); // verify that Z was unchanged
9921027
});
9931028

1029+
it('should execute `SUB r0, r1` instruction when result overflows', () => {
1030+
loadProgram('SUB r0, r1');
1031+
cpu.data[r0] = 0;
1032+
cpu.data[r1] = 10;
1033+
avrInstruction(cpu);
1034+
expect(cpu.pc).toEqual(1);
1035+
expect(cpu.cycles).toEqual(1);
1036+
expect(cpu.data[r0]).toEqual(246);
1037+
expect(cpu.data[SREG]).toEqual(SREG_S | SREG_N | SREG_C);
1038+
});
1039+
9941040
it('should execute `SWAP r1` instruction', () => {
9951041
loadProgram('SWAP r1');
9961042
cpu.data[r1] = 0xa5;
@@ -1000,6 +1046,14 @@ describe('avrInstruction', () => {
10001046
expect(cpu.data[r1]).toEqual(0x5a);
10011047
});
10021048

1049+
it('should execute `WDR` instruction and call `cpu.onWatchdogReset`', () => {
1050+
loadProgram('WDR');
1051+
cpu.onWatchdogReset = jest.fn();
1052+
expect(cpu.onWatchdogReset).not.toHaveBeenCalled();
1053+
avrInstruction(cpu);
1054+
expect(cpu.onWatchdogReset).toHaveBeenCalled();
1055+
});
1056+
10031057
it('should execute `XCH Z, r21` instruction', () => {
10041058
loadProgram('XCH Z, r21');
10051059
cpu.data[r21] = 0xa1;

0 commit comments

Comments
 (0)