Address exception on array of byte - mP 7.0.1

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Author
Message
VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Address exception on array of byte - mP 7.0.1

#1 Post by VCC » 20 May 2018 21:37

Hi,
after few days of debugging a random crash/exception in a pretty big project, I finally reduced the code to a misaligned memory access. What makes it random is that adding or removing code, results in generating the exception or not. The code where the exception occurs assumes that it will work with aligned variables or constants. However, the linker does not keep track of variables and constants passed to this code, if they are aligned or not, so a misaligned var/const will trigger an exception.
The below code contains a 17-byte constant array (OddData), which makes allocation of another constant array (InputData) to be made at odd address. When working with the second array constant as a series of DWords (ProcessData procedure), it is assumed that this array is already aligned to a word boundary, so an exception occurs.

Code: Select all

program AddrException;

type
  PDWord = ^DWord;
  PWord = ^Word;
  PByte = ^Byte;


var
  DebugLED: sbit at LATB.0;
  DebugLED_Direction: sbit at TRISB.0;


procedure InfiniteLedBlink1;
begin
  repeat
    DebugLED := 1;
    Delay_ms(40);
    DebugLED := 0;
    Delay_ms(40);
  until False;
end;


procedure AddressTrap; org 0x06; //if the addressing mode is wrong, the code jumps here
begin
  INTCON1.3 := 0;
  InfiniteLedBlink1;
end;


procedure Init_MCU;
begin
  // Setting output frequency to 140MHz
  PLLFBD := 68;            // PLL multiplier M=70
  CLKDIV := 0x0000;        // PLL prescaler N1=2, PLL postscaler N2=2

  ANSELA := 0x00;          // Convert all I/O pins to digital
  ANSELB := 0x00;
  ANSELC := 0x00;
  ANSELD := 0x00;
  ANSELE := 0x00;
  ANSELG := 0x00;

  DebugLED_Direction := 0; //RB2
end;


procedure ProcessData(InData, OutData: PByte);
var
  r: Word;//DWord;
  aa: array[0..3] of array [0..3] of Byte;
begin
  PDword(@aa[0][0])^ := PDword(InData)^;
  PDword(@aa[1][0])^ := PDword(DWord(InData) + 4)^;     //exception here, because InData is not word-aligned
  PDword(@aa[2][0])^ := PDword(DWord(InData) + 8)^;
  PDword(@aa[3][0])^ := PDword(DWord(InData) + 12)^;

  //...
  //do some math here
  //...

  PDWord(OutData)^ := PDWord(@aa[0][0])^;
  PDWord(DWord(OutData) + 4)^ := PDWord(@aa[1][0])^;
  PDWord(DWord(OutData) + 8)^ := PDWord(@aa[2][0])^;
  PDWord(DWord(OutData) + 12)^ := PDWord(@aa[3][0])^;
end;


procedure DummyProc;
var
  i: Integer;
const
  OddData: array[0..16] of Byte = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);   //org 0x237A4;
begin
  for i := 0 to 2 do
    LATA := OddData[i];
end;


procedure TestAddressException;
const
  InputData: array[0..15] of Byte = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);  //org 0x235A4;
var
  ALocalVar: array[0..15] of Byte;
begin
  ProcessData(PByte(@InputData), PByte(@ALocalVar));
end;


begin
  Delay_ms(100);
  Init_MCU;
  DummyProc;

  TestAddressException;
end.

Code: Select all

_ProcessData:
0x0200	0xFA0010  	LNK	#16
;AddrException.mpas,53 :: 		begin
;AddrException.mpas,54 :: 		PDword(@aa[0][0])^ := PDword(InData)^;
0x0202	0x470060  	ADD	W14, #0, W0
0x0204	0x78183A  	MOV	[W10++], [W0++]
0x0206	0x78102A  	MOV	[W10--], [W0--]
;AddrException.mpas,55 :: 		PDword(@aa[1][0])^ := PDword(DWord(InData) + 4)^;
0x0208	0x470264  	ADD	W14, #4, W4
0x020A	0x78000A  	MOV	W10, W0
0x020C	0xEB0080  	CLR	W1
0x020E	0x400164  	ADD	W0, #4, W2
0x0210	0x4881E0  	ADDC	W1, #0, W3
0x0212	0xBE0012  	MOV.D	[W2], W0    ; Exception here, because W2 is an odd number
0x0214	0xBE8A00  	MOV.D	W0, [W4]
;AddrException.mpas,56 :: 	
Microcontroller: PIC24EP512GU810.
Compiler mikroPascal for dsPIC, v7.0.1.

In the above example, the ProcessData procedure is called with a constant, because it is easier to reproduce allocation of that constant at an odd address without memory specifiers. However, when this is called with a variable, from another procedure, I'm not sure if that variable will always have an aligned address or I should use global vars with hardcoded addresses.

I there any way to make sure variables and constants are allocated at word-aligned addresses? I already have several 1KB arrays in this project, passed to this kind of code, so converting them to array of words is not an option. Also, manually allocating at aligned addresses is an extra maintenance that sooner or later will be impossible to do.

The problem may also be found in PIC32 under a different use case viewtopic.php?f=172&t=61287
Thank you :)
Attachments
AddrException.zip
(6.49 KiB) Downloaded 128 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Address exception on array of byte - mP 7.0.1

#2 Post by filip » 21 May 2018 12:48

Hi,

I apologize for this issue, I have successfully reproduced it.

I will consult our developers, maybe there is a workaround for it.

Regards,
Filip.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#3 Post by VCC » 30 May 2018 15:54

Hi,
here https://libstock.mikroe.com/projects/vi ... ess-finder is a tool, which can notify users about misaligned variables and constants. It works with both 16-bit and 32-bit addresses.

OT
Posts: 581
Joined: 19 May 2005 05:08
Location: Fairbanks, Alaska

Re: Address exception on array of byte - mP 7.0.1

#4 Post by OT » 30 May 2018 21:15

Did you test this with ver 7.1.0? A user reports an alignment problem went away in 7.1.0, reported here:
viewtopic.php?f=106&t=72125

Thanks for making a tool available.
mikropascal dsPIC, Visual TFT, MMBdsPIC v.105, 1.10_9A, mikroProg, "Big"(P30F6012A)EasydsPIC2

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#5 Post by VCC » 31 May 2018 05:47

No, I didn't test it on 7.1.0. This issue seems to be intermittent, because adding or removing code will make the routines, variables and constants be allocated at different addresses. Sooner or later, some variables and/or constants will be misaligned and they will have to be aligned. I intend to use this tool for any new version of these compilers, no matter how many fixes will be added regarding this matter. Anyway, no changes were mentioned in the new compiler version, beside adding support for new MCUs and fixing some memory size limitation, so I don't expect anything to have been fixed. Eventually, I'll have to look into the new version about how it allocates stuff.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Address exception on array of byte - mP 7.0.1

#6 Post by Dany » 04 May 2020 12:17

Hi Vcc,

Thanks for again a wonderful and usefull tool. :D

One question however: If I remember well (also to be found in the documentation of the Pic24/dsPIC compiler) then single byte variables are allowed to reside on odd addresses, while all others have to be aligned to even addresses.
Nevertheless, single byte variables are flagged as 'misaligned' by the tool. Is this done on purpose or what reason is behind it?
Thanks in advance.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#7 Post by VCC » 05 May 2020 07:24

Hi,
all variables and constants are displayed as they are. If you want more options/features (e.g. filtering in this case), any feedback is appreciated.

BTW, do you know how to use the aligned attribute in mikroPascal (PIC24 or PIC32)? It seems that the compiler doesn't like the keyword.
It is mentioned as working in mikroC (maybe it's a mikroC feature only): viewtopic.php?p=298608#p298608
Thank you :)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Address exception on array of byte - mP 7.0.1

#8 Post by Dany » 14 May 2020 21:50

VCC wrote:
05 May 2020 07:24
BTW, do you know how to use the aligned attribute in mikroPascal (PIC24 or PIC32)? It seems that the compiler doesn't like the keyword.
It is mentioned as working in mikroC (maybe it's a mikroC feature only): viewtopic.php?p=298608#p298608
Thank you :)
Well, 'aligned' is not mentioned in the compiler's help.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Address exception on array of byte - mP 7.0.1

#9 Post by filip » 15 May 2020 14:25

Hi,

Variable attribute aligned is currently implemented in mikroC only.

Regards,
Filip.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#10 Post by VCC » 23 May 2020 11:30

Dany wrote:
14 May 2020 21:50
Well, 'aligned' is not mentioned in the compiler's help.
filip wrote:
15 May 2020 14:25
Hi,

Variable attribute aligned is currently implemented in mikroC only.

Regards,
Filip.
Good to know, thank you.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#11 Post by VCC » 25 May 2020 21:46

Dany wrote:
04 May 2020 12:17
Nevertheless, single byte variables are flagged as 'misaligned' by the tool.
Hi,
see the new version of the tool https://libstock.mikroe.com/projects/vi ... ess-finder It has a checkbox to hide byte sized variables.
:D

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Address exception on array of byte - mP 7.0.1

#12 Post by Dany » 30 May 2020 18:39

VCC wrote:
25 May 2020 21:46
Hi,
see the new version of the tool https://libstock.mikroe.com/projects/vi ... ess-finder It has a checkbox to hide byte sized variables.
:D
Thanks very much.
One small remark (no problem tough): the item 'Hide Byte Sized items' has no bound to the right hand side of the tool window (e.g. after horizontal resizing).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Address exception on array of byte - mP 7.0.1

#13 Post by VCC » 31 May 2020 05:10

Dany wrote:
30 May 2020 18:39
Thanks very much.
One small remark (no problem tough): the item 'Hide Byte Sized items' has no bound to the right hand side of the tool window (e.g. after horizontal resizing).
Fixed. Thank you for reporting :)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Address exception on array of byte - mP 7.0.1

#14 Post by Dany » 03 Jun 2020 18:52

VCC wrote:
25 May 2020 21:46
Hi,
see the new version of the tool https://libstock.mikroe.com/projects/vi ... ess-finder It has a checkbox to hide byte sized variables.
Hi Vcc,

Not only byte size variables tend to be 'misaligned' but also arrays of byte. This sounds logical as long as the bytes in the array are approached as single bytes of course. So, from the compiler's point of view byte (arrays) do not have to be aligned.
If approached as word or Dword things can go wrong of course.
In this respect it is perhaps a little strong to talk about 'mis-alignment' (they only are a risk if approached in a non-byte way I think), but I do not have another name that reflects the problem well.

To conclude:
Byte arrays have the same alignment restrictions (none) as single bytes.
Also Word arrays or Dword arrays have the same alignment restrictions as single words (alignment 2) or Dwords (alignment 2 for Pic24/dsPIC and alignment 4 for PIC32).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Address exception on array of byte - mP 7.0.1

#15 Post by Dany » 03 Jun 2020 19:13

Code: Select all

//** Variables locations ** 
//ADDRESS    SIZE    VARIABLE
//----------------------------------------------
0xA0000000      [16]    _MyRec2
0xA0000010       [1]    _Siz2
0xA0000011       [1]    _Siz
0xA0000014      [52]    _MyRec
Hi Vcc,
Above is an exerpt of a Pic32 project listfile. However no variables were shown in the Check for Mis alignment tool (generated report from the tool):

Code: Select all

2020.06.03 20:09:11
//** Variables locations **
//ADDRESS    SIZE    VARIABLE
//----------------------------------------------

//** Constants locations **
//ADDRESS    SIZE    CONSTANT
//----------------------------------------------
Am I missing something?
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”