Skip to content

Instantly share code, notes, and snippets.

@urish
Created November 28, 2021 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urish/b8b92e9945638b22c8f32b52dd306a76 to your computer and use it in GitHub Desktop.
Save urish/b8b92e9945638b22c8f32b52dd306a76 to your computer and use it in GitHub Desktop.
`default_nettype none
`timescale 1ns/1ns
module encoder (
input clk,
input reset,
input a,
input b,
output reg [7:0] value
);
reg old_a;
reg old_b;
wire [3:0] inputs = {a, old_a, b, old_b};
always @(posedge clk) begin
if (reset) begin
old_a <= 0;
old_b <= 0;
value <= 0;
end else begin
if (inputs == 4'b1000 || inputs == 4'b0111) begin
value <= value + 1;
end else if (inputs === 4'b0010 || inputs == 4'b1101) begin
value <= value - 1;
end
old_a = a;
old_b = b;
end
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment