Holey_yan

20141212_Verilog_ShiftRegister

Dec 11th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************/
  2. /** 20141211 Shift Register           **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/12/12 am.00:00      **/
  6. /***************************************/
  7.  
  8. /* Set port
  9.    Clk      = PIN_Y2 (Is 50MHz)
  10.    Clr      = PIN_M23
  11.    Trigger  = PIN_M21
  12.    out[8:0] = PIN_F17
  13.           PIN_G21
  14.           PIN_G22
  15.           PIN_G20
  16.           PIN_H21
  17.           PIN_E24
  18.           PIN_E25
  19.           PIN_E22
  20.           PIN_E21       */
  21.  
  22. module ShiftRegister(Clk, Trigger, Clr, Out);
  23.     input       Clk, Trigger, Clr;
  24.     output reg  [8:0] Out;
  25.  
  26.     //A customize divider
  27.     wire    o_Clk;
  28.     divn #(.WIDTH(30),
  29.            .N(5000000)) tmp(.Clk(Clk), .rst_n(Clr), .o_Clk(o_Clk));
  30.  
  31.     /*Set a variable to remember Style(Right-Move or Left-Move)
  32.       If(tmp_style == 0)
  33.             It's will be a Right-Shift Regedit.
  34.       Else If(tmp_style == 1)
  35.             It's will be a Right-Shift Regedit. */
  36.     reg tmp_style = 0;
  37.     reg [8:0]   tmp_out;
  38.  
  39.     //Trigger Style
  40.     always@(negedge Trigger)
  41.     begin
  42.         if(Trigger == 0)
  43.             tmp_style = ~tmp_style;
  44.     end
  45.  
  46.     //Move
  47.     always@(posedge o_Clk or negedge Clr)
  48.     begin :f //ifhave a variable declaration in always, you should key in this
  49.         integer i; //For [For] to use
  50.         if(Clr == 0)
  51.             tmp_out = 9'b111111110;
  52.         else
  53.             if(o_Clk == 1)
  54.                 if(tmp_style == 0)
  55.                     if(tmp_out == 9'b011111111)
  56.                         tmp_out = 9'b111111110;
  57.                     else
  58.                     begin
  59.                         for(i = 8; i > 0; i = i - 1)
  60.                             tmp_out[i] = tmp_out[i - 1];
  61.                         tmp_out[0] = 1;
  62.                     end
  63.                 else if(tmp_style == 1)
  64.                     if(tmp_out == 9'b111111110)
  65.                         tmp_out = 9'b011111111;
  66.                     else
  67.                     begin
  68.                         for(i = 0; i < 8; i = i + 1)
  69.                             tmp_out[i] = tmp_out[i + 1];
  70.                         tmp_out[8] = 1;
  71.                     end
  72.         Out = tmp_out; //To transmit the value to output port(Out)
  73.     end
  74. endmodule
  75.  
  76. //A Mod N Divider
  77. module divn(Clk, rst_n, o_Clk);
  78.     input   Clk, rst_n;
  79.     output  o_Clk;
  80.     parameter WIDTH = 3; //Register long
  81.     parameter N     = 6; //A number which you want to mod
  82.  
  83.     reg [WIDTH - 1:0]   cnt_p;
  84.     reg [WIDTH - 1:0]   cnt_n;
  85.     reg         Clk_p;
  86.     reg         Clk_n;
  87.  
  88.     assign o_Clk = (N == 1) ? Clk :
  89.                (N[0])   ? (Clk_p | Clk_n) : (Clk_p); //N[0] is use for judge N is or not a odd number
  90.  
  91.     always@(posedge Clk or negedge rst_n)
  92.     begin
  93.       if(!rst_n)
  94.          cnt_p <= 0;
  95.       else if(cnt_p == (N - 1))
  96.          cnt_p <= 0;
  97.       else
  98.          cnt_p <= cnt_p + 1;
  99.     end
  100.  
  101.     always@(posedge Clk or negedge rst_n)
  102.     begin
  103.       if(!rst_n)
  104.          Clk_p <= 1;
  105.       else if(cnt_p < (N >> 1)) //(N >> 1) is the same as (N / 2)
  106.          Clk_p = 1;
  107.       else
  108.          Clk_p = 0;
  109.     end
  110.  
  111.     always@(negedge Clk or negedge rst_n)
  112.     begin
  113.       if(!rst_n)
  114.          cnt_n <= 0;
  115.       else if(cnt_n == (N - 1))
  116.          cnt_n <= 0;
  117.       else
  118.          cnt_n <= cnt_n + 1;
  119.     end
  120.  
  121.     always@(negedge Clk or negedge rst_n)
  122.     begin
  123.       if(!rst_n)
  124.          Clk_n <= 1;
  125.       else if(cnt_n < (N >> 1))
  126.          Clk_n = 1;
  127.       else
  128.          Clk_n = 0;
  129.     end
  130. endmodule
Advertisement
Add Comment
Please, Sign In to add comment