Skip to content

Commit

Permalink
ExtAsmDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
aimozg committed Apr 5, 2012
1 parent c17a4a9 commit 8fe7951
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ExtAsmDemo.META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: dcpu.demos.ExtAsmDemo

91 changes: 91 additions & 0 deletions dcpu/demos/ExtAsmDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package dcpu.demos;

import dcpu.Assembler;
import dcpu.Dcpu;
import dcpu.io.InstreamPeripheral;
import dcpu.io.OutstreamPeripheral;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.util.Arrays;

/**
* Assembles and executes external asm file
*/
public class ExtAsmDemo {
public static void main(String[] args) {
String filename = null;
String outbin = null;
int ai = 0;
while (ai < args.length) {
String arg = args[ai++];
if (arg.startsWith("-")) {
if (arg.equals("-O")) {
if (ai == args.length) fail("Missing argument");
outbin = args[ai++];
} else {
// TODO options: run binary, decompile binary, trace, run some cycles
fail("Unrecognized option `%s` . Aborting\n", arg);
}
} else {
if (filename != null) {
fail("Multiple filenames (%s and %s). Aborting\n", filename, args);
}
filename = arg;
}
}
if (filename == null) {
fail("DCPU-16 Assembler and Emulator demo.\n" +
"Usage:\n" +
"\tjava -jar ja-dcpu-demo.jar [OPTIONS] SOURCE\n" +
"Will assemble and execute specified file\n" +
"OPTIONS:\n" +
"\t-O BINOUT save compiled binary to BINOUT file\n");
}
////////////////////////////////
try {
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(filename);
char[] csources = new char[fileInputStream.available()];
new InputStreamReader(fileInputStream).read(csources, 0, csources.length);

Assembler assembler = new Assembler();
short[] bytecode = assembler.assemble(new String(csources));

if (outbin != null) {
FileOutputStream outfile = new FileOutputStream(outbin);
for (short i : bytecode) {
outfile.write((i >> 8) & 0xff);
outfile.write(i & 0xff);
}
outfile.close();
}
Dcpu cpu = new Dcpu();
cpu.upload(bytecode);
OutstreamPeripheral stdout = new OutstreamPeripheral(System.out);
cpu.attach(stdout, 0x8);
InstreamPeripheral stdin = new InstreamPeripheral(System.in, 100);
cpu.attach(stdin, 0x9);

cpu.run();

} catch (IOException e) {
e.printStackTrace();
fail("");
}
}

private static void print(String s) {
System.out.print(s);
}

private static void fail(String fmt, Object... args) {
printf(fmt, args);
System.exit(-1);
}

private static void printf(String fmt, Object... args) {
System.out.printf(fmt, args);
}
}
9 changes: 6 additions & 3 deletions dcpu/demos/StdinoutDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import dcpu.io.InstreamPeripheral;
import dcpu.io.OutstreamPeripheral;

import java.io.ByteArrayOutputStream;

/**
* Asks user's name and greets him.
*/
Expand Down Expand Up @@ -73,17 +75,18 @@ public static void main(String[] args) {
" SET PC,readln\n" +
"\n" +
":greet1\n" +
" dat \"Hello, what's your name?\",0xa,0\n" +
" dat \"Hello, what's your name?\",0xd,0xa,0\n" +
":greet2\n" +
" dat \"Hello, \",0\n" +
":greet3\n" +
" dat \"!\",0xa,0\n" +
" dat \"!\",0xd,0xa,0\n" +
":end\n" +
" dat 0\n" +
":name"
));

OutstreamPeripheral stdout = new OutstreamPeripheral(System.out);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutstreamPeripheral stdout = new OutstreamPeripheral(byteArrayOutputStream);
cpu.attach(stdout, 0x8);
InstreamPeripheral stdin = new InstreamPeripheral(System.in, 100);
cpu.attach(stdin, 0x9);
Expand Down

0 comments on commit 8fe7951

Please sign in to comment.