Skip to content

Instantly share code, notes, and snippets.

@urish
Created November 29, 2021 09:39
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/5a462414fc1cd39b3df9c16c86570952 to your computer and use it in GitHub Desktop.
Save urish/5a462414fc1cd39b3df9c16c86570952 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};
reg [7:0] delta;
always @(*) begin
case (inputs)
4'b1000,
4'b0111: delta <= 8'd1;
4'b0010,
4'b1101: delta <= 8'd255;
default: delta <= 0;
endcase
end
always @(posedge clk) begin
if (reset) begin
old_a <= 0;
old_b <= 0;
value <= 0;
end else begin
value <= value + delta;
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