import java.util.*; import java.io.*; public class Z80compiler { static byte[] mc = new byte[32768]; //memory map static int loc = 0; //location in memory map static Hashtable labels = new Hashtable(); //hashtable storing references "name" / loc static String[] fixref = new String[32768]; //reference of labels to plug back in static Hashtable vars = new Hashtable(); //table storing var names/loc static String[] fixvar = new String[32768]; //reference of vars to plug back in static Stack ifstack = new Stack(); //pop ifcount and increment ifcount when going if static int ifcount = 0; public static void main(String[] args) { System.out.println("STRANGE BEHAVIOR MAY OCCUR IF FILE IS NOT ENDED WITH A NEWLINE"); //make sure we've got two args if (args.length != 2) { System.out.println("Usage: java Z80compiler "); System.exit(0); } //open the source file EasyReader source = new EasyReader(args[0]); //source file //clear out the fixcharts for (int i=0; i")) { //NEITHER NEGATIVE OR ZERO AFTER SUBTRACTION writeByte(250); //JP M endiflabel fixref[loc] = "_RESERVED_ENDIF_LABEL_" + ifcount; writeByte(0); writeByte(0); writeByte(202); //JP Z endiflabel fixref[loc] = "_RESERVED_ENDIF_LABEL_" + ifcount; writeByte(0); writeByte(0); ifstack.push(new Integer(ifcount)); ifcount++; } } if (command.equals("endif")) { //endif - push a label labels.put("_RESERVED_ENDIF_LABEL_" + ifstack.peek(), new Integer(loc)); //System.out.println("_RESERVED_ENDIF_LABEL_" + ifstack.pop() + " at " + loc + "d"); } if (command.equals("add")) { // add var1 value/var2 String variable = source.readWord(); String value = source.readWord(); writeByte(58); //LD A, (var1) fixvar[loc] = variable; writeByte(0); writeByte(0); if (isNumber(value)) { //number writeByte(198); //ADD a, value writeByte(Integer.parseInt(value)); } else { //variable writeByte(33); //LD HL, value fixvar[loc] = value; writeByte(0); writeByte(0); writeByte(134); //ADD A, (HL) } writeByte(50); //LD (var1), A fixvar[loc] = variable; writeByte(0); writeByte(0); } if (command.equals("return")) { //ret writeByte(201); //RET } if (command.equals("call")) { //cal label writeByte(205); //CALL label fixref[loc] = source.readWord(); writeByte(0); writeByte(0); } if (command.equals("sub")) { // sub var1 value/var2 String variable = source.readWord(); String value = source.readWord(); writeByte(58); //LD A, (var1) fixvar[loc] = variable; writeByte(0); writeByte(0); if (isNumber(value)) { //number writeByte(214); //SUB a, value writeByte(Integer.parseInt(value)); } else { //variable writeByte(33); //LD HL, value fixvar[loc] = value; writeByte(0); writeByte(0); writeByte(150); //SUB A, (HL) } writeByte(50); //LD (var1), A fixvar[loc] = variable; writeByte(0); writeByte(0); } command = source.readWord(); } //3. write in variable space, record the address writeByte(118); //HALT, just incase they forgot Iterator iter = vars.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); int value = ((Integer)vars.get(key)).intValue(); writeByte(value); vars.put(key, new Integer(loc-1)); System.out.println("CREATED VARIBLE: " + key + "=" + value + " at " + (loc-1) + "d"); } int END_OF_MEMORY = loc; //4. go through fixvars and plug in variable addresses for (int i=0; i