factor out code

This commit is contained in:
Peter McGoron 2022-10-23 04:56:56 -04:00
parent a9cbb24ca9
commit 31fd1ded97
4 changed files with 51 additions and 32 deletions

View File

@ -18,9 +18,8 @@ spi_master
#(
parameter WID = 24, // Width of bits per transaction.
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.
// One cycle of a transfer is 2(CYCLE_HALF_WAIT + 1)
// clock cycles.
parameter CYCLE_HALF_WAIT = 1, // Half of the wait time of a cycle minus 1.
// One SCK cycle is 2*(CYCLE_HALF_WAIT + 1) clock cycles.
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 PHASE = 0 // 0 = rising-read falling-write, 1 = rising-write falling-read.
@ -104,6 +103,16 @@ task setup_bits();
end
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
case (state)
WAIT_ON_ARM: begin
@ -137,19 +146,16 @@ always @ (posedge clk) begin
end
end
state <= CYCLE_WAIT;
if (CYCLE_HALF_WAIT == 0) begin
cycle_change();
end else begin
state <= CYCLE_WAIT;
end
end
CYCLE_WAIT: begin
if (timer == CYCLE_HALF_WAIT) begin
timer <= 0;
// 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
state <= ON_CYCLE;
sck <= !sck;
end
timer <= 1;
cycle_change();
end else begin
timer <= timer + 1;
end

View File

@ -63,19 +63,30 @@ localparam WAIT_ON_MASTER = 2;
localparam WAIT_ON_ARM_DEASSERT = 3;
reg [2:0] state = WAIT_ON_ARM;
task master_arm();
arm_master <= 1;
state <= WAIT_ON_MASTER;
endtask
always @ (posedge clk) begin
case (state)
WAIT_ON_ARM: begin
if (arm) begin
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;
end
end
WAIT_ON_SS: begin
if (timer == SS_WAIT) begin
arm_master <= 1;
state <= WAIT_ON_MASTER;
master_arm();
end else begin
timer <= timer + 1;
end
end
WAIT_ON_MASTER: begin

View File

@ -16,6 +16,7 @@ run_test() {
-GPOLARITY=$POL -GPHASE=$PHASE -GWID=$WID -CFLAGS -DWID=$WID \
-GWID_LEN=$WIDLEN \
-DSPI_MASTER_TYPE=$MASTER_TYPE -DSPI_SLAVE_TYPE=$SLAVE_TYPE \
-DVCDFILE="\"$DIR.vcd\"" \
--Mdir $DIR \
$EXTARG \
simtop.v write_read.cpp $MODS
@ -34,21 +35,21 @@ for POL in 0 1; do
"../spi_master.v ../spi_slave.v"
)
( \
run_test $POL $PHASE \
spi_master_no_write spi_slave_no_read \
simtop_no_write_$POL$PHASE 24 \
"../spi_master_no_write.v ../spi_slave_no_read.v" \
"-DSPI_MASTER_NO_WRITE -CFLAGS -DSPI_MASTER_NO_WRITE"
)
( \
run_test $POL $PHASE \
spi_master_no_read spi_slave_no_write \
simtop_no_read_$POL$PHASE 24 \
"../spi_master_no_read.v ../spi_slave_no_write.v" \
"-DSPI_MASTER_NO_READ -CFLAGS -DSPI_MASTER_NO_READ"
)
# ( \
# run_test $POL $PHASE \
# spi_master_no_write spi_slave_no_read \
# simtop_no_write_$POL$PHASE 24 \
# "../spi_master_no_write.v ../spi_slave_no_read.v" \
# "-DSPI_MASTER_NO_WRITE -CFLAGS -DSPI_MASTER_NO_WRITE"
# )
#
# ( \
# run_test $POL $PHASE \
# spi_master_no_read spi_slave_no_write \
# simtop_no_read_$POL$PHASE 24 \
# "../spi_master_no_read.v ../spi_slave_no_write.v" \
# "-DSPI_MASTER_NO_READ -CFLAGS -DSPI_MASTER_NO_READ"
# )
done
done

View File

@ -46,7 +46,8 @@ reg slave_error;
.POLARITY(POLARITY),
.PHASE(PHASE),
.WID(WID),
.WID_LEN(WID_LEN)
.WID_LEN(WID_LEN),
.CYCLE_HALF_WAIT(5)
) master (
.clk(clk),
`ifndef SPI_MASTER_NO_WRITE