from litex.soc.interconnect import wishbone
 from litex.soc.cores.cpu import CPU
 
-CPU_VARIANTS = ["standard"]
+CPU_VARIANTS = ["standard", "standard32"]
 
 
 class LibreSoC(CPU):
     name                 = "libre_soc"
     human_name           = "Libre-SoC"
     variants             = CPU_VARIANTS
-    data_width           = 64
     endianness           = "little"
     gcc_triple           = ("powerpc64le-linux", "powerpc64le-linux-gnu")
     linker_output_format = "elf64-powerpcle"
         self.variant      = variant
         self.reset        = Signal()
 
+
+        if variant == "standard32":
+            self.data_width           = 32
+            self.dbus = dbus = wishbone.Interface(data_width=32, adr_width=30)
+        else:
+            self.dbus = dbus = wishbone.Interface(data_width=64, adr_width=29)
+            self.data_width           = 64
         self.ibus = ibus = wishbone.Interface(data_width=64, adr_width=29)
-        self.dbus = dbus = wishbone.Interface(data_width=64, adr_width=29)
 
         self.periph_buses = [ibus, dbus]
         self.memory_buses = []
 
         platform     = Platform()
         sys_clk_freq = int(100e6)
 
+        cpu_data_width = 32
+        #cpu_data_width = 64
+
+        if cpu_data_width == 32:
+            variant = "standard32"
+        else:
+            variant = "standard"
+
         # SoCCore -------------------------------------------------------------
         SoCSDRAM.__init__(self, platform, clk_freq=sys_clk_freq,
             cpu_type                 = "microwatt",
             cpu_cls                  = LibreSoC   if cpu == "libresoc" \
                                        else Microwatt,
             #bus_data_width           = 64,
+            cpu_variant              = variant,
             csr_data_width            = 32,
             l2_cache_size             = 0,
             uart_name                = "sim",
         )
 
         # limit range of pc for debug reporting
-        self.comb += active_dbg.eq((0x5108 <= pc) & (pc <= 0x5234))
+        #self.comb += active_dbg.eq((0x5108 <= pc) & (pc <= 0x5234))
         #self.comb += active_dbg.eq((0x0 < pc) & (pc < 0x58))
+        self.comb += active_dbg.eq(1)
 
         # get the MSR
         self.sync += If(active_dbg & (uptime[0:cyclewid] == 28),
 
     def __init__(self, pspec):
         self.pspec = pspec
         self.pspecslave = pspec
-        self.dbus = self.slavebus = Record(make_wb_layout(pspec))
-        print(self.dbus.sel.shape())
-        self.needs_cvt = False
         if (hasattr(pspec, "dmem_test_depth") and
                      isinstance(pspec.wb_data_wid, int) and
                     pspec.wb_data_wid != pspec.reg_wid):
+            self.dbus = Record(make_wb_layout(pspec), name="int_dbus")
             pspecslave = deepcopy(pspec)
             pspecslave.reg_wid = pspec.wb_data_wid
             mask_ratio = (pspec.reg_wid // pspec.wb_data_wid)
             pspecslave.mask_wid = pspec.mask_wid // mask_ratio
             self.pspecslave = pspecslave
-            self.slavebus = Record(make_wb_layout(pspecslave))
+            self.slavebus = Record(make_wb_layout(pspecslave), name="dbus")
             self.needs_cvt = True
+        else:
+            self.needs_cvt = False
+            self.dbus = self.slavebus = Record(make_wb_layout(pspec))
+
+        print(self.dbus.sel.shape())
         self.mask_wid = mask_wid = pspec.mask_wid
         self.addr_wid = addr_wid = pspec.addr_wid
         self.data_wid = data_wid = pspec.reg_wid
         yield self.m_load_err_o
         yield self.m_store_err_o
         yield self.m_badaddr_o
-        for sig in self.dbus.fields.values():
+        for sig in self.slavebus.fields.values():
             yield sig
 
     def ports(self):
 
                                     ] + \
                 list(self.dbg.dmi.ports()) + \
                 list(self.imem.ibus.fields.values()) + \
-                list(self.core.l0.cmpi.lsmem.lsi.dbus.fields.values())
+                list(self.core.l0.cmpi.lsmem.lsi.slavebus.fields.values())
 
     def ports(self):
         return list(self)
 
                          reg_wid=64,
                          # set to 32 for instruction-memory width=32
                          imem_reg_wid=64,
+                         # set to 32 to make data wishbone bus 32-bit
+                         wb_data_wid=32,
                          units=units)
     dut = TestIssuer(pspec)
 
 
                              addr_wid=48,
                              mask_wid=8,
                              imem_reg_wid=64,
+                             #wb_data_width=32,
                              reg_wid=64)
         m.submodules.issuer = issuer = TestIssuer(pspec)
         imem = issuer.imem._get_memory()