Skip to content

Commit

Permalink
[#201] SSEM: Fix code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Sep 3, 2021
1 parent 0cb5c62 commit e05ee04
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public ByteBuffer generateCode(Program program) {
} else if (instruction.tokenType == SSEMParser.NUM) {
code.putInt((int) NumberUtils.reverseBits(instruction.operand, 32));
} else {
System.out.println(instruction);
writeInstruction(instruction.getOpcode(), instruction.operand);
}
});
return code.clear();
}

private void writeInstruction(int opcode, long operand) {
int instruction = (int)((operand & 0x1F) | (((opcode & 0x07) << 12)));
int instruction = (int)((operand & 0x1F) | (((opcode & 0x07) << 13)));
code.putInt(NumberUtils.reverseBits(instruction, 32));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testCommandLine() throws Exception {

byte[] expected = new byte[33 * 4];
expected[4] = 0x68;
expected[5] = 0x06;
expected[5] = 0x03;
assertArrayEquals(expected, bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotEquals;

public class SSEMCompilerTest {
private SSEMCompiler compiler;
Expand Down Expand Up @@ -98,24 +97,24 @@ public void testCopyrightIsKnown() {
@Test
public void testSTO() throws Exception {
compile("00 STO 22\n");
assertProgram(0x68, 6, 0, 0);
assertProgram(0x68, 3, 0, 0);
}

@Test
public void testLDN() throws Exception {
compile("00 LDN 29");
assertProgram(0xB8,4,0,0);
assertProgram(0xB8,2,0,0);
}

@Test
public void testSUB() throws Exception {
compile("00 SUB 30");
assertProgram(0x78,8,0,0);
assertProgram(0x78,4,0,0);
}

@Test
public void testSTP() throws Exception {
compile("00 STP");
assertProgram(0,0x0E,0,0);
assertProgram(0,7,0,0);
}
}

0 comments on commit e05ee04

Please sign in to comment.