com.cmpware.tools
Class AutoModel

java.lang.Object
  extended by com.cmpware.cmp.Memory
      extended by com.cmpware.cmp.Processor
          extended by com.cmpware.tools.AutoModel
All Implemented Interfaces:
ReadWriteInterface
Direct Known Subclasses:
ARC700, AutoSimple, LatticeMicro32, MicroBlaze, MIPS32, PowerPC, PowerPC64, SPU, Xtensa

public abstract class AutoModel
extends Processor

This abstract class is used to describe the instruction set for a processor. This is in turn used to drive other parts of the system, notably the AsmTool and DasmTool which produce a simple assembler and disassembler. This class also extends the Processor class for use in the AutoModel tool. This permits full Cmpware simulation models to be built using the instruction set data. This model supports instruction sizes up to 64 bits with maximum field sizes of 32 bits.

Copyright (c) 2006 Cmpware, Inc. All Rights Reserved.

Author:
SAG

Nested Class Summary
static interface AutoModel.Asm
          This class defines an assembler function.
static class AutoModel.Decode
          The Decode class describes a field / value pair used to decode an instruction.
static class AutoModel.Field
          This class defines an instruction field.
static class AutoModel.Format
          The format class describes an instruction format.
static interface AutoModel.Function
          This class defines the function of an operation.
static class AutoModel.Instruction
          The instruction class describes an instruction in the processor.
static class AutoModel.Symbol
          This class defines a symbol.
 
Field Summary
static java.lang.String copyright
          Copyright string
protected  long currentInstr
          The current instruction
protected  AutoModel.Instruction currentInstrType
          The current instruction type
protected  int[] instructionSizes
          The different instruction sizes (in bits)
protected static boolean TRACE
          Set this to true to get debug / trace printouts
 
Fields inherited from class com.cmpware.cmp.Processor
bkpt, branchAddr, branchDelay, breakpointHit, currentBranchDelay, currentInstrCode, cycleCount, elf, idlePower, iMix, instructionSize, name, noop, opcodeName, pc, prevInstr, prevInstrCode, r, regName, runningPower, sr, sregName, stall, stallCount
 
Fields inherited from class com.cmpware.cmp.Memory
BIG, LITTLE
 
Constructor Summary
AutoModel()
           
 
Method Summary
 void advancePC()
          This method advances the Program Counter.
 void asmFixup(java.lang.String[] tokens, AutoModel.Instruction instr)
          This method permits one last chance to manipulate the instruction word before the bits are written out to a file in AsmTool.
static int bitsToBytes(int a)
          This routine converts a bit count to a byte count, where the byte count is the minimum number of bytes to hold this many bits.
static java.lang.String[] concatenate(java.lang.String[] a, java.lang.String[] b)
          This method concatenates two arrays of strings.
 java.lang.String dasm(byte[] instr)
          This method disassembles an instruction.
 java.lang.String dasm(long instrData, AutoModel.Instruction instr)
          This method disassembles an instruction.
 void dasmFixup(long instrData, AutoModel.Instruction instr)
          This method can be overloaded to fix up any problems or to do any clean-up in the disassembler.
 int decode(int instr)
          A unique code identifying the instruction is returned by this method.
 void defineInstructions(AutoModel.Instruction[] instructions)
          This method is used to pass the instruction data tables to the AutoModel.
 void execute(int instr)
          This method is called after the fetch() and decode() and represents the execution phase of the instruction.
 void fetch()
          This method fetches an instruction from memory and updates the currentInstr and prevInstr.
 long fetch(int addr, int bytes)
          This method returns a long (64-bit) value based on the data at 'bytes' bytes of memory at address 'addr'.
 byte[] getInstruction(int addr)
          This method fetches an instruction from memory.
 AutoModel.Instruction[] getInstructions()
          This method returns an array of Instruction objects, defining all of the instructions in the instruction set.
 int[] getInstructionSizes()
          This method returns an array of integers containing the different instruction sizes in the model.
 int getPC()
          This method returns the current Program Counter.
static java.lang.String[] getSymbolNames(AutoModel.Symbol[] symbols)
          This method takes in a symbol table and returns an array of strings.
 AutoModel.Instruction instrMatch(long instr, int bits)
          This method returns the instruction object from the instruction table associated with a given instruction.
static void main(AutoModel am, java.lang.String[] args)
          This is the main program which is used to assemble / disassemble from the command line.
 void postExecute(int instr)
          This method is called immediately after execution and may be overloaded to do things like save instruction field values to locals.
 void preExecute(int instr)
          This method is called immediately before execution and may be overloaded to do things like save instruction field values to locals.
 void setPC(int i)
          This method sets the current Program Counter.
static int signExtend(int a, int bits)
          This method is used to take a value 'a' of bit length 'bits' and sign extend it, making it a proper signed integer value.
static long signExtend(long a, int bits)
          This method is used to take a value 'a' of bit length 'bits' and sign extend it, making it a proper signed integer value.
static java.lang.String toHexString(byte[] b)
          This method converts a byte array to a hexaecimal string.
static java.lang.String toHexString(byte[] b, int offset, int byteCount)
          This method returns a string representation of a portion of a byte array in hexadecimal.
static long toLong(byte[] b, boolean endian)
          This method converts a byte array to a long.
 void traceOff()
          This method turns on tracing for model debug.
 void traceOn()
          This method turns on tracing for model debug.
 
Methods inherited from class com.cmpware.cmp.Processor
branch, branchNoDelay, defineBranchDelay, defineBreakpoint, defineInstructionSize, defineName, defineNoop, defineOpcodeNames, defineRegisterNames, defineRegisters, defineSpecialRegisterNames, defineSpecialRegisters, get, getBreakpoint, getBreakpointData, getBreakpoints, getCycleCount, getDebugRegisters, getElf, getIdlePower, getInstructionMix, getInstructionSize, getName, getNoop, getOpcodeNames, getRegister, getRegisterNames, getRegisters, getRunningPower, getSpecialRegister, getSpecialRegisterNames, getSpecialRegisters, getStallCount, isBreakpoint, isStalled, load, postLoad, removeAllBreakpoints, removeBreakpoint, reset, setBreakpoint, setIdlePower, setRegister, setRunningPower, setSpecialRegister, stall, statistics, step
 
Methods inherited from class com.cmpware.cmp.Memory
addInput, addMemory, addOutput, getEndian, getLocalMemory, getMemoryManager, getMmio, getProfile, hexDump, hexDump, isReadable, isValidAddress, isWriteable, profile, read, read, read16, read24, read32, read64, removeMemory, resize, setEndian, size, toInt, toShort, write, write, write16, write24, write32, write64
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

copyright

public static final java.lang.String copyright
Copyright string

See Also:
Constant Field Values

instructionSizes

protected int[] instructionSizes
The different instruction sizes (in bits)


currentInstrType

protected AutoModel.Instruction currentInstrType
The current instruction type


currentInstr

protected long currentInstr
The current instruction


TRACE

protected static boolean TRACE
Set this to true to get debug / trace printouts

Constructor Detail

AutoModel

public AutoModel()
Method Detail

main

public static void main(AutoModel am,
                        java.lang.String[] args)
This is the main program which is used to assemble / disassemble from the command line.

Parameters:
args - The command line args. Should be a flag ("-asm" of "-dasm") followed by an input filename and an output filename (three strings).

defineInstructions

public void defineInstructions(AutoModel.Instruction[] instructions)
This method is used to pass the instruction data tables to the AutoModel. It is not called as a constructor but should probably be called early in the subclass constructor.

Parameters:
instructions - the instruction data tables.

decode

public int decode(int instr)
           throws IllegalOpcodeException,
                  MemoryAccessException
Description copied from class: Processor
A unique code identifying the instruction is returned by this method. In some processors with simple fixed-field opcodes this may be a very simple method to define. Other architectures with more complex decoding will require a more complex implementation.

Specified by:
decode in class Processor
Parameters:
instr - The instruction to be decoded.
Returns:
A unique code identifying the instruction is returned by this method.
Throws:
IllegalOpcodeException - if an illegal opcode is encountered.
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.

execute

public void execute(int instr)
             throws MemoryAccessException,
                    IllegalRegisterException,
                    IllegalOpcodeException
Description copied from class: Processor
This method is called after the fetch() and decode() and represents the execution phase of the instruction. All update of processor state occurs in this method. This is where most of the processor definition will occur. See examples for suggestions on implementation of this method.

Specified by:
execute in class Processor
Parameters:
instr - The instruction to be executed.
Throws:
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.
IllegalRegisterException - if an attempt is made to access a register illegally.
IllegalOpcodeException - is added for models which may combine decode and execute in the same method (such as AutoModel).

setPC

public void setPC(int i)
Description copied from class: Processor
This method sets the current Program Counter.

Specified by:
setPC in class Processor

getPC

public int getPC()
Description copied from class: Processor
This method returns the current Program Counter.

Specified by:
getPC in class Processor
Returns:
This method returns the current Program Counter.

dasm

public java.lang.String dasm(byte[] instr)
Description copied from class: Processor
This method disassembles an instruction. Note that a byte array is provided as the interface. This is to make this method as generic as possible.

Specified by:
dasm in class Processor
Parameters:
instr - The instruction to be disassembled.
Returns:
This method returns the disassembled instruction.

fetch

public void fetch()
           throws MemoryAccessException
Description copied from class: Processor
This method fetches an instruction from memory and updates the currentInstr and prevInstr. Note that breakpoints are taken into account. If the previous instruction executed was a breakpoint, the actual instruction is fetched from the saved value in the breakpoint table.

Overrides:
fetch in class Processor
Throws:
MemoryAccessException

getInstruction

public byte[] getInstruction(int addr)
                      throws MemoryAccessException
Description copied from class: Processor
This method fetches an instruction from memory. Note that breakpoints are taken into account. If the instruction is a breakpoint, the actual instruction is fetched from the saved value in the breakpoint table. Note that this is generally for off-line use and not a replacement for the fetch() method.

Overrides:
getInstruction in class Processor
Parameters:
addr - The address at which to fetch the instruction.
Throws:
MemoryAccessException

advancePC

public void advancePC()
Description copied from class: Processor
This method advances the Program Counter. It takes into account branches and stalls. This is provided as a seperate method (rather than being included as part of step()) to permit it to be overloaded.

Overrides:
advancePC in class Processor

getInstructions

public AutoModel.Instruction[] getInstructions()
This method returns an array of Instruction objects, defining all of the instructions in the instruction set.

Returns:
This method returns an array of Instruction objects, defining all of the operations in the instruction set.

dasm

public java.lang.String dasm(long instrData,
                             AutoModel.Instruction instr)
This method disassembles an instruction.

Parameters:
instrData - The instruction to be diassembled.
instr - The description of the instruction to be diassembled.
Returns:
This method returns a string representing the current instruction.

asmFixup

public void asmFixup(java.lang.String[] tokens,
                     AutoModel.Instruction instr)
This method permits one last chance to manipulate the instruction word before the bits are written out to a file in AsmTool. Overload this method to do any final fixups. Note that typically only the values[] array should be modified.

Parameters:
tokens - The original string tokens from the instruction.
instr - The instruction.

dasmFixup

public void dasmFixup(long instrData,
                      AutoModel.Instruction instr)
This method can be overloaded to fix up any problems or to do any clean-up in the disassembler. This method is called after the data from the instruction has been broken down into the fields in the instr object. Most of what is done here is to adjust the field.val values to take care of disjoint or shifted fields.

Parameters:
instrData - The instruction to be diassembled.
instr - The description of the instruction to be diassembled.

preExecute

public void preExecute(int instr)
                throws MemoryAccessException,
                       IllegalRegisterException,
                       IllegalOpcodeException
This method is called immediately before execution and may be overloaded to do things like save instruction field values to locals.

Parameters:
instr - The instruction
Throws:
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.
IllegalRegisterException - if an attempt is made to access a register illegally.
IllegalOpcodeException - is added for models which may combine decode and execute in the same method (such as AutoModel).

postExecute

public void postExecute(int instr)
                 throws MemoryAccessException,
                        IllegalRegisterException,
                        IllegalOpcodeException
This method is called immediately after execution and may be overloaded to do things like save instruction field values to locals.

Parameters:
instr - The instruction
Throws:
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.
IllegalRegisterException - if an attempt is made to access a register illegally.
IllegalOpcodeException - is added for models which may combine decode and execute in the same method (such as AutoModel).

instrMatch

public AutoModel.Instruction instrMatch(long instr,
                                        int bits)
This method returns the instruction object from the instruction table associated with a given instruction.

Parameters:
instr - the instruction data.
bits - the number of bits in the instruction.
Returns:
This method returns the instruction object from the instruction table associated with a given instruction.

getInstructionSizes

public int[] getInstructionSizes()
This method returns an array of integers containing the different instruction sizes in the model. The values are in bits.

Returns:
This method returns an array of integers containing the different instruction sizes in the model. The values are in bits.

fetch

public long fetch(int addr,
                  int bytes)
           throws MemoryAccessException
This method returns a long (64-bit) value based on the data at 'bytes' bytes of memory at address 'addr'. This takes into account endian. This is provided as a public function to permit overloading for processors that may need unusual functionality when fetching instructions (I'm talking about ARC here).

Parameters:
addr - The address of the fetch.
bytes - The number of bytes in the fetch.
Returns:
This method returns a long (64-bit) value based on the data at 'bytes' bytes of memory at address 'addr'.
Throws:
MemoryAccessException - when an attempt is made to access an illegal memory address.

getSymbolNames

public static java.lang.String[] getSymbolNames(AutoModel.Symbol[] symbols)
This method takes in a symbol table and returns an array of strings. Note that for symbols which may map to two or more values, the last entry in the table for that value is the string returned. This is useful for getting an array of strings used in defineRegisterNames().

Parameters:
symbols - An array of Symbols.
Returns:
this method returns an array of strings representing the symbols.

signExtend

public static int signExtend(int a,
                             int bits)
This method is used to take a value 'a' of bit length 'bits' and sign extend it, making it a proper signed integer value.

Parameters:
a - The value to be sign extended.
bits - the number of bits in the input value.
Returns:
this method returns the value a, signed extended for a given number of bits.

signExtend

public static long signExtend(long a,
                              int bits)
This method is used to take a value 'a' of bit length 'bits' and sign extend it, making it a proper signed integer value. This is for 64-bit long integers.

Parameters:
a - The value to be sign extended.
bits - the number of bits in the input value.
Returns:
this method returns the value a, signed extended for a given number of bits.

bitsToBytes

public static int bitsToBytes(int a)
This routine converts a bit count to a byte count, where the byte count is the minimum number of bytes to hold this many bits.

Parameters:
a - The number of bits.
Returns:
This method returns the number of bytes necessary to hold the number of bits in the input parameter.

toHexString

public static java.lang.String toHexString(byte[] b)
This method converts a byte array to a hexaecimal string.

Parameters:
b - the byte array
Returns:
This method converts a byte array to a hexaecimal string.

toHexString

public static java.lang.String toHexString(byte[] b,
                                           int offset,
                                           int byteCount)
This method returns a string representation of a portion of a byte array in hexadecimal.

Parameters:
b - The byte array
offset - The offset into the byte array of the start of data.
byteCount - The number of bytes to be returned as a hexidecimal string.
Returns:
This method returns a string representation of a portion of a byte array in hexadecimal.

concatenate

public static java.lang.String[] concatenate(java.lang.String[] a,
                                             java.lang.String[] b)
This method concatenates two arrays of strings.

Parameters:
a - the first array of strings.
b - the second array of strings.
Returns:
this method returns the concatenated array of the two string arrays.

toLong

public static long toLong(byte[] b,
                          boolean endian)
This method converts a byte array to a long.

Parameters:
b - The byte array. If this araray is longer than eight bytes, the result is undefined.
endian - The endian of the memory.
Returns:
this method converts a byte array to a long.

traceOn

public void traceOn()
This method turns on tracing for model debug.


traceOff

public void traceOff()
This method turns on tracing for model debug.