Dataflow + some behavioral modeling.
A: 4-bit data inputSRC: 2-bit source selectDST: 2-bit destination selectEN: 1-bit enableY: 4-bit data outputEN=0: All Y bits are 0 regardless of other inputs.EN=1: DST selects which bit of Y to update; SRC selects which bit of A to use. All other bits of Y are 0.module i_wonder_why_design_a(
// Explicitly declare port directions and sizes
A,
SRC,
DST,
EN,
Y
);
// Declare internal wires
SBIT; // Selected bit from A
// The selected bit is based on SRC signal
assign SBIT = [ ];
// If enabled and output bit is selected, connect with selected bit
assign Y[ ] = ( && ( == 2'b )) ? : 1'b ; // Output bit 0
assign Y[ ] = ( && ( == 2'b )) ? : 1'b ; // Output bit 1
assign Y[ ] = ( && ( == 2'b )) ? : 1'b ; // Output bit 2
assign Y[ ] = ( && ( == 2'b )) ? : 1'b ; // Output bit 3
endmodule
module i_wonder_why_design_b(
// Declare port directions and sizes
A,
SRC,
DST,
EN,
Y
);
// Single assign: if enabled, shift a 1 to DST and mask with selected bit
// Masking is done by bitwise ANDing. Mask bits are all 0 except the one we want to keep
assign Y = ( ) ? (4'b0001 ) & ( [ ] ? 4'b1111 : 4'b0000) : 4'b0000;
endmodule
module i_wonder_why_design_c(
// Declare port directions and sizes
A,
SRC,
DST,
EN,
// Note: output driven in always block must be reg
Y
);
// Always block: sensitive to all inputs
always @( ) begin
// Check enable signal
if ( ) begin
// Select destination bit based on DST
case ( )
2'b00: Y = (A[ ]) ? 4'b : 4'b0000;
2'b01: Y = (A[ ]) ? 4'b : 4'b0000;
2'b10: Y = (A[ ]) ? 4'b : 4'b0000;
2'b11: Y = (A[ ]) ? 4'b : 4'b0000;
default: Y = 4'b0000;
endcase
end else begin
// Module disabled
Y = 4'b ;
end
end
endmodule
module i_wonder_why_tb();
A;
SRC, DST;
EN;
Y_a, Y_b, Y_c;
i_wonder_why_design_a dut_a (
.A( ), .SRC( ), .DST( ), .EN( ), .Y( ) );
i_wonder_why_design_b dut_b (
.A( ), .SRC( ), .DST( ), .EN( ), .Y( ) );
i_wonder_why_design_c dut_c (
.A( ), .SRC( ), .DST( ), .EN( ), .Y( ) );
initial begin
A = 4'b1010; SRC = 2'b01; DST = 2'b10; EN = 0; #10;
A = 4'b1010; SRC = 2'b01; DST = 2'b10; EN = 1; #10;
A = 4'b1010; SRC = 2'b00; DST = 2'b00; EN = 1; #10;
A = 4'b1010; SRC = 2'b11; DST = 2'b11; EN = 1; #10;
$finish;
end
endmodule
A=4'b1010; SRC=2'b01; DST=2'b10; EN=0; → Y_a=4'b Y_b=4'b Y_c=4'b A=4'b1010; SRC=2'b01; DST=2'b10; EN=1; → Y_a=4'b Y_b=4'b Y_c=4'b A=4'b1010; SRC=2'b00; DST=2'b00; EN=1; → Y_a=4'b Y_b=4'b Y_c=4'b A=4'b1010; SRC=2'b11; DST=2'b11; EN=1; → Y_a=4'b Y_b=4'b Y_c=4'b