ceb8f664d01fa349e8b83c62cc95ddb07ef28dc2
1 # This file is Copyright (c) 2020 LambdaConcept <contact@lambdaconcept.com>
7 from nmigen
.asserts
import Assert
, Assume
8 from nmigen_soc
import wishbone
, memory
9 from nmigen
.lib
.cdc
import ResetSynchronizer
11 from lambdasoc
.periph
import Peripheral
12 from lambdasoc
.soc
.base
import SoC
14 from gram
.common
import *
15 from gram
.core
import gramCore
16 from gram
.phy
.fakephy
import FakePHY
, SDRAM_VERBOSE_STD
, SDRAM_VERBOSE_DBG
17 from gram
.modules
import MT41K256M16
18 from gram
.frontend
.wishbone
import gramWishbone
20 from gram
.core
.multiplexer
import _AntiStarvation
21 from gram
.test
.utils
import *
24 class DDR3SoC(SoC
, Elaboratable
):
25 def __init__(self
, *, clk_freq
, dramcore_addr
,
27 self
._decoder
= wishbone
.Decoder(addr_width
=30, data_width
=32, granularity
=8,
28 features
={"cti", "bte"})
30 self
.bus
= wishbone
.Interface(addr_width
=30, data_width
=32, granularity
=8)
38 cl
, cwl
= get_cl_cw("DDR3", tck
)
39 cl_sys_latency
= get_sys_latency(nphases
, cl
)
40 cwl_sys_latency
= get_sys_latency(nphases
, cwl
)
41 rdcmdphase
, rdphase
= get_sys_phases(nphases
, cl_sys_latency
, cl
)
42 wrcmdphase
, wrphase
= get_sys_phases(nphases
, cwl_sys_latency
, cwl
)
43 physettings
= PhySettings(
47 dfi_databits
=4*databits
,
52 rdcmdphase
=rdcmdphase
,
53 wrcmdphase
=wrcmdphase
,
56 read_latency
=2 + cl_sys_latency
+ 2 + log2_int(4//nphases
) + 4,
57 write_latency
=cwl_sys_latency
60 ddrmodule
= MT41K256M16(clk_freq
, "1:2")
61 self
.ddrphy
= FakePHY(module
=ddrmodule
,
63 verbosity
=SDRAM_VERBOSE_DBG
)
65 self
.dramcore
= gramCore(
67 geom_settings
=ddrmodule
.geom_settings
,
68 timing_settings
=ddrmodule
.timing_settings
,
70 self
._decoder
.add(self
.dramcore
.bus
, addr
=dramcore_addr
)
72 self
.drambone
= gramWishbone(self
.dramcore
)
73 self
._decoder
.add(self
.drambone
.bus
, addr
=ddr_addr
)
75 self
.memory_map
= self
._decoder
.bus
.memory_map
77 self
.clk_freq
= clk_freq
79 def elaborate(self
, platform
):
82 m
.submodules
.decoder
= self
._decoder
83 m
.submodules
.ddrphy
= self
.ddrphy
84 m
.submodules
.dramcore
= self
.dramcore
85 m
.submodules
.drambone
= self
.drambone
88 self
.bus
.connect(self
._decoder
.bus
),
93 class SocTestCase(FHDLTestCase
):
95 yield from wb_write(bus
, 0x0, 0xE, 0xF) # DFII_CONTROL_ODT|DFII_CONTROL_RESET_N|DFI_CONTROL_CKE
96 yield from wb_write(bus
, 0xC >> 2, 0x0, 0xF)
97 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
98 yield from wb_write(bus
, 0x0, 0xC, 0xF)
99 yield from wb_write(bus
, 0x0, 0xE, 0xF)
102 yield from wb_write(bus
, 0xC >> 2, 0x200, 0xF)
103 yield from wb_write(bus
, 0x10 >> 2, 0x2, 0xF)
104 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
105 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
108 yield from wb_write(bus
, 0xC >> 2, 0x0, 0xF)
109 yield from wb_write(bus
, 0x10 >> 2, 0x3, 0xF)
110 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
111 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
114 yield from wb_write(bus
, 0xC >> 2, 0x6, 0xF)
115 yield from wb_write(bus
, 0x10 >> 2, 0x1, 0xF)
116 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
117 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
120 yield from wb_write(bus
, 0xC >> 2, 0x320, 0xF)
121 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
122 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
123 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
128 yield from wb_write(bus
, 0xC >> 2, 0x400, 0xF)
129 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
130 yield from wb_write(bus
, 0x4 >> 2, 0x3, 0xF)
131 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
135 yield from wb_write(bus
, 0, 0x1, 0xF)
136 for i
in range(2000):
139 def test_multiple_reads(self
):
140 soc
= DDR3SoC(clk_freq
=100e6
,
141 dramcore_addr
=0x00000000,
145 yield from SocTestCase
.init_seq(soc
.bus
)
147 yield from wb_write(soc
.bus
, 0x10000000 >> 2, 0xACAB2020, 0xF, 128)
150 # Check for data persistence
152 res
= yield from wb_read(soc
.bus
, 0x10000000 >> 2, 0xF, 128)
154 self
.assertEqual(res
, 0xACAB2020)
156 runSimulation(soc
, process
, "test_soc_multiple_reads.vcd")
158 def test_interleaved_read_write(self
):
159 soc
= DDR3SoC(clk_freq
=100e6
,
160 dramcore_addr
=0x00000000,
164 yield from SocTestCase
.init_seq(soc
.bus
)
166 yield from wb_write(soc
.bus
, 0x10000000 >> 2, 0xF00DFACE, 0xF, 128)
167 yield from wb_write(soc
.bus
, 0x10000004 >> 2, 0x12345678, 0xF, 128)
168 yield from wb_write(soc
.bus
, 0x10000008 >> 2, 0x00BA0BAB, 0xF, 128)
170 res
= yield from wb_read(soc
.bus
, 0x10000000 >> 2, 0xF, 128)
171 self
.assertEqual(res
, 0xF00DFACE)
173 yield from wb_write(soc
.bus
, 0x10000008 >> 2, 0xCAFE1000, 0xF, 128)
175 res
= yield from wb_read(soc
.bus
, 0x10000004 >> 2, 0xF, 128)
176 self
.assertEqual(res
, 0x12345678)
178 res
= yield from wb_read(soc
.bus
, 0x10000008 >> 2, 0xF, 128)
179 self
.assertEqual(res
, 0xCAFE1000)
181 runSimulation(soc
, process
, "test_soc_interleaved_read_write.vcd")
183 def test_random_memtest(self
):
184 soc
= DDR3SoC(clk_freq
=100e6
,
185 dramcore_addr
=0x00000000,
189 yield from SocTestCase
.init_seq(soc
.bus
)
195 memtest_values
.append(random
.randint(0, 0xFFFFFFFF))
199 yield from wb_write(soc
.bus
, (0x10000000 >> 2) + i
, memtest_values
[i
], 0xF, 256)
203 self
.assertEqual(memtest_values
[i
], (yield from wb_read(soc
.bus
, (0x10000000 >> 2) + i
, 0xF, 256)))
205 runSimulation(soc
, process
, "test_soc_random_memtest.vcd")
207 def test_continuous_memtest(self
):
208 soc
= DDR3SoC(clk_freq
=100e6
,
209 dramcore_addr
=0x00000000,
213 yield from SocTestCase
.init_seq(soc
.bus
)
219 yield from wb_write(soc
.bus
, (0x10000000 >> 2) + i
, 0xFACE0000 | i
, 0xF, 256)
223 self
.assertEqual(0xFACE0000 | i
, (yield from wb_read(soc
.bus
, (0x10000000 >> 2) + i
, 0xF, 256)))
225 runSimulation(soc
, process
, "test_soc_continuous_memtest.vcd")
228 if __name__
== '__main__':