Brainstorming on recursive class definitions

moogyd at yahoo.co.uk moogyd at yahoo.co.uk
Tue Sep 12 12:30:56 EDT 2017


On Tuesday, September 12, 2017 at 5:37:31 PM UTC+2, Johannes Bauer wrote:
> Hi group,
> 
> so I'm having a problem that I'd like to solve *nicely*. I know plenty
> of ways to solve it, but am curious if there's a solution that allows me
> to write the solution in a way that is most comfortable for the user.
> 
> I'm trying to map registers of a processor. So assume you have a n bit
> address space, the processor might have duplicate units of identical
> functionality mapped at different places in memory. For example, assume
> there's a GPIO unit that has registers FOO, BAR and KOO and two GPIO
> ports GPIOA and GPIOB. I'd like to write code along the lines of this:
> 
> class GpioMap(BaseRegisterMap):
> 	FOO = 0x0
> 	BAR = 0x4
> 	KOO = 0x8
> 
> class CPURegisterMap(BaseRegisterMap):
> 	GPIOA = GpioMap(0x10000)
> 	GPIOB = GpioMap(0x20000)
> 
> cpu = CPURegisterMap(0x80000000)
> 
> assert(cpu.addr == 0x80000000)
> assert(cpu.GPIOA.addr == 0x80000000 + 0x10000)
> assert(cpu.GPIOB.addr == 0x80000000 + 0x20000)
> assert(cpu.GPIOA.FOO.addr == 0x80000000 + 0x10000 + 0x0)
> assert(cpu.GPIOA.KOO.addr == 0x80000000 + 0x10000 + 0x8)
> assert(cpu.GPIOB.BAR.addr == 0x80000000 + 0x20000 + 0x4)
> 
> So, obviously, FOO, BAR and KOO are of type "int" without any "addr"
> property, so there would need to be some magic there. Additionally,
> through some way the instanciation of GpioMap() would need the knowledge
> of its parent base, which I'm not sure is even possible. Maybe (that's
> what I'm currently trying to get right) the __getattribute__ would
> propagate the information about the accumulated parent's base address to
> the child during lookup.
> 
> Anyways, I'm looking for your ideas on how to solve such a thing
> "nicely". Note that "BaseRegisterMap" is allowed to do dirty things as
> long as the definition code has a clean look & feel.
> 
> Cheers,
> Joe
> 
> 
> -- 
> >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> > Zumindest nicht öffentlich!
> Ah, der neueste und bis heute genialste Streich unsere großen
> Kosmologen: Die Geheim-Vorhersage.
>  - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>

A child (e.g. instance of GpioMap) should not have any knowledge of it's own base address. This is a function of the GPIORegisyerMap only.
i.e. The GPIORegisyerMap  would do the selection of particular instance.

It may be worth looking at some of the IP-XACT documentation, which defines some common terms (IIRC register, address map, address space etc). Having a consistent definition here will definitely help moving forward
Although it may seem OTT at the moment, it is something you should consider at the outset.
e.g. A single GPIO register may occur at two different addresses, depending on the bus master (CPU Core, Debugger, External SPI access).

Steven




More information about the Python-list mailing list