From 31fd1ded97e89e047dc7f032d8bab22fe56a92d2 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Sun, 23 Oct 2022 04:56:56 -0400 Subject: [PATCH] factor out code --- spi_master.v | 32 +++++++++++++++++++------------- spi_master_ss_template.v | 17 ++++++++++++++--- tests/mk.sh | 31 ++++++++++++++++--------------- tests/simtop.v | 3 ++- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/spi_master.v b/spi_master.v index d8b9020..d6018c0 100644 --- a/spi_master.v +++ b/spi_master.v @@ -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 diff --git a/spi_master_ss_template.v b/spi_master_ss_template.v index eeaf01c..27a4b85 100644 --- a/spi_master_ss_template.v +++ b/spi_master_ss_template.v @@ -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 diff --git a/tests/mk.sh b/tests/mk.sh index f907668..51a9775 100755 --- a/tests/mk.sh +++ b/tests/mk.sh @@ -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 diff --git a/tests/simtop.v b/tests/simtop.v index 91533fd..d47a802 100644 --- a/tests/simtop.v +++ b/tests/simtop.v @@ -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