Compare commits
1 Commits
ec6b3431be
...
tut4-with-
| Author | SHA1 | Date | |
|---|---|---|---|
| 556a79e705 |
@@ -5,25 +5,7 @@ module clk_gen(
|
|||||||
output wire o_clk
|
output wire o_clk
|
||||||
);
|
);
|
||||||
|
|
||||||
// assign o_clk = i_clk;
|
assign o_clk = i_clk;
|
||||||
reg [31:0] counter;
|
|
||||||
reg buf_clk;
|
|
||||||
parameter CLK_RATE_HZ = 12_000_000;
|
|
||||||
|
|
||||||
initial begin
|
|
||||||
counter = 0;
|
|
||||||
buf_clk = 0;
|
|
||||||
end
|
|
||||||
assign o_clk = buf_clk;
|
|
||||||
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
if (counter >= CLK_RATE_HZ/2 - 1) begin
|
|
||||||
counter <= 0;
|
|
||||||
buf_clk <= ~buf_clk;
|
|
||||||
end
|
|
||||||
else
|
|
||||||
counter <= counter + 1;
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
endmodule
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
|
|||||||
@@ -8,17 +8,18 @@ module top(i_clk, o_led, o_led_row_0, i_request, o_busy);
|
|||||||
input wire i_request;
|
input wire i_request;
|
||||||
output wire o_busy;
|
output wire o_busy;
|
||||||
|
|
||||||
wire clk_1Hz;
|
wire clk_12MHz;
|
||||||
|
|
||||||
clk_gen clk_gen_0 (/*autoinst*/
|
clk_gen clk_gen_0 (/*autoinst*/
|
||||||
// Outputs
|
// Outputs
|
||||||
.o_clk (clk_1Hz),
|
.o_clk (clk_12MHz),
|
||||||
// Inputs
|
// Inputs
|
||||||
.i_clk (i_clk));
|
.i_clk (i_clk));
|
||||||
|
|
||||||
reg [WIDTH-1:0] counter;
|
reg [WIDTH-1:0] counter;
|
||||||
reg [3:0] state;
|
reg [3:0] state;
|
||||||
reg [5:0] led_buf; // output buffer, take into account the icefun use active low LED
|
reg [5:0] led_buf; // output buffer, take into account the icefun use active low LED
|
||||||
|
reg strobe;
|
||||||
reg busy_buf;
|
reg busy_buf;
|
||||||
wire req_buf;
|
wire req_buf;
|
||||||
|
|
||||||
@@ -29,36 +30,41 @@ module top(i_clk, o_led, o_led_row_0, i_request, o_busy);
|
|||||||
|
|
||||||
initial begin
|
initial begin
|
||||||
led_buf = 6'h0;
|
led_buf = 6'h0;
|
||||||
counter = 0;
|
{strobe, counter} = 0;
|
||||||
|
// counter = 0;
|
||||||
state = 0;
|
state = 0;
|
||||||
busy_buf = 0;
|
busy_buf = 0;
|
||||||
end
|
end
|
||||||
|
|
||||||
always @(posedge clk_1Hz) begin
|
always @(posedge clk_12MHz) begin
|
||||||
if (!busy_buf && req_buf)
|
if (!busy_buf && req_buf)
|
||||||
busy_buf <= 1;
|
busy_buf <= 1;
|
||||||
else
|
else
|
||||||
busy_buf <= (state != 4'h0);
|
busy_buf <= (state != 4'h0);
|
||||||
end
|
end
|
||||||
// counter and strobe run only during busy signal is High
|
// counter and strobe run only during busy signal is High
|
||||||
always @(posedge clk_1Hz) begin
|
always @(posedge clk_12MHz) begin
|
||||||
if (busy_buf)
|
if (busy_buf)
|
||||||
counter <= counter + 1'b1;
|
// counter <= counter + 1'b1;
|
||||||
|
{strobe, counter} <= counter + 1'b1;
|
||||||
else
|
else
|
||||||
counter <= 0;
|
{strobe, counter} <= 0;
|
||||||
|
// counter <= 0;
|
||||||
end
|
end
|
||||||
|
|
||||||
always @(posedge clk_1Hz) begin
|
// state change once strobe starts
|
||||||
|
always @(posedge clk_12MHz) begin
|
||||||
if (!busy_buf && req_buf)
|
if (!busy_buf && req_buf)
|
||||||
state <= 4'h1;
|
state <= 4'h1;
|
||||||
else if (state >= 4'hB)
|
else if (state >= 4'hB && strobe)
|
||||||
state <= 4'h0;
|
state <= 4'h0;
|
||||||
else if (state != 0)
|
else if (state != 0 && strobe)
|
||||||
state <= state + 1'b1;
|
state <= state + 1'b1;
|
||||||
end
|
end
|
||||||
|
|
||||||
// fsm for led_buf
|
// fsm for led_buf
|
||||||
always @(posedge clk_1Hz) begin
|
always @(posedge clk_12MHz) begin
|
||||||
|
if (strobe)
|
||||||
case (state)
|
case (state)
|
||||||
4'h1: led_buf <= 6'b00_0001;
|
4'h1: led_buf <= 6'b00_0001;
|
||||||
4'h2: led_buf <= 6'b00_0010;
|
4'h2: led_buf <= 6'b00_0010;
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
define colorecho
|
|
||||||
@tput setaf 6
|
|
||||||
@echo $1
|
|
||||||
@tput sgr0
|
|
||||||
endef
|
|
||||||
|
|
||||||
.PHONY: all burn fv clean sim
|
|
||||||
all: $(SIM_TARGET) $(BIN_TARGET)
|
|
||||||
|
|
||||||
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
|
||||||
$(call colorecho, "Running verilator")
|
|
||||||
mkdir -p $(BUILD_DIR)
|
|
||||||
$(VERILATOR) --trace -Wall -cc $^ --top-module top -GWIDTH=10\
|
|
||||||
--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
|
|
||||||
$(call colorecho, "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)
|
|
||||||
$(call colorecho, "Synthesizing ...")
|
|
||||||
mkdir -p $(BUILD_DIR)
|
|
||||||
$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
|
||||||
|
|
||||||
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF) $(TIMING)
|
|
||||||
$(call colorecho, "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 $@
|
|
||||||
$(call colorecho, "Done!")
|
|
||||||
|
|
||||||
sim: $(SIM_TARGET)
|
|
||||||
$(call colorecho, "Running simulation")
|
|
||||||
$(SIM_TARGET) && open $(BUILD_DIR)/waveform.vcd
|
|
||||||
|
|
||||||
burn: $(BIN_TARGET)
|
|
||||||
$(BURN) $<
|
|
||||||
|
|
||||||
fv:
|
|
||||||
$(SBY) -f $(FV_SRC) -d $(BUILD_DIR)/fv
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf $(BUILD_DIR)
|
|
||||||
|
|
||||||
$V.SILENT:
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
# For iceFUN board
|
|
||||||
|
|
||||||
set_io --warn-no-port i_clk P7
|
|
||||||
# set_io --warn-no-port i_start_tx C11
|
|
||||||
# set_io --warn-no-port i_stopN A11
|
|
||||||
# set_io --warn-no-port i_resetN C6
|
|
||||||
|
|
||||||
set_io --warn-no-port o_uart_tx A3
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
ctx.addClock("i_clk", 100)
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
`default_nettype none
|
|
||||||
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
|
||||||
module clk_gen #(parameter DIVISION=22)(
|
|
||||||
input wire i_clk,
|
|
||||||
output wire o_clk_100MHz,
|
|
||||||
output wire o_div_clk
|
|
||||||
);
|
|
||||||
|
|
||||||
/* verilator lint_off PINCONNECTEMPTY */
|
|
||||||
/* verilator lint_off PINMISSING */
|
|
||||||
reg [DIVISION-1:0] counter = 0;
|
|
||||||
`ifdef VERILATOR
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
counter <= counter + 1;
|
|
||||||
end
|
|
||||||
|
|
||||||
assign o_clk_100MHz = i_clk;
|
|
||||||
`else
|
|
||||||
pll_100MHz pll0(.i_clk(i_clk), .o_clk_100MHz(o_clk_100MHz), .o_pll_locked());
|
|
||||||
always @(posedge o_clk_100MHz) begin
|
|
||||||
counter <= counter + 1;
|
|
||||||
end
|
|
||||||
`endif
|
|
||||||
|
|
||||||
assign o_div_clk = counter[DIVISION-1];
|
|
||||||
/* verilator lint_on PINCONNECTEMPTY */
|
|
||||||
/* verilator lint_on PINMISSING */
|
|
||||||
endmodule
|
|
||||||
// Local Variables:
|
|
||||||
// verilog-library-directories:(".." "./rtl" ".")
|
|
||||||
// End:
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* PLL configuration
|
|
||||||
*
|
|
||||||
* This Verilog module was generated automatically
|
|
||||||
* using the icepll tool from the IceStorm project.
|
|
||||||
* Use at your own risk.
|
|
||||||
*
|
|
||||||
* Given input frequency: 12.000 MHz
|
|
||||||
* Requested output frequency: 100.000 MHz
|
|
||||||
* Achieved output frequency: 100.500 MHz
|
|
||||||
*/
|
|
||||||
// this module is skipped by verilator
|
|
||||||
`ifdef VERILATOR
|
|
||||||
`else
|
|
||||||
module pll_100MHz(
|
|
||||||
input i_clk,
|
|
||||||
output o_clk_100MHz,
|
|
||||||
output o_pll_locked
|
|
||||||
);
|
|
||||||
|
|
||||||
wire clk_int;
|
|
||||||
SB_PLL40_CORE #(
|
|
||||||
.FEEDBACK_PATH("SIMPLE"),
|
|
||||||
.DIVR(4'b0000), // DIVR = 0
|
|
||||||
.DIVF(7'b1000010), // DIVF = 66
|
|
||||||
.DIVQ(3'b011), // DIVQ = 3
|
|
||||||
.FILTER_RANGE(3'b001) // FILTER_RANGE = 1
|
|
||||||
) uut (
|
|
||||||
.LOCK(o_pll_locked),
|
|
||||||
.RESETB(1'b1),
|
|
||||||
.BYPASS(1'b0),
|
|
||||||
.REFERENCECLK(i_clk),
|
|
||||||
.PLLOUTCORE(clk_int)
|
|
||||||
);
|
|
||||||
|
|
||||||
SB_GB sbGlobalBuffer_inst( .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_int),
|
|
||||||
.GLOBAL_BUFFER_OUTPUT(o_clk_100MHz));
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
`endif
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
`default_nettype none
|
|
||||||
|
|
||||||
module top #(parameter WIDTH=24)(
|
|
||||||
input wire i_clk,
|
|
||||||
output wire o_uart_tx
|
|
||||||
);
|
|
||||||
parameter CLOCK_RATE_HZ = 100_000_000; // 100MHz clock
|
|
||||||
parameter BAUD_RATE = 115_200; // 115.2 KBaud
|
|
||||||
parameter INITIAL_UART_SETUP = (CLOCK_RATE_HZ/BAUD_RATE);
|
|
||||||
|
|
||||||
wire clk_100MHz;
|
|
||||||
|
|
||||||
/* verilator lint_off PINMISSING */
|
|
||||||
clk_gen #(.DIVISION(26)) clk_gen0 (.o_clk_100MHz (clk_100MHz), .i_clk (i_clk));
|
|
||||||
/* verilator lint_on PINMISSING */
|
|
||||||
|
|
||||||
reg tx_restart = 0;
|
|
||||||
reg [27:0] hz_counter;
|
|
||||||
|
|
||||||
initial hz_counter = 28'h16;
|
|
||||||
always @(posedge clk_100MHz) begin
|
|
||||||
if (hz_counter == 0)
|
|
||||||
hz_counter <= CLOCK_RATE_HZ - 1'b1;
|
|
||||||
else
|
|
||||||
hz_counter <= hz_counter - 1'b1;
|
|
||||||
end
|
|
||||||
|
|
||||||
always @(posedge clk_100MHz)
|
|
||||||
tx_restart <= (hz_counter == 1);
|
|
||||||
|
|
||||||
wire tx_busy;
|
|
||||||
reg tx_stb;
|
|
||||||
reg [3:0] tx_index;
|
|
||||||
reg [7:0] tx_data;
|
|
||||||
|
|
||||||
initial tx_index = 4'h0;
|
|
||||||
always @(posedge clk_100MHz) begin
|
|
||||||
if ((tx_stb)&&(!tx_busy))
|
|
||||||
tx_index <= tx_index + 1'b1;
|
|
||||||
end
|
|
||||||
|
|
||||||
always @(posedge clk_100MHz) begin
|
|
||||||
case(tx_index)
|
|
||||||
4'h0: tx_data <= "H";
|
|
||||||
4'h1: tx_data <= "e";
|
|
||||||
4'h2: tx_data <= "l";
|
|
||||||
4'h3: tx_data <= "l";
|
|
||||||
//
|
|
||||||
4'h4: tx_data <= "o";
|
|
||||||
4'h5: tx_data <= ",";
|
|
||||||
4'h6: tx_data <= " ";
|
|
||||||
4'h7: tx_data <= "W";
|
|
||||||
//
|
|
||||||
4'h8: tx_data <= "o";
|
|
||||||
4'h9: tx_data <= "r";
|
|
||||||
4'ha: tx_data <= "l";
|
|
||||||
4'hb: tx_data <= "d";
|
|
||||||
//
|
|
||||||
4'hc: tx_data <= "!";
|
|
||||||
4'hd: tx_data <= " ";
|
|
||||||
4'he: tx_data <= "\n";
|
|
||||||
4'hf: tx_data <= "\r";
|
|
||||||
//
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
// tx_stb is a request to send a character.
|
|
||||||
initial tx_stb = 1'b0;
|
|
||||||
always @(posedge clk_100MHz) begin
|
|
||||||
if (&tx_restart)
|
|
||||||
tx_stb <= 1'b1;
|
|
||||||
else if ((tx_stb)&&(!tx_busy)&&(tx_index==4'hf))
|
|
||||||
tx_stb <= 1'b0;
|
|
||||||
end
|
|
||||||
|
|
||||||
//
|
|
||||||
// Instantiate a serial port module here
|
|
||||||
//
|
|
||||||
txuart #(INITIAL_UART_SETUP[23:0])
|
|
||||||
transmitter(clk_100MHz, tx_stb, tx_data, o_uart_tx, tx_busy);
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
// Local Variables:
|
|
||||||
// verilog-library-directories:(".." "./rtl" ".")
|
|
||||||
// End:
|
|
||||||
@@ -1,267 +0,0 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Filename: txuart.v
|
|
||||||
//
|
|
||||||
// Project: Verilog Tutorial Example file
|
|
||||||
//
|
|
||||||
// Purpose: Transmit outputs over a single UART line. This particular UART
|
|
||||||
// implementation has been extremely simplified: it does not handle
|
|
||||||
// generating break conditions, nor does it handle anything other than the
|
|
||||||
// 8N1 (8 data bits, no parity, 1 stop bit) UART sub-protocol.
|
|
||||||
//
|
|
||||||
// To interface with this module, connect it to your system clock, and
|
|
||||||
// pass it the byte of data you wish to transmit. Strobe the i_wr line
|
|
||||||
// high for one cycle, and your data will be off. Wait until the 'o_busy'
|
|
||||||
// line is low before strobing the i_wr line again--this implementation
|
|
||||||
// has NO BUFFER, so strobing i_wr while the core is busy will just
|
|
||||||
// get ignored. The output will be placed on the o_txuart output line.
|
|
||||||
//
|
|
||||||
// There are known deficiencies in the formal proof found within this
|
|
||||||
// module. These have been left behind for you (the student) to fix.
|
|
||||||
//
|
|
||||||
// Creator: Dan Gisselquist, Ph.D.
|
|
||||||
// Gisselquist Technology, LLC
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Written and distributed by Gisselquist Technology, LLC
|
|
||||||
//
|
|
||||||
// This program is hereby granted to the public domain.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
//
|
|
||||||
`default_nettype none
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
module txuart(i_clk, i_wr, i_data, o_uart_tx, o_busy);
|
|
||||||
parameter [23:0] CLOCKS_PER_BAUD = 24'd868;
|
|
||||||
input wire i_clk;
|
|
||||||
input wire i_wr;
|
|
||||||
input wire [7:0] i_data;
|
|
||||||
// And the UART output line itself
|
|
||||||
output wire o_uart_tx;
|
|
||||||
// A line to tell others when we are ready to accept data. If
|
|
||||||
// (i_wr)&&(!o_busy) is ever true, then the core has accepted a byte
|
|
||||||
// for transmission.
|
|
||||||
output reg o_busy;
|
|
||||||
|
|
||||||
// Define several states
|
|
||||||
localparam [3:0] START = 4'h0,
|
|
||||||
BIT_ZERO = 4'h1,
|
|
||||||
BIT_ONE = 4'h2,
|
|
||||||
BIT_TWO = 4'h3,
|
|
||||||
BIT_THREE = 4'h4,
|
|
||||||
BIT_FOUR = 4'h5,
|
|
||||||
BIT_FIVE = 4'h6,
|
|
||||||
BIT_SIX = 4'h7,
|
|
||||||
BIT_SEVEN = 4'h8,
|
|
||||||
LAST = 4'h8,
|
|
||||||
IDLE = 4'hf;
|
|
||||||
|
|
||||||
reg [23:0] counter;
|
|
||||||
reg [3:0] state;
|
|
||||||
reg [8:0] lcl_data;
|
|
||||||
reg baud_stb;
|
|
||||||
|
|
||||||
// o_busy
|
|
||||||
//
|
|
||||||
// This is a register, designed to be true is we are ever busy above.
|
|
||||||
// originally, this was going to be true if we were ever not in the
|
|
||||||
// idle state. The logic has since become more complex, hence we have
|
|
||||||
// a register dedicated to this and just copy out that registers value.
|
|
||||||
|
|
||||||
initial o_busy = 1'b0;
|
|
||||||
initial state = IDLE;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((i_wr)&&(!o_busy))
|
|
||||||
// Immediately start us off with a start bit
|
|
||||||
{ o_busy, state } <= { 1'b1, START };
|
|
||||||
else if (baud_stb)
|
|
||||||
begin
|
|
||||||
if (state == IDLE) // Stay in IDLE
|
|
||||||
{ o_busy, state } <= { 1'b0, IDLE };
|
|
||||||
else if (state < LAST) begin
|
|
||||||
o_busy <= 1'b1;
|
|
||||||
state <= state + 1'b1;
|
|
||||||
end else // Wait for IDLE
|
|
||||||
{ o_busy, state } <= { 1'b1, IDLE };
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// lcl_data
|
|
||||||
//
|
|
||||||
// This is our working copy of the i_data register which we use
|
|
||||||
// when transmitting. It is only of interest during transmit, and is
|
|
||||||
// allowed to be whatever at any other time. Hence, if o_busy isn't
|
|
||||||
// true, we can always set it. On the one clock where o_busy isn't
|
|
||||||
// true and i_wr is, we set it and o_busy is true thereafter.
|
|
||||||
// Then, on any baud_stb (i.e. change between baud intervals)
|
|
||||||
// we simple logically shift the register right to grab the next bit.
|
|
||||||
initial lcl_data = 9'h1ff;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((i_wr)&&(!o_busy))
|
|
||||||
lcl_data <= { i_data, 1'b0 };
|
|
||||||
else if (baud_stb)
|
|
||||||
lcl_data <= { 1'b1, lcl_data[8:1] };
|
|
||||||
|
|
||||||
// o_uart_tx
|
|
||||||
//
|
|
||||||
// This is the final result/output desired of this core. It's all
|
|
||||||
// centered about o_uart_tx. This is what finally needs to follow
|
|
||||||
// the UART protocol.
|
|
||||||
//
|
|
||||||
assign o_uart_tx = lcl_data[0];
|
|
||||||
|
|
||||||
|
|
||||||
// All of the above logic is driven by the baud counter. Bits must last
|
|
||||||
// CLOCKS_PER_BAUD in length, and this baud counter is what we use to
|
|
||||||
// make certain of that.
|
|
||||||
//
|
|
||||||
// The basic logic is this: at the beginning of a bit interval, start
|
|
||||||
// the baud counter and set it to count CLOCKS_PER_BAUD. When it gets
|
|
||||||
// to zero, restart it.
|
|
||||||
//
|
|
||||||
// However, comparing a 28'bit number to zero can be rather complex--
|
|
||||||
// especially if we wish to do anything else on that same clock. For
|
|
||||||
// that reason, we create "baud_stb". baud_stb is
|
|
||||||
// nothing more than a flag that is true anytime baud_counter is zero.
|
|
||||||
// It's true when the logic (above) needs to step to the next bit.
|
|
||||||
// Simple enough?
|
|
||||||
//
|
|
||||||
// I wish we could stop there, but there are some other (ugly)
|
|
||||||
// conditions to deal with that offer exceptions to this basic logic.
|
|
||||||
//
|
|
||||||
// 1. When the user has commanded a BREAK across the line, we need to
|
|
||||||
// wait several baud intervals following the break before we start
|
|
||||||
// transmitting, to give any receiver a chance to recognize that we are
|
|
||||||
// out of the break condition, and to know that the next bit will be
|
|
||||||
// a stop bit.
|
|
||||||
//
|
|
||||||
// 2. A reset is similar to a break condition--on both we wait several
|
|
||||||
// baud intervals before allowing a start bit.
|
|
||||||
//
|
|
||||||
// 3. In the idle state, we stop our counter--so that upon a request
|
|
||||||
// to transmit when idle we can start transmitting immediately, rather
|
|
||||||
// than waiting for the end of the next (fictitious and arbitrary) baud
|
|
||||||
// interval.
|
|
||||||
//
|
|
||||||
// When (i_wr)&&(!o_busy)&&(state == IDLE) then we're not only in
|
|
||||||
// the idle state, but we also just accepted a command to start writing
|
|
||||||
// the next word. At this point, the baud counter needs to be reset
|
|
||||||
// to the number of CLOCKS_PER_BAUD, and baud_stb set to zero.
|
|
||||||
//
|
|
||||||
// The logic is a bit twisted here, in that it will only check for the
|
|
||||||
// above condition when baud_stb is false--so as to make
|
|
||||||
// certain the STOP bit is complete.
|
|
||||||
initial baud_stb = 1'b1;
|
|
||||||
initial counter = 0;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((i_wr)&&(!o_busy))
|
|
||||||
begin
|
|
||||||
counter <= CLOCKS_PER_BAUD - 1'b1;
|
|
||||||
baud_stb <= 1'b0;
|
|
||||||
end else if (!baud_stb)
|
|
||||||
begin
|
|
||||||
baud_stb <= (counter == 24'h01);
|
|
||||||
counter <= counter - 1'b1;
|
|
||||||
end else if (state != IDLE)
|
|
||||||
begin
|
|
||||||
counter <= CLOCKS_PER_BAUD - 1'b1;
|
|
||||||
baud_stb <= 1'b0;
|
|
||||||
end
|
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// FORMAL METHODS
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
`ifdef FORMAL
|
|
||||||
|
|
||||||
`ifdef TXUART
|
|
||||||
`define ASSUME assume
|
|
||||||
`else
|
|
||||||
`define ASSUME assert
|
|
||||||
`endif
|
|
||||||
|
|
||||||
// Setup
|
|
||||||
|
|
||||||
reg f_past_valid;
|
|
||||||
|
|
||||||
initial f_past_valid = 1'b0;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
f_past_valid <= 1'b1;
|
|
||||||
|
|
||||||
// Any outstanding request that was busy on the last cycle,
|
|
||||||
// should remain busy on this cycle
|
|
||||||
initial `ASSUME(!i_wr);
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((f_past_valid)&&($past(i_wr))&&($past(o_busy)))
|
|
||||||
begin
|
|
||||||
`ASSUME(i_wr == $past(i_wr));
|
|
||||||
`ASSUME(i_data == $past(i_data));
|
|
||||||
end
|
|
||||||
|
|
||||||
//////////////////////////////////
|
|
||||||
//
|
|
||||||
// The contract
|
|
||||||
//
|
|
||||||
//////////////////////////////////
|
|
||||||
|
|
||||||
reg [7:0] fv_data;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((i_wr)&&(!o_busy))
|
|
||||||
fv_data <= i_data;
|
|
||||||
|
|
||||||
always @(posedge i_clk)
|
|
||||||
case(state)
|
|
||||||
IDLE: assert(o_uart_tx == 1'b1);
|
|
||||||
START: assert(o_uart_tx == 1'b0);
|
|
||||||
BIT_ZERO: assert(o_uart_tx == fv_data[0]);
|
|
||||||
BIT_ONE: assert(o_uart_tx == fv_data[1]);
|
|
||||||
BIT_TWO: assert(o_uart_tx == fv_data[2]);
|
|
||||||
BIT_THREE: assert(o_uart_tx == fv_data[3]);
|
|
||||||
BIT_FOUR: assert(o_uart_tx == fv_data[4]);
|
|
||||||
BIT_FIVE: assert(o_uart_tx == fv_data[5]);
|
|
||||||
BIT_SIX: assert(o_uart_tx == fv_data[6]);
|
|
||||||
BIT_SEVEN: assert(o_uart_tx == fv_data[7]);
|
|
||||||
default: assert(0);
|
|
||||||
endcase
|
|
||||||
|
|
||||||
//////////////////////////////////
|
|
||||||
//
|
|
||||||
// Internal state checks
|
|
||||||
//
|
|
||||||
//////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Check the baud counter
|
|
||||||
//
|
|
||||||
|
|
||||||
// The baud_stb needs to be identical to our counter being zero
|
|
||||||
always @(posedge i_clk)
|
|
||||||
assert(baud_stb == (counter == 0));
|
|
||||||
|
|
||||||
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if ((f_past_valid)&&($past(counter != 0)))
|
|
||||||
assert(counter == $past(counter - 1'b1));
|
|
||||||
|
|
||||||
always @(posedge i_clk)
|
|
||||||
assert(counter < CLOCKS_PER_BAUD);
|
|
||||||
|
|
||||||
always @(posedge i_clk)
|
|
||||||
if (!baud_stb)
|
|
||||||
assert(o_busy);
|
|
||||||
|
|
||||||
`endif // FORMAL
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#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;
|
|
||||||
for (int k = 0; k < (1<<18); k++)
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
|
|
||||||
}
|
|
||||||
60
tdc/Makefile
60
tdc/Makefile
@@ -1,12 +1,10 @@
|
|||||||
SIM_TARGET = build/top
|
SIM_TARGET = build/top
|
||||||
BIN_TARGET = build/top.bin
|
BIN_TARGET = build/top.bin
|
||||||
PCF = constraints/iceFUN.pcf
|
PCF = iceFUN.pcf
|
||||||
TIMING = constraints/timing.py
|
|
||||||
YOSYS = yosys
|
YOSYS = yosys
|
||||||
PNR = nextpnr-ice40
|
PNR = nextpnr-ice40
|
||||||
IPACK = icepack
|
IPACK = icepack
|
||||||
BURN = iceFUNprog
|
BURN = iceFUNprog
|
||||||
SBY = sby
|
|
||||||
|
|
||||||
VERILATOR=verilator
|
VERILATOR=verilator
|
||||||
VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e "s/^.*=\s*//"')
|
VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e "s/^.*=\s*//"')
|
||||||
@@ -14,59 +12,43 @@ VINC := $(VERILATOR_ROOT)/include
|
|||||||
|
|
||||||
RTL_SRC := $(wildcard rtl/*.v)
|
RTL_SRC := $(wildcard rtl/*.v)
|
||||||
SIM_SRC := $(wildcard sim/*.cc)
|
SIM_SRC := $(wildcard sim/*.cc)
|
||||||
FV_SRC := sim/top.sby
|
|
||||||
|
|
||||||
BUILD_DIR := ./build
|
BUILD_DIR := ./build
|
||||||
|
|
||||||
define colorecho
|
.PHONY: all burn
|
||||||
@tput setaf 6
|
|
||||||
@echo $1
|
|
||||||
@tput sgr0
|
|
||||||
endef
|
|
||||||
|
|
||||||
.PHONY: all burn fv clean sim
|
|
||||||
all: $(SIM_TARGET) $(BIN_TARGET)
|
all: $(SIM_TARGET) $(BIN_TARGET)
|
||||||
|
|
||||||
|
# -GWIDTH=5 allows passing parameter to verilog module
|
||||||
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
||||||
$(call colorecho, "Running verilator")
|
@echo "Running verilator"
|
||||||
mkdir -p $(BUILD_DIR)
|
@mkdir -p $(BUILD_DIR)
|
||||||
$(VERILATOR) --trace -Wall -cc $^ --top-module top -GWIDTH=10\
|
@$(VERILATOR) --trace -Wall -GWIDTH=10 -cc $^ --top-module top\
|
||||||
--Mdir $(BUILD_DIR) --timescale-override 10ns/1ns
|
--Mdir $(BUILD_DIR) --timescale-override 10ns/1ns
|
||||||
|
|
||||||
$(BUILD_DIR)/Vtop__ALL.a: $(BUILD_DIR)/Vtop.cc
|
$(BUILD_DIR)/Vtop__ALL.a: $(BUILD_DIR)/Vtop.cc
|
||||||
make --no-print-directory -C $(BUILD_DIR) -f Vtop.mk
|
@make --no-print-directory -C $(BUILD_DIR) -f Vtop.mk
|
||||||
|
|
||||||
# std=c++11 flag is needed as of verilator v4.100
|
# std=c++11 flag is needed as of verilator v4.100
|
||||||
$(SIM_TARGET): $(SIM_SRC) $(BUILD_DIR)/Vtop__ALL.a
|
$(SIM_TARGET): $(SIM_SRC) $(BUILD_DIR)/Vtop__ALL.a
|
||||||
$(call colorecho, "Compiling simulation executable")
|
@echo "Compiling simulation executable"
|
||||||
g++ -I$(VINC) -I$(BUILD_DIR) -std=c++14 $(VINC)/verilated.cpp\
|
@g++ -I$(VINC) -I$(BUILD_DIR) -std=c++14 $(VINC)/verilated.cpp\
|
||||||
$(VINC)/verilated_vcd_c.cpp $^ -o $@
|
$(VINC)/verilated_vcd_c.cpp $^ -o $@
|
||||||
echo "Run simulation with ./$(SIM_TARGET)"
|
@echo "Run simulation with ./$(SIM_TARGET)"
|
||||||
|
|
||||||
$(BUILD_DIR)/top.json: $(RTL_SRC)
|
$(BUILD_DIR)/top.json: $(RTL_SRC)
|
||||||
$(call colorecho, "Synthesizing ...")
|
@echo "Synthesizing ..."
|
||||||
mkdir -p $(BUILD_DIR)
|
@mkdir -p $(BUILD_DIR)
|
||||||
$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
@$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
||||||
|
|
||||||
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF) $(TIMING)
|
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF)
|
||||||
$(call colorecho, "Routing and building binary stream ...")
|
@echo "Routing and building binary stream ..."
|
||||||
$(PNR) -r --hx8k --json $< --package cb132 \
|
@$(PNR) -r --hx8k --json $< --package cb132 \
|
||||||
--asc $(BUILD_DIR)/top.asc --opt-timing --pcf $(PCF) \
|
--asc $(BUILD_DIR)/top.asc --opt-timing --pcf $(PCF) -q
|
||||||
--pre-pack $(TIMING) -l $(BUILD_DIR)/pnr_report.txt -q
|
@$(IPACK) $(BUILD_DIR)/top.asc $@
|
||||||
$(IPACK) $(BUILD_DIR)/top.asc $@
|
@echo "Done!"
|
||||||
$(call colorecho, "Done!")
|
|
||||||
|
|
||||||
sim: $(SIM_TARGET)
|
|
||||||
$(call colorecho, "Running simulation")
|
|
||||||
$(SIM_TARGET) && open $(BUILD_DIR)/waveform.vcd
|
|
||||||
|
|
||||||
burn: $(BIN_TARGET)
|
burn: $(BIN_TARGET)
|
||||||
$(BURN) $<
|
@$(BURN) $<
|
||||||
|
|
||||||
fv:
|
|
||||||
$(SBY) -f $(FV_SRC) -d $(BUILD_DIR)/fv
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD_DIR)
|
rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
$V.SILENT:
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
# For iceFUN board
|
|
||||||
|
|
||||||
set_io --warn-no-port i_clk P7
|
|
||||||
set_io --warn-no-port i_startN C11
|
|
||||||
set_io --warn-no-port i_stopN A11
|
|
||||||
set_io --warn-no-port i_resetN C6
|
|
||||||
|
|
||||||
set_io --warn-no-port o_led_row_0 A12
|
|
||||||
set_io --warn-no-port o_dataN[0] C10
|
|
||||||
set_io --warn-no-port o_dataN[1] A10
|
|
||||||
set_io --warn-no-port o_dataN[2] D7
|
|
||||||
set_io --warn-no-port o_dataN[3] D6
|
|
||||||
set_io --warn-no-port o_dataN[4] A7
|
|
||||||
set_io --warn-no-port o_dataN[5] C7
|
|
||||||
|
|
||||||
set_io --warn-no-port o_ledN A4
|
|
||||||
set_io --warn-no-port o_readyN C4
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 171 KiB |
@@ -1 +0,0 @@
|
|||||||
ctx.addClock("i_clk", 100)
|
|
||||||
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
|
||||||
@@ -1,30 +1,12 @@
|
|||||||
`default_nettype none
|
`default_nettype none
|
||||||
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
// dummy clock generator, should be replaced by a PLL clock gen eventually
|
||||||
module clk_gen #(parameter DIVISION=22)(
|
module clk_gen(
|
||||||
input wire i_clk,
|
input wire i_clk,
|
||||||
output wire o_clk_100MHz,
|
output wire o_clk
|
||||||
output wire o_div_clk
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/* verilator lint_off PINCONNECTEMPTY */
|
assign o_clk = i_clk;
|
||||||
/* verilator lint_off PINMISSING */
|
|
||||||
reg [DIVISION-1:0] counter = 0;
|
|
||||||
`ifdef VERILATOR
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
counter <= counter + 1;
|
|
||||||
end
|
|
||||||
|
|
||||||
assign o_clk_100MHz = i_clk;
|
|
||||||
`else
|
|
||||||
pll_100MHz pll0(.i_clk(i_clk), .o_clk_100MHz(o_clk_100MHz), .o_pll_locked());
|
|
||||||
always @(posedge o_clk_100MHz) begin
|
|
||||||
counter <= counter + 1;
|
|
||||||
end
|
|
||||||
`endif
|
|
||||||
|
|
||||||
assign o_div_clk = counter[DIVISION-1];
|
|
||||||
/* verilator lint_on PINCONNECTEMPTY */
|
|
||||||
/* verilator lint_on PINMISSING */
|
|
||||||
endmodule
|
endmodule
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// verilog-library-directories:(".." "./rtl" ".")
|
// verilog-library-directories:(".." "./rtl" ".")
|
||||||
|
|||||||
@@ -1,115 +0,0 @@
|
|||||||
// Listing 5.6
|
|
||||||
module debounce
|
|
||||||
(
|
|
||||||
input wire clk, reset,
|
|
||||||
input wire sw,
|
|
||||||
output reg db
|
|
||||||
);
|
|
||||||
|
|
||||||
// symbolic state declaration
|
|
||||||
localparam [2:0]
|
|
||||||
zero = 3'b000,
|
|
||||||
wait1_1 = 3'b001,
|
|
||||||
wait1_2 = 3'b010,
|
|
||||||
wait1_3 = 3'b011,
|
|
||||||
one = 3'b100,
|
|
||||||
wait0_1 = 3'b101,
|
|
||||||
wait0_2 = 3'b110,
|
|
||||||
wait0_3 = 3'b111;
|
|
||||||
|
|
||||||
// number of counter bits (2^N * 10ns = 10ms tick)
|
|
||||||
localparam N =20;
|
|
||||||
|
|
||||||
// signal declaration
|
|
||||||
reg [N-1:0] q_reg;
|
|
||||||
wire [N-1:0] q_next;
|
|
||||||
wire m_tick;
|
|
||||||
reg [2:0] state_reg, state_next;
|
|
||||||
|
|
||||||
// body
|
|
||||||
|
|
||||||
//=============================================
|
|
||||||
// counter to generate 10 ms tick
|
|
||||||
//=============================================
|
|
||||||
always @(posedge clk)
|
|
||||||
q_reg <= q_next;
|
|
||||||
// next-state logic
|
|
||||||
assign q_next = q_reg + 1;
|
|
||||||
// output tick
|
|
||||||
assign m_tick = (q_reg==0) ? 1'b1 : 1'b0;
|
|
||||||
|
|
||||||
//=============================================
|
|
||||||
// debouncing FSM
|
|
||||||
//=============================================
|
|
||||||
// state register
|
|
||||||
always @(posedge clk, posedge reset)
|
|
||||||
if (reset)
|
|
||||||
state_reg <= zero;
|
|
||||||
else
|
|
||||||
state_reg <= state_next;
|
|
||||||
|
|
||||||
// next-state logic and output logic
|
|
||||||
always @*
|
|
||||||
begin
|
|
||||||
state_next = state_reg; // default state: the same
|
|
||||||
db = 1'b0; // default output: 0
|
|
||||||
case (state_reg)
|
|
||||||
zero:
|
|
||||||
if (sw)
|
|
||||||
state_next = wait1_1;
|
|
||||||
wait1_1:
|
|
||||||
if (~sw)
|
|
||||||
state_next = zero;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = wait1_2;
|
|
||||||
wait1_2:
|
|
||||||
if (~sw)
|
|
||||||
state_next = zero;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = wait1_3;
|
|
||||||
wait1_3:
|
|
||||||
if (~sw)
|
|
||||||
state_next = zero;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = one;
|
|
||||||
one:
|
|
||||||
begin
|
|
||||||
db = 1'b1;
|
|
||||||
if (~sw)
|
|
||||||
state_next = wait0_1;
|
|
||||||
end
|
|
||||||
wait0_1:
|
|
||||||
begin
|
|
||||||
db = 1'b1;
|
|
||||||
if (sw)
|
|
||||||
state_next = one;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = wait0_2;
|
|
||||||
end
|
|
||||||
wait0_2:
|
|
||||||
begin
|
|
||||||
db = 1'b1;
|
|
||||||
if (sw)
|
|
||||||
state_next = one;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = wait0_3;
|
|
||||||
end
|
|
||||||
wait0_3:
|
|
||||||
begin
|
|
||||||
db = 1'b1;
|
|
||||||
if (sw)
|
|
||||||
state_next = one;
|
|
||||||
else
|
|
||||||
if (m_tick)
|
|
||||||
state_next = zero;
|
|
||||||
end
|
|
||||||
default: state_next = zero;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* PLL configuration
|
|
||||||
*
|
|
||||||
* This Verilog module was generated automatically
|
|
||||||
* using the icepll tool from the IceStorm project.
|
|
||||||
* Use at your own risk.
|
|
||||||
*
|
|
||||||
* Given input frequency: 12.000 MHz
|
|
||||||
* Requested output frequency: 100.000 MHz
|
|
||||||
* Achieved output frequency: 100.500 MHz
|
|
||||||
*/
|
|
||||||
// this module is skipped by verilator
|
|
||||||
`ifdef VERILATOR
|
|
||||||
`else
|
|
||||||
module pll_100MHz(
|
|
||||||
input i_clk,
|
|
||||||
output o_clk_100MHz,
|
|
||||||
output o_pll_locked
|
|
||||||
);
|
|
||||||
|
|
||||||
wire clk_int;
|
|
||||||
SB_PLL40_CORE #(
|
|
||||||
.FEEDBACK_PATH("SIMPLE"),
|
|
||||||
.DIVR(4'b0000), // DIVR = 0
|
|
||||||
.DIVF(7'b1000010), // DIVF = 66
|
|
||||||
.DIVQ(3'b011), // DIVQ = 3
|
|
||||||
.FILTER_RANGE(3'b001) // FILTER_RANGE = 1
|
|
||||||
) uut (
|
|
||||||
.LOCK(o_pll_locked),
|
|
||||||
.RESETB(1'b1),
|
|
||||||
.BYPASS(1'b0),
|
|
||||||
.REFERENCECLK(i_clk),
|
|
||||||
.PLLOUTCORE(clk_int)
|
|
||||||
);
|
|
||||||
|
|
||||||
SB_GB sbGlobalBuffer_inst( .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_int),
|
|
||||||
.GLOBAL_BUFFER_OUTPUT(o_clk_100MHz));
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
`endif
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
`default_nettype none
|
|
||||||
|
|
||||||
module tdc #(parameter COUNTER_WIDTH=16)(
|
|
||||||
input wire i_clk,
|
|
||||||
input wire i_start,
|
|
||||||
input wire i_stop,
|
|
||||||
input wire i_reset,
|
|
||||||
output wire o_ready,
|
|
||||||
output wire [COUNTER_WIDTH-1:0] o_data
|
|
||||||
);
|
|
||||||
|
|
||||||
reg [COUNTER_WIDTH-1:0] counter;
|
|
||||||
assign o_data = counter;
|
|
||||||
|
|
||||||
// states
|
|
||||||
localparam state_idle = 2'b00;
|
|
||||||
localparam state_started = 2'b01;
|
|
||||||
localparam state_running = 2'b10;
|
|
||||||
localparam state_stopped = 2'b11;
|
|
||||||
reg [1:0] current_state, next_state;
|
|
||||||
|
|
||||||
// ensure that state changes each clock
|
|
||||||
always @(posedge i_clk, posedge i_reset) begin
|
|
||||||
if (i_reset) begin
|
|
||||||
current_state <= state_idle;
|
|
||||||
end else begin
|
|
||||||
current_state <= next_state;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// state logic
|
|
||||||
/* verilator lint_off COMBDLY */
|
|
||||||
always @(*) begin
|
|
||||||
case (current_state)
|
|
||||||
state_idle: begin
|
|
||||||
if (i_start && (~i_stop))
|
|
||||||
next_state <= state_started;
|
|
||||||
else
|
|
||||||
next_state <= state_idle;
|
|
||||||
end
|
|
||||||
state_started: begin
|
|
||||||
if (~i_start && (~i_stop))
|
|
||||||
next_state <= state_running;
|
|
||||||
else
|
|
||||||
next_state <= state_started;
|
|
||||||
end
|
|
||||||
state_running: begin
|
|
||||||
if (~i_start && (i_stop))
|
|
||||||
next_state <= state_stopped;
|
|
||||||
else
|
|
||||||
next_state <= state_running;
|
|
||||||
end
|
|
||||||
state_stopped: begin
|
|
||||||
if (i_reset)
|
|
||||||
next_state <= state_idle;
|
|
||||||
else
|
|
||||||
next_state <= state_stopped;
|
|
||||||
end
|
|
||||||
|
|
||||||
default : next_state <= current_state;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
/* verilator lint_on COMBDLY */
|
|
||||||
|
|
||||||
// counter runs during running state only
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
case (current_state)
|
|
||||||
state_idle: counter <= 0;
|
|
||||||
state_started: counter <= 0;
|
|
||||||
state_running: counter <= counter + 1;
|
|
||||||
state_stopped: counter <= counter;
|
|
||||||
default : counter <= 0;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
assign o_ready = (current_state == state_stopped);
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
// Local Variables:
|
|
||||||
// verilog-library-directories:(".." "./rtl" ".")
|
|
||||||
// End:
|
|
||||||
@@ -1,65 +1,26 @@
|
|||||||
`default_nettype none
|
`default_nettype none
|
||||||
|
|
||||||
module top #(parameter WIDTH=24)(
|
module top(i_clk, o_led, lcol1);
|
||||||
input wire i_clk,
|
parameter WIDTH = 24;
|
||||||
input wire i_startN,
|
input wire i_clk;
|
||||||
input wire i_stopN,
|
output wire o_led;
|
||||||
input wire i_resetN,
|
output wire lcol1;
|
||||||
output wire o_ledN,
|
|
||||||
output wire o_readyN,
|
|
||||||
output wire [5:0] o_dataN,
|
|
||||||
output wire o_led_row_0
|
|
||||||
);
|
|
||||||
wire clk_1Hz; // 1.4 Hz actually
|
|
||||||
wire clk_100MHz;
|
|
||||||
reg buf_led = 0;
|
|
||||||
wire buf_ready;
|
|
||||||
/* verilator lint_off UNUSED */
|
|
||||||
parameter TDC_COUNTER_WIDTH = 28;
|
|
||||||
wire [TDC_COUNTER_WIDTH-1:0] buf_data;
|
|
||||||
assign o_readyN = ~buf_ready;
|
|
||||||
assign o_dataN = ~buf_data[TDC_COUNTER_WIDTH-1:TDC_COUNTER_WIDTH-6];
|
|
||||||
/* verilator lint_on UNUSED */
|
|
||||||
|
|
||||||
/* verilator lint_off PINMISSING */
|
wire clk_12MHz;
|
||||||
clk_gen #(.DIVISION(26)) clk_gen0 (
|
|
||||||
.o_div_clk (clk_1Hz),
|
clk_gen clk_gen_0 (/*autoinst*/
|
||||||
.o_clk_100MHz (clk_100MHz),
|
// Outputs
|
||||||
|
.o_clk (clk_12MHz),
|
||||||
|
// Inputs
|
||||||
.i_clk (i_clk));
|
.i_clk (i_clk));
|
||||||
/* verilator lint_on PINMISSING */
|
|
||||||
|
|
||||||
reg db_start, db_stop;
|
reg [WIDTH-1:0] counter;
|
||||||
debounce db1 (
|
|
||||||
// Outputs
|
|
||||||
.db (db_start),
|
|
||||||
// Inputs
|
|
||||||
.clk (clk_100MHz),
|
|
||||||
.reset (~i_resetN),
|
|
||||||
.sw (~i_startN));
|
|
||||||
debounce db2 (
|
|
||||||
// Outputs
|
|
||||||
.db (db_stop),
|
|
||||||
// Inputs
|
|
||||||
.clk (clk_100MHz),
|
|
||||||
.reset (~i_resetN),
|
|
||||||
.sw (~i_stopN));
|
|
||||||
|
|
||||||
tdc #(.COUNTER_WIDTH(TDC_COUNTER_WIDTH)) tdc0 (
|
always @(posedge clk_12MHz)
|
||||||
// Outputs
|
counter <= counter + 1'b1;
|
||||||
.o_ready (buf_ready),
|
|
||||||
.o_data (buf_data),
|
|
||||||
// Inputs
|
|
||||||
.i_clk (clk_100MHz),
|
|
||||||
.i_start (db_start),
|
|
||||||
.i_stop (db_stop),
|
|
||||||
.i_reset (~i_resetN));
|
|
||||||
|
|
||||||
always @(posedge clk_1Hz) begin
|
assign o_led = counter[WIDTH-1];
|
||||||
buf_led <= ~buf_led;
|
assign lcol1 = 1'b0;
|
||||||
end
|
|
||||||
|
|
||||||
assign o_ledN = ~buf_led;
|
|
||||||
assign o_led_row_0 = 1'b0;
|
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
|
|||||||
@@ -32,41 +32,16 @@ int main(int argc, char **argv) {
|
|||||||
tb->trace(tfp, 00);
|
tb->trace(tfp, 00);
|
||||||
tfp->open("build/waveform.vcd");
|
tfp->open("build/waveform.vcd");
|
||||||
|
|
||||||
tb->i_resetN = 1;
|
|
||||||
tb->i_startN = 1;
|
|
||||||
tb->i_stopN = 1;
|
|
||||||
unsigned tickcount = 0;
|
unsigned tickcount = 0;
|
||||||
for (int k = 0; k < 2; k++)
|
int last_led = tb->o_led;
|
||||||
|
|
||||||
|
for(int k=0; k<(1 << 12); k++) {
|
||||||
tick(++tickcount, tb, tfp);
|
tick(++tickcount, tb, tfp);
|
||||||
|
|
||||||
tb->i_resetN = 0;
|
if (last_led != tb->o_led) {
|
||||||
tick(++tickcount, tb, tfp);
|
printf("k = %7d, led = %d\n", k, tb->o_led);
|
||||||
tb->i_resetN = 1;
|
|
||||||
|
|
||||||
for (int k = 0; k < 3; k++)
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
tb->i_startN = 0;
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
tb->i_startN = 1;
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int k = 0; k < 15; k++)
|
last_led = tb->o_led;
|
||||||
tick(++tickcount, tb, tfp);
|
}
|
||||||
|
|
||||||
tb->i_stopN = 0;
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
tb->i_stopN = 1;
|
|
||||||
|
|
||||||
for (int k = 0; k < 3; k++)
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
|
|
||||||
tb->i_resetN = 0;
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
tb->i_resetN = 1;
|
|
||||||
|
|
||||||
for (int k = 0; k < 3; k++)
|
|
||||||
tick(++tickcount, tb, tfp);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
define colorecho
|
|
||||||
@tput setaf 6
|
|
||||||
@echo $1
|
|
||||||
@tput sgr0
|
|
||||||
endef
|
|
||||||
|
|
||||||
.PHONY: all burn fv clean
|
|
||||||
all: $(SIM_TARGET) $(BIN_TARGET)
|
|
||||||
|
|
||||||
$(BUILD_DIR)/Vtop.cc: $(RTL_SRC)
|
|
||||||
$(call colorecho, "Running verilator")
|
|
||||||
mkdir -p $(BUILD_DIR)
|
|
||||||
$(VERILATOR) --trace -Wall -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
|
|
||||||
$(call colorecho, "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)
|
|
||||||
$(call colorecho, "Synthesizing ...")
|
|
||||||
mkdir -p $(BUILD_DIR)
|
|
||||||
$(YOSYS) -p "synth_ice40 -top top -json build/top.json" -q $^
|
|
||||||
|
|
||||||
$(BIN_TARGET): $(BUILD_DIR)/top.json $(PCF) $(TIMING)
|
|
||||||
$(call colorecho, "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 $@
|
|
||||||
$(call colorecho, "Done!")
|
|
||||||
|
|
||||||
burn: $(BIN_TARGET)
|
|
||||||
$(BURN) $<
|
|
||||||
|
|
||||||
fv:
|
|
||||||
$(SBY) -f $(FV_SRC) -d $(BUILD_DIR)/fv
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf $(BUILD_DIR)
|
|
||||||
|
|
||||||
$V.SILENT:
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# 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 +0,0 @@
|
|||||||
ctx.addClock("i_clk", 100)
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
`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:
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
`default_nettype none
|
|
||||||
|
|
||||||
module top(i_clk,
|
|
||||||
i_cyc, i_stb, i_we, i_addr, i_data,
|
|
||||||
o_stall, o_ack, o_data,
|
|
||||||
o_led, o_led_row_0);
|
|
||||||
input wire i_clk;
|
|
||||||
//
|
|
||||||
// Our wishbone bus interface
|
|
||||||
input wire i_cyc, i_stb, i_we;
|
|
||||||
input wire i_addr;
|
|
||||||
input wire [31:0] i_data;
|
|
||||||
//
|
|
||||||
output wire o_stall;
|
|
||||||
output reg o_ack;
|
|
||||||
output wire [31:0] o_data;
|
|
||||||
//
|
|
||||||
// The output LED
|
|
||||||
output wire o_led_row_0;
|
|
||||||
output reg [5:0] o_led;
|
|
||||||
|
|
||||||
wire busy;
|
|
||||||
reg [3:0] state;
|
|
||||||
|
|
||||||
initial state = 0;
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
if ((i_stb)&&(i_we)&&(!o_stall))
|
|
||||||
state <= 4'h1;
|
|
||||||
else if (state >= 4'd11)
|
|
||||||
state <= 4'h0;
|
|
||||||
else if (state != 0)
|
|
||||||
state <= state + 1'b1;
|
|
||||||
end
|
|
||||||
|
|
||||||
always @(posedge i_clk) begin
|
|
||||||
case(state)
|
|
||||||
4'h1: o_led <= 6'b00_0001;
|
|
||||||
4'h2: o_led <= 6'b00_0010;
|
|
||||||
4'h3: o_led <= 6'b00_0100;
|
|
||||||
4'h4: o_led <= 6'b00_1000;
|
|
||||||
4'h5: o_led <= 6'b01_0000;
|
|
||||||
4'h6: o_led <= 6'b10_0000;
|
|
||||||
4'h7: o_led <= 6'b01_0000;
|
|
||||||
4'h8: o_led <= 6'b00_1000;
|
|
||||||
4'h9: o_led <= 6'b00_0100;
|
|
||||||
4'ha: o_led <= 6'b00_0010;
|
|
||||||
4'hb: o_led <= 6'b00_0001;
|
|
||||||
default: o_led <= 6'b00_0000;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
assign busy = (state != 0);
|
|
||||||
|
|
||||||
initial o_ack = 1'b0;
|
|
||||||
always @(posedge i_clk)
|
|
||||||
o_ack <= (i_stb)&&(!o_stall);
|
|
||||||
|
|
||||||
assign o_stall = (busy)&&(i_we);
|
|
||||||
assign o_data = { 28'h0, state };
|
|
||||||
assign o_led_row_0 = 0;
|
|
||||||
|
|
||||||
// Verilator lint_off UNUSED
|
|
||||||
wire [33:0] unused;
|
|
||||||
assign unused = { i_cyc, i_addr, i_data };
|
|
||||||
// Verilator lint_on UNUSED
|
|
||||||
//
|
|
||||||
endmodule
|
|
||||||
// Local Variables:
|
|
||||||
// verilog-library-directories:(".." "./rtl" ".")
|
|
||||||
// End:
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "Vtop.h"
|
|
||||||
#include "verilated.h"
|
|
||||||
#include "verilated_vcd_c.h"
|
|
||||||
|
|
||||||
int tickcount = 0;
|
|
||||||
Vtop *tb;
|
|
||||||
VerilatedVcdC *tfp;
|
|
||||||
|
|
||||||
void tick(void) {
|
|
||||||
tickcount++;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned wb_read(unsigned a) {
|
|
||||||
tb->i_cyc = tb->i_stb = 1;
|
|
||||||
tb->i_we = 0;
|
|
||||||
tb->eval();
|
|
||||||
tb->i_addr= a;
|
|
||||||
// Make the request
|
|
||||||
while(tb->o_stall)
|
|
||||||
tick();
|
|
||||||
tick();
|
|
||||||
tb->i_stb = 0;
|
|
||||||
// Wait for the ACK
|
|
||||||
while(!tb->o_ack)
|
|
||||||
tick();
|
|
||||||
// Idle the bus, and read the response
|
|
||||||
tb->i_cyc = 0;
|
|
||||||
return tb->o_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wb_write(unsigned a, unsigned v) {
|
|
||||||
tb->i_cyc = tb->i_stb = 1;
|
|
||||||
tb->i_we = 1;
|
|
||||||
tb->eval();
|
|
||||||
tb->i_addr= a;
|
|
||||||
tb->i_data= v;
|
|
||||||
// if busy, keep ticking
|
|
||||||
while(tb->o_stall)
|
|
||||||
tick();
|
|
||||||
// Then, make the bus request
|
|
||||||
tick();
|
|
||||||
// and pull stb down
|
|
||||||
tb->i_stb = 0;
|
|
||||||
// Wait for the acknowledgement
|
|
||||||
while(!tb->o_ack)
|
|
||||||
tick();
|
|
||||||
// Idle the bus and return
|
|
||||||
tb->i_cyc = tb->i_stb = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
int last_led, last_state = 0, state = 0;
|
|
||||||
|
|
||||||
// Call commandArgs first!
|
|
||||||
Verilated::commandArgs(argc, argv);
|
|
||||||
|
|
||||||
// Instantiate our design
|
|
||||||
tb = new Vtop;
|
|
||||||
|
|
||||||
// Generate a trace
|
|
||||||
Verilated::traceEverOn(true);
|
|
||||||
tfp = new VerilatedVcdC;
|
|
||||||
tb->trace(tfp, 99);
|
|
||||||
tfp->open("build/waveform.vcd");
|
|
||||||
|
|
||||||
last_led = tb->o_led;
|
|
||||||
|
|
||||||
// Read from the current state
|
|
||||||
printf("Initial state is: 0x%02x\n",
|
|
||||||
wb_read(0));
|
|
||||||
|
|
||||||
for(int cycle=0; cycle<2; cycle++) {
|
|
||||||
// Wait five clocks
|
|
||||||
for(int i=0; i<5; i++)
|
|
||||||
tick();
|
|
||||||
|
|
||||||
// Start the LEDs cycling
|
|
||||||
wb_write(0,0);
|
|
||||||
tick();
|
|
||||||
|
|
||||||
while((state = wb_read(0))!=0) {
|
|
||||||
if ((state != last_state)
|
|
||||||
||(tb->o_led != last_led)) {
|
|
||||||
printf("%6d: State #%2d ",
|
|
||||||
tickcount, state);
|
|
||||||
for(int j=0; j<6; j++) {
|
|
||||||
if(tb->o_led & (1<<j))
|
|
||||||
printf("O");
|
|
||||||
else
|
|
||||||
printf("-");
|
|
||||||
} printf("\n");
|
|
||||||
} tick();
|
|
||||||
|
|
||||||
last_state = state;
|
|
||||||
last_led = tb->o_led;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tfp->close();
|
|
||||||
delete tfp;
|
|
||||||
delete tb;
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
[options]
|
|
||||||
mode prove
|
|
||||||
|
|
||||||
[engines]
|
|
||||||
smtbmc
|
|
||||||
|
|
||||||
[script]
|
|
||||||
read -formal *.v
|
|
||||||
prep -top top
|
|
||||||
|
|
||||||
[files]
|
|
||||||
rtl/top.v
|
|
||||||
rtl/clk_gen.v
|
|
||||||
Reference in New Issue
Block a user