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.
9 /* verilator lint_off WIDTH */
20 output reg [11:0] xout = `WHIRRRRR, yout = 0,
21 output wire [11:0] xoutreal, youtreal,
24 reg [11:0] x = 0, y = 0; // Used for generating border and timing.
28 parameter XFPORCH = 16;
30 parameter XBPORCH = 48;
32 parameter YFPORCH = 10;
34 parameter YBPORCH = 29;
36 always @(posedge pixclk)
38 if (x >= (`XRES + XFPORCH + XSYNC + XBPORCH))
40 if (y >= (`YRES + YFPORCH + YSYNC + YBPORCH))
48 if (xout >= (`XRES + XFPORCH + XSYNC + XBPORCH))
50 if (yout >= (`YRES + YFPORCH + YSYNC + YBPORCH))
57 hs <= (x >= (`XRES + XFPORCH)) && (x < (`XRES + XFPORCH + XSYNC));
58 vs <= (y >= (`YRES + YFPORCH)) && (y < (`YRES + YFPORCH + YSYNC));
59 border <= (x > `XRES) || (y > `YRES);
65 module NaiveMultiplier(
67 input [`TOPBIT:0] x, y,
69 output reg [`TOPBIT:0] out,
76 (((y[12] ? (x ) : 0) +
77 (y[11] ? (x[`TOPBIT:1]) : 0) +
78 (y[10] ? (x[`TOPBIT:2]) : 0)) +
79 (((y[9] ? (x[`TOPBIT:3]) : 0) +
80 (y[8] ? (x[`TOPBIT:4]) : 0)) +
81 ((y[7] ? (x[`TOPBIT:5]) : 0) +
82 (y[6] ? (x[`TOPBIT:6]) : 0))))+
83 (((y[5] ? (x[`TOPBIT:7]) : 0) +
84 (y[4] ? (x[`TOPBIT:8]) : 0) +
85 (y[3] ? (x[`TOPBIT:9]) : 0)) +
86 ((y[2] ? (x[`TOPBIT:10]): 0) +
87 (y[1] ? (x[`TOPBIT:11]): 0) +
88 (y[0] ? (x[`TOPBIT]): 0)));
89 sign <= xsign ^ ysign;
96 input [`TOPBIT:0] x, y,
98 output wire [`TOPBIT:0] out,
100 output wire overflow);
102 NaiveMultiplier nm(clk, x, y, xsign, ysign, out, sign, overflow);
109 input [`TOPBIT:0] x, y,
111 input [`TOPBIT+2:0] r, i,
113 input [7:0] ibail, icuriter,
114 output reg [`TOPBIT:0] xout, yout,
115 output reg xsout, ysout,
116 output reg [`TOPBIT+2:0] rout, iout,
117 output reg rsout, isout,
118 output reg [7:0] obail, ocuriter);
120 wire [`TOPBIT+1:0] r2, i2;
121 wire [`TOPBIT+2:0] ri, diff;
122 wire [`TOPBIT+3:0] twocdiff;
123 wire r2sign, i2sign, risign, dsign;
124 wire [`TOPBIT+2:0] bigsum;
127 reg [`TOPBIT:0] xd, yd;
130 reg [7:0] ibaild, curiterd;
134 Multiplier r2m(clk, r[`TOPBIT:0], r[`TOPBIT:0], rsign, rsign, r2[`TOPBIT:0], r2sign, r2[`TOPBIT+1]);
135 Multiplier i2m(clk, i[`TOPBIT:0], i[`TOPBIT:0], isign, isign, i2[`TOPBIT:0], i2sign, i2[`TOPBIT+1]);
136 Multiplier rim(clk, r[`TOPBIT:0], i[`TOPBIT:0], rsign, isign, ri[`TOPBIT+1:1], risign, ri[`TOPBIT+2]);
138 assign bigsum = r2[`TOPBIT+1:0] + i2[`TOPBIT+1:0];
139 assign bigsum_ovf = bigsum[`TOPBIT+2];
141 assign twocdiff = r2 - i2;
142 assign diff = twocdiff[`TOPBIT+3] ? -twocdiff : twocdiff;
143 assign dsign = twocdiff[`TOPBIT+3];
145 wire [`TOPBIT+3:0] twocrout = xd - diff;
146 wire [`TOPBIT+3:0] twociout = yd - ri;
148 always @ (posedge clk)
159 curiterd <= icuriter;
160 ineedbaild <= r[`TOPBIT+1] | r[`TOPBIT+2] | i[`TOPBIT+1] | i[`TOPBIT+2];
163 if (xsd ^ dsign) begin
164 if (twocrout[`TOPBIT+3]) begin // diff > xd
173 rsout <= xsd; // xsd == dsign
177 if (ysd ^ risign) begin
178 if (twociout[`TOPBIT+3]) begin // ri > yd
190 // If we haven't bailed out, and we meet any of the bailout conditions,
191 // bail out now. Otherwise, leave the bailout at whatever it was before.
192 if ((ibaild == 255) && (bigsum_ovf | ineedbaild))
196 ocuriter <= curiterd + 8'b1;
205 input [`TOPBIT+1:0] xofs, yofs,
206 input [7:0] colorofs,
208 output reg [2:0] red, green, output reg [1:0] blue);
212 wire [`TOPBIT:0] rx, ry;
213 wire [`TOPBIT+1:0] nx, ny;
216 assign nx = {2'b0,x} + {2'b0,xofs};
217 assign ny = {2'b0,y} + {2'b0,yofs};
218 assign rx = (nx[`TOPBIT+1] ? -nx[`TOPBIT:0] : nx[`TOPBIT:0]) << scale;
219 assign rxsign = nx[`TOPBIT+1];
220 assign ry = (ny[`TOPBIT+1] ? -ny[`TOPBIT:0] : ny[`TOPBIT:0]) << scale;
221 assign rysign = ny[`TOPBIT+1];
223 wire [`TOPBIT+2:0] mr[`MAXOUTN:0], mi[`MAXOUTN:0];
224 wire mrs[`MAXOUTN:0], mis[`MAXOUTN:0];
225 wire [7:0] mb[`MAXOUTN:0];
226 wire [`TOPBIT:0] xprop[`MAXOUTN:0], yprop[`MAXOUTN:0];
227 wire xsprop[`MAXOUTN:0], ysprop[`MAXOUTN:0];
228 wire [7:0] curiter[`MAXOUTN:0];
230 reg [`TOPBIT:0] initx, inity;
231 reg [`TOPBIT+2:0] initr, initi;
232 reg [7:0] initci, initb;
233 reg initxs, initys, initrs, initis;
235 // Values after the number of iterations denoted by the subscript.
236 reg [`TOPBIT:0] stagex [2:1], stagey [2:1];
237 reg [`TOPBIT+2:0] stager [2:1], stagei [2:1];
238 reg [7:0] stageci [2:1], stageb [2:1];
239 reg stagexs [2:1], stageys [2:1], stagers [2:1], stageis [2:1];
241 reg [2:0] state = 3'b001; // One-hot encoded state.
243 // States are advanced one from what they should be, so that they'll
244 // get there on the _next_ mclk.
245 always @(posedge mclk)
247 initx <= (state[2]) ? rx :
248 (state[0]) ? stagex[1] :
249 (state[1]) ? stagex[2] : 0;
250 inity <= (state[2]) ? ry :
251 (state[0]) ? stagey[1] :
252 (state[1]) ? stagey[2] : 0;
253 initr <= (state[2]) ? {2'b0,rx} :
254 (state[0]) ? stager[1] :
255 (state[1]) ? stager[2] : 0;
256 initi <= (state[2]) ? {2'b0,ry} :
257 (state[0]) ? stagei[1] :
258 (state[1]) ? stagei[2] : 0;
259 initxs <= (state[2]) ? rxsign :
260 (state[0]) ? stagexs[1] :
261 (state[1]) ? stagexs[2] : 0;
262 initys <= (state[2]) ? rysign :
263 (state[0]) ? stageys[1] :
264 (state[1]) ? stageys[2] : 0;
265 initrs <= (state[2]) ? rxsign :
266 (state[0]) ? stagers[1] :
267 (state[1]) ? stagers[2] : 0;
268 initis <= (state[2]) ? rysign :
269 (state[0]) ? stageis[1] :
270 (state[1]) ? stageis[2] : 0;
271 initb <= (state[2]) ? 8'b11111111 :
272 (state[0]) ? stageb[1] :
273 (state[1]) ? stageb[2] : 0;
274 initci <= (state[2]) ? 8'b00000000 :
275 (state[0]) ? stageci[1] :
276 (state[1]) ? stageci[2] : 0;
281 // We detect when the state should be poked by a high negedge followed
282 // by a high posedge -- if that happens, then we're guaranteed that the
283 // state following the current state will be 3'b100.
285 always @(negedge mclk)
288 always @(posedge mclk)
290 if (lastneg && pixclk) // If a pixclk has happened, the state should be reset.
292 else // Otherwise, just poke it forward.
294 3'b001: state <= 3'b010;
295 3'b010: state <= 3'b100;
296 3'b100: state <= 3'b001;
298 default: begin $display("invalid state"); $finish; end
302 // Data output handling
304 {red, green, blue} <= {out[0],out[3],out[6],out[1],out[4],out[7],out[2],out[5]};
307 out <= ~mb[`MAXOUTN] + colorofs;
310 if (state[0]) begin // PnR0 in, PnR2 out
311 stagex[2] <= xprop[`MAXOUTN];
312 stagey[2] <= yprop[`MAXOUTN];
313 stager[2] <= mr[`MAXOUTN];
314 stagei[2] <= mi[`MAXOUTN];
315 stagexs[2] <= xsprop[`MAXOUTN];
316 stageys[2] <= ysprop[`MAXOUTN];
317 stagers[2] <= mrs[`MAXOUTN];
318 stageis[2] <= mis[`MAXOUTN];
319 stageb[2] <= mb[`MAXOUTN];
320 stageci[2] <= curiter[`MAXOUTN];
323 if (state[2]) begin // PnR2 in, PnR1 out
324 stagex[1] <= xprop[`MAXOUTN];
325 stagey[1] <= yprop[`MAXOUTN];
326 stager[1] <= mr[`MAXOUTN];
327 stagei[1] <= mi[`MAXOUTN];
328 stagexs[1] <= xsprop[`MAXOUTN];
329 stageys[1] <= ysprop[`MAXOUTN];
330 stagers[1] <= mrs[`MAXOUTN];
331 stageis[1] <= mis[`MAXOUTN];
332 stageb[1] <= mb[`MAXOUTN];
333 stageci[1] <= curiter[`MAXOUTN];
339 initx, inity, initxs, initys,
340 initr, initi, initrs, initis,
342 xprop[0], yprop[0], xsprop[0], ysprop[0],
343 mr[0], mi[0], mrs[0], mis[0],
346 `define MAKE_UNIT(name, num) \
347 MandelUnit name(mclk, \
348 xprop[(num)], yprop[(num)], xsprop[(num)], ysprop[(num)], mr[(num)], mi[(num)], mrs[(num)], mis[(num)], mb[(num)], curiter[(num)], \
349 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])
378 output wire [2:0] red, green, output wire [1:0] blue);
380 reg [1:0] logo[8191:0];
381 initial $readmemb("logo.readmemb", logo);
383 assign enb = (x < 96) && (y < 64);
384 wire [12:0] addr = {y[5:0], x[6:0]};
385 wire [1:0] data = logo[addr];
386 assign {red, green, blue} =
387 (data == 2'b00) ? 8'b00000000 :
388 ((data == 2'b01) ? 8'b00011100 :
389 ((data == 2'b10) ? 8'b11100000 :
397 input gclk, output wire dcmok,
400 output wire [2:0] red, green, output [1:0] blue,
401 input left, right, up, down, rst, cycle, logooff,
406 wire pixclk, mclk, clk;
408 assign dcmok = dcm1ok && dcm2ok;
410 IBUFG iclkbuf(.O(clk), .I(gclk));
412 pixDCM dcm( // CLKIN is 50MHz xtal, CLKFX_OUT is 25MHz
427 reg [`TOPBIT+1:0] xofs = -`XRES/2, yofs = -`YRES/2;
428 reg [5:0] slowctr = 0;
429 reg [7:0] colorcycle = 0;
430 wire [11:0] realx, realy;
433 wire [2:0] mandelr, mandelg, logor, logog;
434 wire [1:0] mandelb, logob;
436 SyncGen sync(pixclk, vs, hs, x, y, realx, realy, border);
437 Mandelbrot mandel(mclk, pixclk, x, y, xofs, yofs, cycle ? colorcycle : 8'b0, scale, mandelr, mandelg, mandelb);
438 Logo logo(pixclk, realx, realy, logoenb, logor, logog, logob);
440 assign {red,green,blue} =
441 border ? 8'b00000000 :
442 (!logooff && logoenb) ? {logor, logog, logob} : {mandelr, mandelg, mandelb};
452 if (up) yofs <= yofs + 1;
453 else if (down) yofs <= yofs - 1;
455 if (left) xofs <= xofs + 1;
456 else if (right) xofs <= xofs - 1;
459 colorcycle <= colorcycle + 1;
465 slowctr <= slowctr + 1;