Greetings, I have a question about usage of $signed in verilog 2001. I
had thought this could be used for sign extension and I employed it in various places in my design. But when I simulate with Icarus verilog I
find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits
plus a 0 bit to make it even) to be sign extended so I can branch
backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this
"old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to
do sign extension? How is $signed supposed to be used?
On 4/13/20 4:32 PM, TJ Edmister wrote:
Greetings, I have a question about usage of $signed in verilog 2001. I
had thought this could be used for sign extension and I employed it in various places in my design. But when I simulate with Icarus verilog I
find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits plus a 0 bit to make it even) to be sign extended so I can branch
backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this
"old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to
do sign extension? How is $signed supposed to be used?
I think the issue is that cpupc is probably unsigned, and an unsigned
plus a signed is done as unsigned, so that branchloc2 isn't signed extended.
If you made a 32 bit signed wire that you put brancedloc2 into first
then you could get the results you want.
Maybe you could also do:
cpupc <= $signed(cpupc) + $signed({branchloc2, 1'b0});
On Monday, April 13, 2020 at 8:09:45 PM UTC-4, Richard Damon wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On 4/13/20 4:32 PM, TJ Edmister wrote:
Greetings, I have a question about usage of $signed in verilog 2001. I
had thought this could be used for sign extension and I employed it in
various places in my design. But when I simulate with Icarus verilog I
find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits >>> plus a 0 bit to make it even) to be sign extended so I can branch
backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this
"old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to
do sign extension? How is $signed supposed to be used?
I think the issue is that cpupc is probably unsigned, and an unsigned
plus a signed is done as unsigned, so that branchloc2 isn't signed extended. >>
If you made a 32 bit signed wire that you put brancedloc2 into first
then you could get the results you want.
Maybe you could also do:
cpupc <= $signed(cpupc) + $signed({branchloc2, 1'b0});
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
On 4/13/20 9:08 PM, Rick C wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
I figured it out with a bit of google-fu, where I found this document: <https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
On 4/13/20 4:32 PM, TJ Edmister wrote:
Greetings, I have a question about usage of $signed in verilog 2001. I
had thought this could be used for sign extension and I employed it in
various places in my design. But when I simulate with Icarus verilog I
find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits
plus a 0 bit to make it even) to be sign extended so I can branch
backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this
"old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to
do sign extension? How is $signed supposed to be used?
I think the issue is that cpupc is probably unsigned, and an unsigned
plus a signed is done as unsigned, so that branchloc2 isn't signed
extended.
If you made a 32 bit signed wire that you put brancedloc2 into first
then you could get the results you want.
Maybe you could also do:
cpupc <= $signed(cpupc) + $signed({branchloc2, 1'b0});
On Mon, 13 Apr 2020 20:09:42 -0400, Richard Damon <Richard@Damon-Family.org> wrote:
On 4/13/20 4:32 PM, TJ Edmister wrote:
Greetings, I have a question about usage of $signed in verilog 2001. I
had thought this could be used for sign extension and I employed it in
various places in my design. But when I simulate with Icarus verilog I
find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits >> plus a 0 bit to make it even) to be sign extended so I can branch
backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this
"old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to
do sign extension? How is $signed supposed to be used?
I think the issue is that cpupc is probably unsigned, and an unsigned
plus a signed is done as unsigned, so that branchloc2 isn't signed extended.
If you made a 32 bit signed wire that you put brancedloc2 into first
then you could get the results you want.
Maybe you could also do:
cpupc <= $signed(cpupc) + $signed({branchloc2, 1'b0});
Thanks for posting. I'm thinking that if it is this finicky maybe I'll use it only for plain old assignments, and then enumerate the bits myself in any expression that involves computation.
On Monday, April 13, 2020 at 9:51:06 PM UTC-4, Richard Damon wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On 4/13/20 9:08 PM, Rick C wrote:
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
entering all the bits?
I figured it out with a bit of google-fu, where I found this document:
<https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
Great guide. The one on sign extending literals is pathological. I like that they tell you to avoid this problem to "learn Verilog's sign extension rules!" but don't tell you how to do it correctly. I guess there is no "correct" way other than
I'm seven items into the list and it's getting a bit deep for my waders. I'll go back to this when I sit down to learn Verilog. There's one guy I know who swears up and down everyone who learns Verilog will prefer it and be more productive. So oneday I will give it a serious try.
Thanks.
On 4/13/20 10:54 PM, Rick C wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On Monday, April 13, 2020 at 9:51:06 PM UTC-4, Richard Damon wrote:
On 4/13/20 9:08 PM, Rick C wrote:
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
entering all the bits?
I figured it out with a bit of google-fu, where I found this document:
<https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
Great guide. The one on sign extending literals is pathological. I like that they tell you to avoid this problem to "learn Verilog's sign extension rules!" but don't tell you how to do it correctly. I guess there is no "correct" way other than
Yes, because the literal is ALWAYS zero extended to its declared size.
Sign extension of the value only happens after that, and only if the operation will be done as a signed operation. You only get signed
operations if ALL the signals in the expression are signed.
On Tuesday, April 14, 2020 at 2:58:12 PM UTC-4, Richard Damon wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On 4/13/20 10:54 PM, Rick C wrote:
On Monday, April 13, 2020 at 9:51:06 PM UTC-4, Richard Damon wrote:
On 4/13/20 9:08 PM, Rick C wrote:
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
entering all the bits?
I figured it out with a bit of google-fu, where I found this document: >>>> <https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
Great guide. The one on sign extending literals is pathological. I like that they tell you to avoid this problem to "learn Verilog's sign extension rules!" but don't tell you how to do it correctly. I guess there is no "correct" way other than
zeros are appended it's too late anyway.
Yes, because the literal is ALWAYS zero extended to its declared size.
Sign extension of the value only happens after that, and only if the
operation will be done as a signed operation. You only get signed
operations if ALL the signals in the expression are signed.
Not sure I get what you are saying. After there are zeros appended to match sizes, there is no extension. What do you mean by "Sign extension of the value only happens after that"??? It looks like there is never automatic sign extension. Once the
On 4/14/20 6:17 PM, Rick C wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On Tuesday, April 14, 2020 at 2:58:12 PM UTC-4, Richard Damon wrote:
On 4/13/20 10:54 PM, Rick C wrote:
On Monday, April 13, 2020 at 9:51:06 PM UTC-4, Richard Damon wrote:
On 4/13/20 9:08 PM, Rick C wrote:
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
entering all the bits?
I figured it out with a bit of google-fu, where I found this document: >>>> <https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
Great guide. The one on sign extending literals is pathological. I like that they tell you to avoid this problem to "learn Verilog's sign extension rules!" but don't tell you how to do it correctly. I guess there is no "correct" way other than
zeros are appended it's too late anyway.
Yes, because the literal is ALWAYS zero extended to its declared size.
Sign extension of the value only happens after that, and only if the
operation will be done as a signed operation. You only get signed
operations if ALL the signals in the expression are signed.
Not sure I get what you are saying. After there are zeros appended to match sizes, there is no extension. What do you mean by "Sign extension of the value only happens after that"??? It looks like there is never automatic sign extension. Once the
If you take 4'sb1111 + 3'sb111 then the 3 ones will be extended to 4
bits with sign extension, or if two signed vectors get operated on
together, the smaller will get extended to match the size of the larger.
(Or possibly bigger, as the size of the result can also cause the fields
to get extended).
In a literal of the form x'sby, if the value y takes less than x bits,
it is zero extended to that size, and THEN if that literal needs to be expanded to be used in the expression, the MSB of that value is check to decide sign extension.
On Tuesday, April 14, 2020 at 6:46:46 PM UTC-4, Richard Damon wrote:language uses so as to fully understand what to expect? I've asked about this a number of times and no one seems to know of a good reference.
On 4/14/20 6:17 PM, Rick C wrote:
On Tuesday, April 14, 2020 at 2:58:12 PM UTC-4, Richard Damon wrote:
On 4/13/20 10:54 PM, Rick C wrote:
On Monday, April 13, 2020 at 9:51:06 PM UTC-4, Richard Damon wrote: >>>>>> On 4/13/20 9:08 PM, Rick C wrote:
This is the sort of issue I have been warned about with Verilog. I would like to learn the details so I can use the language without fighting issues like this. Is there a good book on the language that explains all the default assumptions the
entering all the bits?
I figured it out with a bit of google-fu, where I found this document: >>>>>> <https://sutherland-hdl.com/papers/2006-SNUG-Boston_standard_gotchas_presentation.pdf>
Which explains a number of traps like that.
Great guide. The one on sign extending literals is pathological. I like that they tell you to avoid this problem to "learn Verilog's sign extension rules!" but don't tell you how to do it correctly. I guess there is no "correct" way other than
zeros are appended it's too late anyway.
Yes, because the literal is ALWAYS zero extended to its declared size. >>>> Sign extension of the value only happens after that, and only if the
operation will be done as a signed operation. You only get signed
operations if ALL the signals in the expression are signed.
Not sure I get what you are saying. After there are zeros appended to match sizes, there is no extension. What do you mean by "Sign extension of the value only happens after that"??? It looks like there is never automatic sign extension. Once the
If you take 4'sb1111 + 3'sb111 then the 3 ones will be extended to 4
bits with sign extension, or if two signed vectors get operated on
together, the smaller will get extended to match the size of the larger.
(Or possibly bigger, as the size of the result can also cause the fields
to get extended).
In a literal of the form x'sby, if the value y takes less than x bits,
it is zero extended to that size, and THEN if that literal needs to be
expanded to be used in the expression, the MSB of that value is check to
decide sign extension.
Thank you, that is clear.
I'm not very conversant in Verilog so perhaps you can explain some details.
cpupc <= $signed(cpupc) + $signed({branchloc2, 1'b0});
This works I assume. If branchloc2 is a signed quantity would this also work?
cpupc <= $signed(cpupc) + {branchloc2, 1'b0};
I assume the aggregate would then be signed and would be properly sign extended?
Greetings, I have a question about usage of $signed in verilog 2001. I had thought this could be used for sign extension and I employed it in various places in my design. But when I simulate with Icarus verilog I find that
it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits plus a 0 bit to make it even) to be sign extended so I can branch backward as well as forward... :)
Since this failed in the simulation, I replaced that code with this "old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to do sign extension? How is $signed supposed to be used?
On Monday, April 13, 2020 at 2:32:23 PM UTC-6, TJ Edmister wrote:use this because it's Systemverilog and too many customers whine when you use Systemverilog because it's too "new" (2009).
Greetings, I have a question about usage of $signed in verilog 2001. I had
thought this could be used for sign extension and I employed it in various
places in my design. But when I simulate with Icarus verilog I find that it is not doing the sign extension.
cpupc <= cpupc + $signed({branchloc2, 1'b0});
cpupc is a 32-bit value and I wanted the branch offset (which is 10 bits plus a 0 bit to make it even) to be sign extended so I can branch backward
as well as forward... :)
Since this failed in the simulation, I replaced that code with this "old" way of doing sign extension, and this one does work:
cpupc <= cpupc + {{21{branchloc2[9]}}, branchloc2, 1'b0};
I don't like the look of this one quite as much, is it the only way to do sign extension? How is $signed supposed to be used?
I usually use an intermediate sign-extended version of the addend, which is a little clumsy.
Below are three examples of methods that work. I was surprised to find that Richard D's method of converting cpupc to $signed before the addition was successful. Then there is my intermediate sign extension. Last is casting to an integer. I don't
reg [31:0] cpupc;
reg signed [9:0] branchloc2 = -4;
wire signed [$left(cpupc):0] branchloc2_sgnext = branchloc2;
initial begin
// convert cpupc to signed before addition
cpupc = 64; cpupc = $signed(cpupc) + (branchloc2<<<1);
$display("cpupc: %0d", cpupc);
// use sign-extended version of branchloc2
cpupc = 64; cpupc = cpupc + (branchloc2_sgnext<<<1);
$display("cpupc: %0d", cpupc);
// Systemverilog casting to signed integer
cpupc = 64; cpupc = cpupc + (int'(branchloc2))*2;
$display("cpupc: %0d", cpupc);
end
ucli% run;
cpupc: 56
cpupc: 56
cpupc: 56
In the second version, is the assignment to branchloc2_sgnext a regular concurrent statement or is this only executed during initialization?
In the third case w
hat size is an int in Verilog? Is it like VHDL, 32 bits by default? If cpupc were less than 32 bits would that be a problem? If cpupc were more than 32 bits would that be a problem?
In the second version, is the assignment to branchloc2_sgnext a regular concurrent statement or is this only executed during initialization?
branchloc2_sgnext is a continuous assignment so it is automatically updated whenever branchloc2 is.
In the third case w
hat size is an int in Verilog? Is it like VHDL, 32 bits by default? If cpupc were less than 32 bits would that be a problem? If cpupc were more than 32 bits would that be a problem?
I believe an 'int' is always 32 bits, so this case would probably not work if cpupc were bigger than 32 bits.
What would happen if cpupc were smaller than 32 bits? In VHDL the int has a range of a 32 bit word, but it's not really a 32 bit word... it's just an int. To add an int to a signed or unsigned it has to be converted (in the "+" definition) to a sizematching the signed or unsigned.
It's unclear to me how ints are handled in Verilog.
size matching the signed or unsigned.What would happen if cpupc were smaller than 32 bits? In VHDL the int has a range of a 32 bit word, but it's not really a 32 bit word... it's just an int. To add an int to a signed or unsigned it has to be converted (in the "+" definition) to a
conversions.It's unclear to me how ints are handled in Verilog.
I think an 'int' is identical to a 32-bit signed reg. If cpupc were less than 32 bits, I don't think there would be a problem. The extra upper bits in the int will just be truncated. In Verilog, you can sum together things of different sizes with no
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 56:21:24 |
Calls: | 2,471 |
Files: | 8,638 |
Messages: | 1,898,012 |