77
88from amaranth import *
99from amaranth .lib import wiring
10- from amaranth .lib .wiring import Out , connect , flipped
10+ from amaranth .lib .wiring import In , Out , connect , flipped
1111from amaranth .utils import ceil_log2
1212
1313from amaranth .sim import Simulator
2323class HyperRAMPins (wiring .PureInterface ):
2424 class Signature (wiring .Signature ):
2525 def __init__ (self , * , cs_count = 1 ):
26- self .cs_count = cs_count
2726 super ().__init__ ({
28- "clk" : Out (OutputPinSignature (1 )),
29- "csn" : Out (OutputPinSignature (cs_count )),
30- "rstn" : Out (OutputPinSignature (1 )),
27+ "clk" : Out (OutputPinSignature (1 )),
28+ "csn" : Out (OutputPinSignature (cs_count )),
29+ "rstn" : Out (OutputPinSignature (1 )),
3130 "rwds" : Out (BidirPinSignature (1 )),
32- "dq" : Out (BidirPinSignature (1 )),
31+ "dq" : Out (BidirPinSignature (8 )),
3332 })
3433
3534 def create (self , * , path = (), src_loc_at = 0 ):
3635 return HyperRAMPins (cs_count = self .cs_count , src_loc_at = 1 + src_loc_at )
3736
3837 def __init__ (self , * , cs_count = 1 , path = (), src_loc_at = 0 ):
3938 super ().__init__ (self .Signature (cs_count = cs_count ), path = path , src_loc_at = 1 + src_loc_at )
39+ self .cs_count = cs_count
4040
4141
4242class HyperRAM (wiring .Component ):
@@ -59,14 +59,14 @@ class HRAMConfig(csr.Register, access="w"):
5959 """
6060 def __init__ (self , mem_name = ("mem" ,), * , pins , init_latency = 7 ):
6161 self .pins = pins
62- self .cs_count = len ( self . pins .csn_o )
62+ self .cs_count = pins .cs_count
6363 self .size = 2 ** 23 * self .cs_count # 8MB per CS pin
6464 self .init_latency = init_latency
6565 assert self .init_latency in (6 , 7 ) # TODO: anything else possible ?
6666
6767 regs = csr .Builder (addr_width = 3 , data_width = 8 )
6868
69- self ._ctrl_cfg = regs .add ("ctrl_cfg" , self .CtrlConfig (), offset = 0x0 )
69+ self ._ctrl_cfg = regs .add ("ctrl_cfg" , self .CtrlConfig (init_latency ), offset = 0x0 )
7070 self ._hram_cfg = regs .add ("hram_cfg" , self .HRAMConfig (), offset = 0x4 )
7171
7272 self ._bridge = csr .Bridge (regs .as_memory_map ())
@@ -76,9 +76,9 @@ def __init__(self, mem_name=("mem",), *, pins, init_latency=7):
7676 data_memory_map .add_resource (name = mem_name , size = self .size , resource = self )
7777
7878 super ().__init__ ({
79- "ctrl_bus" : csr .Signature (addr_width = regs .addr_width , data_width = regs .data_width ),
80- "data_bus" : wishbone .Signature (addr_width = ceil_log2 (self .size / 4 ), data_width = 32 ,
81- granularity = 8 ),
79+ "ctrl_bus" : In ( csr .Signature (addr_width = regs .addr_width , data_width = regs .data_width ) ),
80+ "data_bus" : In ( wishbone .Signature (addr_width = ceil_log2 (self .size >> 2 ), data_width = 32 ,
81+ granularity = 8 ) ),
8282 })
8383 self .ctrl_bus .memory_map = ctrl_memory_map
8484 self .data_bus .memory_map = data_memory_map
@@ -87,7 +87,7 @@ def elaborate(self, platform):
8787 m = Module ()
8888 m .submodules .bridge = self ._bridge
8989
90- connect (m , flipped (self .bus ), self ._bridge .bus )
90+ connect (m , flipped (self .ctrl_bus ), self ._bridge .bus )
9191
9292 is_ctrl_write = Signal ()
9393 latched_adr = Signal (len (self .data_bus .adr ))
@@ -119,27 +119,27 @@ def elaborate(self, platform):
119119 m .d .sync += counter .eq (counter - 1 )
120120 with m .If (counter .any ()):
121121 # move shift register (sample/output data) on posedge
122- m .d .sync += sr .eq (Cat (self .pins .dq_i , sr [:- 8 ]))
122+ m .d .sync += sr .eq (Cat (self .pins .dq . i , sr [:- 8 ]))
123123
124124 m .d .comb += [
125- self .pins .clk_o .eq (clk ),
126- self .pins .csn_o .eq (csn ),
127- self .pins .rstn_o .eq (~ ResetSignal ()),
128- self .pins .dq_o .eq (sr [- 8 :]),
125+ self .pins .clk . o .eq (clk ),
126+ self .pins .csn . o .eq (csn ),
127+ self .pins .rstn . o .eq (~ ResetSignal ()),
128+ self .pins .dq . o .eq (sr [- 8 :]),
129129 self .data_bus .dat_r .eq (sr [:32 ]),
130130 ]
131131
132132 with m .FSM () as fsm :
133133 with m .State ("IDLE" ):
134134 m .d .sync += [
135135 counter .eq (0 ),
136- self .pins .rwds_oe .eq (0 ),
136+ self .pins .rwds . oe .eq (0 ),
137137 csn .eq ((1 << self .cs_count ) - 1 ), # all disabled
138138 ]
139139 with m .If (self .data_bus .stb & self .data_bus .cyc ): # data bus activity
140140 m .d .sync += [
141141 csn .eq (~ (1 << (self .data_bus .adr [21 :]))),
142- self .pins .dq_oe .eq (1 ),
142+ self .pins .dq . oe .eq (1 ),
143143 counter .eq (6 ),
144144 # Assign CA
145145 sr [47 ].eq (~ self .data_bus .we ), # R/W#
@@ -157,7 +157,7 @@ def elaborate(self, platform):
157157 with m .If (self ._hram_cfg .f .val .w_stb ): # config register write
158158 m .d .sync += [
159159 csn .eq (~ (1 << (self ._hram_cfg .f .val .w_data [16 :16 + ceil_log2 (self .cs_count )]))),
160- self .pins .dq_oe .eq (1 ),
160+ self .pins .dq . oe .eq (1 ),
161161 counter .eq (6 ),
162162 # Assign CA
163163 sr [47 ].eq (0 ), # R/W#
@@ -177,7 +177,7 @@ def elaborate(self, platform):
177177 with m .If (counter == 3 ):
178178 # RWDS tells us if we need 2x latency or not
179179 # sample at an arbitrary midpoint in CA
180- m .d .sync += x2_lat .eq (self .pins .rwds_i )
180+ m .d .sync += x2_lat .eq (self .pins .rwds . i )
181181 with m .If (counter == 1 ):
182182 # (almost) done shifting CA
183183 with m .If (is_ctrl_write ):
@@ -195,31 +195,31 @@ def elaborate(self, platform):
195195 m .d .sync += counter .eq (2 * self ._ctrl_cfg .f .latency .data - 2 )
196196 m .next = "WAIT_LAT"
197197 with m .State ("WAIT_LAT" ):
198- m .d .sync += self .pins .dq_oe .eq (0 )
198+ m .d .sync += self .pins .dq . oe .eq (0 )
199199 with m .If (counter == 1 ):
200200 # About to shift data
201201 m .d .sync += [
202202 sr [:16 ].eq (0 ),
203203 sr [16 :].eq (self .data_bus .dat_w ),
204- self .pins .dq_oe .eq (self .data_bus .we ),
205- self .pins .rwds_oe .eq (self .data_bus .we ),
206- self .pins .rwds_o .eq (~ self .data_bus .sel [3 ]),
204+ self .pins .dq . oe .eq (self .data_bus .we ),
205+ self .pins .rwds . oe .eq (self .data_bus .we ),
206+ self .pins .rwds . o .eq (~ self .data_bus .sel [3 ]),
207207 counter .eq (4 ),
208208 ]
209209 m .next = "SHIFT_DAT"
210210 with m .State ("SHIFT_DAT" ):
211211 with m .If (counter == 4 ):
212- m .d .sync += self .pins .rwds_o .eq (~ self .data_bus .sel [2 ])
212+ m .d .sync += self .pins .rwds . o .eq (~ self .data_bus .sel [2 ])
213213 with m .If (counter == 3 ):
214- m .d .sync += self .pins .rwds_o .eq (~ self .data_bus .sel [1 ])
214+ m .d .sync += self .pins .rwds . o .eq (~ self .data_bus .sel [1 ])
215215 with m .If (counter == 2 ):
216- m .d .sync += self .pins .rwds_o .eq (~ self .data_bus .sel [0 ])
216+ m .d .sync += self .pins .rwds . o .eq (~ self .data_bus .sel [0 ])
217217 with m .If (counter == 1 ):
218218 m .next = "ACK_XFER"
219219 with m .State ("ACK_XFER" ):
220220 m .d .sync += [
221- self .pins .rwds_oe .eq (0 ),
222- self .pins .dq_oe .eq (0 ),
221+ self .pins .rwds . oe .eq (0 ),
222+ self .pins .dq . oe .eq (0 ),
223223 self .data_bus .ack .eq (1 ),
224224 wait_count .eq (9 )
225225 ]
@@ -235,9 +235,9 @@ def elaborate(self, platform):
235235 m .d .sync += [
236236 sr [:16 ].eq (0 ),
237237 sr [16 :].eq (self .data_bus .dat_w ),
238- self .pins .dq_oe .eq (self .data_bus .we ),
239- self .pins .rwds_oe .eq (self .data_bus .we ),
240- self .pins .rwds_o .eq (~ self .data_bus .sel [3 ]),
238+ self .pins .dq . oe .eq (self .data_bus .we ),
239+ self .pins .rwds . oe .eq (self .data_bus .we ),
240+ self .pins .rwds . o .eq (~ self .data_bus .sel [3 ]),
241241 latched_adr .eq (self .data_bus .adr ),
242242 counter .eq (4 ),
243243 ]
@@ -254,8 +254,8 @@ def elaborate(self, platform):
254254 m .next = "CTRL_DONE"
255255 with m .State ("CTRL_DONE" ):
256256 m .d .sync += [
257- self .pins .rwds_oe .eq (0 ),
258- self .pins .dq_oe .eq (0 ),
257+ self .pins .rwds . oe .eq (0 ),
258+ self .pins .dq . oe .eq (0 ),
259259 csn .eq ((1 << self .cs_count ) - 1 ),
260260 ]
261261 m .next = "IDLE"
@@ -284,8 +284,8 @@ def process():
284284 yield hram .data_bus .we .eq (0 )
285285 yield hram .data_bus .stb .eq (1 )
286286 yield hram .data_bus .cyc .eq (1 )
287- yield pins .rwds_i .eq (1 )
288- yield pins .dq_i .eq (0xFF )
287+ yield pins .rwds . i .eq (1 )
288+ yield pins .dq . i .eq (0xFF )
289289 for i in range (100 ):
290290 if (yield hram .data_bus .ack ):
291291 yield hram .data_bus .stb .eq (0 )
0 commit comments