-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsesame.v
executable file
·538 lines (480 loc) · 16.3 KB
/
sesame.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 14:22:31 02/18/2016
// Design Name:
// Module Name: sesame
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module sesame(
input clk,
input [7:0] sw,
input MISO,
input btnS,
input btnU, //
input btnD,
input btnR,
input btnL,
output SS,
output MOSI,
output SCLK,
output reg [2:0] vgaRed,
output reg [2:0] vgaGreen,
output reg [2:1] vgaBlue,
output reg Hsync,
output reg Vsync
);
// Constants
wire [9:0] drawAreaWidth = 240;
wire [9:0] drawAreaHeight = 240;
wire [9:0] drawAreaStartX = 152 + 80;
wire [9:0] drawAreaStartY = 37;
wire [7:0] COLOR_BLACK = 0;
wire [7:0] COLOR_WHITE = 8'b11111111;
wire [7:0] COLOR_PURPLE = 8'b10000010;
wire [7:0] COLOR_PINK = 8'b11101110;
/******************************/
/*********VGA Driver***********/
/******************************/
reg clk_vga = 0;
reg counter_vga = 0;
reg [9:0] vgaX = 0; // VGA Driver's horizontal scanning position, 0-799
reg [9:0] vgaY = 0; // VGA Driver's vertical scanning position, 0-524
wire [9:0] vgaDrawX; // current X position in the draw screen for VGA Driver's scan position, 0-screenWidth
assign vgaDrawX = (vgaX - drawAreaStartX) / 2;
wire [9:0] vgaDrawY; // current Y position in the draw screen for VGA Driver's scan position, 0-screenHeight
assign vgaDrawY = (vgaY - drawAreaStartY) / 2;
// True if VGA scanning position is in the drawing region
wire inDrawArea;
assign inDrawArea = (vgaX >= drawAreaStartX && vgaX < (drawAreaStartX + (drawAreaWidth*2)) &&
vgaY >= drawAreaStartY && vgaY < (drawAreaStartY + (drawAreaHeight*2)));
// True if VGA scanning position is in the screen region
wire inScreenArea;
assign inScreenArea = (vgaX >= 152 && vgaX < 792 && vgaY >= 37 && vgaY < 517);
reg [9:0] cursorX = 100;
reg [9:0] cursorY = 100;
always @(posedge clk)
begin
counter_vga <= ~counter_vga;
end
always @(posedge counter_vga)
begin
clk_vga <= ~clk_vga;
end
// Update Hsync, Vsync, and vga X/Y
always @(posedge clk_vga)
begin
Hsync <= ~(vgaX >= 8 && vgaX < 104);
Vsync <= ~(vgaY >= 2 && vgaY < 4);
vgaX <= vgaX == 799 ? 0 : vgaX + 1; // vgaX goes 0-799 on clk
if (vgaX == 799)
vgaY <= vgaY == 524 ? 0 : vgaY + 1; // vgaY goes 0-524 on ~HSync and clk
end
// Update VGA RGB
always @(posedge clk_vga)
begin
// Feed RGB black when outside screen area
if (~inScreenArea)
begin
update_VGA_RGB(COLOR_BLACK);
end
// When in the draw area, use the pixel map
else if (inDrawArea)
begin
//drawing stamp cursor
if (current_tool == TOOL_STAMP && vgaDrawX >= cursorX - 15 && vgaDrawX < cursorX + 15 && vgaDrawY >= cursorY - 15 && vgaDrawY < cursorY + 15)
begin
stamp_menu_addr <= (vgaDrawX - (cursorX - 15)) + ((vgaDrawY - (cursorY - 15)) * 30);
if (stamp_menu_pixel != 8'b00000011 )
update_VGA_RGB(stamp_menu_pixel);
else
begin
pmop_addr <= vgaDrawX + vgaDrawY * drawAreaWidth;
update_VGA_RGB(pmop_pixel);
end
end
else if (vgaDrawX >= cursorX - pencil_size / 2 && vgaDrawX < cursorX + pencil_size / 2 && vgaDrawY >= cursorY - pencil_size / 2 && vgaDrawY < cursorY + pencil_size / 2)
begin
if (vgaDrawX >= cursorX - (pencil_size-1) / 2 && vgaDrawX < cursorX + (pencil_size-1) / 2 && vgaDrawY >= cursorY - (pencil_size-1) / 2 && vgaDrawY < cursorY + (pencil_size-1) / 2)
update_VGA_RGB(current_tool == TOOL_PENCIL ? sw : COLOR_PINK);
else
update_VGA_RGB(COLOR_BLACK);
end
else
begin
pmop_addr <= vgaDrawX + vgaDrawY * drawAreaWidth;
update_VGA_RGB(pmop_pixel);
end
end
// When outside the draw area, have other logic (draw menus?)
else
begin
// Possibly use vgaX and vgaY to draw menus, with another module to get menu pixels?
update_VGA_RGB(sw);
end
end
// Take in 8 bit color and set VGA colors accordingly
task update_VGA_RGB;
input [7:0] color;
begin
vgaRed = color[7:5];
vgaGreen = color[4:2];
vgaBlue = color[1:0];
end
endtask
/******************************/
/*******Cursor Control*********/
/******************************/
reg [31:0] counter_cursor = 0;
reg clk_cursor = 0; // Ticks at 30 Hz
always @(posedge clk)
begin
counter_cursor <= (counter_cursor == 1666666) ? 0 : counter_cursor + 1;
if (counter_cursor == 0)
clk_cursor <= ~clk_cursor;
end
always @(posedge clk_cursor)
begin
if (joystick_x < 150 && cursorX < drawAreaWidth - 2) cursorX <= cursorX + 2;
else if (joystick_x < 400 && cursorX < drawAreaWidth - 1) cursorX <= cursorX + 1;
if (joystick_x > 850 && cursorX > 2) cursorX <= cursorX - 2;
else if (joystick_x > 600 && cursorX > 1) cursorX <= cursorX - 1;
if (joystick_y < 150 && cursorY > 2) cursorY <= cursorY - 2;
else if (joystick_y < 400 && cursorY > 1) cursorY <= cursorY - 1;
if (joystick_y > 850 && cursorY < drawAreaHeight - 2) cursorY <= cursorY + 2;
else if (joystick_y > 600 && cursorY < drawAreaHeight - 1) cursorY <= cursorY + 1;
end
/******************************/
/********Tool Control**********/
/******************************/
reg [5:0] current_tool = 2;
reg [5:0] old_tool = 0; // Used to go back to current_tool after resetting screen
wire [5:0] TOOL_NONE = 0;
wire [5:0] TOOL_RESET = 1;
wire [5:0] TOOL_PENCIL = 2;
wire [5:0] TOOL_ERASER = 3;
wire [5:0] TOOL_STAMP = 4;
reg is_resetting = 0;
reg is_tool_on = 0;
reg [31:0] reset_counter = 0;
reg [31:0] pencil_size = 4;
reg [31:0] pencil_x = 0;
reg [31:0] pencil_y = 0;
reg [31:0] stamp_size = 30;
reg [31:0] stamp_x = 0;
reg [31:0] stamp_y = 0;
reg [31:0] current_stamp = 4;
reg [31:0] counter_debounce = 0;
wire clk_debounce;
assign clk_debounce = counter_debounce == 166666;
always @(posedge clk)
begin
counter_debounce <= counter_debounce == 166666 ? 0 : counter_debounce + 1;
end
reg btnRdb = 0;
reg btnLdb = 0;
reg btnUdb = 0;
reg btnDdb = 0;
always @(posedge clk_debounce)
begin
btnRdb <= btnR;
btnLdb <= btnL;
btnUdb <= btnU;
btnDdb <= btnD;
end
wire comboLR = btnRdb || btnLdb;
always @(posedge comboLR)
begin
if (btnRdb)
begin
if (current_tool == TOOL_STAMP)
begin
current_stamp <= current_stamp == 6 ? 0 :current_stamp + 1;
end
else
begin
case (pencil_size)
2: pencil_size <= 4;
4: pencil_size <= 6;
endcase
end
end
if (btnLdb)
begin
if (current_tool == TOOL_STAMP)
begin
current_stamp <= current_stamp == 0 ? 6 :current_stamp - 1;
end
else
begin
case (pencil_size)
4: pencil_size <= 2;
6: pencil_size <= 4;
endcase
end
end
end
wire comboUD = btnUdb || btnDdb;
always @(posedge comboUD)
begin
if (btnUdb)
begin
if (current_tool == TOOL_PENCIL) current_tool <= TOOL_STAMP;
if (current_tool == TOOL_ERASER) current_tool <= TOOL_PENCIL;
if (current_tool == TOOL_STAMP) current_tool <= TOOL_ERASER;
end
if (btnDdb)
begin
if (current_tool == TOOL_PENCIL) current_tool <= TOOL_ERASER;
if (current_tool == TOOL_ERASER) current_tool <= TOOL_STAMP;
if (current_tool == TOOL_STAMP) current_tool <= TOOL_PENCIL;
end
end
always @(posedge clk)
begin
// Set current tool
if (btnS && current_tool != TOOL_RESET)
begin
is_resetting <= 1;
reset_counter <= 0;
end
else if (joystick_btn_left)
is_tool_on <= 1;
if (is_resetting)
begin
if (reset_counter == drawAreaWidth * drawAreaHeight)
begin
is_resetting <= 0;
wea <= 0;
end
else
begin
wea <= 1;
dina <= COLOR_WHITE;
pmin_addr <= reset_counter;
reset_counter <= reset_counter + 1;
end
end
if (is_tool_on && current_tool == TOOL_PENCIL)
begin
if (pencil_y == pencil_size)
begin
is_tool_on <= 0;
wea <= 0;
pencil_x <= 0;
pencil_y <= 0;
end
else
begin
wea <= 1;
dina <= sw;
pmin_addr <= (cursorX + pencil_x - pencil_size / 2) + (cursorY + pencil_y - pencil_size / 2) * drawAreaWidth;
if (pencil_x == pencil_size - 1)
begin
pencil_x <= 0;
pencil_y <= pencil_y + 1;
end
else
pencil_x <= pencil_x + 1;
end
end
if (is_tool_on && current_tool == TOOL_ERASER)
begin
if (pencil_y == pencil_size)
begin
is_tool_on <= 0;
wea <= 0;
pencil_x <= 0;
pencil_y <= 0;
end
else
begin
wea <= 1;
dina <= COLOR_WHITE;
pmin_addr <= (cursorX + pencil_x - pencil_size / 2) + (cursorY + pencil_y - pencil_size / 2) * drawAreaWidth;
if (pencil_x == pencil_size - 1)
begin
pencil_x <= 0;
pencil_y <= pencil_y + 1;
end
else
pencil_x <= pencil_x + 1;
end
end
if (is_tool_on && current_tool == TOOL_STAMP)
begin
if (stamp_y == stamp_size)
begin
is_tool_on <= 0;
wea <= 0;
stamp_x <= 0;
stamp_y <= 0;
stamp_draw_addr <= 0;
end
else
begin
stamp_draw_addr <= (stamp_x + (stamp_y * stamp_size));
wea <= stamp_draw_pixel != 8'b00000011;
dina <= stamp_draw_pixel;
pmin_addr <= (cursorX + stamp_x - stamp_size / 2) + (cursorY + stamp_y - stamp_size / 2) * drawAreaWidth;
if (stamp_x == stamp_size)
begin
stamp_x <= 0;
stamp_y <= stamp_y + 1;
end
else
stamp_x <= stamp_x + 1;
end
end
end
/******************************/
/*********Pixel Map ***********/
/******************************/
reg [15:0] pmop_addr;
wire [7:0] pmop_pixel;
reg wea;
reg [15 : 0] pmin_addr;
reg [7 : 0] dina;
pixel_map pixel_map(
.clka(clk), // input clka
.wea(wea), // input [0 : 0] wea
.addra(pmin_addr), // input [15 : 0] addra
.dina(dina), // input [7 : 0] dina
.clkb(clk), // input clkb
.addrb(pmop_addr), // input [15 : 0] addrb
.doutb(pmop_pixel) // output [7 : 0] doutb
);
reg [9:0] stamp_draw_addr = 0;
wire [7:0] stamp_draw_pixel;
reg [9:0] stamp_menu_addr = 0;
wire [7:0] stamp_menu_pixel;
assign stamp_draw_pixel =
current_stamp == 0 ? stamp_draw_pixel_0 :
current_stamp == 1 ? stamp_draw_pixel_1 :
current_stamp == 2 ? stamp_draw_pixel_2 :
current_stamp == 3 ? stamp_draw_pixel_3 :
current_stamp == 4 ? stamp_draw_pixel_4 :
current_stamp == 5 ? stamp_draw_pixel_5 : stamp_draw_pixel_6;
assign stamp_menu_pixel =
current_stamp == 0 ? stamp_menu_pixel_0 :
current_stamp == 1 ? stamp_menu_pixel_1 :
current_stamp == 2 ? stamp_menu_pixel_2 :
current_stamp == 3 ? stamp_menu_pixel_3 :
current_stamp == 4 ? stamp_menu_pixel_4 :
current_stamp == 5 ? stamp_menu_pixel_5 : stamp_menu_pixel_6;
wire [7:0] stamp_draw_pixel_0;
wire [7:0] stamp_menu_pixel_0;
stamp_matt stamp0 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_0), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_0) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_1;
wire [7:0] stamp_menu_pixel_1;
stamp_penguin stamp1 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_1), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_1) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_2;
wire [7:0] stamp_menu_pixel_2;
stamp_jellyfish stamp2 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_2), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_2) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_3;
wire [7:0] stamp_menu_pixel_3;
stamp_cat stamp3 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_3), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_3) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_4;
wire [7:0] stamp_menu_pixel_4;
stamp_taco stamp4 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_4), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_4) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_5;
wire [7:0] stamp_menu_pixel_5;
stamp_mushroom stamp5 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_5), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_5) // output [7 : 0] doutb
);
wire [7:0] stamp_draw_pixel_6;
wire [7:0] stamp_menu_pixel_6;
stamp_heart stamp6 (
.clka(clk), // input clka
.addra(stamp_draw_addr), // input [9 : 0] addra
.douta(stamp_draw_pixel_6), // output [7 : 0] douta
.clkb(clk), // input clkb
.addrb(stamp_menu_addr), // input [9 : 0] addrb
.doutb(stamp_menu_pixel_6) // output [7 : 0] doutb
);
wire [7:0] stamp_menu_pixel_7;
stamp_pencil stamp7 (
.clka(clk), // input clka
.addra(stamp_menu_addr), // input [9 : 0] addra
.douta(stamp_menu_pixel_7) // output [7 : 0] douta
);
wire [7:0] stamp_menu_pixel_8;
stamp_eraser stamp8 (
.clka(clk), // input clka
.addra(stamp_menu_addr), // input [9 : 0] addra
.douta(stamp_menu_pixel_8) // output [7 : 0] douta
);
/******************************/
/*********Joystick ***********/
/******************************/
wire SS; // Active low
wire MOSI; // Data transfer from master to slave
wire SCLK; // Serial clock that controls communication
assign MOSI = 0;
// Data read from PmodJSTK
wire [39:0] jstkData;
wire [9 : 0] joystick_y = {jstkData[9:8], jstkData[23:16]};
wire [9 : 0] joystick_x = {jstkData[25:24], jstkData[39:32]};
wire joystick_btn_right = jstkData[1];
wire joystick_btn_left = jstkData[2];
joy joy(
.CLK(clk),
.sndRec(clk_cursor),
.MISO(MISO),
.SS(SS),
.SCLK(SCLK),
.DOUT(jstkData)
);
endmodule