Skip to content

Commit 9d40e8f

Browse files
committed
Some instruction changes to improve usability. Minor bug fix.
1 parent 2ca6661 commit 9d40e8f

File tree

16 files changed

+247
-229
lines changed

16 files changed

+247
-229
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ our code of conduct and the process for submitting pull requests to us.
7474

7575
## License
7676

77-
This project is licensed under the [Your Choice of License] - see the LICENSE.md
78-
file for details.
77+
This project is licensed under the Apache License 2.0 - see the LICENSE.md file
78+
for details.
7979

8080
## Acknowledgments
8181

src/main/java/instruction/Inc.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package instruction;
2+
3+
import io.IO;
4+
import model.Memory;
5+
import model.ProgramCounter;
6+
import model.Registry;
7+
8+
public class Inc extends Instruction {
9+
10+
public Inc(int operand) {
11+
super(InstructionFactory.INST_NAME_INC, operand);
12+
}
13+
14+
@Override
15+
protected void internalExecute(Memory mem, Registry reg, ProgramCounter pc, IO io) {
16+
// Operand is a register index, to be incremented.
17+
reg.setValueAt(operand, reg.getValueAt(operand) + 1);
18+
}
19+
20+
@Override
21+
protected String printOperand() {
22+
return String.format("(%s)", Registry.idxToName(operand));
23+
}
24+
25+
@Override
26+
public int[] getAffectedMemoryCells(Memory mem, Registry reg, ProgramCounter pc) {
27+
return new int[] {pc.getCurrentIndex()};
28+
}
29+
30+
@Override
31+
public int[] getAffectedRegisters(Memory mem, Registry reg, ProgramCounter pc) {
32+
return new int[] {operand};
33+
}
34+
}

src/main/java/instruction/InstructionFactory.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ public class InstructionFactory {
99
public static final int INST_NOP = 000; // No-op
1010
public static final int INST_ADD = 0x10; // Addition
1111
public static final int INST_SUB = 0x20; // Subtraction
12-
public static final int INST_CPY = 0x30; // Copy value from one cell to another
13-
public static final int INST_MOV = 0x40; // Move value from one cell to another
14-
public static final int INST_LOD = 0x50; // Load the next value from memory into registry
15-
public static final int INST_LDA = 0x60; // Load address (resolve first) into registry
16-
public static final int INST_STO = 0x70; // Store register value in memory
17-
public static final int INST_JMP = 0x80; // Jump to address
18-
public static final int INST_JEQ = 0x90; // Jump if equal
19-
public static final int INST_JNE = 0xA0; // Jump if not equal
20-
public static final int INST_PRT = 0xB0; // Print
21-
public static final int INST_PRD = 0xC0; // Print
22-
public static final int INST_PRL = 0xD0; // Print Loop
23-
public static final int INST_HLT = 0xE0; // Halt
12+
public static final int INST_INC = 0x30; // Subtraction
13+
public static final int INST_CPY = 0x40; // Copy value from one cell to another
14+
public static final int INST_MOV = 0x50; // Move value from one cell to another
15+
public static final int INST_LOD = 0x60; // Load the next value from memory into registry
16+
public static final int INST_LDA = 0x70; // Load address (resolve first) into registry
17+
public static final int INST_STO = 0x80; // Store register value in memory
18+
public static final int INST_JMP = 0x90; // Jump to address
19+
public static final int INST_JEQ = 0xA0; // Jump if equal
20+
public static final int INST_JNE = 0xB0; // Jump if not equal
21+
public static final int INST_PRT = 0xC0; // Print
22+
public static final int INST_PRD = 0xD0; // Print
23+
public static final int INST_PRL = 0xE0; // Print Loop
24+
public static final int INST_HLT = 0xF0; // Halt
2425

2526
// PRT
2627
// Add 2 columns: Instr, Char
2728

2829
public static final String INST_NAME_NOP = "NOP";
2930
public static final String INST_NAME_ADD = "ADD";
3031
public static final String INST_NAME_SUB = "SUB";
32+
public static final String INST_NAME_INC = "INC";
3133
public static final String INST_NAME_CPY = "CPY";
3234
public static final String INST_NAME_MOV = "MOV";
3335
public static final String INST_NAME_LOD = "LD";
@@ -45,6 +47,7 @@ public boolean isInstruction(int code) {
4547
return code == INST_NOP
4648
|| code == INST_ADD
4749
|| code == INST_SUB
50+
|| code == INST_INC
4851
|| code == INST_CPY
4952
|| code == INST_MOV
5053
|| code == INST_LOD
@@ -70,6 +73,8 @@ public Instruction createInstruction(int code) {
7073
return new Add(operand);
7174
case INST_SUB:
7275
return new Sub(operand);
76+
case INST_INC:
77+
return new Inc(operand);
7378
case INST_CPY:
7479
return new Cpy(operand);
7580
case INST_MOV:
@@ -83,7 +88,7 @@ public Instruction createInstruction(int code) {
8388
case INST_JMP:
8489
return new Jmp(operand);
8590
case INST_JEQ:
86-
return new Je(operand);
91+
return new Jeq(operand);
8792
case INST_JNE:
8893
return new Jne(operand);
8994
case INST_PRT:

src/main/java/instruction/Je.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/main/java/instruction/Jeq.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package instruction;
2+
3+
import io.IO;
4+
import model.Memory;
5+
import model.ProgramCounter;
6+
import model.Registry;
7+
8+
public class Jeq extends Instruction {
9+
10+
public Jeq(int operand) {
11+
super(InstructionFactory.INST_NAME_JEQ, operand);
12+
}
13+
14+
@Override
15+
protected void internalExecute(Memory mem, Registry reg, ProgramCounter pc, IO io) {
16+
// Read next value, and split into two 4-bit operands.
17+
int value = mem.getValueAt(pc.next());
18+
int op1 = value >> 4;
19+
int op2 = value & 0xF;
20+
21+
// Read the next value, use as the destination address.
22+
int dst = mem.getValueAt(pc.next());
23+
24+
int a = reg.getValueAt(op1);
25+
int b = reg.getValueAt(op2);
26+
27+
if (a == b) {
28+
pc.jumpTo(dst);
29+
}
30+
}
31+
32+
@Override
33+
protected String printOperand() {
34+
return "";
35+
}
36+
37+
@Override
38+
public int[] getAffectedMemoryCells(Memory mem, Registry reg, ProgramCounter pc) {
39+
int cur = pc.getCurrentIndex();
40+
41+
int value = mem.getValueAt(cur + 1);
42+
int op1 = value >> 4;
43+
int op2 = value & 0xF;
44+
45+
int dst = mem.getValueAt(cur + 2);
46+
47+
int a = reg.getValueAt(op1);
48+
int b = reg.getValueAt(op2);
49+
50+
if (a == b) {
51+
return new int[] {cur, cur + 1, cur + 2, dst};
52+
}
53+
54+
return new int[] {cur, cur + 1, cur + 2};
55+
}
56+
57+
@Override
58+
public int[] getAffectedRegisters(Memory mem, Registry reg, ProgramCounter pc) {
59+
int cur = pc.getCurrentIndex();
60+
61+
int value = mem.getValueAt(cur + 1);
62+
int op1 = value >> 4;
63+
int op2 = value & 0xF;
64+
65+
return new int[] {op1, op2};
66+
}
67+
}

src/main/java/instruction/Jmp.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@ public Jmp(int operand) {
1313

1414
@Override
1515
protected void internalExecute(Memory mem, Registry reg, ProgramCounter pc, IO io) {
16-
// Destination address is read from register indexed by operand.
17-
int dst = reg.getValueAt(operand);
16+
// Read the next value, use as the destination address.
17+
int dst = mem.getValueAt(pc.next());
1818
pc.jumpTo(dst);
1919
}
2020

2121
@Override
2222
protected String printOperand() {
23-
return String.format("(dst: %s)", Registry.idxToName(operand));
23+
return "";
2424
}
2525

2626
@Override
2727
public int[] getAffectedMemoryCells(Memory mem, Registry reg, ProgramCounter pc) {
28-
int dst = reg.getValueAt(operand);
29-
return new int[] {pc.getCurrentIndex(), dst};
28+
int cur = pc.getCurrentIndex();
29+
int dst = mem.getValueAt(cur + 1);
30+
return new int[] {cur, cur + 1, dst};
3031
}
3132

3233
@Override
3334
public int[] getAffectedRegisters(Memory mem, Registry reg, ProgramCounter pc) {
34-
return new int[] {operand};
35+
return new int[0];
3536
}
3637
}

src/main/java/instruction/Jne.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,55 @@ public Jne(int operand) {
1313

1414
@Override
1515
protected void internalExecute(Memory mem, Registry reg, ProgramCounter pc, IO io) {
16-
// Destination address is read from register indexed by operand.
17-
// Values being compared are read from the registers OP1 and OP2.
16+
// Read next value, and split into two 4-bit operands.
17+
int value = mem.getValueAt(pc.next());
18+
int op1 = value >> 4;
19+
int op2 = value & 0xF;
1820

19-
int a = reg.getRegister(Registry.REG_OP1);
20-
int b = reg.getRegister(Registry.REG_OP2);
21+
// Read the next value, use as the destination address.
22+
int dst = mem.getValueAt(pc.next());
23+
24+
int a = reg.getValueAt(op1);
25+
int b = reg.getValueAt(op2);
2126

2227
if (a != b) {
23-
int dst = reg.getValueAt(operand);
2428
pc.jumpTo(dst);
2529
}
2630
}
2731

2832
@Override
2933
protected String printOperand() {
30-
return String.format("(dst: %s)", Registry.idxToName(operand));
34+
return "";
3135
}
3236

3337
@Override
3438
public int[] getAffectedMemoryCells(Memory mem, Registry reg, ProgramCounter pc) {
35-
int a = reg.getRegister(Registry.REG_OP1);
36-
int b = reg.getRegister(Registry.REG_OP2);
39+
int cur = pc.getCurrentIndex();
3740

38-
if (a != b) {
39-
int dst = reg.getValueAt(operand);
40-
return new int[] {pc.getCurrentIndex(), dst};
41+
int value = mem.getValueAt(cur + 1);
42+
int op1 = value >> 4;
43+
int op2 = value & 0xF;
44+
45+
int dst = mem.getValueAt(cur + 2);
46+
47+
int a = reg.getValueAt(op1);
48+
int b = reg.getValueAt(op2);
49+
50+
if (a == b) {
51+
return new int[] {cur, cur + 1, cur + 2, dst};
4152
}
4253

43-
return new int[] {pc.getCurrentIndex()};
54+
return new int[] {cur, cur + 1, cur + 2};
4455
}
4556

4657
@Override
4758
public int[] getAffectedRegisters(Memory mem, Registry reg, ProgramCounter pc) {
48-
return new int[] {
49-
Registry.nameToIdx(Registry.REG_OP1), Registry.nameToIdx(Registry.REG_OP2), operand
50-
};
59+
int cur = pc.getCurrentIndex();
60+
61+
int value = mem.getValueAt(cur + 1);
62+
int op1 = value >> 4;
63+
int op2 = value & 0xF;
64+
65+
return new int[] {op1, op2};
5166
}
5267
}

src/main/java/view/AbstractSelecter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public void endSelection() {
264264
selectStartRange = range.from();
265265
selectEndRange = range.to();
266266
mouseSelectingOngoing = false;
267+
caretPosRow = selectStartRange;
267268
paintRange(range);
268269
}
269270
}

0 commit comments

Comments
 (0)