Skip to content

Instantly share code, notes, and snippets.

@lovely-error
Created February 7, 2025 10:28
Show Gist options
  • Save lovely-error/99f7269a8a79974834825abac43802a2 to your computer and use it in GitHub Desktop.
Save lovely-error/99f7269a8a79974834825abac43802a2 to your computer and use it in GitHub Desktop.
blinkin it the right way
module REG (CLK, WE, D, O);
parameter BITWIDTH = 1;
parameter [BITWIDTH-1:0] INIT = '0;
input CLK;
input WE;
input [BITWIDTH-1:0] D;
output [BITWIDTH-1:0] O;
genvar i;
generate for (i = 0; i < BITWIDTH; i = i + 1) begin : loop
// gowin's def of Sync WE FF primitive
DFFE _b (.CLK(CLK), .D(D[i]), .CE(WE), .Q(O[i]));
defparam _b.INIT = INIT[i];
end endgenerate
endmodule
module led2 (
input sys_clk, // clk input
output logic [5:0] led // 6 LEDS pin
);
parameter DELAY = 24'd1349_9999;
typedef logic [6-1:0] LEDS;
typedef logic [24-1:0] COUNTER;
bit counter_wen ;
COUNTER counter_in ;
COUNTER counter_out ;
REG _counter (.CLK(sys_clk), .WE(counter_wen), .D(counter_in), .O(counter_out)) ;
defparam _counter.BITWIDTH = 24;
bit leds_wen ;
LEDS leds_in ;
LEDS leds_out ;
REG _leds (.CLK(sys_clk), .WE(leds_wen), .D(leds_in), .O(leds_out));
defparam _leds.BITWIDTH = 6;
always_comb begin
// Default assignments
led = ~leds_out;
counter_wen = 1'b1; // Always enable the counter write
counter_in = counter_out + 24'd1; // Increment counter by default
leds_wen = 1'b0; // Disable LED write by default
leds_in = 'z; // High-impedance by default
// Special case when counter reaches DELAY
if (counter_out == DELAY) begin
counter_in = 24'd0; // Reset the counter
leds_wen = 1'b1; // Enable LED write
leds_in = leds_out + 1'b1; // Increment the LEDs
end
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment