Commit af429f49 authored by thomas@cs.ru.nl's avatar thomas@cs.ru.nl
Browse files

Added a very simple head model:

* the heap starts at a fixed position in memory and grows in the same direction as the stack (away from it)
* the next free address in the heap is stored in register R4(HP)
* there are four instructions to store/load to/from the heap (STH, STMH s, LDH o, and LDMH o s where o is an offset and s a size), producing and consuming an address respectively
parent 608211b4
This diff is collapsed.
This diff is collapsed.
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Enumeration; public class MachineState extends Model{ protected int stackBottom ; protected int stackGrowthDir ; protected int code ; protected int instrPC ; protected Instruction instr ; protected int nInlineOpnds ; protected int inlineOpnds[] ; protected Memory memory ; protected Registers registers ; protected MemoryUser memoryUser ; public boolean isHalted ; public MachineState( int initMemCapacity, Messenger m ) { memory = new Memory( initMemCapacity, m ) ; registers = new Registers( memory, m ) ; stackGrowthDir = 1 ; reset() ; } protected void fireStateChange( MachineStateEvent ev ) { for( Enumeration e = getListeners() ; e.hasMoreElements() ; ) { MachineStateListener l = (MachineStateListener)e.nextElement() ; l.stateChanged( ev ) ; } } public void addMachineStateListener( MachineStateListener l ) { addListener( l ) ; } public void removeMachineStateListener( MachineStateListener l ) { removeListener( l ) ; } public void reset( ) { memory.reset() ; registers.reset() ; resetToInitialState() ; } public void resetToInitialState() { registers.setPC( 0 ) ; stackBottom = memory.getUsedForCode() + 16 ; registers.setSP( stackBottom - stackGrowthDir ) ; registers.setMP( registers.getSP() ) ; isHalted = false ; } public int dir( int v ) { return v * stackGrowthDir ; } public void setCurrentInstr( int pc, int code, Instruction instr ) { this.code = code ; instrPC = pc ; this.instr = instr ; nInlineOpnds = instr.getNrInlineOpnds() ; if ( nInlineOpnds > 0 ) inlineOpnds = new int[ nInlineOpnds ] ; } public Memory getMemory() { return memory ; } public int getStackBottom() { return stackBottom ; } public Registers getRegisters() { return registers ; } public boolean stackIsEmpty() { return stackBottom == registers.getSP() ; } public int stackTop() { return registers.getRegInd( Registers.SP ) ; } public int getSR( ) { return stackIsEmpty() ? 0 : stackTop() ; } public String getSRAsString( ) { int sr = getSR() ; return "Z=" + ((sr & Instruction.ZZ) >> Instruction.SR_Z) + " C=" + ((sr & Instruction.CC) >> Instruction.SR_C) + " V=" + ((sr & Instruction.VV) >> Instruction.SR_V) + " N=" + ((sr & Instruction.NN) >> Instruction.SR_N) ; } class UndoStateModification implements Modification { private boolean wasHalted ; UndoStateModification( boolean h ) { wasHalted = h ; } public void modify() { isHalted = wasHalted ; } } public void setHalted() { isHalted = true ; fireStateChange( new MachineStateEvent( this, new UndoStateModification( false ) ) ) ; } public boolean isHalted() { return isHalted ; } public String toString() { return "state code=" + code + " instr-pc=" + instrPC + " n-inl=" + nInlineOpnds ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Enumeration; public class MachineState extends Model{ protected int stackBottom ; protected int stackGrowthDir ; private final int startAddressOfHeap; protected int code ; protected int instrPC ; protected Instruction instr ; protected int nInlineOpnds ; protected int inlineOpnds[] ; protected Memory memory ; protected Registers registers ; protected MemoryUser memoryUser ; public boolean isHalted ; public MachineState( int initMemCapacity, int startAddressOfHeap, Messenger m ) { memory = new Memory( initMemCapacity, m ) ; registers = new Registers( memory, m ) ; stackGrowthDir = 1 ; this.startAddressOfHeap = startAddressOfHeap; reset() ; } protected void fireStateChange( MachineStateEvent ev ) { for( Enumeration e = getListeners() ; e.hasMoreElements() ; ) { MachineStateListener l = (MachineStateListener)e.nextElement() ; l.stateChanged( ev ) ; } } public void addMachineStateListener( MachineStateListener l ) { addListener( l ) ; } public void removeMachineStateListener( MachineStateListener l ) { removeListener( l ) ; } public void reset( ) { memory.reset() ; registers.reset() ; resetToInitialState() ; } public void resetToInitialState() { registers.setPC( 0 ) ; stackBottom = memory.getUsedForCode() + 16 ; registers.setSP( stackBottom - stackGrowthDir ) ; registers.setMP( registers.getSP() ) ; registers.setHP(startAddressOfHeap); isHalted = false ; } public int dir( int v ) { return v * stackGrowthDir ; } public void setCurrentInstr( int pc, int code, Instruction instr ) { this.code = code ; instrPC = pc ; this.instr = instr ; nInlineOpnds = instr.getNrInlineOpnds() ; if ( nInlineOpnds > 0 ) inlineOpnds = new int[ nInlineOpnds ] ; } public Memory getMemory() { return memory ; } public int getStackBottom() { return stackBottom ; } public int getStartAddressOfHeap() { return startAddressOfHeap; } public Registers getRegisters() { return registers ; } public boolean stackIsEmpty() { return stackBottom == registers.getSP() ; } public int stackTop() { return registers.getRegInd( Registers.SP ) ; } public int getSR( ) { return stackIsEmpty() ? 0 : stackTop() ; } public String getSRAsString( ) { int sr = getSR() ; return "Z=" + ((sr & Instruction.ZZ) >> Instruction.SR_Z) + " C=" + ((sr & Instruction.CC) >> Instruction.SR_C) + " V=" + ((sr & Instruction.VV) >> Instruction.SR_V) + " N=" + ((sr & Instruction.NN) >> Instruction.SR_N) ; } class UndoStateModification implements Modification { private boolean wasHalted ; UndoStateModification( boolean h ) { wasHalted = h ; } public void modify() { isHalted = wasHalted ; } } public void setHalted() { isHalted = true ; fireStateChange( new MachineStateEvent( this, new UndoStateModification( false ) ) ) ; } public boolean isHalted() { return isHalted ; } public String toString() { return "state code=" + code + " instr-pc=" + instrPC + " n-inl=" + nInlineOpnds ; } }
\ No newline at end of file
......
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Hashtable; public class Memory extends AbstractMemoryCellModel implements MemoryCellModel{ private int cells[] ; private Hashtable<Integer,MemoryAnnotation> annotations ; private int nUsedForCode ; private Messenger messenger ; protected Memory( int initCapacity, Messenger m ) { messenger = m ; cells = new int[ initCapacity ] ; for ( int i = 0 ; i < cells.length ; i++ ) cells[ i ] = 0 ; reset() ; } public void reset() { nUsedForCode = 0 ; annotations = new Hashtable<Integer,MemoryAnnotation>() ; for ( int i = 0 ; i < cells.length ; i++ ) setAt( i, 0 ) ; } public int getCapacity() { return cells.length ; } public int getUsedForCode() { return nUsedForCode ; } private boolean checkWithinMemory( int addr ) { boolean res ; if ( res = ( addr < 0 || addr >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access location {0} outside {1}" , new Object[] { Utils.asHex(addr), ""+this } ) ) ; } return ! res ; } class UndoMemoryModification implements Modification { private int offset, value ; UndoMemoryModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setAt( offset, value ) ; } } public void setAt( int addr, int v ) { if ( checkWithinMemory( addr ) ) { int oldv = cells[ addr ] ; cells[ addr ] = v ; fireCellChange( this, addr, oldv, new UndoMemoryModification( addr, oldv ) ) ; } } public void setAt( int addr, String v ) { setAt( addr, Utils.fromHex( v ) ) ; } public int getAt( int addr ) { int res = 0 ; if ( checkWithinMemory( addr ) ) { res = cells[ addr ] ; } return res ; } public int[] getAt( int addr, int len ) { int res[] = new int[ len ] ; for ( int i = 0 ; i < len && checkWithinMemory( addr + i ) ; i++ ) { res[ i ] = cells[ addr + i ] ; } return res ; } public String getAsHexAt( int addr ) { return Utils.asHex( getAt( addr ) ) ; } class UndoAnnotationModification implements Modification { private int offset ; private MemoryAnnotation value ; UndoAnnotationModification( int o, MemoryAnnotation v ) { offset = o ; value = v ; } public void modify() { setAnnotationAt( offset, value ) ; } } public void setAnnotationAt( int addr, MemoryAnnotation v ) { if ( checkWithinMemory( addr ) ) { //System.out.println( "mem set annote at " + addr + "=" + v ) ; MemoryAnnotation oldv ; if ( v != null ) oldv = annotations.put( addr, v ) ; else oldv = annotations.remove( addr ) ; if ( oldv != v ) fireCellChange( this, addr, oldv, new UndoAnnotationModification( addr, oldv ) ) ; } } public MemoryAnnotation getAnnotationAt( int addr ) { MemoryAnnotation res ; res = annotations.get( addr ) ; //System.out.println( "mem got annote at " + addr + "=" + res ) ; return res ; } public void ensureCapacity( int nCells ) { if ( nCells > (cells.length - nUsedForCode) ) { int newCells[] = new int[ 2 * (nCells + 10) + cells.length ] ; System.arraycopy( cells, 0, newCells, 0, cells.length ) ; cells = newCells ; } } public void deleteAt( int pos, int n ) { for( int i = pos + n ; i < nUsedForCode ; i++ ) cells[ i-n ] = cells[ i ] ; nUsedForCode -= n ; } public void reserveAt( int pos, int n ) { ensureCapacity( n ) ; for( int i = nUsedForCode - 1 ; i >= pos ; i-- ) { cells[ i+n ] = cells[ i ] ; } for( int i = 0 ; i < n ; i++ ) cells[ i+pos ] = 0 ; nUsedForCode += n ; } public void shiftAt( int pos, int n ) { if ( n > 0 ) reserveAt( pos, n ) ; else if ( n < 0 ) deleteAt( pos, -n ) ; } public void copyAt( int pos, int[] vals ) { for( int i = 0 ; i < vals.length ; i++ ) cells[ i+pos ] = vals[ i ] ; } public void insertAt( int pos, int[] vals ) { reserveAt( pos, vals.length ) ; copyAt( pos, vals ) ; } public String toString() { return "memory [0(" + Utils.asHex(nUsedForCode,false) + ").." + Utils.asHex(getCapacity()-1,false) + "]" ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Hashtable; public class Memory extends AbstractMemoryCellModel implements MemoryCellModel{ private int cells[] ; private Hashtable<Integer,MemoryAnnotation> annotations ; private int nUsedForCode ; private Messenger messenger ; protected Memory( int initCapacity, Messenger m ) { cells = new int[ initCapacity ] ; for ( int i = 0 ; i < cells.length ; i++ ) cells[ i ] = 0 ; messenger = m ; reset() ; } public void reset() { nUsedForCode = 0 ; annotations = new Hashtable<Integer,MemoryAnnotation>() ; for ( int i = 0 ; i < cells.length ; i++ ) setAt( i, 0 ) ; } public int getCapacity() { return cells.length ; } public int getUsedForCode() { return nUsedForCode ; } private boolean checkWithinMemory( int addr ) { boolean res ; if ( res = ( addr < 0 || addr >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access location {0} outside {1}" , new Object[] { Utils.asHex(addr), ""+this } ) ) ; } return ! res ; } class UndoMemoryModification implements Modification { private int offset, value ; UndoMemoryModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setAt( offset, value ) ; } } public void setAt( int addr, int v ) { if ( checkWithinMemory( addr ) ) { int oldv = cells[ addr ] ; cells[ addr ] = v ; fireCellChange( this, addr, oldv, new UndoMemoryModification( addr, oldv ) ) ; } } public void setAt( int addr, String v ) { setAt( addr, Utils.fromHex( v ) ) ; } public int getAt( int addr ) { int res = 0 ; if ( checkWithinMemory( addr ) ) { res = cells[ addr ] ; } return res ; } public int[] getAt( int addr, int len ) { int res[] = new int[ len ] ; for ( int i = 0 ; i < len && checkWithinMemory( addr + i ) ; i++ ) { res[ i ] = cells[ addr + i ] ; } return res ; } public String getAsHexAt( int addr ) { return Utils.asHex( getAt( addr ) ) ; } class UndoAnnotationModification implements Modification { private int offset ; private MemoryAnnotation value ; UndoAnnotationModification( int o, MemoryAnnotation v ) { offset = o ; value = v ; } public void modify() { setAnnotationAt( offset, value ) ; } } public void setAnnotationAt( int addr, MemoryAnnotation v ) { if ( checkWithinMemory( addr ) ) { //System.out.println( "mem set annote at " + addr + "=" + v ) ; MemoryAnnotation oldv ; if ( v != null ) oldv = annotations.put( addr, v ) ; else oldv = annotations.remove( addr ) ; if ( oldv != v ) fireCellChange( this, addr, oldv, new UndoAnnotationModification( addr, oldv ) ) ; } } public MemoryAnnotation getAnnotationAt( int addr ) { MemoryAnnotation res ; res = annotations.get( addr ) ; //System.out.println( "mem got annote at " + addr + "=" + res ) ; return res ; } public void ensureCapacity( int nCells ) { if ( nCells > (cells.length - nUsedForCode) ) { int newCells[] = new int[ 2 * (nCells + 10) + cells.length ] ; System.arraycopy( cells, 0, newCells, 0, cells.length ) ; cells = newCells ; } } public void deleteAt( int pos, int n ) { for( int i = pos + n ; i < nUsedForCode ; i++ ) cells[ i-n ] = cells[ i ] ; nUsedForCode -= n ; } public void reserveAt( int pos, int n ) { // Disable the automated expansion of the memory since the start address of the heap is fixed // in MachineState.java and because the references to cells in the heap cannot change. //ensureCapacity( n ) ; for( int i = nUsedForCode - 1 ; i >= pos ; i-- ) { cells[ i+n ] = cells[ i ] ; } for( int i = 0 ; i < n ; i++ ) cells[ i+pos ] = 0 ; nUsedForCode += n ; } public void shiftAt( int pos, int n ) { if ( n > 0 ) reserveAt( pos, n ) ; else if ( n < 0 ) deleteAt( pos, -n ) ; } public void copyAt( int pos, int[] vals ) { for( int i = 0 ; i < vals.length ; i++ ) cells[ i+pos ] = vals[ i ] ; } public void insertAt( int pos, int[] vals ) { reserveAt( pos, vals.length ) ; copyAt( pos, vals ) ; } public String toString() { return "memory [0(" + Utils.asHex(nUsedForCode,false) + ").." + Utils.asHex(getCapacity()-1,false) + "]" ; } }
\ No newline at end of file
......
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Enumeration;import java.util.Vector; public class Registers extends AbstractMemoryCellModel implements MemoryCellModel{ public static final int PC = 0 ; public static final int SP = 1 ; public static final int MP = 2 ; //public static final int RR = 3 ; private static final int nrRegs = 8 ; private static final String[] regNames = { "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7" } ; private static final String[] regNamesAlias = { "PC", "SP", "MP", "RR" } ; //private MemoryCell cells[] ; private int cells[] ; private Memory memory ; private Messenger messenger ; protected Registers( Memory m, Messenger msgr ) { messenger = msgr ; cells = new int[ nrRegs ] ; for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ] = new MemoryCell() ; cells[ i ] = 0 ; memory = m ; reset() ; } public void reset() { for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ].setValue( 0 ) ; cells[ i ] = 0 ; } public static int getNrRegs( ) { return nrRegs ; } /* private static String[] getRegNames( ) { return regNames ; } */ public static String getRegName( int r ) { return (r < regNames.length && r >= 0) ? regNames[ r ] : ("R"+r) ; } public static String getRegOrAliasName( int r ) { return r < regNamesAlias.length ? regNamesAlias[r] : getRegName( r ) ; } public static String getRegNAliasName( int r ) { return getRegName(r) + ( r < regNamesAlias.length ? ("(" + regNamesAlias[r] + ")") : "" ) ; } public static Enumeration<String> getRegNAliasNames( ) { Vector<String> v = new Vector<String>() ; Utils.addAllTo( v, Utils.asVector( regNames ) ) ; Utils.addAllTo( v, Utils.asVector( regNamesAlias ) ) ; return v.elements() ; } public static int findRegOfName( String nm ) // should be in some kinda environment { for ( int i = 0 ; i < regNames.length ; i++ ) { if ( regNames[i].equals( nm ) ) return i ; } for ( int i = 0 ; i < regNamesAlias.length ; i++ ) { if ( regNamesAlias[i].equals( nm ) ) return i ; } return -1 ; } private boolean checkWithinMemory( int r ) { boolean res ; if ( res = ( r < 0 || r >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access nonexisting register {0,number,integer}" , new Object[] { new Integer(r) } ) ) ; } return ! res ; } public int getReg( int r ) { int res = 0 ; if ( checkWithinMemory( r ) ) { res = cells[ r ] ; } return res ; } public int getRegDispl( int r, int d ) { return getReg( r ) + d ; } public int getRegDisplInd( int r, int d ) { return memory.getAt( getRegDispl( r, d ) ) ; } public int getRegInd( int r ) { return getRegDisplInd( r, 0 ) ; } class UndoRegistersModification implements Modification { private int offset, value ; UndoRegistersModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setReg( offset, value ) ; } } public void setReg( int r, int v ) { if ( checkWithinMemory( r ) ) { int oldv = cells[ r ] ; cells[ r ] = v ; fireCellChange( this, r, oldv, new UndoRegistersModification( r, oldv ) ) ; } } public void setRegDisplInd( int r, int d, int v ) { memory.setAt( getRegDispl( r, d ), v ) ; } public void setRegInd( int r, int v ) { setRegDisplInd( r, 0, v ) ; } public int swapReg( int r, int v ) { int oldVal = getReg( r ) ; setReg( r, v ) ; return oldVal ; } public void adjustReg( int r, int v ) { setReg( r, getReg( r ) + v ) ; } public void setPC( int v ) { setReg( PC, v ) ; } public int getPC( ) { return getReg( PC ) ; } public void adjustPC( int v ) { setPC( getPC() + v ) ; } public void setSP( int v ) { setReg( SP, v ) ; } public int getSP( ) { return getReg( SP ) ; } public void adjustSP( int v ) { setSP( getSP() + v ) ; } public void setMP( int v ) { setReg( MP, v ) ; } public int getMP( ) { return getReg( MP ) ; } public void adjustMP( int v ) { setMP( getMP() + v ) ; } public int getTOS() { return getRegInd( SP ) ; } public String toString() { return "Register PC=" + getReg( PC ) + " SP=" + getReg( SP ) + " MP=" + getReg( MP ) ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.Enumeration;import java.util.Vector; public class Registers extends AbstractMemoryCellModel implements MemoryCellModel{ public static final int PC = 0 ; public static final int SP = 1 ; public static final int MP = 2 ; //public static final int RR = 3 ; public static final int HP = 4 ; private static final int nrRegs = 8 ; private static final String[] regNames = { "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7" } ; private static final String[] regNamesAlias = { "PC", "SP", "MP", "RR", "HP" } ; //private MemoryCell cells[] ; private int cells[] ; private Memory memory ; private Messenger messenger ; protected Registers( Memory m, Messenger msgr ) { messenger = msgr ; cells = new int[ nrRegs ] ; for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ] = new MemoryCell() ; cells[ i ] = 0 ; memory = m ; reset() ; } public void reset() { for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ].setValue( 0 ) ; cells[ i ] = 0 ; } public static int getNrRegs( ) { return nrRegs ; } /* private static String[] getRegNames( ) { return regNames ; } */ public static String getRegName( int r ) { return (r < regNames.length && r >= 0) ? regNames[ r ] : ("R"+r) ; } public static String getRegOrAliasName( int r ) { return r < regNamesAlias.length ? regNamesAlias[r] : getRegName( r ) ; } public static String getRegNAliasName( int r ) { return getRegName(r) + ( r < regNamesAlias.length ? ("(" + regNamesAlias[r] + ")") : "" ) ; } public static Enumeration<String> getRegNAliasNames( ) { Vector<String> v = new Vector<String>() ; Utils.addAllTo( v, Utils.asVector( regNames ) ) ; Utils.addAllTo( v, Utils.asVector( regNamesAlias ) ) ; return v.elements() ; } public static int findRegOfName( String nm ) // should be in some kinda environment { for ( int i = 0 ; i < regNames.length ; i++ ) { if ( regNames[i].equals( nm ) ) return i ; } for ( int i = 0 ; i < regNamesAlias.length ; i++ ) { if ( regNamesAlias[i].equals( nm ) ) return i ; } return -1 ; } private boolean checkWithinMemory( int r ) { boolean res ; if ( res = ( r < 0 || r >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access nonexisting register {0,number,integer}" , new Object[] { new Integer(r) } ) ) ; } return ! res ; } public int getReg( int r ) { int res = 0 ; if ( checkWithinMemory( r ) ) { res = cells[ r ] ; } return res ; } public int getRegDispl( int r, int d ) { return getReg( r ) + d ; } public int getRegDisplInd( int r, int d ) { return memory.getAt( getRegDispl( r, d ) ) ; } public int getRegInd( int r ) { return getRegDisplInd( r, 0 ) ; } class UndoRegistersModification implements Modification { private int offset, value ; UndoRegistersModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setReg( offset, value ) ; } } public void setReg( int r, int v ) { if ( checkWithinMemory( r ) ) { int oldv = cells[ r ] ; cells[ r ] = v ; fireCellChange( this, r, oldv, new UndoRegistersModification( r, oldv ) ) ; } } public void setRegDisplInd( int r, int d, int v ) { memory.setAt( getRegDispl( r, d ), v ) ; } public void setRegInd( int r, int v ) { setRegDisplInd( r, 0, v ) ; } public int swapReg( int r, int v ) { int oldVal = getReg( r ) ; setReg( r, v ) ; return oldVal ; } public void adjustReg( int r, int v ) { setReg( r, getReg( r ) + v ) ; } public void setPC( int v ) { setReg( PC, v ) ; } public int getPC( ) { return getReg( PC ) ; } public void adjustPC( int v ) { setPC( getPC() + v ) ; } public void setSP( int v ) { setReg( SP, v ) ; } public int getSP( ) { return getReg( SP ) ; } public void adjustSP( int v ) { setSP( getSP() + v ) ; } public void setMP( int v ) { setReg( MP, v ) ; } public int getMP( ) { return getReg( MP ) ; } public void adjustMP( int v ) { setMP( getMP() + v ) ; } public void setHP(int v) { setReg (HP, v); } public int getHP() { return getReg(HP); } public void adjustHP(int v) { setHP(getHP() + v); } public int getTOS() { return getRegInd( SP ) ; } public String toString() { return "Register PC=" + getReg( PC ) + " SP=" + getReg( SP ) + " MP=" + getReg( MP ) ; } }
\ No newline at end of file
......
package nl.uu.cs.ssmui; import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel; import nl.uu.cs.ssm.ColoredText;import nl.uu.cs.ssm.MachineState;import nl.uu.cs.ssm.Memory;import nl.uu.cs.ssm.MemoryAnnotation;import nl.uu.cs.ssm.MemoryCellEvent;import nl.uu.cs.ssm.MemoryCellListener;import nl.uu.cs.ssm.Utils; public class HeapTableModel extends AbstractTableModel implements MemoryCellListener { private static final long serialVersionUID = 1L; private static final int C_ADDRESS = 0; private static final int C_VALUE = 1; protected static final int C_ANNOTE = 2; private static final String[] columnNames = {"Address", "Value", "Structure"}; private MachineState machineState ; private Memory memory ; public HeapTableModel(MachineState machineState) { this.machineState = machineState; reset(); } public void reset() { if (memory != null) { memory.removeMemoryCellListener(this); } memory = machineState.getMemory(); memory.addMemoryCellListener(this); fireTableChanged(new TableModelEvent(this)); } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return machineState.getRegisters().getHP() - machineState.getStartAddressOfHeap(); } public boolean isCellEditable(int row, int column) { return column == C_VALUE ; } private int rowToMemLoc(int row) { return row + machineState.getStartAddressOfHeap(); } public Object getValueAt(int row, int column) { Object res = "" ; int memLoc = rowToMemLoc(row); switch(column) { case C_ADDRESS : res = Utils.asHex(memLoc); break; case C_VALUE : res = Utils.asHex(memory.getAt(memLoc)) ; break; case C_ANNOTE : MemoryAnnotation ann = memory.getAnnotationAt(memLoc); res = ann == null ? ColoredText.blankDefault : ann; break; } return res; } public void setValueAt(Object aValue, int row, int column) { if (column == C_VALUE) { String strValue = null; if (aValue instanceof String) strValue = (String)aValue; else strValue = aValue.toString(); memory.setAt(rowToMemLoc(row), strValue); } } public String getColumnName(int column) { return columnNames[column] ; } public Class getColumnClass(int column) { if (column == C_ANNOTE) return ColoredText.class; else return SSMRunner.tableModelColumnClass; } public void cellChanged(MemoryCellEvent e) { fireTableDataChanged(); }}
\ No newline at end of file
This diff is collapsed.
package nl.uu.cs.ssmui; import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel; import nl.uu.cs.ssm.ColoredText;import nl.uu.cs.ssm.MachineState;import nl.uu.cs.ssm.Memory;import nl.uu.cs.ssm.MemoryAnnotation;import nl.uu.cs.ssm.MemoryCellEvent;import nl.uu.cs.ssm.MemoryCellListener;import nl.uu.cs.ssm.Registers;import nl.uu.cs.ssm.Utils; public class StackTableModel extends AbstractTableModel implements MemoryCellListener{ private static final long serialVersionUID = 1L ; private static final int C_ADDRESS = 0 ; private static final int C_VALUE = 1 ; protected static final int C_REGPTRS = 2 ; public static final int C_ANNOTE = 3 ; private static final String[] columnNames = { "Address", "Value", "RegPtrs", "Annote" } ; private MachineState machineState ; private Memory memory ; private Registers registers ; private int beforeMinAccessedSP ; private int maxAccessedSP ; private boolean isSPChanged = false ; public void beforeReset() { } public void reset() { if ( memory != null ) memory.removeMemoryCellListener( this ) ; if ( registers != null ) registers.removeMemoryCellListener( this ) ; memory = machineState.getMemory() ; registers = machineState.getRegisters() ; memory.addMemoryCellListener( this ) ; registers.addMemoryCellListener( this ) ; beforeMinAccessedSP = maxAccessedSP = registers.getReg( Registers.SP ) ; fireTableChanged( new TableModelEvent( this ) ) ; //fireTableStructureChanged() ; } public StackTableModel( MachineState mst ) { machineState = mst ; reset() ; } protected boolean isSPChanged() { return isSPChanged ; } public int getColumnCount() { return columnNames.length ; } public int getRowCount() { return machineState.dir( maxAccessedSP - beforeMinAccessedSP ) ; //return memory.getCapacity() ; } private int rowToMemLoc( int row ) { return row + (beforeMinAccessedSP + 1) ; //return getRowCount() - row - 1 ; } protected int memLocToRow( int loc ) { return loc - (beforeMinAccessedSP + 1) ; //return rowToMemLoc( loc ) ; } public boolean isCellEditable( int row, int column ) { return column == C_VALUE ; } private boolean isMemLocWithinStackRange( int memLoc ) { return memLoc <= maxAccessedSP && memLoc > beforeMinAccessedSP ; } public Object getValueAt( int row, int column ) { Object res = "" ; int memLoc = rowToMemLoc( row ) ; switch( column ) { case C_ADDRESS : res = Utils.asHex( memLoc ) ; break ; case C_VALUE : res = Utils.asHex( memory.getAt( memLoc ) ) ; break ; case C_REGPTRS : { String pc, sp, mp ; pc = sp = mp = "" ; if ( memLoc == registers.getReg( Registers.PC ) ) pc = " PC" ; if ( memLoc == registers.getReg( Registers.SP ) ) sp = " SP" ; if ( memLoc == registers.getReg( Registers.MP ) ) mp = " MP" ; res = pc + sp + mp ; } break ; case C_ANNOTE : MemoryAnnotation ann = memory.getAnnotationAt( memLoc ) ; //System.out.println( "annote stack at " + memLoc + "=" + ann ) ; res = ann == null ? ColoredText.blankDefault : ann ; break ; } return res ; } public void setValueAt( Object aValue, int row, int column ) { if ( column == C_VALUE ) { String strValue = null ; if ( aValue instanceof String ) strValue = (String)aValue ; else strValue = aValue.toString() ; memory.setAt( rowToMemLoc( row ), strValue ) ; } } public String getColumnName( int column ) { return columnNames[ column ] ; } public Class getColumnClass( int column ) { if ( column == C_ANNOTE ) return ColoredText.class ; else return SSMRunner.tableModelColumnClass ; } public void cellChanged( MemoryCellEvent e ) { Object src = e.getSource() ; int v ; if ( src == memory ) { int row = memLocToRow( e.cellIndex ) ; if ( isMemLocWithinStackRange( e.cellIndex ) ) fireTableRowsUpdated( row, row ) ; } else if ( src == registers && e.event == MemoryCellEvent.CELL ) { int loc = registers.getReg( e.cellIndex ) ; if ( e.cellIndex == Registers.SP && maxAccessedSP < loc ) { int oldSPRow = rowToMemLoc( maxAccessedSP ) ; int newSPRow = rowToMemLoc( loc ) ; maxAccessedSP = loc ; isSPChanged = true ; fireTableRowsInserted( oldSPRow+1, newSPRow ) ; } else isSPChanged = false ; if ( isMemLocWithinStackRange( v = e.getOldCellValue() ) ) { int oldrow = memLocToRow( v ) ; fireTableCellUpdated( oldrow, C_REGPTRS ) ; } if ( isMemLocWithinStackRange( loc ) ) { int row = memLocToRow( loc ) ; fireTableCellUpdated( row, C_REGPTRS ) ; } } } }
\ No newline at end of file
package nl.uu.cs.ssmui; import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel; import nl.uu.cs.ssm.ColoredText;import nl.uu.cs.ssm.MachineState;import nl.uu.cs.ssm.Memory;import nl.uu.cs.ssm.MemoryAnnotation;import nl.uu.cs.ssm.MemoryCellEvent;import nl.uu.cs.ssm.MemoryCellListener;import nl.uu.cs.ssm.Registers;import nl.uu.cs.ssm.Utils; public class StackTableModel extends AbstractTableModel implements MemoryCellListener{ private static final long serialVersionUID = 1L ; private static final int C_ADDRESS = 0 ; private static final int C_VALUE = 1 ; protected static final int C_REGPTRS = 2 ; public static final int C_ANNOTE = 3 ; private static final String[] columnNames = { "Address", "Value", "RegPtrs", "Annote" } ; private MachineState machineState ; private Memory memory ; private Registers registers ; private int startAddressOfStack ; private int maxAccessedSP ; private boolean isSPChanged = false ; public void reset() { if ( memory != null ) memory.removeMemoryCellListener( this ) ; if ( registers != null ) registers.removeMemoryCellListener( this ) ; memory = machineState.getMemory() ; registers = machineState.getRegisters() ; memory.addMemoryCellListener( this ) ; registers.addMemoryCellListener( this ) ; startAddressOfStack = maxAccessedSP = registers.getReg( Registers.SP ) ; fireTableChanged( new TableModelEvent( this ) ) ; } public StackTableModel( MachineState mst ) { machineState = mst ; reset() ; } protected boolean isSPChanged() { return isSPChanged ; } public int getColumnCount() { return columnNames.length ; } public int getRowCount() { return maxAccessedSP - startAddressOfStack; } private int rowToMemLoc( int row ) { return row + (startAddressOfStack + 1) ; } protected int memLocToRow( int loc ) { return loc - (startAddressOfStack + 1) ; } public boolean isCellEditable( int row, int column ) { return column == C_VALUE ; } private boolean isMemLocWithinStackRange( int memLoc ) { return memLoc > startAddressOfStack && memLoc <= maxAccessedSP; } public Object getValueAt( int row, int column ) { Object res = "" ; int memLoc = rowToMemLoc( row ) ; switch( column ) { case C_ADDRESS : res = Utils.asHex( memLoc ) ; break ; case C_VALUE : res = Utils.asHex( memory.getAt( memLoc ) ) ; break ; case C_REGPTRS : { String pc, sp, mp ; pc = sp = mp = "" ; if ( memLoc == registers.getReg( Registers.PC ) ) pc = " PC" ; if ( memLoc == registers.getReg( Registers.SP ) ) sp = " SP" ; if ( memLoc == registers.getReg( Registers.MP ) ) mp = " MP" ; res = pc + sp + mp ; } break ; case C_ANNOTE : MemoryAnnotation ann = memory.getAnnotationAt( memLoc ) ; res = ann == null ? ColoredText.blankDefault : ann ; break ; } return res ; } public void setValueAt( Object aValue, int row, int column ) { if ( column == C_VALUE ) { String strValue = null ; if ( aValue instanceof String ) strValue = (String)aValue ; else strValue = aValue.toString() ; memory.setAt( rowToMemLoc( row ), strValue ) ; } } public String getColumnName( int column ) { return columnNames[ column ] ; } public Class getColumnClass( int column ) { if ( column == C_ANNOTE ) return ColoredText.class ; else return SSMRunner.tableModelColumnClass ; } public void cellChanged( MemoryCellEvent e ) { //fireTableDataChanged(); Object src = e.getSource() ; int v ; if ( src == memory ) { int row = memLocToRow( e.cellIndex ) ; if ( isMemLocWithinStackRange( e.cellIndex ) ) fireTableRowsUpdated( row, row ) ; } else if ( src == registers && e.event == MemoryCellEvent.CELL ) { int loc = registers.getReg( e.cellIndex ) ; if ( e.cellIndex == Registers.SP && maxAccessedSP < loc ) { int oldSPRow = rowToMemLoc( maxAccessedSP ) ; int newSPRow = rowToMemLoc( loc ) ; maxAccessedSP = loc ; isSPChanged = true ; fireTableRowsInserted( oldSPRow+1, newSPRow ) ; } else isSPChanged = false ; if ( isMemLocWithinStackRange( v = e.getOldCellValue() ) ) { int oldrow = memLocToRow( v ) ; fireTableCellUpdated( oldrow, C_REGPTRS ) ; } if ( isMemLocWithinStackRange( loc ) ) { int row = memLocToRow( loc ) ; fireTableCellUpdated( row, C_REGPTRS ) ; } } } }
\ No newline at end of file
......
package nl.uu.cs.ssmui; import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel; import nl.uu.cs.ssm.MachineState;import nl.uu.cs.ssm.MemoryCellEvent;import nl.uu.cs.ssm.MemoryCellListener;import nl.uu.cs.ssm.Registers;import nl.uu.cs.ssm.Utils; public class StatusTableModel extends AbstractTableModel implements MemoryCellListener{ private static final long serialVersionUID = 1L ; private static final int C_PC = 0 ; private static final int C_SP = 1 ; private static final int C_MP = 2 ; private static final int C_R3 = 3 ; private static final int C_R4 = 4 ; private static final int C_R5 = 5 ; private static final int C_R6 = 6 ; private static final int C_R7 = 7 ; private static final int C_SR = 8 ; //private static final String[] columnNames = { "PC", "SP", "MP", "RR", "SR" } ; private MachineState machineState ; private Registers registers ; public void beforeReset() { } public void reset() { if ( registers != null ) registers.removeMemoryCellListener( this ) ; /* if ( memory != null ) memory.removeMemoryCellListener( this ) ; */ registers = machineState.getRegisters() ; registers.addMemoryCellListener( this ) ; /* memory = machineState.getMemory() ; memory.addMemoryCellListener( this ) ; */ fireTableChanged( new TableModelEvent( this ) ) ; //fireTableStructureChanged() ; } public StatusTableModel( MachineState mst ) { machineState = mst ; reset() ; } public int getColumnCount() { return Registers.getNrRegs() + 1 ; } public int getRowCount() { return 1 ; } public boolean isCellEditable( int row, int column ) { return column != C_SR ; } public Object getValueAt( int row, int column ) { Object res ; if ( column == C_SR ) { res = machineState.getSRAsString() ; } else { int reg = column ; res = Utils.asHex( registers.getReg( reg ) ) ; } return res ; } public void setValueAt( Object aValue, int row, int column ) { String strValue ; if ( aValue instanceof String ) strValue = (String)aValue ; else strValue = aValue.toString() ; if ( column != C_SR ) { int reg = column ; registers.setReg( reg, Utils.fromHex( strValue ) ) ; } } public String getColumnName( int column ) { return column == C_SR ? "SR" : Registers.getRegNAliasName( column ) ; } public Class getColumnClass( int column ) { return SSMRunner.tableModelColumnClass ; } public void cellChanged( MemoryCellEvent e ) { fireTableCellUpdated( 0, e.cellIndex ) ; } }
\ No newline at end of file
package nl.uu.cs.ssmui; import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel; import nl.uu.cs.ssm.MachineState;import nl.uu.cs.ssm.MemoryCellEvent;import nl.uu.cs.ssm.MemoryCellListener;import nl.uu.cs.ssm.Registers;import nl.uu.cs.ssm.Utils; public class StatusTableModel extends AbstractTableModel implements MemoryCellListener{ private static final long serialVersionUID = 1L ; private static final int C_PC = 0 ; private static final int C_SP = 1 ; private static final int C_MP = 2 ; private static final int C_R3 = 3 ; private static final int C_R4 = 4 ; private static final int C_R5 = 5 ; private static final int C_R6 = 6 ; private static final int C_R7 = 7 ; private static final int C_SR = 8 ; //private static final String[] columnNames = { "PC", "SP", "MP", "RR", "SR" } ; private MachineState machineState ; private Registers registers ; public void reset() { if ( registers != null ) registers.removeMemoryCellListener( this ) ; /* if ( memory != null ) memory.removeMemoryCellListener( this ) ; */ registers = machineState.getRegisters() ; registers.addMemoryCellListener( this ) ; /* memory = machineState.getMemory() ; memory.addMemoryCellListener( this ) ; */ fireTableChanged( new TableModelEvent( this ) ) ; //fireTableStructureChanged() ; } public StatusTableModel( MachineState mst ) { machineState = mst ; reset() ; } public int getColumnCount() { return Registers.getNrRegs() + 1 ; } public int getRowCount() { return 1 ; } public boolean isCellEditable( int row, int column ) { return column != C_SR ; } public Object getValueAt( int row, int column ) { Object res ; if ( column == C_SR ) { res = machineState.getSRAsString() ; } else { int reg = column ; res = Utils.asHex( registers.getReg( reg ) ) ; } return res ; } public void setValueAt( Object aValue, int row, int column ) { String strValue ; if ( aValue instanceof String ) strValue = (String)aValue ; else strValue = aValue.toString() ; if ( column != C_SR ) { int reg = column ; registers.setReg( reg, Utils.fromHex( strValue ) ) ; } } public String getColumnName( int column ) { return column == C_SR ? "SR" : Registers.getRegNAliasName( column ) ; } public Class getColumnClass( int column ) { return SSMRunner.tableModelColumnClass ; } public void cellChanged( MemoryCellEvent e ) { fireTableCellUpdated( 0, e.cellIndex ) ; } }
\ No newline at end of file
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment