Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Verilog HDL please comment your code and provide simulation result Design a mach

ID: 2291719 • Letter: V

Question

Verilog HDL


please comment your code and provide simulation result


Design a machine to average 4-bit binary numbers in blocks of eight numbers. Assume that the numbers are all positive. The data is inputted on a 4-bit bus NUM [0:3]. A new 4-bit number is available on the bus synchronous with the falling edge of the system clock. A new average AVE [0:3] is outputted and held after each group of eight input numbers. The circuit should continue operation as long as the control signal ADD is asserted. If the circuit is in the middle of determining an average when ADD is de-asserted, it should finish the average and then stop operation. Simulate and verify correct operation of your design.

Explanation / Answer

module average (
   input [3:0] NUM,
   input        CLK, RST,
   input        ADD,
   output [3:0] AVE
);
reg [3:0] AVE;
reg [4:0] COUNT;

always @ (negedge CLK)
begin
    if (RST)
      begin
         AVE   <= 4'd0;
         COUNT <= 4'd0;
      end
    else
      if (ADD && (COUNT <= 4'd7)) begin
         COUNT <= COUNT + 1'b1;
       AVE <= ({1'b0, NUM[3:1]} + {1'b0, AVE[3:1]});
      end
      else begin
          COUNT <= 4'd0;
          AVE <= AVE;
      end
end

endmodule


module average_tb;

reg [3:0] NUM;
reg         CLK, RST, ADD;
wire [3:0] AVE;

average m1 (.NUM(NUM), .CLK(CLK), .RST(RST), .AVE(AVE));

integer i;

always
#5 CLK = ~CLK;

initial
begin
      CLK = 1'b1;
      RST = 1'b1;
      ADD = 1'b0;
      NUM = 4'd0;
      repeat(2)
      @(posedge CLK);
      RST = 1'b0;
      repeat(2)
      @(posedge CLK);
      for(i = 0; i < 8; i = i + 1) begin
        ADD = 1'b1;
        NUM = i + 4;
       @(posedge CLK);
      end
        ADD = 1'b0;
      repeat(2)
      @(posedge CLK);
      for(i = 0; i < 4; i = i + 1) begin
        ADD = 1'b1;
        NUM = i + 6;
       @(posedge CLK);
      end
        ADD = 1'b0;
      repeat(2)
      @(posedge CLK);
      $finish;
end

initial begin // dump creation for waveform
$dumpfile("dump.vcd");
$dumpvars;
end

endmodule