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 2

Go to most recent revision | 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
//  $Id: simple_gpio.v,v 1.1.1.1 2002-12-02 17:19:34 rherveille Exp $
38
//
39
//  $Date: 2002-12-02 17:19:34 $
40
//  $Revision: 1.1.1.1 $
41
//  $Author: rherveille $
42
//  $Locker:  $
43
//  $State: Exp $
44
//
45
 
46
 
47
 
48
//
49
// Very basic 8bit GPIO core
50
//
51
//
52
// Registers:
53
//
54
// 0x00: Control Register   <io[7:0]>
55
//       bits 7:0 R/W Input/Output    '1' = output mode
56
//                                    '0' = input mode
57
// 0x02: Line Register
58
//       bits 7:0 R   Status                Current GPIO pin level
59
//                W   Output                GPIO pin output level
60
//
61
//
62
// HOWTO:
63
//
64
// Use a pin as an input:
65
// Program the corresponding bit in the control register to 'input mode' ('0').
66
// The pin's state (input level) can be checked by reading the Line Register.
67
// Writing to the GPIO pin's Line Register bit while in input mode has no effect.
68
//
69
// Use a pin as an output:
70
// Program the corresponding bit in the control register to 'output mode' ('1').
71
// Program the GPIO pin's output level by writing to the corresponding bit in
72
// the Line Register.
73
// Reading the GPIO pin's Line Register bit while in output mode returns the
74
// current output level.
75
//
76
// Addapt the core for fewer GPIOs:
77
// If less than 8 GPIOs are required, than the 'io' parameter can be set to
78
// the amount of required interrupts. GPIOs are mapped starting at the LSBs.
79
// So only the 'io' LSBs per register are valid.
80
// All other bits (i.e. the 8-'io' MSBs) are set to zero '0'.
81
// Codesize is approximately linear to the amount of interrupts. I.e. using
82
// 4 instead of 8 GPIO sources reduces the size by approx. half.
83
//
84
 
85
 
86
// synopsys translate_off
87
`include "timescale.v"
88
// synopsys translate_on
89
 
90
module simple_gpio(
91
  clk_i, rst_i, cyc_i, stb_i, adr_i, we_i, dat_i, dat_o, ack_o,
92
  gpio
93
);
94
 
95
  //
96
  // Inputs & outputs
97
  //
98
  parameter io = 8;            // number of GPIOs
99
 
100
  // 8bit WISHBONE bus slave interface
101
  input         clk_i;         // clock
102
  input         rst_i;         // reset (asynchronous active low)
103
  input         cyc_i;         // cycle
104
  input         stb_i;         // strobe
105
  input         adr_i;         // address adr_i[1]
106
  input         we_i;          // write enable
107
  input  [ 7:0] dat_i;         // data output
108
  output [ 7:0] dat_o;         // data input
109
  output        ack_o;         // normal bus termination
110
 
111
  // GPIO pins
112
  inout  [io:1] gpio;
113
 
114
  //
115
  // Module body
116
  //
117
  reg [io:1] ctrl, line;                  // ControlRegister, LineRegister
118
  reg [io:1] lgpio, llgpio;               // LatchedGPIO pins
119
 
120
  //
121
  // perform parameter checks
122
  //
123
  // synopsys translate_off
124
  initial
125
  begin
126
      if(io > 8)
127
        $display("simple_gpio: max. 8 GPIOs supported.");
128
  end
129
  // synopsys translate_on
130
 
131
  //
132
  // WISHBONE interface
133
 
134
  wire wb_acc = cyc_i & stb_i;            // WISHBONE access
135
  wire wb_wr  = wb_acc & we_i;            // WISHBONE write access
136
 
137
  always @(posedge clk_i or negedge rst_i)
138
    if (~rst_i)
139
      begin
140
          ctrl <= #1 {{io}{1'b0}};
141
          line <= #1 {{io}{1'b0}};
142
      end
143
    else if (wb_wr)
144
       if ( adr_i )
145
         ctrl <= #1 dat_i[io-1:0];
146
       else
147
         line <= #1 dat_i[io-1:0];
148
 
149
 
150
  reg [7:0] dat_o;
151
  always @(posedge clk_i)
152
    if ( adr_i )
153
      dat_o <= #1 { {{8-io}{1'b0}}, ctrl};
154
    else
155
      dat_o <= #1 { {{8-io}{1'b0}}, llgpio};
156
 
157
  reg ack_o;
158
  always @(posedge clk_i or negedge rst_i)
159
    if (~rst_i)
160
      ack_o <= #1 1'b0;
161
    else
162
      ack_o <= #1 wb_acc & !ack_o;
163
 
164
 
165
  //
166
  // GPIO section
167
 
168
  // latch GPIO input pins
169
  always @(posedge clk_i)
170
    lgpio <= #1 gpio;
171
 
172
  // latch again (reduce meta-stability risc)
173
  always @(posedge clk_i)
174
    llgpio <= #1 lgpio;
175
 
176
  // assign GPIO outputs
177
  integer n;
178
  reg [io:1] igpio; // temporary internal signal
179
 
180
  always @(ctrl or line)
181
    for(n=1;n<=io;n=n+1)
182
      igpio[n] <= ctrl[n] ? line[n] : 1'bz;
183
 
184
  assign gpio = igpio;
185
 
186
endmodule
187
 

powered by: WebSVN 2.1.0

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