---
# System prepended metadata

title: Lab 9

---

# Lab 9
Name: Aarya
Roll No.: CS22B018

---
## Question 1

**Code and TestBench**
```verilog=
module ALU(input [31:0]a, input [31:0]b,input [1:0]c,output [31:0]o);
    wire [31:0]_W1;
    wire [31:0]_W2;
    wire [31:0]_W3;
    wire [31:0]_W4;
    assign _W3=a+b;
    assign _W4=a-b;
     assign _W1=a&b;
    assign _W2=a|b;
    assign o = (c[0]==1'b0) ? ((c[1]==1'b0) ? _W1 : _W2 ) :
    ((c[1]==1'b0) ? _W3 : _W4 );

endmodule
module ALU_TB();

//Select Lines : 00 - AND, 01 - OR, 10 - ADD, 11 - SUB
reg [31:0]a; reg [31:0]b;
reg [1:0]s;
wire [31:0]out;

ALU Q(.a(a), .b(b), .c(s) , .o(out));


initial
begin
$monitor("a = %d, b = %d,  selectlines : s0 = %b , s1 = %b,  Output = %d ",a,b,s[0],s[1], out);
a = 8'd5; b = 8'd7;  s[1] = 0; s[0] = 0;
#10

a = 8'd3; b = 8'd9;  s[1] = 0; s[0] = 1;
#10

a = 8'd6; b = 8'd3;  s[1] = 1; s[0] = 0;
#10

a = 8'd9; b = 8'd2;  s[1] = 1; s[0] = 1;
#10

a = 8'd9; b = 8'd7;  s[1] = 1; s[0] = 0;
#10

a = 8'd10; b = 8'd8;  s[1] = 0; s[0] = 1;
#10

 a = 8'd11; b = 8'd3;  s[1] = 0; s[0] = 0;
#10

a = 8'd5; b = 8'd2;  s[1] = 1; s[0] = 1;

end

endmodule
```
**Output**

![image](https://hackmd.io/_uploads/r16r-sZlR.png)

## Question 2

**Code and TestBench**
```verilog=
module simpleProcessor(input [7:0] ins,
output reg [7:0]_Out);
    

    reg [7:0]loc;
    reg [2:0]off;
        reg [7:0] memory [255:0]; // 256 bytes of memory
    reg [7:0] regFile [15:0]; // 16 general purpose 8-bit registers
    
    always@(*)
    begin
       
        regFile[0]=8'b00000000;
        regFile[2]=8'b00000111;
         memory[1]=8'b00000011;
        memory[0]=8'b00000010;
        regFile[1]=8'b00000000;
        if(ins[7] == 1'b0)
        begin
            loc = regFile[ins[1:0]];
            off = ins[4:2];
            regFile[ins[6:5]] = memory[loc + off];
            _Out = regFile[ins[6:5]];
        end
        else
        begin
            loc = regFile[ins[1:0]];
            off = ins[4:2];
            memory[loc + off] = regFile[ins[6:5]];
            _Out = memory[loc + off];
        end
    end
endmodule

module Q2bench();

wire [7:0]_Out;

reg [7:0]ins;


simpleProcessor Dut(.ins(ins), ._Out(_Out));
initial
begin

    $monitor(" Output : %b ",_Out);
    
    ins = 8'b00000000;
    #5
    ins = 8'b11000001;
    
end

endmodule
```
**Output**
![image](https://hackmd.io/_uploads/r12_5jbeA.png)

---
## Question 3

**Content of inscount0.log**
```
Count 724514

```
**Code in inscount0.cpp**
```c=

/*
 * Copyright (C) 2004-2021 Intel Corporation.
 * SPDX-License-Identifier: MIT
 */

#include <iostream>
#include <fstream>
#include "pin.H"
using std::cerr;
using std::endl;
using std::ios;
using std::ofstream;
using std::string;

ofstream OutFile;

// The running count of instructions is kept here
// make it static to help the compiler optimize docount
static UINT64 icount = 0;

// This function is called before every instruction is executed
VOID docount() { icount++; }

// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID* v)
{
    // Insert a call to docount before every instruction, no arguments are passed
    INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}

KNOB< string > KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "inscount.out", "specify output file name");

// This function is called when the application exits
VOID Fini(INT32 code, VOID* v)
{
    // Write to a file since cout and cerr maybe closed by the application
    OutFile.setf(ios::showbase);
    OutFile << "Count " << icount << endl;
    OutFile.close();
}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    cerr << "This tool counts the number of dynamic instructions executed" << endl;
    cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */
/*   argc, argv are the entire command line: pin -t <toolname> -- ...    */
/* ===================================================================== */

int main(int argc, char* argv[])
{
    // Initialize pin
    if (PIN_Init(argc, argv)) return Usage();

    OutFile.open(KnobOutputFile.Value().c_str());

    // Register Instruction to be called to instrument instructions
    INS_AddInstrumentFunction(Instruction, 0);

    // Register Fini to be called when the application exits
    PIN_AddFiniFunction(Fini, 0);

    // Start the program, never returns
    PIN_StartProgram();

    return 0;
}

```
**C language Code**
```c=
#include<stdio.h>
int main()
{
    int a[5];
    int eveCount=0;
    for(int i=0;i<5;i++)
    {
        a[i]=i+1;
    }
    for(int i=0;i<5;i++)
    {
        if(a[i]%2==0)
        {
            eveCount++;
        }
    }
    printf("%d\n",eveCount);
}
```
**Content of inscount0.log for code**

```
Count 134339
