This is assignment 6 and each assignment builds off of eachother, I attached my other assignments along with the code so far, there are 4 parts to this. ICSI404 – Assignment 6 – build on the computer...

1 answer below »

This is assignment 6 and each assignment builds off of eachother, I attached my other assignments along with the code so far, there are 4 parts to this.


ICSI404 – Assignment 6 – build on the computer


This assignment builds on the previous assignment.


Part 1 – The PC


Programs are stored in memory. As you know, memory requires an address to know where to fetch from. We need to create a “tracker” in the computer so that it knows where to load the next part of the program from. Create a longword in your computer called “PC” (program counter); it should default to 0. With this complete, we can start loading instructions.


In your fetch() method, read the next longword from memory using the PC as the address. We will need this value later, so make a “currentInstruction” longword and store the value there. Use a rippleAdder to increment the PC by 2 (the size of an instruction, for now).



Part 2 – The Registers


As we have discussed in class, accessing memory is expensive. We want to store our “local variables” of our program in memory that is closer to the CPU and quicker to work with. Of course, that memory is expensive, so we can’t have a lot. Create an array of 16 longwords; these will be our registers.



Part 3 – Understand instructions


Our instructions will (eventually) look like this:


add R1 R2 R3 // Add R1 to R2 and put the result in R3


This breaks down:


4 bits for the instruction (add) (1110 – this comes from our ALU project)


4 bits for each of the registers (1,2,3). A total of 16 bits (2 bytes – note the increment in PC in part 1). In order, these would be: 0001 0010 0011


Our whole instruction in bits is: 1110 0001 0010 0011


This allows us to add (or perform any of the ALU operations) on the registers.



Part 4 – Decode, Execute and Store


In the decode process, we get the values from the registers and prepare for the ALU. This seems odd to us as programmers – register[index] is very simple. Why not just put that into the execute function?


Two reasons – one is that it isn’t as simple in hardware – it takes time that execute() needs to do the work. The second is that a register can only do one thing at a time and we will be updating the register in store(). Consider an instruction like add R1 R2 R1 (the same as R1 += R2). Store needs to have clear access to R1.



In decode, examine currentInstruction to determine which 2 registers are our source. This will require shifting and masking and is a little complex. Create two new longwords in our CPU (op1 and op2). Copy the data from the registers indicated by the instruction into op1 and op2.


In execute, pass the control bits (the opcode) into the ALU along with op1 and op2. Create a new longword called result and put the result from the ALU into the result longword.


In store, copy the value from the result longword into the register indicated by the instruction.


This code is not testable yet – we have no output and no way to get values INTO the registers. Every operation will return 0 right now.





You must submit buildable .java files for credit.

































































Rubric



Poor



OK



Good



Great



Comments



None/Excessive (0)



“What” not “Why”, few (5)



Some “what” comments or missing some (7)



Anything not obvious has reasoning (10)



Variable/Function naming



Single letters everywhere (0)



Lots of abbreviations (5)



Full words most of the time (8)



Full words, descriptive (10)



PC



None (0)



Exists


(7)



Exists and is used in fetch (13)



Exists, is used in fetch and is incremented (20)



Registers



None (0)









Exist (10)



Decode



None (0)






Extracts wrong bits or doesn’t copy into OP1/2(10)



Copies both registers into OP1/OP2


(20)



Execute



None (0)






Extracts wrong bits or doesn’t call ALU (10)



Extracts the right bits and calls ALU (20)



Store



None (0)






Extracts wrong bits or doesn’t copy into register(5)



Copies into register (10)








Answered 2 days AfterMar 21, 2021

Answer To: This is assignment 6 and each assignment builds off of eachother, I attached my other assignments...

Sandeep Kumar answered on Mar 24 2021
131 Votes
bit.java
bit.java
public class bit {

    private int value;

    // the getter
    public int getValue() {
        return value;
    }
    // the setter
    public void setValue(int value) {
        this.value = value;
    }

}
computer.java
computer.java
public class computer {
    // All the variable we will need to build a computer

    // Indicate should the while loop be continue running or nor
    bit halted = new bit();

    // The PC. 
    longword PC = new longword();
    longword currentInstruction = new longword();
    // Assignment 10 
    longword SP = new longword();

    // An instance of the memory
    private memory aMemory = new memory();

    // This will hold the operation bit to let excute, store know what to do. 
    bit[] opcode = new bit[4];
    // Part 2(registers) of the assignment (from Assignment 6)
    longword[] Registers = new longword[16];

    // Part 4(decode) of the assignment. This is the op1 and op2.  (From Assignment 6)
    longword op1 = new longword();
    longword op2 = new longword();

    // Part 4(execute) of the assignment, this is the where the result from the ALU will goto. (From Assignment 6)
    longword result = new longword();
    // Assignment 9, part 2, compare operation
    bit isGreater = new bit();
    bit isEqual = new bit();
    /**
     * A while loop that will be running until halted is not zero.
     * 
     * @throws Exception This happens when the user is trying to access a index of memory that is out of bound.
     */
    public void run() throws Exception
    {
        halted.setValue(0);

        // This is a copy of the PC. 
        Impl_Longword forPC = new Impl_Longword();
        // I default the value of copy of PC to zero.
        forPC.set(0);
        // I then change PC so the value is not all NULL.
        PC.setArray(forPC.original.getArray());
        // To set the value of SP to 1020
        Impl_Longword forSP = new Impl_Longword();
        forSP.set(1020);
        SP.setArray(forSP.original.getArray()); // Default the SP to 1020. 

        while(halted.ge
tValue() != 1)
        {
            fetch();
            decode();
            execute();
            store();
        }
    }

    /**
     * This method read the PC and store the next longword of PC pointer of memory
     * and store in currentInstruction. then increment PC by 2.
     * 
     * @throws Exception This happens when the user is trying to access a index of memory that is out of bound.
     */
    public void fetch() throws Exception
    {
        // This is the copy of PC since I don't want to directly touch PC.
        Impl_Longword accessToPC = new Impl_Longword();
        accessToPC.original = PC;

        System.out.println("Current address in memory: " + accessToPC.getSigned());
        currentInstruction = aMemory.read(accessToPC.original);
        // Create a longword which has a value of 2
        Impl_Longword theNumberTwo = new Impl_Longword();
        theNumberTwo.set(2);

        // this longword will be holding for the result of PC after increment by 2
        longword result = new longword();

        // Use a rippleAdder to increment the PC by 2.
        result = rippleAdder.add(PC, theNumberTwo.original);

        // update the value of PC. 
        PC.setArray(result.getArray());
    }

    /**
     * This method detech which operation code the instruction specify and based on
     * the different operation code, this method does different action. 
     * 
     * Halt: Set halted to be 1 and end this function.
     * Move: Does nothing for move. 
     * Interrupt: Does nothing for interrupt.
     * ALU: Store the first value and second value into op1 and op2.
     * Jump: This will jump the current PC to the indicated value in term of byte.
     * Compare: This will compare the value between two register and store the proper result after the comparision.
     * Branches : These are varies branches name which will execute if the condition code is matches. Or else, it will not execute.
     * 
     * @return 0 indicate this method is runned successfully
     */
    public int decode()
    {
        // Create a copy of currentInstruction so that I don't touch the Instruction.
        Impl_Longword copyOfInstruction = new Impl_Longword();
        copyOfInstruction.original= currentInstruction;
        // Create the mask for extracting the right most 4 bit. 
        Impl_Longword maskForRight4Bit = new Impl_Longword();

        // Set the mask to : 00000000 00000000 00000000 00001111
        maskForRight4Bit.set(15);

        // Right shift currentInstruction so that the part I want to extract is in the 
        // right most index. 
        // The opCode part. Getting the first 4 bit.
        Impl_Longword storeShifting = new Impl_Longword();
        storeShifting.original = copyOfInstruction.rightShift(28);
        // Setting the opCode for this instruction.
        readingOpCode(storeShifting.original);
        int decimalOpCode = bitToDecimal(opcode, 0);

        if(decimalOpCode == 0) // Halt operation code.
        {
            halted.setValue(1); 
            // Since halt instruction is receive, this function will end. 
            return 0;
        }
        else if(decimalOpCode == 1) // Move operation code
        {
            // decode don't do anything for move operation.
            return 0;
        }
        else if(decimalOpCode == 2) // Interrupt operation code
        {
            // As assignment 7 specify, decode don't do anything for interrupt instruction. 
            return 0;
        }
        else if(decimalOpCode == 3) // Jump operation code.
        {
            // As assignment 9 specify, decode don't do anything for jump instruction.
            return 0;
        }
        else if(decimalOpCode == 4) // Compare operation code
        {
            // As assignment 9 specify, the store() should be use for compare instruction.
            return 0;
        }
        else if(decimalOpCode == 5) // Branch operation code
        { 
            // Based on assignment 9, decode doesn't do anything for branch.
            return 0;
        }
        else if(decimalOpCode == 6) // Stack related operation code
        {
            bit[] stackCode = new bit[2];
            for(int i = 0; i < stackCode.length; i++)
            {
                bit holder = new bit();
                holder.setValue(currentInstruction.getArray()[4 + i].getValue());
                stackCode[i] = holder;
            }
            int stackOp = bitToDecimal(stackCode, 0);
            if(stackOp == 0) // Push operation code
            {
                // I think it reasonable to let store() for handling the Stack-related operation
                return 0;
            }
            else if(stackOp == 1) // Pop operation code
            {
                // I think it reasonable to let store() for handling the Stack-related operation
                return 0;
            }
            else if(stackOp == 2) // Call operation code
            {
                // I think it reasonable to let store() for handling the Stack-related operation
                return 0;
            }
            else if(stackOp == 3) // Return operation code
            {
                // I think it reasonable to let store() for handling the Stack-related operation
                return 0;
            }
            return 0;
        }
        else if(decimalOpCode > 6) // ALU operation code
        {  // Assignment 6 code, with some renovation for easy to read purpose. 
            // Shifting the instruction where R3 is at the rightest index.
            storeShifting.original = copyOfInstruction.rightShift(20);
            // Mask the last 4 bit, ex) 000000...000...0000XXXX
            longword extract4Bit = storeShifting.and(maskForRight4Bit.original);
 
            // I want to change the longword representation of register index to 
            // decimal representation. I will be using the getSigned() function.
            Impl_Longword longwordToDecimal = new Impl_Longword();
            longwordToDecimal.original.setArray(extract4Bit.getArray());
            int registerIndex = longwordToDecimal.getSigned();
            // Final steps, store the value at register's index into the correct op (op2).
            op2.setArray(Registers[registerIndex].getArray());
            // A repeat of the above step but with different parameter. The steps and reasons
            // behind each code is identical so look at the top code for reference.
            // Shifting the instruction where R2 is at the rightest index.
            storeShifting.original = copyOfInstruction.rightShift(24);
            extract4Bit = storeShifting.and(maskForRight4Bit.original);
            longwordToDecimal.original.setArray(extract4Bit.getArray());
            registerIndex = longwordToDecimal.getSigned();
            op1.setArray(Registers[registerIndex].getArray());
        }
        return 0;
    }

    /**
     * This is where most action will be happening. This method will do different action based on the 
     * different operation code.
     * Halt: Automatic end this function.
     * Move: Save the value that will be store into op1.
     * Interrupt: This will print out either the registers or the memory based on type of interrupt.
     * ALU: Call the ALU class to perform the calculation and save the result for storing.
     * Jump: This will jump the current PC to the indicated value in term of byte.
     * Compare: This will compare the value between two register and store the proper result after the comparision.
     * Branches : These are varies branches name which will execute if the condition code is matches. Or else, it will not execute.
     * 
     * @throws Exception This exception is throw when user is trying to access a index of memory that is out of bound.
     * @return 0 This happen when either halt or interrupt is encountered.
     * @return 1 This happen when either move ALU is encountered and successfully performed. 
     */
    public int execute() throws Exception 
    {
        // Check to see which operation instruction received. 
        int decimalOpCode = bitToDecimal(opcode, 0);
        // Create a copy of currentInstruction so that I don't touch the Instruction.
        Impl_Longword copyOfInstruction = new Impl_Longword();
        copyOfInstruction.original = currentInstruction; 
        // Create the mask for extracting the right most 4 bit. 
        Impl_Longword maskForRight4Bit = new Impl_Longword();
        // Set the mask to : 00000000 00000000 00000000 00001111
        maskForRight4Bit.set(15);
        if(decimalOpCode == 0) // Halt operation code.
        {
            // Since halt instruction is receive, this function will end. 
            return 0;
        }
        else if(decimalOpCode == 1) // Move operation code
        {
            // left shift currentInstruction so that the part I want to extract is in the 
            // left most index. 
            // The getting value part. Getting the value that will be move to register.
            Impl_Longword storeShifting = new Impl_Longword();
            storeShifting.original = copyOfInstruction.leftshift(8);
            // Create a bit array, size 8, that will hold the value that will be move.
            bit[] size8Binary = new bit[8];
            // This for loop will copy the left-shifted instruction into the bit array which will hold 
            // the value of the number that will be move. 
            for(int i = 0; i < 8; i++)
            {
                bit aNewBit = new bit();
                aNewBit.setValue(storeShifting.original.getArray()[i].getValue());
                size8Binary[i] = aNewBit;
            }
            // This is the decimal value of the longword value.
            int bitArrayToDecimal = bitToDecimal(size8Binary, 1);
            // Set the longword's value to represent the decimal.
            Impl_Longword longwordRep = new Impl_Longword();
            longwordRep.set(bitArrayToDecimal);
            // This is the final step, which store the value into the op1. 
            op1.setArray(longwordRep.original.getArray());
        }
        else if(decimalOpCode == 2) // Interrupt operation code
        {
            // Shifting the instruction where R4 is at the rightest index.
            Impl_Longword storeShifting = new Impl_Longword();
            storeShifting.original = copyOfInstruction.rightShift(16);
            // Mask the last 4 bit, ex) 000000...000...0000XXXX
            longword extract4Bit = storeShifting.and(maskForRight4Bit.original);
            // I want to change the longword representation of register index to 
            // decimal representation. I will be using the getSigned() function.
            Impl_Longword longwordToDecimal = new Impl_Longword();
            longwordToDecimal.original.setArray(extract4Bit.getArray());
            int interruptType = (int)longwordToDecimal.getUnsigned();
            // This is the last index of the interrupt is 0
            if(interruptType == 0)
            {
                // This longword will be use mainly for printing out the data.
                Impl_Longword aPrinter = new Impl_Longword();
                System.out.println("Printing the registers\n");
                // A loop that goes over each registers check if it null or not
                for(int i = 0; i < Registers.length; i++)
                { 
                    if(Registers[i] == null)
                    {
                        // If the register is null, then tell user this register is null
                        System.out.println("Registers[" + i + "]: null" );
                    }
                    else
                    {
                        // If the registers is not null, then call the printer to print out the register.
                        System.out.println("Registers[" + i + "]: ");
 
                        aPrinter.printBit(Registers[i].getArray());
                    }
                }
                // skip a line.
                System.out.println();
            }
            else if(interruptType == 1) // This is if the last index of interrupt is 1
            {
                System.out.println("Printing the memory...\n");
                // For printing out the memory.
                Impl_Longword aPrinter = new Impl_Longword();
                // For accessing different address of memory.
                Impl_Longword address = new Impl_Longword();
                Impl_Longword numberFour = new Impl_Longword();
                // Initialize address.
                address.set(0);
                // Use for increment the index of memory by 4 byte.
                numberFour.set(4);
                // A loop that goes voer the first 40 byte of memory and prints them out.
                for(int i = 0; i <= 40; i = i+4)
                {
                    aPrinter.printBit(aMemory.read(address.original).getArray());
                    address.original = rippleAdder.add(address.original, numberFour.original);
                }
                address.set(1004);
                System.out.println("\nPrinting the stack...\n");
                System.out.print("...");
                for(int i = 0; i < 20; i = i+4)
                {
                    aPrinter.printBit(aMemory.read(address.original).getArray());
                    address.original = rippleAdder.add(address.original, numberFour.original);
                }
                System.out.println();
            }
        }
        else if(decimalOpCode == 3) // Jump operation code
        {
            // As assignment 9 specify, execute don't do anything for jump instruction. 
            return 0;
        } 
        else if(decimalOpCode == 4) // Compare operation code
        {
            // As assignment 9 specify, store() should be operating the compare instruction.
            return 0;
        }
        else if(decimalOpCode == 5) // Branch operation code
        {
            // Branch If Equal          - 01
            // Branch Not Equal         - 00
            // Branch Greater Than      - 10
            // Greater Than or Equal    - 11 
            bit signs = copyOfInstruction.original.getArray()[6];
            bit checkEqual = copyOfInstruction.original.getArray()[5];
            bit checkGreater = copyOfInstruction.original.getArray()[4];
            // Set the mask to ...00 0000 0001 1111 1111
            Impl_Longword maskFor9Bit = new Impl_Longword();
            maskFor9Bit.set(-1);
            maskFor9Bit.original = maskFor9Bit.rightShift(23);
            Impl_Longword shiftForInstruction = new Impl_Longword();
            shiftForInstruction.original = copyOfInstruction.rightShift(16);
            if(checkEqual.getValue() == isEqual.getValue()) // Compare if result is equal or not equal. 
            { 
                // This will satisfy branch Equal and branch Not Equal
                Impl_Longword store9Bit = new Impl_Longword();
                store9Bit.original = shiftForInstruction.and(maskFor9Bit.original);
                if(signs.getValue() == 1)
                {
                    Impl_Longword changeToNeg = new Impl_Longword();
                    changeToNeg.original = store9Bit.not();
                    Impl_Longword numberOne = new Impl_Longword();
                    numberOne.set(1);
                    Impl_Longword resultNum = new Impl_Longword();
                    resultNum.original = rippleAdder.add(changeToNeg.original, numberOne.original);
                    result.setArray(resultNum.original.getArray());
                    return 0;
                }
                else if(signs.getValue() == 0)
                {
                    //result = store9Bit.original;
                    result.setArray(store9Bit.original.getArray());
                    return 0;
                }
            }
            else if(checkGreater.getValue() == isGreater.getValue())
            {
                 // Check for if the greater than condition match. 
 
 
                    // This will satisfy branch Greater than and branch Greater than or Equal
                    Impl_Longword store9Bit = new Impl_Longword();
                    store9Bit.original = shiftForInstruction.and(maskFor9Bit.original);
                    if(signs.getValue() == 1)
                    {
                        Impl_Longword changeToNeg = new Impl_Longword();
                        changeToNeg.original = store9Bit.not();
 
                        Impl_Longword numberOne = new Impl_Longword();
                        numberOne.set(1);

                        Impl_Longword resultNum = new Impl_Longword();
                        resultNum.original = rippleAdder.add(changeToNeg.original, numberOne.original);

                        result.setArray(store9Bit.original.getArray());
                        return 0;
                    }
                    else if(signs.getValue() == 0)
                    {
                        result.setArray(store9Bit.original.getArray());
                        return 0;
                    }
 
            } 
            else
            {
                // Lets said Rx and Ry is equal to each other. If Program runs into this part of code means, 
                // 1) The condition code for equal is "not equal" but the result from "compare" is equal. The Equals don't match.
                // 2) Since the equals don't match, it is default that this branch can not be branch into. 
                // Let's said Rx is less than Ry. This will make the compare to produce 
                // bit Equal = 0    bit greaterThan = 0
                // 1) Let's said the condition code for equal and the result from compare do match, they both are 0. 
                // 2) This will lead us into checking the greater than code, which, if run into this part of code means: 
                //    they don't match. So the condition code from this instruction is 10 but the bit stored in CPU is 00. 
                // 3) Since one said it greater, and other said it lesser, we don't branch in.
                Impl_Longword zero = new Impl_Longword();
                zero.set(0);
                result = zero.original;
                return 0;
            }
            return 0;
        }
        else if(decimalOpCode == 6)
        {
            bit[] stackCode = new bit[2];
            for(int i = 0; i < stackCode.length; i++)
            {
                bit holder = new bit();
                holder.setValue(currentInstruction.getArray()[4 + i].getValue());
                stackCode[i] = holder;
            }
            int stackOp = bitToDecimal(stackCode, 0);
            if(stackOp == 0) // Push operation code
            {
                // I think it reasonable to let store() for handling the Stack-related operation
                return 0;
            }
            else if(stackOp == 1) // Pop operation code
            {
                // I think it reasonable to let store() for handlin...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here