【verilog】VL5 位拆分与运算

VL5 位拆分与运算

题目描述:
现在输入了一个压缩的16位数据,其实际上包含了四个数据[3:0][7:4][11:8][15:12],
现在请按照sel选择输出四个数据的相加结果,并输出valid_out信号(在不输出时候拉低)
0: 不输出且只有此时的输入有效
1:输出[3:0]+[7:4]
2:输出[3:0]+[11:8]
3:输出[3:0]+[15:12]
在这里插入图片描述
写法一:

`timescale 1ns/1ns
 
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
 
output  reg [4:0] out,
output validout
);
//*************code***********//
assign validout = rst & (sel != 0);
 
reg [15:0] temp;
 
always @(*)
    if(sel == 0) temp = d;

always @(*) begin
    if(!rst) out <= 0;
    else  begin
        case(sel)
        2'b00: out = 0;
        2'b01: out = temp[3:0]+ temp[7:4];
        2'b10: out = temp[3:0]+ temp[11:8];
        2'b11: out = temp[3:0]+ temp[15:12];
        default:out = 0;
        endcase
    end
end
 
//*************code***********//
endmodule

写法二:

`timescale 1ns/1ns
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
 
output  reg [4:0] out,
output validout
);
//*************code***********//
assign validout = rst & (sel != 0);
reg [15:0] temp;
always @(posedge clk , negedge rst) begin
    if(!rst) temp <= 0;
    else    if(sel == 0) temp <= d;
end
always @(*) begin   
        case(sel)
        2'b00: out = 0;
        2'b01: out = temp[3:0]+ temp[7:4];
        2'b10: out = temp[3:0]+ temp[11:8];
        2'b11: out = temp[3:0]+ temp[15:12];
        default:out = 0;
        endcase  
end
//*************code***********//
endmodule

比较:
写法一temp部分会产生latch
写法二更好

补充:
什么情况下会产生 latch?
只有在 组合逻辑过程块(always @(*)) 中,变量未在所有分支赋值时,才会推断 latch。

总结:
1.wire 用 assign,reg 用 always
2.要存储,用时钟
3.从输出波形看,sel 和 out 同步变化,所以可以用组合逻辑。

关于采样

always @(posedge clk , negedge rst) begin
    if(!rst) temp <= 0;
    else    if(sel == 0) temp <= d;
end

在时钟上升沿时,如果sel == 0,则d会给temp,
也就是说,d是和sel同周期的那个d。
这样也符合要求:sel=0: 不输出且只有此时的输入有效 。
另外,
假设rst 固定为1 , sel 固定为 0 ,那么temp的变化比d晚一拍。

注意:这种写法不对,因为组合逻辑块不能用 <= 非阻塞赋值。

always @(*) begin
    if(!rst) temp <= 0;
    else    if(sel == 0) temp <= d;
end
### 一、实验目的 通过Quartus II平台进行拆分运算实验,深入理解Verilog硬件描述语言中操作的原理和实现方法,掌握如何对数据进行拆分以及执行级别的逻辑运算,提升数字电路设计和调试能力。 ### 二、实验环境 - **软件**:Quartus II软件,用于Verilog代码的编写、编译和仿真。 - **硬件**:相应的FPGA开发板(如DE2等),用于将设计下载到硬件中进行实际验证。 ### 三、实验原理 #### 拆分 拆分是将一个多的二进制数按照一定规则拆分成多个低的二进制数。例如,将一个8二进制数拆分成两个4二进制数。在Verilog中,可以通过选择操作来实现拆分,如`data[7:4]`和`data[3:0]`分别表示8数据`data`的高4和低4。 #### 运算 Verilog支持多种运算,如按(`&`)、按或(`|`)、按异或(`^`)、按取反(`~`)等。这些运算可以对二进制数的每一进行独立操作。 ### 四、实验代码实现 #### 拆分模块 ```verilog module bit_split ( input wire [7:0] data_in, output wire [3:0] high_bits, output wire [3:0] low_bits ); assign high_bits = data_in[7:4]; assign low_bits = data_in[3:0]; endmodule ``` #### 运算模块 ```verilog module bit_operation ( input wire [3:0] a, input wire [3:0] b, output wire [3:0] and_result, output wire [3:0] or_result, output wire [3:0] xor_result ); assign and_result = a & b; assign or_result = a | b; assign xor_result = a ^ b; endmodule ``` #### 顶层模块 ```verilog module top_module ( input wire [7:0] data_in, output wire [3:0] and_result, output wire [3:0] or_result, output wire [3:0] xor_result ); wire [3:0] high_bits; wire [3:0] low_bits; bit_split u1 ( .data_in(data_in), .high_bits(high_bits), .low_bits(low_bits) ); bit_operation u2 ( .a(high_bits), .b(low_bits), .and_result(and_result), .or_result(or_result), .xor_result(xor_result) ); endmodule ``` ### 五、实验步骤 #### 1. 代码编写 在Quartus II中创建新的工程,将上述Verilog代码分别保存为`bit_split.v`、`bit_operation.v`和`top_module.v`文件,并添加到工程中。 #### 2. 代码编译 对工程进行编译,检查代码中是否存在语法错误。如果编译通过,Quartus II会生成相应的编程文件。 #### 3. 仿真验证 编写测试平台代码对顶层模块进行仿真验证。以下是一个简单的测试平台代码示例: ```verilog `timescale 1ns / 1ps module tb_top_module; reg [7:0] data_in; wire [3:0] and_result; wire [3:0] or_result; wire [3:0] xor_result; top_module uut ( .data_in(data_in), .and_result(and_result), .or_result(or_result), .xor_result(xor_result) ); initial begin data_in = 8'b10101100; #100; data_in = 8'b11110000; #100; $stop; end endmodule ``` 在Quartus II中运行仿真,观察波形图,验证拆分运算的结果是否正确。 #### 4. 硬件下载 将编译生成的编程文件下载到FPGA开发板中,通过开发板上的输入输出接口验证实际硬件运行结果。 ### 六、实验结果分析 通过仿真波形图和硬件验证结果,可以观察到输入的8数据被正确拆分成高4和低4,并且对这两个4数据进行或和异或运算的结果符合预期。 ### 七、实验总结 通过本次实验,掌握了在Quartus II平台下使用Verilog进行拆分运算的方法。理解了操作在数字电路设计中的重要性,同时也熟悉了Quartus II的代码编写、编译、仿真和硬件下载等基本操作流程。 ### 八、实验拓展 可以进一步扩展实验,实现更复杂的操作,如多数据的多级拆分运算的组合应用等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值