Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting a module output to a register

Tags:

verilog

I have tried connecting a module output to a register, as follows:

module test
(
  input rst_n,
  input clk,
  output reg [7:0] count
);
  always @(posedge clk or negedge rst_n) begin
    if(!rst_n) begin
      count <= 7'h0;
    end else begin
      if(count == 8) begin
        count <= count;
      end else begin
        count <= count + 1'b1;
      end
    end
  end
endmodule

module test_tb;
    reg clk;
    reg rst_n;
    reg [7:0] counter;

    initial begin
        clk = 1'b0;
        rst_n = 1'b0;
        # 10;
        rst_n = 1'b1;
    end
    always begin
        #20 clk <= ~clk;
    end

    test test1 (
        .rst_n(rst_n),
        .clk(clk),
        .count(counter) /* This is the problematic line! */
    );
endmodule

I got the error "Illegal output or inout port connection for "port 'count'" in ModelSim. Even though the error matches my code, I do not understand why, fundamentally, I cannot connect a module output to a register.

Why can I not connect a module output to a register in Verilog?

like image 292
Randomblue Avatar asked Apr 02 '12 10:04

Randomblue


People also ask

Can output be a wire in Verilog?

reg and wire specify how the object will be assigned and are therefore only meaningful for outputs. If you plan to assign your output in sequential code,such as within an always block, declare it as a reg (which really is a misnomer for "variable" in Verilog). Otherwise, it should be a wire , which is also the default.

Can you assign a wire to a reg in Verilog?

As you can see from the example above, a wire can be assigned a value by an assign statement. Default data type is wire: this means that if you declare a variable without specifying reg or wire, it will be a 1-bit wide wire. Now, coming to reg data type, reg can store value and drive strength.

How do you input in Verilog?

input clk; input [15:0] inp;//dataset output out; . . . inputs are wire type by definition (and for a reason). in the module that is external to your example, the output can be a reg there and that would make sense.

What is a wire in Verilog?

wire elements are simple wires (or busses of arbitrary width) in Verilog designs. The following are syntax. rules when using wires: 1. wire elements are used to connect input and output ports of a module instantiation together with some other element in your design.


1 Answers

You can only assign a value to a reg within a procedural always block. You can not drive a reg from a module instance. That would be a continuous assisgnment.

Use a wire inside test_tb.

like image 155
toolic Avatar answered Nov 15 '22 08:11

toolic