News:

Let's find out together what makes a PIC Tick!

Main Menu

Possible addition to the compiler

Started by JonW, Nov 06, 2024, 07:33 PM

Previous topic - Next topic

JonW

Les, Have you ever thought of adding struct-like functions to the compiler such that you can use the .dot notation for long registers?  Many of the newer SOC have large 24 or even 32-bit registers that are split into several chunks, structs and unions  in C seem really powerful to keep track of them.   Like this:-

TYPE SensorData as word
  Data [0:7]
  Set1 [8:12]     
  Set2 [13:15]   
END TYPE


Dim VAR as byte = 0
Dim VAR1 as byte = 0


SensorData.set2 = 3
VAR = SensorData.data   
VAR1 = SensorData.Set2          ' Stored in a byte and in LSB , thus VAR1 = 00000011
HRSOUT Sensordata                 ' Sends the whole word
HRSOUT SensorData.Set2         ' only Set 2 sent out

*Fixed typo







Dompie

Yes, that seems like a very useful feature that I would like to use.
@Jon I think the line "VAR1 = SensorData.Set3" is a typo and should be .Set2.

Greetings
Johan

PS Wasn't there a user many years ago who tried to implement something like that via some kind of preprocessor program??

SeanG_65

This would be EXTREMELY useful.

I have sent a long data stream, then used LEFT$, RIGHT$ and MID$ to extract fields, but this makes the job much simpler.

Gamboa

It's an improvement, but it's also elegant how you can do it with what we have now.

Dim SensorData as Long
Dim Data as SensorData.Byte0
Dim Set1 as SensorData.Byte1
Dim Set2 as SensorData.Byte2

Dim VAR as byte = 0
Dim VAR1 as byte = 0

Set2  = 3
VAR = Data   
VAR1 = Set2           ' Stored in a byte and in LSB , thus VAR1 = 00000011
HRSOUT Sensordata                 ' Sends the whole word
HRSOUT Set2           ' only Set 2 sent out

Regards,
Gamboa
 
Long live for you

RGV250

Hi Gamboa,
The only difference is that Jon's proposal uses a word variable which could make a difference if sending out lots of data.
My first thought when I saw it was if it could be done in a procedure?

Bob

JonW

I already use the .byte notation. Its the chunks of 2,3 and 5bit non aligned data that become more problematic.  I use some soc that have 100 plus registers now and of course this can be done by dim or bit shifting and also simplified in procs but creates alot of code to follow in basic.

trastikata

Hello JonW,

I think behind a structure is a lot of byte and bit manipulation that the Compiler does in the background. So instead, you can set your own Procedures.

Main:
    Dim dRegStructure As Dword                                                          'Dim my register structure
    dRegStructure = 0x22101028                                                          'Set some value %00100010000100000001000000101000

    TftPrintSndString("RegDummy1=" + Str$(Hex2 ReadRegDummy1()),1,0,0,1,GREEN,BLACK,0)  'Print RegDummy1, expected value 0x05
    WriteRegDummy1(%100)                                                                'Write b100 to RegDummy1
    TftPrintSndString("dRegStructure=" + Str$(Hex8 dRegStructure),1,0,1,1,GREEN,BLACK,0)'Confirm dRegStructure changed, expected value 0x22101020
End   

Proc ReadRegDummy1(), Byte
    Result = dRegStructure.Byte0 & 0x38
    Result = Result >> 3
EndProc 

Proc WriteRegDummy1(bValue)
    bValue = bValue << 3
    dRegStructure.Byte0 = dRegStructure.Byte0 & bValue
EndProc 

Notice in my code when I have to get value of the register I use ReadRegDummy1() which has return value of the virtual register formed by bits 3 to 5 in the 32b structure and I can use it in any other complex commands lie it was a variable ... which it is.

TftPrintSndString("RegDummy1=" + Str$(Hex2 ReadRegDummy1()),1,0,0,1,GREEN,BLACK,0)

When I have to write a value there I use WriteRegDummy1(bValue) and bValue can be any expression too.

CIMG2333.JPG

JonW

Like I previously stated it can be done and I currently use the proc and dims like you have posted. Structs have to manipulate behind the scenes like any compiler, its the readability and code speed that I like.  I would rather the compiler manipulate the shifts as it, in theory shouldn't make mistakes and be more efficient.

Just a idea from using C for a while.




Dompie

Yes, Structures are one of the few things in C that I really miss in the compiler.
But that is no reason at all to go back to C, that does not happen anymore with this old man. I will definitely stick with Positron with or without struct.

Johan