4只保留有复杂时序逻辑和状态机的组件,其他基本组件使用数据库存储
7from typing
import Dict, List, Any, Optional, Set, Tuple
9from dataclasses
import dataclass
29 state: LogicState = LogicState.UNKNOWN
30 timestamp: float = 0.0
73 if new_state
in self.states:
74 old_state = self.current_state
75 self.current_state = new_state
76 self.state_history.append((new_state, time.time()))
77 print(f
"[{self.name}] State transition: {old_state} -> {new_state}")
131 elif ff_type ==
"JK":
136 elif ff_type ==
"SR":
162 d_input = self.
inputs[
"D"].state
163 if d_input
in [LogicState.LOW, LogicState.HIGH]:
165 self.
outputs[
"Q"].set_state(d_input)
166 self.
outputs[
"Q_bar"].set_state(
167 LogicState.HIGH
if d_input == LogicState.LOW
else LogicState.LOW
172 j_input = self.
inputs[
"J"].state
173 k_input = self.
inputs[
"K"].state
176 if j_input == LogicState.LOW
and k_input == LogicState.LOW:
179 elif j_input == LogicState.LOW
and k_input == LogicState.HIGH:
182 elif j_input == LogicState.HIGH
and k_input == LogicState.LOW:
185 elif j_input == LogicState.HIGH
and k_input == LogicState.HIGH:
187 self.
internal_state[
"Q"] = LogicState.HIGH
if current_q == LogicState.LOW
else LogicState.LOW
190 self.
outputs[
"Q_bar"].set_state(
191 LogicState.HIGH
if self.
internal_state[
"Q"] == LogicState.LOW
else LogicState.LOW
196 s_input = self.
inputs[
"S"].state
197 r_input = self.
inputs[
"R"].state
199 if s_input == LogicState.LOW
and r_input == LogicState.LOW:
202 elif s_input == LogicState.LOW
and r_input == LogicState.HIGH:
205 elif s_input == LogicState.HIGH
and r_input == LogicState.LOW:
213 self.
outputs[
"Q_bar"].set_state(
214 LogicState.HIGH
if self.
internal_state[
"Q"] == LogicState.LOW
else LogicState.LOW
220 self.
outputs[
"Q"].set_state(LogicState.LOW)
221 self.
outputs[
"Q_bar"].set_state(LogicState.HIGH)
226 def __init__(self, name: str, width: int = 8, count_up: bool =
True):
234 for i
in range(width):
252 new_count = (current_count + 1) % (self.
max_count + 1)
253 carry = (current_count == self.
max_count)
255 new_count = (current_count - 1) % (self.
max_count + 1)
256 carry = (current_count == 0)
261 for i
in range(self.
width):
262 bit_value = (new_count >> i) & 1
263 self.
outputs[f
"Q{i}"].set_state(
264 LogicState.HIGH
if bit_value
else LogicState.LOW
267 self.
outputs[
"carry"].set_state(LogicState.HIGH
if carry
else LogicState.LOW)
268 self.
outputs[
"zero"].set_state(LogicState.HIGH
if new_count == 0
else LogicState.LOW)
273 for i
in range(self.
width):
274 self.
outputs[f
"Q{i}"].set_state(LogicState.LOW)
275 self.
outputs[
"carry"].set_state(LogicState.LOW)
276 self.
outputs[
"zero"].set_state(LogicState.HIGH)
320 if current_state ==
"IDLE" and self.
input_signals[
"start"].state == LogicState.HIGH:
322 elif current_state ==
"FETCH" and self.
input_signals[
"instruction_ready"].state == LogicState.HIGH:
324 elif current_state ==
"DECODE" and self.
input_signals[
"decoded"].state == LogicState.HIGH:
326 elif current_state ==
"EXECUTE":
327 if self.
input_signals[
"halt_instruction"].state == LogicState.HIGH:
329 elif self.
input_signals[
"executed"].state == LogicState.HIGH:
331 elif current_state ==
"WRITEBACK" and self.
input_signals[
"written_back"].state == LogicState.HIGH:
341 signal.set_state(LogicState.LOW)
351 self.
output_signals[
"writeback_enable"].set_state(LogicState.HIGH)
356if __name__ ==
"__main__":
357 print(
"=== 复杂逻辑电路系统演示 ===")
360 print(
"\n--- D触发器演示 ---")
362 d_ff.inputs[
"D"].set_state(LogicState.HIGH)
363 d_ff.enable_signal.set_state(LogicState.HIGH)
365 print(f
"D输入: {d_ff.inputs['D'].state}")
367 print(f
"Q输出: {d_ff.outputs['Q'].state}")
368 print(f
"Q_bar输出: {d_ff.outputs['Q_bar'].state}")
371 print(
"\n--- 4位计数器演示 ---")
372 counter =
Counter(
"Counter_4bit", width=4, count_up=
True)
373 counter.enable_signal.set_state(LogicState.HIGH)
377 counter.on_clock_edge()
378 count_value = counter.internal_state[
"count"]
379 print(f
"时钟{i+1}: 计数值={count_value}, 零标志={counter.outputs['zero'].state}")
382 print(
"\n--- 处理器状态机演示 ---")
387 cpu_fsm.input_signals[
"start"].set_state(LogicState.HIGH)
388 cpu_fsm.process_inputs()
390 cpu_fsm.input_signals[
"instruction_ready"].set_state(LogicState.HIGH)
391 cpu_fsm.process_inputs()
393 cpu_fsm.input_signals[
"decoded"].set_state(LogicState.HIGH)
394 cpu_fsm.process_inputs()
396 cpu_fsm.input_signals[
"executed"].set_state(LogicState.HIGH)
397 cpu_fsm.process_inputs()
399 cpu_fsm.input_signals[
"written_back"].set_state(LogicState.HIGH)
400 cpu_fsm.process_inputs()
402 print(
"\n=== 总结 ===")
403 print(
"✓ 复杂逻辑电路使用类来处理时序和状态")
404 print(
"✓ 基本组件(电阻、电容等)使用数据库存储")
405 print(
"✓ 这种混合架构更适合EDA工具的实际需求")
406 print(
"✓ 状态机和时序逻辑需要复杂的行为建模")
407 print(
"✓ 数据库存储提供了更大的灵活性")
__init__(self, str name, int width=8, bool count_up=True)
__init__(self, str name, str ff_type="D")
add_output(self, str signal_name)
add_input(self, str signal_name)
set_state(self, LogicState state)
add_state(self, str state)
transition_to(self, str new_state)
add_output(self, str signal_name)
add_transition(self, str from_state, str to_state, str condition)
add_input(self, str signal_name)
List[Tuple[str, float]] get_state_history(self)