玩转FPGA山寨版

看了《玩转FPGA》,写的不错,写写山寨版和大家交流!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、gray counter

************************************************

module gray(
output wire [7:0] out    , // counter out
input  wire       enable , // enable for counter
input  wire       clk    , // clock
input  wire       rst      // active hight reset
);
//------------Internal Variables--------
reg [7:0] count;
//-------------Code Starts Here---------
always_ff @ (posedge clk)
if (rst) begin
  count <= 0;
end else if (enable) begin
  count ++;
end
 
assign out = { count[7], (count[7] ^ count[6]),(count[6] ^
             count[5]),(count[5] ^ count[4]), (count[4] ^
             count[3]),(count[3] ^ count[2]), (count[2] ^
             count[1]),(count[1] ^ count[0]) };
 
endmodule

**************************************************************

RTL Viewer

 

2gray counter 2

********************************************************************

`timescale 1ns/1ps
module GrayCounter
     #(parameter   COUNTER_WIDTH = 4)  (
     output reg  [COUNTER_WIDTH-1:0]    GrayCount_out,  //'Gray' code count output.
     input wire                         Enable_in,  //Count enable.
     input wire                         Clear_in,   //Count reset.
     input wire                         Clk);

    /////////Internal connections & variables///////
    reg    [COUNTER_WIDTH-1:0]         BinaryCount;

    /////////Code///////////////////////
  always@(posedge Clk)
        if (Clear_in) begin
            BinaryCount   <= '0 + 1'b1;  //Gray count begins @ '1' with
            GrayCount_out <= '0;      // first 'Enable_in'.
        end
        else if (Enable_in) begin
            BinaryCount ++;
            GrayCount_out <= {BinaryCount[COUNTER_WIDTH-1],
                              BinaryCount[COUNTER_WIDTH-2:0] ^ BinaryCount[COUNTER_WIDTH-1:1]};
        end
   
endmodule

********************************************************************

RTL Viewer

 

posted on 2010-09-04 16:11  Neddy11  阅读(503)  评论(0)    收藏  举报