-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart_sim.py
65 lines (50 loc) · 1.58 KB
/
uart_sim.py
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
import os, pty
from amaranth import *
from amaranth.sim import *
from nmigen_lib import UART
class UartSim(Elaboratable):
def __init__(self, base_freq, uart_freq):
self.rx_data = Signal(unsigned(8))
self.tx_data = Signal(unsigned(8))
self.tx_trg = Signal()
self.tx_rdy = Signal()
self.base_freq = base_freq
self.uart_freq = uart_freq
self.master, self.slave = pty.openpty()
name = os.ttyname(self.slave)
print(f"Listening on {name}")
def elaborate(self, platform):
divisor = int(self.base_freq // self.uart_freq)
uart = UART(divisor=divisor)
m = Module()
m.d.comb += [
uart.tx_data.eq(self.tx_data),
uart.tx_trg.eq(self.tx_trg),
uart.rx_pin.eq(uart.tx_pin),
self.tx_rdy.eq(uart.tx_rdy)
]
m.submodules.uart = uart
return m
def read_char(self):
c = os.read(self.master, 1)
print(f"read_char: '{c}'")
return c
if __name__ == '__main__':
dut = UartSim(100e6, 115200)
sim = Simulator(dut)
def proc():
while True:
c = dut.read_char()
yield dut.tx_data.eq(ord(c))
yield dut.tx_trg.eq(1)
yield
yield dut.tx_trg.eq(0)
yield
if c == b'0':
break
while (yield dut.tx_rdy) == 0:
yield
sim.add_clock(1/10e6)
sim.add_sync_process(proc)
with sim.write_vcd('uart_sim.vcd', 'uart_sim_orig.gtkw'):
sim.run_until(10)