2a96b3d872b423fbebf3f23f68e366f7d87d14a2
1 # This file is Copyright (c) 2020 LambdaConcept <contact@lambdaconcept.com>
6 from nmigen
.asserts
import Assert
, Assume
7 from nmigen_soc
import wishbone
, memory
8 from nmigen
.lib
.cdc
import ResetSynchronizer
10 from lambdasoc
.periph
import Peripheral
11 from lambdasoc
.soc
.base
import SoC
13 from gram
.common
import *
14 from gram
.core
import gramCore
15 from gram
.phy
.fakephy
import FakePHY
, SDRAM_VERBOSE_STD
, SDRAM_VERBOSE_DBG
16 from gram
.modules
import MT41K256M16
17 from gram
.frontend
.wishbone
import gramWishbone
19 from gram
.core
.multiplexer
import _AntiStarvation
20 from gram
.test
.utils
import *
22 class DDR3SoC(SoC
, Elaboratable
):
23 def __init__(self
, *, clk_freq
, dramcore_addr
,
25 self
._decoder
= wishbone
.Decoder(addr_width
=30, data_width
=32, granularity
=8,
26 features
={"cti", "bte"})
28 self
.bus
= wishbone
.Interface(addr_width
=30, data_width
=32, granularity
=8)
36 cl
, cwl
= get_cl_cw("DDR3", tck
)
37 cl_sys_latency
= get_sys_latency(nphases
, cl
)
38 cwl_sys_latency
= get_sys_latency(nphases
, cwl
)
39 rdcmdphase
, rdphase
= get_sys_phases(nphases
, cl_sys_latency
, cl
)
40 wrcmdphase
, wrphase
= get_sys_phases(nphases
, cwl_sys_latency
, cwl
)
41 physettings
= PhySettings(
45 dfi_databits
=4*databits
,
50 rdcmdphase
=rdcmdphase
,
51 wrcmdphase
=wrcmdphase
,
54 read_latency
=2 + cl_sys_latency
+ 2 + log2_int(4//nphases
) + 4,
55 write_latency
=cwl_sys_latency
58 ddrmodule
= MT41K256M16(clk_freq
, "1:2")
59 self
.ddrphy
= FakePHY(module
=ddrmodule
,
61 verbosity
=SDRAM_VERBOSE_DBG
)
63 self
.dramcore
= gramCore(
65 geom_settings
=ddrmodule
.geom_settings
,
66 timing_settings
=ddrmodule
.timing_settings
,
68 self
._decoder
.add(self
.dramcore
.bus
, addr
=dramcore_addr
)
70 self
.drambone
= gramWishbone(self
.dramcore
)
71 self
._decoder
.add(self
.drambone
.bus
, addr
=ddr_addr
)
73 self
.memory_map
= self
._decoder
.bus
.memory_map
75 self
.clk_freq
= clk_freq
77 def elaborate(self
, platform
):
80 m
.submodules
.decoder
= self
._decoder
81 m
.submodules
.ddrphy
= self
.ddrphy
82 m
.submodules
.dramcore
= self
.dramcore
83 m
.submodules
.drambone
= self
.drambone
86 self
.bus
.connect(self
._decoder
.bus
),
91 class SocTestCase(FHDLTestCase
):
93 yield from wb_write(bus
, 0x0, 0xE, 0xF) # DFII_CONTROL_ODT|DFII_CONTROL_RESET_N|DFI_CONTROL_CKE
94 yield from wb_write(bus
, 0xC >> 2, 0x0, 0xF)
95 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
96 yield from wb_write(bus
, 0x0, 0xC, 0xF)
97 yield from wb_write(bus
, 0x0, 0xE, 0xF)
100 yield from wb_write(bus
, 0xC >> 2, 0x200, 0xF)
101 yield from wb_write(bus
, 0x10 >> 2, 0x2, 0xF)
102 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
103 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
106 yield from wb_write(bus
, 0xC >> 2, 0x0, 0xF)
107 yield from wb_write(bus
, 0x10 >> 2, 0x3, 0xF)
108 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
109 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
112 yield from wb_write(bus
, 0xC >> 2, 0x6, 0xF)
113 yield from wb_write(bus
, 0x10 >> 2, 0x1, 0xF)
114 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
115 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
118 yield from wb_write(bus
, 0xC >> 2, 0x320, 0xF)
119 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
120 yield from wb_write(bus
, 0x4 >> 2, 0xF, 0xF)
121 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
126 yield from wb_write(bus
, 0xC >> 2, 0x400, 0xF)
127 yield from wb_write(bus
, 0x10 >> 2, 0x0, 0xF)
128 yield from wb_write(bus
, 0x4 >> 2, 0x3, 0xF)
129 yield from wb_write(bus
, 0x8 >> 2, 0x1, 0xF)
133 yield from wb_write(bus
, 0, 0x1, 0xF)
134 for i
in range(2000):
137 def test_multiple_reads(self
):
138 soc
= DDR3SoC(clk_freq
=100e6
,
139 dramcore_addr
=0x00000000,
143 yield from SocTestCase
.init_seq(soc
.bus
)
145 yield from wb_write(soc
.bus
, 0x10000000 >> 2, 0xACAB2020, 0xF, 128)
148 # Check for data persistence
150 res
= yield from wb_read(soc
.bus
, 0x10000000 >> 2, 0xF, 128)
152 self
.assertEqual(res
, 0xACAB2020)
154 runSimulation(soc
, process
, "test_soc_multiple_reads.vcd")
156 def test_interleaved_read_write(self
):
157 soc
= DDR3SoC(clk_freq
=100e6
,
158 dramcore_addr
=0x00000000,
162 yield from SocTestCase
.init_seq(soc
.bus
)
164 yield from wb_write(soc
.bus
, 0x10000000 >> 2, 0xF00DFACE, 0xF, 128)
165 yield from wb_write(soc
.bus
, 0x10000004 >> 2, 0x12345678, 0xF, 128)
166 yield from wb_write(soc
.bus
, 0x10000008 >> 2, 0x00BA0BAB, 0xF, 128)
168 res
= yield from wb_read(soc
.bus
, 0x10000000 >> 2, 0xF, 128)
169 self
.assertEqual(res
, 0xF00DFACE)
171 yield from wb_write(soc
.bus
, 0x10000008 >> 2, 0xCAFE1000, 0xF, 128)
173 res
= yield from wb_read(soc
.bus
, 0x10000004 >> 2, 0xF, 128)
174 self
.assertEqual(res
, 0x12345678)
176 res
= yield from wb_read(soc
.bus
, 0x10000008 >> 2, 0xF, 128)
177 self
.assertEqual(res
, 0xCAFE1000)
179 runSimulation(soc
, process
, "test_soc_interleaved_read_write.vcd")
181 def test_random_memtest(self
):
182 soc
= DDR3SoC(clk_freq
=100e6
,
183 dramcore_addr
=0x00000000,
187 yield from SocTestCase
.init_seq(soc
.bus
)
193 memtest_values
.append(random
.randint(0, 0xFFFFFFFF))
197 yield from wb_write(soc
.bus
, (0x10000000 >> 2) + i
, memtest_values
[i
], 0xF, 256)
201 self
.assertEqual(memtest_values
[i
], (yield from wb_read(soc
.bus
, (0x10000000 >> 2) + i
, 0xF, 256)))
203 runSimulation(soc
, process
, "test_soc_random_memtest.vcd")
205 def test_continuous_memtest(self
):
206 soc
= DDR3SoC(clk_freq
=100e6
,
207 dramcore_addr
=0x00000000,
211 yield from SocTestCase
.init_seq(soc
.bus
)
217 yield from wb_write(soc
.bus
, (0x10000000 >> 2) + i
, 0xFACE0000 | i
, 0xF, 256)
221 self
.assertEqual(0xFACE0000 | i
, (yield from wb_read(soc
.bus
, (0x10000000 >> 2) + i
, 0xF, 256)))
223 runSimulation(soc
, process
, "test_soc_continuous_memtest.vcd")