`timescale 1ns/1ns `include \module alutext; wire[3:0] out;
reg[3:0] a1,a2,a3,a4,a5,a6,a7,a8; reg[2:0] opcode; initial begin
a1={$random}; a2={$random}; a3={$random}; a4={$random}; a5={$random}; a6={$random}; a7={$random}; a8={$random}; repeat(100) begin
#100 opcode={$random}%8; a1={$random}; a2={$random}; a3={$random}; a4={$random}; a5={$random}; a6={$random}; a7={$random}; a8={$random}; end
#100 $stop; end
alu alu(out,opcode,a1,a2,a3,a4,a5,a6,a7,a8); endmodule
实验六 在 Verilog HDL中使用函数
实验目的
设计一个带控制端的逻辑运算电路,分别完成正整数的平方、立方和最大数为5的阶乘运算。
实验仿真结果
实验代码
主程序
module tryfunct(clk,n,result1,result2,result3,reset); output[31:0]result1,result2,result3; input[3:0]n; input reset,clk;
reg[31:0]result1,result2,result3; always@(posedge clk) begin
if(!reset) begin
result1<=0; result2<=0; result3<=0; end else begin
result1<=fun1(n); result2<=fun2(n); result3<=fun3(n); end end
function[31:0]fun1;
input[3:0]operand;
fun1=operand*operand; endfunction
function[31:0]fun2; input[3:0]operand; begin
fun2=operand*operand; fun2=operand*fun2; end
endfunction
function[31:0]fun3; input[3:0]operand; reg[3:0]index; begin fun3=1;
if(operand<11)
for(index=2;index<=operand;index=index+1) fun3=index*fun3; else
for(index=2;index<=10;index=index+1) fun3=index*fun3; end endfunction
endmodule
测试程序
`include\`timescale 1ns/100ps module tryfunctTop; reg[3:0] n,i; reg reset,clk;
wire[31:0]result1,result2,result3; initial begin clk=0; n=0; reset=1;
#100 reset=0; #100 reset=1;
for(i=0;i<=15;i=i+1) begin
#200 n=i; end
#100 $stop; end
always#50 clk=~clk;
tryfunct m(.clk(clk),.n(n),.result1(result1),.result2(result2),.result3(result3),.reset(reset)); endmodule
实验七 在Verilog HDL中使用任务(task)
实验目的
用两种不同方法设计一个功能相同的模块,该模块能完成四个8位2进制输入数据的冒泡排序。第一种,模仿原题例子中用纯组合逻辑实现;第二种,假设8位数据是按照时钟节拍串行输入的,要求用时钟触发任务的执行法,每个时钟周期完成一次数据交换操作。
实验仿真结果
实验代码
主程序1
module rank(ra,rb,rc,rd,a,b,c,d); output[7:0]ra,rb,rc,rd; input[7:0]a,b,c,d;
reg[7:0]ra,rb,rc,rd,va,vb,vc,vd,tmp; reg i;
always@(a or b or c or d) begin
{va,vb,vc,vd}={a,b,c,d}; repeat(7) begin
exchange(va,vb); exchange(vb,vc); exchange(vc,vd); end
{ra,rb,rc,rd}={va,vb,vc,vd}; end
task exchange; inout[7:0] x,y; reg[7:0] tmp; if(x>y) begin tmp=x; x=y; y=tmp;