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