Compare commits
16 Commits
ccdfe6da64
...
tut4-with-
| Author | SHA1 | Date | |
|---|---|---|---|
| 556a79e705 | |||
| 6096187250 | |||
| 0a724d30d0 | |||
| 5f18f8f88c | |||
| e284e518ed | |||
| 66010c90d9 | |||
| 70d8ea268e | |||
| 400ebbb9aa | |||
| 45160b50a9 | |||
| 3a3830b443 | |||
| 12829a3e9c | |||
| 42c5b8a47f | |||
| f9b7340667 | |||
| 9fe8d26364 | |||
| 7bb2d41932 | |||
| 1198429d53 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,6 +1,4 @@
|
|||||||
*/obj_dir
|
|
||||||
*/build
|
*/build
|
||||||
*/*.bin
|
*/obj_dir
|
||||||
*/*.asc
|
*/diagram
|
||||||
*/*.json
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ all: $(SIM_TARGET) $(BIN_TARGET)
|
|||||||
# -GWIDTH=5 allows passing parameter to verilog module
|
# -GWIDTH=5 allows passing parameter to verilog module
|
||||||
obj_dir/Vblinky.cpp: blinky.v
|
obj_dir/Vblinky.cpp: blinky.v
|
||||||
@echo "Running verilator"
|
@echo "Running verilator"
|
||||||
@$(VERILATOR) --trace -Wall -GWIDTH=20 -cc blinky.v
|
@$(VERILATOR) --trace -Wall -GWIDTH=15 -cc blinky.v --timescale-override 10ns/1ns
|
||||||
|
|
||||||
obj_dir/Vblinky__ALL.a: obj_dir/Vblinky.cpp
|
obj_dir/Vblinky__ALL.a: obj_dir/Vblinky.cpp
|
||||||
@make --no-print-directory -C obj_dir -f Vblinky.mk
|
@make --no-print-directory -C obj_dir -f Vblinky.mk
|
||||||
@@ -27,13 +27,14 @@ $(SIM_TARGET): blinky.cpp obj_dir/Vblinky__ALL.a
|
|||||||
@mkdir -p build
|
@mkdir -p build
|
||||||
@g++ -I$(VINC) -I obj_dir -std=c++11 $(VINC)/verilated.cpp $(VINC)/verilated_vcd_c.cpp \
|
@g++ -I$(VINC) -I obj_dir -std=c++11 $(VINC)/verilated.cpp $(VINC)/verilated_vcd_c.cpp \
|
||||||
$^ -o $@
|
$^ -o $@
|
||||||
@echo "Run simulation with ./$(TARGET)"
|
@echo "Run simulation with ./$(SIM_TARGET)"
|
||||||
|
|
||||||
$(BIN_TARGET): blinky.v $(PCF)
|
$(BIN_TARGET): blinky.v $(PCF)
|
||||||
@echo "Building binary stream"
|
@echo "Building binary stream"
|
||||||
@$(YOSYS) -p "synth_ice40 -top blinky -json build/blinky.json" -q $<
|
@$(YOSYS) -p "synth_ice40 -top blinky -json build/blinky.json" -q $<
|
||||||
@$(PNR) -r --hx8k --json build/blinky.json --package cb132 \
|
@$(PNR) -r --hx8k --json build/blinky.json --package cb132 \
|
||||||
--asc build/blinky.asc --opt-timing --pcf $(PCF) -q
|
--asc build/blinky.asc --opt-timing --pcf $(PCF)\
|
||||||
|
--pre-pack clock_constraints.py -l build/pnr_report.txt -q
|
||||||
@$(IPACK) build/blinky.asc build/blinky.bin
|
@$(IPACK) build/blinky.asc build/blinky.bin
|
||||||
|
|
||||||
burn: $(BIN_TARGET)
|
burn: $(BIN_TARGET)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ int main(int argc, char **argv) {
|
|||||||
unsigned tickcount = 0;
|
unsigned tickcount = 0;
|
||||||
int last_led = tb->o_led;
|
int last_led = tb->o_led;
|
||||||
|
|
||||||
for(int k=0; k<(1 << 23); k++) {
|
for(int k=0; k<(1 << 18); k++) {
|
||||||
tick(++tickcount, tb, tfp);
|
tick(++tickcount, tb, tfp);
|
||||||
|
|
||||||
if (last_led != tb->o_led) {
|
if (last_led != tb->o_led) {
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ module blinky(i_clk, o_led, lcol1);
|
|||||||
always @(posedge i_clk)
|
always @(posedge i_clk)
|
||||||
counter <= counter + 1'b1;
|
counter <= counter + 1'b1;
|
||||||
|
|
||||||
assign o_led = counter[WIDTH-1];
|
assign o_led = counter[WIDTH-1]; // normal, symmetrical blinking
|
||||||
|
// assign o_led = ~(&counter[WIDTH-1:WIDTH-3]); // short on, long off (strobe)
|
||||||
|
// assign o_led = ~(counter[5:0] < counter[WIDTH-1:WIDTH-6]); // PWM, dimmer
|
||||||
assign lcol1 = 1'b0;
|
assign lcol1 = 1'b0;
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
|
|||||||
1
blinky/clock_constraints.py
Normal file
1
blinky/clock_constraints.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ctx.addClock("i_clk", 100)
|
||||||
61
fsm-tut4/Makefile
Normal file
61
fsm-tut4/Makefile
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
SIM_TARGET = build/top
|
||||||
|
BIN_TARGET = build/top.bin
|
||||||
|
PCF = constraints/iceFUN.pcf
|
||||||
|
TIMING = constraints/timing.py
|
||||||
|
YOSYS = yosys
|
||||||
|
PNR = nextpnr-ice40
|
||||||
|
IPACK = icepack
|
||||||
|
BURN = iceFUNprog
|
||||||
|
SBY = sby
|
||||||
|
|
||||||
|
VERILATOR=verilator
|
||||||
|
VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e "s/^.*=\s*//"')
|
||||||
|
VINC := $(VERILATOR_ROOT)/include
|
||||||
|
|
||||||
|
RTL_SRC := $(wildcard rtl/*.v)
|
||||||
|
SIM_SRC := $(wildcard sim/*.cc)
|
||||||
|
FV_SRC := sim/top.sby
|
||||||
|
|
||||||
|
BUILD_DIR := ./build
|
||||||
|
|
||||||
|
.PHONY: all burn fv clean
|
||||||
|
all: $(SIM_TARGET) $(BIN_TARGET)
|
||||||
|
|
||||||
|
# -GWIDTH=5 allows passing parameter to verilog module
|
||||||
|
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
||||||
|
@echo "Running verilator"
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(VERILATOR) --trace -Wall -GWIDTH=10 -cc $^ --top-module top\
|
||||||
|
--Mdir $(BUILD_DIR) --timescale-override 10ns/1ns
|
||||||
|
|
||||||
|
$(BUILD_DIR)/Vtop__ALL.a: $(BUILD_DIR)/Vtop.cc
|
||||||
|
@make --no-print-directory -C $(BUILD_DIR) -f Vtop.mk
|
||||||
|
|
||||||
|
# std=c++11 flag is needed as of verilator v4.100
|
||||||
|
$(SIM_TARGET): $(SIM_SRC) $(BUILD_DIR)/Vtop__ALL.a
|
||||||
|
@echo "Compiling simulation executable"
|
||||||
|
@g++ -I$(VINC) -I$(BUILD_DIR) -std=c++14 $(VINC)/verilated.cpp\
|
||||||
|
$(VINC)/verilated_vcd_c.cpp $^ -o $@
|
||||||
|
@echo "Run simulation with ./$(SIM_TARGET)"
|
||||||
|
|
||||||
|
$(BUILD_DIR)/top.json: $(RTL_SRC)
|
||||||
|
@echo "Synthesizing ..."
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
||||||
|
|
||||||
|
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF) $(TIMING)
|
||||||
|
@echo "Routing and building binary stream ..."
|
||||||
|
@$(PNR) -r --hx8k --json $< --package cb132 \
|
||||||
|
--asc $(BUILD_DIR)/top.asc --opt-timing --pcf $(PCF) \
|
||||||
|
--pre-pack $(TIMING) -l $(BUILD_DIR)/pnr_report.txt -q
|
||||||
|
@$(IPACK) $(BUILD_DIR)/top.asc $@
|
||||||
|
@echo "Done!"
|
||||||
|
|
||||||
|
burn: $(BIN_TARGET)
|
||||||
|
@$(BURN) $<
|
||||||
|
|
||||||
|
fv:
|
||||||
|
@$(SBY) -f $(FV_SRC) -d $(BUILD_DIR)/fv
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
14
fsm-tut4/constraints/iceFUN.pcf
Normal file
14
fsm-tut4/constraints/iceFUN.pcf
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# For iceFUN board
|
||||||
|
|
||||||
|
set_io --warn-no-port i_clk P7
|
||||||
|
set_io --warn-no-port i_request A5
|
||||||
|
|
||||||
|
set_io --warn-no-port o_led_row_0 A12
|
||||||
|
set_io --warn-no-port o_led[0] C10
|
||||||
|
set_io --warn-no-port o_led[1] A10
|
||||||
|
set_io --warn-no-port o_led[2] D7
|
||||||
|
set_io --warn-no-port o_led[3] D6
|
||||||
|
set_io --warn-no-port o_led[4] A7
|
||||||
|
set_io --warn-no-port o_led[5] C7
|
||||||
|
# set_io --warn-no-port o_led[6] A4
|
||||||
|
set_io --warn-no-port o_busy C4
|
||||||
1
fsm-tut4/constraints/timing.py
Normal file
1
fsm-tut4/constraints/timing.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ctx.addClock("i_clk", 100)
|
||||||
13
fsm-tut4/rtl/clk_gen.v
Normal file
13
fsm-tut4/rtl/clk_gen.v
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
`default_nettype none
|
||||||
|
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
||||||
|
module clk_gen(
|
||||||
|
input wire i_clk,
|
||||||
|
output wire o_clk
|
||||||
|
);
|
||||||
|
|
||||||
|
assign o_clk = i_clk;
|
||||||
|
|
||||||
|
endmodule
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
127
fsm-tut4/rtl/top.v
Normal file
127
fsm-tut4/rtl/top.v
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
`default_nettype none
|
||||||
|
|
||||||
|
module top(i_clk, o_led, o_led_row_0, i_request, o_busy);
|
||||||
|
parameter WIDTH = 22;
|
||||||
|
input wire i_clk;
|
||||||
|
output wire [5:0] o_led;
|
||||||
|
output wire o_led_row_0;
|
||||||
|
input wire i_request;
|
||||||
|
output wire o_busy;
|
||||||
|
|
||||||
|
wire clk_12MHz;
|
||||||
|
|
||||||
|
clk_gen clk_gen_0 (/*autoinst*/
|
||||||
|
// Outputs
|
||||||
|
.o_clk (clk_12MHz),
|
||||||
|
// Inputs
|
||||||
|
.i_clk (i_clk));
|
||||||
|
|
||||||
|
reg [WIDTH-1:0] counter;
|
||||||
|
reg [3:0] state;
|
||||||
|
reg [5:0] led_buf; // output buffer, take into account the icefun use active low LED
|
||||||
|
reg strobe;
|
||||||
|
reg busy_buf;
|
||||||
|
wire req_buf;
|
||||||
|
|
||||||
|
assign o_busy = ~busy_buf;
|
||||||
|
assign o_led = ~led_buf;
|
||||||
|
assign o_led_row_0 = 1'b0;
|
||||||
|
assign req_buf = ~i_request;
|
||||||
|
|
||||||
|
initial begin
|
||||||
|
led_buf = 6'h0;
|
||||||
|
{strobe, counter} = 0;
|
||||||
|
// counter = 0;
|
||||||
|
state = 0;
|
||||||
|
busy_buf = 0;
|
||||||
|
end
|
||||||
|
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (!busy_buf && req_buf)
|
||||||
|
busy_buf <= 1;
|
||||||
|
else
|
||||||
|
busy_buf <= (state != 4'h0);
|
||||||
|
end
|
||||||
|
// counter and strobe run only during busy signal is High
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (busy_buf)
|
||||||
|
// counter <= counter + 1'b1;
|
||||||
|
{strobe, counter} <= counter + 1'b1;
|
||||||
|
else
|
||||||
|
{strobe, counter} <= 0;
|
||||||
|
// counter <= 0;
|
||||||
|
end
|
||||||
|
|
||||||
|
// state change once strobe starts
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (!busy_buf && req_buf)
|
||||||
|
state <= 4'h1;
|
||||||
|
else if (state >= 4'hB && strobe)
|
||||||
|
state <= 4'h0;
|
||||||
|
else if (state != 0 && strobe)
|
||||||
|
state <= state + 1'b1;
|
||||||
|
end
|
||||||
|
|
||||||
|
// fsm for led_buf
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (strobe)
|
||||||
|
case (state)
|
||||||
|
4'h1: led_buf <= 6'b00_0001;
|
||||||
|
4'h2: led_buf <= 6'b00_0010;
|
||||||
|
4'h3: led_buf <= 6'b00_0100;
|
||||||
|
4'h4: led_buf <= 6'b00_1000;
|
||||||
|
4'h5: led_buf <= 6'b01_0000;
|
||||||
|
4'h6: led_buf <= 6'b10_0000;
|
||||||
|
4'h7: led_buf <= 6'b01_0000;
|
||||||
|
4'h8: led_buf <= 6'b00_1000;
|
||||||
|
4'h9: led_buf <= 6'b00_0100;
|
||||||
|
4'ha: led_buf <= 6'b00_0010;
|
||||||
|
4'hb: led_buf <= 6'b00_0001;
|
||||||
|
default: led_buf <= 6'b00_0000;
|
||||||
|
endcase
|
||||||
|
end
|
||||||
|
|
||||||
|
`ifdef FORMAL
|
||||||
|
// state should never go beyond 13
|
||||||
|
always @(*)
|
||||||
|
assert(state <= 4'hd);
|
||||||
|
|
||||||
|
// I prefix all of the registers (or wires) I use in formal
|
||||||
|
// verification with f_, to distinguish them from the rest of the
|
||||||
|
// project.
|
||||||
|
reg f_valid_output;
|
||||||
|
always @(*)
|
||||||
|
begin
|
||||||
|
// Determining if the output is valid or not is a rather
|
||||||
|
// complex task--unusual for a typical assertion. Here, we'll
|
||||||
|
// use f_valid_output and a series of _blocking_ statements
|
||||||
|
// to determine if the output is one of our valid outputs.
|
||||||
|
f_valid_output = 0;
|
||||||
|
|
||||||
|
case(led_buf)
|
||||||
|
8'h01: f_valid_output = 1'b1;
|
||||||
|
8'h02: f_valid_output = 1'b1;
|
||||||
|
8'h04: f_valid_output = 1'b1;
|
||||||
|
8'h08: f_valid_output = 1'b1;
|
||||||
|
8'h10: f_valid_output = 1'b1;
|
||||||
|
8'h20: f_valid_output = 1'b1;
|
||||||
|
8'h40: f_valid_output = 1'b1;
|
||||||
|
8'h80: f_valid_output = 1'b1;
|
||||||
|
endcase
|
||||||
|
|
||||||
|
assert(f_valid_output);
|
||||||
|
|
||||||
|
// SV supports a $onehot function which we could've also used
|
||||||
|
// depending upon your version of Yosys. This function will
|
||||||
|
// be true if one, and only one, bit in the argument is true.
|
||||||
|
// Hence we might have said
|
||||||
|
// assert($onehot(o_led));
|
||||||
|
// and avoided this case statement entirely.
|
||||||
|
end
|
||||||
|
`endif
|
||||||
|
|
||||||
|
endmodule
|
||||||
|
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
60
fsm-tut4/sim/top.cc
Normal file
60
fsm-tut4/sim/top.cc
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "verilated.h"
|
||||||
|
#include "verilated_vcd_c.h"
|
||||||
|
#include "Vtop.h"
|
||||||
|
|
||||||
|
void tick(int tickcount, Vtop *tb, VerilatedVcdC* tfp) {
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10 - 2);
|
||||||
|
tb->i_clk = 1;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10);
|
||||||
|
tb->i_clk = 0;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp) {
|
||||||
|
tfp->dump(tickcount * 10 + 5);
|
||||||
|
tfp->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
// Call commandArgs first!
|
||||||
|
Verilated::commandArgs(argc, argv);
|
||||||
|
|
||||||
|
// Instantiate our design
|
||||||
|
Vtop *tb = new Vtop;
|
||||||
|
Verilated::traceEverOn(true);
|
||||||
|
VerilatedVcdC* tfp = new VerilatedVcdC;
|
||||||
|
|
||||||
|
tb->trace(tfp, 99);
|
||||||
|
tfp->open("build/waveform.vcd");
|
||||||
|
|
||||||
|
int last_led, last_state = 0, state = 0;
|
||||||
|
printf("Initial state is: 0x%02x\n", tb->o_led);
|
||||||
|
unsigned tickcount = 0;
|
||||||
|
last_led = tb->o_led;
|
||||||
|
|
||||||
|
tb->i_request = 1;
|
||||||
|
|
||||||
|
for (int k = 0; k < 4; k++) {
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
tb->i_request = 0;
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
tb->i_request = 1;
|
||||||
|
|
||||||
|
for(int k=0; k<(1 << 16); k++) {
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
|
||||||
|
// if (last_led != tb->o_led) {
|
||||||
|
// printf("k = %7d, led = %d\n", k, tb->o_led);
|
||||||
|
// }
|
||||||
|
|
||||||
|
last_led = tb->o_led;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
fsm-tut4/sim/top.sby
Normal file
13
fsm-tut4/sim/top.sby
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
[options]
|
||||||
|
mode prove
|
||||||
|
|
||||||
|
[engines]
|
||||||
|
smtbmc
|
||||||
|
|
||||||
|
[script]
|
||||||
|
read -formal *.v
|
||||||
|
prep -top top
|
||||||
|
|
||||||
|
[files]
|
||||||
|
rtl/top.v
|
||||||
|
rtl/clk_gen.v
|
||||||
61
fsm/Makefile
Normal file
61
fsm/Makefile
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
SIM_TARGET = build/top
|
||||||
|
BIN_TARGET = build/top.bin
|
||||||
|
PCF = constraints/iceFUN.pcf
|
||||||
|
TIMING = constraints/timing.py
|
||||||
|
YOSYS = yosys
|
||||||
|
PNR = nextpnr-ice40
|
||||||
|
IPACK = icepack
|
||||||
|
BURN = iceFUNprog
|
||||||
|
SBY = sby
|
||||||
|
|
||||||
|
VERILATOR=verilator
|
||||||
|
VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e "s/^.*=\s*//"')
|
||||||
|
VINC := $(VERILATOR_ROOT)/include
|
||||||
|
|
||||||
|
RTL_SRC := $(wildcard rtl/*.v)
|
||||||
|
SIM_SRC := $(wildcard sim/*.cc)
|
||||||
|
FV_SRC := sim/top.sby
|
||||||
|
|
||||||
|
BUILD_DIR := ./build
|
||||||
|
|
||||||
|
.PHONY: all burn fv clean
|
||||||
|
all: $(SIM_TARGET) $(BIN_TARGET)
|
||||||
|
|
||||||
|
# -GWIDTH=5 allows passing parameter to verilog module
|
||||||
|
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
||||||
|
@echo "Running verilator"
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(VERILATOR) --trace -Wall -GWIDTH=10 -cc $^ --top-module top\
|
||||||
|
--Mdir $(BUILD_DIR) --timescale-override 10ns/1ns
|
||||||
|
|
||||||
|
$(BUILD_DIR)/Vtop__ALL.a: $(BUILD_DIR)/Vtop.cc
|
||||||
|
@make --no-print-directory -C $(BUILD_DIR) -f Vtop.mk
|
||||||
|
|
||||||
|
# std=c++11 flag is needed as of verilator v4.100
|
||||||
|
$(SIM_TARGET): $(SIM_SRC) $(BUILD_DIR)/Vtop__ALL.a
|
||||||
|
@echo "Compiling simulation executable"
|
||||||
|
@g++ -I$(VINC) -I$(BUILD_DIR) -std=c++14 $(VINC)/verilated.cpp\
|
||||||
|
$(VINC)/verilated_vcd_c.cpp $^ -o $@
|
||||||
|
@echo "Run simulation with ./$(SIM_TARGET)"
|
||||||
|
|
||||||
|
$(BUILD_DIR)/top.json: $(RTL_SRC)
|
||||||
|
@echo "Synthesizing ..."
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
||||||
|
|
||||||
|
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF) $(TIMING)
|
||||||
|
@echo "Routing and building binary stream ..."
|
||||||
|
@$(PNR) -r --hx8k --json $< --package cb132 \
|
||||||
|
--asc $(BUILD_DIR)/top.asc --opt-timing --pcf $(PCF) \
|
||||||
|
--pre-pack $(TIMING) -l $(BUILD_DIR)/pnr_report.txt -q
|
||||||
|
@$(IPACK) $(BUILD_DIR)/top.asc $@
|
||||||
|
@echo "Done!"
|
||||||
|
|
||||||
|
burn: $(BIN_TARGET)
|
||||||
|
@$(BURN) $<
|
||||||
|
|
||||||
|
fv:
|
||||||
|
@$(SBY) -f $(FV_SRC) -d $(BUILD_DIR)/fv
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
12
fsm/constraints/iceFUN.pcf
Normal file
12
fsm/constraints/iceFUN.pcf
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# For iceFUN board
|
||||||
|
|
||||||
|
set_io --warn-no-port i_clk P7
|
||||||
|
set_io --warn-no-port lcol1 A12
|
||||||
|
set_io --warn-no-port o_led[0] C10
|
||||||
|
set_io --warn-no-port o_led[1] A10
|
||||||
|
set_io --warn-no-port o_led[2] D7
|
||||||
|
set_io --warn-no-port o_led[3] D6
|
||||||
|
set_io --warn-no-port o_led[4] A7
|
||||||
|
set_io --warn-no-port o_led[5] C7
|
||||||
|
set_io --warn-no-port o_led[6] A4
|
||||||
|
set_io --warn-no-port o_led[7] C4
|
||||||
1
fsm/constraints/timing.py
Normal file
1
fsm/constraints/timing.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ctx.addClock("i_clk", 100)
|
||||||
13
fsm/rtl/clk_gen.v
Normal file
13
fsm/rtl/clk_gen.v
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
`default_nettype none
|
||||||
|
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
||||||
|
module clk_gen(
|
||||||
|
input wire i_clk,
|
||||||
|
output wire o_clk
|
||||||
|
);
|
||||||
|
|
||||||
|
assign o_clk = i_clk;
|
||||||
|
|
||||||
|
endmodule
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
115
fsm/rtl/top.v
Normal file
115
fsm/rtl/top.v
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
`default_nettype none
|
||||||
|
|
||||||
|
module top(i_clk, o_led, lcol1);
|
||||||
|
parameter WIDTH = 22;
|
||||||
|
input wire i_clk;
|
||||||
|
output reg [7:0] o_led;
|
||||||
|
output wire lcol1;
|
||||||
|
|
||||||
|
wire clk_12MHz;
|
||||||
|
|
||||||
|
clk_gen clk_gen_0 (/*autoinst*/
|
||||||
|
// Outputs
|
||||||
|
.o_clk (clk_12MHz),
|
||||||
|
// Inputs
|
||||||
|
.i_clk (i_clk));
|
||||||
|
|
||||||
|
reg [WIDTH-1:0] counter;
|
||||||
|
reg [7:0] obuf; // output buffer, take into account the icefun use active low LED
|
||||||
|
reg [3:0] led_index;
|
||||||
|
reg strobe;
|
||||||
|
|
||||||
|
initial begin
|
||||||
|
obuf = 8'h1;
|
||||||
|
{strobe, counter} = 0;
|
||||||
|
led_index = 0;
|
||||||
|
end
|
||||||
|
|
||||||
|
always @(posedge clk_12MHz)
|
||||||
|
{strobe, counter} <= counter + 1'b1;
|
||||||
|
|
||||||
|
// fsm
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (strobe) // led_index change only when strobe is 1
|
||||||
|
if (led_index >= 4'hd)
|
||||||
|
led_index <= 0;
|
||||||
|
else
|
||||||
|
led_index <= led_index + 1'b1;
|
||||||
|
|
||||||
|
case (led_index)
|
||||||
|
4'h0: obuf <= 8'h01;
|
||||||
|
4'h1: obuf <= 8'h02;
|
||||||
|
4'h2: obuf <= 8'h04;
|
||||||
|
4'h3: obuf <= 8'h08;
|
||||||
|
4'h4: obuf <= 8'h10;
|
||||||
|
4'h5: obuf <= 8'h20;
|
||||||
|
4'h6: obuf <= 8'h40;
|
||||||
|
4'h7: obuf <= 8'h80;
|
||||||
|
4'h8: obuf <= 8'h40;
|
||||||
|
4'h9: obuf <= 8'h20;
|
||||||
|
4'ha: obuf <= 8'h10;
|
||||||
|
4'hb: obuf <= 8'h08;
|
||||||
|
4'hc: obuf <= 8'h04;
|
||||||
|
4'hd: obuf <= 8'h02;
|
||||||
|
default : begin
|
||||||
|
obuf <= 8'h01;
|
||||||
|
end
|
||||||
|
endcase
|
||||||
|
end
|
||||||
|
|
||||||
|
`ifdef FORMAL
|
||||||
|
// led_index should never go beyond 13
|
||||||
|
always @(*)
|
||||||
|
assert(led_index <= 4'hd);
|
||||||
|
|
||||||
|
// I prefix all of the registers (or wires) I use in formal
|
||||||
|
// verification with f_, to distinguish them from the rest of the
|
||||||
|
// project.
|
||||||
|
reg f_valid_output;
|
||||||
|
always @(*)
|
||||||
|
begin
|
||||||
|
// Determining if the output is valid or not is a rather
|
||||||
|
// complex task--unusual for a typical assertion. Here, we'll
|
||||||
|
// use f_valid_output and a series of _blocking_ statements
|
||||||
|
// to determine if the output is one of our valid outputs.
|
||||||
|
f_valid_output = 0;
|
||||||
|
|
||||||
|
case(obuf)
|
||||||
|
8'h01: f_valid_output = 1'b1;
|
||||||
|
8'h02: f_valid_output = 1'b1;
|
||||||
|
8'h04: f_valid_output = 1'b1;
|
||||||
|
8'h08: f_valid_output = 1'b1;
|
||||||
|
8'h10: f_valid_output = 1'b1;
|
||||||
|
8'h20: f_valid_output = 1'b1;
|
||||||
|
8'h40: f_valid_output = 1'b1;
|
||||||
|
8'h80: f_valid_output = 1'b1;
|
||||||
|
endcase
|
||||||
|
|
||||||
|
assert(f_valid_output);
|
||||||
|
|
||||||
|
// SV supports a $onehot function which we could've also used
|
||||||
|
// depending upon your version of Yosys. This function will
|
||||||
|
// be true if one, and only one, bit in the argument is true.
|
||||||
|
// Hence we might have said
|
||||||
|
// assert($onehot(o_led));
|
||||||
|
// and avoided this case statement entirely.
|
||||||
|
end
|
||||||
|
`endif
|
||||||
|
/* shift reg
|
||||||
|
// shifting bit
|
||||||
|
always @(posedge clk_12MHz)
|
||||||
|
if (strobe)
|
||||||
|
obuf <= {obuf[6:0], obuf[7]}; // left shift
|
||||||
|
// obuf <= {obuf[0], obuf[7:1]}; // right shift
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
always @(posedge clk_12MHz)
|
||||||
|
o_led <= ~obuf;
|
||||||
|
|
||||||
|
assign lcol1 = 1'b0;
|
||||||
|
endmodule
|
||||||
|
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
47
fsm/sim/top.cc
Normal file
47
fsm/sim/top.cc
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "verilated.h"
|
||||||
|
#include "verilated_vcd_c.h"
|
||||||
|
#include "Vtop.h"
|
||||||
|
|
||||||
|
void tick(int tickcount, Vtop *tb, VerilatedVcdC* tfp) {
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10 - 2);
|
||||||
|
tb->i_clk = 1;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10);
|
||||||
|
tb->i_clk = 0;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp) {
|
||||||
|
tfp->dump(tickcount * 10 + 5);
|
||||||
|
tfp->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
// Call commandArgs first!
|
||||||
|
Verilated::commandArgs(argc, argv);
|
||||||
|
|
||||||
|
// Instantiate our design
|
||||||
|
Vtop *tb = new Vtop;
|
||||||
|
Verilated::traceEverOn(true);
|
||||||
|
VerilatedVcdC* tfp = new VerilatedVcdC;
|
||||||
|
|
||||||
|
tb->trace(tfp, 00);
|
||||||
|
tfp->open("build/waveform.vcd");
|
||||||
|
|
||||||
|
unsigned tickcount = 0;
|
||||||
|
int last_led = tb->o_led;
|
||||||
|
|
||||||
|
for(int k=0; k<(1 << 16); k++) {
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
|
||||||
|
// if (last_led != tb->o_led) {
|
||||||
|
// printf("k = %7d, led = %d\n", k, tb->o_led);
|
||||||
|
// }
|
||||||
|
|
||||||
|
last_led = tb->o_led;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
fsm/sim/top.sby
Normal file
13
fsm/sim/top.sby
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
[options]
|
||||||
|
mode prove
|
||||||
|
|
||||||
|
[engines]
|
||||||
|
smtbmc
|
||||||
|
|
||||||
|
[script]
|
||||||
|
read -formal *.v
|
||||||
|
prep -top top
|
||||||
|
|
||||||
|
[files]
|
||||||
|
rtl/top.v
|
||||||
|
rtl/clk_gen.v
|
||||||
54
tdc/Makefile
Normal file
54
tdc/Makefile
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
SIM_TARGET = build/top
|
||||||
|
BIN_TARGET = build/top.bin
|
||||||
|
PCF = iceFUN.pcf
|
||||||
|
YOSYS = yosys
|
||||||
|
PNR = nextpnr-ice40
|
||||||
|
IPACK = icepack
|
||||||
|
BURN = iceFUNprog
|
||||||
|
|
||||||
|
VERILATOR=verilator
|
||||||
|
VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e "s/^.*=\s*//"')
|
||||||
|
VINC := $(VERILATOR_ROOT)/include
|
||||||
|
|
||||||
|
RTL_SRC := $(wildcard rtl/*.v)
|
||||||
|
SIM_SRC := $(wildcard sim/*.cc)
|
||||||
|
BUILD_DIR := ./build
|
||||||
|
|
||||||
|
.PHONY: all burn
|
||||||
|
all: $(SIM_TARGET) $(BIN_TARGET)
|
||||||
|
|
||||||
|
# -GWIDTH=5 allows passing parameter to verilog module
|
||||||
|
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
||||||
|
@echo "Running verilator"
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(VERILATOR) --trace -Wall -GWIDTH=10 -cc $^ --top-module top\
|
||||||
|
--Mdir $(BUILD_DIR) --timescale-override 10ns/1ns
|
||||||
|
|
||||||
|
$(BUILD_DIR)/Vtop__ALL.a: $(BUILD_DIR)/Vtop.cc
|
||||||
|
@make --no-print-directory -C $(BUILD_DIR) -f Vtop.mk
|
||||||
|
|
||||||
|
# std=c++11 flag is needed as of verilator v4.100
|
||||||
|
$(SIM_TARGET): $(SIM_SRC) $(BUILD_DIR)/Vtop__ALL.a
|
||||||
|
@echo "Compiling simulation executable"
|
||||||
|
@g++ -I$(VINC) -I$(BUILD_DIR) -std=c++14 $(VINC)/verilated.cpp\
|
||||||
|
$(VINC)/verilated_vcd_c.cpp $^ -o $@
|
||||||
|
@echo "Run simulation with ./$(SIM_TARGET)"
|
||||||
|
|
||||||
|
$(BUILD_DIR)/top.json: $(RTL_SRC)
|
||||||
|
@echo "Synthesizing ..."
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
||||||
|
|
||||||
|
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF)
|
||||||
|
@echo "Routing and building binary stream ..."
|
||||||
|
@$(PNR) -r --hx8k --json $< --package cb132 \
|
||||||
|
--asc $(BUILD_DIR)/top.asc --opt-timing --pcf $(PCF) -q
|
||||||
|
@$(IPACK) $(BUILD_DIR)/top.asc $@
|
||||||
|
@echo "Done!"
|
||||||
|
|
||||||
|
burn: $(BIN_TARGET)
|
||||||
|
@$(BURN) $<
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
5
tdc/iceFUN.pcf
Normal file
5
tdc/iceFUN.pcf
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# For iceFUN board
|
||||||
|
|
||||||
|
set_io --warn-no-port o_led C10
|
||||||
|
set_io --warn-no-port i_clk P7
|
||||||
|
set_io --warn-no-port lcol1 A12
|
||||||
13
tdc/rtl/clk_gen.v
Normal file
13
tdc/rtl/clk_gen.v
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
`default_nettype none
|
||||||
|
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
||||||
|
module clk_gen(
|
||||||
|
input wire i_clk,
|
||||||
|
output wire o_clk
|
||||||
|
);
|
||||||
|
|
||||||
|
assign o_clk = i_clk;
|
||||||
|
|
||||||
|
endmodule
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
28
tdc/rtl/top.v
Normal file
28
tdc/rtl/top.v
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
`default_nettype none
|
||||||
|
|
||||||
|
module top(i_clk, o_led, lcol1);
|
||||||
|
parameter WIDTH = 24;
|
||||||
|
input wire i_clk;
|
||||||
|
output wire o_led;
|
||||||
|
output wire lcol1;
|
||||||
|
|
||||||
|
wire clk_12MHz;
|
||||||
|
|
||||||
|
clk_gen clk_gen_0 (/*autoinst*/
|
||||||
|
// Outputs
|
||||||
|
.o_clk (clk_12MHz),
|
||||||
|
// Inputs
|
||||||
|
.i_clk (i_clk));
|
||||||
|
|
||||||
|
reg [WIDTH-1:0] counter;
|
||||||
|
|
||||||
|
always @(posedge clk_12MHz)
|
||||||
|
counter <= counter + 1'b1;
|
||||||
|
|
||||||
|
assign o_led = counter[WIDTH-1];
|
||||||
|
assign lcol1 = 1'b0;
|
||||||
|
endmodule
|
||||||
|
|
||||||
|
// Local Variables:
|
||||||
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
// End:
|
||||||
47
tdc/sim/top.cc
Normal file
47
tdc/sim/top.cc
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "verilated.h"
|
||||||
|
#include "verilated_vcd_c.h"
|
||||||
|
#include "Vtop.h"
|
||||||
|
|
||||||
|
void tick(int tickcount, Vtop *tb, VerilatedVcdC* tfp) {
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10 - 2);
|
||||||
|
tb->i_clk = 1;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp)
|
||||||
|
tfp->dump(tickcount * 10);
|
||||||
|
tb->i_clk = 0;
|
||||||
|
tb->eval();
|
||||||
|
if (tfp) {
|
||||||
|
tfp->dump(tickcount * 10 + 5);
|
||||||
|
tfp->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
// Call commandArgs first!
|
||||||
|
Verilated::commandArgs(argc, argv);
|
||||||
|
|
||||||
|
// Instantiate our design
|
||||||
|
Vtop *tb = new Vtop;
|
||||||
|
Verilated::traceEverOn(true);
|
||||||
|
VerilatedVcdC* tfp = new VerilatedVcdC;
|
||||||
|
|
||||||
|
tb->trace(tfp, 00);
|
||||||
|
tfp->open("build/waveform.vcd");
|
||||||
|
|
||||||
|
unsigned tickcount = 0;
|
||||||
|
int last_led = tb->o_led;
|
||||||
|
|
||||||
|
for(int k=0; k<(1 << 12); k++) {
|
||||||
|
tick(++tickcount, tb, tfp);
|
||||||
|
|
||||||
|
if (last_led != tb->o_led) {
|
||||||
|
printf("k = %7d, led = %d\n", k, tb->o_led);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_led = tb->o_led;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user