From 3f0f6f04e3f00b82311ad6aeb50f4a0be583a6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Luis=20Monta=C3=B1es=20Ojados?= Date: Sun, 1 Mar 2026 01:02:44 +0100 Subject: [PATCH] decoder_encoder --- decoder_encoder/decoder_encoder.v | 30 ++++++++++++++++++++++ decoder_encoder/decoder_encoder_tb.v | 38 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 decoder_encoder/decoder_encoder.v create mode 100644 decoder_encoder/decoder_encoder_tb.v diff --git a/decoder_encoder/decoder_encoder.v b/decoder_encoder/decoder_encoder.v new file mode 100644 index 0000000..0786f02 --- /dev/null +++ b/decoder_encoder/decoder_encoder.v @@ -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 \ No newline at end of file diff --git a/decoder_encoder/decoder_encoder_tb.v b/decoder_encoder/decoder_encoder_tb.v new file mode 100644 index 0000000..a222dba --- /dev/null +++ b/decoder_encoder/decoder_encoder_tb.v @@ -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 \ No newline at end of file