add getting of "fast" SPRs over DMI interface
[libresoc-litex.git] / sim.py
1 #!/usr/bin/env python3
2
3 # Notes for "Debug" mode:
4 # both microwatt and Libre-SOC implement (pretty much) the same DMI
5 # interface. TBD: really, there should be an OPF Debug SIG which
6 # defines this properly. still, these two are interoperable.
7 # https://git.libre-soc.org/?p=soc.git;a=blob;f=src/soc/debug/dmi.py
8 # https://github.com/antonblanchard/microwatt/blob/master/core_debug.vhdl
9
10 import os
11 import argparse
12
13 from migen import (Signal, FSM, If, Display, Finish, NextValue, NextState)
14
15 from litex.build.generic_platform import Pins, Subsignal
16 from litex.build.sim import SimPlatform
17 from litex.build.io import CRG
18 from litex.build.sim.config import SimConfig
19
20 from litex.soc.integration.soc import SoCRegion
21 from litex.soc.integration.soc_core import SoCCore
22 from litex.soc.integration.soc_sdram import SoCSDRAM
23 from litex.soc.integration.builder import Builder
24 from litex.soc.integration.common import get_mem_data
25
26 from litedram import modules as litedram_modules
27 from litedram.phy.model import SDRAMPHYModel
28 from litex.tools.litex_sim import sdram_module_nphases, get_sdram_phy_settings
29
30 from litex.tools.litex_sim import Platform
31
32 from libresoc import LibreSoC
33 from microwatt import Microwatt
34
35 # HACK!
36 from litex.soc.integration.soc import SoCCSRHandler
37 SoCCSRHandler.supported_address_width.append(12)
38
39 # LibreSoCSim -----------------------------------------------------------------
40
41 class LibreSoCSim(SoCSDRAM):
42 def __init__(self, cpu="libresoc", variant="standardjtag", debug=False,
43 with_sdram=True,
44 sdram_module = "AS4C16M16",
45 #sdram_data_width = 16,
46 #sdram_module = "MT48LC16M16",
47 sdram_data_width = 16,
48 irq_reserved_irqs = {'uart': 0},
49 ):
50 assert cpu in ["libresoc", "microwatt"]
51 platform = Platform()
52 sys_clk_freq = int(100e6)
53
54 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
55 # "hello_world/hello_world.bin"
56 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
57 # "tests/1.bin"
58 #ram_fname = "/tmp/test.bin"
59 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
60 # "micropython/firmware.bin"
61 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
62 # "tests/xics/xics.bin"
63 ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
64 "tests/decrementer/decrementer.bin"
65 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
66 # "hello_world/hello_world.bin"
67 #ram_fname = "/home/lkcl/src/libresoc/microwatt/" \
68 # "tests/mmu/mmu.bin"
69 #ram_fname = None
70
71 # reserve XICS ICP and XICS memory addresses.
72 self.mem_map['xicsicp'] = 0xc0004000
73 self.mem_map['xicsics'] = 0xc0005000
74 self.mem_map['gpio'] = 0xc0007000
75 #self.csr_map["xicsicp"] = 8 # 8 x 0x800 == 0x4000
76 #self.csr_map["xicsics"] = 10 # 10 x 0x800 == 0x5000
77
78 ram_init = []
79 if ram_fname:
80 #ram_init = get_mem_data({
81 # ram_fname: "0x00000000",
82 # }, "little")
83 ram_init = get_mem_data(ram_fname, "little")
84
85 # remap the main RAM to reset-start-address
86 self.mem_map["main_ram"] = 0x00000000
87
88 # without sram nothing works, therefore move it to higher up
89 self.mem_map["sram"] = 0x90000000
90
91 # put UART at 0xc000200 (w00t! this works!)
92 self.csr_map["uart"] = 4
93
94
95 # SoCCore -------------------------------------------------------------
96 SoCSDRAM.__init__(self, platform, clk_freq=sys_clk_freq,
97 cpu_type = "microwatt",
98 cpu_cls = LibreSoC if cpu == "libresoc" \
99 else Microwatt,
100 #bus_data_width = 64,
101 csr_address_width = 12, # limit to 0x4000
102 cpu_variant = variant,
103 csr_data_width = 8,
104 l2_size = 0,
105 uart_name = "sim",
106 with_sdram = with_sdram,
107 sdram_module = sdram_module,
108 sdram_data_width = sdram_data_width,
109 integrated_rom_size = 0 if ram_fname else 0x10000,
110 integrated_sram_size = 0x40000,
111 #integrated_main_ram_init = ram_init,
112 integrated_main_ram_size = 0x00000000 if with_sdram \
113 else 0x10000000 , # 256MB
114 )
115 self.platform.name = "sim"
116
117 if cpu == "libresoc":
118 # XICS interrupt devices
119 icp_addr = self.mem_map['xicsicp']
120 icp_wb = self.cpu.xics_icp
121 icp_region = SoCRegion(origin=icp_addr, size=0x20, cached=False)
122 self.bus.add_slave(name='xicsicp', slave=icp_wb, region=icp_region)
123
124 ics_addr = self.mem_map['xicsics']
125 ics_wb = self.cpu.xics_ics
126 ics_region = SoCRegion(origin=ics_addr, size=0x1000, cached=False)
127 self.bus.add_slave(name='xicsics', slave=ics_wb, region=ics_region)
128
129 if "gpio" in variant:
130 # Simple GPIO peripheral
131 gpio_addr = self.mem_map['gpio']
132 gpio_wb = self.cpu.simple_gpio
133 gpio_region = SoCRegion(origin=gpio_addr, size=0x20, cached=False)
134 self.bus.add_slave(name='gpio', slave=gpio_wb, region=gpio_region)
135
136
137 # CRG -----------------------------------------------------------------
138 self.submodules.crg = CRG(platform.request("sys_clk"))
139
140 #ram_init = []
141
142 # SDRAM ----------------------------------------------------
143 if with_sdram:
144 sdram_clk_freq = int(100e6) # FIXME: use 100MHz timings
145 sdram_module_cls = getattr(litedram_modules, sdram_module)
146 sdram_rate = "1:{}".format(
147 sdram_module_nphases[sdram_module_cls.memtype])
148 sdram_module = sdram_module_cls(sdram_clk_freq, sdram_rate)
149 phy_settings = get_sdram_phy_settings(
150 memtype = sdram_module.memtype,
151 data_width = sdram_data_width,
152 clk_freq = sdram_clk_freq)
153 self.submodules.sdrphy = SDRAMPHYModel(sdram_module,
154 phy_settings,
155 init=ram_init
156 )
157 self.register_sdram(
158 self.sdrphy,
159 sdram_module.geom_settings,
160 sdram_module.timing_settings)
161 # FIXME: skip memtest to avoid corrupting memory
162 self.add_constant("MEMTEST_BUS_SIZE", 128//16)
163 self.add_constant("MEMTEST_DATA_SIZE", 128//16)
164 self.add_constant("MEMTEST_ADDR_SIZE", 128//16)
165 self.add_constant("MEMTEST_BUS_DEBUG", 1)
166 self.add_constant("MEMTEST_ADDR_DEBUG", 1)
167 self.add_constant("MEMTEST_DATA_DEBUG", 1)
168
169
170 if "jtag" in variant:
171 # add JTAG platform pins
172 platform.add_extension([
173 ("jtag", 0,
174 Subsignal("tck", Pins(1)),
175 Subsignal("tms", Pins(1)),
176 Subsignal("tdi", Pins(1)),
177 Subsignal("tdo", Pins(1)),
178 )
179 ])
180
181 jtagpads = platform.request("jtag")
182 self.comb += self.cpu.jtag_tck.eq(jtagpads.tck)
183 self.comb += self.cpu.jtag_tms.eq(jtagpads.tms)
184 self.comb += self.cpu.jtag_tdi.eq(jtagpads.tdi)
185 self.comb += jtagpads.tdo.eq(self.cpu.jtag_tdo)
186
187
188 # Debug ---------------------------------------------------------------
189 # (enable with ./sim.py --debug --variant=standard)
190 if not debug:
191 return
192
193 # In debug mode, the DMI interface is used to perform single-step
194 # and dump of the full register set (MSR, r0-r31, CR, XER, PC).
195 # by running the exact same program with microwatt and libre-soc
196 # a straight "diff -u" of the complete progress dumps can be done
197 # and therefore computation instruction discrepancies found immediately
198 # and easily, running at "verilator" speed.
199 #
200 # the FSM is a bit of a dog's dinner, it relies on the way that DMI
201 # works, sending requests at periodic intervals. needs work. DoesTheJob.
202
203 # setup running of DMI FSM
204 dmi_addr = Signal(4)
205 dmi_din = Signal(64)
206 dmi_dout = Signal(64)
207 dmi_wen = Signal(1)
208 dmi_req = Signal(1)
209
210 # debug log out
211 dbg_addr = Signal(4)
212 regnum = Signal(6)
213 dbg_dout = Signal(64)
214 dbg_msg = Signal(1)
215
216 # capture pc from dmi
217 pc = Signal(64)
218 active_dbg = Signal()
219 active_dbg_cr = Signal()
220 active_dbg_xer = Signal()
221
222 # xer flags
223 xer_so = Signal()
224 xer_ca = Signal()
225 xer_ca32 = Signal()
226 xer_ov = Signal()
227 xer_ov32 = Signal()
228
229 # increment counter, Stop after 100000 cycles
230 uptime = Signal(64)
231 self.sync += uptime.eq(uptime + 1)
232 #self.sync += If(uptime == 1000000000000, Finish())
233
234 # DMI FSM counter and FSM itself
235 dmicount = Signal(10)
236 dmirunning = Signal(1)
237 dmi_monitor = Signal(1)
238 dmifsm = FSM()
239 self.submodules += dmifsm
240
241 # DMI FSM
242 dmifsm.act("START",
243 If(dmi_req & dmi_wen,
244 (self.cpu.dmi_addr.eq(dmi_addr), # DMI Addr
245 self.cpu.dmi_din.eq(dmi_din), # DMI in
246 self.cpu.dmi_req.eq(1), # DMI request
247 self.cpu.dmi_wr.eq(1), # DMI write
248 If(self.cpu.dmi_ack,
249 (NextState("IDLE"),
250 )
251 ),
252 ),
253 ),
254 If(dmi_req & ~dmi_wen,
255 (self.cpu.dmi_addr.eq(dmi_addr), # DMI Addr
256 self.cpu.dmi_req.eq(1), # DMI request
257 self.cpu.dmi_wr.eq(0), # DMI read
258 If(self.cpu.dmi_ack,
259 # acknowledge received: capture data.
260 (NextState("IDLE"),
261 NextValue(dbg_addr, dmi_addr),
262 NextValue(dbg_dout, self.cpu.dmi_dout),
263 NextValue(dbg_msg, 1),
264 ),
265 ),
266 ),
267 )
268 )
269
270 # DMI response received: reset the dmi request and check if
271 # in "monitor" mode
272 dmifsm.act("IDLE",
273 If(dmi_monitor,
274 NextState("FIRE_MONITOR"), # fire "monitor" on next cycle
275 ).Else(
276 NextState("START"), # back to start on next cycle
277 ),
278 NextValue(dmi_req, 0),
279 NextValue(dmi_addr, 0),
280 NextValue(dmi_din, 0),
281 NextValue(dmi_wen, 0),
282 )
283
284 # "monitor" mode fires off a STAT request
285 dmifsm.act("FIRE_MONITOR",
286 (NextValue(dmi_req, 1),
287 NextValue(dmi_addr, 1), # DMI STAT address
288 NextValue(dmi_din, 0),
289 NextValue(dmi_wen, 0), # read STAT
290 NextState("START"), # back to start on next cycle
291 )
292 )
293
294 self.comb += xer_so.eq((dbg_dout & 1) == 1)
295 self.comb += xer_ca.eq((dbg_dout & 4) == 4)
296 self.comb += xer_ca32.eq((dbg_dout & 8) == 8)
297 self.comb += xer_ov.eq((dbg_dout & 16) == 16)
298 self.comb += xer_ov32.eq((dbg_dout & 32) == 32)
299
300 # debug messages out
301 self.sync += If(dbg_msg,
302 (If(active_dbg & (dbg_addr == 0b10), # PC
303 Display("pc : %016x", dbg_dout),
304 ),
305 If(dbg_addr == 0b10, # PC
306 pc.eq(dbg_dout), # capture PC
307 ),
308 If(dbg_addr == 0b11, # MSR
309 Display(" msr: %016x", dbg_dout),
310 ),
311 If(dbg_addr == 0b1000, # CR
312 Display(" cr : %016x", dbg_dout),
313 ),
314 If(dbg_addr == 0b1001, # XER
315 Display(" xer: so %d ca %d 32 %d ov %d 32 %d",
316 xer_so, xer_ca, xer_ca32, xer_ov, xer_ov32),
317 ),
318 If(dbg_addr == 0b101, # GPRs (and "fast" SPRs)
319 If(regnum <= 31, Display(" gpr%02x: %016x",
320 regnum, dbg_dout),), # GPRs
321 If(regnum == 32, Display(" LR: %016x", dbg_dout),), # LR
322 If(regnum == 33, Display(" CTR: %016x", dbg_dout),), # CTR
323 If(regnum == 34, Display(" SRR0: %016x", dbg_dout),), # SRR0
324 If(regnum == 35, Display(" SRR1: %016x", dbg_dout),), # SRR1
325 If(regnum == 36, Display(" HSRR0: %016x", dbg_dout),), # HSRR0
326 If(regnum == 37, Display(" HSRR1: %016x", dbg_dout),), # HSRR1
327 If(regnum == 38, Display(" SPRG0: %016x", dbg_dout),), # SPRG0
328 If(regnum == 39, Display(" SPRG1: %016x", dbg_dout),), # SPRG1
329 If(regnum == 40, Display(" SPRG2: %016x", dbg_dout),), # SPRG2
330 If(regnum == 41, Display(" SPRG3: %016x", dbg_dout),), # SPRG3
331 If(regnum == 42, Display(" HSPRG0: %016x", dbg_dout),), # HSPRG0
332 If(regnum == 43, Display(" HSPRG1: %016x", dbg_dout),), # HSPRG1
333 If(regnum == 44, Display(" XER: %016x", dbg_dout),), # XER
334 If(regnum == 45, Display(" TAR: %016x", dbg_dout),), # TAR
335 ),
336 # also check if this is a "stat"
337 If(dbg_addr == 1, # requested a STAT
338 #Display(" stat: %x", dbg_dout),
339 If(dbg_dout & 2, # bit 2 of STAT is "stopped" mode
340 dmirunning.eq(1), # continue running
341 dmi_monitor.eq(0), # and stop monitor mode
342 ),
343 ),
344 dbg_msg.eq(0)
345 )
346 )
347
348 # kick off a "stop"
349 self.sync += If(uptime == 0,
350 (dmi_addr.eq(0), # CTRL
351 dmi_din.eq(1<<0), # STOP
352 dmi_req.eq(1),
353 dmi_wen.eq(1),
354 )
355 )
356
357 self.sync += If(uptime == 4,
358 dmirunning.eq(1),
359 )
360
361 self.sync += If(dmirunning,
362 dmicount.eq(dmicount + 1),
363 )
364
365 # loop every 1<<N cycles
366 cyclewid = 9
367
368 # get the PC
369 self.sync += If(dmicount == 4,
370 (dmi_addr.eq(0b10), # NIA
371 dmi_req.eq(1),
372 dmi_wen.eq(0),
373 )
374 )
375
376 # kick off a "step"
377 self.sync += If(dmicount == 8,
378 (dmi_addr.eq(0), # CTRL
379 dmi_din.eq(1<<3), # STEP
380 dmi_req.eq(1),
381 dmi_wen.eq(1),
382 dmirunning.eq(0), # stop counter, need to fire "monitor"
383 dmi_monitor.eq(1), # start "monitor" instead
384 )
385 )
386
387 # limit range of pc for debug reporting
388 #self.comb += active_dbg.eq((0x378c <= pc) & (pc <= 0x38d8))
389 #self.comb += active_dbg.eq((0x0 < pc) & (pc < 0x58))
390 self.comb += active_dbg.eq(1)
391
392 # get the MSR
393 self.sync += If(active_dbg & (dmicount == 12),
394 (dmi_addr.eq(0b11), # MSR
395 dmi_req.eq(1),
396 dmi_wen.eq(0),
397 )
398 )
399
400 if cpu == "libresoc": # XXX TODO: waiting on microwatt upstream patch
401 #self.comb += active_dbg_cr.eq((0x10300 <= pc) & (pc <= 0x12600))
402 self.comb += active_dbg_cr.eq(0)
403
404 # get the CR
405 self.sync += If(active_dbg_cr & (dmicount == 16),
406 (dmi_addr.eq(0b1000), # CR
407 dmi_req.eq(1),
408 dmi_wen.eq(0),
409 )
410 )
411
412 #self.comb += active_dbg_xer.eq((0x10300 <= pc) & (pc <= 0x1094c))
413 self.comb += active_dbg_xer.eq(active_dbg_cr)
414
415 # get the XER
416 self.sync += If(active_dbg_xer & (dmicount == 20),
417 (dmi_addr.eq(0b1001), # XER
418 dmi_req.eq(1),
419 dmi_wen.eq(0),
420 )
421 )
422
423 # read all 32 GPRs plus the next 16 which in microwatt are
424 # the "fast" SPRs, LR, CTR, SRR0, SRR1, etc.
425 for i in range(48):
426 self.sync += If(active_dbg & (dmicount == 24+(i*8)),
427 (dmi_addr.eq(0b100), # GSPR addr
428 dmi_din.eq(i), # register number (0-31 GPR, 32-48 fast SPRs)
429 regnum.eq(i),
430 dmi_req.eq(1),
431 dmi_wen.eq(1),
432 )
433 )
434
435 self.sync += If(active_dbg & (dmicount == 28+(i*8)),
436 (dmi_addr.eq(0b101), # GSPR data
437 dmi_req.eq(1),
438 dmi_wen.eq(0),
439 )
440 )
441
442 # monitor bbus read/write
443 self.sync += If(active_dbg & self.cpu.dbus.stb & self.cpu.dbus.ack,
444 Display(" [%06x] dadr: %8x, we %d s %01x w %016x r: %016x",
445 #uptime,
446 0,
447 self.cpu.dbus.adr,
448 self.cpu.dbus.we,
449 self.cpu.dbus.sel,
450 self.cpu.dbus.dat_w,
451 self.cpu.dbus.dat_r
452 )
453 )
454
455 return
456
457 # monitor ibus write
458 self.sync += If(active_dbg & self.cpu.ibus.stb & self.cpu.ibus.ack &
459 self.cpu.ibus.we,
460 Display(" [%06x] iadr: %8x, s %01x w %016x",
461 #uptime,
462 0,
463 self.cpu.ibus.adr,
464 self.cpu.ibus.sel,
465 self.cpu.ibus.dat_w,
466 )
467 )
468 # monitor ibus read
469 self.sync += If(active_dbg & self.cpu.ibus.stb & self.cpu.ibus.ack &
470 ~self.cpu.ibus.we,
471 Display(" [%06x] iadr: %8x, s %01x r %016x",
472 #uptime,
473 0,
474 self.cpu.ibus.adr,
475 self.cpu.ibus.sel,
476 self.cpu.ibus.dat_r
477 )
478 )
479
480 # Build -----------------------------------------------------------------------
481
482 def main():
483 parser = argparse.ArgumentParser(description="LiteX LibreSoC CPU Sim")
484 parser.add_argument("--cpu", default="libresoc",
485 help="CPU to use: libresoc (default) or microwatt")
486 parser.add_argument("--variant", default="standardjtag",
487 help="Specify variant with different features")
488 parser.add_argument("--debug", action="store_true",
489 help="Enable debug traces")
490 parser.add_argument("--trace", action="store_true",
491 help="Enable tracing")
492 parser.add_argument("--trace-start", default=0,
493 help="Cycle to start FST tracing")
494 parser.add_argument("--trace-end", default=-1,
495 help="Cycle to end FST tracing")
496 args = parser.parse_args()
497
498 sim_config = SimConfig(default_clk="sys_clk")
499 sim_config.add_module("serial2console", "serial")
500 sim_config.add_module("jtagremote", "jtag", args={'port': 44853})
501
502 for i in range(2):
503 soc = LibreSoCSim(cpu=args.cpu, debug=args.debug, variant=args.variant)
504 builder = Builder(soc,compile_gateware = i!=0)
505 builder.build(sim_config=sim_config,
506 run = i!=0,
507 trace = args.trace,
508 trace_start = int(args.trace_start),
509 trace_end = int(args.trace_end),
510 trace_fst = 0)
511 os.chdir("../")
512
513 if __name__ == "__main__":
514 main()