]>
Commit | Line | Data |
---|---|---|
cf1ae842 | 1 | /* |
05c0805b JW |
2 | * MandelFPGA |
3 | * by Joshua Wise and Chris Lu | |
4 | * | |
5 | * An implementation of a pipelined algorithm to calculate the Mandelbrot set | |
6 | * in real time on an FPGA. | |
7 | */ | |
8 | ||
9 | `define XRES 640 | |
10 | `define YRES 480 | |
281eac32 | 11 | `define WHIRRRRR 27 |
05c0805b JW |
12 | |
13 | module SyncGen( | |
14 | input pixclk, | |
15 | output reg vs, hs, | |
16 | output reg [11:0] xout = `WHIRRRRR, yout = 0, | |
17 | output wire [11:0] xoutreal, youtreal, | |
18 | output reg border); | |
19 | ||
20 | reg [11:0] x = 0, y = 0; // Used for generating border and timing. | |
21 | assign xoutreal = x; | |
22 | assign youtreal = y; | |
23 | ||
24 | parameter XFPORCH = 16; | |
25 | parameter XSYNC = 96; | |
26 | parameter XBPORCH = 48; | |
27 | ||
28 | parameter YFPORCH = 10; | |
29 | parameter YSYNC = 2; | |
30 | parameter YBPORCH = 29; | |
31 | ||
32 | always @(posedge pixclk) | |
33 | begin | |
34 | if (x >= (`XRES + XFPORCH + XSYNC + XBPORCH)) | |
35 | begin | |
36 | if (y >= (`YRES + YFPORCH + YSYNC + YBPORCH)) | |
37 | y <= 0; | |
38 | else | |
39 | y <= y + 1; | |
40 | x <= 0; | |
41 | end else | |
42 | x <= x + 1; | |
43 | ||
44 | if (xout >= (`XRES + XFPORCH + XSYNC + XBPORCH)) | |
45 | begin | |
46 | if (yout >= (`YRES + YFPORCH + YSYNC + YBPORCH)) | |
47 | yout <= 0; | |
48 | else | |
49 | yout <= yout + 1; | |
50 | xout <= 0; | |
51 | end else | |
52 | xout <= xout + 1; | |
53 | hs <= (x >= (`XRES + XFPORCH)) && (x < (`XRES + XFPORCH + XSYNC)); | |
54 | vs <= (y >= (`YRES + YFPORCH)) && (y < (`YRES + YFPORCH + YSYNC)); | |
55 | border <= (x > `XRES) || (y > `YRES); | |
56 | end | |
57 | endmodule | |
58 | ||
59 | // bits: 1.12 | |
60 | ||
61 | module NaiveMultiplier( | |
62 | input clk, | |
63 | input [12:0] x, y, | |
64 | input xsign, ysign, | |
65 | output reg [12:0] out, | |
66 | output reg sign, | |
fb8d158b | 67 | output reg ovf); |
05c0805b JW |
68 | |
69 | always @(posedge clk) | |
70 | begin | |
71 | {ovf,out} <= | |
11cc2d9b JW |
72 | (((y[12] ? (x ) : 0) + |
73 | (y[11] ? (x >> 1) : 0) + | |
74 | (y[10] ? (x >> 2) : 0)) + | |
75 | (((y[9] ? (x >> 3) : 0) + | |
76 | (y[8] ? (x >> 4) : 0)) + | |
ec33b708 | 77 | ((y[7] ? (x >> 5) : 0) + |
fb8d158b | 78 | (y[6] ? (x >> 6) : 0))))+ |
11cc2d9b JW |
79 | (((y[5] ? (x >> 7) : 0) + |
80 | (y[4] ? (x >> 8) : 0) + | |
81 | (y[3] ? (x >> 9) : 0)) + | |
82 | ((y[2] ? (x >> 10): 0) + | |
83 | (y[1] ? (x >> 11): 0) + | |
05c0805b JW |
84 | (y[0] ? (x >> 12): 0))); |
85 | sign <= xsign ^ ysign; | |
86 | end | |
87 | ||
88 | endmodule | |
89 | ||
90 | module Multiplier( | |
91 | input clk, | |
92e851e1 | 92 | input [12:0] x, y, |
05c0805b | 93 | input xsign, ysign, |
92e851e1 | 94 | output wire [12:0] out, |
05c0805b | 95 | output wire sign, |
fb8d158b | 96 | output wire overflow); |
05c0805b JW |
97 | |
98 | NaiveMultiplier nm(clk, x, y, xsign, ysign, out, sign, overflow); | |
99 | ||
100 | endmodule | |
101 | ||
fb8d158b | 102 | // Yuq. |
05c0805b JW |
103 | module MandelUnit( |
104 | input clk, | |
105 | input [12:0] x, y, | |
106 | input xsign, ysign, | |
92e851e1 | 107 | input [14:0] r, i, |
05c0805b JW |
108 | input rsign, isign, |
109 | input [7:0] ibail, icuriter, | |
110 | output reg [12:0] xout, yout, | |
111 | output reg xsout, ysout, | |
92e851e1 | 112 | output reg [14:0] rout, iout, |
05c0805b JW |
113 | output reg rsout, isout, |
114 | output reg [7:0] obail, ocuriter); | |
115 | ||
6cdf39e2 JW |
116 | wire [13:0] r2, i2; |
117 | wire [14:0] ri, diff; | |
9032b2b5 | 118 | wire [15:0] twocdiff; |
05c0805b | 119 | wire r2sign, i2sign, risign, dsign; |
02d0a24b | 120 | wire [14:0] bigsum; |
6cdf39e2 | 121 | wire bigsum_ovf; |
05c0805b JW |
122 | |
123 | reg [12:0] xd, yd; | |
2afeab21 | 124 | reg ineedbaild; |
05c0805b JW |
125 | reg xsd, ysd; |
126 | reg [7:0] ibaild, curiterd; | |
127 | ||
128 | assign ri[0] = 0; | |
129 | ||
a3a4354b JW |
130 | Multiplier r2m(clk, r[12:0], r[12:0], rsign, rsign, r2[12:0], r2sign, r2[13]); |
131 | Multiplier i2m(clk, i[12:0], i[12:0], isign, isign, i2[12:0], i2sign, i2[13]); | |
132 | Multiplier rim(clk, r[12:0], i[12:0], rsign, isign, ri[13:1], risign, ri[14]); | |
05c0805b | 133 | |
02d0a24b JW |
134 | assign bigsum = r2[13:0] + i2[13:0]; |
135 | wire shnasto = bigsum[14]; | |
136 | wire shnasto2 = // o shi | |
7af9813c JW |
137 | ((r[12] & i[12]) | |
138 | ((r[12] ^ i[12]) & | |
139 | ((r[11] & i[11]) | | |
140 | ((r[11] ^ i[11]) & | |
141 | ((r[10] & i[10]) | | |
142 | ((r[10] ^ i[10]) & | |
143 | ((r[ 9] & i[ 9]) | | |
144 | ((r[ 9] ^ i[ 9]) & | |
145 | ((r[ 8] & i[ 8]) | | |
146 | ((r[ 8] ^ i[ 8]) & | |
147 | ((r[ 7] & i[ 7]) | | |
148 | ((r[ 7] ^ i[ 7]) & | |
149 | ((r[ 6] & i[ 6]) | | |
150 | ((r[ 6] ^ i[ 6]) & | |
151 | ((r[ 5] & i[ 5]) | | |
152 | ((r[ 5] ^ i[ 5]) & | |
153 | ((r[ 4] & i[ 4]) | | |
154 | ((r[ 4] ^ i[ 4]) & | |
155 | ((r[ 3] & i[ 3]) | | |
156 | ((r[ 3] ^ i[ 3]) & | |
157 | ((r[ 2] & i[ 2]) | | |
158 | ((r[ 2] ^ i[ 2]) & | |
159 | ((r[ 1] & i[ 1]) | | |
160 | ((r[ 1] ^ i[ 1]) & | |
161 | (r[ 0] & i[ 0]) | |
162 | )))))))))))))))))))))))); | |
163 | assign bigsum_ovf = shnasto | r2[13] | i2[13]; | |
2afeab21 | 164 | |
9032b2b5 JW |
165 | assign twocdiff = r2 - i2; |
166 | assign diff = twocdiff[15] ? -twocdiff : twocdiff; | |
167 | assign dsign = twocdiff[15]; | |
2afeab21 JW |
168 | |
169 | wire [15:0] twocrout = xd - diff; | |
170 | wire [15:0] twociout = yd - ri; | |
05c0805b JW |
171 | |
172 | always @ (posedge clk) | |
173 | begin | |
174 | xd <= x; | |
175 | yd <= y; | |
176 | xsd <= xsign; | |
177 | ysd <= ysign; | |
178 | xout <= xd; | |
179 | yout <= yd; | |
180 | xsout <= xsd; | |
181 | ysout <= ysd; | |
182 | ibaild <= ibail; | |
183 | curiterd <= icuriter; | |
2afeab21 | 184 | ineedbaild <= r[13] | r[14] | i[13] | i[14]; |
05c0805b | 185 | |
2afeab21 | 186 | // r^2 - i^2 + x |
05c0805b | 187 | if (xsd ^ dsign) begin |
2afeab21 JW |
188 | if (twocrout[15]) begin // diff > xd |
189 | rout <= -twocrout; | |
05c0805b JW |
190 | rsout <= dsign; |
191 | end else begin | |
2afeab21 | 192 | rout <= twocrout; |
05c0805b JW |
193 | rsout <= xsd; |
194 | end | |
195 | end else begin | |
196 | rout <= diff + xd; | |
2afeab21 | 197 | rsout <= xsd; // xsd == dsign |
05c0805b JW |
198 | end |
199 | ||
2afeab21 | 200 | // 2 * r * i + y |
05c0805b | 201 | if (ysd ^ risign) begin |
2afeab21 JW |
202 | if (twociout[15]) begin // ri > yd |
203 | iout <= -twociout; | |
05c0805b JW |
204 | isout <= risign; |
205 | end else begin | |
2afeab21 | 206 | iout <= twociout; |
05c0805b JW |
207 | isout <= ysd; |
208 | end | |
209 | end else begin | |
210 | iout <= ri + yd; | |
211 | isout <= ysd; | |
212 | end | |
213 | ||
214 | // If we haven't bailed out, and we meet any of the bailout conditions, | |
215 | // bail out now. Otherwise, leave the bailout at whatever it was before. | |
2afeab21 | 216 | if ((ibaild == 255) && (bigsum_ovf | ineedbaild)) |
05c0805b JW |
217 | obail <= curiterd; |
218 | else | |
219 | obail <= ibaild; | |
220 | ocuriter <= curiterd + 8'b1; | |
221 | end | |
222 | ||
223 | endmodule | |
224 | ||
225 | module Mandelbrot( | |
226 | input mclk, | |
227 | input pixclk, | |
228 | input [11:0] x, y, | |
92e851e1 | 229 | input [13:0] xofs, yofs, |
05c0805b JW |
230 | input [7:0] colorofs, |
231 | input [2:0] scale, | |
232 | output reg [2:0] red, green, output reg [1:0] blue); | |
281eac32 | 233 | |
534b3903 | 234 | `define MAXOUTN 11 |
05c0805b JW |
235 | |
236 | wire [12:0] rx, ry; | |
237 | wire [13:0] nx, ny; | |
238 | wire rxsign, rysign; | |
239 | ||
240 | assign nx = x + xofs; | |
241 | assign ny = y + yofs; | |
242 | assign rx = (nx[13] ? -nx[12:0] : nx[12:0]) << scale; | |
243 | assign rxsign = nx[13]; | |
244 | assign ry = (ny[13] ? -ny[12:0] : ny[12:0]) << scale; | |
245 | assign rysign = ny[13]; | |
05c0805b | 246 | |
281eac32 JW |
247 | wire [14:0] mr[`MAXOUTN:0], mi[`MAXOUTN:0]; |
248 | wire mrs[`MAXOUTN:0], mis[`MAXOUTN:0]; | |
249 | wire [7:0] mb[`MAXOUTN:0]; | |
250 | wire [12:0] xprop[`MAXOUTN:0], yprop[`MAXOUTN:0]; | |
251 | wire xsprop[`MAXOUTN:0], ysprop[`MAXOUTN:0]; | |
252 | wire [7:0] curiter[`MAXOUTN:0]; | |
05c0805b | 253 | |
79af494a JW |
254 | reg [14:0] initx, inity, initr, initi; |
255 | reg [7:0] initci, initb; | |
256 | reg initxs, initys, initrs, initis; | |
05c0805b | 257 | |
534b3903 JW |
258 | // Values after the number of iterations denoted by the subscript. |
259 | reg [14:0] stagex [2:1], stagey [2:1], stager [2:1], stagei [2:1]; | |
260 | reg [7:0] stageci [2:1], stageb [2:1]; | |
261 | reg stagexs [2:1], stageys [2:1], stagers [2:1], stageis [2:1]; | |
05c0805b | 262 | |
534b3903 | 263 | reg [2:0] state = 3'b001; // One-hot encoded state. |
05c0805b | 264 | |
79af494a JW |
265 | // States are advanced one from what they should be, so that they'll |
266 | // get there on the _next_ mclk. | |
267 | always @(posedge mclk) | |
268 | begin | |
269 | initx <= (state[2]) ? rx : | |
270 | (state[0]) ? stagex[1] : | |
271 | (state[1]) ? stagex[2] : 0; | |
272 | inity <= (state[2]) ? ry : | |
273 | (state[0]) ? stagey[1] : | |
274 | (state[1]) ? stagey[2] : 0; | |
275 | initr <= (state[2]) ? rx : | |
276 | (state[0]) ? stager[1] : | |
277 | (state[1]) ? stager[2] : 0; | |
278 | initi <= (state[2]) ? ry : | |
279 | (state[0]) ? stagei[1] : | |
280 | (state[1]) ? stagei[2] : 0; | |
281 | initxs <= (state[2]) ? rxsign : | |
282 | (state[0]) ? stagexs[1] : | |
283 | (state[1]) ? stagexs[2] : 0; | |
284 | initys <= (state[2]) ? rysign : | |
285 | (state[0]) ? stageys[1] : | |
286 | (state[1]) ? stageys[2] : 0; | |
287 | initrs <= (state[2]) ? rxsign : | |
288 | (state[0]) ? stagers[1] : | |
289 | (state[1]) ? stagers[2] : 0; | |
290 | initis <= (state[2]) ? rysign : | |
291 | (state[0]) ? stageis[1] : | |
292 | (state[1]) ? stageis[2] : 0; | |
293 | initb <= (state[2]) ? 8'b11111111 : | |
294 | (state[0]) ? stageb[1] : | |
295 | (state[1]) ? stageb[2] : 0; | |
296 | initci <= (state[2]) ? 8'b00000000 : | |
297 | (state[0]) ? stageci[1] : | |
298 | (state[1]) ? stageci[2] : 0; | |
299 | end | |
05c0805b JW |
300 | |
301 | reg [7:0] out; | |
251788d8 JW |
302 | |
303 | // We detect when the state should be poked by a high negedge followed | |
fb8d158b JW |
304 | // by a high posedge -- if that happens, then we're guaranteed that the |
305 | // state following the current state will be 3'b100. | |
251788d8 | 306 | reg lastneg; |
265061f2 | 307 | always @(negedge mclk) |
251788d8 | 308 | lastneg <= pixclk; |
05c0805b JW |
309 | |
310 | always @(posedge mclk) | |
311 | begin | |
251788d8 JW |
312 | if (lastneg && pixclk) // If a pixclk has happened, the state should be reset. |
313 | state <= 3'b100; | |
314 | else // Otherwise, just poke it forward. | |
a3a4354b JW |
315 | case(state) |
316 | 3'b001: state <= 3'b010; | |
317 | 3'b010: state <= 3'b100; | |
318 | 3'b100: state <= 3'b001; | |
319 | endcase | |
251788d8 | 320 | |
534b3903 JW |
321 | // Data output handling |
322 | if (state[0]) begin | |
05c0805b | 323 | {red, green, blue} <= {out[0],out[3],out[6],out[1],out[4],out[7],out[2],out[5]}; |
05c0805b | 324 | end |
3068fa61 | 325 | if (state[1]) begin |
534b3903 JW |
326 | out <= ~mb[`MAXOUTN] + colorofs; |
327 | end | |
328 | ||
3068fa61 | 329 | if (state[0]) begin // PnR0 in, PnR2 out |
534b3903 JW |
330 | stagex[2] <= xprop[`MAXOUTN]; |
331 | stagey[2] <= yprop[`MAXOUTN]; | |
332 | stager[2] <= mr[`MAXOUTN]; | |
333 | stagei[2] <= mi[`MAXOUTN]; | |
334 | stagexs[2] <= xsprop[`MAXOUTN]; | |
335 | stageys[2] <= ysprop[`MAXOUTN]; | |
336 | stagers[2] <= mrs[`MAXOUTN]; | |
337 | stageis[2] <= mis[`MAXOUTN]; | |
338 | stageb[2] <= mb[`MAXOUTN]; | |
339 | stageci[2] <= curiter[`MAXOUTN]; | |
340 | end | |
341 | ||
3068fa61 | 342 | if (state[2]) begin // PnR2 in, PnR1 out |
534b3903 JW |
343 | stagex[1] <= xprop[`MAXOUTN]; |
344 | stagey[1] <= yprop[`MAXOUTN]; | |
345 | stager[1] <= mr[`MAXOUTN]; | |
346 | stagei[1] <= mi[`MAXOUTN]; | |
347 | stagexs[1] <= xsprop[`MAXOUTN]; | |
348 | stageys[1] <= ysprop[`MAXOUTN]; | |
349 | stagers[1] <= mrs[`MAXOUTN]; | |
350 | stageis[1] <= mis[`MAXOUTN]; | |
351 | stageb[1] <= mb[`MAXOUTN]; | |
352 | stageci[1] <= curiter[`MAXOUTN]; | |
353 | end | |
05c0805b JW |
354 | end |
355 | ||
356 | MandelUnit mu0( | |
357 | mclk, | |
358 | initx, inity, initxs, initys, | |
359 | initr, initi, initrs, initis, | |
360 | initb, initci, | |
361 | xprop[0], yprop[0], xsprop[0], ysprop[0], | |
362 | mr[0], mi[0], mrs[0], mis[0], | |
363 | mb[0], curiter[0]); | |
e03ccef9 JW |
364 | |
365 | `define MAKE_UNIT(name, num) \ | |
366 | MandelUnit name(mclk, \ | |
367 | xprop[(num)], yprop[(num)], xsprop[(num)], ysprop[(num)], mr[(num)], mi[(num)], mrs[(num)], mis[(num)], mb[(num)], curiter[(num)], \ | |
368 | xprop[(num)+1], yprop[(num)+1], xsprop[(num)+1], ysprop[(num)+1], mr[(num)+1], mi[(num)+1], mrs[(num)+1], mis[(num)+1], mb[(num)+1], curiter[(num)+1]) | |
05c0805b | 369 | |
e03ccef9 JW |
370 | `MAKE_UNIT(mu1, 0); |
371 | `MAKE_UNIT(mu2, 1); | |
372 | `MAKE_UNIT(mu3, 2); | |
373 | `MAKE_UNIT(mu4, 3); | |
374 | `MAKE_UNIT(mu5, 4); | |
375 | `MAKE_UNIT(mu6, 5); | |
376 | `MAKE_UNIT(mu7, 6); | |
377 | `MAKE_UNIT(mu8, 7); | |
378 | `MAKE_UNIT(mu9, 8); | |
379 | `MAKE_UNIT(mua, 9); | |
380 | `MAKE_UNIT(mub, 10); | |
05c0805b JW |
381 | endmodule |
382 | ||
383 | module Logo( | |
384 | input pixclk, | |
385 | input [11:0] x, y, | |
386 | output wire enb, | |
387 | output wire [2:0] red, green, output wire [1:0] blue); | |
388 | ||
389 | reg [1:0] logo[8191:0]; | |
390 | initial $readmemb("logo.readmemb", logo); | |
391 | ||
392 | assign enb = (x < 96) && (y < 64); | |
393 | wire [12:0] addr = {y[5:0], x[6:0]}; | |
394 | wire [1:0] data = logo[addr]; | |
395 | assign {red, green, blue} = | |
396 | (data == 2'b00) ? 8'b00000000 : | |
397 | ((data == 2'b01) ? 8'b00011100 : | |
398 | ((data == 2'b10) ? 8'b11100000 : | |
399 | 8'b11111111)); | |
400 | endmodule | |
401 | ||
402 | module MandelTop( | |
403 | input gclk, output wire dcmok, | |
404 | output wire vs, hs, | |
405 | output wire [2:0] red, green, output [1:0] blue, | |
406 | input left, right, up, down, rst, cycle, logooff, | |
407 | input [2:0] scale); | |
408 | ||
11cc2d9b | 409 | wire pixclk, mclk, clk; |
534b3903 | 410 | wire dcm1ok, dcm2ok; |
c3ed4329 | 411 | assign dcmok = dcm1ok && dcm2ok; |
265061f2 | 412 | |
e03ccef9 | 413 | IBUFG iclkbuf(.O(clk), .I(gclk)); |
534b3903 | 414 | |
c3ed4329 JW |
415 | pixDCM dcm( // CLKIN is 50MHz xtal, CLKFX_OUT is 25MHz |
416 | .CLKIN_IN(clk), | |
417 | .CLKFX_OUT(pixclk), | |
418 | .LOCKED_OUT(dcm1ok) | |
419 | ); | |
534b3903 | 420 | |
c3ed4329 JW |
421 | mandelDCM dcm2( |
422 | .CLKIN_IN(clk), | |
423 | .CLKFX_OUT(mclk), | |
424 | .LOCKED_OUT(dcm2ok) | |
425 | ); | |
534b3903 | 426 | |
05c0805b | 427 | wire border; |
05c0805b | 428 | wire [11:0] x, y; |
92e851e1 | 429 | reg [13:0] xofs = -`XRES/2, yofs = -`YRES/2; |
05c0805b JW |
430 | reg [5:0] slowctr = 0; |
431 | reg [7:0] colorcycle = 0; | |
432 | wire [11:0] realx, realy; | |
433 | ||
434 | wire logoenb; | |
435 | wire [2:0] mandelr, mandelg, logor, logog; | |
436 | wire [1:0] mandelb, logob; | |
437 | ||
05c0805b | 438 | SyncGen sync(pixclk, vs, hs, x, y, realx, realy, border); |
534b3903 | 439 | Mandelbrot mandel(mclk, pixclk, x, y, xofs, yofs, cycle ? colorcycle : 0, scale, mandelr, mandelg, mandelb); |
05c0805b JW |
440 | Logo logo(pixclk, realx, realy, logoenb, logor, logog, logob); |
441 | ||
442 | assign {red,green,blue} = | |
443 | border ? 8'b00000000 : | |
444 | (!logooff && logoenb) ? {logor, logog, logob} : {mandelr, mandelg, mandelb}; | |
445 | ||
446 | always @(posedge vs) | |
447 | begin | |
448 | if (rst) | |
449 | begin | |
450 | xofs <= -`XRES/2; | |
451 | yofs <= -`YRES/2; | |
452 | colorcycle <= 0; | |
453 | end else begin | |
454 | if (up) yofs <= yofs + 1; | |
455 | else if (down) yofs <= yofs - 1; | |
456 | ||
457 | if (left) xofs <= xofs + 1; | |
458 | else if (right) xofs <= xofs - 1; | |
459 | ||
460 | if (slowctr == 0) | |
461 | colorcycle <= colorcycle + 1; | |
462 | end | |
463 | ||
464 | if (slowctr == 12) | |
465 | slowctr <= 0; | |
466 | else | |
467 | slowctr <= slowctr + 1; | |
468 | end | |
469 | endmodule |