
module big_ila_usb_rec
(
    input       CLK
,   input       RESET
    
,   input       RXACT
,   input       RXVAL
,   input [7:0] RXDAT

,   output      ARM
,   input       CAPTURE_DONE
);

localparam      CMD_ARM = 8'h01, // arm capture for a single trigger event
                CMD_AUTO_ARM = 8'h02, // arm and continuously hold armed
                CMD_DISARM = 8'h00; // disarm either of the arm commands

reg arm;
reg hold_arm;

always @(posedge CLK or posedge RESET) begin
    if(RESET) begin
        arm <= 1'b0;
        hold_arm <= 1'b0;
    end else begin
        if(RXACT) begin
            // Currently receiving a USB packet
            // Expects a single byte.
            // Will accept any length of packet, and each byte
            // will be processed as if it is the only byte in the
            // packet.
            if(RXVAL) begin
                case(RXDAT)

                CMD_ARM: begin
                    arm <= 1'b1;
                    hold_arm <= 1'b0;
                end
                CMD_AUTO_ARM: begin
                    arm <= 1'b1;
                    hold_arm <= 1'b1;
                end
                CMD_DISARM: begin
                    arm <= 1'b0;
                    hold_arm <= 1'b0;
                end
                default: begin
                end
                endcase
            end
        end
        else begin

            if(arm) begin
                if(CAPTURE_DONE) begin
                    if(~hold_arm) begin
                        arm <= 1'b0;
                    end
                end
            end
        end
    end
end

assign ARM = arm;

endmodule
