FTDI Community
General Category => Discussion - Software => Topic started by: willycat on August 22, 2021, 11:31:39 am
-
Hi
I wasted my time to try to found why my code is not working properly and the reason seems to be your VinAsm tool.
Look at this piece of my code:
...
typedef struct _BSS_t
{
...
unsigned char devsState;
...
} BSS_t;
...
BSS_t *BSS;
...
unsigned char msk1;
unsigned char msk2;
unsigned char msk3;
unsigned char msk4;
unsigned char msks1;
unsigned char msks2;
...
void cmd_cr()
{
msk1 = HOST_DD1;
msk2 = HOST_DD2;
msk3 = HOST_DR1;
msk4 = HOST_DR2;
msks1 = (HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
msks2 = ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
BSS->devsState &= ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
}
...
The compilation process produce a code which seems correct to me in the ".asm" generated file:
.FUNCTION "cmd_cr"
.RETURN "void" 0 0 0 3 0 0 0
PUSH32 %r0
PUSH32 %r1
.LINE 56
LD8 msk1 $1
.LINE 57
LD8 msk2 $2
.LINE 58
LD8 msk3 $4
.LINE 59
LD8 msk4 $8
.LINE 61
LD8 msks1 $15
.LINE 62
LD8 msks2 $240
.LINE 64
LD16 %r0 $BSS
CPY16 %r0 (%r0)
LD16 %ebx $280
ADD16 %r0 %ebx
CPY8 %r1 (%r0)
AND8 %r1 $240 <-----
CPY8 (%r0) %r1
.LINE 64
POP32 %r1
POP32 %r0
RTS
.FUNC_END "cmd_cr"
Look especially at the marked line: $240 = 0xf0 which is effectively ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2)
But when i take a look at the disassembler file genereted during debugging, i find this which explain why my code doesn't work:
158ee f200 800c PUSH32 0x000c
158f0 f200 8010 PUSH32 0x0010
158f2 2201 03af LD8 0x03af $0x01
158f4 2202 03b0 LD8 0x03b0 $0x02
158f6 2204 03b1 LD8 0x03b1 $0x04
158f8 2208 03b2 LD8 0x03b2 $0x08
158fa 220f 03b3 LD8 0x03b3 $0x0f
158fc 22f0 03b4 LD8 0x03b4 $0xf0
158fe 2400 000c 03ad LD16 0x000c $0x03ad
15901 4401 400c 000c CPY16 0x000c (0x000c)
15904 2400 0004 0118 LD16 0x0004 $0x0118
15907 7400 4004 000c ADD16 0x000c 0x0004
1590a 4401 000c 0010 CPY8 0x0010 (0x000c)
1590d 4a30 0010 AND8 0x0010 $0x30 <-----
1590f 4500 0010 000c CPY8 (0x000c) 0x0010
15912 f380 0010 POP32 0x0010
15914 f380 000c POP32 0x000c
15916 0006 RTS
0x03ad is the address of the BSS pointer. 0x118 is the offset of the devsState member in the BSS_t structure.
Look especially at the marked line: 0x30 is not 0xf0 and then is not ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2) !
Is this a known bug ?
Apparently, this is a problem with the &= operator: If i replace
BSS->devsState &= ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
by
BSS->devsState = BSS->devsState & ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
i obtain this code:
15951 2400 0014 03ad LD16 0x0014 $0x03ad
15954 4401 4014 0018 CPY16 0x0018 (0x0014)
15957 2400 0004 0118 LD16 0x0004 $0x0118
1595a 7400 4004 0018 ADD16 0x0018 0x0004
1595d 4401 4014 0014 CPY16 0x0014 (0x0014)
15960 2400 0004 0118 LD16 0x0004 $0x0118
15963 7400 4004 0014 ADD16 0x0014 0x0004
15966 4401 0014 0014 CPY8 0x0014 (0x0014)
15969 4400 0014 0000 CPY8 0x0000 0x0014
1596c 4e00 0000 00ff 0000 AND32 0x0000 $0x000000ff
15970 2600 0014 fff0 ffff LD32 0x0014 $0xfffffff0
15974 5400 8000 0014 AND32 0x0014 0x0000
15977 4500 0014 0018 CPY8 (0x0018) 0x0014
which works but is badly optimized.
Willy.
-
Hi Willy,
It maybe a bug, but not a known one.
We have been unable to replicate the issue you observed here . It might be due to the level of optimisations? What optimisation level are you working at?
Regards
FTDI Community
-
Hi,
to replicate this issue, compile this file:
/*********************************************************************************
*********************************************************************************/
#define HOST_DD1 0x01
#define HOST_DD2 0x02
#define HOST_DR1 0x04
#define HOST_DR2 0x08
#define HOST_DEV 0x40
#define HOST_DSK 0x80
#define NUMBER_OF_DEVICES 6
#define GPIO_A_5 2
#define GPIO_A_6 3
/********************************************************************************/
typedef struct _BSS_t
{
// Hardware type
unsigned char hwType;
// Hardware version
unsigned char hwVersion;
// Swap order LEDs
unsigned char swapActivityLeds;
// Device type bitmap for USB ports 1
unsigned char host1DevType;
// Device type bitmap for USB ports 2
unsigned char host2DevType;
// Devices state
unsigned char devsState;
// State of monitor LEDs
unsigned char monLEDsON;
// Sate of Individual monitor LEDs
unsigned char monLEDsState;
} BSS_t;
/********************************************************************************/
/********************************************************************************/
unsigned char msk1;
unsigned char msk2;
unsigned char msk3;
unsigned char msk4;
unsigned char msks1;
unsigned char msks2;
BSS_t *BSS;
/*********************************************************************************
*********************************************************************************/
void main(void)
{
for (;;);
}
/********************************************************************************/
/********************************************************************************/
void cmd_cr()
{
msk1 = HOST_DD1;
msk2 = HOST_DD2;
msk3 = HOST_DR1;
msk4 = HOST_DR2;
msks1 = (HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
msks2 = ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
BSS->devsState &= ~(HOST_DD1 | HOST_DD2 | HOST_DR1 | HOST_DR2);
}
/********************************************************************************/
/********************************************************************************/
using the following commands:
VinC.exe -c -d 1 -O 4 -o Debug\USB-BUG.asm USB-BUG.c
VinAsm.exe -v -d 1 -o Debug\USB-BUG.obj Debug\USB-BUG.asm
VinL.exe -d 1 -o Debug\USB-BUG Debug\USB-BUG.obj
and you will obtain this file with the bug:
003c0 fa00 03e0 JUMP 0x003e0
003c2 fa00 03c0 JUMP 0x003c0
003c4 fa00 03be JUMP 0x003be
003c6 fa00 03c0 JUMP 0x003c0
003c8 fa00 03c0 JUMP 0x003c0
003ca fa00 03c0 JUMP 0x003c0
003cc fa00 03c0 JUMP 0x003c0
003ce fa00 03c0 JUMP 0x003c0
003d0 b500 0000 OUTPORT 0x100 0x0000
003d2 0000 NOP
003d3 0000 NOP
003d4 0000 NOP
003d5 0000 NOP
003d6 0000 NOP
003d7 0000 NOP
003d8 2e78
003d9 7a79
003da 0000 NOP
003db 0000 NOP
003dc 0000 NOP
003dd 0000 NOP
003de 0000 NOP
003df 0000 NOP
003e0 b028 0001 OUTPORT 0x028 $0x01
003e2 bc00 3ffc INPORT 0x000 0x3ffc
003e4 3204 3ffc SHR8 0x3ffc $0x04
003e6 da01 3ffc CMP8 0x3ffc $0x01
003e8 fa40 0408 JNZ 0x00408
003ea b00f 0000 OUTPORT 0x00f $0x00
003ec b080 0012 OUTPORT 0x080 $0x12
003ee b088 000f OUTPORT 0x088 $0x0f
003f0 b089 00f0 OUTPORT 0x089 $0xf0
003f2 b084 0000 OUTPORT 0x084 $0x00
003f4 b085 0000 OUTPORT 0x085 $0x00
003f6 b08a 00ff OUTPORT 0x08a $0xff
003f8 b08b 003f OUTPORT 0x08b $0x3f
003fa b081 0001 OUTPORT 0x081 $0x01
003fc c883 0000 PORTTST 0x083 $0 Z
003fe fa40 03fc JNZ 0x003fc
00400 b080 0000 OUTPORT 0x080 $0x00
00402 b088 0000 OUTPORT 0x088 $0x00
00404 b089 0000 OUTPORT 0x089 $0x00
00406 b083 00ff OUTPORT 0x083 $0xff
00408 2600 3ffa 08c4 0000 LD32 0x3ffa $0x000008c4
0040c 3201 bffa SHR32 0x3ffa $0x01
0040e 2400 3ffe 0000 LD16 0x3ffe $0x0000
00411 2400 3ff8 001e LD16 0x3ff8 $0x001e
00414 3201 7ff8 SHR16 0x3ff8 $0x01
00416 dc00 3ff8 0000 CMP16 0x3ff8 $0x0000
00419 fa1c 042a JLE 0x0042a
0041b 2d01 3ffa 3ffe CPYROM (0x3ffe) 0x3ffa $01
0041e 6c00 3ffe 0002 ADD16 0x3ffe $0x0002
00421 6e00 3ffa 0001 0000 ADD32 0x3ffa $0x00000001
00425 6c20 3ff8 0001 SUB16 0x3ff8 $0x0001
00428 fa00 0416 JUMP 0x00416
0042a 2600 3ffc 0000 0000 LD32 0x3ffc $0x00000000
0042e 2600 3ff8 0000 0000 LD32 0x3ff8 $0x00000000
00432 0a00 0000 SP_LOAD $0x0000
00434 fa00 043a JUMP 0x0043a
00436 0000 NOP
00437 0000 NOP
00438 0000 NOP
00439 0000 NOP
0043a fa00 043a JUMP 0x0043a
0043c 0006 RTS
0043d f200 800c PUSH32 0x000c
0043f f200 8010 PUSH32 0x0010
00441 2201 0020 LD8 0x0020 $0x01
00443 2202 0021 LD8 0x0021 $0x02
00445 2204 0022 LD8 0x0022 $0x04
00447 2208 0023 LD8 0x0023 $0x08
00449 220f 0024 LD8 0x0024 $0x0f
0044b 22f0 0025 LD8 0x0025 $0xf0
0044d 2400 000c 001e LD16 0x000c $0x001e
00450 4401 400c 000c CPY16 0x000c (0x000c)
00453 8205 400c INC16 0x000c $0x05
00455 4401 000c 0010 CPY8 0x0010 (0x000c)
00458 4a30 0010 AND8 0x0010 $0x30
0045a 4500 0010 000c CPY8 (0x000c) 0x0010
0045d f380 0010 POP32 0x0010
0045f f380 000c POP32 0x000c
00461 0006 RTS
00462 0000 NOP
00463 0000 NOP
00464 0000 NOP
00465 0000 NOP
00466 0000 NOP
00467 0000 NOP
00468 0000 NOP
00469 0000 NOP
0046a 0000 NOP
0046b 0000 NOP
0046c 0000 NOP
0046d 0000 NOP
0046e 0000 NOP
0046f 0000 NOP
00470 0000 NOP
00471 1808
00472 150e
00473 1014
00474 d182
Willy.