3 * by Joshua Wise and Chris Lu
5 * An implementation of a pipelined algorithm to calculate the Mandelbrot set
6 * in real time on an FPGA.
16 output reg [11:0] xout = `WHIRRRRR, yout = 0,
17 output wire [11:0] xoutreal, youtreal,
20 reg [11:0] x = 0, y = 0; // Used for generating border and timing.
24 parameter XFPORCH = 16;
26 parameter XBPORCH = 48;
28 parameter YFPORCH = 10;
30 parameter YBPORCH = 29;
32 always @(posedge pixclk)
34 if (x >= (`XRES + XFPORCH + XSYNC + XBPORCH))
36 if (y >= (`YRES + YFPORCH + YSYNC + YBPORCH))
44 if (xout >= (`XRES + XFPORCH + XSYNC + XBPORCH))
46 if (yout >= (`YRES + YFPORCH + YSYNC + YBPORCH))
53 hs <= (x >= (`XRES + XFPORCH)) && (x < (`XRES + XFPORCH + XSYNC));
54 vs <= (y >= (`YRES + YFPORCH)) && (y < (`YRES + YFPORCH + YSYNC));
55 border <= (x > `XRES) || (y > `YRES);
61 module NaiveMultiplier(
65 output reg [12:0] out,
72 (((y[12] ? (x ) : 0) +
73 (y[11] ? (x[12:1]) : 0) +
74 (y[10] ? (x[12:2]) : 0)) +
75 (((y[9] ? (x[12:3]) : 0) +
76 (y[8] ? (x[12:4]) : 0)) +
77 ((y[7] ? (x[12:5]) : 0) +
78 (y[6] ? (x[12:6]) : 0))))+
79 (((y[5] ? (x[12:7]) : 0) +
80 (y[4] ? (x[12:8]) : 0) +
81 (y[3] ? (x[12:9]) : 0)) +
82 ((y[2] ? (x[12:10]): 0) +
83 (y[1] ? (x[12:11]): 0) +
84 (y[0] ? (x[12]): 0)));
85 sign <= xsign ^ ysign;
94 output wire [12:0] out,
96 output wire overflow);
98 NaiveMultiplier nm(clk, x, y, xsign, ysign, out, sign, overflow);
109 input [7:0] ibail, icuriter,
110 output reg [12:0] xout, yout,
111 output reg xsout, ysout,
112 output reg [14:0] rout, iout,
113 output reg rsout, isout,
114 output reg [7:0] obail, ocuriter);
117 wire [14:0] ri, diff;
118 wire [15:0] twocdiff;
119 wire r2sign, i2sign, risign, dsign;
126 reg [7:0] ibaild, curiterd;
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]);
134 assign bigsum = r2[13:0] + i2[13:0];
135 wire shnasto = bigsum[14];
136 wire shnasto2 = // o shi
164 ))))))))))))))))))))))))));
165 assign bigsum_ovf = shnasto;
167 assign twocdiff = r2 - i2;
168 assign diff = twocdiff[15] ? -twocdiff : twocdiff;
169 assign dsign = twocdiff[15];
171 wire [15:0] twocrout = xd - diff;
172 wire [15:0] twociout = yd - ri;
174 always @ (posedge clk)
185 curiterd <= icuriter;
186 ineedbaild <= r[13] | r[14] | i[13] | i[14];
189 if (xsd ^ dsign) begin
190 if (twocrout[15]) begin // diff > xd
199 rsout <= xsd; // xsd == dsign
203 if (ysd ^ risign) begin
204 if (twociout[15]) begin // ri > yd
216 // If we haven't bailed out, and we meet any of the bailout conditions,
217 // bail out now. Otherwise, leave the bailout at whatever it was before.
218 if ((ibaild == 255) && (bigsum_ovf | ineedbaild))
222 ocuriter <= curiterd + 8'b1;
231 input [13:0] xofs, yofs,
232 input [7:0] colorofs,
234 output reg [2:0] red, green, output reg [1:0] blue);
242 assign nx = x + xofs;
243 assign ny = y + yofs;
244 assign rx = (nx[13] ? -nx[12:0] : nx[12:0]) << scale;
245 assign rxsign = nx[13];
246 assign ry = (ny[13] ? -ny[12:0] : ny[12:0]) << scale;
247 assign rysign = ny[13];
249 wire [14:0] mr[`MAXOUTN:0], mi[`MAXOUTN:0];
250 wire mrs[`MAXOUTN:0], mis[`MAXOUTN:0];
251 wire [7:0] mb[`MAXOUTN:0];
252 wire [12:0] xprop[`MAXOUTN:0], yprop[`MAXOUTN:0];
253 wire xsprop[`MAXOUTN:0], ysprop[`MAXOUTN:0];
254 wire [7:0] curiter[`MAXOUTN:0];
256 reg [14:0] initx, inity, initr, initi;
257 reg [7:0] initci, initb;
258 reg initxs, initys, initrs, initis;
260 // Values after the number of iterations denoted by the subscript.
261 reg [14:0] stagex [2:1], stagey [2:1], stager [2:1], stagei [2:1];
262 reg [7:0] stageci [2:1], stageb [2:1];
263 reg stagexs [2:1], stageys [2:1], stagers [2:1], stageis [2:1];
265 reg [2:0] state = 3'b001; // One-hot encoded state.
267 // States are advanced one from what they should be, so that they'll
268 // get there on the _next_ mclk.
269 always @(posedge mclk)
271 initx <= (state[2]) ? rx :
272 (state[0]) ? stagex[1] :
273 (state[1]) ? stagex[2] : 0;
274 inity <= (state[2]) ? ry :
275 (state[0]) ? stagey[1] :
276 (state[1]) ? stagey[2] : 0;
277 initr <= (state[2]) ? rx :
278 (state[0]) ? stager[1] :
279 (state[1]) ? stager[2] : 0;
280 initi <= (state[2]) ? ry :
281 (state[0]) ? stagei[1] :
282 (state[1]) ? stagei[2] : 0;
283 initxs <= (state[2]) ? rxsign :
284 (state[0]) ? stagexs[1] :
285 (state[1]) ? stagexs[2] : 0;
286 initys <= (state[2]) ? rysign :
287 (state[0]) ? stageys[1] :
288 (state[1]) ? stageys[2] : 0;
289 initrs <= (state[2]) ? rxsign :
290 (state[0]) ? stagers[1] :
291 (state[1]) ? stagers[2] : 0;
292 initis <= (state[2]) ? rysign :
293 (state[0]) ? stageis[1] :
294 (state[1]) ? stageis[2] : 0;
295 initb <= (state[2]) ? 8'b11111111 :
296 (state[0]) ? stageb[1] :
297 (state[1]) ? stageb[2] : 0;
298 initci <= (state[2]) ? 8'b00000000 :
299 (state[0]) ? stageci[1] :
300 (state[1]) ? stageci[2] : 0;
305 // We detect when the state should be poked by a high negedge followed
306 // by a high posedge -- if that happens, then we're guaranteed that the
307 // state following the current state will be 3'b100.
309 always @(negedge mclk)
312 always @(posedge mclk)
314 if (lastneg && pixclk) // If a pixclk has happened, the state should be reset.
316 else // Otherwise, just poke it forward.
318 3'b001: state <= 3'b010;
319 3'b010: state <= 3'b100;
320 3'b100: state <= 3'b001;
323 // Data output handling
325 {red, green, blue} <= {out[0],out[3],out[6],out[1],out[4],out[7],out[2],out[5]};
328 out <= ~mb[`MAXOUTN] + colorofs;
331 if (state[0]) begin // PnR0 in, PnR2 out
332 stagex[2] <= xprop[`MAXOUTN];
333 stagey[2] <= yprop[`MAXOUTN];
334 stager[2] <= mr[`MAXOUTN];
335 stagei[2] <= mi[`MAXOUTN];
336 stagexs[2] <= xsprop[`MAXOUTN];
337 stageys[2] <= ysprop[`MAXOUTN];
338 stagers[2] <= mrs[`MAXOUTN];
339 stageis[2] <= mis[`MAXOUTN];
340 stageb[2] <= mb[`MAXOUTN];
341 stageci[2] <= curiter[`MAXOUTN];
344 if (state[2]) begin // PnR2 in, PnR1 out
345 stagex[1] <= xprop[`MAXOUTN];
346 stagey[1] <= yprop[`MAXOUTN];
347 stager[1] <= mr[`MAXOUTN];
348 stagei[1] <= mi[`MAXOUTN];
349 stagexs[1] <= xsprop[`MAXOUTN];
350 stageys[1] <= ysprop[`MAXOUTN];
351 stagers[1] <= mrs[`MAXOUTN];
352 stageis[1] <= mis[`MAXOUTN];
353 stageb[1] <= mb[`MAXOUTN];
354 stageci[1] <= curiter[`MAXOUTN];
360 initx, inity, initxs, initys,
361 initr, initi, initrs, initis,
363 xprop[0], yprop[0], xsprop[0], ysprop[0],
364 mr[0], mi[0], mrs[0], mis[0],
367 `define MAKE_UNIT(name, num) \
368 MandelUnit name(mclk, \
369 xprop[(num)], yprop[(num)], xsprop[(num)], ysprop[(num)], mr[(num)], mi[(num)], mrs[(num)], mis[(num)], mb[(num)], curiter[(num)], \
370 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])
389 output wire [2:0] red, green, output wire [1:0] blue);
391 reg [1:0] logo[8191:0];
392 initial $readmemb("logo.readmemb", logo);
394 assign enb = (x < 96) && (y < 64);
395 wire [12:0] addr = {y[5:0], x[6:0]};
396 wire [1:0] data = logo[addr];
397 assign {red, green, blue} =
398 (data == 2'b00) ? 8'b00000000 :
399 ((data == 2'b01) ? 8'b00011100 :
400 ((data == 2'b10) ? 8'b11100000 :
405 input gclk, output wire dcmok,
407 output wire [2:0] red, green, output [1:0] blue,
408 input left, right, up, down, rst, cycle, logooff,
411 wire pixclk, mclk, clk;
413 assign dcmok = dcm1ok && dcm2ok;
415 IBUFG iclkbuf(.O(clk), .I(gclk));
417 pixDCM dcm( // CLKIN is 50MHz xtal, CLKFX_OUT is 25MHz
431 reg [13:0] xofs = -`XRES/2, yofs = -`YRES/2;
432 reg [5:0] slowctr = 0;
433 reg [7:0] colorcycle = 0;
434 wire [11:0] realx, realy;
437 wire [2:0] mandelr, mandelg, logor, logog;
438 wire [1:0] mandelb, logob;
440 SyncGen sync(pixclk, vs, hs, x, y, realx, realy, border);
441 Mandelbrot mandel(mclk, pixclk, x, y, xofs, yofs, cycle ? colorcycle : 0, scale, mandelr, mandelg, mandelb);
442 Logo logo(pixclk, realx, realy, logoenb, logor, logog, logob);
444 assign {red,green,blue} =
445 border ? 8'b00000000 :
446 (!logooff && logoenb) ? {logor, logog, logob} : {mandelr, mandelg, mandelb};
456 if (up) yofs <= yofs + 1;
457 else if (down) yofs <= yofs - 1;
459 if (left) xofs <= xofs + 1;
460 else if (right) xofs <= xofs - 1;
463 colorcycle <= colorcycle + 1;
469 slowctr <= slowctr + 1;