lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

What's logic behind this?

Tue Nov 18, 2014 6:55 pm

Code: Select all

 cfg = *(&pio->cfg[0] + index);
    cfg &= ~(0xf << offset);
    cfg |= val << offset;

    *(&pio->cfg[0] + index) = cfg
anyone see the logic of this?

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6229
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: What's logic behind this?

Tue Nov 18, 2014 7:18 pm

You're clearing a bit then setting it to val.

Again, context please. Where's it from? What's the hardware? Do you have a datasheet? Google tells me it's for olimex hardware or some other allwinner board, in which case I think you best ask them.

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Tue Nov 18, 2014 10:02 pm

lilzz wrote:

Code: Select all

 cfg = *(&pio->cfg[0] + index);
    cfg &= ~(0xf << offset);
    cfg |= val << offset;

    *(&pio->cfg[0] + index) = cfg
anyone see the logic of this?
The first line gets the content of a configuration register

Second line clears a nibble in the configuration register

Third line places a new value in the configuration register (where the nibble was cleared)

Last line writes it back.

Looks like:

index = specifies which index register to modify
&pio->cfg[0] = base address of a range of configuration registers
offset = bit offset for the nibble to be modified

Fairly normal way of modifying a memory mapped configuration register.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6229
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: What's logic behind this?

Tue Nov 18, 2014 10:05 pm

Ah, sorry for saying bit... missed the 0xf

Heater
Posts: 15950
Joined: Tue Jul 17, 2012 3:02 pm

Re: What's logic behind this?

Wed Nov 19, 2014 3:26 am

mikronauts,
Fairly normal way of modifying a memory mapped configuration register.
Normal?!

Why not just write:

Code: Select all

cfg = pio->cfg[index];
Which compiles to the same code as shown above but is a lot clearer.

The code as given is only normal in that it is how some C programmers obfuscate their code for job security :)
Memory in C++ is a leaky abstraction .

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Wed Nov 19, 2014 4:17 am

I've seen worse, including written as answers to questions given to job applicants (needless to say they were not hired)

At least it indicated a minimal understanding of pointers.

I did not re-write the code, as that is not what the OP asked for.
Heater wrote:mikronauts,
Fairly normal way of modifying a memory mapped configuration register.
Normal?!

Why not just write:

Code: Select all

cfg = pio->cfg[index];
Which compiles to the same code as shown above but is a lot clearer.

The code as given is only normal in that it is how some C programmers obfuscate their code for job security :)
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

Heater
Posts: 15950
Joined: Tue Jul 17, 2012 3:02 pm

Re: What's logic behind this?

Wed Nov 19, 2014 4:35 am

I've probably written worse myself :)

It's not clear to me what lilzz asked in the OP. As someone said, without context who knows what that code has any logic. I thought perhaps that weird addressing was a stumbling block for lilzz.
Memory in C++ is a leaky abstraction .

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Wed Nov 19, 2014 3:17 pm

I thought lilzz did not understand what the code was doing... but I could be wrong :)

Heater, I cannot see you writing anything as ugly as that... never mind worse! ZOG would get you!
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

Heater
Posts: 15950
Joined: Tue Jul 17, 2012 3:02 pm

Re: What's logic behind this?

Wed Nov 19, 2014 3:44 pm

Mickronauts,

Indeed, ZOG would not approve.

How long does it take Parallax to swap a server? I'm getting twitchy here.
Memory in C++ is a leaky abstraction .

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: What's logic behind this?

Wed Nov 19, 2014 5:33 pm

It looks like a test. Is it homework on pointers and bitwise logic?

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: What's logic behind this?

Wed Nov 19, 2014 5:48 pm

PiGraham wrote:It looks like a test. Is it homework on pointers and bitwise logic?
Looks like a psychiatrist's test for ill programmers to me :)

Like a Rorschach ink blot test for programmers :lol:

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Wed Nov 19, 2014 5:56 pm

It is taking them too long...

I wonder if any content will be lost? That would suck.
Heater wrote:Mickronauts,

Indeed, ZOG would not approve.

How long does it take Parallax to swap a server? I'm getting twitchy here.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Wed Nov 19, 2014 5:57 pm

Good point. I did not think of that.
PiGraham wrote:It looks like a test. Is it homework on pointers and bitwise logic?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What's logic behind this?

Wed Nov 19, 2014 5:57 pm

LOL!
Ravenous wrote:
PiGraham wrote:It looks like a test. Is it homework on pointers and bitwise logic?
Looks like a psychiatrist's test for ill programmers to me :)

Like a Rorschach ink blot test for programmers :lol:
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: What's logic behind this?

Wed Nov 19, 2014 6:00 pm

Ravenous wrote:Like a Rorschach ink blot test for programmers :lol:
Could be. It's not a good programming style example! :lol:

Heater
Posts: 15950
Joined: Tue Jul 17, 2012 3:02 pm

Re: What's logic behind this?

Wed Nov 19, 2014 8:04 pm

Sure enough google finds that code in some Allwinner gpio library here: https://www.olimex.com/forum/index.php?topic=436.0

In a slightly different way:

Code: Select all

//   cfg = readl(&pio->cfg[0] + index);
   cfg = *(&pio->cfg[0] + index);
   cfg &= ~(0xf << offset);
   cfg |= val << offset;

//   writel(cfg, &pio->cfg[0] + index);
   *(&pio->cfg[0] + index) = cfg;
Seems to have been extracted from U-BOOT according to the comments.

It's code like that that leads us to Heartbleed and such problems.
Memory in C++ is a leaky abstraction .

Return to “C/C++”