transpose.sv 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Conditionally register a data word; useful for parameterizing modules.
  3. //
  4. module condreg
  5. #(parameter bits,
  6. parameter register)
  7. (
  8. input clk,
  9. input [bits-1:0] d,
  10. output [bits-1:0] q
  11. );
  12. reg [bits-1:0] qr;
  13. wire clock = register ? clk : 1'b0;
  14. assign q = register ? qr : d;
  15. always @(posedge clock)
  16. qr <= d;
  17. endmodule // condreg
  18. //
  19. // To transpose the dimensions of a packed array containing
  20. // two-dimentional data, optionally reversing the word/bits order.
  21. // The terms "words" and "bits" are as defined to the input data
  22. // stream; i.e. the with words = 3, bits = 2 and the d[] array
  23. // containing b2 b1 b0 a2 a1 a0, the q[] array will contain:
  24. // b2 a2 b1 a1 b0 a0 with no reversal,
  25. // a2 b2 a1 b1 a0 b0 with word reversal,
  26. // b0 a0 b1 a1 b2 a2 with bit reversal, and
  27. // a0 b0 a1 b1 a2 b2 with word and bit reversals.
  28. //
  29. // If the parameter "transpose" is 0, then no actual transpose is done;
  30. // this may be useful to parameterize other modules.
  31. //
  32. module transpose
  33. #(parameter integer words,
  34. parameter integer bits,
  35. parameter [0:0] reverse_w = 1'b0,
  36. parameter [0:0] reverse_b = 1'b0,
  37. parameter [0:0] reg_d = 1'b0,
  38. parameter [0:0] reg_q = 1'b0,
  39. parameter [0:0] transpose = 1'b1)
  40. (
  41. input clk,
  42. input [words*bits-1:0] d,
  43. output [words*bits-1:0] q
  44. );
  45. wire [words*bits-1:0] in;
  46. reg [words*bits-1:0] out;
  47. condreg #(.bits(words*bits), .register(reg_d))
  48. dreg (.clk (clk), .d (d), .q(in));
  49. condreg #(.bits(words*bits), .register(reg_q))
  50. qreg (.clk (clk), .d (out), .q(q));
  51. always @(*)
  52. begin
  53. integer w, b;
  54. for (w = 0; w < words; w = w + 1)
  55. for (b = 0; b < bits; b = b + 1)
  56. begin
  57. integer ww, bb, ii, oo;
  58. ww = reverse_w ? (words-1)-w : w;
  59. bb = reverse_b ? (bits -1)-b : b;
  60. ii = ww*bits+bb;
  61. oo = transpose ? b*words+w : w*bits+b;
  62. out[oo] = in[ii];
  63. end
  64. end // always @ (*)
  65. endmodule // parameter
  66. //
  67. // Bit-reverse a packed array
  68. //
  69. // If the parameter "reverse" is 0, then just pass through
  70. // the input; this may be useful to parameterize other modules.
  71. // This is just a special case of the transpose module.
  72. //
  73. module reverse
  74. #(parameter integer bits,
  75. parameter [0:0] reg_d = 1'b0,
  76. parameter [0:0] reg_q = 1'b0,
  77. parameter [0:0] reverse = 1'b1)
  78. (
  79. input clk,
  80. input [bits-1:0] d,
  81. output [bits-1:0] q
  82. );
  83. transpose #(
  84. .words ( 1 ),
  85. .bits ( bits ),
  86. .reverse_w ( 1'b0 ),
  87. .reverse_b ( reverse ),
  88. .reg_d ( reg_d ),
  89. .reg_q ( reg_q ),
  90. .transpose ( 1'b0 )
  91. )
  92. rev ( .clk (clk), .d (d), .q (q) );
  93. endmodule // parameter