ControlUnit.java
// ControlUnit.java
import java.io.*;
// Class ControlUnit
// -------------------------------------------------------------------
/**
* Application that reads instructions from a text file and displays
* the control word for each one.
*/
public class ControlUnit
{
// generateControlWord()
// -----------------------------------------------------------------
/**
* Decodes a 32-bit instruction and returns the control word for
* it.
*/
public static ControlWord generateControlWord(long instruction)
{
// Decode the instruction
int opcode = (int)((instruction & 0x0FC000000) >> 26);
int rs = (int)((instruction & 0x003E00000) >> 21);
int rt = (int)((instruction & 0x0001F0000) >> 16);
int rd = (int)((instruction & 0x00000F800) >> 11);
int shamt = (int)((instruction & 0x0000007C0) >> 6);
int func = (int)(instruction & 0x00000003F);
int immediate = (int)(instruction & 0x0000FFFF);
int address = (int)(instruction & 0x03FFFFFF);
// Get the control word and return it.
return new ControlWord(opcode, rs, rt, rd, shamt, func, immediate,
address);
}
// main()
// -----------------------------------------------------------------
/**
* For each file named on the command line, read instructions from
* the file, and print the control word for executing each one.
* Lines in the file are structured with the hexadecimal
* representation of the instruction, whitespace, and the assembly
* language represenation of the same instruction, suitable for
* printing.
*/
public static void main(String[] args) throws IOException,
NumberFormatException
{
// Loop over the file names specified as command line arguments.
for (int arg = 0; arg < args.length; arg++ )
{
// Set up to read text lines from a file
BufferedReader br = new BufferedReader(
new FileReader( args[arg] ) );
System.out.println("\nFile: " + args[arg] );
String lineBuf;
// Read and process all lines in the file
while ( (lineBuf = br.readLine()) != null )
{
// Extract the hex representation of the instruction
lineBuf.trim();
System.out.println(lineBuf);
int firstSpace = lineBuf.indexOf(" ");
lineBuf = lineBuf.substring(0, firstSpace);
Long LongVal = Long.decode(lineBuf);
long instruction = LongVal.longValue();
// Get the control word for the instruction and print it.
ControlWord cw = ControlUnit.generateControlWord(instruction);
System.out.println( " " + cw );
}
}
System.exit(0);
}
}