Verilog 简易ALU

ALU设计

 ALU模块:

//
//创建日期:2022/11/6 20:06:00
//设计名称:ALU算术逻辑单元
//课程名称:alu
//说明: 
//输入:   [11:0] alu_control;  // ALU控制信号
//        [31:0] alu_src1;     // ALU操作数1
//        [31:0] alu_src2;     // ALU操作数2
//输出:   [31:0] alu_result;   // ALU结果
//依赖项:
//      
//版次:
//版本0.01-文件已创建
//其他注释:
//
//
module alu(alu_control,alu_src1,alu_src2,alu_result);
    input  [11:0] alu_control;  // ALU控制信号
    input  [31:0] alu_src1;     // ALU操作数1
    input  [31:0] alu_src2;     // ALU操作数2
    output [31:0] alu_result;   // ALU结果
    reg [31:0] alu_result;
    // 控制信号为独热编码
    always @(*)
    begin
        case(alu_control)   // 下面的1,2指操作数1,操作数2
            12'b000000000001:alu_result<=alu_src1<<16;         // 高位加载       1
            12'b000000000010:alu_result<=alu_src1>>>alu_src2;         // 算术右移       2
            12'b000000000100:alu_result<=alu_src1>>alu_src2; // 逻辑右移      4
            12'b000000001000:alu_result<=alu_src1<<alu_src2; // 逻辑左移      8
            12'b000000010000:alu_result<=alu_src1^alu_src2; // 按位异或    16
            12'b000000100000:alu_result<=alu_src1|alu_src2;// 按位或    32
            12'b000001000000:alu_result<=~(alu_src1|alu_src2); // 按位或非       64
            12'b000010000000:alu_result<=alu_src1&alu_src2; // 按位与       128
            12'b000100000000:alu_result<=alu_src1<alu_src2?32'd1:32'd0;// 无符号比较,小于置位  256
            12'b001000000000:alu_result<=$signed(alu_src1)<$signed(alu_src2)?32'd1:32'd0;// 有符号比较,小于置位  512
            12'b010000000000:alu_result<=alu_src1+alu_src2;// 1加     1024
            12'b100000000000:alu_result<=alu_src1-alu_src2;// 1减     2048
            default: alu_result<=alu_src1;
        endcase
    end
endmodule

alu_display.v

//*************************************************************************
//   > 文件名: alu_display.v
//   > 描述  :ALU显示模块,调用FPGA板上的IO接口和触摸屏
//   > 作者  : LOONGSON
//   > 日期  : 2016-04-14
//*************************************************************************
module alu_display(
    //时钟与复位信号
    input clk,
    input resetn,    //后缀"n"代表低电平有效

    //拨码开关,用于选择输入数
    input [1:0] input_sel, //00:输入为控制信号(alu_control)
                           //10:输入为源操作数1(alu_src1)
                           //11:输入为源操作数2(alu_src2)

    //触摸屏相关接口,不需要更改
    output lcd_rst,
    output lcd_cs,
    output lcd_rs,
    output lcd_wr,
    output lcd_rd,
    inout[15:0] lcd_data_io,
    output lcd_bl_ctr,
    inout ct_int,
    inout ct_sda,
    output ct_scl,
    output ct_rstn
    );
//-----{调用ALU模块}begin
    reg   [11:0] alu_control;  // ALU控制信号
    reg   [31:0] alu_src1;     // ALU操作数1
    reg   [31:0] alu_src2;     // ALU操作数2
    wire  [31:0] alu_result;   // ALU结果
    alu alu_module(
        .alu_control(alu_control),
        .alu_src1   (alu_src1   ),
        .alu_src2   (alu_src2   ),
        .alu_result (alu_result )
    );
//-----{调用ALU模块}end

//---------------------{调用触摸屏模块}begin--------------------//
//-----{实例化触摸屏}begin
//此小节不需要更改
    reg         display_valid;
    reg  [39:0] display_name;
    reg  [31:0] display_value;
    wire [5 :0] display_number;
    wire        input_valid;
    wire [31:0] input_value;

    lcd_module lcd_module(
        .clk            (clk           ),   //10Mhz
        .resetn         (resetn        ),

        //调用触摸屏的接口
        .display_valid  (display_valid ),
        .display_name   (display_name  ),
        .display_value  (display_value ),
        .display_number (display_number),
        .input_valid    (input_valid   ),
        .input_value    (input_value   ),

        //lcd触摸屏相关接口,不需要更改
        .lcd_rst        (lcd_rst       ),
        .lcd_cs         (lcd_cs        ),
        .lcd_rs         (lcd_rs        ),
        .lcd_wr         (lcd_wr        ),
        .lcd_rd         (lcd_rd        ),
        .lcd_data_io    (lcd_data_io   ),
        .lcd_bl_ctr     (lcd_bl_ctr    ),
        .ct_int         (ct_int        ),
        .ct_sda         (ct_sda        ),
        .ct_scl         (ct_scl        ),
        .ct_rstn        (ct_rstn       )
    ); 
//-----{实例化触摸屏}end

//-----{从触摸屏获取输入}begin
//根据实际需要输入的数修改此小节,
//建议对每一个数的输入,编写单独一个always块
    //当input_sel为00时,表示输入数控制信号,即alu_control
    always @(posedge clk)
    begin
        if (!resetn)
        begin
            alu_control <= 12'd0;
        end
        else if (input_valid && input_sel==2'b00)
        begin
            alu_control <= input_value[11:0];
        end
    end
    
    //当input_sel为10时,表示输入数为源操作数1,即alu_src1
    always @(posedge clk)
    begin
        if (!resetn)
        begin
            alu_src1 <= 32'd0;
        end
        else if (input_valid && input_sel==2'b10)
        begin
            alu_src1 <= input_value;
        end
    end

    //当input_sel为11时,表示输入数为源操作数2,即alu_src2
    always @(posedge clk)
    begin
        if (!resetn)
        begin
            alu_src2 <= 32'd0;
        end
        else if (input_valid && input_sel==2'b11)
        begin
            alu_src2 <= input_value;
        end
    end
//-----{从触摸屏获取输入}end

//-----{输出到触摸屏显示}begin
//根据需要显示的数修改此小节,
//触摸屏上共有44块显示区域,可显示44组32位数据
//44块显示区域从1开始编号,编号为1~44,
    always @(posedge clk)
    begin
        case(display_number)
            6'd1 :
            begin
                display_valid <= 1'b1;
                display_name  <= "SRC_1";
                display_value <= alu_src1;
            end
            6'd2 :
            begin
                display_valid <= 1'b1;
                display_name  <= "SRC_2";
                display_value <= alu_src2;
            end
            6'd3 :
            begin
                display_valid <= 1'b1;
                display_name  <= "CONTR";
                display_value <={20'd0, alu_control};
            end
            6'd4 :
            begin
                display_valid <= 1'b1;
                display_name  <= "RESUL";
                display_value <= alu_result;
            end
            default :
            begin
                display_valid <= 1'b0;
                display_name  <= 40'd0;
                display_value <= 32'd0;
            end
        endcase
    end
//-----{输出到触摸屏显示}end
//----------------------{调用触摸屏模块}end---------------------//
endmodule

 tp仿真

`timescale 1ns / 1ps
module tb;
    reg   [11:0] alu_control;  // ALU控制信号
    reg   [31:0] alu_src1;     // ALU操作数1
    reg   [31:0] alu_src2;     // ALU操作数2
    wire  [31:0] alu_result;   // ALU结果
    alu alu_module(
        .alu_control(alu_control),
        .alu_src1   (alu_src1   ),
        .alu_src2   (alu_src2   ),
        .alu_result (alu_result )
    );
 
    initial begin
        alu_control=12'd0;
        alu_src1=32'd0;
        alu_src2=32'd0;
        #10;
		alu_control=12'b000000000001;     // 1
            #10;
            alu_src1=32'd1024;
            #10;
            alu_src1=32'd512;
            #10;
            alu_src2=32'd64;
		#50;
        alu_control=12'b000000000010;   // 2
            #10;
            alu_src2=32'd1024;
            #10;
            alu_src2=32'd512;
            #10;
            alu_src1=32'd64;
        #50;
        alu_control=12'b000000000100;     // 4
            #10;
            alu_src1=32'd192;
            #10;
            alu_src2=32'd192;
            #10;
            alu_src2=32'd16;
        #50;
        alu_control=12'b000000001000;     // 8
            #10;
            alu_src1=32'd192;
            #10;
            alu_src2=32'd192;
            #10;
            alu_src2=32'd16;
        #50;
        alu_control=12'b000000010000;     // 16
            #10;
            alu_src1=32'd128;
            #10;
            alu_src2=32'd127;
            #10;
            alu_src2=32'd0;
        #50;
        alu_control=12'b000000100000;     // 32
            #10;
            alu_src1=32'd128;
            #10;
            alu_src2=32'd127;
            #10;
            alu_src2=32'd0;
        #50;
        alu_control=12'b000001000000;     // 64
            #10;
            alu_src1=32'hffffffff;
            #10;
            alu_src2=32'h1;
            #10;
            alu_src2=32'hffffffff;
        #50;
        alu_control=12'b000010000000;     // 128
            #10;
            alu_src1=32'h0fffffff;
            #10;
            alu_src2=32'h1;
            #10;
            alu_src1=32'h0;
        #50;
        alu_control=12'b000100000000;     // 256
            #10;
            alu_src1=32'hffffffff;
            alu_src2=32'd1;
            #10;
            alu_src2=32'd3;
            #10;
            alu_src2=32'd5;
        #50;
        alu_control=12'b001000000000;     // 512
            #10;
            alu_src1=32'd1;
            #10;
            alu_src1=32'd3;
            alu_src2=32'hffffffff;
            #10;
            alu_src1=32'd5;
        #50;
        alu_control=12'b010000000000;     // 1024
            #10;
            alu_src1=32'd1;
            #10;
            alu_src1=32'd2048;
            #10;
            alu_src1=32'hf0000000;
        #50;
        alu_control=12'b100000000000;     // 2048
            #10;
            alu_src1=32'd1;
            #10;
            alu_src1=32'd2048;
            #10;
            alu_src1=32'hf0000000;
    end
endmodule

约束(COE2020)(板不一样的不要用这个约束)

#时钟信号连接
set_property PACKAGE_PIN AC19 [get_ports clk]

#脉冲开关,用于输入作为复位信号,低电平有效
#set_property PACKAGE_PIN Y3 [get_ports resetn]
set_property PACKAGE_PIN AB21 [get_ports resetn]

#拨码开关连接,用于输入,依次为sw0,sw1
#set_property PACKAGE_PIN AE20 [get_ports input_sel[0]]
set_property PACKAGE_PIN AE17   [get_ports input_sel[0]]
#set_property PACKAGE_PIN AD24 [get_ports input_sel[1]]
set_property PACKAGE_PIN AF17 [get_ports input_sel[1]]

set_property IOSTANDARD LVCMOS33 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports resetn]
set_property IOSTANDARD LVCMOS33 [get_ports input_sel[1]]
set_property IOSTANDARD LVCMOS33 [get_ports input_sel[0]]

#触摸屏引脚连接
#set_property PACKAGE_PIN J25 [get_ports lcd_rst]
#set_property PACKAGE_PIN H18 [get_ports lcd_cs]
#set_property PACKAGE_PIN K16 [get_ports lcd_rs]
#set_property PACKAGE_PIN L8 [get_ports lcd_wr]
#set_property PACKAGE_PIN K8 [get_ports lcd_rd]
#set_property PACKAGE_PIN J15 [get_ports lcd_bl_ctr]
#set_property PACKAGE_PIN H9 [get_ports {lcd_data_io[0]}]
#set_property PACKAGE_PIN K17 [get_ports {lcd_data_io[1]}]
#set_property PACKAGE_PIN J20 [get_ports {lcd_data_io[2]}]
#set_property PACKAGE_PIN M17 [get_ports {lcd_data_io[3]}]
#set_property PACKAGE_PIN L17 [get_ports {lcd_data_io[4]}]
#set_property PACKAGE_PIN L18 [get_ports {lcd_data_io[5]}]
#set_property PACKAGE_PIN L15 [get_ports {lcd_data_io[6]}]
#set_property PACKAGE_PIN M15 [get_ports {lcd_data_io[7]}]
#set_property PACKAGE_PIN M16 [get_ports {lcd_data_io[8]}]
#set_property PACKAGE_PIN L14 [get_ports {lcd_data_io[9]}]
#set_property PACKAGE_PIN M14 [get_ports {lcd_data_io[10]}]
#set_property PACKAGE_PIN F22 [get_ports {lcd_data_io[11]}]
#set_property PACKAGE_PIN G22 [get_ports {lcd_data_io[12]}]
#set_property PACKAGE_PIN G21 [get_ports {lcd_data_io[13]}]
#set_property PACKAGE_PIN H24 [get_ports {lcd_data_io[14]}]
#set_property PACKAGE_PIN J16 [get_ports {lcd_data_io[15]}]
#set_property PACKAGE_PIN L19 [get_ports ct_int]
#set_property PACKAGE_PIN J24 [get_ports ct_sda]
#set_property PACKAGE_PIN H21 [get_ports ct_scl]
#set_property PACKAGE_PIN G24 [get_ports ct_rstn]
set_property PACKAGE_PIN E5  [get_ports lcd_rst]
set_property PACKAGE_PIN G7  [get_ports lcd_cs]
set_property PACKAGE_PIN H7  [get_ports lcd_rs]
set_property PACKAGE_PIN E6  [get_ports lcd_wr]
set_property PACKAGE_PIN D5  [get_ports lcd_rd]
set_property PACKAGE_PIN J5  [get_ports lcd_bl_ctr]
set_property PACKAGE_PIN C4  [get_ports {lcd_data_io[0]}]
set_property PACKAGE_PIN C3  [get_ports {lcd_data_io[1]}]
set_property PACKAGE_PIN D4  [get_ports {lcd_data_io[2]}]
set_property PACKAGE_PIN D3  [get_ports {lcd_data_io[3]}]
set_property PACKAGE_PIN F5  [get_ports {lcd_data_io[4]}]
set_property PACKAGE_PIN G6  [get_ports {lcd_data_io[5]}]
set_property PACKAGE_PIN F4  [get_ports {lcd_data_io[6]}]
set_property PACKAGE_PIN E3  [get_ports {lcd_data_io[7]}]
set_property PACKAGE_PIN G5  [get_ports {lcd_data_io[8]}]
set_property PACKAGE_PIN H6  [get_ports {lcd_data_io[9]}]
set_property PACKAGE_PIN F2  [get_ports {lcd_data_io[10]}]
set_property PACKAGE_PIN F3  [get_ports {lcd_data_io[11]}]
set_property PACKAGE_PIN G4  [get_ports {lcd_data_io[12]}]
set_property PACKAGE_PIN G2  [get_ports {lcd_data_io[13]}]
set_property PACKAGE_PIN H4  [get_ports {lcd_data_io[14]}]
set_property PACKAGE_PIN H3  [get_ports {lcd_data_io[15]}]
set_property PACKAGE_PIN K6  [get_ports ct_int]
set_property PACKAGE_PIN J6  [get_ports ct_sda]
set_property PACKAGE_PIN L8  [get_ports ct_scl]
set_property PACKAGE_PIN K7  [get_ports ct_rstn]


set_property IOSTANDARD LVCMOS33 [get_ports lcd_rst]
set_property IOSTANDARD LVCMOS33 [get_ports lcd_cs]
set_property IOSTANDARD LVCMOS33 [get_ports lcd_rs]
set_property IOSTANDARD LVCMOS33 [get_ports lcd_wr]
set_property IOSTANDARD LVCMOS33 [get_ports lcd_rd]
set_property IOSTANDARD LVCMOS33 [get_ports lcd_bl_ctr]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[8]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[9]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[10]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[11]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[12]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[13]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[14]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_data_io[15]}]
set_property IOSTANDARD LVCMOS33 [get_ports ct_int]
set_property IOSTANDARD LVCMOS33 [get_ports ct_sda]
set_property IOSTANDARD LVCMOS33 [get_ports ct_scl]
set_property IOSTANDARD LVCMOS33 [get_ports ct_rstn]
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值