FPGA-Verilog

Verilog基础练习

  • [1 累加累乘器](#1 累加累乘器)
  • [2 异或、与运算求解](#2 异或、与运算求解)
    • [2.1 匹配4种case下的求解](#2.1 匹配4种case下的求解)
  • [3 补码计算器](#3 补码计算器)
  • [4 计数器](#4 计数器)

参考视频链接

1 累加累乘器

c 复制代码
`timescale 1ns/10ps

module inout_test();
parameter ratio = 2;
reg [15:0]num  ;
reg [31:0]save_add_v;
reg [31:0]save_multi_v;

initial
    begin
        save_add_v = 100;
        save_multi_v = 100;
        forever begin
            save_add_v = save_add_v + ratio;
            save_multi_v = save_multi_v * ratio;
            #1;
            $display("This is a test program");
            $display("save_add_v:%d,save_multi_v:%d" , save_add_v , save_multi_v);
            if($time >4)
                $finish;
          end
    end
endmodule;

输出:

2 异或、与运算求解

c 复制代码
`timescale 1ns/10ps

module fn_sw( a , b , sel , y );
input a;
input b;
input sel;
output y;

//assign y = sel? (a^b):(a&b);
reg y;
always@(a ,b , sel)
begin
    if(sel == 1)
        y = a^b;
    else
        y = a & b;

end

endmodule;


module fn_sw_tb;
reg a,b,sel;
wire y;
fn_sw fn_sw( 
        .a(a),
        .b( b),
        .sel(sel ),
        .y(y)
       ); 
initial begin
        a<=0;b<=0;sel<=0;
        #10 a<=0;b<=0;sel<=1;
        #10 a<=0;b<=1;sel<=0;
        #10 a<=1;b<=0;sel<=0;
        #10 a<=0;b<=1;sel<=0;
        #10 a<=0;b<=1;sel<=1;
        #10 a<=1;b<=0;sel<=0;
        #10 $stop;
 end;
endmodule;

2.1 匹配4种case下的求解

c 复制代码
`timescale 1ns/10ps

module fn_sw( a , b , sel , y );
input a;
input b;
input [1:0]sel;
output y;

//assign y = sel? (a^b):(a&b);
reg y;
always@(a ,b , sel)
begin
    case(sel)
        2'b00:
            y<= a&b;
        2'b01:
            y<= a|b;
        2'b10:
            y<= a^b;
        2'b11:
            y<=~(a^b);
     endcase
end
endmodule;


module fn_sw_tb;
reg a,b;
reg [1:0]sel;
wire y;
fn_sw fn_sw( 
        .a(a),
        .b( b),
        .sel(sel ),
        .y(y)
       ); 
initial begin
        a<=0;b<=0;sel<=0;
        #10 a<=0;b<=0;sel<=1;
        #10 a<=0;b<=1;sel<=2;
        #10 a<=1;b<=0;sel<=3;
        #10 a<=0;b<=1;sel<=0;
        #10 a<=0;b<=1;sel<=1;
        #10 a<=1;b<=0;sel<=2;
        #10 a<=1;b<=0;sel<=3;
        #10 $stop;
end;
endmodule;

3 补码计算器

正数的补码是本身;

负数的补码是符号位不变,其他位,安位取反后加1;

以8 bit数为例,可操作的位数是[6:0]

bash 复制代码
`timescale 1ns/1ns
module comp_conv(
                a,
                a_comp
);
input   [7:0]   a;
output  [7:0]   a_comp;

wire    [6:0]   b;  //按位取反的幅度位
wire    [7:0]   y;  //负数的补码

assign     b = ~a[6:0];
assign      y[6:0] = b+1;   //按位取反+1;
assign      y[7] = a[7];    //符号位不变
assign      a_comp = a[7]?y:a;  //2选1

endmodule;

module comp_conv_tb;
reg[7:0]        a_in;
wire[7:0]       y_out;

comp_conv   comp_conv(
                    .a(a_in),
                    .a_comp(y_out)
);

initial begin
    a_in <= 0;
    #3000 $stop;
end

always #10 a_in <= a_in + 1;

endmodule

负数的补码:

正数的不变:

其中计算补码的部分,也可用一句assign 实现:

c 复制代码
assign      a_comp = a[7] ? {a[7], ~a[6:0]+1}:a;

4 计数器

每产生一个上升沿,计数值加1

c 复制代码
`timescale 1ns/10ps
module counter(
                clk,
                reset,
                y,
                sum
              
);
input   clk , reset;
output  [15:0]y;
reg  [15:0]  y;
output wire [15:0] sum;// +1 运算结果
reg     [15:0]p_sum;
assign sum = y + 1;

always @(posedge clk or negedge reset )
begin
if (~reset) 
    y <= 0;
else
    y <= sum;
end
endmodule


module counter_tb;
reg     clk,reset;
wire    [15:0]   y;
wire    [15:0]  sum;
counter counter(
    .clk(clk),
    .reset(reset),
    .y(y),
    .sum(sum)
);

initial
    begin
        clk <= 0; reset <= 0;
        #17     reset <= 1;
        #2880;
        $display("counter num is:%d", sum);
        $stop;
    end

always #5 clk <= ~clk;

endmodule

结果:

设定simulation run time 的地方:

相关推荐
XINVRY-FPGA36 分钟前
EPM240T100I5N Altera FPGA MAX II CPLD
人工智能·嵌入式硬件·fpga开发·硬件工程·dsp开发·射频工程·fpga
第二层皮-合肥6 小时前
FPGA实现ETH接口
单片机·嵌入式硬件·fpga开发
璞致电子9 小时前
【PZ-ZU47DR-KFB】璞致FPGA ZYNQ UltraScalePlus RFSOC QSPI Flash 固化常见问题说明
嵌入式硬件·fpga开发·fpga·软件无线电·sdr
陌夏微秋10 小时前
FPGA硬件设计2 最小芯片系统-ZYNQ7020/7010
嵌入式硬件·fpga开发·硬件架构·硬件工程·信息与通信·智能硬件
风已经起了1 天前
FPGA学习笔记——IIC协议简介
笔记·学习·fpga开发
逐梦之程1 天前
FPGA-Vivado2017.4-建立AXI4用于单片机与FPGA之间数据互通
fpga开发
XINVRY-FPGA2 天前
10CL016YF484C8G Altera FPGA Cyclone
嵌入式硬件·网络协议·fpga开发·云计算·硬件工程·信息与通信·fpga
嵌入式-老费2 天前
产品开发实践(常见的软硬结合方式)
fpga开发
FakeOccupational3 天前
【电路笔记 通信】AXI4-Lite协议 FPGA实现 & Valid-Ready Handshake 握手协议
笔记·fpga开发
I'm a winner3 天前
FPGA+护理:跨学科发展的探索(五)
fpga开发