We planned to upgrade GitLab and Mattermost to the latest version this Friday morning (early). You may experience some downtime!

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

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
......
This diff is collapsed.
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.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.
This diff is collapsed.
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