当前位置:   article > 正文

【基于FPGA的芯片设计】多功能ALU_alu代码设计及测试数据设计

alu代码设计及测试数据设计

目录

一、实验要求

二、源代码

1. 顶层模块

2. 数据输入模块

3. ALU运算模块

4. 结果处理模块

5. 扫描数码管模块

5.1 扫描数码管顶层

5.2 分频器

5.3 数码管显示

三、仿真代码

四、结构层图

五、管脚配置


实验板卡:xc7a100tlc sg324-2L,共20个开关

一、实验要求

通过高低位控制,实现32位数据A、B及运算方式的输入,通过8个数码管显示ALU的十六进制运算结果, 通过4个led灯显示4个运算信号ZF SF CF OF(判零、符号、进位、判溢)

二、源代码

1. 顶层模块

  1. `timescale 1ns / 1ps
  2. module TOP(
  3. input CLK_100M,
  4. input[15:0] in_data,
  5. input ctrl_in,
  6. input rst_,
  7. input CLK_A,
  8. input CLK_B,
  9. input CLK_OP,
  10. input ShowA,
  11. input ShowB,
  12. output[7:0] AN,
  13. output[7:0] SEG,
  14. output[3:0] out_flags
  15. );
  16. wire[31:0] a;
  17. wire[31:0] b;
  18. wire[31:0] res;
  19. wire[31:0] alu_f;
  20. wire[3:0] in_flags;
  21. reg[31:0] out_data;
  22. DataInput A(in_data, ctrl_in, CLK_A, rst_, a);
  23. DataInput B(in_data, ctrl_in, CLK_B, rst_, b); //数据输入
  24. ALU alu(a, b, in_data[3:0], res, in_flags);
  25. ALU_F f(res, in_flags, CLK_OP, rst_, alu_f, out_flags);
  26. LED led(CLK_100M, rst_, out_data, AN, SEG);
  27. always@(ShowA or ShowB)
  28. begin
  29. if(!ShowA)
  30. out_data = a;
  31. else if(!ShowB)
  32. out_data = b;
  33. else
  34. out_data = alu_f;
  35. end
  36. endmodule

2. 数据输入模块

  1. `timescale 1ns / 1ps
  2. module DataInput(
  3. input[15:0] data_in,
  4. input ctrl_in,
  5. input clk,
  6. input rst_,
  7. output reg[31:0] data_out
  8. );
  9. always@(negedge clk or negedge rst_)
  10. begin
  11. if(!rst_)
  12. data_out <= 32'b0;
  13. else
  14. begin
  15. if(ctrl_in == 1'b0)
  16. data_out[15:0] <= data_in;
  17. else
  18. data_out[31:16] <= data_in;
  19. end
  20. end
  21. endmodule

3. ALU运算模块

  1. `timescale 1ns / 1ps
  2. module ALU(
  3. input[31:0] a,
  4. input[31:0] b,
  5. input[31:0] op,
  6. output reg[32:0] res,
  7. output reg[3:0] flags
  8. );
  9. // flags: ZF SF CF OF (高位->低位)
  10. // 判零 符号 进位 判溢
  11. always@(*)
  12. begin
  13. case(op)
  14. 4'b0000:
  15. begin
  16. res <= a + b;
  17. flags[1] = res[32];
  18. flags[0] = a[31] ^ b[31] ^ res[31] ^ res[32];
  19. end
  20. 4'b0001: res <= a << b;
  21. 4'b0010: res <= ($signed(a) < $signed(b)) ? 1 : 0;
  22. 4'b0011: res <= (a < b) ? 1 : 0;
  23. 4'b0100: res <= a ^ b;
  24. 4'b0101: res <= a >> b;
  25. 4'b0110: res <= a | b;
  26. 4'b0111: res <= a & b;
  27. 4'b1000:
  28. begin
  29. res <= a - b;
  30. flags[1] = res[32];
  31. flags[0] = a[31] ^ b[31] ^ res[31] ^ res[32];
  32. end
  33. 4'b1001: res <= $signed(a) >>> b;
  34. endcase
  35. flags[3] = (res == 32'd0) ? 1 : 0;
  36. flags[2] = res[31];
  37. end
  38. endmodule

4. 结果处理模块

  1. `timescale 1ns / 1ps
  2. module ALU_F(
  3. input[32:0] res,
  4. input[3:0] in_flags,
  5. input clk,
  6. input rst_,
  7. output reg[32:0] alu_f,
  8. output reg[3:0] out_flags
  9. );
  10. always@(negedge clk or negedge rst_)
  11. begin
  12. if(!rst_)
  13. begin
  14. alu_f <= 32'b0;
  15. out_flags <= 4'b1000;
  16. end
  17. else if(clk)
  18. begin
  19. alu_f <= res;
  20. out_flags <= in_flags;
  21. end
  22. end
  23. endmodule

5. 扫描数码管模块

5.1 扫描数码管顶层

  1. `timescale 1ns / 1ps
  2. module LED(
  3. input clk_100M,
  4. input rst_,
  5. input[31:0] data,
  6. output[7:0] AN,
  7. output[7:0] SEG
  8. );
  9. wire clk_ref; //数码管刷新频率:500hz
  10. wire clk_inc; //数字刷新频率:10hz
  11. Fdiv fdiv(clk_100M,32'd100000,clk_ref);
  12. Scanner scanner(data,rst_,clk_ref,AN,SEG);
  13. endmodule

5.2 分频器

  1. `timescale 1ns / 1ps
  2. module Fdiv(
  3. input clk_in,
  4. input [31:0] count,
  5. output reg clk_out
  6. );
  7. reg[31:0] num;
  8. always@(posedge clk_in)
  9. begin
  10. if(num == count)
  11. begin
  12. clk_out <= ~clk_out;
  13. num <= 1'b0;
  14. end
  15. else
  16. num <= num + 1'b1;
  17. end
  18. endmodule

5.3 数码管显示

  1. `timescale 1ns / 1ps
  2. module Scanner(
  3. input[31:0] data,
  4. input rst_,
  5. input clk_ref,
  6. output reg[7:0] AN,
  7. output reg[7:0] SEG
  8. );
  9. reg[3:0] data_x;
  10. reg[2:0] bit;
  11. always@(negedge rst_ or posedge clk_ref)
  12. begin
  13. if(!rst_)
  14. bit <= 3'd0;
  15. else
  16. bit <= bit + 1'b1;
  17. end
  18. always@(*)
  19. begin
  20. case(bit)
  21. 3'b000: begin AN <= 8'b1111_1110; data_x <= data[3:0]; end
  22. 3'b001: begin AN <= 8'b1111_1101; data_x <= data[7:4]; end
  23. 3'b010: begin AN <= 8'b1111_1011; data_x <= data[11:8]; end
  24. 3'b011: begin AN <= 8'b1111_0111; data_x <= data[15:12]; end
  25. 3'b100: begin AN <= 8'b1110_1111; data_x <= data[19:16]; end
  26. 3'b101: begin AN <= 8'b1101_1111; data_x <= data[23:20]; end
  27. 3'b110: begin AN <= 8'b1011_1111; data_x <= data[27:24]; end
  28. 3'b111: begin AN <= 8'b0111_1111; data_x <= data[31:28]; end
  29. endcase
  30. case(data_x)
  31. 4'b0000: SEG <= 8'b0000_0011;
  32. 4'b0001: SEG <= 8'b1001_1111;
  33. 4'b0010: SEG <= 8'b0010_0101;
  34. 4'b0011: SEG <= 8'b0000_1101;
  35. 4'b0100: SEG <= 8'b1001_1001;
  36. 4'b0101: SEG <= 8'b0100_1001;
  37. 4'b0110: SEG <= 8'b0100_0001;
  38. 4'b0111: SEG <= 8'b0001_1111;
  39. 4'b1000: SEG <= 8'b0000_0001;
  40. 4'b1001: SEG <= 8'b0000_1001;
  41. 4'b1010: SEG <= 8'b0001_0001;
  42. 4'b1011: SEG <= 8'b1100_0001;
  43. 4'b1100: SEG <= 8'b0110_0011;
  44. 4'b1101: SEG <= 8'b1000_0101;
  45. 4'b1110: SEG <= 8'b0110_0001;
  46. 4'b1111: SEG <= 8'b0111_0001;
  47. endcase
  48. end
  49. endmodule

三、仿真代码

  1. `timescale 1ns / 1ps
  2. module ALU_top_sim();
  3. reg[31:0] a;
  4. reg[31:0] b;
  5. reg[3:0] op;
  6. wire[32:0] res;
  7. wire[3:0] flags; //顺序:ZF、SF、CF、OF
  8. initial
  9. begin
  10. a = 32'h8000_0003;
  11. b = 32'h0000_0001;
  12. #100
  13. op = 4'b0000; //加法
  14. #100
  15. op = 4'b0001; //逻辑左移
  16. #100
  17. op = 4'b0010; //有符号数比较
  18. #100
  19. op = 4'b0011; //无符号数比较
  20. #100
  21. op = 4'b0100; //异或
  22. #100
  23. op = 4'b0101; //逻辑右移
  24. #100
  25. op = 4'b0110; //按位或
  26. #100
  27. op = 4'b0111; //按位与
  28. #100
  29. op = 4'b1000; //减法
  30. #100
  31. op = 4'b1001; //算术右移
  32. end
  33. ALU alu_sim(a,b,op,res,flags);
  34. endmodule

四、结构层图

五、管脚配置

  1. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN E3} [get_ports CLK_100M]
  2. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN V5} [get_ports in_data[15]]
  3. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T4} [get_ports in_data[14]]
  4. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN V6} [get_ports in_data[13]]
  5. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T5} [get_ports in_data[12]]
  6. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T6} [get_ports in_data[11]]
  7. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN V7} [get_ports in_data[10]]
  8. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R8} [get_ports in_data[9]]
  9. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U9} [get_ports in_data[8]]
  10. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T9} [get_ports in_data[7]]
  11. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN V10} [get_ports in_data[6]]
  12. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R10} [get_ports in_data[5]]
  13. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U11} [get_ports in_data[4]]
  14. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R11} [get_ports in_data[3]]
  15. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U12} [get_ports in_data[2]]
  16. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T13} [get_ports in_data[1]]
  17. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN V14} [get_ports in_data[0]]
  18. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN T14} [get_ports ctrl_in]
  19. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets rst_]
  20. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets CLK_A]
  21. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets CLK_B]
  22. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets CLK_OP]
  23. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN N17} [get_ports rst_]
  24. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN P18} [get_ports CLK_A]
  25. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN P17} [get_ports CLK_B]
  26. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R18} [get_ports CLK_OP]
  27. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets ShowA]
  28. set_property CLOCK_DEDICATED_ROUTE FALSE [get_nets ShowB]
  29. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U18} [get_ports ShowA]
  30. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U17} [get_ports ShowB]
  31. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U6} [get_ports out_flags[3]]
  32. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R5} [get_ports out_flags[2]]
  33. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN U7} [get_ports out_flags[1]]
  34. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN R6} [get_ports out_flags[0]]
  35. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN C9} [get_ports AN[7]]
  36. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN C10} [get_ports AN[6]]
  37. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN D10} [get_ports AN[5]]
  38. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN C11} [get_ports AN[4]]
  39. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN M17} [get_ports AN[3]]
  40. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN J14} [get_ports AN[2]]
  41. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN K13} [get_ports AN[1]]
  42. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN P14} [get_ports AN[0]]
  43. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN F14} [get_ports SEG[7]]
  44. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN N14} [get_ports SEG[6]]
  45. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN J13} [get_ports SEG[5]]
  46. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN G13} [get_ports SEG[4]]
  47. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN F13} [get_ports SEG[3]]
  48. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN G14} [get_ports SEG[2]]
  49. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN M13} [get_ports SEG[1]]
  50. set_property -dict {IOSTANDARD LVCMOS18 PACKAGE_PIN H14} [get_ports SEG[0]]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/605075
推荐阅读
相关标签
  

闽ICP备14008679号