| 1 | 
         2 | 
         olivier.gi | 
         //----------------------------------------------------------------------------
  | 
      
      
         | 2 | 
         117 | 
         olivier.gi | 
         // Copyright (C) 2009 , Olivier Girard
  | 
      
      
         | 3 | 
         2 | 
         olivier.gi | 
         //
  | 
      
      
         | 4 | 
         66 | 
         olivier.gi | 
         // Redistribution and use in source and binary forms, with or without
  | 
      
      
         | 5 | 
          | 
          | 
         // modification, are permitted provided that the following conditions
  | 
      
      
         | 6 | 
          | 
          | 
         // are met:
  | 
      
      
         | 7 | 
          | 
          | 
         //     * Redistributions of source code must retain the above copyright
  | 
      
      
         | 8 | 
          | 
          | 
         //       notice, this list of conditions and the following disclaimer.
  | 
      
      
         | 9 | 
          | 
          | 
         //     * Redistributions in binary form must reproduce the above copyright
  | 
      
      
         | 10 | 
          | 
          | 
         //       notice, this list of conditions and the following disclaimer in the
  | 
      
      
         | 11 | 
          | 
          | 
         //       documentation and/or other materials provided with the distribution.
  | 
      
      
         | 12 | 
          | 
          | 
         //     * Neither the name of the authors nor the names of its contributors
  | 
      
      
         | 13 | 
          | 
          | 
         //       may be used to endorse or promote products derived from this software
  | 
      
      
         | 14 | 
          | 
          | 
         //       without specific prior written permission.
  | 
      
      
         | 15 | 
         2 | 
         olivier.gi | 
         //
  | 
      
      
         | 16 | 
         66 | 
         olivier.gi | 
         // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  | 
      
      
         | 17 | 
          | 
          | 
         // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  | 
      
      
         | 18 | 
          | 
          | 
         // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  | 
      
      
         | 19 | 
          | 
          | 
         // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  | 
      
      
         | 20 | 
          | 
          | 
         // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  | 
      
      
         | 21 | 
          | 
          | 
         // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  | 
      
      
         | 22 | 
          | 
          | 
         // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  | 
      
      
         | 23 | 
          | 
          | 
         // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  | 
      
      
         | 24 | 
          | 
          | 
         // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  | 
      
      
         | 25 | 
          | 
          | 
         // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  | 
      
      
         | 26 | 
          | 
          | 
         // THE POSSIBILITY OF SUCH DAMAGE
  | 
      
      
         | 27 | 
         2 | 
         olivier.gi | 
         //
  | 
      
      
         | 28 | 
          | 
          | 
         //----------------------------------------------------------------------------
  | 
      
      
         | 29 | 
          | 
          | 
         //
  | 
      
      
         | 30 | 
          | 
          | 
         // *File Name: template_periph_16b.v
  | 
      
      
         | 31 | 
          | 
          | 
         // 
  | 
      
      
         | 32 | 
          | 
          | 
         // *Module Description:
  | 
      
      
         | 33 | 
          | 
          | 
         //                       16 bit peripheral template.
  | 
      
      
         | 34 | 
          | 
          | 
         //
  | 
      
      
         | 35 | 
          | 
          | 
         // *Author(s):
  | 
      
      
         | 36 | 
          | 
          | 
         //              - Olivier Girard,    olgirard@gmail.com
  | 
      
      
         | 37 | 
          | 
          | 
         //
  | 
      
      
         | 38 | 
          | 
          | 
         //----------------------------------------------------------------------------
  | 
      
      
         | 39 | 
         17 | 
         olivier.gi | 
         // $Rev: 134 $
  | 
      
      
         | 40 | 
          | 
          | 
         // $LastChangedBy: olivier.girard $
  | 
      
      
         | 41 | 
          | 
          | 
         // $LastChangedDate: 2012-03-22 21:31:06 +0100 (Thu, 22 Mar 2012) $
  | 
      
      
         | 42 | 
          | 
          | 
         //----------------------------------------------------------------------------
  | 
      
      
         | 43 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 44 | 
          | 
          | 
         module  template_periph_16b (
  | 
      
      
         | 45 | 
          | 
          | 
          
  | 
      
      
         | 46 | 
          | 
          | 
         // OUTPUTs
  | 
      
      
         | 47 | 
          | 
          | 
             per_dout,                       // Peripheral data output
  | 
      
      
         | 48 | 
          | 
          | 
          
  | 
      
      
         | 49 | 
          | 
          | 
         // INPUTs
  | 
      
      
         | 50 | 
          | 
          | 
             mclk,                           // Main system clock
  | 
      
      
         | 51 | 
          | 
          | 
             per_addr,                       // Peripheral address
  | 
      
      
         | 52 | 
          | 
          | 
             per_din,                        // Peripheral data input
  | 
      
      
         | 53 | 
          | 
          | 
             per_en,                         // Peripheral enable (high active)
  | 
      
      
         | 54 | 
         106 | 
         olivier.gi | 
             per_we,                         // Peripheral write enable (high active)
  | 
      
      
         | 55 | 
         111 | 
         olivier.gi | 
             puc_rst                         // Main system reset
  | 
      
      
         | 56 | 
         2 | 
         olivier.gi | 
         );
  | 
      
      
         | 57 | 
          | 
          | 
          
  | 
      
      
         | 58 | 
          | 
          | 
         // OUTPUTs
  | 
      
      
         | 59 | 
          | 
          | 
         //=========
  | 
      
      
         | 60 | 
          | 
          | 
         output       [15:0] per_dout;       // Peripheral data output
  | 
      
      
         | 61 | 
          | 
          | 
          
  | 
      
      
         | 62 | 
          | 
          | 
         // INPUTs
  | 
      
      
         | 63 | 
          | 
          | 
         //=========
  | 
      
      
         | 64 | 
          | 
          | 
         input               mclk;           // Main system clock
  | 
      
      
         | 65 | 
         111 | 
         olivier.gi | 
         input        [13:0] per_addr;       // Peripheral address
  | 
      
      
         | 66 | 
         2 | 
         olivier.gi | 
         input        [15:0] per_din;        // Peripheral data input
  | 
      
      
         | 67 | 
          | 
          | 
         input               per_en;         // Peripheral enable (high active)
  | 
      
      
         | 68 | 
         106 | 
         olivier.gi | 
         input         [1:0] per_we;         // Peripheral write enable (high active)
  | 
      
      
         | 69 | 
         111 | 
         olivier.gi | 
         input               puc_rst;        // Main system reset
  | 
      
      
         | 70 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 71 | 
          | 
          | 
          
  | 
      
      
         | 72 | 
          | 
          | 
         //=============================================================================
  | 
      
      
         | 73 | 
          | 
          | 
         // 1)  PARAMETER DECLARATION
  | 
      
      
         | 74 | 
          | 
          | 
         //=============================================================================
  | 
      
      
         | 75 | 
          | 
          | 
          
  | 
      
      
         | 76 | 
         111 | 
         olivier.gi | 
         // Register base address (must be aligned to decoder bit width)
  | 
      
      
         | 77 | 
          | 
          | 
         parameter       [14:0] BASE_ADDR   = 15'h0190;
  | 
      
      
         | 78 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 79 | 
         111 | 
         olivier.gi | 
         // Decoder bit width (defines how many bits are considered for address decoding)
  | 
      
      
         | 80 | 
          | 
          | 
         parameter              DEC_WD      =  3;
  | 
      
      
         | 81 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 82 | 
         111 | 
         olivier.gi | 
         // Register addresses offset
  | 
      
      
         | 83 | 
          | 
          | 
         parameter [DEC_WD-1:0] CNTRL1      = 'h0,
  | 
      
      
         | 84 | 
          | 
          | 
                                CNTRL2      = 'h2,
  | 
      
      
         | 85 | 
          | 
          | 
                                CNTRL3      = 'h4,
  | 
      
      
         | 86 | 
          | 
          | 
                                CNTRL4      = 'h6;
  | 
      
      
         | 87 | 
          | 
          | 
          
  | 
      
      
         | 88 | 
          | 
          | 
         // Register one-hot decoder utilities
  | 
      
      
         | 89 | 
         134 | 
         olivier.gi | 
         parameter              DEC_SZ      =  (1 << DEC_WD);
  | 
      
      
         | 90 | 
         111 | 
         olivier.gi | 
         parameter [DEC_SZ-1:0] BASE_REG    =  {{DEC_SZ-1{1'b0}}, 1'b1};
  | 
      
      
         | 91 | 
          | 
          | 
          
  | 
      
      
         | 92 | 
         2 | 
         olivier.gi | 
         // Register one-hot decoder
  | 
      
      
         | 93 | 
         111 | 
         olivier.gi | 
         parameter [DEC_SZ-1:0] CNTRL1_D    = (BASE_REG << CNTRL1),
  | 
      
      
         | 94 | 
          | 
          | 
                                CNTRL2_D    = (BASE_REG << CNTRL2),
  | 
      
      
         | 95 | 
          | 
          | 
                                CNTRL3_D    = (BASE_REG << CNTRL3),
  | 
      
      
         | 96 | 
          | 
          | 
                                CNTRL4_D    = (BASE_REG << CNTRL4);
  | 
      
      
         | 97 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 98 | 
          | 
          | 
          
  | 
      
      
         | 99 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 100 | 
          | 
          | 
         // 2)  REGISTER DECODER
  | 
      
      
         | 101 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 102 | 
          | 
          | 
          
  | 
      
      
         | 103 | 
         111 | 
         olivier.gi | 
         // Local register selection
  | 
      
      
         | 104 | 
          | 
          | 
         wire              reg_sel   =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
  | 
      
      
         | 105 | 
          | 
          | 
          
  | 
      
      
         | 106 | 
          | 
          | 
         // Register local address
  | 
      
      
         | 107 | 
          | 
          | 
         wire [DEC_WD-1:0] reg_addr  =  {per_addr[DEC_WD-2:0], 1'b0};
  | 
      
      
         | 108 | 
          | 
          | 
          
  | 
      
      
         | 109 | 
         2 | 
         olivier.gi | 
         // Register address decode
  | 
      
      
         | 110 | 
         111 | 
         olivier.gi | 
         wire [DEC_SZ-1:0] reg_dec   =  (CNTRL1_D  &  {DEC_SZ{(reg_addr == CNTRL1 )}})  |
  | 
      
      
         | 111 | 
          | 
          | 
                                        (CNTRL2_D  &  {DEC_SZ{(reg_addr == CNTRL2 )}})  |
  | 
      
      
         | 112 | 
          | 
          | 
                                        (CNTRL3_D  &  {DEC_SZ{(reg_addr == CNTRL3 )}})  |
  | 
      
      
         | 113 | 
          | 
          | 
                                        (CNTRL4_D  &  {DEC_SZ{(reg_addr == CNTRL4 )}});
  | 
      
      
         | 114 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 115 | 
          | 
          | 
         // Read/Write probes
  | 
      
      
         | 116 | 
         111 | 
         olivier.gi | 
         wire              reg_write =  |per_we & reg_sel;
  | 
      
      
         | 117 | 
          | 
          | 
         wire              reg_read  = ~|per_we & reg_sel;
  | 
      
      
         | 118 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 119 | 
          | 
          | 
         // Read/Write vectors
  | 
      
      
         | 120 | 
         111 | 
         olivier.gi | 
         wire [DEC_SZ-1:0] reg_wr    = reg_dec & {DEC_SZ{reg_write}};
  | 
      
      
         | 121 | 
          | 
          | 
         wire [DEC_SZ-1:0] reg_rd    = reg_dec & {DEC_SZ{reg_read}};
  | 
      
      
         | 122 | 
         2 | 
         olivier.gi | 
          
  | 
      
      
         | 123 | 
          | 
          | 
          
  | 
      
      
         | 124 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 125 | 
          | 
          | 
         // 3) REGISTERS
  | 
      
      
         | 126 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 127 | 
          | 
          | 
          
  | 
      
      
         | 128 | 
          | 
          | 
         // CNTRL1 Register
  | 
      
      
         | 129 | 
          | 
          | 
         //-----------------   
  | 
      
      
         | 130 | 
          | 
          | 
         reg  [15:0] cntrl1;
  | 
      
      
         | 131 | 
          | 
          | 
          
  | 
      
      
         | 132 | 
          | 
          | 
         wire        cntrl1_wr = reg_wr[CNTRL1];
  | 
      
      
         | 133 | 
          | 
          | 
          
  | 
      
      
         | 134 | 
         111 | 
         olivier.gi | 
         always @ (posedge mclk or posedge puc_rst)
  | 
      
      
         | 135 | 
          | 
          | 
           if (puc_rst)        cntrl1 <=  16'h0000;
  | 
      
      
         | 136 | 
         2 | 
         olivier.gi | 
           else if (cntrl1_wr) cntrl1 <=  per_din;
  | 
      
      
         | 137 | 
          | 
          | 
          
  | 
      
      
         | 138 | 
          | 
          | 
          
  | 
      
      
         | 139 | 
          | 
          | 
         // CNTRL2 Register
  | 
      
      
         | 140 | 
          | 
          | 
         //-----------------   
  | 
      
      
         | 141 | 
          | 
          | 
         reg  [15:0] cntrl2;
  | 
      
      
         | 142 | 
          | 
          | 
          
  | 
      
      
         | 143 | 
          | 
          | 
         wire        cntrl2_wr = reg_wr[CNTRL2];
  | 
      
      
         | 144 | 
          | 
          | 
          
  | 
      
      
         | 145 | 
         111 | 
         olivier.gi | 
         always @ (posedge mclk or posedge puc_rst)
  | 
      
      
         | 146 | 
          | 
          | 
           if (puc_rst)        cntrl2 <=  16'h0000;
  | 
      
      
         | 147 | 
         2 | 
         olivier.gi | 
           else if (cntrl2_wr) cntrl2 <=  per_din;
  | 
      
      
         | 148 | 
          | 
          | 
          
  | 
      
      
         | 149 | 
          | 
          | 
          
  | 
      
      
         | 150 | 
          | 
          | 
         // CNTRL3 Register
  | 
      
      
         | 151 | 
          | 
          | 
         //-----------------   
  | 
      
      
         | 152 | 
          | 
          | 
         reg  [15:0] cntrl3;
  | 
      
      
         | 153 | 
          | 
          | 
          
  | 
      
      
         | 154 | 
          | 
          | 
         wire        cntrl3_wr = reg_wr[CNTRL3];
  | 
      
      
         | 155 | 
          | 
          | 
          
  | 
      
      
         | 156 | 
         111 | 
         olivier.gi | 
         always @ (posedge mclk or posedge puc_rst)
  | 
      
      
         | 157 | 
          | 
          | 
           if (puc_rst)        cntrl3 <=  16'h0000;
  | 
      
      
         | 158 | 
         2 | 
         olivier.gi | 
           else if (cntrl3_wr) cntrl3 <=  per_din;
  | 
      
      
         | 159 | 
          | 
          | 
          
  | 
      
      
         | 160 | 
          | 
          | 
          
  | 
      
      
         | 161 | 
          | 
          | 
         // CNTRL4 Register
  | 
      
      
         | 162 | 
          | 
          | 
         //-----------------   
  | 
      
      
         | 163 | 
          | 
          | 
         reg  [15:0] cntrl4;
  | 
      
      
         | 164 | 
          | 
          | 
          
  | 
      
      
         | 165 | 
          | 
          | 
         wire        cntrl4_wr = reg_wr[CNTRL4];
  | 
      
      
         | 166 | 
          | 
          | 
          
  | 
      
      
         | 167 | 
         111 | 
         olivier.gi | 
         always @ (posedge mclk or posedge puc_rst)
  | 
      
      
         | 168 | 
          | 
          | 
           if (puc_rst)        cntrl4 <=  16'h0000;
  | 
      
      
         | 169 | 
         2 | 
         olivier.gi | 
           else if (cntrl4_wr) cntrl4 <=  per_din;
  | 
      
      
         | 170 | 
          | 
          | 
          
  | 
      
      
         | 171 | 
          | 
          | 
          
  | 
      
      
         | 172 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 173 | 
          | 
          | 
         // 4) DATA OUTPUT GENERATION
  | 
      
      
         | 174 | 
          | 
          | 
         //============================================================================
  | 
      
      
         | 175 | 
          | 
          | 
          
  | 
      
      
         | 176 | 
          | 
          | 
         // Data output mux
  | 
      
      
         | 177 | 
          | 
          | 
         wire [15:0] cntrl1_rd  = cntrl1  & {16{reg_rd[CNTRL1]}};
  | 
      
      
         | 178 | 
          | 
          | 
         wire [15:0] cntrl2_rd  = cntrl2  & {16{reg_rd[CNTRL2]}};
  | 
      
      
         | 179 | 
          | 
          | 
         wire [15:0] cntrl3_rd  = cntrl3  & {16{reg_rd[CNTRL3]}};
  | 
      
      
         | 180 | 
          | 
          | 
         wire [15:0] cntrl4_rd  = cntrl4  & {16{reg_rd[CNTRL4]}};
  | 
      
      
         | 181 | 
          | 
          | 
          
  | 
      
      
         | 182 | 
          | 
          | 
         wire [15:0] per_dout   =  cntrl1_rd  |
  | 
      
      
         | 183 | 
          | 
          | 
                                   cntrl2_rd  |
  | 
      
      
         | 184 | 
          | 
          | 
                                   cntrl3_rd  |
  | 
      
      
         | 185 | 
          | 
          | 
                                   cntrl4_rd;
  | 
      
      
         | 186 | 
          | 
          | 
          
  | 
      
      
         | 187 | 
          | 
          | 
          
  | 
      
      
         | 188 | 
          | 
          | 
         endmodule // template_periph_16b
  |