1 # This file is Copyright (c) 2020 LambdaConcept <contact@lambdaconcept.com>
7 from nmigen
.utils
import log2_int
9 from nmigen_soc
import wishbone
10 from nmigen_soc
.memory
import MemoryMap
11 from lambdasoc
.periph
import Peripheral
14 class gramWishbone(Peripheral
, Elaboratable
):
15 def __init__(self
, core
, data_width
= 32):
16 super().__init
__(name
="wishbone")
19 self
._port
= core
.crossbar
.get_native_port()
20 #self._port = core.crossbar.get_port(data_width=8, mode="read")
22 dram_size
= core
.size
//4
23 dram_addr_width
= log2_int(dram_size
)
26 self
.bus
= wishbone
.Interface(addr_width
=dram_addr_width
,
27 data_width
=self
.dw
, granularity
=granularity
)
29 map = MemoryMap(addr_width
=dram_addr_width
+
30 log2_int(granularity
)-1, data_width
=granularity
)
31 self
.bus
.memory_map
= map
33 def elaborate(self
, platform
):
38 self
._port
.wdata
.valid
.eq(self
.bus
.cyc
& self
.bus
.stb
& self
.bus
.we
),
39 self
._port
.wdata
.data
.eq(self
.bus
.dat_w
),
40 self
._port
.wdata
.we
.eq(self
.bus
.sel
),
45 self
.bus
.dat_r
.eq(self
._port
.rdata
.data
),
46 self
._port
.rdata
.ready
.eq(1),
50 ratio
= self
.dw
//2**int(log2(len(self
._port
.wdata
.data
)))
51 count
= Signal(range(max(ratio
, 2)))
53 with m
.State("Send-Cmd"):
55 self
._port
.cmd
.valid
.eq(self
.bus
.cyc
& self
.bus
.stb
),
56 self
._port
.cmd
.we
.eq(self
.bus
.we
),
57 self
._port
.cmd
.addr
.eq(self
.bus
.adr
*ratio
+ count
- adr_offset
),
59 with m
.If(self
._port
.cmd
.valid
& self
._port
.cmd
.ready
):
60 m
.d
.sync
+= count
.eq(count
+1)
61 with m
.If(count
== (max(ratio
, 2)-1)):
62 m
.d
.sync
+= count
.eq(0)
63 with m
.If(self
.bus
.we
):
68 with m
.State("Wait-Read"):
69 with m
.If(self
._port
.rdata
.valid
):
70 m
.d
.comb
+= self
.bus
.ack
.eq(1)
73 with m
.State("Wait-Write"):
74 with m
.If(self
._port
.wdata
.ready
):
75 m
.d
.comb
+= self
.bus
.ack
.eq(1)