我们目标是设计一个学号指纹生成器 具体的要求如下: 2. 组长学号中每2位数组成一个字节参与指纹生成运算。例如,组长学号为3012345678,则5个参与指纹运算的字节分别为30h、12h、34h、56h、78h。 3. 指纹运算的操作一共有5种:+、-、AND、OR、XOR。 4. 指纹运算的规则是:取组员学号的后5位,每1位对5取余后,按照以下表格选择运算操作: 数值 操作 0 + 1 - 2 AND 3 OR 4 XOR 例如,组员学号是3087654321,取后5位每位对5取余,得到的序列是04321,则应依次做+、XOR、OR、AND、-的运算操作。 5. 在进行指纹运算操作时,首先将结果寄存器R清零,然后进行5次如下的迭代运算: R?R (op) X 迭代运算中的(op)依次为(4)中获得的各种操作,X依次为(2)中获得的各位运算字节。 例如,在以上例子中,对组长的学号应进行如下的指纹计算: R?R + 30h R?R XOR 12h R?R OR 34h R?R AND 56h R?R - 78h 代码如下,我们确定只有mateinput和calc模块有问题,问题主要是在calc模块中,i和j的值不能增加 就是i=i+1 j=j-1不起作用 我们对verilog的赋值语法规则一直很茫然。 在mateinput中任一处增加j=0的赋值语句就会出现错误,不能通过编译请各位大侠指教!!
module top(clk,s,btn,digit_anode,segment); input clk,s; input [3:0] btn; output [3:0] digit_anode; output [7:0] segment;
wire [19:0] outnum; wire clk_1ms; wire [3:0] btn_out; wire [15:0] register; wire [15:0] register2; Timer_1ms M0(clk,clk_1ms); anti_jitter M1(clk,clk_1ms,btn,btn_out); mateinput M2(clk,s,btn_out[1],btn_out[2],outnum,register2); calc M3(s,outnum,btn[0],btn_out[3],register); display M4(clk,s,register,register2,digit_anode,segment); //用S控制输出register还是register2 endmodule module mateinput(clk,s,btn1,btn2,outnum,register2); //这个模块读入组员的学号,注意只输入后五位,如果输多的话会覆盖前边 input clk,s; input btn1,btn2; output [19:0] outnum; output [15:0] register2;
reg [3:0] tmp [4:0]; //类似数组 reg [3:0] i; reg [3:0] j; //j用于控制当前输入值,显示在数码管最后一位,btn1增加值,btn2移到下一位
initial begin tmp[0]=4'b0000; tmp[1]=4'b0000; tmp[2]=4'b0000; tmp[3]=4'b0000; tmp[4]=4'b0000; i=4'b0100; j=4'b0000; end
always@(posedge btn1) begin if(s==1) begin if(j==9) j=0; else j=j+1; end end
always@(posedge btn2) begin if(s==1) begin tmp=j; //j<=4'b0000; if(i==0) i=4; else i=i-1; end end
assign outnum[19:16]=tmp[4]; assign outnum[15:12]=tmp[3]; assign outnum[11:8]=tmp[2]; assign outnum[7:4]=tmp[1]; assign outnum[3:0]=tmp[0]; assign register2=j;
endmodule module calc(s,outnum,btn0,btn3,register); //根据组员学号后五位计算,组长学号内置 input [19:0] outnum; input btn0,btn3,s; output [15:0] register;
reg [7:0] leader [4:0]; reg [15:0] register=0; reg [3:0] i; reg [3:0] j;
always@(posedge btn3) begin //组长学号在这里更改,btn3键用于刷新,j用于控制 leader[4]=8'h02; leader[3]=8'h02; leader[2]=8'h03; leader[1]=8'h04; leader[0]=8'h05; i=4'b0101; j=4'b0000; end
always@(posedge btn0) begin if(s==0) begin
i=i-1; j=j+1; if(j==1) begin case(outnum[19:16]) 4'b0000:register=register+leader; 4'b0001:register=register-leader; 4'b0010:register=register&leader; 4'b0011:register=register|leader; 4'b0100:register=register^leader; 4'b0101:register=register+leader; 4'b0110:register=register-leader; 4'b0111:register=register&leader; 4'b1000:register=register|leader; 4'b1001:register=register^leader; endcase end else if(j==2) begin case(outnum[15:12]) 4'b0000:register=register+leader; 4'b0001:register=register-leader; 4'b0010:register=register&leader; 4'b0011:register=register|leader; 4'b0100:register=register^leader; 4'b0101:register=register+leader; 4'b0110:register=register-leader; 4'b0111:register=register&leader; 4'b1000:register=register|leader; 4'b1001:register=register^leader; endcase
end else if(j==3) begin case(outnum[11:8]) 4'b0000:register=register+leader; 4'b0001:register=register-leader; 4'b0010:register=register&leader; 4'b0011:register=register|leader; 4'b0100:register=register^leader; 4'b0101:register=register+leader; 4'b0110:register=register-leader; 4'b0111:register=register&leader; 4'b1000:register=register|leader; 4'b1001:register=register^leader; endcase
end else if(j==4) begin case(outnum[7:4]) 4'b0000:register=register+leader; 4'b0001:register=register-leader; 4'b0010:register=register&leader; 4'b0011:register=register|leader; 4'b0100:register=register^leader; 4'b0101:register=register+leader; 4'b0110:register=register-leader; 4'b0111:register=register&leader; 4'b1000:register=register|leader; 4'b1001:register=register^leader; endcase
end else if(j==5) begin case(outnum[3:0]) 4'b0000:register=register+leader; 4'b0001:register=register-leader; 4'b0010:register=register&leader; 4'b0011:register=register|leader; 4'b0100:register=register^leader; 4'b0101:register=register+leader; 4'b0110:register=register-leader; 4'b0111:register=register&leader; 4'b1000:register=register|leader; 4'b1001:register=register^leader; endcase end else begin end
end if(s==1) register=0; end
endmodule module anti_jitter(clk,clk_1ms,btn,btn_out); input clk; input [3:0] btn; input clk_1ms; output [3:0] btn_out; reg [3:0] cnt_aj=0; reg [3:0] btn_old=0; reg [3:0] btn_out;
always@(posedge clk) begin if(btn!=btn_old) begin cnt_aj<=4'b0000; btn_old<=btn; end else begin if(clk_1ms) begin if(cnt_aj==4'b1111) begin cnt_aj<=4'b0000; btn_out<=btn; end cnt_aj<=cnt_aj+1; end end end
endmodule module Timer_1ms(clk,clk_1ms); input clk; output clk_1ms; reg clk_1ms; reg [15:0] cnt; initial begin cnt[15:0]<=0; end
always@(posedge clk) begin if(cnt>=49999) begin cnt<=0; clk_1ms<=1; end else begin cnt<=cnt+1; clk_1ms<=0; end end
endmodule module display(clk,s,register,register2,digit_anode,segment); input clk,s; input [15:0] register; input [15:0] register2; output [3:0] digit_anode; output [7:0] segment;
reg [3:0] digit_anode; reg [7:0] segment; reg [11:0] cnt=0; reg [3:0] num; reg [15:0] disp_num=0;
always@(posedge clk) begin if(s==1) disp_num<=register2; else disp_num<=register; end
always@(posedge clk) begin case(cnt[11:10]) 2'b00:begin digit_anode<=4'b1110; num<=disp_num[3:0]; end 2'b01:begin digit_anode<=4'b1101; num<=disp_num[7:4]; end 2'b10:begin digit_anode<=4'b1011; num<=disp_num[11:8]; end 2'b11:begin digit_anode<=4'b0111; num<=disp_num[15:11]; end endcase
case(num) 4'b0000:segment<=8'b11000000; 4'b0001:segment<=8'b11111001; 4'b0010:segment<=8'b10100100; 4'b0011:segment<=8'b10110000; 4'b0100:segment<=8'b10011001; 4'b0101:segment<=8'b10010010; 4'b0110:segment<=8'b10000010; 4'b0111:segment<=8'b11111000; 4'b1000:segment<=8'b10000000; 4'b1001:segment<=8'b10010000; 4'b1010:segment<=8'b10001000; 4'b1011:segment<=8'b10000011; 4'b1100:segment<=8'b11000110; 4'b1101:segment<=8'b10100001; 4'b1110:segment<=8'b10000110; 4'b1111:segment<=8'b10001110; endcase end
always@(posedge clk) begin cnt<=cnt+1; end
endmodule
[此贴子已经被作者于2009-1-14 16:28:56编辑过] |