FPGA作业二

来源:互联网 发布:淘宝网电器城五金 编辑:程序博客网 时间:2024/06/08 18:00

FPGA作业二


一 Verilog设计作业1中功能电路

1 4-16译码器

使用两种方法实现3-8译码器。一种参考夏宇闻 《verilog数字系统设计教程》的译码器写法,使用了左移运算符,另一种使用case语句

1.1使用左移运算符完成4-16译码器

1.1.1代码
module decoder_v(input [3:0] data_in,output reg [15:0]data_out);always @(data_in)begin data_out=1'b1<<data_in; endendmodule
1.1.2RTL电路

如图可看出,使用左移运算符将1’b1向左移动data_in位,用data_out输出。输出为高电平。
这里写图片描述

1.1.3vwf波形

data_in和data_out没有同时改变;由于竞争冒险现象的存在,有毛刺产生这里写图片描述

1.2使用case语句完成4-16译码器

1.2.1代码
module decoder(input [3:0] data_in,output reg [15:0]data_out);always @(data_in)begin    case(data_in)    4'b0000:begin data_out=16'b0000_0000_0000_0001; end    4'b0001:begin data_out=16'b0000_0000_0000_0010; end    4'b0010:begin data_out=16'b0000_0000_0000_0100; end    4'b0011:begin data_out=16'b0000_0000_0000_1000; end    4'b0100:begin data_out=16'b0000_0000_0001_0000; end    4'b0101:begin data_out=16'b0000_0000_0010_0000; end    4'b0110:begin data_out=16'b0000_0000_0100_0000; end    4'b0111:begin data_out=16'b0000_0000_1000_0000; end    4'b1000:begin data_out=16'b0000_0001_0000_0000; end    4'b1001:begin data_out=16'b0000_0010_0000_0000; end    4'b1010:begin data_out=16'b0000_0100_0000_0000; end    4'b1011:begin data_out=16'b0000_1000_0000_0000; end    4'b1100:begin data_out=16'b0001_0000_0000_0000; end    4'b1101:begin data_out=16'b0010_0000_0000_0000; end    4'b1110:begin data_out=16'b0100_0000_0000_0000; end    4'b1111:begin data_out=16'b1000_0000_0000_0000; end    endcaseendendmodule
1.2.2RTL电路

从图中可以看出,直接生成了一个Decoder()门电路
这里写图片描述

1.2.3vwf波形

data_in和data_out没有同时改变;由于竞争冒险现象的存在,有毛刺产生
这里写图片描述

2 计数器

作业1中需要设计12位和20位计数器,使用verilog代码实现,它们的结构形式基本相同。
代码中的rst为复位信号,当它为高电平时,计数器正常工作,否则计数器置零。

2.1 12位计数器

2.1.1代码
module test2_1_v(input rst,input clk,//input [3:0] data_in,output reg[3:0] counter_output,output ov);parameter COUNT_NUM=12;//reg [3:0] counter_output_r;//assign counter_output=counter_output_r;assign ov=(counter_output==(COUNT_NUM-1))?1:0;always @(posedge clk or negedge rst)begin    if(!rst)    begin        //counter_output_r<=4'd0;        counter_output<=4'd0;    end    else    begin        //if(counter_output_r<COUNT_NUM-1)        if(counter_output<COUNT_NUM-1)        //begin counter_output_r<=counter_output_r+1'b1; end        begin counter_output<=counter_output+1'b1; end        else         //begin counter_output_r=4'b0; end        begin counter_output=4'b0; end    endendendmodule

我曾使用下面两句代码。但是output是可以同时定义为reg类型,所以我直接将counter_output作为了寄存器和输出。

//reg [3:0] counter_output_r;//assign counter_output=counter_output_r;
2.1.2 RTL电路

这里写图片描述

2.1.3 vwf波形

这里写图片描述

2.2 20位计数器

代码与12位计数器基本相同

module counter20(input rst,input clk,//input [3:0] data_in,output reg[4:0] counter_output,output ov);parameter COUNT_NUM=20;//reg [4:0] counter_output_r;//assign counter_output=counter_output_r;assign ov=(counter_output==(COUNT_NUM-1))?1:0;always @(posedge clk or negedge rst)begin    if(!rst)    begin        //counter_output_r<=4'd0;        counter_output<=4'd0;    end    else    begin        //if(counter_output_r<COUNT_NUM-1)        if(counter_output<COUNT_NUM-1)        //begin counter_output_r<=counter_output_r+1'b1; end        begin counter_output<=counter_output+1'b1; end        else         //begin counter_output_r<=4'b0; end        begin counter_output<=4'b0; end    endendendmodule
2.2.2RTL电路

和12位计数器基本相同
这里写图片描述

2.2.3vwf波形

这里写图片描述

二 往复计数器

  • 功能:设计一个计数器,从0递增1计数到9,然后递减1计数到0,如此往复。
  • 设计:使用状态机,分别为递增过程INC和递减过程DEC,用变量state记录。用两个always块,一个用于改变状态state,另一个用于实际的递增或递减

1 代码

module counterRe(input clk,rst,output reg[3:0]counter);reg state;parameter INC=1'b0,            DEC=1'b1;always @(posedge clk or negedge rst)begin    if(!rst)    begin        state<=INC;    end    else    begin        case(state)        INC:if(counter==4'd8)begin state<=DEC;end        DEC:if(counter==4'd1)begin state<=INC;end        default:begin state<=INC; end        endcase    endendalways @(posedge clk)begin    case(state)    INC:begin counter<=counter+1'b1; end    DEC:begin counter<=counter-1'b1; end    endcaseendendmodule

2 RTL电路

这里写图片描述

3 vwf波形

这里写图片描述

原创粉丝点击