/* * Created on Oct 15, 2004 * by dr */ import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class P88 extends java.applet.Applet implements ActionListener { JTextField ipTF, axTF, irTF, cfTF; JTextField[] mem; JTextField inTF; JTextField delayTF; TextArea outTA; JLabel[] colLbl, rowLbl; JLabel[] ops = {new JLabel("P88"), new JLabel("Operations"), new JLabel("12 add"), new JLabel("13 sub"), new JLabel("15 cmp"), new JLabel("20 copy (load)"), new JLabel("21 copy (store)"), new JLabel("35 mul"), new JLabel("36 div"), new JLabel("40 jmp"), new JLabel("41 jnb"), new JLabel("42 jb"), new JLabel("55 in"), new JLabel("56 out"), new JLabel("99 nop"), new JLabel("other: halt")}; JLabel crnrLbl; JLabel ipL, axL, irL, cfL; JButton fetchB, execB, loadB, runB, resetB, delayB; JComboBox asmCB; String[] asmPgms = {"demo.as", "sum.as", "largest.as", "summer.as", "fact.as"}; boolean running, tofetch; Color memCol = new Color(255, 255, 100); Color memHLCol = new Color(255, 255, 190); Color targCol = Color.RED; Color srcCol = Color.GREEN; int addr; Timer t; int delay; AsmPgm pgms; String[] asm; public void init(){ makeComponents(); makeLayout(); tofetch = true; running = false; } public void makeComponents(){ mem = new JTextField[100]; for (int k = 0; k < mem.length; k++){ mem[k] = new JTextField("0"); mem[k].setBackground(memCol); } colLbl = new JLabel[10]; rowLbl = new JLabel[10]; for (int k = 0; k < 10; k++){ rowLbl[k] = new JLabel(""+10*k+" "); rowLbl[k].setHorizontalAlignment(JLabel.RIGHT); colLbl[k] = new JLabel(""+k+" "); colLbl[k].setHorizontalAlignment(JLabel.RIGHT); } crnrLbl = new JLabel("10 \\ 1"); crnrLbl.setHorizontalAlignment(JLabel.RIGHT); ipL = new JLabel("IP"); ipL.setHorizontalAlignment(JLabel.RIGHT); ipTF = new JTextField(5); ipTF.setBackground(memHLCol); axL = new JLabel("AX"); axL.setHorizontalAlignment(JLabel.RIGHT); axTF = new JTextField(5); irL = new JLabel("IR"); irL.setHorizontalAlignment(JLabel.RIGHT); irTF = new JTextField(5); cfL = new JLabel("CF"); cfL.setHorizontalAlignment(JLabel.RIGHT); cfTF = new JTextField("???"); fetchB = new JButton("Fetch"); fetchB.addActionListener(this); execB = new JButton("Exec"); execB.addActionListener(this); loadB = new JButton("Load"); loadB.addActionListener(this); inTF = new JTextField(10); inTF.addActionListener(this); outTA = new TextArea(10, 10); runB = new JButton("Run"); runB.addActionListener(this); resetB = new JButton("Reset"); resetB.addActionListener(this); delayB = new JButton("Set Delay"); delayB.addActionListener(this); delayTF = new JTextField(6); delay = 500; delayTF.setText(""+delay); t = new Timer(delay, this); pgms = new AsmPgm(); asmCB = new JComboBox(pgms.getNames()); } Panel getMem(){ Panel memP = new Panel(); memP.setLayout(new GridLayout(11,11)); memP.add(crnrLbl); for (int k = 0; k < 10; k++) memP.add(colLbl[k]); for (int k = 0; k < 100; k++){ if (k%10 == 0) memP.add(rowLbl[k/10]); memP.add(mem[k]); } return memP; } Panel getRegs(){ Panel regP = new Panel(); regP.add(axL); regP.add(axTF); regP.add(ipL); regP.add(ipTF); regP.add(irL); regP.add(irTF); regP.add(cfL); regP.add(cfTF); return regP; } Panel getCtl(){ Panel ctlP = new Panel(); ctlP.add(asmCB); ctlP.add(loadB); ctlP.add(new JLabel(" ")); //spacer ctlP.add(fetchB); ctlP.add(execB); ctlP.add(runB); ctlP.add(new JLabel(" ")); //spacer ctlP.add(delayB); ctlP.add(delayTF); return ctlP; } Panel getIO(){ Panel ioP = new Panel(); ioP.setLayout(new BorderLayout()); ioP.add(getIn(), BorderLayout.NORTH); ioP.add(getOut(), BorderLayout.SOUTH); return ioP; } Panel getIn(){ Panel inP = new Panel(); inP.setLayout(new BorderLayout()); inP.add(new JLabel("Input"), BorderLayout.NORTH); inP.add(inTF, BorderLayout.SOUTH); return inP; } Panel getOut(){ Panel outP = new Panel(); outP.setLayout(new BorderLayout()); outP.add(new JLabel("Output"), BorderLayout.NORTH); outP.add(outTA, BorderLayout.SOUTH); return outP; } Panel getOps(){ Panel opP = new Panel(); opP.setLayout(new GridLayout(ops.length, 1)); for (int k = 0; k < ops.length; k++) opP.add(ops[k]); return opP; } public void makeLayout(){ setLayout(new BorderLayout()); add(getRegs(), BorderLayout.NORTH); add(getCtl(), BorderLayout.SOUTH); add(getMem(), BorderLayout.CENTER); add(getIO(), BorderLayout.EAST); add(getOps(),BorderLayout.WEST); } public void loadFromClass(){ int index = asmCB.getSelectedIndex(); asm = pgms.getPgm(index); ipTF.setText(asm[1]); for (int k = 2; k < asm.length; k++){ String s = asm[k]; int loc = s.indexOf(" "); if (loc != -1) s = s.substring(0,loc); mem[k-2].setText(s); } } public void read(String fname) throws IOException { int k = 0; FileReader reader = new FileReader(fname); BufferedReader in = new BufferedReader(reader); String line = in.readLine(); if (line != null) ipTF.setText(line); else return; line = in.readLine(); while (line != null){ int loc = line.indexOf(" "); if (loc != -1) line = line.substring(0,loc); mem[k].setText(line); line = in.readLine(); k = k + 1; } } public void fetch(){ fetchB.setBackground(Color.PINK); execB.setBackground(Color.LIGHT_GRAY); int ip= integer(ipTF.getText()); String inst = mem[ip%100].getText(); irTF.setText(inst); int op = integer(irTF.getText()); op = op/1000; highlight(op); mem[ip].setBackground(memCol); ip = ip + 1; ipTF.setText(""+ip); mem[ip].setBackground(memHLCol); } public int integer(String s){ if (s.length()==0) return 0; String first; int loc = s.indexOf(" "); if (loc == -1) first = s; else first = s.substring(0,loc); return (int) Integer.parseInt(first); } public void highlight(int op){ boolean set = false; for (int k=2; k < ops.length-1; k++){ String s = ops[k].getText(); if (integer(s.substring(0,2))== op) { ops[k].setForeground(Color.BLUE); set = true; } else ops[k].setForeground(Color.BLACK); } if (set) ops[ops.length-1].setForeground(Color.BLACK); else ops[ops.length-1].setForeground(Color.BLUE); } public void exec(){ int inst, op, a, b, c; execB.setBackground(Color.PINK); fetchB.setBackground(Color.LIGHT_GRAY); inst = integer(irTF.getText()); op = inst/1000; axTF.setBackground(Color.WHITE); cfTF.setBackground(Color.WHITE); mem[addr].setBackground(memCol); addr = inst%100; // ignore address > 99 a = integer(axTF.getText()); b = integer(mem[addr].getText()); mem[addr].setBackground(srcCol); if (op == 12){ //add axTF.setText((a+b) +""); axTF.setBackground(targCol); } else if (op == 13){ //sub axTF.setText(a-b +""); axTF.setBackground(targCol); } else if (op == 15){ //cmp if(a