pkt_spi_write.v 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * pkt_spi_write.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module pkt_spi_write #(
  35. parameter BASE = 8'h20
  36. )(
  37. // SPI 'simple bus'
  38. input wire [7:0] sb_addr,
  39. input wire [7:0] sb_data,
  40. input wire sb_first,
  41. input wire sb_last,
  42. input wire sb_strobe,
  43. // Packet FIFO write
  44. output reg [7:0] fifo_data,
  45. output reg fifo_last,
  46. output reg fifo_wren,
  47. input wire fifo_full,
  48. // Clock / Reset
  49. input wire clk,
  50. input wire rst
  51. );
  52. // Signals
  53. reg [7:0] data;
  54. reg first;
  55. reg last;
  56. reg [2:0] cnt;
  57. reg [7:0] data_mux;
  58. reg hit_ena;
  59. reg hit_type;
  60. reg hit_ext;
  61. // Decode 'hits'
  62. always @(posedge clk)
  63. begin
  64. hit_ena <= sb_strobe & (sb_addr[7:1] == (BASE >> 1));
  65. hit_type <= sb_addr[0] & cnt[2] & ~sb_first;
  66. hit_ext <= hit_ena & hit_type;
  67. end
  68. // Register data
  69. always @(posedge clk)
  70. if (sb_strobe) begin
  71. data <= sb_data;
  72. first <= sb_first;
  73. last <= sb_last;
  74. end
  75. // Position counter
  76. always @(posedge clk)
  77. if (sb_strobe) begin
  78. if (sb_first)
  79. cnt <= 0;
  80. else
  81. cnt <= cnt + { 3'b000, ~cnt[2] };
  82. end
  83. // Data Mux
  84. always @(*)
  85. if (~hit_type)
  86. // RAW
  87. data_mux = data;
  88. else if (~hit_ext)
  89. // Ext First byte
  90. // data_mux = { data[4:2], data[1:0], data[1:0], data[1] };
  91. data_mux = { data[5:3], data[2:0], data[2:1] };
  92. else
  93. // Ext Second byte
  94. // data_mux = { data[7:5], data[7:6], data[4:2] };
  95. data_mux = { data[7:6], data[7:6], data[7], data[5:3] };
  96. // FIFO interface
  97. always @(posedge clk)
  98. begin
  99. fifo_data <= data_mux;
  100. fifo_last <= last & (~hit_type | hit_ext);
  101. fifo_wren <= hit_ena | hit_ext;
  102. end
  103. endmodule // pkt_spi_write