// Exercise P7: a bit-serial comparator.  It compares two numbers of any
// length that arrive one bit per clock, lowest bit first.
//
// Generic version that matches schematic 1, not tied to any chip.  gt says
// that the bits seen so far make A greater than B, lt that they make it
// less.  A new bit weighs more than all the earlier ones together: if the
// new bits of A and B differ, they decide; if they are equal, the earlier
// result stays.  eq is 1 while neither gt nor lt is.  rst starts a new
// comparison at once, without the clock.  Synthesizable.

module serial_compare
(
    input  logic clk,
    input  logic rst,
    input  logic a,    // one bit of A per clock, lowest first
    input  logic b,    // one bit of B per clock, lowest first
    output logic gt,   // A > B
    output logic lt,   // A < B
    output logic eq    // A = B
);

    wire same = a ~^ b;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
        begin
            gt <= 1'b0;
            lt <= 1'b0;
        end
        else
        begin
            gt <= (a & ~ b) | (gt & same);
            lt <= (~ a & b) | (lt & same);
        end

    assign eq = ~ (gt | lt);

endmodule
