| 1 | 80 | olivier.gi | //----------------------------------------------------------------------------
 | 
      
         | 2 | 136 | olivier.gi | // Copyright (C) 2009 , Olivier Girard
 | 
      
         | 3 | 80 | olivier.gi | //
 | 
      
         | 4 |  |  | // 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 |  |  | //
 | 
      
         | 16 |  |  | // 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 |  |  | //
 | 
      
         | 28 |  |  | //----------------------------------------------------------------------------
 | 
      
         | 29 |  |  | //
 | 
      
         | 30 |  |  | // *File Name: template_periph_8b.v
 | 
      
         | 31 |  |  | // 
 | 
      
         | 32 |  |  | // *Module Description:
 | 
      
         | 33 |  |  | //                       8 bit peripheral template.
 | 
      
         | 34 |  |  | //
 | 
      
         | 35 |  |  | // *Author(s):
 | 
      
         | 36 |  |  | //              - Olivier Girard,    olgirard@gmail.com
 | 
      
         | 37 |  |  | //
 | 
      
         | 38 |  |  | //----------------------------------------------------------------------------
 | 
      
         | 39 | 111 | olivier.gi | // $Rev: 103 $
 | 
      
         | 40 | 80 | olivier.gi | // $LastChangedBy: olivier.girard $
 | 
      
         | 41 | 111 | olivier.gi | // $LastChangedDate: 2011-03-05 15:44:48 +0100 (Sat, 05 Mar 2011) $
 | 
      
         | 42 | 80 | olivier.gi | //----------------------------------------------------------------------------
 | 
      
         | 43 |  |  |  
 | 
      
         | 44 |  |  | module  template_periph_8b (
 | 
      
         | 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 | 107 | olivier.gi |     per_we,                         // Peripheral write enable (high active)
 | 
      
         | 55 | 111 | olivier.gi |     puc_rst                         // Main system reset
 | 
      
         | 56 | 80 | 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 | 80 | olivier.gi | input       [15:0] per_din;         // Peripheral data input
 | 
      
         | 67 |  |  | input              per_en;          // Peripheral enable (high active)
 | 
      
         | 68 | 107 | olivier.gi | input        [1:0] per_we;          // Peripheral write enable (high active)
 | 
      
         | 69 | 111 | olivier.gi | input              puc_rst;         // Main system reset
 | 
      
         | 70 | 80 | 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'h0090;
 | 
      
         | 78 | 80 | olivier.gi |  
 | 
      
         | 79 | 111 | olivier.gi | // Decoder bit width (defines how many bits are considered for address decoding)
 | 
      
         | 80 |  |  | parameter              DEC_WD      =  2;
 | 
      
         | 81 |  |  |  
 | 
      
         | 82 |  |  | // Register addresses offset
 | 
      
         | 83 |  |  | parameter [DEC_WD-1:0] CNTRL1      =  'h0,
 | 
      
         | 84 |  |  |                        CNTRL2      =  'h1,
 | 
      
         | 85 |  |  |                        CNTRL3      =  'h2,
 | 
      
         | 86 |  |  |                        CNTRL4      =  'h3;
 | 
      
         | 87 |  |  |  
 | 
      
         | 88 | 80 | olivier.gi |  
 | 
      
         | 89 | 111 | olivier.gi | // Register one-hot decoder utilities
 | 
      
         | 90 | 136 | olivier.gi | parameter              DEC_SZ      =  (1 << DEC_WD);
 | 
      
         | 91 | 111 | olivier.gi | parameter [DEC_SZ-1:0] BASE_REG    =  {{DEC_SZ-1{1'b0}}, 1'b1};
 | 
      
         | 92 |  |  |  
 | 
      
         | 93 | 80 | olivier.gi | // Register one-hot decoder
 | 
      
         | 94 | 111 | olivier.gi | parameter [DEC_SZ-1:0] CNTRL1_D  = (BASE_REG << CNTRL1),
 | 
      
         | 95 |  |  |                        CNTRL2_D  = (BASE_REG << CNTRL2),
 | 
      
         | 96 |  |  |                        CNTRL3_D  = (BASE_REG << CNTRL3),
 | 
      
         | 97 |  |  |                        CNTRL4_D  = (BASE_REG << CNTRL4);
 | 
      
         | 98 | 80 | olivier.gi |  
 | 
      
         | 99 |  |  |  
 | 
      
         | 100 |  |  | //============================================================================
 | 
      
         | 101 |  |  | // 2)  REGISTER DECODER
 | 
      
         | 102 |  |  | //============================================================================
 | 
      
         | 103 |  |  |  
 | 
      
         | 104 | 111 | olivier.gi | // Local register selection
 | 
      
         | 105 |  |  | wire              reg_sel      =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
 | 
      
         | 106 |  |  |  
 | 
      
         | 107 |  |  | // Register local address
 | 
      
         | 108 |  |  | wire [DEC_WD-1:0] reg_addr     =  {1'b0, per_addr[DEC_WD-2:0]};
 | 
      
         | 109 |  |  |  
 | 
      
         | 110 | 80 | olivier.gi | // Register address decode
 | 
      
         | 111 | 111 | olivier.gi | wire [DEC_SZ-1:0] reg_dec      = (CNTRL1_D  &  {DEC_SZ{(reg_addr==(CNTRL1 >>1))}}) |
 | 
      
         | 112 |  |  |                                  (CNTRL2_D  &  {DEC_SZ{(reg_addr==(CNTRL2 >>1))}}) |
 | 
      
         | 113 |  |  |                                  (CNTRL3_D  &  {DEC_SZ{(reg_addr==(CNTRL3 >>1))}}) |
 | 
      
         | 114 |  |  |                                  (CNTRL4_D  &  {DEC_SZ{(reg_addr==(CNTRL4 >>1))}});
 | 
      
         | 115 | 80 | olivier.gi |  
 | 
      
         | 116 |  |  | // Read/Write probes
 | 
      
         | 117 | 111 | olivier.gi | wire              reg_lo_write =  per_we[0] & reg_sel;
 | 
      
         | 118 |  |  | wire              reg_hi_write =  per_we[1] & reg_sel;
 | 
      
         | 119 |  |  | wire              reg_read     = ~|per_we   & reg_sel;
 | 
      
         | 120 | 80 | olivier.gi |  
 | 
      
         | 121 |  |  | // Read/Write vectors
 | 
      
         | 122 | 111 | olivier.gi | wire [DEC_SZ-1:0] reg_hi_wr    = reg_dec & {DEC_SZ{reg_hi_write}};
 | 
      
         | 123 |  |  | wire [DEC_SZ-1:0] reg_lo_wr    = reg_dec & {DEC_SZ{reg_lo_write}};
 | 
      
         | 124 |  |  | wire [DEC_SZ-1:0] reg_rd       = reg_dec & {DEC_SZ{reg_read}};
 | 
      
         | 125 | 80 | olivier.gi |  
 | 
      
         | 126 |  |  |  
 | 
      
         | 127 |  |  | //============================================================================
 | 
      
         | 128 |  |  | // 3) REGISTERS
 | 
      
         | 129 |  |  | //============================================================================
 | 
      
         | 130 |  |  |  
 | 
      
         | 131 |  |  | // CNTRL1 Register
 | 
      
         | 132 |  |  | //-----------------
 | 
      
         | 133 |  |  | reg  [7:0] cntrl1;
 | 
      
         | 134 |  |  |  
 | 
      
         | 135 | 111 | olivier.gi | wire       cntrl1_wr  = CNTRL1[0] ? reg_hi_wr[CNTRL1] : reg_lo_wr[CNTRL1];
 | 
      
         | 136 |  |  | wire [7:0] cntrl1_nxt = CNTRL1[0] ? per_din[15:8]     : per_din[7:0];
 | 
      
         | 137 | 80 | olivier.gi |  
 | 
      
         | 138 | 111 | olivier.gi | always @ (posedge mclk or posedge puc_rst)
 | 
      
         | 139 |  |  |   if (puc_rst)        cntrl1 <=  8'h00;
 | 
      
         | 140 | 80 | olivier.gi |   else if (cntrl1_wr) cntrl1 <=  cntrl1_nxt;
 | 
      
         | 141 |  |  |  
 | 
      
         | 142 |  |  |  
 | 
      
         | 143 |  |  | // CNTRL2 Register
 | 
      
         | 144 |  |  | //-----------------
 | 
      
         | 145 |  |  | reg  [7:0] cntrl2;
 | 
      
         | 146 |  |  |  
 | 
      
         | 147 | 111 | olivier.gi | wire       cntrl2_wr  = CNTRL2[0] ? reg_hi_wr[CNTRL2] : reg_lo_wr[CNTRL2];
 | 
      
         | 148 |  |  | wire [7:0] cntrl2_nxt = CNTRL2[0] ? per_din[15:8]     : per_din[7:0];
 | 
      
         | 149 | 80 | olivier.gi |  
 | 
      
         | 150 | 111 | olivier.gi | always @ (posedge mclk or posedge puc_rst)
 | 
      
         | 151 |  |  |   if (puc_rst)        cntrl2 <=  8'h00;
 | 
      
         | 152 | 80 | olivier.gi |   else if (cntrl2_wr) cntrl2 <=  cntrl2_nxt;
 | 
      
         | 153 |  |  |  
 | 
      
         | 154 |  |  |  
 | 
      
         | 155 |  |  | // CNTRL3 Register
 | 
      
         | 156 |  |  | //-----------------
 | 
      
         | 157 |  |  | reg  [7:0] cntrl3;
 | 
      
         | 158 |  |  |  
 | 
      
         | 159 | 111 | olivier.gi | wire       cntrl3_wr  = CNTRL3[0] ? reg_hi_wr[CNTRL3] : reg_lo_wr[CNTRL3];
 | 
      
         | 160 |  |  | wire [7:0] cntrl3_nxt = CNTRL3[0] ? per_din[15:8]     : per_din[7:0];
 | 
      
         | 161 | 80 | olivier.gi |  
 | 
      
         | 162 | 111 | olivier.gi | always @ (posedge mclk or posedge puc_rst)
 | 
      
         | 163 |  |  |   if (puc_rst)        cntrl3 <=  8'h00;
 | 
      
         | 164 | 80 | olivier.gi |   else if (cntrl3_wr) cntrl3 <=  cntrl3_nxt;
 | 
      
         | 165 |  |  |  
 | 
      
         | 166 |  |  |  
 | 
      
         | 167 |  |  | // CNTRL4 Register
 | 
      
         | 168 |  |  | //-----------------
 | 
      
         | 169 |  |  | reg  [7:0] cntrl4;
 | 
      
         | 170 |  |  |  
 | 
      
         | 171 | 111 | olivier.gi | wire       cntrl4_wr  = CNTRL4[0] ? reg_hi_wr[CNTRL4] : reg_lo_wr[CNTRL4];
 | 
      
         | 172 |  |  | wire [7:0] cntrl4_nxt = CNTRL4[0] ? per_din[15:8]     : per_din[7:0];
 | 
      
         | 173 | 80 | olivier.gi |  
 | 
      
         | 174 | 111 | olivier.gi | always @ (posedge mclk or posedge puc_rst)
 | 
      
         | 175 |  |  |   if (puc_rst)        cntrl4 <=  8'h00;
 | 
      
         | 176 | 80 | olivier.gi |   else if (cntrl4_wr) cntrl4 <=  cntrl4_nxt;
 | 
      
         | 177 |  |  |  
 | 
      
         | 178 |  |  |  
 | 
      
         | 179 |  |  |  
 | 
      
         | 180 |  |  | //============================================================================
 | 
      
         | 181 |  |  | // 4) DATA OUTPUT GENERATION
 | 
      
         | 182 |  |  | //============================================================================
 | 
      
         | 183 |  |  |  
 | 
      
         | 184 |  |  | // Data output mux
 | 
      
         | 185 | 111 | olivier.gi | wire [15:0] cntrl1_rd   = {8'h00, (cntrl1  & {8{reg_rd[CNTRL1]}})}  << (8 & {4{CNTRL1[0]}});
 | 
      
         | 186 |  |  | wire [15:0] cntrl2_rd   = {8'h00, (cntrl2  & {8{reg_rd[CNTRL2]}})}  << (8 & {4{CNTRL2[0]}});
 | 
      
         | 187 |  |  | wire [15:0] cntrl3_rd   = {8'h00, (cntrl3  & {8{reg_rd[CNTRL3]}})}  << (8 & {4{CNTRL3[0]}});
 | 
      
         | 188 |  |  | wire [15:0] cntrl4_rd   = {8'h00, (cntrl4  & {8{reg_rd[CNTRL4]}})}  << (8 & {4{CNTRL4[0]}});
 | 
      
         | 189 | 80 | olivier.gi |  
 | 
      
         | 190 |  |  | wire [15:0] per_dout  =  cntrl1_rd  |
 | 
      
         | 191 |  |  |                          cntrl2_rd  |
 | 
      
         | 192 |  |  |                          cntrl3_rd  |
 | 
      
         | 193 |  |  |                          cntrl4_rd;
 | 
      
         | 194 |  |  |  
 | 
      
         | 195 |  |  |  
 | 
      
         | 196 |  |  | endmodule // template_periph_8b
 |