Commit efc5e2b6 authored by atzedijkstra@gmail.com's avatar atzedijkstra@gmail.com
Browse files

initial

parents
BRA main Swap2Ints: LINK 1 LDL -3 LDA 0 STL 1 LDL -2 LDA 0 LDL -3 STA 0 LDL -2 LDL 1 STA 0 UNLINK STS -2 AJS -1 RET main: LINK 2 LDC 4 STL 1 LDC 6 STL 2 LDLA 1 LDLA 2 BSR Swap2Ints UNLINK
\ No newline at end of file
BRA main f: LINK 2 LDL -3 LDL -2 ADD STL 1 LDL -3 LDL -2 MUL STL 2 LDL 2 LDL 1 SUB STR RR UNLINK STS -2 AJS -1 RET g: LINK 1 LDC 3 LDC 4 LDL -2 BSR f LDR RR MUL STL 1 UNLINK STS -1 RET main: LINK 0 LDC 5 BSR g UNLINK
\ No newline at end of file
BRA main CpInts: LINK 0 CpBeg: LDL -2 LDC 0 EQ BRT CpEnd LDL -2 LDC 1 SUB STL -2 LDL -4 LDL -2 ADD LDA 0 LDL -3 LDL -2 ADD STA 0 BRA CpBeg CpEnd: UNLINK STS -3 AJS -2 RET main: LINK 12 LDLA 1 LDLA 6 LDC 4 BSR CpInts UNLINK
\ No newline at end of file
BRA Main DoIf: LINK 1 LDL -2 LDC 2 MOD LDC 0 ne brt Then LDC 0 STL 1 BRA IfEnd Then: LDC 1 STL 1 IfEnd: UNLINK STS -1 RET Main: LDC 10 BSR DoIf LDC 11 BSR DoIf
\ No newline at end of file
BRA main max3: LINK 0 LDL -3 LDL -2 le brt L1 LDL -3 STL -2 L1: LDL -4 LDL -2 le brt L2 LDL -4 STL -2 L2: LDL -2 STR RR UNLINK STS -3 AJS -2 RET main: LINK 0 LDC 4 LDC 5 MUL LDC 2 LDC 8 LDC 6 BSR max3 LDR RR ADD UNLINK
\ No newline at end of file
BRA main max3: LINK 0 LDL -3 LDL -2 GT BRF L1 LDL -3 STL -2 L1: LDL -4 LDL -2 GT BRF L2 LDL -4 STL -2 L2: LDL -2 STR RR UNLINK STS -3 AJS -2 RET main: LINK 0 LDC 4 LDC 5 MUL LDC 2 LDC 8 LDC 6 BSR max3 LDR RR ADD UNLINK
\ No newline at end of file
ldc 4 ldc 5 ldms -1 2 ldms -2 3 ldc 4 ldma 2 5 mul stml 1 4
\ No newline at end of file
<html><head><title>SSM Changes</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head> <body bgcolor="#FFFFFF"><h2>Changes in SSM</h2><h3></h3><h3>Version 1.3</h3><ul> <li>20020407. <ul> <li>Added backward stepping (undoing).</li> <li>The instructions in the code pane now also display their arguments, as mnemonic if possible.</li> <li>If code changes during execution a warning is printed.</li> </ul> </li></ul><h3></h3><h3>Version 1.2.3</h3><ul> <li>20020213. Fixed documentation errors.</li></ul><h3>Version 1.2.2</h3><ul> <li>20011210. Fixed some weird splitpane behavior and other GUI stuff. The tests now reflect the the instruction set.</li></ul><h3>Version 1.2.1</h3><ul> <li>20010330. Toolbar had disappeared, it now displays again (untested on WinXX JDK1.3)</li></ul><h3></h3><h3></h3><h3>Version 1.2</h3><ul> <li>20010131. Added blockmove instructions, deleted 'cmp' and related 'brcc' instructions. Changed definition of 'sta' (swapping of parameters on stack) to be consistent with 'stma'. The address is now on top.</li></ul><h3>Version 1.1.3</h3><ul> <li>20000928. Repaired documentation bug for 'jsr'.</li></ul><h3></h3><h3>Version 1.1.2</h3><ul> <li>20000914, Added 'brt' instruction. True is 0xFFFFFFFF (-1).</li></ul><h3>Version 1.1.1</h3><ul> <li>20000912, Fixed documentation errors.</li></ul><h3></h3><h3>Version 1.1</h3><ul> <li>20000911, Added instructions 'eq', 'ne', 'lt', 'le', 'gt', 'ge' and 'brf' for comparing purposes. Added 'ldrr' and 'swprr' for register manipulation.</li></ul><h3></h3><h3>Version 1.0.2</h3><ul> <li> 20000911, Fixed examples in help w.r.t. labels &amp; ':'.</li> <li> 20000911, Tables for instruction description fixed width.</li></ul><h3>Version 1.0.1</h3><ul> <li>20000910, Added Trap instruction.</li> <li>20000909, Changed name of instruction 'ld' to 'lds', 'st' to 'sts'. Added 'ldsa', 'ldaa'. All for better orthogonality of instructionset.</li> <li>20000909, Automatic scroll of codepane to position of PC.</li></ul></body></html>
\ No newline at end of file
src/logo.GIF

853 Bytes

/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; abstract class AbstractMemoryCellModel extends Model implements MemoryCellModel{ protected void fireCellChange( MemoryCellModel m, MemoryCellEvent ev ) { for( Enumeration e = getListeners() ; e.hasMoreElements() ; ) { MemoryCellListener l = (MemoryCellListener)e.nextElement() ; l.cellChanged( ev ) ; } } protected void fireCellChange( MemoryCellModel m, int cellIndex, int oldCellValue, Modification mdf ) { fireCellChange( m, new MemoryCellEvent( m, cellIndex, oldCellValue, mdf ) ) ; } protected void fireCellChange( MemoryCellModel m, int cellIndex, MemoryAnnotation oldCellValue, Modification mdf ) { fireCellChange( m, new MemoryCellEvent( m, cellIndex, oldCellValue, mdf ) ) ; } public void addMemoryCellListener( MemoryCellListener l ) { addListener( l ) ; } public void removeMemoryCellListener( MemoryCellListener l ) { removeListener( l ) ; } }
\ 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.* ;import java.awt.* ; public class ColoredText{ public static final ColoredText blankDefault = new ColoredText( "", Color.gray ) ; private String text ; private Color color ; public ColoredText( String t, Color c ) { text = t ; color = c ; } public String getText() { return text ; } public Color getColor() { return color ; } public String toString() { return "ColoredText " + text ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ /** * Configuration * */package nl.uu.cs.ssm ; import java.awt.event.KeyEvent ; public class Config{ public static final int[] keysLoad = { KeyEvent.VK_O } ; public static final int[] keysReload = { KeyEvent.VK_R } ; public static final int[] keysPause = { KeyEvent.VK_SPACE } ; public static final int[] keysFullForward = { KeyEvent.VK_DOWN } ; public static final int[] keysFullBackward = { KeyEvent.VK_UP } ; public static final int[] keysStep1Forward = { KeyEvent.VK_RIGHT, KeyEvent.VK_SPACE } ; public static final int[] keysStep1Backward = { KeyEvent.VK_LEFT, KeyEvent.VK_BACK_SPACE } ; public static final String extensionSSM = "ssm" ; public static final int allowedAutomaticMemoryIncrease = 100 ; public static final String version() { return "1.3.3" ; } public static final String versionDate() { return "2002 04 15" ; } public static final String author() { return "Atze Dijkstra, Utrecht University" ; } public static final String authorEmail() { return "atze@cs.uu.nl" ; } }
\ 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.* ;import java.io.* ; public abstract class HelpAccumulator{ //private StringBuffer accumulator ; protected Writer accumulator ; public HelpAccumulator( String fileName ) { try { //System.out.println( "help acc " + fileName ) ; if ( fileName == null ) accumulator = new StringWriter() ; else accumulator = new FileWriter( fileName ) ; } catch( IOException ex ) { ex.printStackTrace() ; accumulator = null ; } } protected boolean canWrite() { return accumulator != null ; } public abstract void beginHeadTitleBody( String t ) ; public abstract void endHeadTitleBody() ; public abstract void anchor( String a ) ; public abstract void a( String href, String title ) ; public abstract void anchorA( String href, String title ) ; public abstract void beginCentered() ; public abstract void endCentered() ; public abstract void beginPara() ; public abstract void endPara() ; public void para( String text ) { beginPara( ) ; append( text ) ; endPara() ; } public abstract void beginSectionTitle() ; public abstract void endSectionTitle() ; public void sectionTitle( String s ) { beginSectionTitle() ; append( s ) ; endSectionTitle() ; } public abstract void beginSubsectionTitle() ; public abstract void endSubsectionTitle() ; public void subsectionTitle( String s ) { beginSubsectionTitle() ; append( s ) ; endSubsectionTitle() ; } public abstract void beginTable( int nCols, int widthPerc, int[] colWidthWeight ) ; public abstract void endTable() ; public abstract void beginTableRow( ) ; public abstract void endTableRow() ; public abstract void beginTableData( ) ; public abstract void endTableData() ; public void tableRow( Object[] data ) // not recursive (yet) { beginTableRow() ; for ( int i = 0 ; i < data.length ; i++ ) { beginTableData() ; append( data[i].toString() ) ; endTableData() ; } endTableRow() ; } public void tableHeaderRow( Object[] data ) // not recursive (yet) { beginTableRow() ; for ( int i = 0 ; i < data.length ; i++ ) { beginTableData() ; bold( data[i].toString() ) ; endTableData() ; } endTableRow() ; } public abstract void beginBlockQuote( ) ; public abstract void endBlockQuote() ; public abstract void linebreak() ; public void append( String o ) { if ( ! canWrite() ) return ; try { accumulator.write( o ) ; } catch ( IOException ex ) { accumulator = null ; } } public void append( char o ) { append( "" + o ) ; } public void append( int o ) { append( "" + o ) ; } public void nl( ) { append( '\n' ) ; } //public abstract void font( String f, String s ) ; public abstract void bold( String s ) ; public void string( String s ) { append( "\"" ) ; append( s ) ; append( "\"" ) ; } //public abstract void define( String nm, String val ) ; public void close( ) { try { accumulator.close() ; } catch ( IOException ex ) { } } public String toString() { close() ; if ( accumulator instanceof StringWriter ) return accumulator.toString() ; else return "" ; } public abstract void mathEquationList( Enumeration e, char subscript ) ; public abstract void beginVerbatimList() ; public abstract void endVerbatimList() ; public abstract void verbatimLine( String s ) ; public void verbatimList( Enumeration e ) { if ( ! e.hasMoreElements() ) return ; beginVerbatimList() ; for ( ; e.hasMoreElements() ; ) { verbatimLine( (String)e.nextElement() ) ; } endVerbatimList() ; } public abstract void beginAttributeList( ) ; public abstract void endAttributeList( ) ; public abstract void attributeName( String s ) ; public abstract void beginAttributeValue( ) ; public abstract void endAttributeValue( ) ; public String reprAsHex( int v ) { return Utils.asHex( v, 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.* ;import java.io.* ; public class HelpHTMLAccumulator extends HelpAccumulator{ private boolean isOldHtml ; public HelpHTMLAccumulator( boolean old, String fileName ) { super( fileName ) ; isOldHtml = old ; } private void beginTag( String t, boolean end ) { append( '<' ) ; if ( end ) append( '/' ) ; append( t ) ; } private void endTag( ) { append( '>' ) ; } private void tag( String t, boolean end, String params ) { beginTag( t, end ) ; if ( params != null ) { append( " " ) ; append( params ) ; } endTag() ; } private void tag( String t, boolean end ) { tag( t, end, null ) ; } private void tagged( String t, String s ) { tag( t, false ) ; append( s ) ; tag( t, true ) ; } public void beginHeadTitleBody( String t ) { if ( ! isOldHtml ) { tag( "!doctype", false, "html public \"-//w3c//dtd html 4.0 transitional//en\"" ) ; tag( "html", false ) ; tag( "head", false ) ; tag( "meta", false, "http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"" ) ; tag( "meta", false, "name=\"GENERATOR\" content=\"Simple Stack Machine, " + Config.version() + ", " + Config.versionDate() + "\"" ) ; tag( "title", false ) ; append( t ) ; tag( "title", true ) ; tag( "head", true ) ; } tag( "body", false ) ; } public void endHeadTitleBody() { tag( "body", true ) ; if ( ! isOldHtml ) { tag( "html", true ) ; } } private void beginA( String name, String href ) { beginTag( "a", false ) ; if ( href != null ) define( "href", href ) ; if ( name != null ) define( "name", name ) ; endTag() ; } private void endA() { tag( "a", true ) ; } public void anchor( String a ) { beginA( a, null ) ; endA( ) ; } public void a( String href, String title ) { beginA( null, href ) ; append( title ) ; endA( ) ; } public void anchorA( String href, String title ) { a( "#" + href, title ) ; } private void beginPara( String align ) { beginTag( "p", false ) ; if ( align != null ) define( "align", align ) ; endTag() ; } public void beginPara( ) { beginPara( null ) ; } public void endPara() { tag( "p", true ) ; } public void beginCentered( ) { beginPara( "center" ) ; } public void endCentered() { endPara() ; } public void beginSectionTitle() { tag( "h2", false ) ; } public void endSectionTitle() { tag( "h2", true ) ; } public void beginSubsectionTitle() { tag( "h3", false ) ; } public void endSubsectionTitle() { tag( "h3", true ) ; } public void beginTable( int nCols, int widthPerc, int[] colWidthWeight ) { tag( "table", false, "BORDER COLS=" + nCols + " WIDTH=\"" + (widthPerc == 0 ? 100 : widthPerc) + "%\"" ) ; } public void endTable() { tag( "table", true ) ; } public void beginTableRow( ) { tag( "tr", false ) ; } public void endTableRow() { tag( "tr", true ) ; } public void beginTableData( ) { tag( "td", false ) ; } public void endTableData() { tag( "td", true ) ; } public void beginBlockQuote( ) { tag( "blockquote", false ) ; } public void endBlockQuote() { tag( "blockquote", true ) ; } public void linebreak() { tag( "br", false ) ; } private void font( String f, String s ) { tag( f, false ) ; append( s ) ; tag( f, true ) ; } public void bold( String s ) { font( "b", s ) ; } private void define( String nm, String val ) { append( " " ) ; append( nm ) ; append( "=" ) ; string( val ) ; } public void mathEquationList( Enumeration e, char subscript ) { for ( ; e.hasMoreElements() ; ) { append( ((String)e.nextElement()) ) ; if ( e.hasMoreElements() ) linebreak() ; } } public void beginAttributeList( ) { } public void endAttributeList( ) { } public void attributeName( String s ) { subsectionTitle( s ) ; } public void beginAttributeValue( ) { beginBlockQuote() ; } public void endAttributeValue( ) { endBlockQuote() ; } public void beginVerbatimList() { tag( "pre", false ) ; nl() ; } public void endVerbatimList() { tag( "pre", true ) ; nl() ; } public void verbatimLine( String s ) { append( s ) ; nl() ; } }
\ 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.* ;import java.io.* ; public class HelpLaTeXAccumulator extends HelpAccumulator{ public HelpLaTeXAccumulator( String fileName ) { super( fileName ) ; } private void beginEnv( String t, String param ) { macro( "begin", t ) ; if ( param != null ) bracket( param ) ; } private void beginEnv( String t ) { beginEnv( t, null ) ; } private void endEnv( String t ) { macro( "end", t ) ; } private void beginMath( ) { macro( "(" ) ; } private void endMath() { macro( ")" ) ; } private void beginParen( ) { append( '(' ) ; } private void endParen( ) { append( ')' ) ; } private void beginBracket( ) { append( '{' ) ; } private void endBracket( ) { append( '}' ) ; } private void bracket( String t ) { beginBracket( ) ; append( t ) ; endBracket() ; } private void beginMacro( String t ) { append( '\\' ) ; append( t ) ; beginBracket() ; } private void endMacro( ) { endBracket() ; } private void macro( String t, String a1 ) { append( '\\' ) ; append( t ) ; if ( a1 != null ) bracket( a1 ) ; else append( " " ) ; } private void macro( String t ) { macro( t, null ) ; } public void beginHeadTitleBody( String t ) { } public void endHeadTitleBody() { } public void anchor( String a ) { macro( "label", "ssmanchor:" + a ) ; macro( "Ix", a ) ; } public void a( String href, String title ) { append( title ) ; beginParen() ; macro( "Ref", href ) ; endParen() ; } public void anchorA( String href, String title ) { a( "ssmanchor:" + href, title ) ; } public void beginPara( ) { nl() ; nl() ; } public void endPara() { nl() ; nl() ; } public void beginCentered( ) { beginEnv( "center" ) ; } public void endCentered() { endEnv( "center" ) ; } public void beginSectionTitle() { beginMacro( "Sec" ) ; } public void endSectionTitle() { endMacro() ; } public void beginSubsectionTitle() { beginMacro( "Par" ) ; } public void endSubsectionTitle() { endMacro() ; } public void beginTable( int nCols, int widthPerc, int[] colWidthWeight ) { int perc = Math.min( 99, widthPerc ) ; StringBuffer colFmt = new StringBuffer() ; colFmt.append( "l" ) ; beginEnv( "tabular*", "0." + perc + "\\textwidth" ) ; if ( colWidthWeight == null ) { colFmt.append( "l" + Utils.repeat( "p{0." + (perc / nCols) + "\\textwidth}", nCols ) ) ; } else { int sum = 0 ; for ( int i = 0 ; i < colWidthWeight.length ; i++ ) sum += colWidthWeight[ i ] ; for ( int i = 0 ; i < colWidthWeight.length ; i++ ) colFmt.append( "p{0." + (perc * colWidthWeight[ i ] / sum) + "\\textwidth}" ) ; } bracket( colFmt.toString() ) ; } public void endTable() { endEnv( "tabular*" ) ; } public void beginTableRow( ) { } public void endTableRow() { linebreak() ; } public void beginTableData( ) { append( "& " ) ; macro( "begingroup" ) ; macro( "raggedright" ) ; } public void endTableData() { macro( "endgroup" ) ; } public void beginBlockQuote( ) { } public void endBlockQuote() { } public void linebreak() { append( "\\\\" ) ; nl() ; } public void bold( String s ) { beginBracket() ; macro( "bf" ) ; append( s ) ; endBracket() ; } private final static int MATH_NORMAL = 0 ; private final static int MATH_SUBSCRIPT = 1 ; private void mathMangle( String s, char subscript ) { StreamTokenizer t = new StreamTokenizer( new StringReader( s ) ) ; t.resetSyntax() ; t.wordChars( 'a', 'z' ) ; t.wordChars( 'A', 'Z' ) ; int tok ; int state = MATH_NORMAL ; beginMath() ; try { while ( ( tok = t.nextToken() ) != StreamTokenizer.TT_EOF ) { if ( tok == StreamTokenizer.TT_WORD ) { if ( state == MATH_SUBSCRIPT ) { bracket( t.sval ) ; state = MATH_NORMAL ; } else append( t.sval ) ; } else { if ( tok == subscript ) state = MATH_SUBSCRIPT ; if ( tok == '&' || tok == '%' ) append( '\\' ) ; append( (char)tok ) ; } } } catch ( IOException ex ) { append( ex.toString() ) ; } endMath() ; } public void mathEquationList( Enumeration e, char subscript ) { for ( ; e.hasMoreElements() ; ) { mathMangle( ((String)e.nextElement()), subscript ) ; if ( e.hasMoreElements() ) linebreak() ; } } public void beginAttributeList( ) { beginBracket() ; macro( "renewcommand", "\\arraystretch" ) ; bracket( "1.7" ) ; beginTable( 2, 90, new int[] {1,2} ) ; } public void endAttributeList( ) { endTable() ; endBracket() ; } public void attributeName( String s ) { beginTableRow() ; beginTableData() ; bold( s ) ; endTableData() ; } public void beginAttributeValue( ) { beginTableData() ; beginEnv( "minipage" ) ; append( "[t]" ) ; bracket( "0.6\\textwidth" ) ; } public void endAttributeValue( ) { endEnv( "minipage" ) ; endTableData() ; endTableRow() ; } public void beginVerbatimList() { beginEnv( "Verbatim" ) ; nl() ; } public void endVerbatimList() { endEnv( "Verbatim" ) ; nl() ; } public void verbatimLine( String s ) { append( s ) ; nl() ; } }
\ 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.* ; public interface HelpSupplier{ public Enumeration getTopics() ; public String getHelpSupplierName() ; public String getShortSummaryForTopic( String topic ) ; public void getHelpForTopic( String topic, HelpAccumulator acc ) ; }
\ 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.* ; public class Instruction implements HelpSupplier{ /** * Misc constants */ protected final static int UNKNOWN = -1 ; protected final static int nWordBits = 32 ; protected final static int CONST_TRUE = 0xFFFFFFFF; protected final static int CONST_FALSE = 0x00000000; /** * Condition codes */ protected final static int SR_Z = 0 ; protected final static int SR_C = 1 ; protected final static int SR_V = 2 ; protected final static int SR_N = 3 ; protected final static int ZZ = 1<<SR_Z ; protected final static int CC = 1<<SR_C ; protected final static int VV = 1<<SR_V ; protected final static int NN = 1<<SR_N ; private final static String ccNames[] = { "Z", "C", "V", "N" } ; /** * Categories of inline operands */ protected final static int OPND_INL_PC_REL = 0 ; protected final static int OPND_INL_REG = 1 ; protected final static int OPND_INL_AS_IS = 2 ; /** * Categories of instructions */ protected final static int CTG_OP = 0 ; protected final static int CTG_BINOP = 1 ; protected final static int CTG_UNOP = 2 ; protected final static int CTG_PRAGMA = 3 ; protected final static int CTG_TRAP = 4 ; protected final static int CTG_BRCC = 5 ; protected final static int CTG_META = 6 ; private final static String[] categNames = { "Normal instruction", "Binary operator", "Unary operator", "Pragma", "Trap", "Branch on condition", "Meta" } ; /** * Binary int instructions */ protected final static int BI_ADD = 1 ; protected final static int BI_AND = 2 ; //protected final static int BI_CMP = 3 ; protected final static int BI_DIV = 4 ; protected final static int BI_LSL = 5 ; protected final static int BI_LSR = 6 ; protected final static int BI_MOD = 7 ; protected final static int BI_MUL = 8 ; protected final static int BI_OR = 9 ; protected final static int BI_ROL = 10 ; protected final static int BI_ROR = 11 ; protected final static int BI_SUB = 12 ; protected final static int BI_XOR = 13 ; protected final static int BI_EQ = 14 ; protected final static int BI_NE = 15 ; protected final static int BI_LT = 16 ; protected final static int BI_GT = 17 ; protected final static int BI_LE = 18 ; protected final static int BI_GE = 19 ; /** * Unary int instructions */ protected final static int UI_NEG = 32 ; protected final static int UI_NOT = 33 ; /** * Branch instructions */ /* protected final static int BR_CC = 64 ; protected final static int BR_CS = 65 ; protected final static int BR_EQ = 66 ; protected final static int BR_GE = 67 ; protected final static int BR_GT = 68 ; protected final static int BR_HI = 69 ; protected final static int BR_LE = 70 ; protected final static int BR_LS = 71 ; protected final static int BR_LT = 72 ; protected final static int BR_MI = 73 ; protected final static int BR_NE = 74 ; protected final static int BR_PL = 75 ; */ /** * Other instructions */ protected final static int I_ADJS = 100 ; protected final static int I_BRA = 104 ; protected final static int I_BRF = 108 ; protected final static int I_BRT = 109 ; protected final static int I_BSR = 112 ; protected final static int I_HALT = 116 ; protected final static int I_JSR = 120 ; protected final static int I_LDA = 124 ; protected final static int I_LDMA = 126 ; protected final static int I_LDAA = 128 ; protected final static int I_LDC = 132 ; protected final static int I_LDL = 136 ; protected final static int I_LDML = 138 ; protected final static int I_LDLA = 140 ; protected final static int I_LDR = 144 ; protected final static int I_LDRR = 148 ; protected final static int I_LDS = 152 ; protected final static int I_LDMS = 154 ; protected final static int I_LDSA = 156 ; protected final static int I_LINK = 160 ; protected final static int I_NOP = 164 ; protected final static int I_RET = 168 ; protected final static int I_STA = 172 ; protected final static int I_STMA = 174 ; protected final static int I_STL = 176 ; protected final static int I_STML = 178 ; protected final static int I_STR = 180 ; protected final static int I_STS = 184 ; protected final static int I_STMS = 186 ; protected final static int I_SWP = 188 ; protected final static int I_SWPR = 192 ; protected final static int I_SWPRR = 196 ; protected final static int I_TRAP = 200 ; protected final static int I_UNLINK = 204 ; /** * Pragma's */ protected final static int PR_DB = 250 ; protected final static int PR_DC = 251 ; protected final static int PR_DS = 252 ; protected final static int PR_DW = 253 ; /** * Traps */ protected final static int TR_PR_INT = 0 ; /** * Metas */ protected final static int M_ANNOTE = 255 ; private static Hashtable codeToInstr ; private static Hashtable reprToInstr ; private static void defBinOp( String repr, int code ) { new Instruction( repr, CTG_BINOP, code, 0, 2, 1, OPND_INL_AS_IS, null ) ; } private static void defMeta( String repr, int code, int nI, MetaInstrInstantiator mii ) { new Instruction( repr, CTG_META, code, nI, 0, 0, OPND_INL_AS_IS, mii ) ; } private static Instruction defBranchOp( String repr, int code ) { return new Instruction( repr, CTG_BRCC, code, 1, 1, 0, OPND_INL_PC_REL, null ) ; } private static void defInstr( String repr, int code, int nI, int nS, int nR, int inlOpndKind ) { new Instruction( repr, CTG_OP, code, nI, nS, nR, inlOpndKind, null ) ; } private static void defPragma( String repr, int code, int nI, int nS, int nR ) { new Instruction( repr, CTG_PRAGMA, code, nI, nS, nR, OPND_INL_AS_IS, null ) ; } private static void defRegInstr( String repr, int code, int nI, int nS, int nR ) { new Instruction( repr, CTG_OP, code, nI, nS, nR, OPND_INL_REG, null ) ; } private static void defUnOp( String repr, int code ) { new Instruction( repr, CTG_UNOP, code, 0, 1, 1, OPND_INL_AS_IS, null ) ; } private static void defTrap( String repr, int code ) { new Instruction( repr, CTG_TRAP, code, 0, 0, 0, OPND_INL_AS_IS, null ) ; } static { codeToInstr = new Hashtable() ; reprToInstr = new Hashtable() ; defBinOp ( "add" , BI_ADD ) ; defBinOp ( "and" , BI_AND ) ; //defBinOp ( "cmp" , BI_CMP ) ; defBinOp ( "div" , BI_DIV ) ; defBinOp ( "lsl" , BI_LSL ) ; defBinOp ( "lsr" , BI_LSR ) ; defBinOp ( "mod" , BI_MOD ) ; defBinOp ( "mul" , BI_MUL ) ; defBinOp ( "or" , BI_OR ) ; defBinOp ( "rol" , BI_ROL ) ; defBinOp ( "ror" , BI_ROR ) ; defBinOp ( "sub" , BI_SUB ) ; defBinOp ( "xor" , BI_XOR ) ; defBinOp ( "eq" , BI_EQ ) ; defBinOp ( "ne" , BI_NE ) ; defBinOp ( "lt" , BI_LT ) ; defBinOp ( "gt" , BI_GT ) ; defBinOp ( "le" , BI_LE ) ; defBinOp ( "ge" , BI_GE ) ; /* defBranchOp ( "bcc" , BR_CC ).defCCForBranch( 0, CC ) ; defBranchOp ( "bcs" , BR_CS ).defCCForBranch( CC, CC ) ; defBranchOp ( "beq" , BR_EQ ).defCCForBranch( ZZ, ZZ ) ; defBranchOp ( "bge" , BR_GE ).defCCForBranch( NN|VV, NN|VV, 0, NN|VV ) ; defBranchOp ( "bgt" , BR_GT ).defCCForBranch( NN|VV, NN|VV|ZZ, 0, NN|VV|ZZ ) ; defBranchOp ( "bhi" , BR_HI ).defCCForBranch( 0, ZZ|CC ) ; defBranchOp ( "ble" , BR_LE ).defCCForBranch( NN, NN|VV, VV, NN|VV, ZZ, ZZ ) ; defBranchOp ( "bls" , BR_LS ).defCCForBranch( CC, CC, ZZ, ZZ ) ; defBranchOp ( "blt" , BR_LT ).defCCForBranch( NN, NN|VV, VV, NN|VV ) ; defBranchOp ( "bmi" , BR_MI ).defCCForBranch( NN, NN ) ; defBranchOp ( "bne" , BR_NE ).defCCForBranch( 0, ZZ ) ; defBranchOp ( "bpl" , BR_PL ).defCCForBranch( 0, NN ) ; */ defInstr ( "ajs" , I_ADJS , 1, UNKNOWN, 0, OPND_INL_AS_IS ) ; defInstr ( "bra" , I_BRA , 1, 0, 0, OPND_INL_PC_REL ) ; defInstr ( "brf" , I_BRF , 1, 1, 0, OPND_INL_PC_REL ) ; defInstr ( "brt" , I_BRT , 1, 1, 0, OPND_INL_PC_REL ) ; defInstr ( "bsr" , I_BSR , 1, 0, 1, OPND_INL_PC_REL ) ; defInstr ( "halt", I_HALT , 0, 0, 0, OPND_INL_AS_IS ) ; defInstr ( "jsr" , I_JSR , 0, 1, 1, OPND_INL_AS_IS ) ; defInstr ( "lds" , I_LDS , 1, 0, 1, OPND_INL_AS_IS ) ; defInstr ( "ldms", I_LDMS , 2, 0, UNKNOWN, OPND_INL_AS_IS ) ; defInstr ( "ldsa", I_LDSA , 1, 0, 1, OPND_INL_AS_IS ) ; defInstr ( "lda" , I_LDA , 1, 1, 1, OPND_INL_AS_IS ) ; defInstr ( "ldma", I_LDMA , 2, 1, UNKNOWN, OPND_INL_AS_IS ) ; defInstr ( "ldaa", I_LDAA , 1, 1, 1, OPND_INL_AS_IS ) ; defInstr ( "ldc" , I_LDC , 1, 0, 1, OPND_INL_AS_IS ) ; defInstr ( "ldl" , I_LDL , 1, 0, 1, OPND_INL_AS_IS ) ; defInstr ( "ldml", I_LDML , 2, 0, UNKNOWN, OPND_INL_AS_IS ) ; defInstr ( "ldla", I_LDLA , 1, 0, 1, OPND_INL_AS_IS ) ; defInstr ( "link", I_LINK , 1, 0, UNKNOWN, OPND_INL_AS_IS ) ; defInstr ( "nop" , I_NOP , 0, 0, 0, OPND_INL_AS_IS ) ; defInstr ( "ret" , I_RET , 0, 1, 0, OPND_INL_AS_IS ) ; defInstr ( "sts" , I_STS , 1, 1, 0, OPND_INL_AS_IS ) ; defInstr ( "stms", I_STMS , 2, UNKNOWN, 0, OPND_INL_AS_IS ) ; defInstr ( "sta" , I_STA , 1, 2, 0, OPND_INL_AS_IS ) ; defInstr ( "stma", I_STMA , 2, UNKNOWN, 0, OPND_INL_AS_IS ) ; defInstr ( "stl" , I_STL , 1, 1, 0, OPND_INL_AS_IS ) ; defInstr ( "stml", I_STML , 2, UNKNOWN, 0, OPND_INL_AS_IS ) ; defInstr ( "swp" , I_SWP , 0, 2, 2, OPND_INL_AS_IS ) ; defInstr ( "trap", I_TRAP , 1, UNKNOWN, UNKNOWN, OPND_INL_AS_IS ) ; defInstr ( "unlink", I_UNLINK, 0, UNKNOWN, 0, OPND_INL_AS_IS ) ; //defPragma ( ".db" , PR_DB , UNKNOWN, 0, 0 ) ; //defPragma ( ".dc" , PR_DC , 1, 0, 0 ) ; //defPragma ( ".ds" , PR_DS , 1, 0, 0 ) ; //defPragma ( ".dw" , PR_DW , UNKNOWN, 0, 0 ) ; defRegInstr ( "ldr" , I_LDR , 1, 0, 1 ) ; defRegInstr ( "ldrr" , I_LDRR , 2, 0, 0 ) ; defRegInstr ( "str" , I_STR , 1, 1, 0 ) ; defRegInstr ( "swpr" , I_SWPR , 1, 1, 1 ) ; defRegInstr ( "swprr", I_SWPRR , 2, 0, 0 ) ; // ?? //defTrap ( "printf" , TR_PRINTF ) ; defUnOp ( "neg" , UI_NEG ) ; defUnOp ( "not" , UI_NOT ) ; defMeta ( "annote" , M_ANNOTE, 5, MetaInstrInstantiator.newAnnoteInstantiator() ) ; } public static Instruction findByRepr( String repr ) { return (Instruction)reprToInstr.get( repr ) ; } public static Instruction findByCode( int code ) { return (Instruction)codeToInstr.get( new Integer( code ) ) ; } private class CCInfo { int check, mask ; CCInfo( int c, int m ) { check = c ; mask = m ; } } private class OpndInfo { int inlineOpndKind ; OpndInfo( int io ) { inlineOpndKind = io ; } } private String repr ; private int categ ; private int code ; private int nrInlineOpnds ; private int nrStackOpnds ; private int nrStackResults ; private CCInfo[] brccInfo ; private OpndInfo[] opndInfo ; private MetaInstrInstantiator metaInstrInstantiator ; /** * Define an instruction */ private Instruction ( String repr, int categ, int code , int nInl, int nSt, int nR , int inlOpndKind , MetaInstrInstantiator mii ) { this.repr = repr ; this.categ = categ ; this.code = code ; this.nrInlineOpnds = nInl ; this.nrStackOpnds = nSt ; this.nrStackResults = nR ; codeToInstr.put( new Integer( code ), this ) ; reprToInstr.put( repr, this ) ; brccInfo = null ; metaInstrInstantiator = mii ; opndInfo = new OpndInfo[ nrInlineOpnds ] ; for ( int i = 0 ; i < opndInfo.length ; i++ ) opndInfo[ i ] = new OpndInfo( inlOpndKind ) ; } private void defCCForBranch( int check1, int mask1, int check2, int mask2, int check3, int mask3 ) { brccInfo = new CCInfo[] { new CCInfo(check1, mask1), new CCInfo(check2, mask2), new CCInfo(check3, mask3) } ; } private void defCCForBranch( int check1, int mask1, int check2, int mask2 ) { brccInfo = new CCInfo[] { new CCInfo(check1, mask1), new CCInfo(check2, mask2) } ; } private void defCCForBranch( int check1, int mask1 ) { brccInfo = new CCInfo[] { new CCInfo(check1, mask1) } ; } protected boolean srMatchesCCInfo( int sr ) { if ( brccInfo != null ) { for ( int i = 0 ; i < brccInfo.length ; i++ ) { if ( ( sr & brccInfo[i].mask ) == brccInfo[i].check ) return true ; } } return false ; } public int getNrInlineOpnds() { return nrInlineOpnds ; } public String getRepr() { return repr ; } public String getRepr( int[] args ) { String s = getRepr() ; for ( int i = 0 ; i < args.length ; i++ ) { String a ; switch( opndInfo[ i ].inlineOpndKind ) { case OPND_INL_REG : a = Registers.getRegOrAliasName( args[i] ) ; break ; default : a = Utils.asHex( args[i], false, true, false ) ; break ; } s = s + " " + a ; } return s ; } public String getRepr( String usedLabel ) { return getRepr() + " " + usedLabel ; } public int getCode() { return code ; } public int getNrMemCells() { return getNrInlineOpnds() + 1 ; } protected int getNrStackOpnds() { return nrStackOpnds ; } protected int getNrStackResults() { return nrStackResults ; } public boolean isRelativeOpnd( int arg ) { return opndInfo[ arg ].inlineOpndKind == OPND_INL_PC_REL ; } public boolean isMeta( ) { return getCategory() == CTG_META ; } public MetaInstruction instantiateMeta( Vector args ) { return metaInstrInstantiator.instantiate( this, args ) ; } protected int getCategory() { return categ ; } private String getCategoryName() { return categNames[ getCategory() ] ; } private static Enumeration getInstructionNames() { return reprToInstr.keys() ; } public static HelpSupplier getHelpSupplier() { return findByRepr( "halt" ) ; } public Enumeration getTopics() { return getInstructionNames() ; } public String getHelpSupplierName() { return "Internal administration" ; } public String getShortSummaryForTopic( String topic ) { Instruction instr ; if ( ( instr = findByRepr( topic ) ) != null ) { return instr.getRepr() + " internal administration only" ; } return "" ; } private static String textForNr( int n ) { return n == UNKNOWN ? "instruction dependent" : "" + n ; } private void getHelpOnCC( HelpAccumulator acc ) { for ( int i = 0 ; i < brccInfo.length ; i++ ) { acc.append( "(" ) ; for ( int cc = SR_Z ; cc <= SR_N ; cc++ ) { if ( ((1<<cc) & brccInfo[i].mask) > 0 ) { acc.append( ccNames[cc] + "=" + ( (brccInfo[i].check >> cc) & 1 ) ) ; if ( brccInfo[i].mask >> (cc+1) > 0 ) { acc.append( " and " ) ; } } } acc.append( ")" ) ; if ( i < (brccInfo.length - 1) ) { acc.append( " or " ) ; } } } public void getHelpForTopic( String topic, HelpAccumulator acc ) { Instruction instr ; if ( ( instr = findByRepr( topic ) ) == null ) { acc.append( "?? should not happen, no help for " + topic ) ; } else { acc.beginBlockQuote() ; acc.beginTable( 5, 85, null ) ; acc.tableHeaderRow ( new Object[] { "Instruction" , "Nr of inline Opnds" , "Nr of stack Opnds" , "Nr of stack Results" , "Instr code (hex)" } ) ; acc.tableRow ( new Object[] { topic , textForNr( instr.getNrInlineOpnds() ) , textForNr( instr.getNrStackOpnds() ) , textForNr( instr.getNrStackResults() ) , acc.reprAsHex( instr.getCode() ) } ) ; acc.endTable() ; /* acc.linebreak() ; acc.append( "Is a " + instr.getCategoryName() + "." ) ; if ( instr.brccInfo != null ) { acc.linebreak() ; acc.append( "Branches on " ) ; instr.getHelpOnCC( acc ) ; } */ acc.endBlockQuote() ; } } public String toString() { return "instr " + repr + " code=(" + Utils.asHex(categ) + "/" + Utils.asHex(code) + ") ninl=" + nrInlineOpnds ; } }
\ 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.* ;import java.awt.* ; public class Machine{ protected Memory memory ; protected Registers registers ; protected MachineState state ; private Messenger messenger ; public Machine( MachineState st, Messenger m ) { state = st ; messenger = m ; reset() ; } public void reset() { state.reset() ; memory = state.getMemory() ; registers = state.getRegisters( ) ; } public MachineState state() { return state ; } public Memory memory() { return memory ; } public Registers registers() { return registers ; } private int dir( int v ) { return state.dir( v ) ; } private void annote( MemoryAnnotation ann ) { memory.setAnnotationAt( registers.getReg( registers.SP ), ann ) ; } private void copyMem( int fromA, int toA, int size, MemoryAnnotation ann ) { if ( dir(fromA) < dir(toA) ) { for ( int i = size-1 ; i >= 0 ; i-- ) memory.setAt( toA + dir(i), memory.getAt( fromA + dir(i) ) ) ; } else { for ( int i = 0 ; i < size ; i++ ) memory.setAt( toA + dir(i), memory.getAt( fromA + dir(i) ) ) ; } for ( int i = 0 ; i < size ; i++ ) memory.setAnnotationAt( toA + dir(i), ann ) ; } private void pushMultiple( int fromA, int sz, MemoryAnnotation ann ) { int toA = registers.getReg( registers.SP ) + dir(1) ; registers.adjustSP( dir(sz) ) ; copyMem( fromA, toA, sz, ann ) ; } private void pushMultiple( int fromA, int sz, Instruction instr ) { pushMultiple( fromA, sz, new MemoryAnnotation( instr.getRepr(), null ) ) ; } private void pushMultiple( int fromA, int sz ) { pushMultiple( fromA, sz, state.instr ) ; } private void push( int v, MemoryAnnotation ann ) { registers.adjustSP( dir(1) ) ; registers.setRegInd( registers.SP, v ) ; annote( ann ) ; } private void push( int v, Instruction instr ) { push( v, new MemoryAnnotation( instr.getRepr(), null ) ) ; } private void push( int v ) { push( v, state.instr ) ; } private void pushCopyOfReg( int r ) { push( registers.getReg(r), new MemoryAnnotation( "copy of " + registers.getRegName(r), Color.cyan ) ) ; } private void pushPCAsReturnAddr() { push( registers.getReg(registers.PC), new MemoryAnnotation( "return addr ", Color.red ) ) ; } private void pushMP() { push( registers.getReg(registers.MP), new MemoryAnnotation( "prev " + registers.getRegName(registers.MP), Color.blue ) ) ; } private int pop() { annote( null ) ; int v = registers.getRegInd( registers.SP ) ; registers.adjustSP( dir(-1) ) ; return v ; } private void popMultiple( int toA, int sz ) { registers.adjustSP( dir(-sz) ) ; int fromA = registers.getReg( registers.SP ) + dir(1) ; copyMem( fromA, toA, sz, null ) ; } public MachineState getMachineState() { return state ; } private int fetchNextInstr() { int pc = registers.getPC() ; int oldpc = pc ; int code = memory.getAt( pc ) ; pc++ ; Instruction instr = Instruction.findByCode( code ) ; if ( instr == null ) { messenger.println( "illegal instruction code " + Utils.asHex(code) ) ; code = Instruction.I_HALT ; instr = Instruction.findByCode( code ) ; } else { state.setCurrentInstr( oldpc, code, instr ) ; for ( int i = 0 ; i < state.nInlineOpnds ; i++ ) { state.inlineOpnds[ i ] = memory.getAt( pc++ ) ; } } registers.setPC( pc ) ; return code ; } public int execBinop( int code, int o1, int o2 ) { switch( code ) { case Instruction.BI_ADD : o1 += o2 ; break ; case Instruction.BI_SUB : o1 -= o2 ; break ; case Instruction.BI_MUL : o1 *= o2 ; break ; case Instruction.BI_DIV : o1 /= o2 ; break ; case Instruction.BI_MOD : o1 %= o2 ; break ; case Instruction.BI_AND : o1 &= o2 ; break ; case Instruction.BI_OR : o1 |= o2 ; break ; case Instruction.BI_XOR : o1 ^= o2 ; break ; case Instruction.BI_LSL : o1 <<= o2 ; break ; case Instruction.BI_LSR : o1 >>= o2 ; break ; case Instruction.BI_ROL : o1 = ( o1 << o2 ) | ( o1 >> ( Instruction.nWordBits - o2 ) ) ; break ; case Instruction.BI_ROR : o1 = ( o1 >> o2 ) | ( o1 << ( Instruction.nWordBits - o2 ) ) ; break ; case Instruction.BI_EQ : o1 = o1 == o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; case Instruction.BI_NE : o1 = o1 != o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; case Instruction.BI_LT : o1 = o1 < o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; case Instruction.BI_GT : o1 = o1 > o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; case Instruction.BI_LE : o1 = o1 <= o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; case Instruction.BI_GE : o1 = o1 >= o2 ? Instruction.CONST_TRUE : Instruction.CONST_FALSE ; break ; /* case Instruction.BI_CMP : int sr, r, c1, c2 ; sr = c1 = c2 = 0 ; r = o1 - o2 ; if ( r == 0 ) sr |= Instruction.ZZ ; if ( (int)r < 0 ) sr |= Instruction.NN ; r = ( o1 >> 1 ) + ( ( - o2 ) >> 1 ) ; if ( (int)r < 0 ) { sr |= Instruction.CC ; c1 = Instruction.VV ; } r = ( ( o1 << 1 ) >> 1 ) + ( ( ( - o2 ) << 1 ) >> 1 ) ; if ( (int)r < 0 ) c2 = Instruction.VV ; sr |= ( c1 ^ c2 ) & Instruction.VV ; o1 = sr ; break ; */ } return o1 ; } public int execUnop( int code, int o1 ) { switch( code ) { case Instruction.UI_NEG : o1 = -o1 ; break ; case Instruction.UI_NOT : o1 = ~o1 ; break ; } return o1 ; } public void halt() { state.setHalted() ; messenger.println( "machine halted" ) ; } public void executeOne() { if ( state.isHalted ) return ; int code = fetchNextInstr() ; //System.out.println( "exec1 " + state ) ; int tmp1, tmp2, tmp3 ; switch( state.instr.getCategory() ) { case Instruction.CTG_BINOP : tmp2 = pop() ; tmp1 = pop() ; push( execBinop( code, tmp1, tmp2 ) ) ; break ; case Instruction.CTG_UNOP : tmp1 = pop() ; push( execUnop( code, tmp1 ) ) ; break ; case Instruction.CTG_OP : switch( code ) { case Instruction.I_ADJS : registers.adjustReg( registers.SP, dir(state.inlineOpnds[ 0 ]) ) ; break ; case Instruction.I_BRA : registers.adjustReg( registers.PC, state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_BRF : tmp1 = pop() ; if ( tmp1 == 0 ) registers.adjustReg( registers.PC, state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_BRT : tmp1 = pop() ; if ( tmp1 != 0 ) registers.adjustReg( registers.PC, state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_BSR : pushPCAsReturnAddr() ; registers.adjustReg( registers.PC, state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_HALT : halt() ; break ; case Instruction.I_JSR : tmp1 = pop() ; pushPCAsReturnAddr() ; registers.setReg( registers.PC, tmp1 ) ; break ; case Instruction.I_LDS : push( registers.getRegDisplInd( registers.SP, dir(state.inlineOpnds[ 0 ]) ) ) ; break ; case Instruction.I_LDMS : pushMultiple( registers.getRegDispl( registers.SP, dir(state.inlineOpnds[ 0 ]) ), state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_LDA : push( memory.getAt( pop() + dir(state.inlineOpnds[ 0 ]) ) ) ; break ; case Instruction.I_LDMA : pushMultiple( pop() + dir(state.inlineOpnds[ 0 ]), state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_LDC : push( state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_LDL : push( registers.getRegDisplInd( registers.MP, dir(state.inlineOpnds[ 0 ]) ) ) ; break ; case Instruction.I_LDML : pushMultiple( registers.getRegDispl( registers.MP, dir(state.inlineOpnds[ 0 ]) ), state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_LDAA : push( pop() + state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_LDSA : push( registers.getRegDispl( registers.SP, dir(state.inlineOpnds[ 0 ]) ) ) ; break ; case Instruction.I_LDLA : push( registers.getRegDispl( registers.MP, dir(state.inlineOpnds[ 0 ]) ) ) ; break ; case Instruction.I_LDR : pushCopyOfReg( state.inlineOpnds[ 0 ] ) ; break ; case Instruction.I_LDRR : registers.setReg( state.inlineOpnds[ 0 ], registers.getReg( state.inlineOpnds[ 1 ] ) ) ; break ; case Instruction.I_LINK : pushMP( ) ; registers.setReg( registers.MP, registers.getReg( registers.SP ) ) ; tmp1 = registers.getReg( registers.SP ) + dir(1) ; registers.adjustReg( registers.SP, dir(state.inlineOpnds[ 0 ]) ) ; tmp2 = registers.getReg( registers.SP ) ; break ; case Instruction.I_NOP : break ; case Instruction.I_RET : registers.setReg( registers.PC, pop() ) ; break ; case Instruction.I_STS : tmp1 = registers.getRegDispl( registers.SP, dir(state.inlineOpnds[ 0 ]) ) ; memory.setAt( tmp1, pop() ) ; break ; case Instruction.I_STMS : popMultiple( registers.getRegDispl( registers.SP, dir(state.inlineOpnds[ 0 ]) ), state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_STA : tmp1 = pop() ; tmp2 = pop() ; memory.setAt( tmp1 + state.inlineOpnds[ 0 ], tmp2 ) ; break ; case Instruction.I_STMA : tmp1 = pop() ; popMultiple( tmp1 + state.inlineOpnds[ 0 ], state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_STL : registers.setRegDisplInd( registers.MP, dir(state.inlineOpnds[ 0 ]), pop() ) ; break ; case Instruction.I_STML : popMultiple( registers.getRegDispl( registers.MP, dir(state.inlineOpnds[ 0 ]) ), state.inlineOpnds[ 1 ] ) ; break ; case Instruction.I_STR : registers.setReg( state.inlineOpnds[ 0 ], pop() ) ; break ; case Instruction.I_SWP : tmp1 = registers.getRegDisplInd( registers.SP, dir(-1) ) ; registers.setRegDisplInd( registers.SP, dir(-1), registers.getRegInd( registers.SP ) ) ; registers.setRegInd( registers.SP, tmp1 ) ; break ; case Instruction.I_SWPR : tmp1 = registers.getRegInd( registers.SP ) ; tmp2 = state.inlineOpnds[ 0 ] ; registers.setRegInd( registers.SP, registers.getReg( tmp2 ) ) ; registers.setReg( tmp2, tmp1 ) ; break ; case Instruction.I_SWPRR : tmp1 = state.inlineOpnds[ 0 ] ; tmp2 = state.inlineOpnds[ 1 ] ; tmp3 = registers.getReg( tmp1 ) ; registers.setReg( tmp1, registers.getReg( tmp2 ) ) ; registers.setReg( tmp2, tmp3 ) ; break ; case Instruction.I_UNLINK : registers.setReg( registers.SP, registers.getReg( registers.MP ) ) ; registers.setReg( registers.MP, pop() ) ; break ; case Instruction.I_TRAP : switch( state.inlineOpnds[ 0 ] ) { case Instruction.TR_PR_INT : messenger.println( "" + pop() ) ; break ; default : break ; } break ; default : messenger.println( state.instr + " at " + Utils.asHex(state.instrPC) + " not (yet) implemented" ) ; break ; } break ; case Instruction.CTG_BRCC : if ( state.instr.srMatchesCCInfo( pop() ) ) registers.adjustReg( registers.PC, state.inlineOpnds[ 0 ] ) ; break ; default : messenger.println( state.instr + " at " + Utils.asHex(state.instrPC) + " not (yet) implemented" ) ; break ; } } }
\ 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.* ; 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.* ;
public class MachineStateEvent extends UndoableEvent
{
protected MachineStateEvent( Object src, Modification mdf )
{
super( src, mdf ) ;
}
}
\ No newline at end of file
Supports Markdown
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