-
Notifications
You must be signed in to change notification settings - Fork 0
/
Altera_UP_Avalon_PS2.v
219 lines (173 loc) · 6.28 KB
/
Altera_UP_Avalon_PS2.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*****************************************************************************
* *
* Module: Altera_UP_Avalon_PS2 *
* Description: *
* This module connects the PS2 core to Avalon. *
* *
*****************************************************************************/
/*
*
* Data Register Bits
* Read Available 31-16, Incoming Data or Outgoing Command 7-0
*
* Control Register Bits
* CE 10, RI 8, RE 0
*
**/
module Altera_UP_Avalon_PS2 (
// Inputs
clk,
reset,
address,
chipselect,
byteenable,
read,
write,
writedata,
// Bidirectionals
PS2_CLK, // PS2 Clock
PS2_DAT, // PS2 Data
// Outputs
irq,
readdata,
waitrequest
);
/*****************************************************************************
* Parameter Declarations *
*****************************************************************************/
/*****************************************************************************
* Port Declarations *
*****************************************************************************/
// Inputs
input clk;
input reset;
input address;
input chipselect;
input [3:0] byteenable;
input read;
input write;
input [31:0] writedata;
// Bidirectionals
inout PS2_CLK;
inout PS2_DAT;
// Outputs
output irq;
output [31:0] readdata;
output waitrequest;
reg [31:0] readdata;
/*****************************************************************************
* Constant Declarations *
*****************************************************************************/
/*****************************************************************************
* Internal wires and registers Declarations *
*****************************************************************************/
// Internal Wires
wire [7:0] data_from_the_PS2_port;
wire data_from_the_PS2_port_en;
wire get_data_from_PS2_port;
wire send_command_to_PS2_port;
wire clear_command_error;
wire set_interrupt_enable;
wire command_was_sent;
wire error_sending_command;
wire data_fifo_is_empty;
wire data_fifo_is_full;
// Internal Registers
reg [31:0] data_register;
reg [31:0] control_register;
reg [7:0] data_in_fifo;
reg [15:0] data_available;
// State Machine Registers
/*****************************************************************************
* Finite State Machine(s) *
*****************************************************************************/
/*****************************************************************************
* Sequential logic *
*****************************************************************************/
always @(posedge clk)
begin
if (reset == 1'b1)
readdata <= 32'h00000000;
else if (chipselect == 1'b1)
begin
if (address == 1'b0)
readdata <= {data_available, 8'h00, data_in_fifo};
else
readdata <= control_register;
end
end
always @(posedge clk)
begin
if (reset == 1'b1)
control_register <= 32'h00000000;
else
begin
if (error_sending_command == 1'b1)
control_register[10] <= 1'b1;
else if (clear_command_error == 1'b1)
control_register[10] <= 1'b0;
control_register[8] <= ~data_fifo_is_empty & control_register[0];
if ((chipselect == 1'b1) && (set_interrupt_enable == 1'b1))
control_register[0] <= writedata[0];
end
end
/*****************************************************************************
* Combinational logic *
*****************************************************************************/
assign irq = control_register[8];
assign waitrequest = send_command_to_PS2_port &
~(command_was_sent | error_sending_command);
assign get_data_from_PS2_port = chipselect & byteenable[0] & ~address & read;
assign send_command_to_PS2_port= chipselect & byteenable[0] & ~address & write;
assign clear_command_error = chipselect & byteenable[1] & address & write;
assign set_interrupt_enable = chipselect & byteenable[0] & address & write;
/*****************************************************************************
* Internal Modules *
*****************************************************************************/
Altera_UP_PS2 PS2_Serial_Port (
// Inputs
.clk (clk),
.reset (reset),
.the_command (writedata[7:0]),
.send_command (send_command_to_PS2_port),
// Bidirectionals
.PS2_CLK (PS2_CLK),
.PS2_DAT (PS2_DAT),
// Outputs
.command_was_sent (command_was_sent),
.error_communication_timed_out (error_sending_command),
.received_data (data_from_the_PS2_port),
.received_data_en (data_from_the_PS2_port_en)
);
scfifo Incoming_Data_FIFO (
// Inputs
.clock (clk),
.sclr (reset),
.rdreq (get_data_from_PS2_port & ~data_fifo_is_empty),
.wrreq (data_from_the_PS2_port_en & ~data_fifo_is_full),
.data (data_from_the_PS2_port),
// Bidirectionals
// Outputs
.q (data_in_fifo),
.usedw (data_available),
.empty (data_fifo_is_empty),
.full (data_fifo_is_full)
// synopsys translate_off
,
.almost_empty (),
.almost_full (),
.aclr ()
// synopsys translate_on
);
defparam
Incoming_Data_FIFO.add_ram_output_register = "ON",
Incoming_Data_FIFO.intended_device_family = "Cyclone II",
Incoming_Data_FIFO.lpm_numwords = 256,
Incoming_Data_FIFO.lpm_showahead = "ON",
Incoming_Data_FIFO.lpm_type = "scfifo",
Incoming_Data_FIFO.lpm_width = 8,
Incoming_Data_FIFO.lpm_widthu = 8,
Incoming_Data_FIFO.overflow_checking = "OFF",
Incoming_Data_FIFO.underflow_checking = "OFF",
Incoming_Data_FIFO.use_eab = "ON";
endmodule