@@ -27,33 +27,38 @@ void setup() {
27
27
28
28
@ Test
29
29
void testLoadFromAddressOperation () {
30
- int operand = Registry .nameToIdx (Registry .REG_R1 ); // Operand: register to load value into
30
+ int op1 = Registry .nameToIdx (Registry .REG_R1 );
31
+ int op2 = Registry .nameToIdx (Registry .REG_RES );
32
+ int operand = (op1 << 4 ) | op2 ;
31
33
int value = 123 ; // The value to be loaded from memory
32
34
int memoryAddress = 33 ; // The address in memory where value is stored
33
35
34
36
// Simulate the program counter pointing to the next memory address
35
37
when (mockPC .next ()).thenReturn (1 );
36
38
37
- // Simulate memory returning the memory address
38
- when (mockMemory .getValueAt (1 )).thenReturn (memoryAddress );
39
+ // Simulate fetching the operand from memory
40
+ when (mockMemory .getValueAt (1 )).thenReturn (operand );
39
41
40
- // Simulate memory returning the value from the memory address
42
+ // Simulate fetching the value from the register specified by the operand
43
+ when (mockRegistry .getValueAt (op1 )).thenReturn (memoryAddress );
41
44
when (mockMemory .getValueAt (memoryAddress )).thenReturn (value );
42
45
43
- LdA ldaInstruction = new LdA (operand );
46
+ LdA ldaInstruction = new LdA (0 );
44
47
ldaInstruction .execute (mockMemory , mockRegistry , mockPC , null );
45
48
46
49
// Verify the registry's setRegister method is called with the correct value
47
- verify (mockRegistry ).setValueAt (operand , value );
50
+ verify (mockRegistry ).setValueAt (op2 , value );
48
51
}
49
52
50
53
@ Test
51
54
void testPrettyPrint () {
52
- when (mockMemory .getValueAt (1 )).thenReturn (81 );
53
-
54
- LdA load = new LdA (Registry .nameToIdx (Registry .REG_R1 ));
55
+ LdA load = new LdA (0 );
56
+ int leftOp = Registry .nameToIdx (Registry .REG_R2 );
57
+ int rightOp = Registry .nameToIdx (Registry .REG_OP1 );
58
+ int operand = (leftOp << 4 ) | rightOp ;
59
+ when (mockMemory .getValueAt (1 )).thenReturn (operand );
55
60
assertEquals (
56
- InstructionFactory .INST_NAME_LDA + " (m[81] " + Instruction .RIGHT_ARROW_CHAR + " R1 )" ,
61
+ InstructionFactory .INST_NAME_LDA + " (*R2 " + Instruction .RIGHT_ARROW_CHAR + " OP1 )" ,
57
62
load .prettyPrint (mockMemory , mockRegistry , 0 ));
58
63
}
59
64
}
0 commit comments