1 # XXX Adapt test to new architecture
4 self
.clk_freq
= 83333333
6 self
.pads
= Record([("rx", 1), ("tx", 1)])
7 self
.submodules
.slave
= UART(self
.pads
, self
.clk_freq
, self
.baud
)
9 def wait_for(self
, ns_time
):
10 freq_in_ghz
= self
.clk_freq
/(10**9)
11 period
= 1/freq_in_ghz
12 num_loops
= int(ns_time
/period
)
13 for i
in range(num_loops
+1):
16 def gen_simulation(self
, selfp
):
17 baud_in_ghz
= self
.baud
/(10**9)
18 uart_period
= int(1/baud_in_ghz
)
19 half_uart_period
= int(1/(2*baud_in_ghz
))
21 # Set TX an RX lines idle
26 # First send a few characters
29 print("Sending string: " + tx_string
)
31 selfp
.slave
._r
_rxtx
.r
= ord(c
)
32 selfp
.slave
._r
_rxtx
.re
= 1
34 selfp
.slave
._r
_rxtx
.re
= 0
36 yield from self
.wait_for(half_uart_period
)
39 print("FAILURE: no start bit sent")
43 yield from self
.wait_for(uart_period
)
48 yield from self
.wait_for(uart_period
)
50 if selfp
.pads
.tx
== 0:
51 print("FAILURE: no stop bit sent")
54 print("FAILURE: sent decimal value "+str(val
)+" (char "+chr(val
)+") instead of "+c
)
56 print("SUCCESS: sent "+c
)
57 while selfp
.slave
.ev
.tx
.trigger
!= 1:
60 # Then receive a character
63 print("Receiving character "+rx_string
)
64 rx_value
= ord(rx_string
)
76 selfp
.pads
.rx
= 1 if (rx_value
& 1) else 0
78 yield from self
.wait_for(uart_period
)
80 rx_value
= ord(rx_string
)
81 received_value
= selfp
.slave
._r
_rxtx
.w
82 if (received_value
== rx_value
):
87 print("received "+chr(received_value
))
92 if __name__
== "__main__":
93 from migen
.sim
.generic
import Simulator
, TopLevel
94 from migen
.sim
import icarus
95 with
Simulator(UARTTB(), TopLevel("top.vcd", clk_period
=int(1/0.08333333)),
96 icarus
.Runner(keep_files
=False)) as s
: