Discussion:
[fpc-pascal] Pascal Ardiono (avr) library
Joost van der Sluis via fpc-pascal
2021-04-03 17:49:08 UTC
Permalink
Hi all,

During some spare free time I've ported parts of the Arduino AVR library
to Free Pascal. So now it is possible to use things like 'DigitalWrite'
and 'Delay'.

More info here:
https://lazarussupport.com/introducing-pasduino-the-pascal-avr-arduino-library/

The library itself is at: https://gitlab.freepascal.org/Joost/pasduino

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinf
Florian Klämpfl via fpc-pascal
2021-04-03 18:42:50 UTC
Permalink
Post by Joost van der Sluis via fpc-pascal
Hi all,
During some spare free time I've ported parts of the Arduino AVR library to Free Pascal. So now it is possible to use things like 'DigitalWrite' and 'Delay'.
More info here: https://lazarussupport.com/introducing-pasduino-the-pascal-avr-arduino-library/
You write that the assembler is far from ideal. Did you notice any problems in particular? Because in general it’s not that bad as long as one keeps in mind that one is working on a 8 bit systems where e.g. 32 bit integers hurt.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepasc
Joost van der Sluis via fpc-pascal
2021-04-04 10:50:30 UTC
Permalink
Post by Florian Klämpfl via fpc-pascal
Post by Joost van der Sluis via fpc-pascal
Hi all,
During some spare free time I've ported parts of the Arduino AVR library to Free Pascal. So now it is possible to use things like 'DigitalWrite' and 'Delay'.
More info here: https://lazarussupport.com/introducing-pasduino-the-pascal-avr-arduino-library/
You write that the assembler is far from ideal. Did you notice any problems in particular? Because in general it’s not that bad as long as one keeps in mind that one is working on a 8 bit systems where e.g. 32 bit integers hurt.
I came across some issues while doing this, but I can not remember all
of them.

One thing is constant-propagation in combination with auto-inlining. It
would be really nice when calling PinMode with two constant parameters,
would only lead to setting the constant value at two memory locations.
(Between avr_save and avr_restore) Yes, then I have to change the
parameters to const, and add {$WRITEABLECONST OFF} to the Arduino unit.

I doubt many (if any) compiler will/can do this. But that would be
ideal. (I think we need the concept Gareth calls 'pure functions' for this)

Another thing is the less-then-ideal use of registers, at least in my
eyes, but it could be that I miss something.

Take this function:

procedure THardwareSerial.SerialEnd;
begin
// wait for transmission of outgoing data
//Flush();

Fucsrb^ := Fucsrb^ and not (1 shl RXEN0);
Fucsrb^ := Fucsrb^ and not (1 shl TXEN0);
Fucsrb^ := Fucsrb^ and not (1 shl RXCIE0);
Fucsrb^ := Fucsrb^ and not (1 shl UDRIE0);

// clear any received data
//FRXBufferHead := FRXBufferTail;
end;

The 4 statements could be converted into 1. But I agree with the
compiler that this is not done, as reading and writing to a memory
location this way should be considered 'volatile'.

Leads to this, my comments prefixed with JvdS:

.section .text.n_hardwareserials_sthardwareserial_s__ss_serialend,"ax"
.globl HARDWARESERIALs_sTHARDWARESERIAL_s__ss_SERIALEND
HARDWARESERIALs_sTHARDWARESERIAL_s__ss_SERIALEND:
# Register r24,r25,r18 allocated
# [224] begin
mov r18,r24
# Register r24 released
# Var $self located in register r18:r25

JvdS: self in r18:r25? Strange combination?

# Register r30 allocated
mov r30,r18
# Register r31 allocated
mov r31,r25

# Now self is in Z.

# Register r19 allocated
ldd r19,Z+18
# Register r20 allocated
ldd r20,Z+19
# Register r31 released
# [228] Fucsrb^ := Fucsrb^ and not (1 shl RXEN0);

JvdS: Here we override Z. After this is points to Fucsrb^

mov r30,r19
# Register r31 allocated
mov r31,r20
# Register r21 allocated
ld r21,Z
andi r21,-17
# Register r19,r20 released
st Z,r21
# Register r21,r31 released

JvdS: Now we do exactly the same again? This could all be skipped!

mov r30,r18
# Register r31 allocated
mov r31,r25
# Register r19 allocated
ldd r19,Z+18
# Register r20 allocated
ldd r20,Z+19
# Register r31 released
# [229] Fucsrb^ := Fucsrb^ and not (1 shl TXEN0);
mov r30,r19
# Register r31 allocated
mov r31,r20
# Register r21 allocated
ld r21,Z
andi r21,-9
# Register r19,r20 released
st Z,r21

JvdS: And again.....

# Register r21,r31 released
mov r30,r18
# Register r31 allocated
mov r31,r25
# Register r19 allocated
ldd r19,Z+18
# Register r20 allocated
ldd r20,Z+19
# Register r31 released
# [230] Fucsrb^ := Fucsrb^ and not (1 shl RXCIE0);
mov r30,r19
# Register r31 allocated
mov r31,r20

<<cut>>

Isn't it at least a good practice to store self at Y. So we have Z free
for other calculations and can access members directly using ldd (),y+().

But maybe that's difficult?

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/
Jonas Maebe via fpc-pascal
2021-04-04 11:00:47 UTC
Permalink
Post by Joost van der Sluis via fpc-pascal
One thing is constant-propagation in combination with auto-inlining. It
would be really nice when calling PinMode with two constant parameters,
would only lead to setting the constant value at two memory locations.
(Between avr_save and avr_restore) Yes, then I have to change the
parameters to const, and add {$WRITEABLECONST OFF} to the Arduino unit.
I doubt many (if any) compiler will/can do this. But that would be
ideal. (I think we need the concept Gareth calls 'pure functions' for this)
No, but you would need to add support to the compiler for keeping a
symbolic representation of typed constants.


Jonas
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/
Joost van der Sluis via fpc-pascal
2021-04-04 11:03:40 UTC
Permalink
Post by Joost van der Sluis via fpc-pascal
I came across some issues while doing this, but I can not remember all
of them.
Another one: (Also constant propagation)

-O4, {$WRITEABLECONST OFF}

# [19] a := DigitalPinToBitMask[5];
lds r18,(TC_sARDUINO_ss_DIGITALPINTOBITMASK+5)
# Var a located in register r18
# Register r18 allocated
# Register r18 released
# [20] a := 32;
ldi r18,32

First, the compiler could remove line 19 completely.

Secondly, (TC_sARDUINO_ss_DIGITALPINTOBITMASK+5) is a constant with
value 32. So why the lds, and not ldi? (ie: line 19 and 20 are exactly
the same!)

btw: the value of a was not used in this example, so a warning was
raised. But the compiler could also throw this piece of code away
altogether....

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.
Florian Klämpfl via fpc-pascal
2021-04-04 11:33:46 UTC
Permalink
Isn't it at least a good practice to store self at Y. So we have Z free for other calculations and can access members directly using ldd (),y+().
But maybe that's difficult?
Using Y might be indeed difficult as the compiler knows only after register allocation that it does not need Y for other purposes. It would basically require the ability to redo code generation.

But tracking the use of Z is indeed something I would like to implement already for a longer time.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listin
Joost van der Sluis via fpc-pascal
2021-04-04 13:36:05 UTC
Permalink
Post by Florian Klämpfl via fpc-pascal
Isn't it at least a good practice to store self at Y. So we have Z free for other calculations and can access members directly using ldd (),y+().
But maybe that's difficult?
Using Y might be indeed difficult as the compiler knows only after register allocation that it does not need Y for other purposes. It would basically require the ability to redo code generation.
In my head I've been thinking a lot about another register-allocator:

During the code-generation the code-generation only asks the
register-allocator 'I need a register now that cas capabilities X,Y and
Z). Or: give me the same register as I used last time.

And then, afterwards, once code has been generated for the whole
'block', the register-allocator fills in the registers. And
store/restores them when needed. This can be done using an algorithm
that uses a tree to 'peel-down' (is this English?) all the solutions.
Just like is done with a regular-expression parser.

Just dreaming. I don't have the time to work on it, and I don't even
know how it works at the moment. But that would seem to be the ideal
solution to me.

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-
Florian Klämpfl via fpc-pascal
2021-04-04 13:43:31 UTC
Permalink
Post by Florian Klämpfl via fpc-pascal
Isn't it at least a good practice to store self at Y. So we have Z free for other calculations and can access members directly using ldd (),y+().
But maybe that's difficult?
Using Y might be indeed difficult as the compiler knows only after register allocation that it does not need Y for other purposes. It would basically require the ability to redo code generation.
During the code-generation the code-generation only asks the register-allocator 'I need a register now that cas capabilities X,Y and Z). Or: give me the same register as I used last time.
And then, afterwards, once code has been generated for the whole 'block', the register-allocator fills in the registers. And store/restores them when needed. This can be done using an algorithm that uses a tree to 'peel-down' (is this English?) all the solutions. Just like is done with a regular-expression parser.
Just dreaming. I don't have the time to work on it, and I don't even know how it works at the moment. But that would seem to be the ideal solution to me.
But this is how it is basically currently done?

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freep
Joost van der Sluis via fpc-pascal
2021-04-04 15:30:53 UTC
Permalink
Post by Florian Klämpfl via fpc-pascal
Post by Florian Klämpfl via fpc-pascal
Isn't it at least a good practice to store self at Y. So we have Z free for other calculations and can access members directly using ldd (),y+().
But maybe that's difficult?
Using Y might be indeed difficult as the compiler knows only after register allocation that it does not need Y for other purposes. It would basically require the ability to redo code generation.
During the code-generation the code-generation only asks the register-allocator 'I need a register now that cas capabilities X,Y and Z). Or: give me the same register as I used last time.
And then, afterwards, once code has been generated for the whole 'block', the register-allocator fills in the registers. And store/restores them when needed. This can be done using an algorithm that uses a tree to 'peel-down' (is this English?) all the solutions. Just like is done with a regular-expression parser.
Just dreaming. I don't have the time to work on it, and I don't even know how it works at the moment. But that would seem to be the ideal solution to me.
But this is how it is basically currently done?
But why do you need to redo the code generation? At the moment the real
registers are assigned, you do know if you need the y register for some
specific task, no?

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/
Florian Klämpfl via fpc-pascal
2021-04-04 15:51:17 UTC
Permalink
Post by Florian Klämpfl via fpc-pascal
And then, afterwards, once code has been generated for the whole 'block', the register-allocator fills in the registers. And store/restores them when needed. This can be done using an algorithm that uses a tree to 'peel-down' (is this English?) all the solutions. Just like is done with a regular-expression parser.
Just dreaming. I don't have the time to work on it, and I don't even know how it works at the moment. But that would seem to be the ideal solution to me.
But this is how it is basically currently done?
But why do you need to redo the code generation? At the moment the real registers are assigned, you do know if you need the y register for some specific task, no?
Well, thinking about it, maybe yes with some hacky approach. The issue is: only after register allocation it is known if temps. for spilling are needed. If any local data is used (local vars, temps, temps for spilling etc) then a frame pointer is needed, and if spilling temps are needed is only known when register allocation is complete.
Dimitrios Chr. Ioannidis via fpc-pascal
2021-04-03 19:14:22 UTC
Permalink
Hi,
Post by Joost van der Sluis via fpc-pascal
Hi all,
During some spare free time I've ported parts of the Arduino AVR
library to Free Pascal. So now it is possible to use things like
'DigitalWrite' and 'Delay'.
https://lazarussupport.com/introducing-pasduino-the-pascal-avr-arduino-library/
The library itself is at: https://gitlab.freepascal.org/Joost/pasduino
< snip >

Nice !

  BTW, IMHO, porting Arduino wiring code is not the way to go. Their
libraries are written with an ease of use philosophy and they try to
optimize them with the use of a lot of linker and preprocessor trickery,
which are not easy to port.

  IMHO, if you want to use library or framework I'll recommend to take
a look to Michael Ring's Microcontroller Board Framework MBF (
https://github.com/michael-ring/mbf ). I think that has support for AVR.

  One question off topic, can anyone register and use the gitlab scm
server ( gitlab.freepascal.org ) ? Is it open for Free Pascal / Lazarus
projects ?

regards,
--
Dimitrios chr. Ioannidis


_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org
Joost van der Sluis via fpc-pascal
2021-04-03 22:39:26 UTC
Permalink
Post by Dimitrios Chr. Ioannidis via fpc-pascal
Post by Joost van der Sluis via fpc-pascal
During some spare free time I've ported parts of the Arduino AVR
library to Free Pascal. So now it is possible to use things like
'DigitalWrite' and 'Delay'.
  BTW, IMHO, porting Arduino wiring code is not the way to go. Their
libraries are written with an ease of use philosophy and they try to
optimize them with the use of a lot of linker and preprocessor trickery,
which are not easy to port.
I would say that it was not that difficult to port. Although I could not
port all the 'trickery' literally.

And it was my intention to follow the 'ease of use philosophy'. That is
exactly what was missing at the Freepascal side of Arduino development.

When you are more experienced, you might not need this, and you are
better of writing to the corresponding addresses, instead of using
'DigitalWrite'. This is for beginners and simple proof-of-concepts. (The
developers of the original Arduino software also state this)
Post by Dimitrios Chr. Ioannidis via fpc-pascal
  IMHO, if you want to use library or framework I'll recommend to take
a look to Michael Ring's Microcontroller Board Framework MBF (
https://github.com/michael-ring/mbf ). I think that has support for AVR.
Could be a good tip, I didn't know about it's existence.
Post by Dimitrios Chr. Ioannidis via fpc-pascal
  One question off topic, can anyone register and use the gitlab scm
server ( gitlab.freepascal.org ) ? Is it open for Free Pascal / Lazarus
projects ?
Michael can tell you more. But as I understood it it is more a
proof-of-concept at the moment. A playground to get more familiar
hosting git, in preparation of the switch from Subversion to Git for the
Free Pascal sources.

But at this moment nothing has been decided. Especially if we are gonna
host fpc on Gitlab itself or host it on our on.

Regards,

Joost.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cg
Loading...