Devanshverilog
Devanshverilog
Assignment – 2
Devansh Singh
210108014
The ALU updates conditional flags that the processor utilises for operations like
condition checking and branching. In this instance, two flags are employed:
1. Zero Flag: Set if all bits in the result data are zero.
2. Carry Out Flag: Set if the addition of the two operands results in a carry out.
Truth Table
The ALU works on 8-bit operands. It supports 8 instructions which are selected by the
3-bit Opcode.
Verilog Module :
The ALU receives a 3-bit Opcode, along with two 8-bit operands labelled Operand1
and Operand2. The outcome of the operation is conveyed through the 16-bit Result
port. Additionally, there are two flags: flagC for carry and flagZ for zero.
Verilog Code for the 8-bit ALU (ALU8bit.v) :
This Verilog code defines an 8-bit Arithmetic Logic Unit (ALU) module that performs
various operations based on a 3-bit Opcode input. The module has inputs Operand1
and Operand2 (both 8 bits), and outputs Result (16 bits), flagC (carry flag), and flagZ
(zero flag).
The `always @` block specifies that the outputs are updated whenever there is a
change in Opcode, Operand1, or Operand2. Inside the block, a `case` statement
selects the operation based on the Opcode, and the corresponding Result and flags
are computed.
This Verilog test bench (ALU8bit_tb) is designed to simulate the behavior of the 8-bit
Arithmetic Logic Unit (ALU) module (ALU8bit). It provides inputs to the ALU module,
monitors its outputs, and includes a stimulus to test different operations.
● reg [2:0] Opcode: 3-bit input for specifying the operation to be performed by the
ALU.
● reg [7:0] Operand1, Operand2: 8-bit inputs representing the operands for ALU
operations.
● wire [15:0] Result: 16-bit output for the result of ALU operations. ● wire
flagC, flagZ: Output wires for carry and zero flags.
Temporary Variable:
● reg [2:0] count: A 3-bit register used as a counter in the for loop.
Instantiation of ALU:
The ALU module (ALU8bit) is instantiated (uut) with connections to inputs and outputs.
Initialization in the initial block:
Initial values are set for Opcode, Operand1, and Operand2.
A delay of 100 ns is introduced to allow for any global reset to complete. Stimulus
Generation:
Stimulus is added to test different operations. In this case, Operand1 is set to 0xAA,
Operand2 is set to 0x55, and a loop is used to cycle through different Opcode values
from 0 to 7.
For each iteration of the loop, the Opcode is set, and a delay of 20 time units (#20) is
added to observe the output.
The test bench is designed to observe the behaviour of the ALU under different
combinations of Opcode, Operand1, and Operand2. The results can be analysed to
ensure that the ALU functions correctly for various operations.
Timing Diagram :