Добавить ядро цифрового Scope и адаптер декодеров DSView
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
#ifndef SET_LOGIC_SCOPE_H
|
||||
#define SET_LOGIC_SCOPE_H
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LOGIC_SCOPE_MAX 65536u
|
||||
typedef struct {
|
||||
uint8_t samples[LOGIC_SCOPE_MAX];
|
||||
unsigned mode[8]; /* 0 off, 1 logic, 2 PWM envelope */
|
||||
unsigned count, clock, divider, running, single, generation;
|
||||
double origin_s;
|
||||
char status[96];
|
||||
} LogicScope;
|
||||
|
||||
static inline unsigned logic_scope_channels(const LogicScope *s)
|
||||
{
|
||||
unsigned n=0;
|
||||
for (unsigned i=0;i<8;++i) n+=s->mode[i]!=0;
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Capture the requested interval with the finest representable sample period. */
|
||||
static inline int logic_scope_plan(double span, unsigned clock, unsigned depth,
|
||||
unsigned *count, unsigned *divider)
|
||||
{
|
||||
if (!isfinite(span) || span<=0 || !clock || depth<2 || depth>LOGIC_SCOPE_MAX) return 0;
|
||||
double ticks=ceil(span*clock/depth);
|
||||
ticks=fmax(1,fmin(65536,ticks));
|
||||
*divider=(unsigned)ticks-1;
|
||||
*count=(unsigned)fmax(2,fmin(depth,ceil(span*clock/ticks)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
typedef void (*LogicScopePoint)(void *context, double time, double value);
|
||||
/* Ignore clipped first/last cycles. A constant level has no measurable period. */
|
||||
static inline unsigned logic_scope_pwm(const LogicScope *s, unsigned ch,
|
||||
LogicScopePoint point, void *context)
|
||||
{
|
||||
if (ch>=8 || !s->clock || s->count<2) return 0;
|
||||
unsigned rise=0,fall=0,have_rise=0,have_fall=0,n=0;
|
||||
unsigned previous=(s->samples[0]>>ch)&1u;
|
||||
double dt=(s->divider+1.0)/s->clock;
|
||||
for (unsigned i=1;i<s->count;++i) {
|
||||
unsigned level=(s->samples[i]>>ch)&1u;
|
||||
if (level!=previous) {
|
||||
if (level) {
|
||||
if (have_rise && have_fall) {
|
||||
point(context,s->origin_s+(rise+(i-rise)*0.5)*dt,
|
||||
2.0*(fall-rise)/(i-rise)-1.0);
|
||||
++n;
|
||||
}
|
||||
rise=i; have_rise=1; have_fall=0;
|
||||
} else if (have_rise) { fall=i; have_fall=1; }
|
||||
}
|
||||
previous=level;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#ifndef SCOPE_DECODERS_H
|
||||
#define SCOPE_DECODERS_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#define SD_ITEMS 256
|
||||
#define SD_FIELDS 2048
|
||||
#define SD_RESULTS 4096
|
||||
typedef struct { char id[64],name[96],inputs[128],outputs[128]; int unavailable; } ScopeDecoder;
|
||||
typedef struct { int owner,channel,mandatory; char id[64],name[128],value[128],choices[512]; } ScopeDecoderField;
|
||||
typedef struct { uint64_t start,end; char decoder[64],row[96],text[256]; } ScopeAnnotation;
|
||||
typedef struct {
|
||||
ScopeDecoder items[SD_ITEMS]; ScopeDecoderField fields[SD_FIELDS]; ScopeAnnotation results[SD_RESULTS];
|
||||
unsigned order[SD_ITEMS],count,field_count,result_count; int open,view,page,selected,edit,request,busy;
|
||||
unsigned chain[8],chain_count,visible_fields[128],visible_count; double rate;
|
||||
char status[160],edit_text[128];
|
||||
} ScopeDecoders;
|
||||
static inline void sd_copy(char *to,size_t size,const char *from) { size_t n=strlen(from); if(n>=size)n=size-1; memcpy(to,from,n);to[n]=0; }
|
||||
static inline unsigned sd_split(char *line,char **out,unsigned cap) {
|
||||
unsigned n=0; char *p=line; while(n<cap) { out[n++]=p; char *end=strchr(p,'\t'); if(!end)break;*end=0;p=end+1; }
|
||||
if(n)out[n-1][strcspn(out[n-1],"\r\n")]=0; return n;
|
||||
}
|
||||
static inline int sd_find(const ScopeDecoders *s,const char *id) { for(unsigned i=0;i<s->count;++i)if(!strcmp(s->items[i].id,id))return (int)i;return -1; }
|
||||
static inline int sd_has(const char *list,const char *token) {
|
||||
size_t n=strlen(token);for(const char *p=list;*p;) {const char *e=strchr(p,'|');size_t k=e?(size_t)(e-p):strlen(p);if(k==n&&!strncmp(p,token,n))return 1;if(!e)break;p=e+1;}return 0;
|
||||
}
|
||||
static inline int sd_catalog(ScopeDecoders *s,const char *path) {
|
||||
FILE *f=fopen(path,"r");if(!f)return 0;s->count=s->field_count=0;char line[4096];
|
||||
while(fgets(line,sizeof(line),f)) {char *v[8];unsigned n=sd_split(line,v,8);
|
||||
if((!strcmp(v[0],"D")||!strcmp(v[0],"E"))&&n>=3&&s->count<SD_ITEMS) {
|
||||
ScopeDecoder *d=&s->items[s->count++];memset(d,0,sizeof(*d));sd_copy(d->id,sizeof(d->id),v[1]);sd_copy(d->name,sizeof(d->name),v[2]);d->unavailable=v[0][0]=='E';
|
||||
if(n>=5){sd_copy(d->inputs,sizeof(d->inputs),v[3]);sd_copy(d->outputs,sizeof(d->outputs),v[4]);}
|
||||
} else if((v[0][0]=='C'||v[0][0]=='O')&&n>=5&&s->field_count<SD_FIELDS) {
|
||||
int owner=sd_find(s,v[1]);if(owner<0)continue;ScopeDecoderField *d=&s->fields[s->field_count++];memset(d,0,sizeof(*d));d->owner=owner;d->channel=v[0][0]=='C';
|
||||
sd_copy(d->id,sizeof(d->id),v[2]);sd_copy(d->name,sizeof(d->name),v[3]);d->mandatory=d->channel&&atoi(v[4]);
|
||||
sd_copy(d->value,sizeof(d->value),d->channel?"-1":n>5?v[5]:"");if(n>6)sd_copy(d->choices,sizeof(d->choices),v[6]);
|
||||
}
|
||||
}fclose(f);for(unsigned i=0;i<s->count;++i){unsigned j=i;s->order[i]=i;while(j>0&&strcmp(s->items[s->order[j-1]].id,s->items[i].id)>0){s->order[j]=s->order[j-1];--j;}s->order[j]=i;}snprintf(s->status,sizeof(s->status),"%u DSView decoders / D0-D7 inputs",s->count);return s->count!=0;
|
||||
}
|
||||
static inline int sd_resolve(ScopeDecoders *s,unsigned id,unsigned depth) {
|
||||
if(depth>=8||id>=s->count||s->items[id].unavailable)return 0;
|
||||
ScopeDecoder *d=&s->items[id];if(sd_has(d->inputs,"logic")){s->chain[s->chain_count++]=id;return 1;}
|
||||
char inputs[128];sd_copy(inputs,sizeof(inputs),d->inputs);char *p=inputs;
|
||||
while(*p) {char *end=strchr(p,'|');if(end)*end=0;int base=sd_find(s,p);
|
||||
if(base>=0&&(unsigned)base!=id&&sd_resolve(s,(unsigned)base,depth+1)){s->chain[s->chain_count++]=id;return 1;}
|
||||
for(int direct=1;direct>=0;--direct)for(unsigned i=0;i<s->count;++i)if(i!=id&&sd_has(s->items[i].inputs,"logic")==direct&&sd_has(s->items[i].outputs,p)&&sd_resolve(s,i,depth+1)){s->chain[s->chain_count++]=id;return 1;}
|
||||
if(!end)break;p=end+1;
|
||||
}return 0;
|
||||
}
|
||||
static inline void sd_select(ScopeDecoders *s,unsigned id) {
|
||||
s->selected=(int)id;s->page=0;s->view=1;s->edit=-1;s->chain_count=s->visible_count=0;
|
||||
if(!sd_resolve(s,id,0)){sd_copy(s->status,sizeof(s->status),"No supported logic input stack / import failed");return;}
|
||||
unsigned next=0;
|
||||
for(unsigned c=0;c<s->chain_count;++c)for(unsigned f=0;f<s->field_count;++f)if(s->fields[f].owner==(int)s->chain[c]&&s->visible_count<128){
|
||||
s->visible_fields[s->visible_count++]=f;ScopeDecoderField *field=&s->fields[f];
|
||||
if(field->channel&&field->mandatory&&atoi(field->value)<0&&next<8)snprintf(field->value,sizeof(field->value),"%u",next++);
|
||||
}
|
||||
sd_copy(s->status,sizeof(s->status),"Tap a field to change. DECODE uses the last digital snapshot.");
|
||||
}
|
||||
static inline int sd_config(const ScopeDecoders *s,const char *path) {
|
||||
FILE *f=fopen(path,"w");if(!f)return 0;
|
||||
for(unsigned c=0;c<s->chain_count;++c){unsigned id=s->chain[c];fprintf(f,"[%s]\n",s->items[id].id);
|
||||
for(unsigned i=0;i<s->field_count;++i){const ScopeDecoderField *d=&s->fields[i];if(d->owner==(int)id)fprintf(f,"%s.%s=%s\n",d->channel?"ch":"opt",d->id,d->value);}}
|
||||
int error=ferror(f);return fclose(f)==0&&!error;
|
||||
}
|
||||
static inline int sd_results(ScopeDecoders *s,const char *path) {
|
||||
FILE *f=fopen(path,"r");if(!f)return 0;char line[2048];s->result_count=0;sd_copy(s->status,sizeof(s->status),"No annotations / check channel mapping and sample rate");
|
||||
while(fgets(line,sizeof(line),f)){char *v[7];unsigned n=sd_split(line,v,7);
|
||||
if(n>=6&&!strcmp(v[0],"A")&&s->result_count<SD_RESULTS){ScopeAnnotation *a=&s->results[s->result_count++];a->start=strtoull(v[1],NULL,10);a->end=strtoull(v[2],NULL,10);sd_copy(a->decoder,sizeof(a->decoder),v[3]);sd_copy(a->row,sizeof(a->row),v[4]);sd_copy(a->text,sizeof(a->text),v[5]);}
|
||||
else if(n>=2&&!strcmp(v[0],"ERROR"))sd_copy(s->status,sizeof(s->status),v[1]);
|
||||
else if(n>=3&&!strcmp(v[0],"DONE")&&s->result_count)snprintf(s->status,sizeof(s->status),"%u annotations / %s",s->result_count,v[2]);
|
||||
}fclose(f);s->view=2;s->page=0;return 1;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user