This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Re: Long Integer Multiple Error

Think you.
I modified my source :

        printf("%lu * %bu = ", N, M);
        printf("%lu\n", Freq);

        Freq=25690112L;
        printf("%lu * %bu = ", Freq, 4);
        Freq*=4;
        printf("%lu\n", Freq);


It work correctly.
but the Long Integer Multiple fault also.

Run Result:
24500000 * 4 = 4294926936
25690112 * 4 = 1568

  • i give you answer in other thread!!!

    what you expect???

    24500000 * 4 = 98000000

    NO

    biggest unsigned long integer is 4294967296
    biggest signed integer is 2147483647

    on keil the long integer is 32 bits

    you have a maths overflow!!!

  • in c an integer maths overflow can give undefinededed result

    it normally wraps but not is guaranteeed

    referr to ansi c book

    float maths give error value you think

  • Yes, I expect the result is:
    24500000 * 4 = 98000000

    I defined the variable as unsigned long, and unsigned long range is 0..4294967295.
    The 9800000 is far smaller than 4294967295, so I think there may exists a bug of Long Integer Multiple.

  • When I disable the "Use On-chip Arithmetic Accelerator" on Target Options, the program runs
    correctly.

  • "Use On-chip Arithmetic Accelerator"

    what chip is that?

    are you one more of those that expect the simulator to simulate exotic chips fully?

    see my general comment at http://www.keil.com/forum/docs/thread9976.asp

    Erik

  • Does your specific chip actually have an On-chip Arithmetic Accelerator?

  • Long Integer Multiple Error. Target Options: MCU : C8051F120F, Use On-chip Arithmetic Accelerator.

    Source:

    #include <C8051F120.H>
    #include <stdio.h>
    
    unsigned long Freq;
    
    void DetectFreq()
    {
            unsigned long N;
            unsigned char M;
        SFRPAGE   = CONFIG_PAGE;
            M= PLL0MUL;
            switch (CLKSEL&0x03) {
                    case 00:        //Internal XTAL
                    case 03:
                                            Freq=25690112;//24.5L*1024L*1024L
                                            switch(OSCICN&0x03){
                                                    case 0x00: Freq/=8; break;
                                                    case 0x01: Freq/=4; break;
                                                    case 0x02: Freq/=2; break;
                                            }
                                            break;
    
                    case 01:        // External XTAL
                                            Freq=25L*1024L*1024L;
                                            switch (OSCXCN&0x70) {
                                                    case 0x03:
                                                    case 0x05:
                                                    case 0x07: Freq=Freq/2; break;
                                            }
                                            break;
                    case 02:        //PLL
                                            if (PLL0CN&0x04) { // External XTAL
                                                    Freq=26214400L;  //25L*1024L*1024L
                                                    N=Freq;
                                                    Freq*=PLL0MUL;
                                                    Freq/=PLL0DIV;
                                                    switch (OSCXCN&0x70) {
                                                            case 0x03:
                                                            case 0x05:
                                                            case 0x07: Freq=Freq/2; break;
                                                    }
    
                                            }
                                            else{
                                                    //Freq=25690112L;
                                                    Freq=24500000L;//25690112L;
                                                    N=Freq;
                                                    Freq*=PLL0MUL;
                                                    //FreqMul(PLL0MUL);
                                                    Freq/=PLL0DIV;
                                                    switch(OSCICN&0x03){
                                                            case 0x00: Freq/=8; break;
                                                            case 0x01: Freq/=4; break;
                                                            case 0x02: Freq/=2; break;
                                                    }
                                            }
                                            break;
            }
            SFRPAGE   = LEGACY_PAGE;
            printf("%lu * %u = ", N, M);
            printf("%lu\n", Freq);
    
            Freq=25690112L;
            printf("%lu * %u = ", Freq, 4);
            Freq*=4;
            printf("%lu\n", Freq);
    
    }
    
    void InitSerial()
    {
            SFRPAGE =LEGACY_PAGE;
        SCON0  = 0x50;                      /* SCON: mode 1, 8-bit UART, enable rcvr      */
        TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
        TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
        TR1   = 1;                  /* TR1:  timer 1 run                          */
        TI0    = 1;                  /* TI:   set TI to send first char of UART    */
    }
    
    // Peripheral specific initialization functions,
    // Called from the Init_Device() function
    void Reset_Sources_Init()
    {
        WDTCN     = 0xDE;
        WDTCN     = 0xAD;
    }
    
    void EMI_Init()
    {
        SFRPAGE   = EMI0_PAGE;
        EMI0CF    = 0x1F;
        //EMI0TC    = 0x4F;
               EMI0TC    = 0xCF;
    
    }
    
    void Port_IO_Init()
    {
        SFRPAGE   = CONFIG_PAGE;
        P4MDOUT   = 0xFF;
        P5MDOUT   = 0xFF;
        P6MDOUT   = 0xFF;
        P7MDOUT   = 0xFF;
        XBR0      = 0x07;
        XBR1      = 0x94;
        XBR2      = 0x40;
    }
    
    void Oscillator_Init()
    {
        data int i = 0;
        SFRPAGE   = CONFIG_PAGE;
        CCH0CN    &= ~0x20;
        SFRPAGE   = LEGACY_PAGE;
        FLSCL     = 0xB0;
        SFRPAGE   = CONFIG_PAGE;
        CCH0CN    |= 0x20;
        PLL0CN    |= 0x01;
        PLL0DIV   = 0x01;
        PLL0FLT   = 0x01;
        PLL0MUL   = 0x04;
        for (i = 0; i < 15; i++);  // Wait 5us for initialization
        PLL0CN    |= 0x02;
        //while ((PLL0CN & 0x10) == 0);
        CLKSEL    = 0x02;
        OSCICN    = 0x83;
    }
    
    // Initialization function for device,
    // Call Init_Device() from your main program
    void Init_Device(void)
    {
        Reset_Sources_Init();
        EMI_Init();
        Port_IO_Init();
        Oscillator_Init();
    }
    
    void main()
    {
            Init_Device();
            InitSerial();
        printf ("Hello World\n");   /* Print "Hello World" */
            DetectFreq();
            printf("%lu", Freq);
            while(1);
    }
    
    

    Real Run Result:


    Hello World
    24500000 * 4 = 4294926936
    25690112 * 4 = 1568
    1568

    Our expect result:

    Hello World
    24500000 * 4 = 98000000
    25690112 * 4 = 102760448
    102760448
    


    I defined the variable as unsigned long, and unsigned long range is 0..4294967295.
    The 9800000 is far smaller than 4294967295, so I think there may exists a bug of Long Integer Multiple.

    conclusion:

    1.There are mistake of Long Integer Mutiple where open the on-chip arithmetic accelerator.

  • You didn't answer the question:

    Does the C8051F120F actually have an On-chip Arithmetic Accelerator?

    and, if it does, do the Keil tools support it?

  • sir heixi

    i loooked at C8051F120.H

    it is a cygnal C8051F120/1/2/3 or C8051F124/5/6/7 yes ????

    i found datasheeeet at
    www.kemt.fei.tuke.sk/.../c8051f12x.pdf

    it has lot of good peritherrals but i see not by on the on-chip arithmeratic accceleratoror??????!!!!!!

    do it have one you thinking??????

    if no then silly to set tick in ide probably

  • C8051F120 DataSheet said that: Page 157, Rev1.2
    13. MULTIPLY AND ACCUMULATE (MAC0)
    The C8051F120/1/2/3 devices include a multiply and accumulate engine which can be used to speed up many mathematical
    operations. MAC0 contains a 16-by-16 bit multiplier and a 40-bit adder, which can perform integer or fractional
    multiply-accumulate and multiply operations on signed input values in two SYSCLK cycles. A rounding
    engine provides a rounded 16-bit fractional result after an additional (third) SYSCLK cycle. MAC0 also contains a 1-
    bit arithmetic shifter that will left or right-shift the contents of the 40-bit accumulator in a single SYSCLK cycle.
    Figure 13.1 shows a block diagram of the MAC0 unit and its associated Special Function Registers.

  • yes i see

    i look at prelimanary data who does not have mac

    i look at newe data rev 1.4 at

    www.silabs.com/.../C8051F12x-13x.pdf

    has details in page 165

    do you errorr in ide or on hardare?????

    sir erac is very right with ide not emulater all devs

    if hardware is give error tell keil see they fix it??????

  • My Program runs on simulator .
    It runs out correct result on simulator when not use on-chip arichmetic accelerator.

  • but do it run correct on hardwaaare???!!!

    if just on simuletor then think it is simulater do not simelate good!!!!????

    then no be bug of compiler huh????

  • but do it run correct on hardwaaare???!!!

    if just on simuletor then think it is simulater do not simelate good!!!!????

    then no be bug of compiler huh????

    A SIMULATOR simulates and as such will NEVER give you the full (and correct) story. Also, as stated before, there is no way it is economically feasible for Keil to simulate every exotic function of all exotic derivatives of the '51.

    such special interest issues is what keeps Keil from taking time for general and important issues such as changing compiler things like
    mov r7,Ralph
    mov a,r7
    to
    mov a,Ralph
    in the COMPILER, not the optimizer

    If you doubt the simulator, try it on the hardware, stick the code on your devboard use the emulator and check. This is so darn simple and would have been resolved in 5 minutes instead of the DAYS these threads has run.

    Erik

  • "A SIMULATOR simulates and as such will NEVER give you the full (and correct) story"

    i not think you be right with NEVER!!!!!!

    a dedacated simulator for a specifec cpu is posssible to give correct story

    but point by me is that compiler probly do not have the bug whot he say before!!!