-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainController.v
executable file
·417 lines (312 loc) · 7.81 KB
/
MainController.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
module proc
(
CLOCK_50, // On Board 50 MHz
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK, // VGA BLANK
VGA_SYNC, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B, // VGA Blue[9:0]
SW,
KEY,
LEDR,
LEDG,
HEX0,
HEX1,
HEX2,
HEX3,
HEX4,
HEX5,
HEX6,
HEX7,
LEDR,
LEDG,
PS2_CLK,
PS2_DAT,
);
input [17:0]SW;
input [3:0]KEY;
output reg [17:0]LEDR;
output [7:0]LEDG;
input CLOCK_50; // 50 MHz
inout PS2_CLK;
inout PS2_DAT;
output VGA_CLK; // VGA Clock
output VGA_HS; // VGA H_SYNC
output VGA_VS; // VGA V_SYNC
output VGA_BLANK; // VGA BLANK
output VGA_SYNC; // VGA SYNC
output [9:0] VGA_R; // VGA Red[9:0]
output [9:0] VGA_G; // VGA Green[9:0]
output [9:0] VGA_B; // VGA Blue[9:0]
output [6:0] HEX0;
output [6:0] HEX1;
output [6:0] HEX2;
output [6:0] HEX3;
output [6:0] HEX4;
output [6:0] HEX5;
output [6:0] HEX6;
output [6:0] HEX7;
wire [10:0]addressCPU;
wire writeEnableRamCPU;
wire [31:0]dataWriteCPU;
reg enableCPU;
wire acknowledgeCPU;
wire [10:0]addressWTR;
wire writeEnableRamWTR;
wire [31:0]dataWriteWTR;
reg enableWTR;
wire acknowledgeWTR;
wire [10:0]addressSTR;
wire writeEnableRamSTR;
wire [31:0]dataWriteSTR;
reg enableSTR;
wire acknowledgeSTR;
wire [10:0]addressDRAW;
wire writeEnableRamDRAW;
wire [31:0]dataWriteDRAW;
reg enableDRAW;
wire acknowledgeDRAW;
reg [10:0]address;
reg writeEnableRam;
reg [31:0]dataWrite;
wire [31:0]dataRead;
reg [2:0]states_Q;
reg [2:0]states_D;
reg [1:0]wait_Q;
reg [1:0]wait_D;
reg [10:0]counterAddress_Q;
reg [10:0]counterAddress_D;
wire [3:0]options;
assign options[0]=enter;
assign options[1]=did_change;
assign options[2]=submit;
assign options[3]=SW[17];//for debugin
always@(*)
begin
case(states_Q)
3'b000://rest
begin
wait_D<=0;
enableDRAW<=0;
enableWTR<=0;
address=0;
writeEnableRam=0;
dataWrite=0;
enableSTR<=0;
enableCPU<=0;
counterAddress_D<=counterAddress_Q;
case(options)
4'b0001: states_D<=3'b101;//press enter
4'b0010: states_D<=3'b100;//after every change
4'b0100: states_D<=3'b110;//goes to cpu
4'b1000: states_D<=3'b011;//for debugin
default: states_D<=3'b000;//goes to 0
endcase
end
3'b001://write to high ram
begin
wait_D<=0;
enableDRAW<=0;
enableWTR<=1;
enableSTR<=0;
enableCPU<=0;
address=~addressWTR;//last ram
writeEnableRam=writeEnableRamWTR;
dataWrite=dataWriteWTR;
counterAddress_D<=counterAddress_Q;
if(acknowledgeWTR)
states_D<=3'b010;
else
states_D<=3'b001;
end
3'b010://draw to vga using last ram
begin
wait_D<=0;
address=~addressDRAW;//last ram
writeEnableRam=0;
dataWrite=0;
enableDRAW<=1;
enableWTR<=0;
enableSTR<=0;
enableCPU<=0;
counterAddress_D<=counterAddress_Q;
if(acknowledgeDRAW)
states_D<='b000;
else
states_D<=3'b010;
end
3'b011://check (only for debuging)
begin
wait_D<=0;
address[5:0]=SW[15:10];
address[6]=SW[16];
address[7]=SW[16];
address[8]=SW[16];
address[9]=SW[16];
address[10]=SW[16];
writeEnableRam=0;
dataWrite=0;
enableDRAW<=0;
enableWTR<=0;
enableSTR<=0;
enableCPU<=0;
counterAddress_D<=counterAddress_Q;
if(SW[17])
states_D<=3'b011;
else
states_D<=3'b000;
end
3'b100://modify ram
begin
address=~0;
writeEnableRam=1;
dataWrite=data;
enableDRAW<=0;
enableWTR<=0;
enableSTR<=0;
enableCPU<=0;
counterAddress_D<=counterAddress_Q;
if(&wait_Q)
begin
states_D<=3'b010;
wait_D<=0;
end
else
begin
states_D<=3'b100;
wait_D<=wait_Q+1;
end
end
3'b101://write to high ram
begin
wait_D<=0;
enableDRAW<=0;
enableWTR<=0;
enableCPU<=0;
enableSTR<=1;
address=addressSTR;//last ram
writeEnableRam=writeEnableRamSTR;
dataWrite=dataWriteSTR;
if(acknowledgeSTR)
begin
counterAddress_D<=counterAddress_Q+1;
states_D<=3'b001;
end
else
begin
states_D<=3'b101;
counterAddress_D<=counterAddress_Q;
end
end
3'b110://go to cpu
begin
wait_D<=0;
enableDRAW<=0;
enableWTR<=0;
enableSTR<=0;
enableCPU<=1;
address=addressCPU;//last ram
writeEnableRam=writeEnableRamCPU;
dataWrite=dataWriteCPU;
if(acknowledgeCPU)
begin
counterAddress_D<=0;
states_D<=3'b010;
end
else
begin
states_D<=3'b110;
end
end
endcase
end
always@(posedge CLOCK_50)
begin
states_Q<=states_D;
wait_Q<=wait_D;
counterAddress_Q=counterAddress_D;
end
wire writeEnable;
wire [31:0]data;
wire [2:0] color;
wire [7:0] x;
wire [6:0] y;
wire resetIn;
assign resetIn = KEY[0];
wire [17:0]LEDR2;
assign LEDR2[15]=&wait_Q;
assign LEDR2[12:10]=states_Q;
assign LEDR2[4:0]=address[4:0];
assign LEDR2[5]=address[10];
assign LEDR2[7:6]=wait_Q;
assign LEDR2[9]=writeEnableRam;
assign LEDG=dataRead[7:0];
wire [17:0]LEDR3;
assign LEDR3=dataWrite[17:0];
always@(*)
begin
if(SW[16]&~SW[15])
LEDR=LEDR2;
if(~SW[16]&SW[15])
LEDR=LEDR3;
if(~SW[16]&~SW[15])
LEDR=LEDR1;
if(SW[16]&SW[15])
LEDR[10:0]=address;
end
wire enter,submit,did_change;
wire [31:0]datafake;
wire [4:0]KEYfake;
wire [7:0]LEDRfake;
wire [7:0]LEDGfake;
//acces to ram
mainMemoryPreload (address,CLOCK_50,dataWrite,writeEnableRam,dataRead);
drawToVGA(enableDRAW,acknowledgeDRAW,dataRead,addressDRAW,x,y,color,writeEnable,CLOCK_50,resetIn);
//accees to key board
input_control (
// Inputs
CLOCK_50,
KEYfake,
// Bidirectionals
PS2_CLK,
PS2_DAT,
// Outputs (DE2)
HEX0,HEX1,
HEX2,HEX3,
HEX4,HEX5,
HEX6,HEX7,
LEDRfake,LEDGfake,enter,submit,did_change,data);
//writes to high ram
writeToRam(0,enableWTR,dataRead,acknowledgeWTR, addressWTR,dataWriteWTR,writeEnableRamWTR,CLOCK_50,resetIn);
wire [17:0]LEDR1;
mainControllerCPU(enableCPU,acknowledgeCPU,dataRead,addressCPU,dataWriteCPU,writeEnableRamCPU,CLOCK_50,resetIn,LEDR1,SW[9:0],~KEY[3]);
//wirtes to the low ram
wire [17:0]fakeLEDR2;
saveToLowRam(enableSTR,acknowledgeSTR,dataRead,counterAddress_Q, addressSTR,dataWriteSTR,writeEnableRamSTR,CLOCK_50,resetIn);
// Create an Instance of a VGA controller - "There can be only one!"
// Define the number of colours as well as the initial background
// image file (.MIF) for the controller.
vga_adapter VGA(
.resetn(resetIn),
.clock(CLOCK_50),
.colour(color),
.x(x),
.y(y),
.plot(writeEnable),
/* Signals for the DAC to drive the monitor. */
.VGA_R(VGA_R),
.VGA_G(VGA_G),
.VGA_B(VGA_B),
.VGA_HS(VGA_HS),
.VGA_VS(VGA_VS),
.VGA_BLANK(VGA_BLANK),
.VGA_SYNC(VGA_SYNC),
.VGA_CLK(VGA_CLK));
defparam VGA.RESOLUTION = "160x120";
defparam VGA.MONOCHROME = "FALSE";
defparam VGA.BITS_PER_COLOUR_CHANNEL = 1;
defparam VGA.BACKGROUND_IMAGE = "backgroundMemory.mif";
endmodule