factor out code
This commit is contained in:
parent
a9cbb24ca9
commit
31fd1ded97
32
spi_master.v
32
spi_master.v
|
@ -18,9 +18,8 @@ spi_master
|
||||||
#(
|
#(
|
||||||
parameter WID = 24, // Width of bits per transaction.
|
parameter WID = 24, // Width of bits per transaction.
|
||||||
parameter WID_LEN = 5, // Length in bits required to store WID
|
parameter WID_LEN = 5, // Length in bits required to store WID
|
||||||
parameter CYCLE_HALF_WAIT = 1, // One less than half of the wait time of a cycle.
|
parameter CYCLE_HALF_WAIT = 1, // Half of the wait time of a cycle minus 1.
|
||||||
// One cycle of a transfer is 2(CYCLE_HALF_WAIT + 1)
|
// One SCK cycle is 2*(CYCLE_HALF_WAIT + 1) clock cycles.
|
||||||
// clock cycles.
|
|
||||||
parameter TIMER_LEN = 3, // Length in bits required to store CYCLE_HALF_WAIT
|
parameter TIMER_LEN = 3, // Length in bits required to store CYCLE_HALF_WAIT
|
||||||
parameter POLARITY = 0, // 0 = sck idle low, 1 = sck idle high
|
parameter POLARITY = 0, // 0 = sck idle low, 1 = sck idle high
|
||||||
parameter PHASE = 0 // 0 = rising-read falling-write, 1 = rising-write falling-read.
|
parameter PHASE = 0 // 0 = rising-read falling-write, 1 = rising-write falling-read.
|
||||||
|
@ -104,6 +103,16 @@ task setup_bits();
|
||||||
end
|
end
|
||||||
endtask
|
endtask
|
||||||
|
|
||||||
|
task cycle_change();
|
||||||
|
// Stop transfer when the clock returns to its original polarity.
|
||||||
|
if (bit_counter == WID[WID_LEN-1:0] && sck == POLARITY[0]) begin
|
||||||
|
state <= WAIT_FINISHED;
|
||||||
|
end else begin
|
||||||
|
sck <= !sck;
|
||||||
|
state <= ON_CYCLE;
|
||||||
|
end
|
||||||
|
endtask
|
||||||
|
|
||||||
always @ (posedge clk) begin
|
always @ (posedge clk) begin
|
||||||
case (state)
|
case (state)
|
||||||
WAIT_ON_ARM: begin
|
WAIT_ON_ARM: begin
|
||||||
|
@ -137,19 +146,16 @@ always @ (posedge clk) begin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
state <= CYCLE_WAIT;
|
if (CYCLE_HALF_WAIT == 0) begin
|
||||||
|
cycle_change();
|
||||||
|
end else begin
|
||||||
|
state <= CYCLE_WAIT;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
CYCLE_WAIT: begin
|
CYCLE_WAIT: begin
|
||||||
if (timer == CYCLE_HALF_WAIT) begin
|
if (timer == CYCLE_HALF_WAIT) begin
|
||||||
timer <= 0;
|
timer <= 1;
|
||||||
// Stop transfer when the clock returns
|
cycle_change();
|
||||||
// to its original polarity.
|
|
||||||
if (bit_counter == WID[WID_LEN-1:0] && sck == POLARITY[0]) begin
|
|
||||||
state <= WAIT_FINISHED;
|
|
||||||
end else begin
|
|
||||||
state <= ON_CYCLE;
|
|
||||||
sck <= !sck;
|
|
||||||
end
|
|
||||||
end else begin
|
end else begin
|
||||||
timer <= timer + 1;
|
timer <= timer + 1;
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,19 +63,30 @@ localparam WAIT_ON_MASTER = 2;
|
||||||
localparam WAIT_ON_ARM_DEASSERT = 3;
|
localparam WAIT_ON_ARM_DEASSERT = 3;
|
||||||
reg [2:0] state = WAIT_ON_ARM;
|
reg [2:0] state = WAIT_ON_ARM;
|
||||||
|
|
||||||
|
task master_arm();
|
||||||
|
arm_master <= 1;
|
||||||
|
state <= WAIT_ON_MASTER;
|
||||||
|
endtask
|
||||||
|
|
||||||
always @ (posedge clk) begin
|
always @ (posedge clk) begin
|
||||||
case (state)
|
case (state)
|
||||||
WAIT_ON_ARM: begin
|
WAIT_ON_ARM: begin
|
||||||
if (arm) begin
|
if (arm) begin
|
||||||
timer <= 1;
|
timer <= 1;
|
||||||
state <= WAIT_ON_SS;
|
if (SS_WAIT == 0) begin
|
||||||
|
master_arm();
|
||||||
|
end else begin
|
||||||
|
timer <= 1;
|
||||||
|
state <= WAIT_ON_SS;
|
||||||
|
end
|
||||||
ss <= 1;
|
ss <= 1;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
WAIT_ON_SS: begin
|
WAIT_ON_SS: begin
|
||||||
if (timer == SS_WAIT) begin
|
if (timer == SS_WAIT) begin
|
||||||
arm_master <= 1;
|
master_arm();
|
||||||
state <= WAIT_ON_MASTER;
|
end else begin
|
||||||
|
timer <= timer + 1;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
WAIT_ON_MASTER: begin
|
WAIT_ON_MASTER: begin
|
||||||
|
|
31
tests/mk.sh
31
tests/mk.sh
|
@ -16,6 +16,7 @@ run_test() {
|
||||||
-GPOLARITY=$POL -GPHASE=$PHASE -GWID=$WID -CFLAGS -DWID=$WID \
|
-GPOLARITY=$POL -GPHASE=$PHASE -GWID=$WID -CFLAGS -DWID=$WID \
|
||||||
-GWID_LEN=$WIDLEN \
|
-GWID_LEN=$WIDLEN \
|
||||||
-DSPI_MASTER_TYPE=$MASTER_TYPE -DSPI_SLAVE_TYPE=$SLAVE_TYPE \
|
-DSPI_MASTER_TYPE=$MASTER_TYPE -DSPI_SLAVE_TYPE=$SLAVE_TYPE \
|
||||||
|
-DVCDFILE="\"$DIR.vcd\"" \
|
||||||
--Mdir $DIR \
|
--Mdir $DIR \
|
||||||
$EXTARG \
|
$EXTARG \
|
||||||
simtop.v write_read.cpp $MODS
|
simtop.v write_read.cpp $MODS
|
||||||
|
@ -34,21 +35,21 @@ for POL in 0 1; do
|
||||||
"../spi_master.v ../spi_slave.v"
|
"../spi_master.v ../spi_slave.v"
|
||||||
)
|
)
|
||||||
|
|
||||||
( \
|
# ( \
|
||||||
run_test $POL $PHASE \
|
# run_test $POL $PHASE \
|
||||||
spi_master_no_write spi_slave_no_read \
|
# spi_master_no_write spi_slave_no_read \
|
||||||
simtop_no_write_$POL$PHASE 24 \
|
# simtop_no_write_$POL$PHASE 24 \
|
||||||
"../spi_master_no_write.v ../spi_slave_no_read.v" \
|
# "../spi_master_no_write.v ../spi_slave_no_read.v" \
|
||||||
"-DSPI_MASTER_NO_WRITE -CFLAGS -DSPI_MASTER_NO_WRITE"
|
# "-DSPI_MASTER_NO_WRITE -CFLAGS -DSPI_MASTER_NO_WRITE"
|
||||||
)
|
# )
|
||||||
|
#
|
||||||
( \
|
# ( \
|
||||||
run_test $POL $PHASE \
|
# run_test $POL $PHASE \
|
||||||
spi_master_no_read spi_slave_no_write \
|
# spi_master_no_read spi_slave_no_write \
|
||||||
simtop_no_read_$POL$PHASE 24 \
|
# simtop_no_read_$POL$PHASE 24 \
|
||||||
"../spi_master_no_read.v ../spi_slave_no_write.v" \
|
# "../spi_master_no_read.v ../spi_slave_no_write.v" \
|
||||||
"-DSPI_MASTER_NO_READ -CFLAGS -DSPI_MASTER_NO_READ"
|
# "-DSPI_MASTER_NO_READ -CFLAGS -DSPI_MASTER_NO_READ"
|
||||||
)
|
# )
|
||||||
|
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
|
@ -46,7 +46,8 @@ reg slave_error;
|
||||||
.POLARITY(POLARITY),
|
.POLARITY(POLARITY),
|
||||||
.PHASE(PHASE),
|
.PHASE(PHASE),
|
||||||
.WID(WID),
|
.WID(WID),
|
||||||
.WID_LEN(WID_LEN)
|
.WID_LEN(WID_LEN),
|
||||||
|
.CYCLE_HALF_WAIT(5)
|
||||||
) master (
|
) master (
|
||||||
.clk(clk),
|
.clk(clk),
|
||||||
`ifndef SPI_MASTER_NO_WRITE
|
`ifndef SPI_MASTER_NO_WRITE
|
||||||
|
|
Loading…
Reference in New Issue