View Javadoc

1   /*
2    * $Id: Short0Instruction.java,v 1.11 2005/10/25 00:20:07 weiju Exp $
3    * 
4    * Created on 03.10.2005
5    * Copyright 2005 by Wei-ju Wu
6    *
7    * This file is part of The Z-machine Preservation Project (ZMPP).
8    *
9    * ZMPP is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   *
14   * ZMPP is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with ZMPP; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22   */
23  package org.zmpp.vm;
24  
25  
26  /***
27   * This class represents instructions of type SHORT, 0OP.
28   * 
29   * @author Wei-ju Wu
30   * @version 1.0
31   */
32  public class Short0Instruction extends AbstractInstruction {
33  
34    // Opcode numbers for short, 0OP
35    public static final int OP_RTRUE              = 0x00;
36    public static final int OP_RFALSE             = 0x01;
37    public static final int OP_NOP                = 0x04;
38    public static final int OP_SAVE               = 0x05;
39    public static final int OP_RESTORE            = 0x06;
40    public static final int OP_RESTART            = 0x07;
41    public static final int OP_RET_POPPED         = 0x08;
42    public static final int OP_POP                = 0x09; // Versions 1-4
43    public static final int OP_QUIT               = 0x0a;
44    public static final int OP_NEW_LINE           = 0x0b;
45    public static final int OP_SHOW_STATUS        = 0x0c;
46    public static final int OP_VERIFY             = 0x0d;
47    
48    /***
49     * Constructor.
50     * 
51     * @param machineState a reference to the MachineState object
52     * @param opcode the instruction's opcode
53     */
54    public Short0Instruction(Machine machineState, int opcode) {
55      
56      super(machineState, opcode);
57    }
58    
59    /***
60     * {@inheritDoc}
61     */
62    public void execute() {
63      
64      switch (getOpcode()) {
65  
66        case OP_RTRUE:
67          returnFromRoutine(TRUE);
68          break;
69        case OP_RFALSE:
70          returnFromRoutine(FALSE);
71          break;
72        case OP_NOP:
73          nextInstruction();
74          break;
75        case OP_SAVE:
76          branchOnTest(getMachine().save());
77          break;
78        case OP_RESTORE:
79          getMachine().restore();
80          break;
81        case OP_RESTART:
82          getMachine().restart();
83          break;
84        case OP_QUIT:
85          getMachine().quit();
86          break;
87        case OP_RET_POPPED:        
88          returnFromRoutine(getMachine().getVariable(0));
89          break;
90        case OP_POP:        
91          getMachine().getVariable(0);
92          nextInstruction();
93          break;
94        case OP_NEW_LINE:
95          getMachine().newline();
96          nextInstruction();
97          break;
98        case OP_SHOW_STATUS:
99          getMachine().updateStatusLine();
100         nextInstruction();
101         break;
102       case OP_VERIFY:
103         branchOnTest(getMachine().hasValidChecksum());
104         break;
105       default:        
106         throwInvalidOpcode();
107     }
108   }
109   
110   /***
111    * {@inheritDoc}
112    */
113   public InstructionForm getInstructionForm() { return InstructionForm.SHORT; }
114   
115   /***
116    * {@inheritDoc}
117    */
118   public OperandCount getOperandCount() { return OperandCount.C0OP; }
119   
120   /***
121    * {@inheritDoc}
122    */
123   public boolean isBranch() {
124     
125     switch (getOpcode()) {
126     
127       case OP_SAVE:
128       case OP_RESTORE:
129       case OP_VERIFY:
130         return true;
131       default:
132         return false;
133     }
134   }  
135 }