1.四选一多路选择器
sel 00:d3,01:d2,10:d1,11:d0
注:always 中beign end中必须是reg类型
assign 将reg类型数据赋给wire类型
`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output[1:0]mux_out
);
//*************code***********//
reg[1:0]mux_out_reg;
always@(*)begin
case(sel)
2'b00:mux_out_reg=d3;
2'b01:mux_out_reg=d2;
2'b10:mux_out_reg=d1;
2'b11:mux_out_reg=d0;
endcase
end
assign mux_out = mux_out_reg;
//*************code***********//
endmodule
2.异步复位的串联T触发器
异步复位必须为边沿触发,题中rst为低有效,所以为下降沿检测 rst_n
T触发器,输入为1,输出取反,输入为0,输出保持
`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q
);
//*************code***********//
reg q1;
always@(posedge clk or negedge rst)begin
if(~rst)begin
q1<=0;
q<=0;
end
else begin
if(data)
q1<=!q1;
else
q1<=q1;
if(q1)
q<=!q;
else
q<=q;
end
end
//*************code***********//
endmodule
3.奇偶校验
单目运算符(| ^ &)
a=4‘b1010
- c=&a; 检测是否全为1 全为1,c=1’b1
- c=^a; 奇偶校验(检测1的个数是奇数还是偶数) 奇数个1,c=1’b1
- c=|a; 检测是否全为0 全为0,c=1’b1
`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
outpu