forked from tanchou/Verilog
struct
This commit is contained in:
71
Help/presentation_examples/led_walk/src/verilog/counter.v
Normal file
71
Help/presentation_examples/led_walk/src/verilog/counter.v
Normal file
@@ -0,0 +1,71 @@
|
||||
`timescale 1ns/1ps
|
||||
`default_nettype none
|
||||
|
||||
// Simple counter_reg with synchronous reset (and no load)
|
||||
//
|
||||
|
||||
module counter #(
|
||||
parameter WIDTH = 8,
|
||||
parameter INITIAL_VALUE = 8'h0f
|
||||
) (
|
||||
input wire clk, // global clock
|
||||
input wire reset, // synchronous reset signal
|
||||
input wire en, // enable signal
|
||||
output reg strobe // output strobe
|
||||
);
|
||||
|
||||
initial strobe = 0;
|
||||
|
||||
reg[WIDTH-1:0] counter_reg = INITIAL_VALUE;
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (reset) begin
|
||||
counter_reg <= INITIAL_VALUE;
|
||||
end
|
||||
else if (en) begin
|
||||
strobe <= 0;
|
||||
if (counter_reg == 0)
|
||||
counter_reg <= INITIAL_VALUE;
|
||||
else begin
|
||||
if (counter_reg == 1)
|
||||
strobe <= 1;
|
||||
counter_reg <= counter_reg - 1;
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
`ifdef FORMAL
|
||||
|
||||
reg f_past_valid = 0;
|
||||
|
||||
always @* f_past_valid = !$initstate;
|
||||
|
||||
always @* begin
|
||||
assume (counter_reg <= INITIAL_VALUE);
|
||||
end
|
||||
|
||||
always @(posedge clk) begin
|
||||
// if ($initstate) begin
|
||||
// assume(counter_reg==INITIAL_VALUE);
|
||||
// end
|
||||
// if (!$initstate && $past(en)==1 && !$past(reset)) begin
|
||||
if (f_past_valid && $past(en)==1 && !$past(reset)) begin
|
||||
if (counter_reg < INITIAL_VALUE)
|
||||
assert (counter_reg == $past(counter_reg) - 1);
|
||||
else begin
|
||||
assert(counter_reg == INITIAL_VALUE && $past(counter_reg)==0);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
`endif
|
||||
|
||||
|
||||
`ifdef VERIFIC
|
||||
//internal_check: assert property (@(posedge clk)(counter_reg==11)|=>(counter_reg==10));
|
||||
//assert property (@(posedge clk)(counter_reg==11 && en && !reset)|=>(counter_reg==10));
|
||||
`endif
|
||||
|
||||
|
||||
endmodule
|
||||
51
Help/presentation_examples/led_walk/src/verilog/led_walk.v
Normal file
51
Help/presentation_examples/led_walk/src/verilog/led_walk.v
Normal file
@@ -0,0 +1,51 @@
|
||||
`timescale 1ns/1ps
|
||||
`default_nettype none
|
||||
module led_walk #(
|
||||
parameter CLK_SPEED = 27_000_000,
|
||||
parameter NBLEDS = 5
|
||||
)
|
||||
(
|
||||
input wire clk,
|
||||
output reg [NBLEDS-1:0] led
|
||||
);
|
||||
|
||||
wire strobe;
|
||||
|
||||
localparam ONE_HALF_S = CLK_SPEED / 2;
|
||||
localparam ONE_S = CLK_SPEED ;
|
||||
localparam TWO_S = CLK_SPEED *2;
|
||||
|
||||
|
||||
localparam TIME = TWO_S;
|
||||
localparam TIME_WIDTH = $clog2(TIME);
|
||||
|
||||
initial led = {NBLEDS{1'b1}};
|
||||
|
||||
counter #(.WIDTH(TIME_WIDTH), .INITIAL_VALUE(TIME)) counter_inst
|
||||
(
|
||||
.clk(clk),
|
||||
.reset(1'b0),
|
||||
.en(1'b1),
|
||||
.strobe(strobe)
|
||||
);
|
||||
|
||||
|
||||
|
||||
// State machine to cycle through LEDs
|
||||
reg [$clog2(NBLEDS)-1:0] current_led; // Tracks the current LED index
|
||||
|
||||
always_ff @(posedge clk) begin
|
||||
if (strobe) begin
|
||||
// Turn off all LEDs and turn on the next one in the cycle
|
||||
led <= {NBLEDS{1'b1}};
|
||||
led[current_led] <= 1'b0;
|
||||
|
||||
// Move to the next LED in the cycle
|
||||
if (current_led == NBLEDS - 1)
|
||||
current_led <= 0;
|
||||
else
|
||||
current_led <= current_led + 1;
|
||||
end
|
||||
end
|
||||
|
||||
endmodule
|
||||
@@ -0,0 +1,15 @@
|
||||
module blink_top
|
||||
(
|
||||
input wire clk,
|
||||
output wire [5:0] leds
|
||||
);
|
||||
|
||||
led_walk #(
|
||||
.CLK_SPEED(27_000_000),
|
||||
.NBLEDS(6)
|
||||
) lw_inst (
|
||||
.clk(clk),
|
||||
.led(leds)
|
||||
);
|
||||
|
||||
endmodule
|
||||
Reference in New Issue
Block a user