|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// slicer_functions.vh |
| 3 | +// published as part of https://github.com/pConst/basic_verilog |
| 4 | +// Konstantin Pavlov, [email protected] |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +// INFO ------------------------------------------------------------------------ |
| 8 | +// Arbitrary slice functions for 2D and 3D packed SystemVerilog arrays |
| 9 | +// Slices along any array edge, all array sides simultaneously |
| 10 | +// |
| 11 | +// You can also generalize this aproach to support as many dimentions as needed |
| 12 | +// |
| 13 | +// This module does NOT consume any FPGA resources though it is absolutely |
| 14 | +// synthesizable |
| 15 | +// |
| 16 | +// Parametrized classes are supported by Vivado, NOT supported by Quartus. |
| 17 | +// Please use slicer_2d.sv and slicer_3d.sv conventional modules instead. |
| 18 | +// |
| 19 | +// Call syntax: |
| 20 | +// ============ |
| 21 | +// assign a[2:1][2:1] = slicer_functions#( |
| 22 | +// .I2_HI( 3 ), .I2_LO( 0 ), .I1_HI( 7 ), .I1_LO( 0 ), |
| 23 | +// .O2_HI( 2 ), .O2_LO( 1 ), .O1_HI( 2 ), .O1_LO( 1 ) )::slice2d( b[3:0][7:0] ); |
| 24 | +// |
| 25 | +// assign c[2:1][2:1][2:1] = slicer_functions#( |
| 26 | +// .I3_HI( 3 ), .I3_LO( 0 ), .I2_HI( 3 ), .I2_LO( 0 ), |
| 27 | +// .I1_HI( 7 ), .I1_LO( 0 ), .O3_HI( 2 ), .O3_LO( 1 ), |
| 28 | +// .O2_HI( 2 ), .O2_LO( 1 ), .O1_HI( 2 ), .O1_LO( 1 ) )::slice3d( d[3:0][3:0][7:0] ); |
| 29 | +// |
| 30 | + |
| 31 | + |
| 32 | +virtual class slicer_functions #( parameter |
| 33 | + |
| 34 | + // input array shape |
| 35 | + I3_HI = 3, // most significant size |
| 36 | + I3_LO = 0, |
| 37 | + |
| 38 | + I2_HI = 3, |
| 39 | + I2_LO = 0, |
| 40 | + |
| 41 | + I1_HI = 7, // least significant size |
| 42 | + I1_LO = 0, |
| 43 | + |
| 44 | + // sliced array shape |
| 45 | + O3_HI = 2, // most significant size |
| 46 | + O3_LO = 1, |
| 47 | + |
| 48 | + O2_HI = 2, |
| 49 | + O2_LO = 1, |
| 50 | + |
| 51 | + O1_HI = 2, // least significant size |
| 52 | + O1_LO = 1 |
| 53 | +); |
| 54 | + |
| 55 | + |
| 56 | + static function [O2_HI:O2_LO][O1_HI:O1_LO] slice2d( |
| 57 | + input [I2_HI:I2_LO][I1_HI:I1_LO] in |
| 58 | + ); |
| 59 | + |
| 60 | + integer i; |
| 61 | + for ( i=O2_LO; i<=O2_HI; i++ ) begin |
| 62 | + slice2d[i][O1_HI:O1_LO] = in[i][O1_HI:O1_LO]; |
| 63 | + end //i |
| 64 | + endfunction |
| 65 | + |
| 66 | + |
| 67 | + static function [O3_HI:O3_LO][O2_HI:O2_LO][O1_HI:O1_LO] slice3d( |
| 68 | + input [I3_HI:I3_LO][I2_HI:I2_LO][I1_HI:I1_LO] in |
| 69 | + ); |
| 70 | + |
| 71 | + integer i,j; |
| 72 | + for ( i=O3_LO; i<=O3_HI; i++ ) begin |
| 73 | + for ( j=O2_LO; j<=O2_HI; j++ ) begin |
| 74 | + slice3d[i][j][O1_HI:O1_LO] = in[i][j][O1_HI:O1_LO]; |
| 75 | + end //j |
| 76 | + end //i |
| 77 | + endfunction |
| 78 | + |
| 79 | +endclass |
| 80 | + |
0 commit comments