常见面试笔试题-verilog程序库(共14页).docx
《常见面试笔试题-verilog程序库(共14页).docx》由会员分享,可在线阅读,更多相关《常见面试笔试题-verilog程序库(共14页).docx(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上加减法module addsub ( input 7:0 dataa, input 7:0 datab, input add_sub, / if this is 1, add; else subtract input clk, output reg 8:0 result ); always (posedge clk) begin if (add_sub) result = dataa + datab; /or assign cout,sum=dataa+datab; else result 1101,然后和 8,亦即 1000 相加就会得到 5,亦即 0101。至于溢
2、出的最高位可以无视掉。乘法器module mult(outcome,a,b);parameter SIZE=8;inputSIZE:1 a,b;output reg2*SIZE:1 outcome;integer i;always (a or b) begin outcome=0; for(i=0,i=SIZE;i=i+1) if(bi) outcome=outcome+(a(i-1); endendmodule另一种乘法器。在初始化之际,取乘数和被乘数的正负关系,然后取被乘数和乘数的正值。输出结果根据正负关系取得。else if( Start_Sig ) case( i ) 0: begin
3、 isNeg = Multiplicand7 Multiplier7; Mcand = Multiplicand7 ? ( Multiplicand + 1b1 ) : Multiplicand; Mer = Multiplier7 ? ( Multiplier + 1b1 ) : Multiplier; Temp = 16d0; i = i + 1b1; end 1: / Multipling if( Mer = 0 ) i = i + 1b1; else begin Temp = Temp + Mcand; Mer = Mer - 1b1; end 2: begin isDone = 1b
4、1; i = i + 1b1; end 3: begin isDone = 1b0; i = 2d0; end endcase assign Done_Sig = isDone; assign Product = isNeg ? ( Temp + 1b1 ) : Temp; endmodule booth乘法器 module booth_multiplier_module( input CLK, input RSTn, input Start_Sig, input 7:0A, input 7:0B, output Done_Sig, output 15:0Product, output 7:0
5、SQ_a, output 7:0SQ_s, output 16:0SQ_p); reg 3:0i; reg 7:0a; / result of A reg 7:0s; / reverse result of A reg 16:0p; / p空间,16+1位 reg 3:0X;/指示n次循环 reg isDone;always ( posedge CLK or negedge RSTn ) if( !RSTn ) begin i = 4d0; a = 8d0; s = 8d0; p = 17d0; X = 4d0; isDone = 1b0;end else if( Start_Sig ) ca
6、se( i )0: begin a = A; s = ( A + 1b1 ); p = 8d0 , B , 1b0 ; i = i + 1b1; end1: if( X = 8 ) begin X = 4d0; i = i + 4d2; endelse if( p1:0 = 2b01 ) begin p = p16:9 + a , p8:0 ; i = i + 1b1; endelse if( p1:0 = 2b10 ) begin p = p16:9 + s , p8:0 ; i = i + 1b1; endelse i = i + 1b1;/00和11,无操作2:begin p = p16
7、 , p16:1 ; X = X + 1b1; i = i - 1b1; end /右移,最高位补0 or 1.3:begin isDone = 1b1; i = i + 1b1; end4:begin isDone = 1b0; i = 4d0; endendcase assign Done_Sig = isDone; assign Product = p16:1;endmodule 除法器module divider_module( input CLK, input RSTn, input Start_Sig, input 7:0Dividend, input 7:0Divisor, ou
8、tput Done_Sig, output 7:0Quotient, output 7:0Reminder, ); reg 3:0i; reg 7:0Dend; reg 7:0Dsor;reg 7:0Q;reg 7:0R; reg isNeg; reg isDone; always ( posedge CLK or negedge RSTn ) if( !RSTn ) begin i = 4d0; Dend = 8d0; Dsor = 8d0; Q = 8d0; isNeg = 1b0; isDone = 1b0; end else if( Start_Sig ) case( i ) 0: b
9、egin Dend = Dividend7 ? Dividend + 1b1 : Dividend; Dsor = Divisor7 ? Divisor : ( Divisor + 1b1 ); isNeg = Dividend7 Divisor7; i Dend ) begin Q = isNeg ? ( Q + 1b1 ) : Q; i = i + 1b1; endelse begin Dend = Dend + Dsor; Q = Q + 1b1; end2: begin isDone = 1b1; i = i + 1b1; end3: begin isDone = 1b0; i b)
10、begin n=a-b;m=4b0001; state=S1; end else begin m=4b0000;n=a; state=b) begin m=m+1;n=n-b;state=S1;end else begin state=S2;end end S2: begin result=m;yu=n;state=S0;end defule:state=S0; endcase endendmodule13、一个可预置初值的7进制循环计数器verilogmodule count(clk,reset,load,date,out);input load,clk,reset;input3:0 dat
11、e;output reg3:0 out;parameter WIDTH=4d7;always(clk or reset)beginif(reset) out=4d0;else if(load)out=date;else if(out=WIDTH-1) out=4d0;else out=out+1;endendmoduleJohnson计数器约翰逊(Johnson)计数器又称扭环计数器,是一种用n位触发器来表示2n个状态的计数器。它与环形计数器不同,后者用n位触发器仅可表示n个状态。n位二进制计数器(n为触发器的个数)有2n个状态。若以四位二进制计数器为例,它可表示16个状态。“0000-100
12、0-1100-1110-1111-0111-0011-0001-0000-1000”module Johnson(input clk,input clr,output regN-1:0 q);always(posedge clk or negedge clr)if(!clr)q=N1b0else if(!q0)q=1b1,qN-1:1;elseq=1b0,qN-1:1;endmodule任意分频,占空比不为50%always(clk)beginif(count=x-1) count=0;elsecount=count+1;endassign clkout=county/y一般用count的最高
13、位偶数分频(8分频,占空比50%)(计数至n-1,翻转)module count5(reset,clk,out)input clk,reset;output out;reg1:0 count;always(clk)if(reset) begin count=0; out=0; endelse if(count=3)begin count=0;out=!out: endelse count=count+1;endmodule奇数分频电路(占空比50%)。module count5(reset,clk,out)input clk,reset;output out;reg2:0 m,n;reg co
14、unt1;reg count2;always(posedge clk)beginif(reset) beginm=0;count1=0;endelsebegin if(m=4) m=0; else m=m+1; /“4”为分频数NUM-1,NUM=5if(m2) count1=1; else count1=0;endendalways(negedge clk)beginif(reset) beginn=0;count2=0;endelsebegin if(n=4) n=0; else n=n+1;if(n2) count2=1; else count2=0;endendassign out=c
15、ount1|count2;半整数分频module fdiv5_5(clkin,clr,clkout)input clkin,clr;output reg clkout;reg clk1; wire clk2; integer count;xor xor1(clk2,clkin,clk1)always(posedge clkout or negedge clr)beginif(clr) begin clk1=1b0; endelse clk1=clk1;endalways( posedge clk2 or negedge clr)beginif(clr)begin count=0; clkout
16、=1b0; endelse if(count=5)begin count=0; clkout=1b1; endelse begin count=count+1; clkout=1b0; endend endmodule 小数分频N=M/P. N为分配比,M为分频器输入脉冲数,P为分频器输出脉冲数。N=(89+91)/(9+1)=8.1 先做9次8分频再做1次9分频。module fdiv8_1(clkin,rst,clkout) input clkin,rst; output reg clkout; reg3:0 cnt1,cnt2; always(posedge clkin or posed
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常见 试笔 试题 verilog 程序库 14
限制150内