Skip to content

Commit 306259c

Browse files
author
Konstantin Pavlov
committed
Added module to switch dims in a 2D systemverilog array
1 parent 3474f7b commit 306259c

File tree

7 files changed

+322
-0
lines changed

7 files changed

+322
-0
lines changed

reverse_dimensions.sv

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//------------------------------------------------------------------------------
2+
// reverse_dimensions.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// "Physically" reverses dimension order in systemv_erilog 2D vector
8+
// Thus in[7][1] signal becomes out[1][7], in[6][10] becomes out[10][6] and vise-versa
9+
// Module is no doubt synthesizable, but its instance does NOT occupy any FPGA resources!
10+
11+
12+
/* --- INSTANTIATION TEMPLATE BEGIN ---
13+
14+
reverse_dimensions #(
15+
.D1_WIDTH( 8 ),
16+
.D2_WIDTH( 3 )
17+
) RD1 (
18+
.in( smth[7:0][2:0] ),
19+
.out( htms[2:0][7:0] ) // reversed bit order
20+
);
21+
22+
--- INSTANTIATION TEMPLATE END ---*/
23+
24+
25+
module reverse_dimensions #( parameter
26+
D1_WIDTH = 8, // first dimention width
27+
D2_WIDTH = 3 // second dimention width
28+
)(
29+
input [D1_WIDTH-1:0][D2_WIDTH-1:0] in,
30+
output logic [D2_WIDTH-1:0][D1_WIDTH-1:0] out
31+
);
32+
33+
34+
genvar i;
35+
genvar j;
36+
generate
37+
for (i = 0; i < D1_WIDTH ; i++) begin : gen_i
38+
for (j = 0; j < D2_WIDTH ; j++) begin : gen_j
39+
40+
always_comb begin
41+
out[j][i] = in[i][j];
42+
end // always_comb
43+
44+
end // for
45+
end // for
46+
endgenerate
47+
48+
endmodule

reverse_dimensions_tb/c_rand.v

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
// C runtime library random number generator
24+
//
25+
// uses 32 logic cells for DFF/ADD and 8 DSP blocks for the
26+
// 32x18=>32 multiply
27+
28+
module c_rand (clk,rst,reseed,seed_val,out);
29+
input clk,rst,reseed;
30+
input [31:0] seed_val;
31+
output [15:0] out;
32+
wire [15:0] out;
33+
34+
reg [31:0] state;
35+
36+
always @(posedge clk or posedge rst) begin
37+
if (rst) state <= 0;
38+
else begin
39+
if (reseed) state <= seed_val;
40+
else begin
41+
state <= state * 32'h343fd + 32'h269EC3;
42+
end
43+
end
44+
end
45+
46+
assign out = (state >> 16) & 16'h7fff;
47+
48+
endmodule

reverse_dimensions_tb/compile.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
modelsim.exe -do compile.tcl

reverse_dimensions_tb/compile.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# compile.sh
4+
# Konstantin Pavlov, [email protected]
5+
#
6+
# This is a support script for launching "Modelsim compile script" on Linux
7+
8+
9+
vsim -do compile.tcl

reverse_dimensions_tb/compile.tcl

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#------------------------------------------------------------------------------
2+
# compile.tcl
3+
# Konstantin Pavlov, [email protected]
4+
#------------------------------------------------------------------------------
5+
6+
# INFO ------------------------------------------------------------------------
7+
# Modelsim compile script
8+
# based on "ModelSimSE general compile script version 1.1" by Doulos
9+
10+
# launch the script by "vsim -do compile.tcl" command on linux
11+
# or by "modelsim.exe -do compile.tcl" on windows
12+
13+
14+
# Simply change the project settings in this section
15+
# for each new project. There should be no need to
16+
# modify the rest of the script.
17+
set library_file_list {
18+
19+
work {reverse_dimensions_tb.sv
20+
../reverse_dimensions.sv
21+
c_rand.v
22+
../edge_detect.sv
23+
../clk_divider.sv}
24+
}
25+
26+
set vsim_params "-L altera_mf_ver -L altera_mf -L lpm_ver -L lpm"
27+
28+
set top_level work.reverse_dimensions_tb
29+
30+
# Console commands:
31+
# r = Recompile changed and dependent files
32+
# rr = Recompile everything
33+
# q = Quit without confirmation
34+
35+
# After sourcing the script from ModelSim for the
36+
# first time use these commands to recompile.
37+
proc r {} {uplevel #0 source compile.tcl}
38+
proc rr {} {global last_compile_time
39+
set last_compile_time 0
40+
r }
41+
proc q {} {quit -force }
42+
43+
#Does this installation support Tk?
44+
set tk_ok 1
45+
if [catch {package require Tk}] {set tk_ok 0}
46+
47+
# Prefer a fixed point font for the transcript
48+
set PrefMain(font) {Courier 10 roman normal}
49+
50+
# Compile out of date files
51+
set time_now [clock seconds]
52+
if [catch {set last_compile_time}] {
53+
set last_compile_time 0
54+
}
55+
foreach {library file_list} $library_file_list {
56+
vlib $library
57+
vmap work $library
58+
foreach file $file_list {
59+
if { $last_compile_time < [file mtime $file] } {
60+
if [regexp {.vhdl?$} $file] {
61+
vcom -93 $file
62+
} else {
63+
vlog -sv $file
64+
}
65+
set last_compile_time 0
66+
}
67+
}
68+
}
69+
set last_compile_time $time_now
70+
71+
# Load the simulation
72+
eval vsim $top_level $vsim_params
73+
74+
# Load saved wave patterns
75+
do wave.do
76+
77+
# Run the simulation
78+
run 100us
79+
80+
wave zoom range 0 100us
81+
82+
# How long since project began?
83+
if {[file isfile start_time.txt] == 0} {
84+
set f [open start_time.txt w]
85+
puts $f "Start time was [clock seconds]"
86+
close $f
87+
} else {
88+
set f [open start_time.txt r]
89+
set line [gets $f]
90+
close $f
91+
regexp {\d+} $line start_time
92+
set total_time [expr ([clock seconds]-$start_time)/60]
93+
puts "Project time is $total_time minutes"
94+
}
95+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//------------------------------------------------------------------------------
2+
// reverse_dimensions_tb.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// testbench for reverse_dimensions module
8+
9+
10+
`timescale 1ns / 1ps
11+
12+
module reverse_dimensions_tb();
13+
14+
logic clk200;
15+
initial begin
16+
#0 clk200 = 1;
17+
forever
18+
#2.5 clk200 = ~clk200;
19+
end
20+
21+
logic rst;
22+
initial begin
23+
#10.2 rst = 1;
24+
#5 rst = 0;
25+
//#10000;
26+
forever begin
27+
#9985 rst = ~rst;
28+
#5 rst = ~rst;
29+
end
30+
end
31+
32+
logic nrst;
33+
assign nrst = ~rst;
34+
35+
logic rst_once;
36+
initial begin // initializing non-X data before PLL starts
37+
#10.2 rst_once = 1;
38+
#5 rst_once = 0;
39+
end
40+
initial begin
41+
#510.2 rst_once = 1; // PLL starts at 500ns, clock appears, so doing the reset for modules
42+
#5 rst_once = 0;
43+
end
44+
45+
logic nrst_once;
46+
assign nrst_once = ~rst_once;
47+
48+
logic [31:0] DerivedClocks;
49+
clk_divider #(
50+
.WIDTH( 32 )
51+
) CD1 (
52+
.clk( clk200 ),
53+
.nrst( nrst_once ),
54+
.ena( 1'b1 ),
55+
.out( DerivedClocks[31:0] )
56+
);
57+
58+
logic [31:0] E_DerivedClocks;
59+
edge_detect ED1[31:0] (
60+
.clk( {32{clk200}} ),
61+
.nrst( {32{nrst_once}} ),
62+
.in( DerivedClocks[31:0] ),
63+
.rising( E_DerivedClocks[31:0] ),
64+
.falling( ),
65+
.both( )
66+
);
67+
68+
logic [15:0] RandomNumber1;
69+
c_rand RNG1 (
70+
.clk( clk200 ),
71+
.rst( rst_once ),
72+
.reseed( 1'b0 ),
73+
.seed_val( DerivedClocks[31:0] ),
74+
.out( RandomNumber1[15:0] )
75+
);
76+
77+
logic start;
78+
initial begin
79+
#0 start = 1'b0;
80+
#100.2 start = 1'b1;
81+
#5 start = 1'b0;
82+
end
83+
84+
// Module under test ==========================================================
85+
86+
logic [1:0][7:0] in;
87+
assign in = RandomNumber1[15:0];
88+
89+
90+
logic [7:0][1:0] out;
91+
reverse_dimensions #(
92+
.D1_WIDTH( 2 ),
93+
.D2_WIDTH( 8 )
94+
) RD1 (
95+
.in( in ),
96+
.out( out )
97+
);
98+
99+
endmodule

reverse_dimensions_tb/wave.do

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
onerror {resume}
2+
quietly WaveActivateNextPane {} 0
3+
add wave -noupdate -radix binary -childformat {{{/reverse_dimensions_tb/RD1/in[1]} -radix binary} {{/reverse_dimensions_tb/RD1/in[0]} -radix binary}} -expand -subitemconfig {{/reverse_dimensions_tb/RD1/in[1]} {-radix binary} {/reverse_dimensions_tb/RD1/in[0]} {-radix binary}} /reverse_dimensions_tb/RD1/in
4+
add wave -noupdate -radix binary -childformat {{{/reverse_dimensions_tb/RD1/out[7]} -radix binary} {{/reverse_dimensions_tb/RD1/out[6]} -radix binary} {{/reverse_dimensions_tb/RD1/out[5]} -radix binary} {{/reverse_dimensions_tb/RD1/out[4]} -radix binary -childformat {{{[1]} -radix hexadecimal} {{[0]} -radix hexadecimal}}} {{/reverse_dimensions_tb/RD1/out[3]} -radix binary} {{/reverse_dimensions_tb/RD1/out[2]} -radix binary} {{/reverse_dimensions_tb/RD1/out[1]} -radix binary} {{/reverse_dimensions_tb/RD1/out[0]} -radix binary}} -expand -subitemconfig {{/reverse_dimensions_tb/RD1/out[7]} {-radix binary} {/reverse_dimensions_tb/RD1/out[6]} {-radix binary} {/reverse_dimensions_tb/RD1/out[5]} {-radix binary} {/reverse_dimensions_tb/RD1/out[4]} {-radix binary -childformat {{{[1]} -radix hexadecimal} {{[0]} -radix hexadecimal}}} {/reverse_dimensions_tb/RD1/out[4][1]} {-radix hexadecimal} {/reverse_dimensions_tb/RD1/out[4][0]} {-radix hexadecimal} {/reverse_dimensions_tb/RD1/out[3]} {-radix binary} {/reverse_dimensions_tb/RD1/out[2]} {-radix binary} {/reverse_dimensions_tb/RD1/out[1]} {-radix binary} {/reverse_dimensions_tb/RD1/out[0]} {-radix binary}} /reverse_dimensions_tb/RD1/out
5+
TreeUpdate [SetDefaultTree]
6+
WaveRestoreCursors {{Cursor 1} {21806 ps} 0}
7+
quietly wave cursor active 1
8+
configure wave -namecolwidth 150
9+
configure wave -valuecolwidth 100
10+
configure wave -justifyvalue right
11+
configure wave -signalnamewidth 0
12+
configure wave -snapdistance 10
13+
configure wave -datasetprefix 0
14+
configure wave -rowmargin 4
15+
configure wave -childrowmargin 2
16+
configure wave -gridoffset 0
17+
configure wave -gridperiod 1
18+
configure wave -griddelta 40
19+
configure wave -timeline 0
20+
configure wave -timelineunits ns
21+
update
22+
WaveRestoreZoom {4609 ps} {81173 ps}

0 commit comments

Comments
 (0)