Verilog练习参考答案.docx
一、问答题1、下面哪些是正确的用户定义的标识符?对错误的标识符,指出其错误的原因。1)Help2)2nd_itemX,以数字2为首3)casexX,casex是verilog的关键字4)integerX,integer是verilog的关键字5)na36me6)$timeX7)moduleX,modul是everilog的关键字8)'xy_aX,不允许出现字符9)7tyrX,以数字7为首10)myex4mpX,含有空格符2、定义如下的变量和常量1)32位的寄存器变量buflreg31:0bufl;2)整数ageintegerage;3)容量为256单元、字长为16位的存储器memoryreg15:0memory255:0;4)长度为32的向量buf2reg31:0buf2;5)值为50的参数COUNTparameterC0UNT=50;3、写出下列表达式的值1)(20=5)?8:(26>3)?3:932)9/613)4'b11(½4'bllOO01004)4'bll(三4'bllOO15)4'b00114'bllOOIlll6)4'b00114'bllOO17)9%638)4'blll>>200019)4'blll<<21100二、读程序,回答相关问题:1、moduleal(a,b,sei,out);input3:0a,b;input1:0seioutput3:0out;always(aorborsei)case(sei)2'b00:out=a+b;2'bl:out=a;2'biO:out=b2'bll:out=a-b;endcase;endmodule若输入1)a=s"blllb=4'b0100sel=2'b00问输出out=IQll;2)a=s'blllb=4"b0100sel=2,bl问输出out=1011;3)a=s'blllb=4'b0100sel=2,biO问输出out=OlOO;4)a=s'blllb=4'b0100sel=2'bll问输出out=Olll2、'timescale100ns10nsmodulegete2(a,b,out)inputa,boutputout;and#(10.46,5.87)(z,a,b)nor#(9.49,5.37)(out,z,a)endmodulea1540ns¼OIZ590rsout;950ns问:仿真时,当输入a、b从11变到01时,则输出。Ut如何变化?相对a的变化,OUt的变化延时多少时间?延时154OnS3、modulea2(elk,clr,set,a,z);inputa,elk,clr;outputregZ;reg3:0q;always(posedgeelkorposedgeclr)beginif(clr)q<=4'b;elseif(set)q<=4'hf;elsebeginq<<l;q0<=a;endendassignz=q3;endmodule_试判断该逻辑电路的逻辑功能;若输入信号set.clr.elk、a的波形如下,试画出输出4、结构描述电路如下,请画出其逻辑电路。module(a,b,z);inputal:0,bl:0;output1:0zandAl(t,a0,b0),A2(tl,al,blorB(z0,tl,t);bufif1(z1,t,tl);endmodule5、modulea3(data,elk,read,out,sei);input3;0data;inputelk,sei,readoutputout;reg3;0q;always(posedgeelkorread)if(read)q<=data;elseif(sei)beginout<=q0elsebeginout<.q3endmodule试判断该逻辑电路的逻辑功能。三、程序改错1、moduleexaa(clk,d,z);inputd7:0,elk,clr,set可控并入串出的移位寄存器:异步并入:当输入信号read=l时,将四位数据data并行读入寄存器qo可控移位:当sel=l时,并入的数据在Clk作用下,逐位右移从OUt输出;当SerO时,并入的数据在q>>l;endq<<l;endalwaysif(clr)q<=8'b00000000elseif(set)q<=8'hff;elseq7=d;z<=q7endmodule2、moduleexab(a,b,c,d,e)inputa,b,c;outputd,e;nand(a,b,c,d);bufif(c,d,e);not(d,e,a,b);endmodule,outputregz'input7:0d;inputelk,clr,set;outputregZ;reg7:0q;always©(posedgeelk);elsebeginq=d;z<=q7;endendmodulemoduleexab(a,b,c,d,e);nand(d,a,b,c);bufif(e,c,d);not(d,a);endmodule3下面的程序是右下图电路的结构描述。moduleexac(a,b,c,z)inputa,b,c;outputZ;nxorA(a0,al,y);or(b,z0,x);nad(x,al,c);bufifzl,b,y;endmodulemoduleexac(a,b,c,z);input1:0a;inputb,c;output1:0z;xnorA(x,a0,al);orB(z0,b,x);nandC(y,al,c);bufiflzl,y,b;endmodule四、编程题1、用VerilOg设计74138译码电路。教材P178例7.24s2:10,s3:112、已知同步时序电路“1101”序列检测器的状态图如下所示,请用Verihg语言编程实现该电路。(分别用米里型、摩尔型状态机实现)米里型状态机状态转换图状态编码为sO:00,si:01,inputelk,clr,x;outputregz;regl:0state;parameters=2'b00,sl=2,bl;parameters2=2'bll,s3=2,biO;always(POSedgeelkorposedgeclr)beginif(dr)state<=s;elsecase(state)s:beginif(x)state<=sl;elsestate<=s;endsl:beginif(x)state<=s2;elsestate<=s;ends2:beginif(x)state<=s2;elsestate<=s3;ends3:beginif(x)state<=sl;elsestate<=s;enddefualt:state<=s;always©(state)begincase(state)53: beginif(x)z=l,bl;elsez=l,b;defualt:z=l,b;end0/1So/01/p/摩尔型状态机状态转换图状态编码为sO:000,si:Oil,s2:010,s3:OILs4:1111/S20O/S1OO/s3omodulefsmlll(elk,clr,x,z);inputelk,clr,x;outputregz;reg2:0state;parameters=3,b000,sl=3,b001;parameters2=3,b010,s3=3,bll,s4=3'blOO;always(posedgeelkorposedgeclr)beginif(dr)state<=s;elsecase(state)s:beginif(x)state<=sl;elsestate<=s;endsl:beginif(x)state<=s2;elsestate<=s;ends2:beginif(x)state<=s2;elsestate<=s3;ends3:beginif(x)state<=s4;elsestate<=s;ends4:beginif(x)state<=s2;elsestate<=sdefualt:state<=s;always©(state)begincase(state)54: z=l,bl;defualt:z=l,b;end