1

I am starting in FPGA coding, and one of my first exercises is to code a full adder. I am using SystemVerilog to code within Vivado. My take on this is as follow:

Source:

`timescale 1ns/10ps
module challenge
  (
   input  wire  [2:0]    SW,
   output logic [1:0]    LED
   );
  assign LED[0]=SW[1]^SW[0]^SW[2]; // Write the code for the Sum
  assign LED[1]=(SW[1]&SW[0])|((SW[1]^SW[0])&SW[2]); // Write the code for the Carry
endmodule // challenge

TestBench:

`timescale 1ns/100ps
module tb;

  logic [2:0] SW;
  logic [1:0] LED;

  challenge u_challenge (.*);

  // Stimulus
  initial begin
    $printtimescale(tb);
    SW = '0;
    for (int i = 0; i < 8; i++) begin
      $display("Setting switches to %3b", i[2:0]);
      SW  = i[2:0];
      #100;
    end
    $display("PASS: logic_ex test PASSED!");
    $stop;
  end

  logic [2:0] sum;
  assign sum = SW[0] + SW[1] + SW[2];
  // Checking
  always @(SW, LED) begin
    if (sum !== LED[0]) begin
      $display("FAIL: Addition mismatch");
      $stop;
    end
  end 
endmodule // tb

No matter what I put from SW into LED (any combination of SW[0],SW[1],SW[2]), I will always have my LED values set to X. Is there a problem with my definition of SW?

0

2 Answers 2

1

Your definition of SW looks fine.

I removed the $stop calls from your code so the simulation would run to completion. Using $stop halts your simulation and sets it to interactive mode. Perhaps Vivado is waiting for you to enter commands before it updates your output.

I copied your code onto EDA Playground, and it runs fine. I don't see X in the waveforms.

waves

Here is the output I see:

Setting switches to 001
FAIL: Addition mismatch
Setting switches to 010
Setting switches to 011
FAIL: Addition mismatch
FAIL: Addition mismatch
Setting switches to 100
FAIL: Addition mismatch
Setting switches to 101
FAIL: Addition mismatch
FAIL: Addition mismatch
Setting switches to 110
FAIL: Addition mismatch
Setting switches to 111
FAIL: Addition mismatch
FAIL: Addition mismatch
PASS: logic_ex test PASSED!
Sign up to request clarification or add additional context in comments.

1 Comment

Silly mistake from my end. Removing those stop did the trick.Thanks a lot
-3

I think you can add some wait times before the check.

  // Checking
  always @(SW, LED) begin
    #50;
    if (sum !== LED) begin
      $display("FAIL: Addition mismatch");
      $stop;
    end
  end 

1 Comment

This does not answer the question. It has the same problem as in the question. Did you see the other answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.