array bit access compiler bug

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
SirDudeofWI
Posts: 4
Joined: 18 Jan 2021 17:35

array bit access compiler bug

#1 Post by SirDudeofWI » 18 Jan 2021 19:38

I have had this bug several times and want to call attention to it:

adc2543_sdiport=adc_result[bb].15 'output the address

The intent above is to simply sets an output pin to the highest bit of an array variable.
fyi: adc_result[] is dim as a word. ads2543_sdiport is a valid sbit

The concept is a simple way to roll bits in the result matrix and create a serial stream out the assigned adc2543_sdiport pin.

The problem is:
The pin never changes no matter what the bit is in the array variant.
No compile errors.

The fix after spending hours trying to figure out why the chip is not sending any data is this:

if (adc_result[bb] and 0x8000)=0x8000 then
adc2543_sdiport=1
else
adc2543_sdiport=0
end if

There are probably five other ways to fix this also.

Shouldn't the compiler handle a bit access to an array type?
Is the array type access a pointer so therefor direct bit access is not allowed?

Compiler is V 7.0.1

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

Re: array bit access compiler bug

#2 Post by filip » 20 Jan 2021 14:15

Hi,

I have tried similar code which works for me :

Code: Select all

program test_bit

dim bit_value as bit
dim test_array as word[100]
dim index as byte

main:
  index = 50
  test_array[index] = 0xFFFF
  bit_value = test_array[index].15
end.
Does this code works for you ?

Regards,
Filip.

SirDudeofWI
Posts: 4
Joined: 18 Jan 2021 17:35

Re: array bit access compiler bug

#3 Post by SirDudeofWI » 28 Jan 2021 20:57

Hi,

Thank you for the response. I worked a bit harder to define the problem.

The bug is in the shift left library function. I'm posting the test code and will give the exact variant that causes the bug after it.

This is the test code:


program testproject

' Declarations section

'leds
dim heartb_ledport as sbit at Portc.0
dim heartb_ledddr as sbit at Ddrc.0
dim heartb_ledin as sbit at Pinc.0

dim sio_ledport as sbit at Portc.1
dim sio_ledddr as sbit at Ddrc.1
dim sio_ledin as sbit at Pinc.1

'global
dim adc_result as word[11]
dim aa as byte
dim bb as byte

main:
' Main program

'outputs
heartb_ledport=0
heartb_ledddr=1
sio_ledport=0
sio_ledddr=1

heartb_ledport = 1 'led off

while (true)

sio_ledport = 1 'led off

bb=1 'index
adc_result[bb]=%1010101010101010 'set data to shift out
for aa=0 to 15
heartb_ledport = adc_result[bb].15 'set output pin to highest bit
adc_result[bb] = adc_result[bb] << 1 'shift data left (the bug!!!)

delay_ms(500) 'delay

next aa

sio_ledport=0 'led on - used to flag end of word
delay_ms(500) 'delay

wend

end.


The following and what is posted above will not shift out the lower byte of the (word)matrixed variable.

adc_result[bb]=%1010101010101010 'set data to shift out
for aa=0 to 15
heartb_ledport = adc_result[bb].15 'set output pin to highest bit
adc_result[bb] = adc_result[bb] << 1 'shift data left

The heartb_ledport pin should output %10101010 10101010 but instead outputs %10101010 00000000.


If the code is changed to use a constant for the index then it works correctly:

adc_result[1]=%1010101010101010 'set data to shift out
for aa=0 to 15
heartb_ledport = adc_result[1].15 'set output pin to highest bit
adc_result[1] = adc_result[1] << 1 'shift data left


if the code is change to shift right it works:

adc_result[bb]=%1010101010101010 'set data to shift out
for aa=0 to 15
heartb_ledport = adc_result[bb].0 'set output pin to highest bit
adc_result[bb] = adc_result[bb] >> 1 'shift data right


If the code preloads a word it works:

adc_result[1]=%1010101010101010 'set data to shift out
cc=adc_result[bb]
for aa=0 to 15
heartb_ledport = cc.15 'set output pin to highest bit
cc = cc << 1 'shift data left

I tried direct access to bits wtih no shifting and it worked fine:

adc_result[bb]=%1010101010101010 'set data to shift out
for aa=15 to 0 step -1
heartb_ledport = adc_result[bb].aa 'set output pin to bit
'no shifting


I also tried changing the matrixed variable to an integer. I got the same bad result as the word.
I think that would be expected as the shift-left library function would be the same for both 16bit word and integer datatypes.

So at this point the bug i'm having trouble with would be:

shift-left word[var] does not treat the lower byte correctly
shift-left word[constant] works fine

Do you get the same result?

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

Re: array bit access compiler bug

#4 Post by filip » 01 Feb 2021 11:21

Hi,

I am using latest version of mikroBasic, 7.0.1. The code below works fine :

Code: Select all

bb=1 'index
adc_result[bb]=%1010101010101010 'set data to shift out
for aa=0 to 15
  heartb_ledport = adc_result[bb].15 'set output pin to highest bit
  adc_result[bb] = adc_result[bb] << 1 'shift data left (the bug!!!)
  delay_ms(500) 'delay
next aa
Value pointed by index is shifted correctly to the left and the heartb_ledport is populated with the value of 15th bit.


Regards,
Filip

SirDudeofWI
Posts: 4
Joined: 18 Jan 2021 17:35

Re: array bit access compiler bug

#5 Post by SirDudeofWI » 02 Feb 2021 00:58

Found it.

Had to go to the compiler listing to see it.
The BTS (bit store) is accessing the low byte of adc_result instead of the high byte.
It's not the shift left. It's the bit access to the matrixed word.

;testproject.mbas,43 :: heartb_ledport = adc_result[bb].15 'set output pin to highest bit
0x00F4 0x01009100 LDS R16, _bb+0
0x00F8 0x2F40 MOV R20, R16
0x00FA 0xE050 LDI R21, 0
0x00FC 0x0F44 LSL R20
0x00FE 0x1F55 ROL R21
0x0100 0xE002 LDI R16, lo_addr(_adc_result+0)
0x0102 0xE011 LDI R17, hi_addr(_adc_result+0)
0x0104 0x01FA MOVW R30, R20
0x0106 0x0FE0 ADD R30, R16
0x0108 0x1FF1 ADC R31, R17
0x010A 0x9101 LD R16, Z+
0x010C 0x9111 LD R17, Z+
0x010E 0xFB07 BST R16, 7 <<<<<--------- THAT IS THE LOW BYTE!!!
0x0110 0xB3B5 IN R27, PORTC+0
0x0112 0xF9B0 BLD R27, 0
0x0114 0xBBB5 OUT PORTC+0, R27

I commented out the basic code above and added the assembly. Then changed to BST R17,7 and all works.

So the VB Patch that forces bit 15 is:
heartb_ledport = hi(adc_result[bb]).7
This seems to force the assembler to use the exact offset byte instead of the word:

;testproject.mbas,43 :: heartb_ledport = hi(adc_result[bb]).7 'set output pin to highest bit
0x00F6 0x01009100 LDS R16, _bb+0
0x00FA 0x2F40 MOV R20, R16
0x00FC 0xE050 LDI R21, 0
0x00FE 0x0F44 LSL R20
0x0100 0x1F55 ROL R21
0x0102 0xE002 LDI R16, lo_addr(_adc_result+0)
0x0104 0xE011 LDI R17, hi_addr(_adc_result+0)
0x0106 0x0F04 ADD R16, R20
0x0108 0x1F15 ADC R17, R21
0x010A 0x01F8 MOVW R30, R16
0x010C 0x9631 ADIW R30, 1
0x010E 0x8100 LD R16, Z <<<----- byte only
0x0110 0xFB07 BST R16, 7
0x0112 0xB3B5 IN R27, PORTC+0
0x0114 0xF9B0 BLD R27, 0
0x0116 0xBBB5 OUT PORTC+0, R27

This has been driving me crazy. (not a long drive these days...)

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

Re: array bit access compiler bug

#6 Post by filip » 02 Feb 2021 11:43

Hi,

I apologize for this issue, I have reproduced this problem.

I have reported this to our developers.

Regards,
Filip.

SirDudeofWI
Posts: 4
Joined: 18 Jan 2021 17:35

Re: array bit access compiler bug

#7 Post by SirDudeofWI » 05 Feb 2021 19:30

Thank you Filip!

Glad to have you take the time to work with me on it. Looking forward to the update.

Post Reply

Return to “mikroBasic PRO for AVR General”