2
0

transpose.sv 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 words,
  34. parameter bits,
  35. parameter reverse_w = 0,
  36. parameter reverse_b = 0,
  37. parameter reg_d = 0,
  38. parameter reg_q = 0,
  39. parameter transpose = 1)
  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-w-1 : w;
  59. bb = reverse_b ? bits -b-1 : 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. //
  72. module reverse
  73. #(parameter bits,
  74. parameter reg_d = 0,
  75. parameter reg_q = 0,
  76. parameter reverse = 1)
  77. (
  78. input clk,
  79. input [bits-1:0] d,
  80. output [bits-1:0] q
  81. );
  82. wire [bits-1:0] in;
  83. reg [bits-1:0] out;
  84. condreg #(.bits(bits), .register(reg_d))
  85. dreg (.clk (clk), .d (d), .q(in));
  86. condreg #(.bits(bits), .register(reg_q))
  87. qreg (.clk (clk), .d (out), .q(q));
  88. always @(*)
  89. begin
  90. integer i;
  91. for (i = 0; i < bits; i = i + 1)
  92. begin
  93. integer ii;
  94. ii = reverse ? bits-i-1 : i;
  95. out[i] = in[ii];
  96. end
  97. end
  98. endmodule // parameter