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,
67 output reg [1:0] ovf);
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) +
77 (y[7] ? (x >> 5) : 0) +
78 (y[6] ? (x >> 6) : 0)))+
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) +
84 (y[0] ? (x >> 12): 0)));
85 sign <= xsign ^ ysign;
94 output wire [12:0] out,
96 output wire [1:0] overflow);
98 NaiveMultiplier nm(clk, x, y, xsign, ysign, out, sign, overflow);
108 input [7:0] ibail, icuriter,
109 output reg [12:0] xout, yout,
110 output reg xsout, ysout,
111 output reg [14:0] rout, iout,
112 output reg rsout, isout,
113 output reg [7:0] obail, ocuriter);
115 wire [14:0] r2, i2, ri, diff;
116 wire [15:0] twocdiff;
117 wire r2sign, i2sign, risign, dsign;
119 wire bigsum_ovf, rin_ovf, iin_ovf, throwaway;
124 reg [7:0] ibaild, curiterd;
128 Multiplier r2m(clk, r[12:0], r[12:0], rsign, rsign, r2[12:0], r2sign, r2[14:13]);
129 Multiplier i2m(clk, i[12:0], i[12:0], isign, isign, i2[12:0], i2sign, i2[14:13]);
130 Multiplier rim(clk, r[12:0], i[12:0], rsign, isign, ri[13:1], risign, {throwaway,ri[14]});
132 assign bigsum = r2 + i2;
133 assign bigsum_ovf = bigsum[16] | bigsum[15] | bigsum[14];
136 assign twocdiff = r2 - i2;
137 assign diff = twocdiff[15] ? -twocdiff : twocdiff;
138 assign dsign = twocdiff[15];
140 always @ (posedge clk)
151 curiterd <= icuriter;
155 if (xsd ^ dsign) begin
168 if (ysd ^ risign) begin
181 // If we haven't bailed out, and we meet any of the bailout conditions,
182 // bail out now. Otherwise, leave the bailout at whatever it was before.
183 if ((ibaild == 255) && (bigsum_ovf | rin_ovf | iin_ovf))
187 ocuriter <= curiterd + 8'b1;
196 input [13:0] xofs, yofs,
197 input [7:0] colorofs,
199 output reg [2:0] red, green, output reg [1:0] blue);
207 assign nx = x + xofs;
208 assign ny = y + yofs;
209 assign rx = (nx[13] ? -nx[12:0] : nx[12:0]) << scale;
210 assign rxsign = nx[13];
211 assign ry = (ny[13] ? -ny[12:0] : ny[12:0]) << scale;
212 assign rysign = ny[13];
215 wire [14:0] mr[`MAXOUTN:0], mi[`MAXOUTN:0];
216 wire mrs[`MAXOUTN:0], mis[`MAXOUTN:0];
217 wire [7:0] mb[`MAXOUTN:0];
218 wire [12:0] xprop[`MAXOUTN:0], yprop[`MAXOUTN:0];
219 wire xsprop[`MAXOUTN:0], ysprop[`MAXOUTN:0];
220 wire [7:0] curiter[`MAXOUTN:0];
222 wire [14:0] initx, inity, initr, initi;
223 wire [7:0] initci, initb;
224 wire initxs, initys, initrs, initis;
226 // Values after the number of iterations denoted by the subscript.
227 reg [14:0] stagex [2:1], stagey [2:1], stager [2:1], stagei [2:1];
228 reg [7:0] stageci [2:1], stageb [2:1];
229 reg stagexs [2:1], stageys [2:1], stagers [2:1], stageis [2:1];
231 reg [2:0] state = 3'b001; // One-hot encoded state.
234 // A new value to be loaded comes in, and a value in need of loopback comes out.
236 // A new value in need of loopback comes in, and a completed value comes out.
238 assign initx = state[0] ? rx :
239 state[1] ? stagex[1] :
241 assign inity = state[0] ? ry :
242 state[1] ? stagey[1] :
244 assign initr = state[0] ? rx :
245 state[1] ? stager[1] :
247 assign initi = state[0] ? ry :
248 state[1] ? stagei[1] :
250 assign initxs = state[0] ? rxsign :
251 state[1] ? stagexs[1] :
253 assign initys = state[0] ? rysign :
254 state[1] ? stageys[1] :
256 assign initrs = state[0] ? rxsign :
257 state[1] ? stagers[1] :
259 assign initis = state[0] ? rysign :
260 state[1] ? stageis[1] :
262 assign initb = state[0] ? 8'b11111111 :
263 state[1] ? stageb[1] :
265 assign initci = state[0] ? 8'b00000000 :
266 state[1] ? stageb[1] :
270 reg typethea = 0; // Whether we have typed the A.
271 reg statekick = 0; // State needs to be kicked back to 3'b010 on the next mclk.
273 // This is guaranteed to converge after two pixclks.
274 //always @(negedge mclk)
275 // if (pixclk && !typethea) begin
278 // end else if (typethea) begin // This is the edge of the falling anus.
283 always @(posedge mclk)
285 // Data output handling
287 {red, green, blue} <= {out[0],out[3],out[6],out[1],out[4],out[7],out[2],out[5]};
290 out <= ~mb[`MAXOUTN] + colorofs;
293 if (state[1]) begin // PnR0 in, PnR2 out
294 stagex[2] <= xprop[`MAXOUTN];
295 stagey[2] <= yprop[`MAXOUTN];
296 stager[2] <= mr[`MAXOUTN];
297 stagei[2] <= mi[`MAXOUTN];
298 stagexs[2] <= xsprop[`MAXOUTN];
299 stageys[2] <= ysprop[`MAXOUTN];
300 stagers[2] <= mrs[`MAXOUTN];
301 stageis[2] <= mis[`MAXOUTN];
302 stageb[2] <= mb[`MAXOUTN];
303 stageci[2] <= curiter[`MAXOUTN];
306 if (state[0]) begin // PnR2 in, PnR1 out
307 stagex[1] <= xprop[`MAXOUTN];
308 stagey[1] <= yprop[`MAXOUTN];
309 stager[1] <= mr[`MAXOUTN];
310 stagei[1] <= mi[`MAXOUTN];
311 stagexs[1] <= xsprop[`MAXOUTN];
312 stageys[1] <= ysprop[`MAXOUTN];
313 stagers[1] <= mrs[`MAXOUTN];
314 stageis[1] <= mis[`MAXOUTN];
315 stageb[1] <= mb[`MAXOUTN];
316 stageci[1] <= curiter[`MAXOUTN];
319 if (statekick) // If a pixclk has happened, the state should be reset.
321 else // Otherwise, just poke it forward.
322 state <= {state[1], state[0], state[2]};
327 initx, inity, initxs, initys,
328 initr, initi, initrs, initis,
330 xprop[0], yprop[0], xsprop[0], ysprop[0],
331 mr[0], mi[0], mrs[0], mis[0],
335 xprop[0], yprop[0], xsprop[0], ysprop[0], mr[0], mi[0], mrs[0], mis[0], mb[0], curiter[0],
336 xprop[1], yprop[1], xsprop[1], ysprop[1], mr[1], mi[1], mrs[1], mis[1], mb[1], curiter[1]);
338 xprop[1], yprop[1], xsprop[1], ysprop[1], mr[1], mi[1], mrs[1], mis[1], mb[1], curiter[1],
339 xprop[2], yprop[2], xsprop[2], ysprop[2], mr[2], mi[2], mrs[2], mis[2], mb[2], curiter[2]);
341 xprop[2], yprop[2], xsprop[2], ysprop[2], mr[2], mi[2], mrs[2], mis[2], mb[2], curiter[2],
342 xprop[3], yprop[3], xsprop[3], ysprop[3], mr[3], mi[3], mrs[3], mis[3], mb[3], curiter[3]);
344 xprop[3], yprop[3], xsprop[3], ysprop[3], mr[3], mi[3], mrs[3], mis[3], mb[3], curiter[3],
345 xprop[4], yprop[4], xsprop[4], ysprop[4], mr[4], mi[4], mrs[4], mis[4], mb[4], curiter[4]);
347 xprop[4], yprop[4], xsprop[4], ysprop[4], mr[4], mi[4], mrs[4], mis[4], mb[4], curiter[4],
348 xprop[5], yprop[5], xsprop[5], ysprop[5], mr[5], mi[5], mrs[5], mis[5], mb[5], curiter[5]);
350 xprop[5], yprop[5], xsprop[5], ysprop[5], mr[5], mi[5], mrs[5], mis[5], mb[5], curiter[5],
351 xprop[6], yprop[6], xsprop[6], ysprop[6], mr[6], mi[6], mrs[6], mis[6], mb[6], curiter[6]);
353 xprop[6], yprop[6], xsprop[6], ysprop[6], mr[6], mi[6], mrs[6], mis[6], mb[6], curiter[6],
354 xprop[7], yprop[7], xsprop[7], ysprop[7], mr[7], mi[7], mrs[7], mis[7], mb[7], curiter[7]);
356 xprop[7], yprop[7], xsprop[7], ysprop[7], mr[7], mi[7], mrs[7], mis[7], mb[7], curiter[7],
357 xprop[8], yprop[8], xsprop[8], ysprop[8], mr[8], mi[8], mrs[8], mis[8], mb[8], curiter[8]);
359 xprop[8], yprop[8], xsprop[8], ysprop[8], mr[8], mi[8], mrs[8], mis[8], mb[8], curiter[8],
360 xprop[9], yprop[9], xsprop[9], ysprop[9], mr[9], mi[9], mrs[9], mis[9], mb[9], curiter[9]);
362 xprop[9], yprop[9], xsprop[9], ysprop[9], mr[9], mi[9], mrs[9], mis[9], mb[9], curiter[9],
363 xprop[10], yprop[10], xsprop[10], ysprop[10], mr[10], mi[10], mrs[10], mis[10], mb[10], curiter[10]);
365 xprop[10], yprop[10], xsprop[10], ysprop[10], mr[10], mi[10], mrs[10], mis[10], mb[10], curiter[10],
366 xprop[11], yprop[11], xsprop[11], ysprop[11], mr[11], mi[11], mrs[11], mis[11], mb[11], curiter[11]);
374 output wire [2:0] red, green, output wire [1:0] blue);
376 reg [1:0] logo[8191:0];
377 initial $readmemb("logo.readmemb", logo);
379 assign enb = (x < 96) && (y < 64);
380 wire [12:0] addr = {y[5:0], x[6:0]};
381 wire [1:0] data = logo[addr];
382 assign {red, green, blue} =
383 (data == 2'b00) ? 8'b00000000 :
384 ((data == 2'b01) ? 8'b00011100 :
385 ((data == 2'b10) ? 8'b11100000 :
390 input gclk, output wire dcmok,
392 output wire [2:0] red, green, output [1:0] blue,
393 input left, right, up, down, rst, cycle, logooff,
397 wire pixclk, mclk, gclk2, clk;
399 assign dcmok = dcm1ok && dcm2ok;
401 IBUFG typeA(.O(clk), .I(gclk));
403 pixDCM dcm( // CLKIN is 50MHz xtal, CLKFX_OUT is 25MHz
417 reg [13:0] xofs = -`XRES/2, yofs = -`YRES/2;
418 reg [5:0] slowctr = 0;
419 reg [7:0] colorcycle = 0;
420 wire [11:0] realx, realy;
423 wire [2:0] mandelr, mandelg, logor, logog;
424 wire [1:0] mandelb, logob;
428 SyncGen sync(pixclk, vs, hs, x, y, realx, realy, border);
429 Mandelbrot mandel(mclk, pixclk, x, y, xofs, yofs, cycle ? colorcycle : 0, scale, mandelr, mandelg, mandelb);
430 Logo logo(pixclk, realx, realy, logoenb, logor, logog, logob);
432 assign {red,green,blue} =
433 border ? 8'b00000000 :
434 (!logooff && logoenb) ? {logor, logog, logob} : {mandelr, mandelg, mandelb};
444 if (up) yofs <= yofs + 1;
445 else if (down) yofs <= yofs - 1;
447 if (left) xofs <= xofs + 1;
448 else if (right) xofs <= xofs - 1;
451 colorcycle <= colorcycle + 1;
457 slowctr <= slowctr + 1;