MCPcopy Create free account
hub / github.com/badvision/jace / AssemblyHandler

Class AssemblyHandler

src/main/java/jace/assembly/AssemblyHandler.java:17–89  ·  view source on GitHub ↗

@author blurry

Source from the content-addressed store, hash-verified

15 * @author blurry
16 */
17public class AssemblyHandler implements LanguageHandler<ByteBuffer> {
18 @Override
19 public String getNewDocumentContent() {
20 return "\t\t*= $300;\n\t\t!cpu 65c02;\n;--- Insert your code here ---\n";
21 }
22
23 @Override
24 public CompileResult<ByteBuffer> compile(Program proxy) {
25 AcmeCompiler compiler = new AcmeCompiler();
26 compiler.compile(proxy);
27 return compiler;
28 }
29
30 public void compileToRam(String code) {
31 HeadlessProgram prg = new HeadlessProgram(Program.DocumentType.assembly);
32 prg.setValue(code);
33
34 CompileResult<ByteBuffer> lastResult = compile(prg);
35 if (lastResult.isSuccessful()) {
36 Emulator.withComputer(c -> {
37 RAM memory = c.getMemory();
38 ByteBuffer input = lastResult.getCompiledAsset();
39 input.rewind();
40 int startLSB = input.get();
41 int startMSB = input.get();
42 int start = startLSB + startMSB << 8;
43 System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start));
44 c.getCpu().whileSuspended(() -> {
45 int pos = start;
46 while (input.hasRemaining()) {
47 memory.write(pos++, input.get(), false, true);
48 }
49 });
50 });
51 }
52 }
53
54 @Override
55 public void execute(CompileResult<ByteBuffer> lastResult) throws Exception {
56 if (lastResult.isSuccessful()) {
57 Computer c = Emulator.withComputer(c1 -> c1, null);
58
59 RAM memory = c.getMemory();
60 ByteBuffer input = lastResult.getCompiledAsset();
61 input.rewind();
62 int startLSB = input.get() & 0x0ff;
63 int startMSB = input.get() & 0x0ff;
64 int start = startLSB + startMSB << 8;
65 // System.out.printf("Executing code at $%s%n", Integer.toHexString(start));
66 c.getCpu().whileSuspended(() -> {
67 // System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start));
68 int pos = start;
69 while (input.hasRemaining()) {
70 memory.write(pos++, input.get(), false, true);
71 }
72 // System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start));
73 c.getCpu().JSR(start);
74 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected