EmbDev.net

Forum: FPGA, VHDL & Verilog Verilog For Counter: How to store 32 bit counter values as 4 8-bit registers ?


von Saraswathy S. (saras015)


Rate this post
useful
not useful
Hi,

  I am new to HDL language. I have written and compile basic Counter 
Verilog code. But i need store 32 bit counter values as 4 8-bit 
registers ? Please someone help me


module counter
#(parameter WIDTH=8)
(
  input clk, enable, rst_n,
  output reg [WIDTH-1:0] count
);

  always @ (posedge clk or negedge rst_n)
  begin
    if (~rst_n)
      count <= 0;
    else if (enable == 1'b1)
      count <= count + 1;
  end
endmodule

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Saraswathy S. wrote:
> 32 bit counter
There is no 32 bit counter in that design.

> But i need store 32 bit counter values as 4 8-bit registers ?
Where do you need to store the counter? Whats your actual problem? Whats 
the original text of your exercise?

von Saraswathy S. (saras015)


Rate this post
useful
not useful
Thanks for your response.

Sorry. Trying for 32-bit counter
1
#(parameter WIDTH=32)
.

I need to store like below
1
output reg [7:0]  Data_out_0,
2
      output reg [7:0]  Data_out_1,
3
      output reg [7:0]  Data_out_2,
4
      output reg [7:0]  Data_out_3,
.

 Any logic in Verilog, like switch cases.

von Vancouver (Guest)


Rate this post
useful
not useful
Define a local 32bit counter register and connect your Data_out_x to the 
corresponding bytes of this register. Use 'logic' or 'wire' instead of 
'reg' type for the outputs.

von Saraswathy S. (saras015)


Rate this post
useful
not useful
Then how should i include my counter logic.

1
 module counter
2
        #(parameter WIDTH=32)
3
        (
4
        input clk, enable, rst_n,
5
        output reg [WIDTH-1:0] count
6
         );
7
         output wire [7:0]  Data_out_0,
8
        output wire [7:0]  Data_out_1,
9
        output wire [7:0]  Data_out_2,
10
        output wire [7:0]  Data_out_3,

 Please tell me how to complete this process

von Andy (Guest)


Rate this post
useful
not useful
Make the 32bit counter as in your first post, and then assign the right 
portion of the counter to the output byte 0..3. Here the second byte, 
the others are up to you:
1
   ...
2
   assign Data_out_1 = count[15:8];
3
   ...

You don't need to declare the count reg as output, just make it a local 
register.

von Saraswathy S. (saras015)


Rate this post
useful
not useful
Please check now whether my code is right.
1
      module counter
2
      (
3
        input clk, enable, rst_n,
4
        output [31:0] count,
5
      );
6
        output wire [7:0]  Data_out_0;
7
        output wire [7:0]  Data_out_1;
8
        output wire [7:0]  Data_out_2;
9
        output wire [7:0]  Data_out_3;
10
11
        assign Data_out_0 = count[7:0];
12
        assign Data_out_1 = count[15:8];
13
        assign Data_out_2 = count[23:16];
14
        assign Data_out_3 = count[31:24];
15
16
        always @ (posedge clk or negedge rst_n)
17
        begin
18
        if (~rst_n)
19
        count <= 0;
20
        else if (enable == 1'b1)
21
        count <= count + 1;
22
        end
23
        endmodule

von Mark L. (markl)


Rate this post
useful
not useful
I started verilog two weeks ago, so my example may not be the best.
Actually, if it is wrong or could be improved, PLEASE suggest.

The code is modified to emphasize the fact that it does what you need. 
The counter is not starting at 0, nor is it reset, but that is on 
purpose. Read the comments and change whatever you need.

This is what I would do:
1
module top();
2
  wire [7:0] my_reg1;
3
  wire [7:0] my_reg2;
4
  wire [7:0] my_reg3;
5
  wire [7:0] my_reg4;
6
  wire update;
7
  reg clk= 0,enable = 1 ,rst_n = 1;
8
9
  counter count(clk,enable,rst_n,update,my_reg1,my_reg2,my_reg3,my_reg4);
10
11
  always @ (clk)// simplified clock
12
    begin
13
      #1 clk <= ~clk;
14
      $display("CLOCK");
15
    end
16
17
  always @( update) // replace with the store logic
18
/* actually, maybe we should react to event on enable but I decided otherwise for this contrived example*/
19
    begin
20
      if (update == 1)
21
        begin
22
          $display("-->> STORE THIS %h %h %h %h",my_reg1, my_reg2, my_reg3, my_reg4);
23
        end
24
    end
25
26
  initial
27
    begin
28
      #20 $display("FINAL PHASE");
29
      #1 $display("%d %d %d %d",my_reg1, my_reg2, my_reg3, my_reg4);
30
      #1 $finish;
31
    end
32
33
endmodule
34
35
module counter
36
  #(parameter WIDTH=32, WIDTH2=8)
37
  (
38
    input clk, enable, rst_n, 
39
    output wire update,
40
    output wire [WIDTH2-1:0] my_reg1,
41
    output wire [WIDTH2-1:0] my_reg2,
42
    output wire [WIDTH2-1:0] my_reg3,
43
    output wire [WIDTH2-1:0] my_reg4
44
  );
45
46
  /* Could have been the cleaner "reg [7:0] my_arr[4];" but it requires that you change the output and references accordingly
47
  */
48
  reg [WIDTH2-1:0] lmy_reg1;
49
  reg [WIDTH2-1:0] lmy_reg2;
50
  reg [WIDTH2-1:0] lmy_reg3;
51
  reg [WIDTH2-1:0] lmy_reg4;
52
53
  reg [WIDTH-1:0] count = 32'h11_22_33_44; /* force it at an arbitrary value. Deliberately ignoring rst instance. Modify it to your taste.*/
54
  reg lupdate = 0;
55
56
  assign my_reg1 = lmy_reg1;
57
  assign my_reg2 = lmy_reg2;
58
  assign my_reg3 = lmy_reg3;
59
  assign my_reg4 = lmy_reg4;
60
  assign update = lupdate;
61
62
  always @ (posedge clk or negedge rst_n)
63
    begin
64
      lupdate <= 0;
65
      if (~rst_n)
66
        begin
67
          $display("reset on");
68
          count <= 0;
69
        end
70
      else if (enable == 1'b1) // in production code "enable" should drive the always block in top module, not the "update" variable.
71
        begin
72
          $display("count add 1");
73
74
          count <= count + 32'h11_11_11_11; // replace it with
75
          lmy_reg1 <= count[31:24];
76
          lmy_reg2 <= count[23:16];
77
          lmy_reg3 <= count[15:8];
78
          lmy_reg4 <= count[7:0];
79
          lupdate <= 1;
80
        end
81
    end
82
83
endmodule

Again, I am a beginner too in Verilog.

: Edited by User
von Andy (Guest)


Rate this post
useful
not useful
Saraswathy S. wrote:
> Please check now whether my code is right.

Not quite. The Synthesis tool will give you a few errors.

You need to declare all input and output ports inside the first 
parenthesis section.
You also have the Data_out_0..3 as output ports, so they have to go 
there.

If you don't need to access the count from outside the module, declare 
it after the parenthesis as follows:
1
module counter
2
   (
3
      ...    // in-out ports here
4
   );
5
   reg [31:0] count;    // a register used only inside the module 
6
   ...

von Marcus H. (mharnisch)


Rate this post
useful
not useful
You could check if your tool supports SystemVerilog and change the port 
declaration like this:
1
output reg [WIDTH/8-1:0][7:0] count

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.