forked from somhi/jtframe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjt4701.v
281 lines (248 loc) · 7.84 KB
/
jt4701.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* This file is part of JTFRAME.
JTFRAME program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JTFRAME program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JTFRAME. If not, see <http://www.gnu.org/licenses/>.
Author: Jose Tejada Gomez. Twitter: @topapate
Version: 1.0
Date: 12-5-2020 */
// Equivalent to NEC uPD4701A
// if A comes first, it increases the count
module jt4701(
input clk,
input rst,
input [1:0] x_in, // MSB=A, LSB=B
input [1:0] y_in, // MSB=A, LSB=B
input rightn,
input leftn,
input middlen,
input x_rst,
input y_rst,
input csn, // chip select
input uln, // byte selection
input xn_y, // select x or y for reading
output reg cfn, // counter flag
output reg sfn, // switch flag
output reg [7:0] dout,
output reg dir
);
wire [11:0] cntx, cnty;
wire xflagn, yflagn, xdir, ydir;
wire [ 7:0] upper, lower;
assign upper = { sfn, leftn, rightn, middlen, xn_y ? cnty[11:8] : cntx[11:8] };
assign lower = xn_y ? cnty[7:0] : cntx[7:0];
jt4701_axis u_axisx(
.clk ( clk ),
.rst ( x_rst ),
.sigin ( x_in ),
.flag_clrn ( csn ),
.flagn ( xflagn ),
.axis ( cntx ),
.dir ( xdir ),
.step ( )
);
jt4701_axis u_axisy(
.clk ( clk ),
.rst ( y_rst ),
.sigin ( y_in ),
.flag_clrn ( csn ),
.flagn ( yflagn ),
.axis ( cnty ),
.dir ( ydir ),
.step ( )
);
always @(posedge clk, posedge rst) begin
if( rst ) begin
cfn <= 1;
sfn <= 1;
dout <= 8'd0;
end else begin
sfn <= leftn && middlen && rightn;
cfn <= xflagn && yflagn;
dout <= uln ? upper : lower;
dir <= xn_y ? ydir : xdir;
end
end
endmodule
module jt4701_axis(
input clk,
input rst, // synchronous
input [1:0] sigin,
input flag_clrn,
output reg flagn,
output reg [11:0] axis,
output reg dir, // valid value if step is set, 1 for inc 0 for dec
output reg step // high if a step has occured
);
parameter HOTONE=0,
SLOWN =9; // bits for slowdown counter (only used if HOTONE is 1)
wire [1:0] xedge;
reg [SLOWN-1:0] subcnt=0;
reg [1:0] last_in, locked, last_xedge;
wire posedge_a, posedge_b, negedge_a, negedge_b;
reg ping, pong;
assign xedge = sigin ^ last_in;
assign posedge_a = ~last_in[1] & sigin[1];
assign posedge_b = ~last_in[0] & sigin[0];
assign negedge_a = last_in[1] & ~sigin[1];
assign negedge_b = last_in[0] & ~sigin[0];
`ifdef SIMULATION
initial begin
axis = 12'd0;
last_in = 2'b0;
flagn = 1;
locked = 2'b00;
end
`endif
always @(posedge clk) begin
if( rst ) begin
axis <= HOTONE ? 12'd1 : 12'd0;
last_in<= 2'b0;
flagn <= 1;
locked <= 2'b00;
ping <= 0;
pong <= 0;
dir <= 0;
step <= 0;
end else begin
flagn <= !flag_clrn || !(xedge!=2'b00 && locked[0]!=locked[1]);
last_in <= sigin;
if( posedge_b ) begin
ping <= 0;
pong <= 1;
end
if( posedge_a ) begin
ping <= 1;
pong <= 0;
end
step <= 0;
if( (posedge_b && !sigin[1]) || (negedge_b && sigin[1]) ) begin
if(ping) begin
subcnt <= subcnt + 1'd1;
if( !HOTONE ) begin
axis <= axis - 12'd1;
dir <= 0;
step <= 1;
end else if(&subcnt) begin
axis <= {axis[0],axis[11:1]};
dir <= 0;
step <= 1;
end
end
end
if( (posedge_a && !sigin[0]) || (negedge_a && sigin[0]) ) begin
if( pong ) begin
subcnt <= subcnt + 1'd1;
if( !HOTONE ) begin
axis <= axis + 12'd1;
dir <= 1;
step <= 1;
end else if(&subcnt) begin
axis <= {axis[10:0],axis[11]};
dir <= 1;
step <= 1;
end
end
end
if( HOTONE && axis==0 ) axis <= 1;
end
end
endmodule
module jt4701_dialemu(
input rst,
input clk,
input pulse,
input inc,
input dec,
output reg [1:0] dial
);
reg last_pulse;
always @(posedge clk) last_pulse <= pulse;
always @(posedge clk, posedge rst) begin
if( rst ) begin
dial <= 2'b0;
end else if( pulse && !last_pulse && (inc||dec)) begin
if( inc ) begin
case( dial )
2'b00: dial <= 2'b01;
2'b01: dial <= 2'b11;
2'b11: dial <= 2'b10;
2'b10: dial <= 2'b00;
endcase
end else if( dec ) begin
case( dial )
2'b00: dial <= 2'b10;
2'b01: dial <= 2'b00;
2'b11: dial <= 2'b01;
2'b10: dial <= 2'b11;
endcase
end
end
end
endmodule
//////////////////////////////////////////////////////////
module jt4701_dialemu_2axis(
input rst,
input clk,
input LHBL,
input [1:0] inc,
input [1:0] dec,
// 4701 direct connections
input x_rst,
input y_rst,
input uln, // upper / ~lower
input cs,
input xn_y, // ~x / y
output cfn,
output sfn,
output [7:0] dout
);
wire [1:0] x_in, y_in;
reg [1:0] tick;
reg last_LHBL;
// The dial update ryhtm is set to once every four lines
always @(posedge clk) begin
last_LHBL <= LHBL;
if( LHBL && !last_LHBL ) tick <= tick+2'd1;
end
jt4701 u_dial(
.clk ( clk ),
.rst ( rst ),
.x_in ( x_in ),
.y_in ( y_in ),
.rightn ( 1'b1 ),
.leftn ( 1'b1 ),
.middlen ( 1'b1 ),
.x_rst ( x_rst ),
.y_rst ( y_rst ),
.csn ( ~cs ), // chip select
.uln ( uln ), // byte selection
.xn_y ( xn_y ), // select x or y for reading
.cfn ( cfn ), // counter flag
.sfn ( sfn ), // switch flag
.dout ( dout ),
.dir ( )
);
jt4701_dialemu u_dial1p(
.clk ( clk ),
.rst ( rst ),
.pulse ( tick[1] ),
.inc ( inc[0] ),
.dec ( dec[0] ),
.dial ( x_in )
);
jt4701_dialemu u_dial2p(
.clk ( clk ),
.rst ( rst ),
.pulse ( tick[1] ),
.inc ( inc[1] ),
.dec ( dec[1] ),
.dial ( y_in )
);
endmodule