---
# System prepended metadata

title: Lab 10

---

# Lab 10
Name: SUDDULA VINEETH RAGHAVENDRA
Roll No.: CS22B045

---
## Question 1 : ALU

**Code** 
```
module MyALU (
    input [31:0] operand1,
    input [31:0] operand2, 
    input [3:0] alu_control,
    output reg [31:0] alu_result,
    output reg zero_flag
);

    always @(*) begin
        // Operating based on control input
        case(alu_control)
            4'b0000: alu_result = operand1 + operand2;
            4'b0001: alu_result = operand1 - operand2;
            4'b0010: alu_result = operand1 ^ operand2;
            4'b0011: alu_result = operand1 | operand2;
            4'b0100: alu_result = operand1 & operand2;
            4'b0101: alu_result = operand1 << operand2;
            4'b0110: alu_result = operand1 >> operand2;
            4'b0111: alu_result = operand1 >>> operand2;
            4'b1000: alu_result = (operand1 < operand2) ? 1 : 0;
        endcase
        // Setting zero_flag if ALU_result is zero
        zero_flag = (alu_result == 0) ? 1'b1 : 1'b0;
    end

endmodule

module MyALU_Testbench;

    reg [31:0] operand1;
    reg [31:0] operand2;
    reg [3:0] alu_control;
    wire [31:0] alu_result;
    wire zero_flag;

    // Instantiate ALU
    MyALU uut (
        .operand1(operand1),
        .operand2(operand2),
        .alu_control(alu_control),
        .alu_result(alu_result),
        .zero_flag(zero_flag)
    );

    // Stimulus
    initial begin
        operand1 = 32'h0000000A; // Example1 value
        operand2 = 32'h00000005; // Example1 value
        alu_control = 4'b0000;   // Example1 control value
        #10; // Wait for 10 time units
        // Change inputs
        operand1 = 32'h0000000F; // example2 value
        operand2 = 32'h0000000F; // example2 value
        alu_control = 4'b0001;   // example2 control value
        #10; // Wait for 10 time units
        // Add more test cases as needed
        $finish; // End simulation
    end

    // Output display
    always @(alu_result, zero_flag)
        $display("ALU Result = %h, Zero Flag = %b", alu_result, zero_flag);

endmodule

```

**Observations:**
![Screenshot 2024-05-11 205043](https://hackmd.io/_uploads/SJyeTbTGA.png)
