第2章简单系统设计_第1页
第2章简单系统设计_第2页
第2章简单系统设计_第3页
第2章简单系统设计_第4页
第2章简单系统设计_第5页
已阅读5页,还剩85页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第二章 简单系统设计 Verilog HDL基本的硬件描述方式: 1.结构描述 2.数据流描述 3.行为描述 2选1多路选择器元件图2选1多路选择器逻辑电路图 2选1多路选择器设计 2选1数据选择器 modulemodule mux2_1(a, b, s, y); inputinput a, b, s; / 定义输入端口 outputoutput y; / 定义输出端口 assign assign y = (s = 0) ? a : b; endmoduleendmodule 第第2章章 简单系统设计简单系统设计 2.1 组合逻辑电路设计组合逻辑电路设计 2.2 时序逻辑电路设计时序逻辑电路设

2、计 2.3 双向电路和三态控制电路设计双向电路和三态控制电路设计 2.4 有限状态机设计有限状态机设计 2.5 数字系统仿真数字系统仿真 2.1 组合逻辑电路设计组合逻辑电路设计 2.1.1多路选择器的逻辑表达式描述 modulemodule mux2_1(a, b, s, y); inputinput a, b, s; / 定义输入端口 outputoutput y; / 定义输出端口 assignassign y = (a endmoduleendmodule 多路选择器的门原语描述 modulemodule mux2_1(a, b, s, y); inputinput a, b, s;

3、/ 定义输入端口 outputoutput y; / 定义输出端口 wirewire ns, as, bs; notnot(ns, s); andand(as, a, ns); andand(bs, b, s); oror (y, as, bs); endmoduleendmodule 多路选择器的条件语句描述 modulemodule mux2_1(a, b, s, y); inputinput a, b, s; / 定义输入端口 outputoutput y; / 定义输出端口 regreg y; alwaysalways (a, b, s) beginbegin ifif(!s) y =

4、 a; elseelse y = b; endend endmoduleendmodule 多路选择器的仿真输出 2.1.2 Verilog HDL模块的模板 modulemodule(); inputinput input_port_list; outputoutput output_port_list; wirewire result_signal; assignassign = ; alwaysalways () beginbegin / if sentence / other sentence endend (); gate_type_keywordgate_type_keyword

5、(); endmoduleendmodule 2.1.3 常用组合逻辑电路设计常用组合逻辑电路设计 一、 全加器设计 半加器模块设计 modulemodule half_adder(a, b, so, co); inputinput a, b; outputoutput so, co; assignassign so = a b; assignassign co = a endmoduleendmodule 全加器顶层设计 modulemodule adder_top(a, b, ci, so, co); inputinput a, b, ci; outputoutput so, co; wi

6、rewire c1, c2; half_adderhalf_adder u1(.a(a), .b(b), .so(s1), .co(c1); half_adderhalf_adder u2(.a(s1), .b(ci), .so(so), .co(c2); assignassign co = c1 | c2; endmoduleendmodule 全加器仿真结果 使用行为描述的全加器 modulemodule adder(a, b, ci, so, co); parameterparameter widthwidth = 8; inputinput widthwidth 1 : 0 a; in

7、putinput widthwidth 1 : 0 b; inputinput ci; outputoutput widthwidth - 1 : 0 so; outputoutput co; wirewire widthwidth : 0 sum; assignassign sum = a + b + ci; assignassign so = sumwidthwidth 1 : 0; assignassign co = sumwidthwidth; endmoduleendmodule 全加器行为描述的综合结果 全加器行为描述的仿真结果 二、编译码器二、编译码器 1编码器编码器 (1)使用

8、)使用case语句语句 程序是使用程序是使用case语句进行描述的编码器语句进行描述的编码器 的源程序。的源程序。 使用分支语句的编码器 modulemodule encoder(a0, a1, a2, a3, y0, y1); inputinput a0, a1, a2, a3; outputoutput y0, y1; wirewire 3:0 a; regreg 1:0 y; assignassign a = a3, a2, a1, a0; assignassign y0 = y0; assignassign y1 = y1; always always (a) beginbegin c

9、asecase(a) 4b0001 : y = 2b00; 4b0010 : y = 2b01; 4b0100 : y = 2b10; 4b1000 : y = 2b11; defaultdefault : y = 2bxx; endcaseendcase endend endmoduleendmodule 使用分支语句的编码器综合结果 使用分支语句的编码器仿真结果 使用条件语句的编码器 modulemodule encoder(a0, a1, a2, a3, y0, y1); inputinput a0, a1, a2, a3; outputoutput y0, y1; regreg 1:0

10、 y; assignassign y0 = y0; assignassign y1 = y1; alwaysalways (a0, a1, a2, a3) beginbegin ifif(a0) y = 2b00; elseelse ifif(a1) y = 2b01; elseelse ifif(a2) y = 2b10; elseelse y = 2b11; endend endmoduleendmodule 使用条件语句的编码器综合结果 使用条件语句的编码器仿真结果 三、译码器设计 modulemodule decoder(a, y); inputinput 2:0 a; outputo

11、utput 7:0 y; regreg 7:0 y; alwaysalways (a) beginbegin casecase(a) 3b000 : y = 8b00000001; 3b001 : y = 8b00000010; 3b010 : y = 8b00000100; 3b011 : y = 8b00001000; 3b100 : y = 8b00010000; 3b101 : y = 8b00100000; 3b110 : y = 8b01000000; 3b111 : y = 8b10000000; endcaseendcase endend endmoduleendmodule

12、译码器仿真输出 2.2 时序逻辑电路设计 2.2.1 简单D触发器设计 modulemodule d_flip_flop(d, ck, q); inputinput d, ck; outputoutput q; regreg q; alwaysalways (posedgeposedge ck) q = d; endmoduleendmodule 1 带有异步清零和异步置位的D触发器设计 modulemodule d_flip_flop(aclr, aset, d, ck, q); inputinput aclr, aset, d, ck; outputoutput q; regreg q;

13、alwaysalways (posedgeposedge aclr oror posedgeposedge aset oror posedgeposedge ck) beginbegin ifif(aclr) q = 1b0; elseelse ifif(aset) q = 1b1; elseelse q = d; endend endmoduleendmodule 2 带有同步清零和同步置位的D触发器设计 modulemodule d_flip_flop(sclr, sset, d, ck, q); inputinput sclr, sset, d, ck; outputoutput q;

14、regreg q; alwaysalways (posedgeposedge ck) beginbegin ifif(sclr) q = 1b0; elseelse ifif(sset) q = 1b1; elseelse q = d; endend endmoduleendmodule 2.2.2 D触发器触发器Verilog HDL描述的语言现象说明描述的语言现象说明 一、一、Verilog HDL语言中触发器的生成语言中触发器的生成 从从D触发器程序中可以看到,触发器程序中可以看到,Verilog HDL描述描述D触发器时,触发器时, 首先需要定义一个首先需要定义一个reg型的变量,而这

15、个变量的名字可以是输出型的变量,而这个变量的名字可以是输出 端口,也可以是内部信号。定义好这样一个变量后,再通过一个端口,也可以是内部信号。定义好这样一个变量后,再通过一个 always语句块来描述电路的时序行为。语句块来描述电路的时序行为。 二、边沿敏感信号类型以及同步、异步操作二、边沿敏感信号类型以及同步、异步操作 Verilog HDL语言中使用语言中使用posedge和和negedge关键字来关键字来 描述一个边沿敏感型信号,分别表示上升沿(描述一个边沿敏感型信号,分别表示上升沿(positive edge) 和下降沿(和下降沿(negative edge)。)。posedge和和ne

16、gedge关键字除关键字除 了用于描述边沿敏感型信号以外,还可以用在异步事件的描述了用于描述边沿敏感型信号以外,还可以用在异步事件的描述 中,这时中,这时posedge一般表示一个异步事件在信号高电平时有效,一般表示一个异步事件在信号高电平时有效, 而而negedge则表示异步事件在信号低电平时有效。则表示异步事件在信号低电平时有效。 2.2.3 常用同步常用同步 时序逻辑电路的设计时序逻辑电路的设计 1 数据锁存器设计 modulemodule DataLatch(Latch, DataIn, DataOut); parameterparameter widthwidth = 8; inpu

17、tinput Latch; inputinput widthwidth - 1 : 0 DataIn; outputoutput widthwidth - 1 : 0 DataOut; regreg widthwidth - 1 : 0 DataOut; alwaysalways (Latch, DataIn) ifif(Latch) DataOut = DataIn; endmoduleendmodule 2 数据锁存器综合结果 W: CL118 :C:ProgramsDataLatchDataLatch.v:11:4:11:5|Latch generated from always blo

18、ck for signal DataOut7:0, probably caused by a missing assignment in an if or case statement. 3 数据寄存器设计 modulemodule DataRegister(nReset, Clock, DataIn, DataOut); parameterparameter widthwidth = 8; inputinput nReset; inputinput Clock; inputinput widthwidth - 1 : 0 DataIn; outputoutput widthwidth - 1

19、 : 0 DataOut; regreg widthwidth - 1 : 0 DataOut; alwaysalways (negedgenegedge nReset oror posedgeposedge Clock) ifif(!nReset) DataOut = 0; elseelse DataOut = DataIn; endmoduleendmodule 数据寄存器综合结果 简单加法计数器设计 modulemodule Counter(nReset, Clock, Data); inputinput nReset; inputinput Clock; outputoutput 7:

20、0 Data; regreg 7:0 Data; alwaysalways (negedgenegedge nReset oror posedgeposedge Clock) beginbegin ifif(!nReset) Data = 8h00; elseelse Data = Data + 1; endend endmoduleendmodule 简单加法计数器综合结果 简单加法计数器仿真结果 具有同步复位的加法计数器设计 modulemodule Counter(nReset, Clock, Data); inputinput nReset; inputinput Clock; out

21、putoutput 7:0 Data; regreg 7:0 Data; alwaysalways (posedgeposedge Clock) beginbegin ifif(!nReset) Data = 8h00; elseelse Data = Data + 1; endend endmoduleendmodule 具有同步复位的加法计数器综合结果 具有同步复位的加法计数器仿真结果 具有异步复位和同步使能的加减计数器设计 modulemodule Counter(nReset, Enable, Direction, Clock, Data); inputinput nReset, En

22、able, Direction, Clock; outputoutput 7:0 Data; regreg 7:0 Data; alwaysalways (negedgenegedge nReset oror posedgeposedge Clock) beginbegin ifif(!nReset) Data = 0; elseelse beginbegin ifif(Enable) beginbegin ifif(Direction) Data = Data + 1; elseelse Data = Data - 1; endend endend endend endmoduleendmo

23、dule 具有异步复位和同步使能的加减计数器综合结果 具有异步复位和同步使能的加减计数器仿真结果 同步使同步使 能能 异步复异步复 位位 计数方向控制计数方向控制 2.2.4 异步时序逻辑电路 modulemodule AsynchronousSequentialCircuit(A, D, Clock, Q); inputinput A, D, Clock; outputoutput Q; regreg Q1, Q2; assignassign Q = Q2; alwaysalways (posedgeposedge Clock) Q1 = (Q2 | A); alwaysalways (po

24、sedgeposedge Q1) Q2 = D; endmoduleendmodule 简单异步时序逻辑电路综合结果 W:c:programsasynchronoussequentialcircuitasynchronousseq uentialcircuit.v:9:2:9:7|Net Q1 appears to be a clock source which was not identified. Assuming default frequency. 2.3 双向电路和三态控制电路设计双向电路和三态控制电路设计 2.3.1 三态门设计 modulemodule TriGate(Enabl

25、e, DataIn, DataOut); inputinput Enable; inputinput 7:0 DataIn; outputoutput 7:0 DataOut; assignassign DataOut = Enable ? DataIn : 8bzzzz_zzzz; endmoduleendmodule 三态门设计 三态门设计 一般地,可以将z赋给某一输出端口或变量来获得三态 控制门电路,一个z表示一个逻辑位。 在综合时z是一个不确定的值,不同的综合器可能会给 出不同的结果,因而对于综合前的行为仿真结果与综合后 的功能仿真结果也可能是不同的,有时虽然能通过综合, 但却不能获得

26、正确的时序仿真结果,所以建议尽可能不要 将z用作比较值,或者用于表达式或操作数。 无论是设计外围端口上的三态门还是设计器件内部的三态 门,在Verilog HDL表达上都是一样的,在综合与仿真上 也不会有问题。但如果最终实现的目标器件是 FPGA/CPLD器件,是否能被适配进去,则必须根据具体 器件系列来确定 。 2.3.2 双向端口设计 modulemodule BiDirPort(Control, DataIn, BiDirQ, TriStateQ); inputinput Control; inputinput DataIn; inoutinout BiDirQ; outputoutpu

27、t TriStateQ; assignassign TriStateQ = Control ? 1bz : BiDirQ; assignassign BiDirQ = Control ? DataIn : 1bz; endmoduleendmodule 双向端口设计 双向端口设计 双向端口设计 用inoutinout模式设计双向端口也必须考虑三态的使用, 因为双向端口的设计与三态端口的设计十分相似, 都必须考虑端口的三态控制。这是由于双向端口 在完成输入功能时,必须使原来呈输出模式的端 口呈高阻态,否则,待输入的外部数据势必会与 端口处原有电平发生“线与”,导致无法将外部 数据正确地读入,从而

28、实现“双向”的功能。 在端口BiDirQ履行输入功能时,将其设定为高阻 态输出,使BiDirQ成为真正的双向端口 。 2.3.3三态总线电路设计 modulemodule MultipleDriver(Enable, DataIn0, DataIn1, DataIn2, DataIn3, DataOut); inputinput 1:0 Enable; inputinput DataIn0, DataIn1, DataIn2, DataIn3; outputoutput DataOut; regreg DataOut; alwaysalways (Enable, DataIn0, DataIn

29、1, DataIn2, DataIn3) beginbegin ifif(Enable = 2b00) DataOut = DataIn0; elseelse DataOut = 1bz; ifif(Enable = 2b01) DataOut = DataIn1; elseelse DataOut = 1bz; ifif(Enable = 2b10) DataOut = DataIn2; elseelse DataOut = 1bz; ifif(Enable = 2b11) DataOut = DataIn3; elseelse DataOut = 1bz; endend endmodule

30、endmodule 三态总线电路设计 三态总线电路设计 除了DataIn3外,其余3个1位输入端都悬空 没能用上,显然是因为恰好将DataIn3安排作为 always语句块中DataOut的最后一个激励信号的 原因。 三态总线电路设计 modulemodule MultipleDriver(Enable, DataIn0, DataIn1, DataIn2, DataIn3, DataOut); inputinput 1:0 Enable; inputinput DataIn0, DataIn1, DataIn2, DataIn3; outputoutput DataOut; assignas

31、sign DataOut = (Enable = 2b00) ? DataIn0 : 1bz; assignassign DataOut = (Enable = 2b01) ? DataIn1 : 1bz; assignassign DataOut = (Enable = 2b10) ? DataIn2 : 1bz; assignassign DataOut = (Enable = 2b11) ? DataIn3 : 1bz; endmoduleendmodule 三态总线电路设计 三态总线电路设计 由于在module模块中使用了4个并列的 assign并行语句,因此能综合正确电路结构。 对于

32、同一信号有并行的4个赋值源,在实际 电路上完全可能发生“线与”。 2.4 有限状态机设计 时序机 两种类型的有限状态机 MealyMealy型有限状态机型有限状态机 MooreMoore型有限状态机型有限状态机 时序机的描述方式 时序图 状态转移表 状态转移图 算法状态机图 时序机的描述方式 时序图举例 静态随机存储器写周期时序图 状态转移图 FSM的状态转移图(STG,State Transition Graph)是 一种有向图,图中带有标记的节点或顶点,并与时序机的状 态一一对应。当系统处于弧线起点的状态时,用有向边或弧 线表示在输入信号的作用下可能发生的状态转移。 对于一个同步时序机的给

33、定STG,设计的任务就是确定下 一状态和输出逻辑。 对于有效的STG而言,其每个顶点必须表示一个惟一的状 态;每个弧线则表示在指定输入信号的作用下,从给定状态 到下一个状态的转移,并且从一个节点出发的各弧线必须对 应一个惟一的输入。 状态转移图设计举例 本例中一个串行发送的BCD码Bin被转换成一个余3 码串行比特流Bout,给BCD码对应的十进制数加上3,并 将其转换为等价的二进制数就得到了该十进制数的余3 码。 BCD - 余3码转换器状态转移图 BCD - 余3码转换器程序(1) modulemodule BCD2Excess3(nReset, Clock, Bin, Bout); in

34、putinput nReset; inputinput Clock; inputinput Bin; outputoutput Bout; regreg Bout; parameterparameter S0 = 7b0000001, S1 = 7b0000010, S2 = 7b0000100, S3 = 7b0001000, S4 = 7b0010000, S5 = 7b0100000, S6 = 7b1000000; regreg 6:0 State; alwaysalways (negedgenegedge nReset oror posedgeposedge Clock) begin

35、begin ifif(!nReset) Reset; elseelse DoFSM; endend tasktask Reset; beginbegin Bout = 1b0; State = S0; endend endtaskendtask BCD - 余3码转换器程序(2) tasktask DoFSM; beginbegin casecase(State) S0 : beginbegin ifif(Bin) beginbegin Bout = 1b0; State = S2; endend elseelse beginbegin Bout = 1b1; State = S1; ende

36、nd endend S1 : beginbegin ifif(Bin) beginbegin Bout = 1b0; State = S4; endend elseelse beginbegin Bout = 1b1; State = S3; endend endend S2 : beginbegin Bout = Bin; State = S4; endend S3 : beginbegin Bout = Bin; State = S5; endend S4 : beginbegin ifif(Bin) beginbegin Bout = 1b0; State = S6; endend el

37、seelse beginbegin Bout = 1b1; State = S5; endend endend S5 : beginbegin Bout = Bin; State = S0; endend S6 : beginbegin ifif(Bin = 0) Bout = 1b1; State = S0; endend defaultdefault : beginbegin Bout = 1b0; State = S0; endend endcaseendcase endend endtaskendtask endmoduleendmodule BCD - 余3码转换器综合结果 BCD

38、- 余3码转换器综合后的状 态转移图 BCD - 余3码转换器仿真结果 串行线码介绍 NRZ - Manchester串行线码转换器 状态转移图 NRZ - Manchester串行线码转换器 程序 modulemodule NRZ_To_Manchester(nReset, Clock, Bin, Bout); inputinput nReset; inputinput Clock; inputinput Bin; outputoutput Bout; regreg Bout; parameterparameter S0 = 3b001, S1 = 3b010, S2 = 3b100; re

39、greg 2:0 State; alwaysalways (negedgenegedge nReset oror posedgeposedge Clock) beginbegin ifif(!nReset) beginbegin Bout = 1b0; State = S0; endend elseelse beginbegin casecase(State) S0 : beginbegin Bout = Bin; ifif(Bin) State = S2; elseelse State = S1; endend S1 : beginbegin Bout = 1b1; State = S0;

40、endend S2 : beginbegin Bout = 1b0; State = S0; endend defaultdefault : beginbegin Bout = 1b0; State = S0; endend endcaseendcase endend endend endmoduleendmodule NRZ - Manchester串行线码转换器 综合结果 NRZ - Manchester串行线码转换器 仿真结果 算法状态机(ASM,Algorithm State Machine)图 序列检测器 序 列 检 测 器 ASM 图 序列检测器程序 modulemodule Se

41、quenceDetect(Reset, Enable, nClock, Din, Dout); inputinput Reset; inputinput Enable; inputinput nClock; inputinput Din; outputoutput Dout; regreg Dout; parameterparameter Idle = 4b0001, S0 = 4b0010, S1 = 4b0100, S2 = 4b1000; regreg 3:0 State; alwaysalways (negedgenegedge nClock) beginbegin ifif(Rese

42、t) State = Idle; elseelse beginbegin casecase(State) Idle : beginbegin Dout = 1b0; ifif(Enable elseelse ifif(Enable elseelse State = Idle; endend S0 : beginbegin Dout = 1b0; ifif(Din) State = S1; elseelse State = S0; endend S1 : beginbegin Dout = 1b0; ifif(Din) State = S2; elseelse State = S0; endend S2 : beginbegi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论