// Ideal functional model. SET_TIED_LOW=1 is synthesizable with the
// supplied toolchain. General SET/RESET branch is a simulation model.
// SET and RESET simultaneously high is forbidden in this lesson.
// The model shows both outputs high then; release behavior is unspecified.

module cd4013b
# (
    parameter SET_TIED_LOW = 0
)
(
    input        p3_clk1,
    input        p4_rst1,
    input        p5_d1,
    input        p6_set1,
    input        p8_set2,
    input        p9_d2,
    input        p10_rst2,
    input        p11_clk2,
    output logic p1_q1,
    output       p2_qn1,
    output       p12_qn2,
    output logic p13_q2
);

    generate
        if (SET_TIED_LOW != 0)
        begin : reset_only
            always_ff @ (posedge p3_clk1 or posedge p4_rst1)
                if (p4_rst1)
                    p1_q1 <= 1'b0;
                else
                    p1_q1 <= p5_d1;

            assign p2_qn1 = ~ p1_q1;

            always_ff @ (posedge p11_clk2 or posedge p10_rst2)
                if (p10_rst2)
                    p13_q2 <= 1'b0;
                else
                    p13_q2 <= p9_d2;

            assign p12_qn2 = ~ p13_q2;

        end
        else
        begin : set_reset_model
            always_ff @ (posedge p3_clk1 or posedge p4_rst1 or posedge p6_set1)
                if (p6_set1)
                    p1_q1 <= 1'b1;
                else if (p4_rst1)
                    p1_q1 <= 1'b0;
                else
                    p1_q1 <= p5_d1;

            assign p2_qn1 = (p6_set1 & p4_rst1) | ~ p1_q1;

            always_ff @ (posedge p11_clk2 or posedge p10_rst2 or posedge p8_set2)
                if (p8_set2)
                    p13_q2 <= 1'b1;
                else if (p10_rst2)
                    p13_q2 <= 1'b0;
                else
                    p13_q2 <= p9_d2;

            assign p12_qn2 = (p8_set2 & p10_rst2) | ~ p13_q2;

        end
    endgenerate

endmodule
