Abstract
Signed operation is due to the need for 2's complement. Therefore, the multiplication operation method is different from the unsigned operation method, how can we implement these two computing operations?
Introduction
To design a computer, calculate a * B + C. When mode is set to 0, the operator uses unsigned operation. When mode is set to 1, the operator uses signed operation.
OpenGL
1 /*
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: signed_unsigned_arithmetic.v
5 Compiler: Modelsim se 6.1f
6 Description: Demo how to do unsigned operation and signed operation
7 Release: 11/24/2007 1.0
8 02/09/2008 2.0
9 */
10 'Timescale 1 NS / 1 NS
11
12 Module signed_unsigned_arithmetic (
13 I _a,
14 I _ B,
15 I _c,
16 I _mode,
17 O_answer
18 );
19
20 Input [ 3 : 0 ] I _a, I _ B, I _c;
21 Input I _mode;
22 Output [ 7 : 0 ] O_answer;
23
24 Wire [ 7 : 0 ] Answer_unsigned, answer_signed;
25
26 // For unsigned operation
27 Assign answer_unsigned = I _a * I _ B + { 4 ' H0, I _c };
28
29 // For singed operation
30 Assign answer_signed = {{ 4 {I _a [ 3 ] }}, I _a} * {{ 4 {I _ B [ 3 ] }}, I _ B} + {{ 4 {I _c [ 3 ] }}, I _c };
31
32 Assign o_answer = (I _mode = 1 ' B0 )? Answer_unsigned: answer_signed;
33
34 Endmodule
27 rows: Unsigned operation
Assign answer_unsigned = I _a * I _ B + { 4 ' H0, I _c };
Since I _a, I _ B, and I _c are all 4 bits, the operation may result in a maximum of 8 bits. Therefore, in line 21, answer_unsigned and answer_signed are declared as 8 bits, multiplication a * B is automatically 8 bits, so there is no problem, but when addition + I _c, I _c is originally 4 bits, it needs to be changed to 8 bits, you only need to add four zeros to the left, so 8 bit + 8 bit = 8 bit.
39 rows for signed operation
Assign answer_signed = {{ 4 {I _a [ 3 ] }}, I _a} * {{ 4 {I _ B [ 3 ] }}, I _ B} + {{ 4 {I _c [ 3 ] }}, I _c };
A very important concept: To do signed operation, you must sign all the numbers before adding and multiplying them. What is signed extension? Forward the highest digit to the left. If it is 0, use 0 then, and if it is 1, use 1 then. Because the result is 8 bits, I _a, I _ B, and I _c must be signed extension into 8 bits before they can be added and multiplied.
{4{I _a [3]}} , I _a}
This parameter indicates that the I _a [3] I _a is obtained four times after the I _a value is retained, and the principle of I _a is the same as that of I _a, I _ B, and I _c.
Testbench
1 /*
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: signed_unsigned_arithmetic_tb.v
5 Simulator: Modelsim se 6.1f
6 Description: testbench for signed_unsigend_arithmetic.v
7 Release: 11/24/2007 1.0
8 02/09/2008 2.0
9 */
10
11 'Timescale 1 NS / 1 NS
12
13 Module signed_unsigned_arithmetic_tb;
14 Reg [ 3 : 0 ] I _a, I _ B, I _c;
15 Reg I _mode;
16 Wire [ 7 : 0 ] O_answer;
17
18 Signed_unsigned_arithmetic U0 (
19 . I _a (I _a ),
20 . I _ B (I _ B ),
21 . I _c (I _c ),
22 . I _mode (I _mode ),
23 . O_answer (o_answer)
24 );
25
26 Initial begin
27 I _mode = 0 ; // Unsigned operation
28 I _a = 4 ' B0010; // 2
29 I _ B = 4 ' B0011; // 3
30 I _c = 4 ' B0100; // 4
31 // Answer = 8'b0000 _ 1010 // 10
32
33 # 50 ;
34 I _mode = 1 ; // Signed operation
35 I _a = 4 ' B1111; //-1
36 I _ B = 4 ' B1110; //-2
37 I _c = 4 ' B0011; // 3
38 // Answer = 8' 0000 _ 0101 // 5
39 End
40
41 Endmodule
Waveform
Conclusion
In this example, we can see that when the hardware is used to calculate the data volume, it is related to the numeric value. In terms of understanding, in Linux kernel, we will find that they are very careful to use the unsigned Int. if we remember that the unsigned int and INT are both 4 bytes, in fact, it does not save the memory. I recommend it because of the speed of the operation. In this example, we can see the difference between the unsigned operation and the signed operation on the digital path, that is to say, if only Int Is Used in C/C ++, signed operation will be run in the hardware, and unsigned int will run unsigned operation, which is fast, therefore, the estimation is based on the speed consideration using the unsigned Int.
Reference
Example of Digital Circuit Design in the three-digit language (node. s) in the three-digit language (node. s), Yanyu, rulin, and Yangrong, 2006
See also
(Formerly known as pipeline) how to use pipeline (pipeline) to calculate the infinite number of operators? (IC design) (OpenGL)
(Original) How to Deal with the addition operations and overflow of signed integer? (SOC) (OpenGL)
(Original) How to Design 2 power supplies? (SOC) (OpenGL)
(Original) how to design a power-on route? (SOC) (sparse core)