1 # Test of HyperRAM class
3 # Copyright (c) 2019 Florent Kermarrec <florent@enjoy-digital.fr>
4 # Copyright (c) 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 # Based on code from Kermarrec, Licensed BSD-2-Clause
7 # Modifications for the Libre-SOC Project funded by NLnet and NGI POINTER
8 # under EU Grants 871528 and 957073, under the LGPLv3+ License
12 from nmigen
import (Record
, Module
, Signal
, Elaboratable
)
13 from nmigen
.compat
.sim
import run_simulation
15 from lambdasoc
.periph
.hyperram
import HyperRAM
18 return {"-": 1, "_": 0}[c
]
21 def wb_write(bus
, addr
, data
, sel
):
22 yield bus
.adr
.eq(addr
)
23 yield bus
.dat_w
.eq(data
)
29 while not (yield bus
.ack
):
35 def wb_read(bus
, addr
, sel
):
36 yield bus
.adr
.eq(addr
)
41 while not (yield bus
.ack
):
46 return (yield bus
.dat_r
)
53 def __init__(self
, dw
=8):
56 self
.dq
= Record([("oe", 1), ("o", dw
), ("i", dw
)])
57 self
.rwds
= Record([("oe", 1), ("o", dw
//8), ("i", dw
//8)])
60 class TestHyperRAMPHY(Elaboratable
):
61 def __init__(self
, pads
):
67 self
.dq_oe
= pads
.dq
.oe
68 self
.rwds_o
= pads
.rwds
.o
69 self
.rwds_oe
= Signal()
71 def elaborate(self
, platform
):
73 m
.d
.comb
+= self
.pads
.cs_n
.eq(~self
.cs
)
74 m
.d
.comb
+= self
.pads
.rwds
.oe
.eq(self
.rwds_oe
)
78 class TestHyperBusWrite(unittest
.TestCase
):
80 def test_hyperram_write(self
):
82 yield from wb_write(dut
.bus
, 0x1234, 0xdeadbeef, sel
=0b1001)
85 def hyperram_gen(dut
):
86 clk
= "___--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--_______"
87 cs_n
= "--________________________________________________________________------"
88 dq_oe
= "__------------____________________________________________--------______"
89 dq_o
= "002000048d000000000000000000000000000000000000000000000000deadbeef000000"
90 rwds_oe
= "__________________________________________________________--------______"
91 rwds_o
= "____________________________________________________________----________"
93 #clk = "___--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--_______"
94 #cs_n = "--________________________________________________________________------"
95 #dq_oe = "__------------____________________________________________--------______"
96 #dq_o = "002000048d000000000000000000000000000000000000000000000000deadbeef000000"
97 #rwds_oe = "__________________________________________________________--------______"
98 #rwds_o = "________________________________________________________________________"
101 if False: # useful for printing out expected vs results
102 for i
in range(len(clk
)):
104 print ("clk", c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
105 print ("cs_n", c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
106 print ("dq_oe", c2bool(dq_oe
[i
]), (yield dut
.phy
.pads
.dq
.oe
))
107 print ("dq_o", hex(int(dq_o
[2*(i
//2):2*(i
//2)+2], 16)),
108 hex((yield dut
.phy
.pads
.dq
.o
)))
109 print ("rwds_oe", c2bool(rwds_oe
[i
]),
110 (yield dut
.phy
.pads
.rwds
.oe
))
111 print ("rwds_o", c2bool(rwds_o
[i
]),
112 (yield dut
.phy
.pads
.rwds
.o
))
114 for i
in range(len(clk
)):
115 self
.assertEqual(c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
116 self
.assertEqual(c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
117 self
.assertEqual(c2bool(dq_oe
[i
]), (yield dut
.phy
.pads
.dq
.oe
))
118 self
.assertEqual(int(dq_o
[2*(i
//2):2*(i
//2)+2], 16),
119 (yield dut
.phy
.pads
.dq
.o
))
120 self
.assertEqual(c2bool(rwds_oe
[i
]),
121 (yield dut
.phy
.pads
.rwds
.oe
))
122 self
.assertEqual(c2bool(rwds_o
[i
]),
123 (yield dut
.phy
.pads
.rwds
.o
))
126 dut
= HyperRAM(io
=HyperRamPads(), phy_kls
=TestHyperRAMPHY
)
127 run_simulation(dut
, [fpga_gen(dut
), hyperram_gen(dut
)],
130 def test_hyperram_read(self
):
132 dat
= yield from wb_read(dut
.bus
, 0x1234, 0b1111)
133 self
.assertEqual(dat
, 0xdeadbeef)
134 dat
= yield from wb_read(dut
.bus
, 0x1235, 0b1111)
135 self
.assertEqual(dat
, 0xcafefade)
137 def hyperram_gen(dut
):
138 clk
= "___--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--_"
139 cs_n
= "--________________________________________________________________________________"
140 dq_oe
= "__------------____________________________________________________________________"
141 dq_o
= "00a000048d000000000000000000000000000000000000000000000000000000000000000000000000"
142 dq_i
= "0000000000000000000000000000000000000000000000000000000000deadbeefcafefade00000000"
143 rwds_oe
= "__________________________________________________________________________________"
148 for i
in range(len(clk
)):
150 yield dut
.phy
.pads
.dq
.i
.eq(int(dq_i
[2*(i
//2):2*(i
//2)+2], 16))
151 print ("clk", c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
152 print ("cs_n", c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
155 self
.assertEqual(c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
156 self
.assertEqual(c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
157 self
.assertEqual(c2bool(dq_oe
[i
]), (yield dut
.phy
.pads
.dq
.oe
))
158 self
.assertEqual(int(dq_o
[2*(i
//2):2*(i
//2)+2], 16),
159 (yield dut
.phy
.pads
.dq
.o
))
160 self
.assertEqual(c2bool(rwds_oe
[i
]),
161 (yield dut
.phy
.pads
.rwds
.oe
))
164 dut
= HyperRAM(io
=HyperRamPads(), phy_kls
=TestHyperRAMPHY
)
165 run_simulation(dut
, [fpga_gen(dut
), hyperram_gen(dut
)],
166 vcd_name
="rd_sim.vcd")
168 class TestHyperBusRead(unittest
.TestCase
):
169 def test_hyperram_read(self
):
171 dat
= yield from wb_read(dut
.bus
, 0x1234, 0b1111)
172 self
.assertEqual(dat
, 0xdeadbeef)
173 dat
= yield from wb_read(dut
.bus
, 0x1235, 0b1111)
174 self
.assertEqual(dat
, 0xcafefade)
181 def hyperram_gen(dut
):
182 clk
= "___--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--__--_"
183 cs_n
= "--________________________________________________________________________________"
184 dq_oe
= "__------------____________________________________________________________________"
185 dq_o
= "00a000048d000000000000000000000000000000000000000000000000000000000000000000000000"
186 dq_i
= "0000000000000000000000000000000000000000000000000000000000deadbeefcafefade00000000"
187 rwds_oe
= "__________________________________________________________________________________"
192 for i
in range(len(clk
)):
194 yield dut
.phy
.pads
.dq
.i
.eq(int(dq_i
[2*(i
//2):2*(i
//2)+2], 16))
195 print ("clk", c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
196 print ("cs_n", c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
197 print ("dq_oe", c2bool(dq_oe
[i
]), (yield dut
.phy
.pads
.dq
.oe
))
198 print ("dq_o", hex(int(dq_o
[2*(i
//2):2*(i
//2)+2], 16)),
199 hex((yield dut
.phy
.pads
.dq
.o
)))
200 print ("rwds_oe", c2bool(rwds_oe
[i
]),
201 (yield dut
.phy
.pads
.rwds
.oe
))
202 self
.assertEqual(c2bool(clk
[i
]), (yield dut
.phy
.pads
.clk
))
203 self
.assertEqual(c2bool(cs_n
[i
]), (yield dut
.phy
.pads
.cs_n
))
204 self
.assertEqual(c2bool(dq_oe
[i
]), (yield dut
.phy
.pads
.dq
.oe
))
205 self
.assertEqual(int(dq_o
[2*(i
//2):2*(i
//2)+2], 16),
206 (yield dut
.phy
.pads
.dq
.o
))
207 self
.assertEqual(c2bool(rwds_oe
[i
]),
208 (yield dut
.phy
.pads
.rwds
.oe
))
211 dut
= HyperRAM(io
=HyperRamPads(), phy_kls
=TestHyperRAMPHY
)
212 run_simulation(dut
, [fpga_gen(dut
), hyperram_gen(dut
)],
213 vcd_name
="rd_sim.vcd")
215 if __name__
== '__main__':
217 #t = TestHyperBusRead()
218 #t.test_hyperram_read()
219 #t = TestHyperBusWrite()
220 #t.test_hyperram_write()