2015-02-28 04:27:16 -05:00
|
|
|
from misoclib.tools.litescope.common import *
|
2014-04-18 04:33:05 -04:00
|
|
|
from migen.bus import wishbone
|
2015-01-25 07:41:09 -05:00
|
|
|
from migen.genlib.misc import chooser
|
2015-02-02 08:23:01 -05:00
|
|
|
from migen.genlib.record import Record
|
|
|
|
from migen.flow.actor import Sink, Source
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2015-03-01 10:45:50 -05:00
|
|
|
from misoclib.com.uart.phy.serial import UARTPHYSerial
|
2014-09-24 15:56:15 -04:00
|
|
|
|
2015-05-01 11:42:00 -04:00
|
|
|
class LiteScopeWishboneBridge(Module):
|
2015-04-13 07:09:44 -04:00
|
|
|
cmds = {
|
2015-04-13 07:25:27 -04:00
|
|
|
"write": 0x01,
|
|
|
|
"read": 0x02
|
2015-04-13 07:09:44 -04:00
|
|
|
}
|
2015-04-13 07:56:24 -04:00
|
|
|
|
2015-05-01 11:42:00 -04:00
|
|
|
def __init__(self, phy, clk_freq):
|
2015-04-13 07:09:44 -04:00
|
|
|
self.wishbone = wishbone.Interface()
|
2015-04-13 07:56:24 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
byte_counter = Counter(3)
|
|
|
|
word_counter = Counter(8)
|
|
|
|
self.submodules += byte_counter, word_counter
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
cmd = Signal(8)
|
|
|
|
cmd_ce = Signal()
|
2015-01-14 10:23:20 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
length = Signal(8)
|
|
|
|
length_ce = Signal()
|
2015-01-14 10:23:20 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
address = Signal(32)
|
|
|
|
address_ce = Signal()
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
data = Signal(32)
|
|
|
|
rx_data_ce = Signal()
|
|
|
|
tx_data_ce = Signal()
|
2015-01-14 10:23:20 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
self.sync += [
|
2015-05-01 11:42:00 -04:00
|
|
|
If(cmd_ce, cmd.eq(phy.source.data)),
|
|
|
|
If(length_ce, length.eq(phy.source.data)),
|
|
|
|
If(address_ce, address.eq(Cat(phy.source.data, address[0:24]))),
|
2015-04-13 07:09:44 -04:00
|
|
|
If(rx_data_ce,
|
2015-05-01 11:42:00 -04:00
|
|
|
data.eq(Cat(phy.source.data, data[0:24]))
|
2015-04-13 07:09:44 -04:00
|
|
|
).Elif(tx_data_ce,
|
|
|
|
data.eq(self.wishbone.dat_r)
|
|
|
|
)
|
|
|
|
]
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
fsm = InsertReset(FSM(reset_state="IDLE"))
|
|
|
|
timeout = Timeout(clk_freq//10)
|
|
|
|
self.submodules += fsm, timeout
|
|
|
|
self.comb += [
|
|
|
|
timeout.ce.eq(1),
|
2015-05-01 14:19:49 -04:00
|
|
|
fsm.reset.eq(timeout.reached),
|
|
|
|
phy.source.ack.eq(1)
|
2015-04-13 07:09:44 -04:00
|
|
|
]
|
|
|
|
fsm.act("IDLE",
|
|
|
|
timeout.reset.eq(1),
|
2015-05-01 11:42:00 -04:00
|
|
|
If(phy.source.stb,
|
2015-04-13 07:09:44 -04:00
|
|
|
cmd_ce.eq(1),
|
2015-05-01 11:42:00 -04:00
|
|
|
If((phy.source.data == self.cmds["write"]) |
|
|
|
|
(phy.source.data == self.cmds["read"]),
|
2015-04-13 07:09:44 -04:00
|
|
|
NextState("RECEIVE_LENGTH")
|
|
|
|
),
|
|
|
|
byte_counter.reset.eq(1),
|
|
|
|
word_counter.reset.eq(1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_LENGTH",
|
2015-05-01 11:42:00 -04:00
|
|
|
If(phy.source.stb,
|
2015-04-13 07:09:44 -04:00
|
|
|
length_ce.eq(1),
|
|
|
|
NextState("RECEIVE_ADDRESS")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_ADDRESS",
|
2015-05-01 11:42:00 -04:00
|
|
|
If(phy.source.stb,
|
2015-04-13 07:09:44 -04:00
|
|
|
address_ce.eq(1),
|
|
|
|
byte_counter.ce.eq(1),
|
|
|
|
If(byte_counter.value == 3,
|
|
|
|
If(cmd == self.cmds["write"],
|
|
|
|
NextState("RECEIVE_DATA")
|
|
|
|
).Elif(cmd == self.cmds["read"],
|
|
|
|
NextState("READ_DATA")
|
|
|
|
),
|
|
|
|
byte_counter.reset.eq(1),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_DATA",
|
2015-05-01 11:42:00 -04:00
|
|
|
If(phy.source.stb,
|
2015-04-13 07:09:44 -04:00
|
|
|
rx_data_ce.eq(1),
|
|
|
|
byte_counter.ce.eq(1),
|
|
|
|
If(byte_counter.value == 3,
|
|
|
|
NextState("WRITE_DATA"),
|
|
|
|
byte_counter.reset.eq(1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.comb += [
|
|
|
|
self.wishbone.adr.eq(address + word_counter.value),
|
|
|
|
self.wishbone.dat_w.eq(data),
|
|
|
|
self.wishbone.sel.eq(2**flen(self.wishbone.sel)-1)
|
|
|
|
]
|
|
|
|
fsm.act("WRITE_DATA",
|
|
|
|
self.wishbone.stb.eq(1),
|
|
|
|
self.wishbone.we.eq(1),
|
|
|
|
self.wishbone.cyc.eq(1),
|
|
|
|
If(self.wishbone.ack,
|
|
|
|
word_counter.ce.eq(1),
|
|
|
|
If(word_counter.value == (length-1),
|
|
|
|
NextState("IDLE")
|
|
|
|
).Else(
|
|
|
|
NextState("RECEIVE_DATA")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("READ_DATA",
|
|
|
|
self.wishbone.stb.eq(1),
|
|
|
|
self.wishbone.we.eq(0),
|
|
|
|
self.wishbone.cyc.eq(1),
|
|
|
|
If(self.wishbone.ack,
|
|
|
|
tx_data_ce.eq(1),
|
|
|
|
NextState("SEND_DATA")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.comb += \
|
2015-05-01 11:42:00 -04:00
|
|
|
chooser(data, byte_counter.value, phy.sink.data, n=4, reverse=True)
|
2015-04-13 07:09:44 -04:00
|
|
|
fsm.act("SEND_DATA",
|
2015-05-01 11:42:00 -04:00
|
|
|
phy.sink.stb.eq(1),
|
|
|
|
If(phy.sink.ack,
|
2015-04-13 07:09:44 -04:00
|
|
|
byte_counter.ce.eq(1),
|
|
|
|
If(byte_counter.value == 3,
|
|
|
|
word_counter.ce.eq(1),
|
|
|
|
If(word_counter.value == (length-1),
|
|
|
|
NextState("IDLE")
|
|
|
|
).Else(
|
|
|
|
NextState("READ_DATA"),
|
|
|
|
byte_counter.reset.eq(1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|