|
12 | 12 | import veriloggen.thread as vthread |
13 | 13 |
|
14 | 14 |
|
15 | | -class UartTx(Submodule): |
16 | | - __intrinsics__ = ('send', ) |
17 | | - |
18 | | - def __init__(self, m, name, prefix, clk, rst, txd=None, |
19 | | - arg_params=None, arg_ports=None, |
20 | | - as_io=None, as_wire=None, |
21 | | - baudrate=19200, clockfreq=100 * 1000 * 1000): |
22 | | - |
23 | | - if arg_ports is None: |
24 | | - arg_ports = [] |
25 | | - |
26 | | - arg_ports.insert(0, ('CLK', clk)) |
27 | | - arg_ports.insert(1, ('RST', rst)) |
28 | | - |
29 | | - if txd is not None: |
30 | | - arg_ports.insert(2, ('txd', txd)) |
31 | | - |
32 | | - moddef = mkUartTx(baudrate, clockfreq) |
33 | | - |
34 | | - Submodule.__init__(self, m, moddef, name, prefix, |
35 | | - arg_params=arg_params, arg_ports=arg_ports, |
36 | | - as_io=as_io, as_wire=as_wire) |
37 | | - |
38 | | - self.tx_din = self['din'] |
39 | | - self.tx_enable = self['enable'] |
40 | | - self.tx_enable.initval = 0 |
41 | | - self.tx_ready = self['ready'] |
42 | | - |
43 | | - def send(self, fsm, value): |
44 | | - fsm( |
45 | | - self.tx_din(value), |
46 | | - self.tx_enable(1) |
47 | | - ) |
48 | | - fsm.goto_next() |
49 | | - fsm( |
50 | | - self.tx_enable(0) |
51 | | - ) |
52 | | - fsm.goto_next() |
53 | | - fsm.If(self.tx_ready).goto_next() |
54 | | - |
55 | | - |
56 | | -def mkUartTx(baudrate=19200, clockfreq=100 * 1000 * 1000): |
57 | | - m = Module("UartTx") |
58 | | - waitnum = int(clockfreq / baudrate) |
59 | | - |
60 | | - clk = m.Input('CLK') |
61 | | - rst = m.Input('RST') |
62 | | - |
63 | | - din = m.Input('din', 8) |
64 | | - enable = m.Input('enable') |
65 | | - ready = m.OutputReg('ready', initval=1) |
66 | | - txd = m.OutputReg('txd', initval=1) |
67 | | - |
68 | | - fsm = FSM(m, 'fsm', clk, rst) |
69 | | - |
70 | | - mem = m.TmpReg(9, initval=0) |
71 | | - waitcount = m.TmpReg(int(math.log(waitnum, 2)) + 1, initval=0) |
72 | | - |
73 | | - fsm( |
74 | | - waitcount(waitnum - 1), |
75 | | - txd(1), |
76 | | - mem(Cat(din, Int(0, 1))) |
77 | | - ) |
78 | | - |
79 | | - fsm.If(enable)( |
80 | | - ready(0) |
81 | | - ) |
82 | | - |
83 | | - fsm.Then().goto_next() |
84 | | - |
85 | | - for i in range(10): |
86 | | - fsm.If(waitcount > 0)( |
87 | | - waitcount.dec() |
88 | | - ).Else( |
89 | | - txd(mem[0]), |
90 | | - mem(Cat(Int(1, 1), mem[1:9])), |
91 | | - waitcount(waitnum - 1) |
92 | | - ) |
93 | | - fsm.Then().goto_next() |
94 | | - |
95 | | - fsm( |
96 | | - ready(1) |
97 | | - ) |
98 | | - |
99 | | - fsm.goto_init() |
100 | | - |
101 | | - fsm.make_always() |
102 | | - |
103 | | - return m |
104 | | - |
105 | | - |
106 | | -class UartRx(Submodule): |
107 | | - __intrinsics__ = ('recv', ) |
108 | | - |
109 | | - def __init__(self, m, name, prefix, clk, rst, rxd=None, |
110 | | - arg_params=None, arg_ports=None, |
111 | | - as_io=None, as_wire=None, |
112 | | - baudrate=19200, clockfreq=100 * 1000 * 1000): |
113 | | - |
114 | | - if arg_ports is None: |
115 | | - arg_ports = [] |
116 | | - |
117 | | - arg_ports.insert(0, ('CLK', clk)) |
118 | | - arg_ports.insert(1, ('RST', rst)) |
119 | | - |
120 | | - if rxd is not None: |
121 | | - arg_ports.insert(2, ('rxd', rxd)) |
122 | | - |
123 | | - moddef = mkUartRx(baudrate, clockfreq) |
124 | | - Submodule.__init__(self, m, moddef, name, prefix, |
125 | | - arg_params=arg_params, arg_ports=arg_ports, |
126 | | - as_io=as_io, as_wire=as_wire) |
127 | | - |
128 | | - self.rx_dout = self['dout'] |
129 | | - self.rx_valid = self['valid'] |
130 | | - |
131 | | - def recv(self, fsm): |
132 | | - ret = fsm.m.TmpReg(self.rx_dout.width) |
133 | | - fsm.If(self.rx_valid)( |
134 | | - ret(self.rx_dout) |
135 | | - ) |
136 | | - fsm.Then().goto_next() |
137 | | - return ret |
138 | | - |
139 | | - |
140 | | -def mkUartRx(baudrate=19200, clockfreq=100 * 1000 * 1000): |
141 | | - m = Module("UartRx") |
142 | | - waitnum = int(clockfreq / baudrate) |
143 | | - |
144 | | - clk = m.Input('CLK') |
145 | | - rst = m.Input('RST') |
146 | | - |
147 | | - rxd = m.Input('rxd') |
148 | | - dout = m.OutputReg('dout', 8, initval=0) |
149 | | - valid = m.OutputReg('valid', initval=0) |
150 | | - |
151 | | - fsm = FSM(m, 'fsm', clk, rst) |
152 | | - |
153 | | - mem = m.TmpReg(9, initval=0) |
154 | | - waitcount = m.TmpReg(int(math.log(waitnum, 2)) + 1, initval=0) |
155 | | - |
156 | | - fsm( |
157 | | - valid(0), |
158 | | - waitcount(int(waitnum / 2) - 1), |
159 | | - mem(Cat(rxd, mem[1:9])) |
160 | | - ) |
161 | | - |
162 | | - fsm.If(rxd == 0).goto_next() |
163 | | - |
164 | | - for i in range(10): |
165 | | - if i == 0: # check the start bit again |
166 | | - fsm.If(Ands(waitcount == 1, rxd != 0)).goto_init() |
167 | | - |
168 | | - fsm.If(waitcount > 0)( |
169 | | - waitcount.dec() |
170 | | - ).Else( |
171 | | - mem(Cat(rxd, mem[1:9])), |
172 | | - waitcount(waitnum - 1) |
173 | | - ) |
174 | | - fsm.Then().goto_next() |
175 | | - |
176 | | - fsm( |
177 | | - valid(1), |
178 | | - dout(mem[0:9]) |
179 | | - ) |
180 | | - |
181 | | - fsm.goto_init() |
182 | | - |
183 | | - fsm.make_always() |
184 | | - |
185 | | - return m |
186 | | - |
187 | | - |
188 | 15 | def mkTop(clk_name='clk', rst_name='btnCpuReset'): |
189 | 16 | m = Module('top') |
190 | 17 | clk = m.Input(clk_name) |
@@ -227,10 +54,10 @@ def mkLed(baudrate=19200, clockfreq=100 * 1000 * 1000): |
227 | 54 | led = m.OutputReg('led', 16, initval=0) |
228 | 55 | tx = m.Output('utx') |
229 | 56 | rx = m.Input('urx') |
230 | | - uart_tx = UartTx(m, 'inst_tx', 'tx_', clk, rst, tx, |
231 | | - baudrate=baudrate, clockfreq=clockfreq) |
232 | | - uart_rx = UartRx(m, 'inst_rx', 'rx_', clk, rst, rx, |
233 | | - baudrate=baudrate, clockfreq=clockfreq) |
| 57 | + uart_tx = vthread.UartTx(m, 'inst_tx', 'tx_', clk, rst, tx, |
| 58 | + baudrate=baudrate, clockfreq=clockfreq) |
| 59 | + uart_rx = vthread.UartRx(m, 'inst_rx', 'rx_', clk, rst, rx, |
| 60 | + baudrate=baudrate, clockfreq=clockfreq) |
234 | 61 |
|
235 | 62 | def blink(): |
236 | 63 | while True: |
@@ -258,10 +85,10 @@ def mkTest(baudrate=19200, clockfreq=19200 * 10): |
258 | 85 | rx = uut['urx'] |
259 | 86 | sw = uut['sw'] |
260 | 87 |
|
261 | | - uart_tx = UartTx(m, 'inst_tx', 'tx_', clk, rst, as_wire='txd', |
262 | | - baudrate=baudrate, clockfreq=clockfreq) |
263 | | - uart_rx = UartRx(m, 'inst_rx', 'rx_', clk, rst, as_wire='rxd', |
264 | | - baudrate=baudrate, clockfreq=clockfreq) |
| 88 | + uart_tx = vthread.UartTx(m, 'inst_tx', 'tx_', clk, rst, as_wire='txd', |
| 89 | + baudrate=baudrate, clockfreq=clockfreq) |
| 90 | + uart_rx = vthread.UartRx(m, 'inst_rx', 'rx_', clk, rst, as_wire='rxd', |
| 91 | + baudrate=baudrate, clockfreq=clockfreq) |
265 | 92 |
|
266 | 93 | txd = uart_tx['txd'] |
267 | 94 | rxd = uart_rx['rxd'] |
|
0 commit comments