Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module mask_gen (
  2. // ports
  3.     clk,
  4.     nibble_width,
  5.     nibble_start,
  6.     mask
  7. );
  8. input clk; // clock
  9. input wire [3:0] nibble_width; // length of mask in nibbles
  10. input wire [3:0] nibble_start; // nibble where the mask starts
  11. output reg [63:0] mask;// 64 bits mask
  12.  
  13. reg [4:0] n_max;
  14. wire [3:0] nm1 = n_max[3:0];
  15. reg [15:0] bitmask_1;
  16. reg [15:0] bitmask_2;
  17. reg [15:0] bitmask;
  18. //wire [3:0] nm1;
  19.  
  20. always @( posedge clk) begin
  21.     bitmask_1[ 0] = nibble_start==0;
  22.     bitmask_1[ 1] = nibble_start==1  | bitmask_1[0];
  23.     bitmask_1[ 2] = nibble_start==2  | bitmask_1[1];
  24.     bitmask_1[ 3] = nibble_start==3  | bitmask_1[2];
  25.     bitmask_1[ 4] = nibble_start==4  | bitmask_1[3];
  26.     bitmask_1[ 5] = nibble_start==5  | bitmask_1[4];
  27.     bitmask_1[ 6] = nibble_start==6  | bitmask_1[5];
  28.     bitmask_1[ 7] = nibble_start==7  | bitmask_1[6];
  29.     bitmask_1[ 8] = nibble_start==8  | bitmask_1[7];
  30.     bitmask_1[ 9] = nibble_start==9  | bitmask_1[8];
  31.     bitmask_1[10] = nibble_start==10 | bitmask_1[9];
  32.     bitmask_1[11] = nibble_start==11 | bitmask_1[10];
  33.     bitmask_1[12] = nibble_start==12 | bitmask_1[11];
  34.     bitmask_1[13] = nibble_start==13 | bitmask_1[12];
  35.     bitmask_1[14] = nibble_start==14 | bitmask_1[13];
  36.     bitmask_1[15] = nibble_start==15 | bitmask_1[14];
  37.     $display(bitmask_1);
  38.  
  39.  
  40.     n_max <= nibble_start + nibble_width;
  41.     //nm1[3:0] = n_max[3:0];
  42.  
  43.     bitmask_2[15] <= nm1==15;
  44.     bitmask_2[14] <= nm1==14 | bitmask_2[15];
  45.     bitmask_2[13] <= nm1==13 | bitmask_2[14];
  46.     bitmask_2[12] <= nm1==12 | bitmask_2[13];
  47.     bitmask_2[11] <= nm1==11 | bitmask_2[12];
  48.     bitmask_2[10] <= nm1==10 | bitmask_2[11];
  49.     bitmask_2[ 9] <= nm1==9  | bitmask_2[10];
  50.     bitmask_2[ 8] <= nm1==8  | bitmask_2[9];
  51.     bitmask_2[ 7] <= nm1==7  | bitmask_2[8];
  52.     bitmask_2[ 6] <= nm1==6  | bitmask_2[7];
  53.     bitmask_2[ 5] <= nm1==5  | bitmask_2[6];
  54.     bitmask_2[ 4] <= nm1==4  | bitmask_2[5];
  55.     bitmask_2[ 3] <= nm1==3  | bitmask_2[4];
  56.     bitmask_2[ 2] <= nm1==2  | bitmask_2[3];
  57.     bitmask_2[ 1] <= nm1==1  | bitmask_2[2];
  58.     bitmask_2[ 0] <= nm1==0  | bitmask_2[1];
  59.  
  60.     bitmask <= n_max[4] ? bitmask_1 | bitmask_2 : bitmask_1 & bitmask_2;
  61.  
  62.     mask[ 3: 0] <= {4{bitmask[ 0]}};
  63.     mask[ 7: 4] <= {4{bitmask[ 1]}};
  64.     mask[11: 8] <= {4{bitmask[ 2]}};
  65.     mask[15:12] <= {4{bitmask[ 3]}};
  66.     mask[19:16] <= {4{bitmask[ 4]}};
  67.     mask[23:20] <= {4{bitmask[ 5]}};
  68.     mask[27:24] <= {4{bitmask[ 6]}};
  69.     mask[31:28] <= {4{bitmask[ 7]}};
  70.     mask[35:32] <= {4{bitmask[ 8]}};
  71.     mask[39:36] <= {4{bitmask[ 9]}};
  72.     mask[43:40] <= {4{bitmask[10]}};
  73.     mask[47:44] <= {4{bitmask[11]}};
  74.     mask[51:48] <= {4{bitmask[12]}};
  75.     mask[55:52] <= {4{bitmask[13]}};
  76.     mask[59:56] <= {4{bitmask[14]}};
  77.     mask[63:60] <= {4{bitmask[15]}};
  78. end
  79.  
  80. endmodule
  81.  
  82. `ifdef SIM
  83.  
  84. //`timescale 1 ns / 100 ps
  85.  
  86. module mask_gen_tb;
  87.  
  88. // inputs
  89. reg clock;
  90. /*
  91. reg [3:0] nw;
  92. reg [3:0] ns;
  93. // outputs
  94. wire [63:0] m;
  95.  
  96. mask_gen U0 (
  97.     .clk (clock),
  98.     .nibble_width (nw),
  99.     .nibble_start (ns),
  100.     .mask (m)
  101. );
  102. */
  103.  
  104. always
  105.     #10 clock = ! clock;
  106. end
  107.  
  108. initial begin
  109.     $monitor ("clk %b", clock);
  110.     //$monitor ("clk %b | nw %d | ns %d | m %h", clock, nw, ns, m);
  111.     #10 $display("1");
  112.     #10 $display("2");
  113.     #10 $finish  
  114. end
  115. /*
  116. initial begin
  117.     $dumpfile("text.vcd");
  118.     $dumpvars(clock, nw, ns, m);
  119.     $display($time, "starting simulation");
  120.     clock = 0;
  121.     $display("starting the simulation");
  122.     run_mask_gen(4, 0);
  123.     run_mask_gen(4, 1);
  124.     run_mask_gen(4, 2);
  125.     run_mask_gen(4, 3);
  126.     run_mask_gen(4, 4);
  127.     run_mask_gen(4, 5);
  128.     run_mask_gen(4, 6);
  129.     run_mask_gen(4, 7);
  130.     run_mask_gen(4, 8);
  131.     run_mask_gen(4, 9);
  132.     run_mask_gen(4,10);
  133.     run_mask_gen(4,11);
  134.     run_mask_gen(4,12);
  135.     run_mask_gen(4,13);
  136.     run_mask_gen(4,14);
  137.     run_mask_gen(4,15);
  138.  
  139.     //run_mask_gen(4, 0);
  140.     //run_mask_gen(4, 0);
  141.     //run_mask_gen(4, 0);
  142.     //run_mask_gen(4, 0);
  143.     $stop;
  144. end
  145. */
  146. /*
  147. task run_mask_gen;
  148.     input [3:0] _nw;
  149.     input [3:0] _ns;
  150.     begin
  151.         $display("running", _nw, _ns);
  152.         @(posedge clock);
  153.         nw = _nw;
  154.         ns = _ns;
  155.     end
  156. */
  157. endmodule
  158.  
  159. `endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement