6502.org
http://forum.6502.org/

n00b Verilog Questions
http://forum.6502.org/viewtopic.php?f=10&t=2666
Page 1 of 5

Author:  ElEctric_EyE [ Wed Sep 11, 2013 1:50 pm ]
Post subject:  n00b Verilog Questions

Hello, I need to delay a 1-bit signal by 6 cycles. What is a more proper/cleaner way to do the following? I know a multistage shift register would work, but not sure how to code it.
Code:
//delay countflag by 6 cycles      
reg countflag, s1, s2, s3, s4, s5, s6;

always @(posedge clk) begin
   s1 <= countflag;
   s2 <= s1;
   s3 <= s2;
   s4 <= s3;
   s5 <= s4;
   s6 <= s5;
end


TIA

Author:  Arlet [ Wed Sep 11, 2013 2:22 pm ]
Post subject:  Re: n00b Verilog Questions

Your method works, and if you're lucky the tools will synthesize a shift register. But you can also make an explicit shift register yourself:

Code:
reg [5:0] s;

wire q = s[5];

always @(posedge clk)
   s <= { s[4:0], countflag };

Author:  ElEctric_EyE [ Wed Sep 11, 2013 2:58 pm ]
Post subject:  Re: n00b Verilog Questions

I checked the RTL schematic. My code shows multiple FF's in a row which was expected. Your code works, but I cannot see it in the RTL schematic.

I'm puzzled about your code, I would have expected a '<<' or '>>' somewhere. How does the Verilog know that this is a shift register?

Author:  Arlet [ Wed Sep 11, 2013 3:07 pm ]
Post subject:  Re: n00b Verilog Questions

The { } is the concatenation operator. It combines multiple bits. In this case it takes s[4:0] as the most significant bits, and add countflag as the least significant bit. If you write s <= { countflag, s[5:1] } it's a shift register to the right.

Author:  ElEctric_EyE [ Wed Sep 11, 2013 4:26 pm ]
Post subject:  Re: n00b Verilog Questions

Arlet wrote:
...it takes s[4:0] as the most significant bits, and add countflag as the least significant bit....

Ah... Thanks! I was thinking 'replaces' which would result in no action.

Author:  Arlet [ Wed Sep 11, 2013 4:31 pm ]
Post subject:  Re: n00b Verilog Questions

Replacing the least significant bit would look like { s[5:1], countflag }

Author:  ElEctric_EyE [ Wed Sep 11, 2013 4:44 pm ]
Post subject:  Re: n00b Verilog Questions

Excellent! Why then isn't your code showing up in the RTL schematic? Has it been absorbed? Is this the ideal situation when using Verilog?

Author:  Arlet [ Wed Sep 11, 2013 5:00 pm ]
Post subject:  Re: n00b Verilog Questions

I don't know. Are you using the input and output ? If not, it may have been optimized away. I just tried it in ISE, and it shows up in the RTL schematic as 6 FFs. In the technology schematic is shows up as a single slrc16e.

Author:  enso [ Wed Sep 11, 2013 6:43 pm ]
Post subject:  Re: n00b Verilog Questions

Arlet, in your example I imagine you wanted to use q and not countflag... You can make it even simpler without a temporary:
Code:
reg [5:0] s;
always @(posedge clk)
   s <= { s[4:0], s[5] };

and possibly even simpler:
Code:
reg [5:0] s;
always @(posedge clk)
   s <= { s, s[5] };

Verilog should trim the high bit when assigning the 7-bit result to a 6-bit register. I think. I am away from my system to check. Maybe it's an error condition, but verilog is generally pretty permissive.

Of course in the real world to get any data into s you'll have to mux it in somewhere.

Author:  Arlet [ Wed Sep 11, 2013 6:48 pm ]
Post subject:  Re: n00b Verilog Questions

No, countflag is the input, and q is the 6-cycle delayed output.

Author:  enso [ Wed Sep 11, 2013 6:54 pm ]
Post subject:  Re: n00b Verilog Questions

oh, I see, it's just a streaming shift register. My mistake, I was rotating for some reason.

Author:  enso [ Wed Sep 11, 2013 6:56 pm ]
Post subject:  Re: n00b Verilog Questions

I can't remember for sure, but I think verilog allows aggregate lvalues:
Code:
reg  [4:0]s, q;
always @(posedge clk)
   {q,s} <= { s, countflag };

Am I wrong?

[Edited typos]

Author:  enso [ Fri Sep 13, 2013 4:17 am ]
Post subject:  Re: n00b Verilog Questions

OK, here is my favorite way to do this:
Code:
wire q;
SRL16 shifter(q,1,0,1,0,clk,countflag);

This little gem creates a shift register inside a single LUT. 1,0,1,0 is backwards binary 0101 - decimal 5, which creates a tap at the 6th bit.

This method is a little frowned upon since it binds you to Xilinx chips. Personally I am sold, so I write a lot of code like this (I prefer instantiation as it more closely resembles a netlist or a schematic. As I see it, it's more readable for some types of circuits).

Author:  Arlet [ Fri Sep 13, 2013 4:31 am ]
Post subject:  Re: n00b Verilog Questions

My code also creates a shift register inside a single LUT, so it comes down to what you find easier to express.

Author:  enso [ Fri Sep 13, 2013 4:54 am ]
Post subject:  Re: n00b Verilog Questions

To be totally accurate, instantiating guarantees that an SRL16 will be created. Otherwise, it is merely likely that a single SRL16 will be created and not a bunch of flip-flops. It depends on the settings buried in command-line switches and project settings.

An instantiated SRL16 may also be manually placed at a specific location on the chip.

Page 1 of 5 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/