computer
Class ToyComputer

java.lang.Object
  extended by computer.ToyComputer

public class ToyComputer
extends java.lang.Object

A simulator for a toy computer with a few instructions. The computer uses a stack for all computations rather than registers.

Author:
jbergin

Field Summary
private static int add
           
private  Instruction decoder
           
private static int dupl
           
private static int halt
           
private static int inc
           
private static int inp
           
private static int jmp
           
private static int jnn
           
private static int ldd
           
private  int loadAddress
           
private  Memory memory
           
private static int outp
           
private  int pc
           
private static int pop
           
private static int push
           
private  boolean running
           
private  Stack<java.lang.Double> stack
           
private static int subtr
           
private static int toss
           
private static int zer
           
 
Constructor Summary
ToyComputer()
           
 
Method Summary
 void add()
          Pop the two top elements from the stack, add them and push the result
private  void decodeDo()
          Decode the instruction in the decoder (break it into parts) and execute it
 void dupl()
          Duplicate the top of the stack.
private  void fetch()
          Fetch an instruction using the program counter (pc).
 void halt()
          Terminate execution of the running program
 void inc()
          Add 1.0 to the top of the stack.
private  void increment()
          Increment the pc
 void inp()
          Retrieve a double from standard input and push the result
 void jmp(int where)
          Unconditionally jump to an instruction at the specified address
 void jnn(int where)
          Pop the stack.
 void ldd()
          The top of the stack is an address.
 void loadData(double data, int location)
          Load a data cell at an arbitrary memory address
 void loadInstruction(int opcode)
          Load an instruction into the next available memory location starting from zero.
 void loadInstruction(int opcode, int operand)
          Load an instruction into the next available memory location starting from zero
static void main(java.lang.String[] args)
           
 void outp()
          Pop the stack and print the result on standard output
private  void perform(int op, int opnd)
          The internal map between opcode numbers and the operational methods
 void pop(int mem)
          Pop the stack and put the result into memory
 void push(int mem)
          Copies a memory cell and pushes it onto the stack
 void run()
          Execute the program currently stored in the memory.
 void subtr()
          Pop the two top elements from the stack and subtract them.
 void toss()
          Discard the top of the stack.
 void zer()
          Push a zero onto the top of the stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

memory

private Memory memory

stack

private Stack<java.lang.Double> stack

loadAddress

private int loadAddress

running

private boolean running

pc

private int pc

decoder

private Instruction decoder

push

private static final int push
See Also:
Constant Field Values

pop

private static final int pop
See Also:
Constant Field Values

toss

private static final int toss
See Also:
Constant Field Values

outp

private static final int outp
See Also:
Constant Field Values

dupl

private static final int dupl
See Also:
Constant Field Values

add

private static final int add
See Also:
Constant Field Values

subtr

private static final int subtr
See Also:
Constant Field Values

halt

private static final int halt
See Also:
Constant Field Values

inp

private static final int inp
See Also:
Constant Field Values

jmp

private static final int jmp
See Also:
Constant Field Values

jnn

private static final int jnn
See Also:
Constant Field Values

ldd

private static final int ldd
See Also:
Constant Field Values

zer

private static final int zer
See Also:
Constant Field Values

inc

private static final int inc
See Also:
Constant Field Values
Constructor Detail

ToyComputer

public ToyComputer()
Method Detail

push

public void push(int mem)
Copies a memory cell and pushes it onto the stack

Parameters:
mem - the address of the memory cell desired

pop

public void pop(int mem)
Pop the stack and put the result into memory

Parameters:
mem - the memory cell address into which to put the stack top

toss

public void toss()
Discard the top of the stack. Pop, but throws away the result


outp

public void outp()
Pop the stack and print the result on standard output


dupl

public void dupl()
Duplicate the top of the stack. i.e. push a copy of the top onto the top


add

public void add()
Pop the two top elements from the stack, add them and push the result


subtr

public void subtr()
Pop the two top elements from the stack and subtract them. The original top is the left operand and the one below is the right operand. Push the result


jmp

public void jmp(int where)
Unconditionally jump to an instruction at the specified address

Parameters:
where - the address of the next instruction to be executed

zer

public void zer()
Push a zero onto the top of the stack.


inc

public void inc()
Add 1.0 to the top of the stack. Increment.


ldd

public void ldd()
The top of the stack is an address. Pop it, extract the data item at that address and push the result. Replace an address with the item it points to


jnn

public void jnn(int where)
Pop the stack. If the result is non-negative then jump to the specified address

Parameters:
where - the address of the next instruction of the stack top was non-negative

halt

public void halt()
Terminate execution of the running program


inp

public void inp()
Retrieve a double from standard input and push the result

Throws:
java.lang.NumberFormatException - if the input can't be interpreted as a double

loadInstruction

public void loadInstruction(int opcode,
                            int operand)
Load an instruction into the next available memory location starting from zero

Parameters:
opcode - the opcode of the instruction
operand - the operand (memory address) that the opcode will operate on

loadInstruction

public void loadInstruction(int opcode)
Load an instruction into the next available memory location starting from zero. The operand will be set to zero in this instruction

Parameters:
opcode - the opcode of the instruction

loadData

public void loadData(double data,
                     int location)
Load a data cell at an arbitrary memory address

Parameters:
data - the double value to be stored (a Data cell will be created)
location - the memory address into which to put the new cell

run

public void run()
Execute the program currently stored in the memory. Starts at address 0 in all cases. It uses a typical fetch-increment-decode-do cycle


fetch

private void fetch()
Fetch an instruction using the program counter (pc). Put it into the decoder in the CPU (Central Processing Unit)


increment

private void increment()
Increment the pc


decodeDo

private void decodeDo()
Decode the instruction in the decoder (break it into parts) and execute it


perform

private void perform(int op,
                     int opnd)
The internal map between opcode numbers and the operational methods

Parameters:
op - an opcode
opnd - the (optionsl) oparand for this opcode

main

public static void main(java.lang.String[] args)