EDA Playground lets you type in and run HDL code (using a selection of free and commercial simulators and synthesizers).
It's great for learning HDLs, it's great for testing out unfamiliar things and it's great for sharing code.
You can start typing straight away. But to run your code, you'll need to sign or log in. Logging in with a Google account gives you access to all non-commercial simulators and some commercial simulators:
To run commercial simulators, you need to register and log in with a username and password. Registration is free, and only pre-approved email's will have access to the commercial simulators.
208
// Code your testbench here
// or browse Examples
module tb_sort_network_8x4 ();
reg [3:0] data_in [0:7];
wire [3:0] data_sort [0:7];
sort_network_8x4 u1 (data_in, data_sort);
initial begin
#5;
data_in[0] = 7;
data_in[1] = 6;
data_in[2] = 15;
data_in[3] = 14;
data_in[4] = 3;
data_in[5] = 2;
data_in[6] = 1;
data_in[7] = 13;
#5;
data_in[0] = 8;
data_in[1] = 7;
data_in[2] = 6;
data_in[3] = 5;
data_in[4] = 4;
data_in[5] = 3;
data_in[6] = 2;
data_in[7] = 1;
#5;
data_in[0] = 3;
data_in[1] = 4;
data_in[2] = 10;
data_in[3] = 11;
data_in[4] = 14;
data_in[5] = 13;
data_in[6] = 4;
data_in[7] = 3;
#80 $stop;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars(0);
end
endmodule
// Code your design here
module comapre_and_swap(input [3:0] data1, data2,
output [3:0] max, min);
// Comparator output declaration
wire data1_greater_than_data2;
// Comparator output = 1 if data1 > data2
// Comparator output = 0 if data1 <= data2
assign data1_greater_than_data2 = data1 > data2;
// max data
assign max = (data1_greater_than_data2 == 1'b1) ? data1 : data2;
// min data
assign min = (data1_greater_than_data2 == 1'b1) ? data2 : data1;
endmodule
module sort_network_8x4 (input [3:0] data_in [0:7],
output [3:0] data_sort [0:7]);
// wire declarations for all the sorting intermediaries
wire [3:0] stage1 [0:7];
wire [3:0] stage2_0 [0:7];
wire [3:0] stage2_1 [0:7];
wire [3:0] stage3_0 [0:7];
wire [3:0] stage3_1 [0:7];
wire [3:0] stage3_2 [0:7];
//------------------------------------------------------------------------
// Stage 0 has only one level of sorting
//------------------------------------------------------------------------
comapre_and_swap u1 (data_in[0], data_in[1], stage1[1], stage1[0]);
comapre_and_swap u2 (data_in[2], data_in[3], stage1[2], stage1[3]);
comapre_and_swap u3 (data_in[4], data_in[5], stage1[5], stage1[4]);
comapre_and_swap u4 (data_in[6], data_in[7], stage1[6], stage1[7]);
//------------------------------------------------------------------------
//Stage 1 has 2 levels of sorting
//------------------------------------------------------------------------
comapre_and_swap u5 (stage1[0], stage1[2], stage2_0[2], stage2_0[0]);
comapre_and_swap u6 (stage1[1], stage1[3], stage2_0[3], stage2_0[1]);
comapre_and_swap u7 (stage1[4], stage1[6], stage2_0[4], stage2_0[6]);
comapre_and_swap u8 (stage1[5], stage1[7], stage2_0[5], stage2_0[7]);
comapre_and_swap u9 (stage2_0[0], stage2_0[2], stage2_1[2], stage2_1[0]);
comapre_and_swap u10 (stage2_0[1], stage2_0[3], stage2_1[3], stage2_1[1]);
comapre_and_swap u11 (stage2_0[4], stage2_0[6], stage2_1[4], stage2_1[6]);
comapre_and_swap u12 (stage2_0[5], stage2_0[7], stage2_1[5], stage2_1[7]);
//------------------------------------------------------------------------
// Stage 2 has 3 levels of sorting
//------------------------------------------------------------------------
comapre_and_swap u13 (stage2_1[0], stage2_1[4], stage3_0[4], stage3_0[0]);
comapre_and_swap u14 (stage2_1[1], stage2_1[5], stage3_0[5], stage3_0[1]);
comapre_and_swap u15 (stage2_1[2], stage2_1[6], stage3_0[6], stage3_0[2]);
comapre_and_swap u16 (stage2_1[3], stage2_1[7], stage3_0[7], stage3_0[3]);
comapre_and_swap u17 (stage3_0[0], stage3_0[2], stage3_1[2], stage3_1[0]);
comapre_and_swap u18 (stage3_0[1], stage3_0[3], stage3_1[3], stage3_1[1]);
comapre_and_swap u19 (stage3_0[4], stage3_0[6], stage3_1[6], stage3_1[4]);
comapre_and_swap u20 (stage3_0[5], stage3_0[7], stage3_1[7], stage3_1[5]);
comapre_and_swap u21 (stage3_1[0], stage3_1[1], stage3_2[1], stage3_2[0]);
comapre_and_swap u22 (stage3_1[2], stage3_1[3], stage3_2[3], stage3_2[2]);
comapre_and_swap u23 (stage3_1[4], stage3_1[5], stage3_2[5], stage3_2[4]);
comapre_and_swap u24 (stage3_1[6], stage3_1[7], stage3_2[7], stage3_2[6]);
// Sorted data is assigned to output
assign data_sort = stage3_2;
endmodule
Your account is not validated. If you wish to use commercial simulators, you need a validated account.
If you have already registered (or have recently changed your email address), but have not clicked on the link in the email we sent you, please do so. If you cannot find the email, please check your spam/junk folder. Or click here to resend the email.
If you have not already registered for a full account, you can do so by clicking below. You will then need to provide us with some identification information. You may wish to save your code first.
Creating, deleting, and renaming files is not supported during Collaboration. To encourage development of these features for Collaboration, tweet to @EDAPlayground
This playground may have been modified. Please save or copy before starting collaboration.
Your exercise has been submitted.