OpenCores
URL https://opencores.org/ocsvn/simple_gpio/simple_gpio/trunk

Subversion Repositories simple_gpio

[/] [simple_gpio/] [trunk/] [rtl/] [simple_gpio.v] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rherveille
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  OpenCores Simple General Purpose IO core                   ////
4
////                                                             ////
5
////  Author: Richard Herveille                                  ////
6
////          richard@asics.ws                                   ////
7
////          www.asics.ws                                       ////
8
////                                                             ////
9
/////////////////////////////////////////////////////////////////////
10
////                                                             ////
11
//// Copyright (C) 2002 Richard Herveille                        ////
12
////                    richard@asics.ws                         ////
13
////                                                             ////
14
//// This source file may be used and distributed without        ////
15
//// restriction provided that this copyright statement is not   ////
16
//// removed from the file and that any derivative work contains ////
17
//// the original copyright notice and the associated disclaimer.////
18
////                                                             ////
19
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
20
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
21
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
22
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
23
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
24
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
25
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
26
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
27
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
28
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
29
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
30
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
31
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
32
////                                                             ////
33
/////////////////////////////////////////////////////////////////////
34
 
35
//  CVS Log
36
//
37 4 rherveille
//  $Id: simple_gpio.v,v 1.2 2002-12-22 16:10:17 rherveille Exp $
38 2 rherveille
//
39 4 rherveille
//  $Date: 2002-12-22 16:10:17 $
40
//  $Revision: 1.2 $
41 2 rherveille
//  $Author: rherveille $
42
//  $Locker:  $
43
//  $State: Exp $
44
//
45 4 rherveille
// Change History:
46
//               $Log: not supported by cvs2svn $
47
//
48 2 rherveille
 
49
 
50
 
51
//
52
// Very basic 8bit GPIO core
53
//
54
//
55
// Registers:
56
//
57
// 0x00: Control Register   <io[7:0]>
58
//       bits 7:0 R/W Input/Output    '1' = output mode
59
//                                    '0' = input mode
60 4 rherveille
// 0x01: Line Register
61 2 rherveille
//       bits 7:0 R   Status                Current GPIO pin level
62
//                W   Output                GPIO pin output level
63
//
64
//
65
// HOWTO:
66
//
67
// Use a pin as an input:
68
// Program the corresponding bit in the control register to 'input mode' ('0').
69
// The pin's state (input level) can be checked by reading the Line Register.
70
// Writing to the GPIO pin's Line Register bit while in input mode has no effect.
71
//
72
// Use a pin as an output:
73
// Program the corresponding bit in the control register to 'output mode' ('1').
74
// Program the GPIO pin's output level by writing to the corresponding bit in
75
// the Line Register.
76
// Reading the GPIO pin's Line Register bit while in output mode returns the
77
// current output level.
78
//
79
// Addapt the core for fewer GPIOs:
80
// If less than 8 GPIOs are required, than the 'io' parameter can be set to
81
// the amount of required interrupts. GPIOs are mapped starting at the LSBs.
82
// So only the 'io' LSBs per register are valid.
83
// All other bits (i.e. the 8-'io' MSBs) are set to zero '0'.
84
// Codesize is approximately linear to the amount of interrupts. I.e. using
85
// 4 instead of 8 GPIO sources reduces the size by approx. half.
86
//
87
 
88
 
89
// synopsys translate_off
90
`include "timescale.v"
91
// synopsys translate_on
92
 
93
module simple_gpio(
94
  clk_i, rst_i, cyc_i, stb_i, adr_i, we_i, dat_i, dat_o, ack_o,
95
  gpio
96
);
97
 
98
  //
99
  // Inputs & outputs
100
  //
101
  parameter io = 8;            // number of GPIOs
102
 
103
  // 8bit WISHBONE bus slave interface
104
  input         clk_i;         // clock
105
  input         rst_i;         // reset (asynchronous active low)
106
  input         cyc_i;         // cycle
107
  input         stb_i;         // strobe
108
  input         adr_i;         // address adr_i[1]
109
  input         we_i;          // write enable
110
  input  [ 7:0] dat_i;         // data output
111
  output [ 7:0] dat_o;         // data input
112
  output        ack_o;         // normal bus termination
113
 
114
  // GPIO pins
115
  inout  [io:1] gpio;
116
 
117
  //
118
  // Module body
119
  //
120
  reg [io:1] ctrl, line;                  // ControlRegister, LineRegister
121
  reg [io:1] lgpio, llgpio;               // LatchedGPIO pins
122
 
123
  //
124
  // perform parameter checks
125
  //
126
  // synopsys translate_off
127
  initial
128
  begin
129
      if(io > 8)
130
        $display("simple_gpio: max. 8 GPIOs supported.");
131
  end
132
  // synopsys translate_on
133
 
134
  //
135
  // WISHBONE interface
136
 
137
  wire wb_acc = cyc_i & stb_i;            // WISHBONE access
138
  wire wb_wr  = wb_acc & we_i;            // WISHBONE write access
139
 
140
  always @(posedge clk_i or negedge rst_i)
141
    if (~rst_i)
142
      begin
143
          ctrl <= #1 {{io}{1'b0}};
144
          line <= #1 {{io}{1'b0}};
145
      end
146
    else if (wb_wr)
147
       if ( adr_i )
148 4 rherveille
         line <= #1 dat_i[io-1:0];
149
       else
150 2 rherveille
         ctrl <= #1 dat_i[io-1:0];
151
 
152
 
153
  reg [7:0] dat_o;
154
  always @(posedge clk_i)
155
    if ( adr_i )
156 4 rherveille
      dat_o <= #1 { {{8-io}{1'b0}}, llgpio};
157
    else
158 2 rherveille
      dat_o <= #1 { {{8-io}{1'b0}}, ctrl};
159
 
160
  reg ack_o;
161
  always @(posedge clk_i or negedge rst_i)
162
    if (~rst_i)
163
      ack_o <= #1 1'b0;
164
    else
165
      ack_o <= #1 wb_acc & !ack_o;
166
 
167
 
168
  //
169
  // GPIO section
170
 
171
  // latch GPIO input pins
172
  always @(posedge clk_i)
173
    lgpio <= #1 gpio;
174
 
175
  // latch again (reduce meta-stability risc)
176
  always @(posedge clk_i)
177
    llgpio <= #1 lgpio;
178
 
179
  // assign GPIO outputs
180
  integer n;
181
  reg [io:1] igpio; // temporary internal signal
182
 
183
  always @(ctrl or line)
184
    for(n=1;n<=io;n=n+1)
185
      igpio[n] <= ctrl[n] ? line[n] : 1'bz;
186
 
187
  assign gpio = igpio;
188
 
189
endmodule
190
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.