tests/main.c
author Edouard Tisserant
Wed, 24 Nov 2021 08:58:58 +0100
changeset 1095 36bbd0b64816
parent 279 c0453b7f99df
permissions -rwxr-xr-x
Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     1
/*
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     3
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     6
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    10
 *  (at your option) any later version.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    11
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    15
 *  GNU General Public License for more details.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    16
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    19
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    20
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    21
 * This code is made available on the understanding that it will not be
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    23
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    24
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    25
 * Minimal standaloen C runtime, for test purpose.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    26
 * TODO : UPDATE
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    27
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    28
 */
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 150
diff changeset
    29
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    30
#ifdef __WIN32__
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    31
#include <stdio.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    32
#include <sys/timeb.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    33
#include <time.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    34
#include <windows.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    35
#else
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    36
#include <stdio.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    37
#include <string.h>
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    38
#include <time.h>
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    39
#include <signal.h>
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    40
#endif
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    41
150
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    42
#include "iec_types.h"
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    43
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    44
/*
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    45
 * Functions and variables provied by generated C softPLC
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    46
 **/ 
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    47
extern int common_ticktime__;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    48
150
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    49
IEC_BOOL __DEBUG;
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    50
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    51
/*
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    52
 * Functions and variables provied by plc.c
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    53
 **/ 
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    54
void run(long int tv_sec, long int tv_nsec);
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    55
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    56
#define maxval(a,b) ((a>b)?a:b)
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    57
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    58
#ifdef __WIN32__
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    59
void timer_notify()
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    60
{
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    61
   struct _timeb timebuffer;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    62
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    63
   _ftime( &timebuffer );
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    64
   run(timebuffer.time, timebuffer.millitm * 1000000);
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    65
}
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    66
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    67
int main(int argc,char **argv)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    68
{
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    69
    HANDLE hTimer = NULL;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    70
    LARGE_INTEGER liDueTime;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    71
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    72
    liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1);;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    73
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    74
    // Create a waitable timer.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    75
    hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    76
    if (NULL == hTimer)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    77
    {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    78
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    79
        return 1;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    80
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    81
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    82
    config_init__();
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    83
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    84
    // Set a timer to wait for 10 seconds.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    85
    if (!SetWaitableTimer(hTimer, &liDueTime, common_ticktime__, NULL, NULL, 0))
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    86
    {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    87
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    88
        return 2;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    89
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    90
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    91
    while(1){
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    92
    // Wait for the timer.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    93
        if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    94
        {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    95
            printf("WaitForSingleObject failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    96
            break;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    97
        }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    98
        timer_notify();
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    99
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   100
    return 0;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   101
}
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   102
#else
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   103
void timer_notify(sigval_t val)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   104
{
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   105
    struct timespec CURRENT_TIME;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   106
    clock_gettime(CLOCK_REALTIME, &CURRENT_TIME);
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   107
    run(CURRENT_TIME.tv_sec, CURRENT_TIME.tv_nsec);
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   108
}
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   109
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   110
void catch_signal(int sig)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   111
{
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   112
  signal(SIGTERM, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   113
  signal(SIGINT, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   114
  printf("Got Signal %d\n",sig);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   115
}
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   116
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   117
int main(int argc,char **argv)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   118
{
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   119
    timer_t timer;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   120
    struct sigevent sigev;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   121
    long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   122
    time_t tv_sec = common_ticktime__/1000;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   123
    struct itimerspec timerValues;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   124
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   125
    memset (&sigev, 0, sizeof (struct sigevent));
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   126
    memset (&timerValues, 0, sizeof (struct itimerspec));
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   127
    sigev.sigev_value.sival_int = 0;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   128
    sigev.sigev_notify = SIGEV_THREAD;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   129
    sigev.sigev_notify_attributes = NULL;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   130
    sigev.sigev_notify_function = timer_notify;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   131
    timerValues.it_value.tv_sec = tv_sec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   132
    timerValues.it_value.tv_nsec = tv_nsec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   133
    timerValues.it_interval.tv_sec = tv_sec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   134
    timerValues.it_interval.tv_nsec = tv_nsec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   135
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   136
    config_init__();
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   137
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   138
    timer_create (CLOCK_REALTIME, &sigev, &timer);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   139
    timer_settime (timer, 0, &timerValues, NULL);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   140
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   141
    /* install signal handler for manual break */
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   142
    signal(SIGTERM, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   143
    signal(SIGINT, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   144
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   145
    pause();
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   146
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   147
    timer_delete (timer);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   148
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   149
    return 0;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   150
}
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   151
#endif