-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg_rom.v
214 lines (171 loc) · 3.87 KB
/
bg_rom.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
module bg_rom(
input clk,
input resetn,
input start,
output [7:0] x,
output [6:0] y,
output [2:0] c_out,
output done);
wire ld_all, enable;
datapath_back d0(
.clk(clk),
.resetn(resetn),
.ld_all(ld_all),
.enable(enable),
.x(x),
.y(y),
.c_out(c_out));
control_back c0(
.clk(clk),
.resetn(resetn),
.start(start),
.ld_all(ld_all),
.enable(enable),
.done(done));
endmodule
module datapath_back(
input clk,
input resetn,
input ld_all,
input enable,
output reg [7:0] x,
output reg [6:0] y,
output reg [2:0] c_out);
reg [7:0] x_hold;
reg [6:0] y_hold;
reg [2:0] c_hold;
reg [14:0] address;
reg [7:0] x_address;
reg [6:0] y_address;
reg double;
wire [2:0] colour;
// Registers x, y, c with respective input logic
always @ (posedge clk) begin
if (!resetn)
begin
x <= 8'd0;
y <= 7'd0;
c_out <= 3'd0;
end
else if (enable)
begin
x <= x_address;
y <= y_address;
c_out <= colour;
end
end
always@(posedge clk)
begin: address_counter
if(!resetn | !enable)
begin
x_address <= 8'd0;
y_address <= 7'd0;
address <= 15'd0;
double <= 1'd0;
end
else if (enable)
begin
if (double == 1'd1)
begin
double <= 1'd0;
end
else if (address == 15'd19199)
begin
x_address <= 8'd0;
y_address <= 7'd0;
address <= 15'd0;
double <= 1'd1;
end
else if (x_address == 8'd159)
begin
x_address <= 8'd0;
y_address <= y_address + 1'd1;
address <= address + 1'd1;
double <= 1'd1;
end
else
begin
x_address <= x_address + 1'd1;
address <= address + 1'd1;
double <= 1'd1;
end
end
end // address
back_rom b0(
.address(address),
.clock(clk),
.q(colour));
endmodule
module control_back(
input clk,
input resetn,
input start,
output reg ld_all,
output reg enable,
output reg done);
reg [4:0] current_state, next_state;
reg [15:0] counter;
wire wait_plot = (counter > 16'd40000);
localparam S_DRAW = 5'd0,
S_DONE = 5'd1,
S_INITIAL = 5'd2,
S_START = 5'd3,
S_START_WAIT = 5'd4;
// Next state logic aka our state table for plotting
always@(*)
begin: state_table
case (current_state)
S_INITIAL: next_state = start ? S_DRAW : S_INITIAL; // Loads initial x position
S_START: next_state = start ? S_DRAW : S_START;
S_DRAW: next_state = wait_plot ? S_DONE : S_DRAW;
S_DONE: next_state = S_START;
default: next_state = S_INITIAL;
endcase
end // state_table
// Output logic aka all of our datapath control signals
always @(*)
begin: enable_signals
// By default make all our signals 0
ld_all = 1'b0;
enable = 1'b0;
done = 1'b0;
case (current_state)
S_INITIAL:
begin
ld_all = 1'b1;
end
S_DRAW:
begin
enable = 1'b1;
end
S_DONE:
begin
done = 1'b1;
end
default: enable = 1'b0;
endcase
end
always@(posedge clk)
begin: sixteen_counter
if(!resetn | !enable)
begin
counter <= 16'd0;
end
else
begin
counter <= counter + 16'd1;
end
end // counter
// current_state registers
always@(posedge clk)
begin: state_FFs
if(!resetn)
begin
current_state <= S_INITIAL;
end
else
begin
current_state <= next_state;
end
end // state_FFS
endmodule