mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Division by zero triggers INT #231
This commit is contained in:
parent
2ff19f24eb
commit
84a9132f5e
@ -154,7 +154,6 @@ public class CPU implements MongoSerializable {
|
|||||||
|
|
||||||
registerSetSize = registerSet.size();
|
registerSetSize = registerSet.size();
|
||||||
|
|
||||||
// status.breakFlag = true;
|
|
||||||
while (!status.isBreakFlag()) {
|
while (!status.isBreakFlag()) {
|
||||||
counter++;
|
counter++;
|
||||||
|
|
||||||
|
@ -32,9 +32,7 @@ public class DivInstruction extends Instruction {
|
|||||||
(cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
|
(cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
|
||||||
|
|
||||||
if (src.get(srcIndex) == 0) {
|
if (src.get(srcIndex) == 0) {
|
||||||
//Division by 0
|
cpu.interrupt(IntInstruction.INT_DIVISION_BY_ZERO);
|
||||||
status.setBreakFlag(true);
|
|
||||||
status.setErrorFlag(true);
|
|
||||||
} else {
|
} else {
|
||||||
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src.get(srcIndex)));
|
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src.get(srcIndex)));
|
||||||
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src.get(srcIndex)));
|
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src.get(srcIndex)));
|
||||||
@ -52,15 +50,12 @@ public class DivInstruction extends Instruction {
|
|||||||
(cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
|
(cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
|
||||||
|
|
||||||
if (src == 0) {
|
if (src == 0) {
|
||||||
//Division by 0
|
cpu.interrupt(IntInstruction.INT_DIVISION_BY_ZERO);
|
||||||
status.setBreakFlag(true);
|
|
||||||
status.setErrorFlag(true);
|
|
||||||
} else {
|
} else {
|
||||||
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src));
|
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src));
|
||||||
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src));
|
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package net.simon987.mar.server.assembly.instruction;
|
||||||
|
|
||||||
|
import net.simon987.mar.server.TestExecutionResult;
|
||||||
|
import net.simon987.mar.server.assembly.TestHelper;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class DivInstructionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void divisionByZero1() {
|
||||||
|
String code = "" +
|
||||||
|
"isr: \n" +
|
||||||
|
" MOV X, 0x1234 \n" +
|
||||||
|
" IRET \n" +
|
||||||
|
".text \n" +
|
||||||
|
"MOV [0], isr \n" +
|
||||||
|
"MOV A, 0 \n" +
|
||||||
|
"DIV A \n" +
|
||||||
|
"MOV Y, 0x4567 \n" +
|
||||||
|
"brk \n";
|
||||||
|
|
||||||
|
TestExecutionResult res = TestHelper.executeCode(code);
|
||||||
|
|
||||||
|
assertEquals(0x1234, res.regValue("X"));
|
||||||
|
assertEquals(0x4567, res.regValue("Y"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void divisionByZero2() {
|
||||||
|
String code = "" +
|
||||||
|
"isr: \n" +
|
||||||
|
" MOV X, 0x1234 \n" +
|
||||||
|
" IRET \n" +
|
||||||
|
".text \n" +
|
||||||
|
"MOV [0], isr \n" +
|
||||||
|
"DIV 0 \n" +
|
||||||
|
"MOV Y, 0x4567 \n" +
|
||||||
|
"brk \n";
|
||||||
|
|
||||||
|
TestExecutionResult res = TestHelper.executeCode(code);
|
||||||
|
|
||||||
|
assertEquals(0x1234, res.regValue("X"));
|
||||||
|
assertEquals(0x4567, res.regValue("Y"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user