File size: 3,743 Bytes
7ce905d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be94342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19caf8f
 
 
 
 
 
 
 
 
 
 
be94342
 
7ce905d
be94342
7ce905d
be94342
 
7ce905d
 
 
 
 
 
be94342
 
 
7ce905d
 
be94342
 
 
7ce905d
be94342
7ce905d
 
 
 
 
 
 
 
 
be94342
7ce905d
 
 
be94342
 
 
 
 
 
7ce905d
 
 
 
 
 
 
 
 
 
 
 
 
 
be94342
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import gradio as gr

q = 0
q_bar = 1
history = []


def sr_latch(s, r):
    global q, q_bar, history

    s = int(s)
    r = int(r)

    if s == 1 and r == 1:
        state = "INVALID"
    elif s == 1 and r == 0:
        q, q_bar = 1, 0
        state = "SET"
    elif s == 0 and r == 1:
        q, q_bar = 0, 1
        state = "RESET"
    else:
        state = "HOLD"

    q_display = "X" if state == "INVALID" else str(q)
    q_bar_display = "X" if state == "INVALID" else str(q_bar)

    history.append(f"S={s}, R={r}  =>  Q={q_display}, Q'={q_bar_display}  [{state}]")

    log = "\n".join(history)

    return q_display, q_bar_display, state, log


def reset_latch():
    global q, q_bar, history
    q = 0
    q_bar = 1
    history = []
    return "0", "1", "HOLD", ""


custom_css = """
.gradio-container {
    max-width: 720px !important;
    margin: auto;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
h1 {
    color: #1a1a2e !important;
    font-size: 28px !important;
    font-weight: 700 !important;
    margin-bottom: 4px !important;
}
.subtitle {
    color: #5a5a6e !important;
    font-size: 15px !important;
}
.output-box textarea {
    font-size: 22px !important;
    font-weight: 600 !important;
    text-align: center !important;
    color: #1a1a2e !important;
}
.history-box textarea {
    font-family: "SF Mono", "Fira Code", "Consolas", monospace !important;
    font-size: 13px !important;
    color: #3a3a4e !important;
}
.truth-table-wrap table {
    font-size: 14px !important;
}
.truth-table-wrap th {
    color: #5a5a6e !important;
    font-weight: 600 !important;
}
.truth-table-wrap td {
    color: #1a1a2e !important;
}
button.primary {
    background: #1a1a2e !important;
    color: #ffffff !important;
    border: none !important;
}
button.primary:hover {
    background: #2a2a3e !important;
}
input[type="radio"]:checked {
    accent-color: #1a1a2e !important;
}
"""

with gr.Blocks(title="SR Latch Simulator") as demo:
    gr.Markdown("# SR Latch Simulator")
    gr.Markdown(
        "Toggle the S (set) and R (reset) inputs and click Apply to observe the latch behavior.",
        elem_classes=["subtitle"],
    )

    with gr.Row():
        s_input = gr.Radio(choices=["0", "1"], value="0", label="S (Set)")
        r_input = gr.Radio(choices=["0", "1"], value="0", label="R (Reset)")

    with gr.Row():
        apply_btn = gr.Button("Apply", variant="primary")
        reset_btn = gr.Button("Reset")

    with gr.Row():
        q_output = gr.Textbox(label="Q", value="0", interactive=False, elem_classes=["output-box"])
        q_bar_output = gr.Textbox(label="Q'", value="1", interactive=False, elem_classes=["output-box"])
        state_output = gr.Textbox(label="State", value="HOLD", interactive=False, elem_classes=["output-box"])

    gr.Markdown("### Truth Table")
    gr.Dataframe(
        value=[
            ["0", "0", "Prev", "Prev", "Hold"],
            ["1", "0", "1", "0", "Set"],
            ["0", "1", "0", "1", "Reset"],
            ["1", "1", "X", "X", "Invalid"],
        ],
        headers=["S", "R", "Q", "Q'", "State"],
        interactive=False,
        elem_classes=["truth-table-wrap"],
    )

    gr.Markdown("### History")
    history_box = gr.Textbox(
        label="Log",
        lines=6,
        interactive=False,
        elem_classes=["history-box"],
    )

    apply_btn.click(
        fn=sr_latch,
        inputs=[s_input, r_input],
        outputs=[q_output, q_bar_output, state_output, history_box],
    )

    reset_btn.click(
        fn=reset_latch,
        inputs=[],
        outputs=[q_output, q_bar_output, state_output, history_box],
    )

if __name__ == "__main__":
    demo.launch(css=custom_css, theme=gr.themes.Base())