I have a simple program running in HCS08AC128 simulation mode. I found that the argunment passed to a function cannot be correctly received by the function. In main, I call this function SD_CLKDelay (8). This is the function:
void SD_CLKDelay (byte Frames)
Now, if I single step [F5] into this function, the variable Frames should show 8, but instead it shows 0x21 in the variable window. Here is assembly for main calling:
0014 a608  LDA #8
0016 cd0000  JSR SD_CLKDelay
but the logic of the function is still correct when looking at the assembly
0000 2008  BRA LA ;abs = 000a
0002 aeff  LDX #-1
0004 8c  CLRH
0005 87  PSHA
0006 cd0000  JSR SPI_Send_byte
0009 86  PULA
000a 97  TAX
000b 4a  DECA
000c 5d  TSTX
000d 26f3  BNE L2 ;abs = 0002
but when I single step and goes to inside the SPI_Send_byte() function, the variable is wrong and the actual behavior is also wrong. Here is what happen in the assembly:
0000 87  PSHA
0001 0b00fd  BRCLR 5,_SPI1S,L1 ;abs = 0001
0004 b600  LDA _SPI1S
0006 95  TSX
0007 f6  LDA ,X
0008 b700  STA _SPI1D
000a 0f00fd  BRCLR 7,_SPI1S,LA ;abs = 000a
000d b600  LDA _SPI1D
000f 8a  PULH
0010 81  RTS
Now it is obvious that the argunment 0xFF passed to the function is lost.
So there are two problems:
1. Why the compiler compile the code incorrectly?
2. Why the debugger simulator showing the incorrect variable values?
I am using the latest CodeWarrior Development Studio just downloaded from Freescale, and using all the default build configuration.
BR - Henry
Do you have any compiler warnings about implicitly declared functions? Take those very seriously, if the implied type does not 100% match the actual type then such argument passing bugs are happening. And the implicitly deduced types rarely match the declared ones, ....
Thanks for your answer. Yes, the "implicitly declared function" warning was there. After I put in function prototypes, then the problem goes away. I was wondering if it is possible to make this into an error instead of warning? Mismatch function prototype should be considered a very serious mistake and should be regarded as error to make sure the programmer fix it before going on.
The other problem should be just regarded as the debugger watch window problem which should not affect the normal operation of the code.
I think this should be an error as well. If I remember correctly, declaring functions without a prototype is considered an obsolete part of the C language by the standard committee, and they will likely remove it from the language should there ever be a new standard released.
You can set the C compiler options to regard "implicitly declared function" warnings as an error instead:
option -Wpd in the C compiler options.
Also available in the drop down menu Edit>XXXX Settings>Compiler for HCXX>Messages>Error for implicit parameter declaration
(default is unclicked and disabled)