ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcdcsSEQ_PRG.hpp
Go to the documentation of this file.
1
7#ifndef ngcdcsSEQ_PRG_H
8#define ngcdcsSEQ_PRG_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15#include <ngc/core/ngcbAddr.h>
17
22 int ticks; //< Ticks
23 int mticks; //< Megaticks
24
27 mticks = 0; ticks = 0;
28 }
29
31 ngcdcs_ticks_t(int mt, int t) {mticks = mt; ticks = t;}
32
34 void Reset() {mticks = 0; ticks = 0;}
35
38
40 void Add(int t) {
41 mticks += ((ticks + t) / 1000000);
42 ticks = (ticks + t) % 1000000;
43 }
44
46 void Sub(int t) {
47 if (ticks >= t)
48 {
49 ticks -= t;
50 }
51 else
52 {
53 ticks = 1000000 + ticks - t;
54 mticks--;
55 }
56 }
57
60 mticks += t.mticks + ((ticks + t.ticks) / 1000000);
61 ticks = (ticks + t.ticks) % 1000000;
62 }
63
66 mticks -= t.mticks;
67 if (ticks >= t.ticks)
68 {
69 ticks -= t.ticks;
70 }
71 else
72 {
73 ticks = 1000000 + ticks - t.ticks;
74 mticks--;
75 }
76 }
77
78 /*
79 * Check if zero
80 */
81 int EqZero() {
82 if ((ticks == 0) && (mticks == 0)) return (1);
83 else return (0);
84 }
85
86 /*
87 * Check if greater than zero
88 */
89 int GtZero() {
90 if (mticks > 0) return (1);
91 else if (mticks < 0) return (0);
92 else if (ticks >= 0) return (1);
93 else return (0);
94 }
95};
96
101 int addr; //< Start address (relative to RAM base)
102 int index; //< Reference index
103 int length; //< Subroutine code length
104 double time; //< Execution time in seconds
105 char name[256]; //< Subroutine name
106
109 addr = 0; index = 0; length = 0; time = 0.0;
110 memset(name, 0, sizeof(name));
111 }
112};
113
118public:
119 int code[ngcbSEQ_RAM_SIZE]; //< Sequencer program code
120 int length; //< Program code length
122 int numSubRt; //< Number of subroutines
123 int stored; //< Program is stored in controller
125 ngcdcs_seqp_t() {code[0] = 0x0; length = 0; numSubRt = 0; stored = 0;}
126};
127
132 int pos; //< Loop start position
133 int cnt; //< Loop counter
134 int infinite; //< Infinite sequence flag
135
137 ngcdcs_loop_t() {pos = 0; cnt = 0; infinite = 0;}
138};
139
144{
145public:
146 int code; //< Command code
147 int addr; //< Address field
148 int cnt; //< Counter field
149
151 ngcdcsSEQ_TOK() {code = 0x0; addr = 0; cnt = 0;}
152
154 explicit ngcdcsSEQ_TOK(int instr) {Instruction(instr);
155 }
156
158 void Instruction(int instr) {
159 code = (instr & ngcbSEQ_RAM_CODE) >> 28;
160
161 switch (code)
162 {
163 case ngcbSEQ_CODE_JSR:
164 {
165 addr = instr & ngcbSEQ_RAM_ADDR;
166 cnt = (instr & ngcbSEQ_RAM_CNT) >> 11;
167 break;
168 }
170 {
171 addr = 0;
172 cnt = (instr & ngcbSEQ_RAM_CNT) >> 11;
173 break;
174 }
176 {
177 addr = 0;
178 cnt = -1;
179 break;
180 }
182 {
183 addr = instr & ngcbSEQ_RAM_ADDR;
184 cnt = (instr & ngcbSEQ_RAM_CNT) >> 11;
185 break;
186 }
187 default:
188 {
189 addr = 0;
190 cnt = 1;
191 break;
192 }
193 }
194 }
195
196protected:
197private:
198};
199
204{
205public:
206 int id; //< Sequencer module reference id
207 int address; //< Actual adresss field
208 int counter; //< Actual counter field
209 int infiniteLoopRuns; //< Infinite loop replacement
210 int offset; //< Offset maintained by the application
211
214 id = -1; // not yet assigned
216 prg_ = &program_;
217 Init();
218 }
219
222 id = -1; // not yet assigned
224 prg_ = prg;
225 Init();
226 }
227
229 virtual ~ngcdcsSEQ_EMU() {}
230
232 ngcdcs_seqp_t *Program() {return prg_;}
233
235 void Init() {
236 int i;
237 for (i=0;i<ngcbSEQ_MAX_SUBRT;i++)
238 {
239 returnPos_[i] = 0;
240 }
241 returnNr_ = 0; loopNr_ = 0; pos_ = 0;
242 error_ = 0; erms_[0] = '\0';
243 terminate_ = 0;
244 callBack_ = 0;
245 breakPoint_ = 0;
246 infinite_ = 0;
247 address = -1; counter = 0; offset = 0;
248 }
249
251 int Instr() {
252 if ((pos_ >= 0) && (pos_ < ngcbSEQ_RAM_SIZE))
253 {
254 return (prg_->code[pos_]);
255 }
256 else
257 {
258 sprintf(erms_, "invalid program address");
259 error_ = 1;
260 return (ngcbSEQ_CODE_EXIT);
261 }
262 }
263
265 int Step() {
266 ngcdcsSEQ_TOK token;
267
268 terminate_ = 0;
269 error_ = 0;
270 callBack_ = 0;
271 address = -1;
272 counter = 0;
273 while (!terminate_ && !error_ && !callBack_)
274 {
275 token.Instruction(Instr());
276
277 switch (token.code)
278 {
281 {
282 OpenLoop(token.cnt);
283 break;
284 }
286 {
287 CloseLoop();
288 break;
289 }
290 case ngcbSEQ_CODE_JSR:
291 {
292 Jsr(token.addr);
293 break;
294 }
296 {
297 Return();
298 break;
299 }
301 {
302 if (loopNr_ > 0)
303 {
304 sprintf(erms_, "error parsing loop (loop not closed)");
305 error_ = 1;
306 }
307 else
308 {
309 terminate_ = 1;
310 }
311 break;
312 }
314 {
315 if ((token.addr < 0) || (token.addr >= ngcbSEQ_PATRAM_SIZE))
316 {
317 sprintf(erms_, "invalid pattern address");
318 error_ = 1;
319 break;;
320 }
321
322 if (token.cnt < 0)
323 {
324 sprintf(erms_, "error parsing loop (negative rep. factor)");
325 error_ = 1;
326 break;
327 }
328
329 if (token.cnt == 0)
330 {
331 Next();
332 break;
333 }
334
335 address = token.addr;
336 counter = token.cnt;
337 callBack_ = 1;
338 break;
339 }
340 default:
341 {
342 /*
343 * We ignore all other tokens
344 */
345 Next();
346 break;
347 }
348 }
349 }
350
351 return (terminate_);
352 }
353
355 void OpenLoop(int cnt) {
356 if ((cnt < -1) || (cnt == 0))
357 {
358 sprintf(erms_, "error parsing loop (invalid loop counter %d)", cnt);
359 error_ = 1;
360 return;
361 }
362
363 if (loopNr_ >= ngcbSEQ_MAX_OPEN_LOOPS)
364 {
365 sprintf(erms_, "error parsing loop (too many open loops)");
366 error_ = 1;
367 return;
368 }
369
370 if (cnt == -1)
371 {
372 loop_[loopNr_].infinite = 1;
373 breakPoint_ = 0;
374 if (infiniteLoopRuns > 0) cnt = infiniteLoopRuns;
375 }
376 else
377 {
378 loop_[loopNr_].infinite = 0;
379 }
380
381 loop_[loopNr_].cnt = cnt;
382 loop_[loopNr_++].pos = pos_ + 1;
383
384 Next();
385 }
386
388 int Infinite() {return (infinite_);
389 }
390
392 void BreakPoint() {breakPoint_ = 1;}
393
395 void CloseLoop() {
396 int n = loopNr_ - 1;
397
398 if (n < 0)
399 {
400 sprintf(erms_, "error parsing loop (skip over loop end)");
401 error_ = 1;
402 return;
403 }
404
405 if (loop_[n].infinite && !breakPoint_) infinite_ = 1;
406 if (loop_[n].cnt >= 0) loop_[n].cnt--;
407 if (loop_[n].cnt == 0)
408 {
409 /*
410 * One less open loop
411 */
412 loopNr_--;
413
414 if (loop_[n].infinite)
415 {
416 /*
417 * We can exit as the following code would
418 * never be executed
419 */
420 terminate_ = 1;
421 }
422
423 /*
424 * We can continue with the next code position
425 */
426 Next();
427 }
428 else
429 {
430 /*
431 * Jump back to loop body start position
432 */
433 Goto(loop_[n].pos);
434 }
435 }
436
438 void Jsr(int addr){
439 if ((addr < 0) || (addr >= ngcbSEQ_RAM_SIZE))
440 {
441 sprintf(erms_,
442 "error parsing loop (invalid subroutine address 0x%04x)",
443 addr);
444 error_ = 1;
445 return;
446 }
447
448 if (returnNr_ >= ngcbSEQ_MAX_SUBRT)
449 {
450 sprintf(erms_,
451 "error parsing loop (too many nested subroutines)");
452 error_ = 1;
453 return;
454 }
455
456 /*
457 * Store address where we have to return
458 */
459 returnPos_[returnNr_++] = pos_ + 1;
460
461 /*
462 * Jump to sub-routine address
463 */
464 Goto(addr);
465 }
466
468 void Return() {
469 if (returnNr_ <= 0)
470 {
471 if (loopNr_ > 0)
472 {
473 sprintf(erms_, "error parsing loop (loop not closed)");
474 error_ = 1;
475 return;
476 }
477
478 /*
479 * We can exit
480 */
481 terminate_ = 1;
482 }
483 else
484 {
485 /*
486 * Go back to address from where the routine has been called
487 */
488 Goto(returnPos_[--returnNr_]);
489 }
490 }
491
493 void Next() {pos_++;}
494
496 void Goto(int newPos) {pos_ = newPos;}
497
499 int Error() {return (error_);}
500
502 char *ErrMsg() {return (erms_);}
503
504protected:
505
506private:
508 int returnPos_[ngcbSEQ_MAX_SUBRT];
509
511 int returnNr_;
512
515
517 int loopNr_;
518
520 int pos_;
521
523 char erms_[256];
524
526 int error_;
527
529 int terminate_;
530
532 int callBack_;
533
535 int breakPoint_;
536
538 int infinite_;
539
541 ngcdcs_seqp_t *prg_;
542
544 ngcdcs_seqp_t program_;
545};
546
547#endif
void Return()
Return from subroutine.
Definition ngcdcsSEQ_PRG.hpp:468
ngcdcsSEQ_EMU()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:213
virtual ~ngcdcsSEQ_EMU()
Destructor.
Definition ngcdcsSEQ_PRG.hpp:229
void OpenLoop(int cnt)
One more open loop.
Definition ngcdcsSEQ_PRG.hpp:355
int id
Definition ngcdcsSEQ_PRG.hpp:206
int counter
Definition ngcdcsSEQ_PRG.hpp:208
void Next()
Goto next code position.
Definition ngcdcsSEQ_PRG.hpp:493
void CloseLoop()
Close loop.
Definition ngcdcsSEQ_PRG.hpp:395
void Goto(int newPos)
Goto code position.
Definition ngcdcsSEQ_PRG.hpp:496
int offset
Definition ngcdcsSEQ_PRG.hpp:210
int Instr()
Get actual instruction.
Definition ngcdcsSEQ_PRG.hpp:251
int Infinite()
Infinite sequence.
Definition ngcdcsSEQ_PRG.hpp:388
int address
Definition ngcdcsSEQ_PRG.hpp:207
void Jsr(int addr)
Jump to subroutine.
Definition ngcdcsSEQ_PRG.hpp:438
int Error()
Check if an error occured.
Definition ngcdcsSEQ_PRG.hpp:499
int infiniteLoopRuns
Definition ngcdcsSEQ_PRG.hpp:209
void BreakPoint()
Breakpoint reached.
Definition ngcdcsSEQ_PRG.hpp:392
void Init()
Initialize the emulation and the generic offset.
Definition ngcdcsSEQ_PRG.hpp:235
char * ErrMsg()
Return pointer to actual error message.
Definition ngcdcsSEQ_PRG.hpp:502
ngcdcs_seqp_t * Program()
Return loaded program.
Definition ngcdcsSEQ_PRG.hpp:232
int Step()
Step forward to next pattern execution.
Definition ngcdcsSEQ_PRG.hpp:265
ngcdcsSEQ_EMU(ngcdcs_seqp_t *prg)
Constructor with program.
Definition ngcdcsSEQ_PRG.hpp:221
Definition ngcdcsSEQ_PRG.hpp:144
ngcdcsSEQ_TOK(int instr)
Constructor with instruction to load.
Definition ngcdcsSEQ_PRG.hpp:154
int code
Definition ngcdcsSEQ_PRG.hpp:146
int cnt
Definition ngcdcsSEQ_PRG.hpp:148
void Instruction(int instr)
Load instruction.
Definition ngcdcsSEQ_PRG.hpp:158
ngcdcsSEQ_TOK()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:151
int addr
Definition ngcdcsSEQ_PRG.hpp:147
#define ngcbSEQ_MAX_SUBRT
Definition ngcbAddr.h:309
#define ngcbSEQ_RAM_CODE
Definition ngcbAddr.h:272
#define ngcbSEQ_CODE_EXIT
Definition ngcbAddr.h:304
#define ngcbSEQ_RAM_SIZE
Definition ngcbAddr.h:277
#define ngcbSEQ_PATRAM_SIZE
Definition ngcbAddr.h:282
#define ngcbSEQ_RAM_CNT
Definition ngcbAddr.h:271
#define ngcbSEQ_CODE_LOOP_INF
Definition ngcbAddr.h:301
#define ngcbSEQ_RAM_ADDR
Definition ngcbAddr.h:270
#define ngcbSEQ_CODE_RETURN
Definition ngcbAddr.h:303
#define ngcbSEQ_CODE_LOOP
Definition ngcbAddr.h:299
#define ngcbSEQ_CODE_EXEC
Definition ngcbAddr.h:298
#define ngcbSEQ_CODE_LOOP_END
Definition ngcbAddr.h:300
#define ngcbSEQ_MAX_OPEN_LOOPS
Definition ngcbAddr.h:314
#define ngcbSEQ_CODE_JSR
Definition ngcbAddr.h:302
Definition ngcdcsSEQ_PRG.hpp:131
int pos
Definition ngcdcsSEQ_PRG.hpp:132
int cnt
Definition ngcdcsSEQ_PRG.hpp:133
int infinite
Definition ngcdcsSEQ_PRG.hpp:134
ngcdcs_loop_t()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:137
Definition ngcdcsSEQ_PRG.hpp:117
int code[ngcbSEQ_RAM_SIZE]
Definition ngcdcsSEQ_PRG.hpp:119
int numSubRt
Definition ngcdcsSEQ_PRG.hpp:122
ngcdcs_seqp_t()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:125
int length
Definition ngcdcsSEQ_PRG.hpp:120
int stored
Definition ngcdcsSEQ_PRG.hpp:123
ngcdcs_subrt_t subRt[ngcbSEQ_MAX_SUBRT]
Definition ngcdcsSEQ_PRG.hpp:121
Definition ngcdcsSEQ_PRG.hpp:100
ngcdcs_subrt_t()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:108
double time
Definition ngcdcsSEQ_PRG.hpp:104
int addr
Definition ngcdcsSEQ_PRG.hpp:101
int index
Definition ngcdcsSEQ_PRG.hpp:102
int length
Definition ngcdcsSEQ_PRG.hpp:103
char name[256]
Definition ngcdcsSEQ_PRG.hpp:105
void Add(ngcdcs_ticks_t t)
Add ticks + megaticks.
Definition ngcdcsSEQ_PRG.hpp:59
ngcdcs_ticks_t()
Constructor.
Definition ngcdcsSEQ_PRG.hpp:26
ngcdcs_ticks_t(int mt, int t)
Constructor specifying megaticks and ticks.
Definition ngcdcsSEQ_PRG.hpp:31
int EqZero()
Definition ngcdcsSEQ_PRG.hpp:81
int mticks
Definition ngcdcsSEQ_PRG.hpp:23
void Sub(int t)
Subtract tick.
Definition ngcdcsSEQ_PRG.hpp:46
void Sub(ngcdcs_ticks_t t)
Subtracct ticks + megaticks.
Definition ngcdcsSEQ_PRG.hpp:65
void Set(ngcdcs_ticks_t t)
Set ticks + megaticks.
Definition ngcdcsSEQ_PRG.hpp:37
void Add(int t)
Add tick.
Definition ngcdcsSEQ_PRG.hpp:40
void Reset()
Reset ticks.
Definition ngcdcsSEQ_PRG.hpp:34
int GtZero()
Definition ngcdcsSEQ_PRG.hpp:89
int ticks
Definition ngcdcsSEQ_PRG.hpp:22