简体   繁体   English

使用Verilog在32位ALU中实现一位标志

[英]Implementing one-bit flags in a 32Bit ALU using Verilog

I am working on an assignment and am a little lost and don't really know how to get started. 我正在做作业,有点迷路,不知道该如何上手。 I need to implement the following flags in a 32Bit ALU: 我需要在32Bit ALU中实现以下标志:

• Z ("Zero"): Set to 1 ("True") if the result of the operation is zero •Z(“零”):如果运算结果为零,则设置为1(“ True”)

• N ("Negative"): Set to 1 ("True") if the first bit of the result is 1, which indicates a negative number •N(“负”):如果结果的第一位为1,则设置为1(“ True”),表示负数

• O ("Overflow"): Set to 1 ("True") to indicate that the operation overflowed the bus width. •O(“溢出”):设置为1(“ True”)表示操作超出了总线宽度。

Additionally, a comparison function that compares input a to input b and then set one of three flags: 此外,还有一个比较函数,将输入a与输入b进行比较,然后设置三个标志之一:

• LT if input a is less than input b •如果输入a小于输入b,则为LT

• GT if input a is greater than input b •GT,如果输入a大于输入b

• EQ if input a is equal to input b •如果输入a等于输入b则等于

I need to modify this ALU to include the three flags and comparison outputs then change the test bench to test for all of these modifications. 我需要修改此ALU以包括三个标志和比较输出,然后更改测试台以测试所有这些修改。

This was all the information I received for this assignment and there is no textbook or any other resources really. 这是我收到的有关此作业的所有信息,实际上没有教科书或任何其他资源。 It's an online class, and I cannot get a response from my instructor. 这是在线课程,我无法从老师那里得到答复。 So I am a little confused as to how to get started. 因此,我对如何开始感到困惑。 I am still a total newbie when it comes to digital logic so please bear with me. 在数字逻辑方面,我还是一个新手,请耐心等待。 I just need some help understanding how these flags and comparison works. 我只需要一些帮助来了解这些标志和比较的工作原理。 If any one can explain this a little better to me as far as how they work and what they do, and possibly how I would implement them into the ALU and testbench, I would really appreciate it. 如果有人能更好地向我解释它们的工作方式和作用,以及可能如何将它们实现到ALU和测试平台中,我将非常感激。

I don't expect anyone to do my assignment, I really just need help understanding it. 我不希望有人来做我的作业,我真的只需要帮助就可以理解它。

ALU ALU

module alu32 (a, b, out, sel);      
    input [31:0] a, b;    
    input [3:0] sel;   
    output [31:0] out,
    reg [31:0] out;  

    //Code starts here 
    always @(a, b, sel)   
    begin     
        case (sel)       
            //Arithmetic Functions       
            0  : out <= a + b;       
            1  : out <= a - b;       
            2  : out <= b - a;       
            3  : out <= a * b;       
            4  : out <= a / b;       
            5  : out <= b % a;       
            //Bit-wise Logic Functions       
            6  : out <= ~a; //Not       
            7  : out <= a & b; //And       
            8  : out <= a | b; //Or       
            9  : out <= a ^ b; //XOR       
            10 : out <= a ^~ b; //XNOR       
            //Logic Functions       
            11 : out <= !a;       
            12 : out <= a && b;       
            13 : out <= a || b;       
            default: out <= a + b;     
        endcase
    end  
endmodule 

ALU Testbench ALU测试台

module alu32_tb();  
    reg [31:0] a, b; 
    reg [3:0] sel; 
    wire [31:0] out; 

initial begin

$monitor("sel=%d a=%d b=%d out=%d", sel,a,b,out);   
    //Fundamental tests - all a+b   
    #0 sel=4'd0; a = 8'd0; b = 8'd0;    
    #1 sel=4'd0; a = 8'd0; b = 8'd25;   
    #1 sel=4'd0; a = 8'd37; b = 8'd0;   
    #1 sel=4'd0; a = 8'd45; b = 8'd75;  
    //Arithmetic   
    #1 sel=4'd1; a = 8'd120; b = 8'd25; //a-b   
    #1 sel=4'd2; a = 8'd30; b = 8'd120; //b-a   
    #1 sel=4'd3; a = 8'd75; b = 8'd3; //a*b   
    #1 sel=4'd4; a = 8'd75; b = 8'd3; //a/b   
    #1 sel=4'd5; a = 8'd74; b = 8'd3; //a%b  
    //Bit-wise Logic Functions   
    #1 sel=4'd6; a = 8'd31; //Not   
    #1 sel=4'd7; a = 8'd31; b = 8'd31; //And   
    #1 sel=4'd8; a = 8'd30; b = 8'd1; //Or   
    #1 sel=4'd9; a = 8'd30; b = 8'd1; //XOR   
    #1 sel=4'd10; a = 8'd30; b = 8'd1; //XNOR  
    //Logic Functions   
    #1 sel=4'd11; a = 8'd25; //Not   
    #1 sel=4'd12; a = 8'd30; b = 8'd0; //And   
    #1 sel=4'd13; a = 8'd0; b = 8'd30; //Or      
    #1 $finish; 
end  

alu32 myalu (.a(a), .b(b), .out(out), .sel(sel));  
endmodule  

You can add these flag outputs to the design . 您可以将这些标志输出添加到设计中 Like the following. 像下面这样。 Simply connect them in testbench. 只需它们连接到测试台即可。

// In design:
output zero;
output overflow;
output negative;

// In testbench:
wire zero,overflow,negative;
alu32 myalu (.a(a), .b(b), .out(out), .sel(sel), .zero(zero), .overflow(overflow),.negative(negative));  

For logic part, you can do it with continuous assignments . 对于逻辑部分,您可以通过连续分配来完成 You may need to add some logic for using these flags only during certain values of sel . 您可能需要添加一些 sel 特定值期间使用这些标志的逻辑

Z ("Zero"): Set to 1 ("True") if the result of the operation is zero Z(“零”):如果运算结果为零,则设置为1(“ True”)

So, we can have condition like all the bits of out must be zero . 因此,我们可以有条件,例如out 所有位必须为零 This can be done in many other ways. 这可以通过许多其他方式来完成。

// Bit wise OR-ing on out
assign zero = ~(|out);

O ("Overflow"): Set to 1 ("True") to indicate that the operation overflowed the bus width. O(“溢出”):设置为1(“ True”)表示操作超出了总线宽度。

According to this description and the code shown, you simply want carry flag here.That is, a signed extension of addition operation. 根据描述和所示代码,您只需要在这里带有进位标志 ,即加法操作的带符号扩展名 Refer to this page on WikiPedia for overflow condition. 有关溢出情况,请参阅WikiPedia上的此页面

But, Overflow condition is not the same as the carry bit. 但是, 溢出条件一样的位。 Overflow represents data loss while carry represents a bit used for calculation in next stage . 溢出表示数据丢失,而进位表示下一阶段用于计算的位

So, doing something like following may be useful: 因此,执行以下操作可能会很有用:

// Extend the result for capturing carry bit
// Simply use this bit if you want result > bus width
{carry,out} <= a+b;
// overflow in signed arithmetic:
assign overflow = ({carry,out[31]} == 2'b01);

N ("Negative"): Set to 1 ("True") if the first bit of the result is 1, which indicates a negative number N(“负”):如果结果的第一位为1,则设置为1(“ True”),表示负数

Again this is simply the MSB of the out register. 同样,这只是out寄存器的MSB But, the underflow condition is entirely a different thing. 但是, 下溢条件完全不同

// Depending on sel, subtraction must be performed here
assign negative = (out[31] == 1 && (sel == 1 || sel == 2));

Also, simple condition like assign lt = (a<b) ? 1 : 0; 同样,简单条件如assign lt = (a<b) ? 1 : 0; assign lt = (a<b) ? 1 : 0; and others can detect the input LT, GT and EQ conditions. 其他人可以检测输入的LT,GT和EQ条件。

Refer the answer here for the overflow/underflow flag understanding . 请参考此处的答案了解上溢/下溢标志 Overflow-Carry link may also be useful. 溢出传送链接也可能有用。

Refer Carryout-Overflow , ALU in Verilog and ALU PDF for further information about ALU implementation. 有关ALU实现的更多信息,请参见Carryout-OverflowVerilog中的 ALUALU PDF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM