verilog练习0

VL3奇偶校验

`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
//*************code***********//
assign check=sel?^bus:~^bus;

//*************code***********//
endmodule

VL1 四选一多路器

 

`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output[1:0]mux_out
);
//*************code***********//

assign mux_out=sel[0]?(sel[1]?3:1):(sel[1]?0:2);
//assign mux_out=sel==0?2:(sel==1?1:(sel==2?0:3));
//*************code***********//
endmodule

VL2 异步复位的串联T触发器

T触发器是在数字电路中,凡在CP时钟脉冲控制下,根据输入信号T取值的不同,具有保持和翻转功能的触发器,即当T=0时能保持状态不变,当T=1时一定翻转的电路。--百度百科

`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q  
);
//*************code***********//
reg q0;
always@(posedge clk or negedge rst) begin
    if(!rst) begin
        q0<=0;
    end else begin
        case(data)
            'b1:q0<=~q0;
            default: q0<=q0;
        endcase
    end
end

always@(posedge clk or negedge rst) begin
    if(!rst) begin
        q<=0;
    end else begin
        case(q0)
            'b1:q<=~q;
            default: q<=q;
        endcase
    end
end

//*************code***********//
endmodule

真值表:

data q0  q0next         q0next   q  q_next

0       0    0                0       0       0

0       1    1                0       1        1

1        0   1                1       0         1

1        1    0               1       1         0

q0_next<=data^q0

q_next<=data^q1

`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q  
);
//*************code***********//
reg q0;

always@(posedge clk or negedge rst) begin
    if(!rst) begin
        q0<=0;
        q<=0;
    end else begin
        q0<=data^q0;
        q<=q0^q;
    end
end

//*************code***********//
endmodule

VL4 移位运算与乘法

 

 

`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
reg [1:0]count;
reg [7:0] reg_n;
always@(posedge clk or negedge rst) begin
    if(!rst) begin
        out<=0;
        input_grant<=0;
        count<=0;
        reg_n<=0;
    end else begin
        if(count==0) begin
            out<=d*1;
            reg_n<=d;
            input_grant<=1;
            count<=count+1;
        end else if(count==1) begin
            out<=reg_n*3;
            input_grant<=0;
            count<=count+1;    
        end else if(count==2) begin
            out<=reg_n*7;
            input_grant<=0;
            count<=count+1; 
        end else begin
            out<=reg_n*8;
            input_grant<=0;
            count<=0;  
        end
    end 
end

//*************code***********//
endmodule
`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
reg [1:0] CS,NS;
reg [7:0] reg_n;
//state change
always@(posedge clk or negedge rst) begin
    if(!rst) begin
        CS<='b00;
        NS<='b00;        
    end else begin
        CS<=NS;
    end
end
//next state
always @(CS or NS)begin
    case(CS)
        'b00:NS<='b01;
        'b01:NS<='b10;
        'b10:NS<='b11;  
        default:NS<='b00;
    endcase
end
//output
always @(posedge clk or negedge rst) begin
    if(!rst) begin
        out<=0;
        input_grant<=0;    
    end else begin
        case(CS)
        'b00:begin
            out<=d;    
            input_grant<=1;
            reg_n<=d;
        end
        'b01:begin
            out<=reg_n*3;    
            input_grant<=0;
        end
        'b10:begin
            out<=reg_n*7;    
            input_grant<=0;
        end
        default:begin
            out<=reg_n*8;    
            input_grant<=0;
        end
        endcase
    end
end

//********

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一道 Verilog 练习题: 题目描述: 设计一个 4 位加法器,包括一个输入端 A,一个输入端 B,一个进位输入端 Cin,一个输出端 S 和一个进位输出端 Cout。输入和输出都是二进制数。如果输入的数位数不足 4 位,则在高位补 0。如果加法结果超出 4 位,则将最高位舍弃。 要求: 1. 设计一个模块 top,包含一个 4 位加法器模块 adder 和一个测试模块 testbench。 2. 在 testbench 中,通过在不同的时间点改变输入信号,模拟所有可能的输入情况,并将结果输出到终端。 3. 用 Verilog 编写代码,并使用任何 Verilog 编译器进行编译和仿真。 参考答案: 下面是一种可能的解法: ``` module adder(input [3:0] A, input [3:0] B, input Cin, output [3:0] S, output Cout); assign S = A + B + Cin; assign Cout = (S[4] == 1) ? 1 : 0; endmodule module testbench; reg [3:0] A, B; reg Cin; wire [3:0] S; wire Cout; adder uut(A, B, Cin, S, Cout); initial begin $monitor("A=%b, B=%b, Cin=%b, S=%b, Cout=%b", A, B, Cin, S, Cout); A = 4'b0000; B = 4'b0000; Cin = 0; #10; A = 4'b0000; B = 4'b0001; Cin = 0; #10; A = 4'b0001; B = 4'b0000; Cin = 0; #10; A = 4'b0001; B = 4'b0001; Cin = 0; #10; A = 4'b1111; B = 4'b0000; Cin = 0; #10; A = 4'b1111; B = 4'b0000; Cin = 1; #10; A = 4'b1111; B = 4'b1111; Cin = 0; #10; A = 4'b1111; B = 4'b1111; Cin = 1; #10; $finish; end endmodule ``` 在这个解法中,adder 模块用于实现 4 位加法器的逻辑,而 testbench 模块用于生成不同的输入信号,并将输出结果打印到终端。在 testbench 中,使用 initial 块定义了一系列输入信号和时间点,模拟了所有可能的输入情况。最后,使用 $finish 关闭仿真过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值