四选一

`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] temp;
always@(*)begin
if(sel==2'd0) temp=d3;
else if(sel==2'd1) temp=d2;
else if(sel==2'd2) temp=d1;
else temp=d0;
end
assign mux_out=temp;
//*************code***********//
endmodule
异步串联T触发器

设置一个中间变量记录第一个T触发器的输出。
`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q
);
//*************code***********//
reg q1;
always@(posedge clk or negedge rst)
if(!rst)
q1 <= 1'd0;
else
begin
q1 <= data ^ q1;
end
always@(posedge clk or negedge rst)
if(!rst)
q = 0;
else
begin
q = q1 ^ q;
end
//*************code***********//
endmodule
奇偶校验

使用归约运算符即可
`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
//*************code***********//
assign check=^bus^(!sel);
//*************code***********//
endmodule
移位运算与乘法

声明一个reg变量当作计数器即可。
`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]cnt;
reg [10:0]cur;
initial begin
cnt<=0;
end
always@(posedge clk or negedge rst)begin
if(!rst) begin
input_grant<=0;
cnt<=0;
cur<=0;
out<=0;
end
else begin
if(input_grant==1)
input_grant<=0;
if(cnt==0)begin
out<=d;
cur<=d;
input_grant<=1;
end
if(cnt==1)
out<=cur*3;
if(cnt==2)
out<=cur*7;
if(cnt==3) begin
out<=cur*8;
end
cnt<=cnt+1;
end
end
//*************code***********//
endmodule
位拆分与位运算

在reg变量后加上位的范围即可
`timescale 1ns/1ns
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
output [4:0]out,
output validout
);
//*************code***********//
reg [4:0]temp;
reg valid;
reg [15:0]data;
always@(posedge clk or negedge rst)begin
if(!rst)begin
temp<=0;
valid<=0;
end
else begin
if(sel==0) begin
valid<=0;
data<=d;
temp<=0;
end
else if(sel==1) begin
valid<=1;
temp<=(data[3:0]+data[7:4]);
end
else if(sel==2) begin
valid<=1;
temp<=(data[3:0]+data[11:8]);
end
else if(sel==3) begin
valid<=1;
temp<=(data[3:0]+data[15:12]);
end
end
end
assign out=temp;
assign validout=valid;
//*************code***********//
endmodule
多功能数据处理器

使用case语句即可
`timescale 1ns/1ns
module data_select(
input clk,
input rst_n,
input signed[7:0]a,
input signed[7:0]b,
input [1:0]select,
output reg signed [8:0]c
);
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
c<=0;
end
else begin
case (select)
0:c<=a;
1:c<=b;
2:c<=a+b;
3:c<=a-b;
endcase
end
end
endmodule
求两个数的差值

使用if else语句判断两数的大小
`timescale 1ns/1ns
module data_minus(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output reg [8:0]c
);
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
c<=0;
end
else begin
if(a>b)
c<=a-b;
else
c<=b-a;
end
end
endmodule
使用generate-for语句简化代码

generate for语句只是起到简化代码的作用,不能等同于循环语句,不能对同一个变量重复赋值和运算。
`timescale 1ns/1ns
module gen_for_module(
input [7:0] data_in,
output [7:0] data_out
);
genvar i;
generate
for(i=0;i<=7;i=i+1)
assign data_out[i]=data_in[7-i];
endgenerate
endmodule
使用子模块实现三输入大小比较

verilog中,子模块不需要语法上的特别声明,在主模块中调用的模块就被视为子模块。注意输入用wire类型。
`timescale 1ns/1ns
module main_mod(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
input [7:0]c,
output [7:0]d
);
wire [7:0]min_ac;
wire [7:0]min_ab;
wire [7:0]min_abc;
min ab(
.clk (clk),
.rst_n(rst_n),
.a (a),
.b (b),
.c (min_ab)
);
min ac(
.clk(clk),
.rst_n(rst_n),
.a (a),
.b (c),
.c (min_ac)
);
min all(
.clk(clk),
.rst_n(rst_n),
.a (min_ab),
.b (min_ac),
.c (min_abc)
);
assign d=min_abc;
endmodule
module min(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output [7:0]c
);
reg [7:0] res;
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
res<=0;
end
else begin
res<=a<b?a:b;
end
end
assign c=res;
endmodule
使用函数实现数据大小端转换

`timescale 1ns/1ns
module function_mod(
input clk,
input rst_n,
input [3:0]a,
input [3:0]b,
output [3:0]c,
output [3:0]d
);
assign c=reverse(a);
assign d=reverse(b);
function [3:0]reverse;
input [3:0]a;
integer i;
for( i=0;i<4;i=i+1)
reverse[i]=a[3-i];
endfunction
endmodule
903

被折叠的 条评论
为什么被折叠?



