decoder_encoder
This commit is contained in:
30
decoder_encoder/decoder_encoder.v
Normal file
30
decoder_encoder/decoder_encoder.v
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Decoder / Encoder
|
||||
*/
|
||||
|
||||
module decoder_3bit (
|
||||
input [2:0] in,
|
||||
output [7:0] out
|
||||
);
|
||||
assign out = 1 << in;
|
||||
endmodule
|
||||
|
||||
module encoder_3bit(
|
||||
input [7:0] in,
|
||||
output reg [2:0] out
|
||||
);
|
||||
always @(*) begin
|
||||
out = 0;
|
||||
|
||||
casez(in)
|
||||
8'b1???????: out = 3'd7;
|
||||
8'b01??????: out = 3'd6;
|
||||
8'b001?????: out = 3'd5;
|
||||
8'b0001????: out = 3'd4;
|
||||
8'b00001???: out = 3'd3;
|
||||
8'b000001??: out = 3'd2;
|
||||
8'b0000001?: out = 3'd1;
|
||||
8'b00000001: out = 3'd0;
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
38
decoder_encoder/decoder_encoder_tb.v
Normal file
38
decoder_encoder/decoder_encoder_tb.v
Normal file
@@ -0,0 +1,38 @@
|
||||
`include "./decoder_encoder/decoder_encoder.v"
|
||||
|
||||
module decoder_3bit_tb;
|
||||
reg [2:0] in;
|
||||
wire [7:0] out;
|
||||
wire [2:0] encoded;
|
||||
|
||||
decoder_3bit dec(
|
||||
.in(in),
|
||||
.out(out)
|
||||
);
|
||||
|
||||
encoder_3bit enc(
|
||||
.in(out),
|
||||
.out(encoded)
|
||||
);
|
||||
|
||||
initial begin
|
||||
$dumpfile("./decoder_encoder/decoder_encoder.vcd");
|
||||
$dumpvars(0, decoder_3bit_tb);
|
||||
|
||||
in = 3'b000;
|
||||
#1
|
||||
$display("in=%d, out=%b, enc=%d", in, out, encoded);
|
||||
in = 3'b001;
|
||||
#1
|
||||
$display("in=%d, out=%b, enc=%d", in, out, encoded);
|
||||
in = 3'b010;
|
||||
#1
|
||||
$display("in=%d, out=%b, enc=%d", in, out, encoded);
|
||||
in = 3'b100;
|
||||
#1
|
||||
$display("in=%d, out=%b, enc=%d", in, out, encoded);
|
||||
in = 3'b111;
|
||||
#1
|
||||
$display("in=%d, out=%b, enc=%d", in, out, encoded);
|
||||
end
|
||||
endmodule
|
||||
Reference in New Issue
Block a user