]>
Commit | Line | Data |
---|---|---|
05c0805b JW |
1 | /* |
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, | |
67 | output reg [1:0] ovf); | |
68 | ||
69 | always @(posedge clk) | |
70 | begin | |
71 | {ovf,out} <= | |
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; | |
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 JW |
95 | output wire sign, |
96 | output wire [1:0] overflow); | |
97 | ||
98 | NaiveMultiplier nm(clk, x, y, xsign, ysign, out, sign, overflow); | |
99 | ||
100 | endmodule | |
101 | ||
102 | module MandelUnit( | |
103 | input clk, | |
104 | input [12:0] x, y, | |
105 | input xsign, ysign, | |
92e851e1 | 106 | input [14:0] r, i, |
05c0805b JW |
107 | input rsign, isign, |
108 | input [7:0] ibail, icuriter, | |
109 | output reg [12:0] xout, yout, | |
110 | output reg xsout, ysout, | |
92e851e1 | 111 | output reg [14:0] rout, iout, |
05c0805b JW |
112 | output reg rsout, isout, |
113 | output reg [7:0] obail, ocuriter); | |
114 | ||
92e851e1 | 115 | wire [14:0] r2, i2, ri, diff; |
05c0805b JW |
116 | wire r2sign, i2sign, risign, dsign; |
117 | wire [16:0] bigsum; | |
118 | wire bigsum_ovf, rin_ovf, iin_ovf, throwaway; | |
119 | ||
120 | reg [12:0] xd, yd; | |
121 | reg rd, id; | |
122 | reg xsd, ysd; | |
123 | reg [7:0] ibaild, curiterd; | |
124 | ||
125 | assign ri[0] = 0; | |
126 | ||
92e851e1 JW |
127 | Multiplier r2m(clk, r[12:0], r[12:0], rsign, rsign, r2[12:0], r2sign, r2[14:13]); |
128 | Multiplier i2m(clk, i[12:0], i[12:0], isign, isign, i2[12:0], i2sign, i2[14:13]); | |
129 | Multiplier rim(clk, r[12:0], i[12:0], rsign, isign, ri[13:1], risign, {throwaway,ri[14]}); | |
05c0805b JW |
130 | |
131 | assign bigsum = r2 + i2; | |
132 | assign bigsum_ovf = bigsum[16] | bigsum[15] | bigsum[14]; | |
133 | assign rin_ovf = rd; | |
134 | assign iin_ovf = id; | |
135 | assign diff = (r2 > i2) ? r2 - i2 : i2 - r2; | |
136 | assign dsign = (r2 > i2) ? 0 : 1; | |
137 | ||
138 | always @ (posedge clk) | |
139 | begin | |
140 | xd <= x; | |
141 | yd <= y; | |
142 | xsd <= xsign; | |
143 | ysd <= ysign; | |
144 | xout <= xd; | |
145 | yout <= yd; | |
146 | xsout <= xsd; | |
147 | ysout <= ysd; | |
148 | ibaild <= ibail; | |
149 | curiterd <= icuriter; | |
92e851e1 JW |
150 | rd <= r[13] | r[14]; |
151 | id <= i[13] | i[14]; | |
05c0805b JW |
152 | |
153 | if (xsd ^ dsign) begin | |
154 | if (diff > xd) begin | |
155 | rout <= diff - xd; | |
156 | rsout <= dsign; | |
157 | end else begin | |
158 | rout <= xd - diff; | |
159 | rsout <= xsd; | |
160 | end | |
161 | end else begin | |
162 | rout <= diff + xd; | |
163 | rsout <= xsd; | |
164 | end | |
165 | ||
166 | if (ysd ^ risign) begin | |
167 | if (ri > yd) begin | |
168 | iout <= ri - yd; | |
169 | isout <= risign; | |
170 | end else begin | |
171 | iout <= yd - ri; | |
172 | isout <= ysd; | |
173 | end | |
174 | end else begin | |
175 | iout <= ri + yd; | |
176 | isout <= ysd; | |
177 | end | |
178 | ||
179 | // If we haven't bailed out, and we meet any of the bailout conditions, | |
180 | // bail out now. Otherwise, leave the bailout at whatever it was before. | |
181 | if ((ibaild == 255) && (bigsum_ovf | rin_ovf | iin_ovf)) | |
182 | obail <= curiterd; | |
183 | else | |
184 | obail <= ibaild; | |
185 | ocuriter <= curiterd + 8'b1; | |
186 | end | |
187 | ||
188 | endmodule | |
189 | ||
190 | module Mandelbrot( | |
191 | input mclk, | |
192 | input pixclk, | |
193 | input [11:0] x, y, | |
92e851e1 | 194 | input [13:0] xofs, yofs, |
05c0805b JW |
195 | input [7:0] colorofs, |
196 | input [2:0] scale, | |
197 | output reg [2:0] red, green, output reg [1:0] blue); | |
281eac32 JW |
198 | |
199 | `define MAXOUTN 12 | |
05c0805b JW |
200 | |
201 | wire [12:0] rx, ry; | |
202 | wire [13:0] nx, ny; | |
203 | wire rxsign, rysign; | |
204 | ||
205 | assign nx = x + xofs; | |
206 | assign ny = y + yofs; | |
207 | assign rx = (nx[13] ? -nx[12:0] : nx[12:0]) << scale; | |
208 | assign rxsign = nx[13]; | |
209 | assign ry = (ny[13] ? -ny[12:0] : ny[12:0]) << scale; | |
210 | assign rysign = ny[13]; | |
211 | ||
212 | ||
281eac32 JW |
213 | wire [14:0] mr[`MAXOUTN:0], mi[`MAXOUTN:0]; |
214 | wire mrs[`MAXOUTN:0], mis[`MAXOUTN:0]; | |
215 | wire [7:0] mb[`MAXOUTN:0]; | |
216 | wire [12:0] xprop[`MAXOUTN:0], yprop[`MAXOUTN:0]; | |
217 | wire xsprop[`MAXOUTN:0], ysprop[`MAXOUTN:0]; | |
218 | wire [7:0] curiter[`MAXOUTN:0]; | |
05c0805b | 219 | |
f802110e | 220 | wire [14:0] initx, inity, initr, initi; |
05c0805b JW |
221 | wire [7:0] initci, initb; |
222 | wire initxs, initys, initrs, initis; | |
223 | ||
92e851e1 | 224 | reg [14:0] loopx, loopy, loopr, loopi; |
05c0805b JW |
225 | reg [7:0] loopci, loopb; |
226 | reg loopxs, loopys, looprs, loopis; | |
227 | ||
228 | reg state = 0; | |
229 | ||
230 | // On pixclk = 1, | |
231 | // A new value to be loaded comes in, and a value in need of loopback comes out. | |
232 | // On pixclk = 0, | |
233 | // A new value in need of loopback comes in, and a completed value comes out. | |
234 | ||
235 | assign initx = state ? rx : loopx; | |
236 | assign inity = state ? ry : loopy; | |
237 | assign initr = state ? rx : loopr; | |
238 | assign initi = state ? ry : loopi; | |
239 | assign initxs = state ? rxsign : loopxs; | |
240 | assign initys = state ? rysign : loopys; | |
241 | assign initrs = state ? rxsign : looprs; | |
242 | assign initis = state ? rysign : loopis; | |
243 | assign initb = state ? 8'b11111111 : loopb; | |
244 | assign initci = state ? 8'b00000000 : loopci; | |
245 | ||
246 | reg [7:0] out; | |
247 | reg pixclksync; | |
248 | always @(negedge mclk) | |
249 | pixclksync <= ~pixclk; | |
250 | ||
251 | always @(posedge mclk) | |
252 | begin | |
253 | if (!state) begin | |
281eac32 | 254 | out <= ~mb[`MAXOUTN] + colorofs; |
05c0805b JW |
255 | end else begin |
256 | {red, green, blue} <= {out[0],out[3],out[6],out[1],out[4],out[7],out[2],out[5]}; | |
281eac32 JW |
257 | loopx <= xprop[`MAXOUTN]; |
258 | loopy <= yprop[`MAXOUTN]; | |
259 | loopr <= mr[`MAXOUTN]; | |
260 | loopi <= mi[`MAXOUTN]; | |
261 | loopxs <= xsprop[`MAXOUTN]; | |
262 | loopys <= ysprop[`MAXOUTN]; | |
263 | looprs <= mrs[`MAXOUTN]; | |
264 | loopis <= mis[`MAXOUTN]; | |
265 | loopb <= mb[`MAXOUTN]; | |
266 | loopci <= curiter[`MAXOUTN]; | |
05c0805b JW |
267 | end |
268 | state <= ~pixclksync; | |
269 | end | |
270 | ||
271 | MandelUnit mu0( | |
272 | mclk, | |
273 | initx, inity, initxs, initys, | |
274 | initr, initi, initrs, initis, | |
275 | initb, initci, | |
276 | xprop[0], yprop[0], xsprop[0], ysprop[0], | |
277 | mr[0], mi[0], mrs[0], mis[0], | |
278 | mb[0], curiter[0]); | |
279 | ||
280 | MandelUnit mu1(mclk, | |
281 | xprop[0], yprop[0], xsprop[0], ysprop[0], mr[0], mi[0], mrs[0], mis[0], mb[0], curiter[0], | |
282 | xprop[1], yprop[1], xsprop[1], ysprop[1], mr[1], mi[1], mrs[1], mis[1], mb[1], curiter[1]); | |
283 | MandelUnit mu2(mclk, | |
284 | xprop[1], yprop[1], xsprop[1], ysprop[1], mr[1], mi[1], mrs[1], mis[1], mb[1], curiter[1], | |
285 | xprop[2], yprop[2], xsprop[2], ysprop[2], mr[2], mi[2], mrs[2], mis[2], mb[2], curiter[2]); | |
286 | MandelUnit mu3(mclk, | |
287 | xprop[2], yprop[2], xsprop[2], ysprop[2], mr[2], mi[2], mrs[2], mis[2], mb[2], curiter[2], | |
288 | xprop[3], yprop[3], xsprop[3], ysprop[3], mr[3], mi[3], mrs[3], mis[3], mb[3], curiter[3]); | |
289 | MandelUnit mu4(mclk, | |
290 | xprop[3], yprop[3], xsprop[3], ysprop[3], mr[3], mi[3], mrs[3], mis[3], mb[3], curiter[3], | |
291 | xprop[4], yprop[4], xsprop[4], ysprop[4], mr[4], mi[4], mrs[4], mis[4], mb[4], curiter[4]); | |
292 | MandelUnit mu5(mclk, | |
293 | xprop[4], yprop[4], xsprop[4], ysprop[4], mr[4], mi[4], mrs[4], mis[4], mb[4], curiter[4], | |
294 | xprop[5], yprop[5], xsprop[5], ysprop[5], mr[5], mi[5], mrs[5], mis[5], mb[5], curiter[5]); | |
295 | MandelUnit mu6(mclk, | |
296 | xprop[5], yprop[5], xsprop[5], ysprop[5], mr[5], mi[5], mrs[5], mis[5], mb[5], curiter[5], | |
297 | xprop[6], yprop[6], xsprop[6], ysprop[6], mr[6], mi[6], mrs[6], mis[6], mb[6], curiter[6]); | |
298 | MandelUnit mu7(mclk, | |
299 | xprop[6], yprop[6], xsprop[6], ysprop[6], mr[6], mi[6], mrs[6], mis[6], mb[6], curiter[6], | |
300 | xprop[7], yprop[7], xsprop[7], ysprop[7], mr[7], mi[7], mrs[7], mis[7], mb[7], curiter[7]); | |
301 | MandelUnit mu8(mclk, | |
302 | xprop[7], yprop[7], xsprop[7], ysprop[7], mr[7], mi[7], mrs[7], mis[7], mb[7], curiter[7], | |
303 | xprop[8], yprop[8], xsprop[8], ysprop[8], mr[8], mi[8], mrs[8], mis[8], mb[8], curiter[8]); | |
304 | MandelUnit mu9(mclk, | |
305 | xprop[8], yprop[8], xsprop[8], ysprop[8], mr[8], mi[8], mrs[8], mis[8], mb[8], curiter[8], | |
306 | xprop[9], yprop[9], xsprop[9], ysprop[9], mr[9], mi[9], mrs[9], mis[9], mb[9], curiter[9]); | |
281eac32 JW |
307 | MandelUnit mua(mclk, |
308 | xprop[9], yprop[9], xsprop[9], ysprop[9], mr[9], mi[9], mrs[9], mis[9], mb[9], curiter[9], | |
309 | xprop[10], yprop[10], xsprop[10], ysprop[10], mr[10], mi[10], mrs[10], mis[10], mb[10], curiter[10]); | |
310 | MandelUnit mub(mclk, | |
311 | xprop[10], yprop[10], xsprop[10], ysprop[10], mr[10], mi[10], mrs[10], mis[10], mb[10], curiter[10], | |
312 | xprop[11], yprop[11], xsprop[11], ysprop[11], mr[11], mi[11], mrs[11], mis[11], mb[11], curiter[11]); | |
313 | MandelUnit muc(mclk, | |
314 | xprop[11], yprop[11], xsprop[11], ysprop[11], mr[11], mi[11], mrs[11], mis[11], mb[11], curiter[11], | |
315 | xprop[12], yprop[12], xsprop[12], ysprop[12], mr[12], mi[12], mrs[12], mis[12], mb[12], curiter[12]); | |
316 | ||
05c0805b JW |
317 | endmodule |
318 | ||
319 | module Logo( | |
320 | input pixclk, | |
321 | input [11:0] x, y, | |
322 | output wire enb, | |
323 | output wire [2:0] red, green, output wire [1:0] blue); | |
324 | ||
325 | reg [1:0] logo[8191:0]; | |
326 | initial $readmemb("logo.readmemb", logo); | |
327 | ||
328 | assign enb = (x < 96) && (y < 64); | |
329 | wire [12:0] addr = {y[5:0], x[6:0]}; | |
330 | wire [1:0] data = logo[addr]; | |
331 | assign {red, green, blue} = | |
332 | (data == 2'b00) ? 8'b00000000 : | |
333 | ((data == 2'b01) ? 8'b00011100 : | |
334 | ((data == 2'b10) ? 8'b11100000 : | |
335 | 8'b11111111)); | |
336 | endmodule | |
337 | ||
338 | module MandelTop( | |
339 | input gclk, output wire dcmok, | |
340 | output wire vs, hs, | |
341 | output wire [2:0] red, green, output [1:0] blue, | |
342 | input left, right, up, down, rst, cycle, logooff, | |
343 | input [2:0] scale); | |
344 | ||
345 | wire border; | |
346 | wire pixclk; | |
347 | wire [7:0] zero = 8'b0; | |
348 | wire clk; | |
349 | wire [11:0] x, y; | |
92e851e1 | 350 | reg [13:0] xofs = -`XRES/2, yofs = -`YRES/2; |
05c0805b JW |
351 | reg [5:0] slowctr = 0; |
352 | reg [7:0] colorcycle = 0; | |
353 | wire [11:0] realx, realy; | |
354 | ||
355 | wire logoenb; | |
356 | wire [2:0] mandelr, mandelg, logor, logog; | |
357 | wire [1:0] mandelb, logob; | |
358 | ||
359 | pixDCM dcm( // CLKIN is 50MHz xtal, CLKFX_OUT is 25MHz | |
360 | .CLKIN_IN(gclk), | |
361 | .CLKFX_OUT(pixclk), | |
362 | .CLKIN_IBUFG_OUT(clk), | |
363 | .LOCKED_OUT(dcmok) | |
364 | ); | |
365 | ||
366 | SyncGen sync(pixclk, vs, hs, x, y, realx, realy, border); | |
367 | Mandelbrot mandel(clk, pixclk, x, y, xofs, yofs, cycle ? colorcycle : 0, scale, mandelr, mandelg, mandelb); | |
368 | Logo logo(pixclk, realx, realy, logoenb, logor, logog, logob); | |
369 | ||
370 | assign {red,green,blue} = | |
371 | border ? 8'b00000000 : | |
372 | (!logooff && logoenb) ? {logor, logog, logob} : {mandelr, mandelg, mandelb}; | |
373 | ||
374 | always @(posedge vs) | |
375 | begin | |
376 | if (rst) | |
377 | begin | |
378 | xofs <= -`XRES/2; | |
379 | yofs <= -`YRES/2; | |
380 | colorcycle <= 0; | |
381 | end else begin | |
382 | if (up) yofs <= yofs + 1; | |
383 | else if (down) yofs <= yofs - 1; | |
384 | ||
385 | if (left) xofs <= xofs + 1; | |
386 | else if (right) xofs <= xofs - 1; | |
387 | ||
388 | if (slowctr == 0) | |
389 | colorcycle <= colorcycle + 1; | |
390 | end | |
391 | ||
392 | if (slowctr == 12) | |
393 | slowctr <= 0; | |
394 | else | |
395 | slowctr <= slowctr + 1; | |
396 | end | |
397 | endmodule |