-1
timescale 1ns/10ps

/* resource counter for nor gates
 */
module global_vars;
  integer count;
endmodule

module my_nor(y, a, b);
  output y;
  input a, b;

  global_vars gv;

  /* at instantiation increment the resources used */
  gv =gv +1;

  /* add 2ns inherent delay */
  nor #2 nor1(y,a,b); 
endmodule

When I compile, there is one syntax error at global_vars gv; I have no idea. Should I initial the gv?

2
  • 1
    The variable defined in module global_vars appears to be count, but the one you reference in my_nor appears to be gv. If this observation is valid, then you either you need to use count in my_nor or you need to rename count in global_vars or you need to add gv to global_vars. Mar 20, 2015 at 4:25
  • I have tried to rename it, it didn't work. Still error. I have also used count, error either. Mar 20, 2015 at 14:02

2 Answers 2

0

In verilog you can not just do this:

 gv = gv +1;

wire types need to use assign:

 wire   gv;
 assign gv = a + b;

reg types can use initial, always @* or always @(posedge clk).

always @* begin
  gv = a + b;
end

Your trying to use an instance like a variable, I am not sure what your trying to do with your global_vars, may be make a global variable but creating an instance would make it local not global. Here you would do just as well to make gv an integer rather than an instance.

Note

Wire assignment and always @* are combinatorial, is there is no time delay in the assignment, therefor the value can not be directly referenced to itself. For example

gv = gv + 1;

Is a combinatorial loop, when do you expect the +1 to happen. This is normally solved by making gv a flip-flop and updating its value on a clock edge:

always @(posedge clk) begin
  gv <= gv + 1;
end

In this case you still need to set an initial value for gv. for FPGAs this can be done using an initial or an async reset for ASIC.

FPGA using initial:

initial begin
  gv = 'b0;
end
always @(posedge clk) begin
  gv <= gv + 1;
end

Or for ASIC using reset:

always @(posedge clk or negedge rst_n) begin
  if (~rst_n) begin
    gv <= 'b0;
  end
  else begin
    gv <= gv + 1;
  end
end
0
0

It looks like you are trying to embed a reference to a global variable within a synthesizable module so that you can count how many times that module has been instantiated. As @Morgan has already pointed out, you can't reference a module instantiate as a variable. But there is an even more fundamental problem -- you are trying to mix behavioral code (i.e. global variables) into synthesizable code and that just doesn't work.

Creating a synthesized module is really a two step process -- design and then coding. In the design phase, you figure out what kind of logic you are trying to create. You don't need to go down to the gate level, but you should be able to sketch out the function with clouds of combinatorial logic, registers, FIFOs, memories etc. Once you've gained some familiarity with the language, this may strictly be a mental process rather than something committed to paper, but you still need to think about the logic you are creating. Then once you have designed your circuit, you actually go write the code to implement your design.

Global variables do not exist in hardware. You can make a counter that is accessible by multiple modules, but but it would need some sort of arbitration process. But what you're trying to do is create a run-time counter that counts up the number of modules instantiated, for which there is no hardware analog. You could use 'define statements to instantiate a variable number of modules and have a signal from each module which is defined as '1' if the module is instantiated and '0' if it isn't and then count the number of '1's, but that is self defeating since you already had to define a macro telling you the number of modules before you instantiated them! In which case, you might as well just use the macro value directly and dispense with all the extra signals.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.