ngspice/examples/xspice/d_process/prog1in4out.c

141 lines
3.2 KiB
C
Raw Normal View History

2023-10-12 00:03:57 +02:00
/*
The organization of this file is modelled after the motorforce example
developed by Uros Platise.
*/
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#if !defined(_MSC_VER) && !defined(__MINGW64__)
#include <unistd.h>
#endif
#if defined(_MSC_VER)
#include <io.h>
#include <process.h>
#include <Windows.h>
#endif
#include "d_process.h"
#define DIGITAL_IN 1
#define DIGITAL_OUT 4
static int compute(
uint8_t *datain, int insz, uint8_t *dataout, int outsz, double time
);
//#define ENABLE_DEBUGGING 1
#ifdef ENABLE_DEBUGGING
2023-10-12 00:03:57 +02:00
static int known_bp(int iargc)
{
return iargc;
}
#endif
2023-10-12 00:03:57 +02:00
int main(int argc, char *argv[]) {
int inlen = D_PROCESS_DLEN(DIGITAL_IN);
int outlen = D_PROCESS_DLEN(DIGITAL_OUT);
#if defined(_MSC_VER) || defined(__MINGW64__)
#pragma pack(push, 1)
struct in_s {
double time;
uint8_t din[D_PROCESS_DLEN(DIGITAL_IN)];
} in;
struct out_s {
uint8_t dout[D_PROCESS_DLEN(DIGITAL_OUT)];
} out;
#pragma pack(pop)
#else
struct in_s {
double time;
uint8_t din[D_PROCESS_DLEN(DIGITAL_IN)];
} __attribute__((packed)) in;
struct out_s {
uint8_t dout[D_PROCESS_DLEN(DIGITAL_OUT)];
} __attribute__((packed)) out;
#endif
int pipein = 0; // default stdin to recv from ngspice
int pipeout= 1; // default stdout to send to ngspice
#if defined(_MSC_VER) || defined(__MINGW64__)
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
#endif
#ifdef ENABLE_DEBUGGING
2023-10-12 00:03:57 +02:00
#if defined(_MSC_VER) || defined(__MINGW64__)
fprintf(stderr, "%s pid %d\n", argv[0], _getpid());
#else
fprintf(stderr, "%s pid %d\n", argv[0], getpid());
#endif
#if !defined(_MSC_VER) && !defined(__MINGW64__)
if (getenv("GO_TO_SLEEP")) {
sleep(40);
}
#endif
#if defined(__MINGW64__)
if (getenv("GO_TO_SLEEP")) {
sleep(40);
}
#endif
#if defined(_MSC_VER)
if (getenv("GO_TO_SLEEP")) {
Sleep(60000);
}
#endif
(void)known_bp(argc);
for (int i=0; i<argc; i++) {
2023-10-12 00:03:57 +02:00
fprintf(stderr, "[%d] %s\n", i, argv[i]);
}
#endif
2023-10-12 00:03:57 +02:00
if (d_process_init(pipein, pipeout, DIGITAL_IN, DIGITAL_OUT) ) {
#if defined(_MSC_VER) || defined(__MINGW64__)
while(_read(pipein, &in, sizeof(in)) == sizeof(in)) {
#else
while(read(pipein, &in, sizeof(in)) == sizeof(in)) {
#endif
if (!compute(in.din, inlen, out.dout, outlen, in.time)) {
return 1;
}
2023-10-12 00:03:57 +02:00
#if defined(_MSC_VER) || defined(__MINGW64__)
_write(pipeout, &out, sizeof(out));
#else
write(pipeout, &out, sizeof(out));
#endif
}
return 0;
}
return -1;
}
static int compute(
uint8_t *datain, int insz, uint8_t *dataout, int outsz, double time
)
{
static uint8_t next[2][16] = {
{15, 14, 0, 1, 13, 12, 2, 3, 11, 10, 4, 5, 9, 8, 6, 7},
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0}
};
uint8_t inbit0 = datain[0] & 1;
static uint8_t count = 0;
if (time < 0.0) {
//fprintf(stderr, "Reset prog1in4out at time %g\n", -time);
2023-10-12 00:03:57 +02:00
count = 15;
}
if (count < 15) {
count++;
} else {
count = 0;
}
dataout[0] = (next[inbit0][count]) & 0x0F;
return 1;
}