Way too late first proper commit.

This commit is contained in:
Florian Nücke
2020-09-12 05:34:47 +02:00
parent 8f2940ebca
commit d86f9be2e7
61 changed files with 7453 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
package li.cil.circuity.vm.riscv;
import li.cil.circuity.vm.device.memory.ByteBufferMemory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ByteBufferMemoryTests {
private ByteBufferMemory memory;
@BeforeEach
public void initialize() throws Exception {
memory = new ByteBufferMemory(4 * 1024);
memory.store32(0x00, 0x11223344);
memory.store32(0x10, 0x55667788);
memory.store32(0x20, 0x99AABBCC);
}
@Test
public void testLoad8() throws Exception {
Assertions.assertEquals((byte) 0x44, memory.load8(0x00 + 0));
Assertions.assertEquals((byte) 0x33, memory.load8(0x00 + 1));
Assertions.assertEquals((byte) 0x22, memory.load8(0x00 + 2));
Assertions.assertEquals((byte) 0x11, memory.load8(0x00 + 3));
Assertions.assertEquals((byte) 0x88, memory.load8(0x10 + 0));
Assertions.assertEquals((byte) 0x77, memory.load8(0x10 + 1));
Assertions.assertEquals((byte) 0x66, memory.load8(0x10 + 2));
Assertions.assertEquals((byte) 0x55, memory.load8(0x10 + 3));
Assertions.assertEquals((byte) 0xCC, memory.load8(0x20 + 0));
Assertions.assertEquals((byte) 0xBB, memory.load8(0x20 + 1));
Assertions.assertEquals((byte) 0xAA, memory.load8(0x20 + 2));
Assertions.assertEquals((byte) 0x99, memory.load8(0x20 + 3));
}
@Test
public void testStore8() throws Exception {
memory.store8(0x00 + 0, (byte) 0x11);
memory.store8(0x00 + 1, (byte) 0x22);
memory.store8(0x00 + 2, (byte) 0x33);
memory.store8(0x00 + 3, (byte) 0x44);
Assertions.assertEquals(0x44332211, memory.load32(0x00 + 0));
}
@Test
public void testLoad16() throws Exception {
Assertions.assertEquals((short) 0x3344, memory.load16(0x00 + 0));
Assertions.assertEquals((short) 0x1122, memory.load16(0x00 + 2));
Assertions.assertEquals((short) 0x7788, memory.load16(0x10 + 0));
Assertions.assertEquals((short) 0x5566, memory.load16(0x10 + 2));
Assertions.assertEquals((short) 0x99AA, memory.load16(0x20 + 2));
Assertions.assertEquals((short) 0xBBCC, memory.load16(0x20 + 0));
}
@Test
public void testStore16() throws Exception {
memory.store16(0x00 + 0, (short) 0x2211);
memory.store16(0x00 + 2, (short) 0x4433);
Assertions.assertEquals(0x44332211, memory.load32(0x00 + 0));
}
@Test
public void testLoad32() throws Exception {
Assertions.assertEquals(0x11223344, memory.load32(0x00));
Assertions.assertEquals(0x55667788, memory.load32(0x10));
Assertions.assertEquals(0x99AABBCC, memory.load32(0x20));
}
@Test
public void testStore32() throws Exception {
memory.store32(0, 0x44332211);
Assertions.assertEquals(0x44332211, memory.load32(0x00));
}
}

View File

@@ -0,0 +1,150 @@
package li.cil.circuity.vm.riscv;
import li.cil.circuity.api.vm.device.memory.MemoryAccessException;
import li.cil.circuity.api.vm.device.memory.PhysicalMemory;
import li.cil.circuity.vm.device.memory.ByteBufferMemory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class InstructionTests {
static final Logger LOGGER = LogManager.getLogger();
PhysicalMemory memory;
R5Board board;
@BeforeEach
public void initialize() {
board = new R5Board();
memory = new ByteBufferMemory(64 * 1014 * 1024);
board.addDevice(0x80000000, memory);
}
@Test
public void testLUI() throws MemoryAccessException {
loadProgram("lui a0, 0xf000");
board.step(2);
final R5CPUStateSnapshot state = board.getCpu().getState();
Assertions.assertEquals(0xf000, state.x[10]);
}
@Test
public void testAUIPC() throws MemoryAccessException {
loadProgram("auipc a0, 0");
board.step(2);
final R5CPUStateSnapshot state = board.getCpu().getState();
Assertions.assertEquals(state.pc - 4, state.x[10]);
}
@Test
public void testJAL() throws MemoryAccessException {
loadProgram("jal a0, 0xf0");
final R5CPUStateSnapshot state1 = board.getCpu().getState();
board.step(2);
final R5CPUStateSnapshot state2 = board.getCpu().getState();
Assertions.assertEquals(state2.pc, state1.pc + 0xf0);
Assertions.assertEquals(state1.pc + 4, state2.x[10]);
board.getCpu().setState(state1);
loadProgram("jal a0, 0xf1");
board.step(2);
final R5CPUStateSnapshot state3 = board.getCpu().getState();
Assertions.assertEquals(state3.pc, state1.pc + 0xf0);
Assertions.assertEquals(state1.pc + 4, state3.x[10]);
}
@Test
public void testJALR() throws MemoryAccessException {
loadProgram("addi a0, a0, 0x100",
"jalr a0, a0, 0x200");
final R5CPUStateSnapshot state1 = board.getCpu().getState();
board.step(3);
final R5CPUStateSnapshot state2 = board.getCpu().getState();
Assertions.assertEquals(state2.pc, 0x300);
Assertions.assertEquals(state1.pc + 8, state2.x[10]);
}
@Test
public void testADDI() throws MemoryAccessException {
loadProgram("addi a0, a0, 42");
board.step(2);
final R5CPUStateSnapshot state = board.getCpu().getState();
Assertions.assertEquals(42, state.x[10]);
}
@Test
public void testBios() throws Exception {
// final String firmware = "C:\\Users\\fnuecke\\Documents\\Repositories\\Circuity-1.15\\buildroot\\fw_jump.bin";
// loadProgramFile(firmware, 0);
//
// final String kernel = "C:\\Users\\fnuecke\\Documents\\Repositories\\Circuity-1.15\\buildroot\\Image";
// loadProgramFile(kernel, 0x400000);
//
// final UARTReader reader = new UARTReader(board);
// final Thread thread = new Thread(reader);
// thread.start();
//
// final int n = 10000000;
// for (int i = 0; i < n; i++) {
// board.step(100000);
// }
//
// reader.stop();
// thread.join();
}
private void loadProgram(final String... value) throws MemoryAccessException {
R5Assembler.assemble(value, memory, 0);
}
private void loadProgramFile(final String path, int address) throws Exception {
try (final FileInputStream is = new FileInputStream(path)) {
final BufferedInputStream bis = new BufferedInputStream(is);
for (int value = bis.read(); value != -1; value = bis.read()) {
memory.store8(address++, (byte) value);
}
}
}
private static final class UARTReader implements Runnable {
private final R5Board board;
private boolean keepRunning = true;
private UARTReader(final R5Board board) {
this.board = board;
}
public void stop() {
keepRunning = false;
}
@Override
public void run() {
try {
while (keepRunning) {
final int i = board.readValue();
if (i >= 0) {
System.out.print((char) i);
}
Thread.yield();
}
} catch (final Throwable t) {
LOGGER.error(t);
}
}
}
}

View File

@@ -0,0 +1,13 @@
package li.cil.circuity.vm.riscv;
import li.cil.circuity.vm.device.memory.ByteBufferMemory;
import org.junit.jupiter.api.Test;
public class VirtualMachineTests {
@Test
public void testVM() {
final R5Board virtualMachine = new R5Board();
virtualMachine.addDevice(new ByteBufferMemory(4 * 1024 * 1024));
virtualMachine.reset();
}
}