-
+ 0000B0ABCB2FE3F54B635A846A7E3A8734ED944A54B9CD6BBD9F8696E97734F4FD0D7D82E964F47034F2B3468654027CA488B78EE3914A89F88C59596E0DF133
fg.v
(0 . 0)(1 . 437)
56 ///////////////////////////////////////////////////////////////////////////
57 // FUCKGOATS CPLD circuit. V.3K (December 2016.)
58 //
59 // This was written for an XC9572XL. It SHOULD work on any gate array of
60 // equal or greater size, but no such assurance is given.
61 // It SHOULD also work quite well in the form of an ASIC, or in TTL, or
62 // using any other reasonably-fast logic element.
63 //
64 // (C) 2016 No Such lAbs.
65 //
66 // You do not have, nor can you ever acquire the right to use, copy or
67 // distribute this software ; Should you use this software for any purpose,
68 // or copy and distribute it to anyone or in any manner, you are breaking
69 // the laws of whatever soi-disant jurisdiction, and you promise to
70 // continue doing so for the indefinite future. In any case, please
71 // always : read and understand any software ; verify any PGP signatures
72 // that you use - for any purpose.
73 ///////////////////////////////////////////////////////////////////////////
74
75 ///////////////////////////////////////////////////////////////////////////
76 // Knobs.
77 ///////////////////////////////////////////////////////////////////////////
78 // BITS of analogue input used to make each BYTE of accum output.
79 // You MAY want to change if you were to use another type of analogue RNG,
80 // particularly one having a different spectrum from the 'TW' board.
81 `define FOLD_RSIZE 4 // size of counter register
82 `define FOLD 4'd15 // bit count
83 ///////////////////////////////////////////////////////////////////////////
84
85
86 // div128, for UART's 115200b on 14.7456MHz crystal.
87 // We also use it as slow clock for the watchdog.
88 module baudgen(clk, clkout);
89 input clk;
90 output clkout;
91
92 reg [6:0] counter = 0;
93 wire clkout;
94
95 always @(posedge clk)
96 begin
97 counter <= counter + 1;
98 end
99 assign clkout = (counter == 7'b1111111);
100 endmodule
101
102
103 // A very simple TX-only UART. 'No user serviceable parts inside.'
104 // No async reset, because nothing good can come of aborting mid-byte.
105 module ser_tx(clk, baud_clk, fire, txbyte, tx, busy);
106 input clk, baud_clk, fire;
107 input [7:0] txbyte;
108 output tx, busy;
109 wire busy;
110
111 // Transmitter state machine
112 reg [3:0] state = 0;
113 wire tx_ready = (state == 0);
114 assign busy = ~tx_ready;
115
116 wire [7:0] txbyteD = txbyte;
117
118 always @(posedge clk)
119 case(state)
120 4'b0000: if(fire) state <= 4'b0001;
121 4'b0001: if(baud_clk) state <= 4'b0100;
122 4'b0100: if(baud_clk) state <= 4'b1000; // start
123 4'b1000: if(baud_clk) state <= 4'b1001; // bit 0
124 4'b1001: if(baud_clk) state <= 4'b1010; // bit 1
125 4'b1010: if(baud_clk) state <= 4'b1011; // bit 2
126 4'b1011: if(baud_clk) state <= 4'b1100; // bit 3
127 4'b1100: if(baud_clk) state <= 4'b1101; // bit 4
128 4'b1101: if(baud_clk) state <= 4'b1110; // bit 5
129 4'b1110: if(baud_clk) state <= 4'b1111; // bit 6
130 4'b1111: if(baud_clk) state <= 4'b0010; // bit 7
131 4'b0010: if(baud_clk) state <= 4'b0000; // stop1
132 4'b0011: if(baud_clk) state <= 4'b0000; // stop2 (off)
133 default: if(baud_clk) state <= 4'b0000;
134 endcase
135
136 reg muxbit;
137 always @(*)
138 case(state[2:0])
139 3'd0: muxbit <= txbyteD[0];
140 3'd1: muxbit <= txbyteD[1];
141 3'd2: muxbit <= txbyteD[2];
142 3'd3: muxbit <= txbyteD[3];
143 3'd4: muxbit <= txbyteD[4];
144 3'd5: muxbit <= txbyteD[5];
145 3'd6: muxbit <= txbyteD[6];
146 3'd7: muxbit <= txbyteD[7];
147 endcase
148
149 reg tx;
150 always @(posedge clk)
151 tx <= (state<4) | (state[3] & muxbit);
152 endmodule
153
154
155 // One way to gather entropy from absolute pulse widths.
156 // This was not feasible in v.10K (it could not rely on synced clk.)
157 module eater(clk, nres, in, valid, out);
158 input clk, nres, in;
159 output valid, out;
160
161 reg [3:0] q;
162 reg out;
163 reg s;
164
165 always @(posedge clk or negedge nres)
166 if (~nres)
167 begin
168 s <= 0;
169 end
170 else
171 begin
172 s <= in;
173 end
174
175 wire pulse = s ^ in; // either rise or fall of input
176
177 always @(posedge clk or negedge nres)
178 if (~nres)
179 begin
180 q <= 0;
181 out <= 0;
182 end
183 else
184 begin
185 out <= q[3] ^ q[2] ^ q[1] ^ q[0];
186 q <= pulse ? 0 : q + 1;
187 end
188
189 assign valid = pulse;
190 endmodule
191 // NOTE that if you are doing a yoke test with two units and one set
192 // of analogue RNGs, you must buffer the latter with a latch clocked
193 // from the common clock, so that they obey the hold time constraint
194 // of the CPLD's input buffer. Without this, operation will ~still~
195 // be repeatable between the yoked units, but ~with errors~.
196
197
198 // Von Neumann's 'fair coin' algorithm.
199 module debias(clk, res, fire, in, valid, out);
200 input clk, res, fire, in;
201 output valid, out;
202
203 reg p, q;
204 reg [1:0] state;
205 wire valid;
206
207 always @(posedge clk or negedge res)
208 if (~res)
209 begin
210 state <= 0;
211 p <= 0;
212 q <= 0;
213 end
214 else
215 begin
216 case(state)
217 2'd0:
218 begin
219 p <= in;
220 state <= fire ? 2'd1 : 2'd0;
221 end
222 2'd1:
223 begin
224 q <= in;
225 state <= fire ? 2'd2 : 2'd1;
226 end
227 2'd2:
228 begin
229 state <= 2'd0;
230 end
231 2'd3: // unused
232 begin
233 state <= 2'd0;
234 end
235 endcase
236 end
237
238 assign valid = (p ^ q) & (state == 2'd2);
239 assign out = p;
240 endmodule
241
242
243 // Byte Accumulator.
244 module accum(clk, nres, fire, in, roll, rdy);
245 parameter fold = 8; // bits per shot
246 parameter count_bits = 4;
247
248 input clk, in, nres, fire;
249 output rdy;
250 output [7:0] roll;
251
252 reg [7:0] roll;
253 reg [count_bits-1:0] bit_count;
254 reg rdy;
255
256 always @(posedge clk or negedge nres)
257 begin
258 if (~nres) // async reset
259 begin
260 bit_count <= 0;
261 roll <= 0;
262 rdy <= 0;
263 end
264 else
265 if (fire)
266 begin
267 roll[bit_count[2:0]] <= roll[bit_count[2:0]] ^ in;
268 bit_count <= bit_count + 1;
269 if (bit_count == fold) rdy <= 1; // raise ready flag
270 end
271 end
272 endmodule
273
274
275 // Watchdog. Counter, stops advancing at max until reset.
276 module dog(clk, nres, run, alarm);
277 input clk, nres, run;
278 output alarm;
279
280 reg [5:0] count;
281
282 assign alarm = (count == 6'b111111);
283
284 always @(posedge clk or negedge nres)
285 begin
286 if (~nres) // async reset
287 begin
288 count <= 0;
289 end
290 else
291 if (run & ~alarm)
292 begin
293 count <= count + 1;
294 end
295 end
296 endmodule
297
298
299 // Werker.
300 module fg(input wire xtal, // on-board clock, from 14.7456MHz resonator
301 inout wire clk, // clock output (master) or input (slave)
302 input wire IN_A, // 'bottom' analogue RNG input, pulled high
303 input wire IN_B, // 'top' analogue RNG input, pulled high
304 output wire ser_tx, // output of UART, to TTL converter
305 output wire SAD // red lamp
306 );
307
308 ///////////////////////////////////////////////////////////////////////////
309 // Serial UART
310 ///////////////////////////////////////////////////////////////////////////
311
312 wire tx;
313 wire fire;
314 wire busy;
315 wire baud_clk;
316 reg [7:0] txbyte;
317
318 // baud clock generator
319 baudgen bg(clk, baud_clk);
320
321 // UART.
322 ser_tx sender(clk, baud_clk, fire, txbyte, tx, busy);
323
324 ///////////////////////////////////////////////////////////////////////////
325 // Master/Slave toggle, for clock-synchronized slave yoke tests.
326 // Yoke two units by connecting the CLK ('RESET' on v1 pcb) pins together.
327 // The first board powered up will become the master.
328 // Slave's red lamp will stay lit, and his clock is inhibited, uses master
329 ///////////////////////////////////////////////////////////////////////////
330 reg [2:0] boot_state = 0;
331
332 // If I master, I output ~my~ clock; otherwise - I slave, wait for a clock:
333 wire am_master = (boot_state == 3'd6);
334 assign clk = am_master ? xtal : 1'bz; // this pin is pulled high
335
336 // Breath of Life
337 always @(posedge xtal) // must use on-board oscillator, for obvious reasons
338 begin
339 case(boot_state)
340 default:
341 begin
342 boot_state <= clk ? boot_state + 1 : 3'd7;
343 end
344 3'd6: // I am a master, for life! (until powerdown)
345 begin
346 boot_state <= 3'd6;
347 end
348 3'd7: // I am a slave, for life! (until powerdown)
349 begin
350 boot_state <= 3'd7;
351 end
352 endcase
353 end
354
355 // we want to hold reset for a short while on bootup
356 reg nreset = 0;
357
358 always @(posedge clk) // use the active clock, ~when we get one~
359 begin
360 if ((~IN_A) & (~IN_B) & baud_clk) // stay in reset until both rng work
361 begin // and until first baud clock pulse
362 nreset <= 1'd1;
363 end
364 end
365
366 // We hold ser_tx low on reset, this way it is easy to see when finished.
367 assign ser_tx = tx & nreset;
368
369 ///////////////////////////////////////////////////////////////////////////
370
371 ///////////////////////////////////////////////////////////////////////////
372 // If this device were to grind to a halt, wouldn't you like to know?
373 ///////////////////////////////////////////////////////////////////////////
374 wire dog_alarm;
375 wire dog_run;
376 wire dog_reset;
377
378 dog sad_dog(clk,
379 dog_reset & nreset, // watchdog reset (if either dips low)
380 dog_run, // watchdog enable-advance
381 dog_alarm); // watchdog alarm, tied to 'SAD' lamp
382 ///////////////////////////////////////////////////////////////////////////
383
384 ///////////////////////////////////////////////////////////////////////////
385 // RNG
386 ///////////////////////////////////////////////////////////////////////////
387 wire eater_a_valid;
388 wire eater_a_out;
389 eater e_a(clk, nreset, IN_A, eater_a_valid, eater_a_out);
390
391 wire eater_b_valid;
392 wire eater_b_out;
393 eater e_b(clk, nreset, IN_B, eater_b_valid, eater_b_out);
394
395 wire valid_a;
396 wire outbit_a;
397
398 debias dbias_a(clk,
399 nreset,
400 eater_a_valid,
401 eater_a_out,
402 valid_a,
403 outbit_a);
404
405 wire valid_b;
406 wire outbit_b;
407
408 debias dbias_b(clk,
409 nreset,
410 eater_b_valid,
411 eater_b_out,
412 valid_b,
413 outbit_b);
414
415 // per xor lemma:
416 wire valid = valid_a | valid_b;
417 wire outbit = outbit_a ^ outbit_b;
418
419 wire acc_res; // reset accum (active low)
420 wire acc_rdy; // accum has byte
421 wire [7:0] acc_byte; // output of accumulator
422
423 accum #(.fold(`FOLD),
424 .count_bits(`FOLD_RSIZE)) acc_a(clk,
425 acc_res & nreset, // if either low
426 valid,
427 outbit,
428 acc_byte,
429 acc_rdy);
430
431 ///////////////////////////////////////////////////////////////////////////
432
433 // device cycles forever through four phases
434 reg [1:0] state;
435
436 always @(posedge clk or negedge nreset)
437 if (~nreset)
438 begin
439 state <= 0;
440 txbyte <= 0;
441 end
442 else
443 begin
444 case(state)
445 2'd0: // ground state
446 begin
447 txbyte <= 0; // clear tx buffer
448 state <= 2'd1; // next
449 end
450 2'd1:
451 begin // wait for 'a' accum
452 if (acc_rdy)
453 begin
454 txbyte <= acc_byte;
455 state <= 2'd2; // next
456 end
457 else
458 state <= 2'd1; // wait
459 end
460 2'd2:
461 begin // clear 'a'; fire tx
462 state <= 2'd3; // next
463 end
464 2'd3:
465 begin // wait to end of tx
466 state <= (~busy) ? 2'd0 : 2'd3; // wait for UART
467 end
468 endcase
469 end
470
471 assign fire = (state == 2'd2); // txbyte is ready, so fire the UART
472 assign acc_res = ~(fire); // zap acc_byte (active low)
473
474 // advance dogometer at the baud clock rate
475 assign dog_run = baud_clk & (state == 2'd1);
476
477 assign dog_reset = acc_res; // reset dogometer we got a byte
478
479 // Red 'Sadness' lamp.
480 // If STEADILY LIT, check ALL connections: the device is halted and is
481 // doing NO USEFUL WORK at all!
482 // Re-seat analogue RNG boards, check their power supplies (if you have,
483 // e.g., a custom isolation system.) Otherwise - one or both analogue
484 // RNG units may need replacement.
485 assign SAD = dog_alarm | ~am_master | (~nreset);
486 // Red lamp will also light if the board is placed into the slave
487 // state (for verification) using the external clock
488 // (marked RESET on early board revs) pin; and when in RESET state.
489 endmodule
490
491 ///////////////////////////////////////////////////////////////////////////
492 ///////////////////////////////////////////////////////////////////////////