// Example program for rle decoder // Note that the first character after reset is not valid // There is an assumption that there is always a character // ready to be taken module rld (input clk, input reset, input [7:0] inChar, output [6:0] outChar, output reg taking); reg loadChar; reg [6:0] char; assign outChar = char; reg loadCount; reg [7:0] count; wire zero = count == 0; wire tag = inChar[7]; localparam CHAR=3'b001, COUNT=3'b010, SPEW=3'b100; reg [2:0] state, nxtState; always @(posedge clk) begin if (reset) begin state <= CHAR; end else begin state <= nxtState; end end always @(posedge clk) begin if (loadChar) char <= inChar[6:0]; end always @(posedge clk) begin if (loadCount) count <= inChar; else count <= count - 1; end always @(*) begin nxtState = state; case (state) CHAR: begin loadCount = 1'bX; loadChar = 1; taking = 1; if (tag) begin nxtState = COUNT; end end COUNT: begin loadCount = 1; loadChar = 0; taking = 1; nxtState = SPEW; end SPEW: begin if (!zero) begin loadCount = 0; loadChar = 0; taking = 0; end else begin loadCount = 1'bX; loadChar = 1; taking = 1; nxtState = CHAR; end end // case: SPEW default: begin nxtState = COUNT; end endcase // case (state) // Override taking while reset is still on if (reset) taking = 0; end // always @ (*) endmodule //