Skip to content

Commit

Permalink
Merge pull request #20 from schemil053/dev
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
schemil053 authored Nov 15, 2024
2 parents 7eaad39 + 0bf3ad1 commit 90a4bed
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.emilschlampp.scheCPU.tests;
package de.emilschlampp.scheCPU.tests.emulator;

import de.emilschlampp.scheCPU.compile.Compiler;
import de.emilschlampp.scheCPU.dissassembler.Decompiler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.emilschlampp.scheCPU.tests.emulator;

import de.emilschlampp.scheCPU.compile.Compiler;
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CPUExecuteTest {
@Test
public void testExecute() {
ProcessorEmulator processorEmulator = new ProcessorEmulator(5, 5, new Compiler("OUTW 16 5").compile());
processorEmulator.execute();

assertEquals(processorEmulator.getIo()[1], 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.emilschlampp.scheCPU.tests.emulator;

import de.emilschlampp.scheCPU.compile.Compiler;
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator;
import de.emilschlampp.scheCPU.util.EmulatorSandboxRestrictions;
import org.junit.jupiter.api.Test;

import java.io.PrintStream;

public class CPUProtectionTest {
@Test
public void testCPUProtection() {
String program = "OUTW 34 72\n" + //H
"OUTW 34 105"; // i


PrintStream printStream = System.out;

System.setOut(new PrintStream(System.out) {
@Override
public void print(char c) {
throw new IllegalStateException("should not print!");
}
});

try {
ProcessorEmulator processorEmulator = new ProcessorEmulator(5, 5, new Compiler(program).compile());
processorEmulator.setRestrictions(new EmulatorSandboxRestrictions().setAllowOutput(false));

while (processorEmulator.canExecute()) {
processorEmulator.execute();
}
} catch (Throwable throwable) {
System.setOut(printStream);
throw throwable;
}
System.setOut(printStream);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.emilschlampp.scheCPU.tests;
package de.emilschlampp.scheCPU.tests.emulator;

import de.emilschlampp.scheCPU.compile.Compiler;
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.emilschlampp.scheCPU.tests.highlang;

import de.emilschlampp.scheCPU.emulator.ProcessorEmulator;
import de.emilschlampp.scheCPU.high.HighProgramCompiler;
import org.junit.jupiter.api.Test;

import static de.emilschlampp.scheCPU.tests.highlang.HighlangIOCompileProcessor.IO_PRINT;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class HighlangCompileTest {
@Test
public void testHighlangCompiler() {
String test = "Hallo!";

HighProgramCompiler compiler = new HighProgramCompiler(
"reserve t "+test.length()+"\n" +
"var t \""+test+"\"\n" +
"print t"
);

compiler.setCompileProcessor(new HighlangIOCompileProcessor());


byte[] program = compiler.compile().toBytecode();

ProcessorEmulator emulator = new ProcessorEmulator(64, 10, program);

StringBuffer buffer = new StringBuffer();

while (emulator.canExecute()) {
emulator.execute();

if(emulator.getIo()[IO_PRINT] != 0) {
buffer.append((char) emulator.getIo()[IO_PRINT]);
emulator.getIo()[IO_PRINT] = 0;
}
}

assertEquals(buffer.toString(), test);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.emilschlampp.scheCPU.tests.highlang;

import de.emilschlampp.scheCPU.high.processor.CompileContext;
import de.emilschlampp.scheCPU.high.processor.CompileProcessor;

public class HighlangIOCompileProcessor extends CompileProcessor {
public static final int IO_PRINT = 129;


@Override
public void startCompile(CompileContext compileContext) {
compileContext.getOptions().put("io-print-port", ""+IO_PRINT);
compileContext.getProtectedOptions().add("io-print-port");
}

@Override
public String generatePreCompileHigh() {
return "";
}

@Override
public String generatePreCompileHighEnd() {
return "";
}

@Override
public String generatePreCompileSCHESEM() {
return "";
}

@Override
public String generateSchesemForInstruction(CompileContext compileContext, String[] instruction) {
return "";
}

@Override
public String generateAfterCompileSCHESEM(CompileContext compileContext) {
return "";
}
}

0 comments on commit 90a4bed

Please sign in to comment.