Skip to content

Instantly share code, notes, and snippets.

View forensicgarlic's full-sized avatar

DLS forensicgarlic

  • Chicago
View GitHub Profile
@forensicgarlic
forensicgarlic / tricky_order.v
Created March 28, 2017 02:18
tricky verilog order of operations
always @ (posedge clk)
begin
// the below runs into problems because apparently the cnt value
// is evaluated after addition instead of the registered version.
// changing cnt <= cnt + 1 to cnt = cnt + 1 had no effect.
//if (cnt == max_value) begin
// results_valid <= 1;
//end else if (accum3 | accum5) begin
// results <= results + cnt;
//end
@forensicgarlic
forensicgarlic / invertor.v
Created March 28, 2017 02:14
an invertor
module myInvert(a,b);
input wire a;
output wire b;
assign b = !a;
endmodule // myInvert
@forensicgarlic
forensicgarlic / slow_count.v
Created March 22, 2017 19:06
ice studio generated .v file example
// Code generated by Icestudio 0.3.0-beta3
// Wed, 22 Mar 2017 19:04:57 GMT
`default_nettype none
module main #(
parameter vb768ba = 4'h2,
parameter vd39cf6 = 4'h5,
parameter v8a74c9 = 12000000
) (
@forensicgarlic
forensicgarlic / convertedPythonToVHDL.vhd
Created April 19, 2016 18:29
VHDL converted from python automatically for euler problem 3
EULER3_A: process (clk, reset) is
begin
if (reset = '1') then
y <= to_unsigned(1, 64);
x <= to_unsigned(1, 64);
product <= to_unsigned(1, 64);
roots <= to_unsigned(1, 64);
elsif rising_edge(clk) then
if (bool(enable) and (not bool(results_valid_int))) then
if ((y mod x) = 0) then
@forensicgarlic
forensicgarlic / hardware_implementation_euler3.py
Created April 19, 2016 18:04
guts of the hardware implementation of project euler problem 3
@always_seq(clk.posedge, reset)
def a():
if enable and not results_valid_int:
if (y % x == 0):
roots.next = x
product.next = product * x
y.next = y // x
else:
x.next = x + 1
elif not enable:
@forensicgarlic
forensicgarlic / euler3 unit test function.py
Created April 19, 2016 17:02
unit test function for euler project problem 3
def euler3_py(self, number):
roots = 0
product = 1
x = 2
y = number
while product != number:
while (y % x == 0):
roots = x
y /= x
product *= roots
@forensicgarlic
forensicgarlic / hardway2_hardheaded2.vhd
Created April 5, 2016 18:25
Surprising VHDL conversion of pythonic implementation of the euler problem 2
HARDWAY2_HARDHEADED2: process (clk, reset) is
variable f1: integer;
variable f2: integer;
variable f1_old: std_logic;
variable results_int: integer;
begin
if (reset = '1') then
results_valid <= '0';
results <= to_unsigned(0, 64);
elsif rising_edge(clk) then
@forensicgarlic
forensicgarlic / hardheaded2.py
Created April 5, 2016 18:22
function for calculating even fibonacci numbers, updated for hardware convertability
@always_seq(clk.posedge, reset)
def hardheaded2():
f1 = 1
f2 = 1
results_int = 0
while (f1 + f2) < max_value:
results_int = results_int + f1 + f2
#tuple assignment is not supported in hardware conversion
#f1, f2 = f1 + (2 * f2), (2 * f1) + (3 * f2)
#if these were signals, I wouldn't be worried about the below being sequential. Since they aren't though...
@forensicgarlic
forensicgarlic / checkResultHW.py
Created April 5, 2016 18:20
if your test function is acting on hardware signals like a generator, it has to be yielded to.
def checkResultHw(self):
self.reset.next = 1
yield self.clk.negedge
self.reset.next = 0
print "set and check 100"
#you can't call the function like
#self.set_and_check(100)
#instead you have to yield to it because it's a myHDL generator.
yield self.set_and_check(100)
@forensicgarlic
forensicgarlic / set_and_check.py
Created April 5, 2016 18:15
set max and check results, with some poor use of yields.
def set_and_check(self, max_value):
print "in set and check"
self.max_value.next = max_value;
yield self.clk.negedge
self.enable.next = True
yield self.results_valid
print "expected results = %d" % self.euler2_py(self.max_value)
print "actual results = %d" % self.results
self.assertEqual(self.results, self.euler2_py(self.max_value),
"does " + str(self.results) + " == " + str(self.euler2_py(self.max_value)) + " for max == " + str(self.max_value))