-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpicopio_emu.h
More file actions
3248 lines (2790 loc) · 84.3 KB
/
Copy pathpicopio_emu.h
File metadata and controls
3248 lines (2790 loc) · 84.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////////////////
// ADAPTED FROM: https://github.com/sumio-morioka/rpipico_simple_PIO_emulator/picopio_emu.h
/////////////////////////////////////////////////////////////////////////////////////////
#ifndef PICOPIO_EMU
#define PICOPIO_EMU
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
////////////////////////////
// emulator API
////////////////////////////
extern void pio_code_start(
const char *funcname,
int sm_id, // state machine ID (for IRQ rel)
int in_pins,
int in_num,
int out_pins,
int out_num,
int sideset_pins,
int sideset_num,
bool sideset_opt,
bool isr_shift_right, // SHIFTCTRL_IN_SHITDIR
bool isr_autopush,
int isr_autopush_threshold, // SHIFTCTRL_PUSH_THRESH
bool osr_shift_right, // SHIFTCTRL_OUT_SHIFTDIR
bool osr_autopull,
int osr_autopull_threshold, // SHIFTCTRL_PULL_THRESH
int jmp_pin, // EXECCTRL_JMP_PIN
bool mov_status_sel, // EXECCTRL_STATUS_SEL
int mov_status_val // EXECCTRL_STATUS_SEL
);
extern void pio_code_start_simple(
const char *funcname,
int sm_id, // state machine ID (will be used for IRQ rel)
int in_pins,
int in_num,
int out_pins,
int out_num,
int set_pins,
int set_num,
int sideset_pins,
int sideset_num,
bool sideset_opt
);
extern void pio_code_end(bool write_code, const char *file_name_code);
extern void pio_run_emulation(int cycles, FILE *file_in, FILE *file_out, FILE *file_trace, FILE *file_asm);
extern void pio_begin_emulation(FILE *file_in, FILE *file_out, FILE *file_trace, FILE *file_asm);
extern void pio_step_emulation(int cycles);
extern void pio_end_emulation();
////////////////////////////
// instructions
////////////////////////////
extern void pio_jmp(int cond, char *lbl, int sideset, int delay);
extern void pio_wait(bool polarity, int src, int index, bool rel, int sideset, int delay);
extern void pio_in(int src, int bitcount, int sideset, int delay);
extern void pio_out(int dest, int bitcount, int sideset, int delay);
extern void pio_push(bool iffull, bool block, int sideset, int delay);
extern void pio_pull(bool ifempty, bool block, int sideset, int delay);
extern void pio_mov(int dest, int op, int src, int sideset, int delay);
extern void pio_irq(bool clr, bool wait, int index, bool rel, int sideset, int delay);
extern void pio_set(int dest, int data, int sideset, int delay);
// (pseudo instructions)
extern void pio_comment(char *string); // max len = MAX_COMMENT_LEN
extern void pio_label(char *lbl);
extern void pio_nop(int sideset, int delay);
extern void pio_wrap_target(void);
extern void pio_wrap(void);
extern void pio_origin(int addr);
////////////////////////////
// constants
////////////////////////////
#define PIO_UNUSE -1
// instructions
#define PIO_INST_IN 0
#define PIO_INST_IRQ 1
#define PIO_INST_JMP 2
#define PIO_INST_MOV 3
#define PIO_INST_OUT 4
#define PIO_INST_PULL 5
#define PIO_INST_PUSH 6
#define PIO_INST_SET 7
#define PIO_INST_WAIT 8
#define PIO_INST_COMMENT 9
#define PIO_INST_LABEL 10
#define PIO_INST_NOP 11
#define PIO_INST_WRAP_TARGET 12
#define PIO_INST_WRAP 13
#define PIO_INST_ORIGIN 14
// operands (in, out, mov)
#define PIO_PINS 100
#define PIO_X 101
#define PIO_Y 102
#define PIO_NULL 103
#define PIO_ISR 104
#define PIO_OSR 105
#define PIO_PC 106
#define PIO_EXEC 107
#define PIO_PINDIRS 108
#define PIO_STATUS 109
#define PIO_NONE 200
#define PIO_INVERT 201
#define PIO_BIT_REVERSE 202
// operands (jmp)
#define PIO_ALWAYS 300
#define PIO_X_EQ_0 301
#define PIO_X_NEQ_0_DEC 302
#define PIO_Y_EQ_0 303
#define PIO_Y_NEQ_0_DEC 304
#define PIO_X_NEQ_Y 305
#define PIO_PIN 306
#define PIO_OSRE_NOTEMPTY 307
// operands (wait)
#define PIO_GPIO 400
//#define PIO_PIN 401 // defined above
#define PIO_IRQ 402
// oprands (push, pull)
#define PIO_BLOCK 500
#define PIO_NOBLOCK 501
////////////////////////////
// other parameters
////////////////////////////
#define PIO_MAX_FUNCNAME_LEN 256
#define PIO_MAX_ASMLINE_LEN 1024
#define PIO_MAX_ASMLINE_NUM 1024
#define PIO_MAX_CSVLINE_LEN 1024
#define PIO_MAX_INST_NUM 32 // don't change
/////////////////////////////////////////////////////////////////////////////////////////
// type definitions & global variables
/////////////////////////////////////////////////////////////////////////////////////////
typedef struct _pio_inst_list {
bool used;
int code_line;
uint8_t addr; // 0-31
int inst_type;
int operand1;
int operand2;
int operand3;
int operand4;
int operand5;
int sideset;
int delay;
char strbuf[PIO_MAX_ASMLINE_LEN + 1]; // label or comment
char asmbuf[PIO_MAX_ASMLINE_LEN + 1]; // assembler output
} _PIO_INST;
typedef struct _pio_csvin_info {
int cycles;
uint32_t gpio_i;
uint8_t irq_i;
bool txfifo_push;
uint32_t txfifo_val;
bool rxfifo_pop;
} _PIO_CSVIN;
typedef struct _pio_global_info {
// func name
char func_name[PIO_MAX_FUNCNAME_LEN];
int sm_id;
// instruction
_PIO_INST asmline[PIO_MAX_ASMLINE_NUM];
int asmline_ptr;
int origin;
int inst_ptr;
// CPU registers
int delay_timer;
uint32_t x;
uint32_t y;
uint32_t isr;
int isr_bitctr;
uint32_t osr;
int osr_bitctr;
uint32_t txfifo[4];
int txfifo_num;
uint32_t rxfifo[4];
int rxfifo_num;
uint32_t gpio_i;
uint32_t gpio_o;
uint32_t sideset_val;
uint8_t irq_i;
uint8_t irq_o;
bool irq_stalling;
// emulator info (configuration values)
// CPU I/O
int cycles;
int in_pins;
int in_num;
int out_pins;
int out_num;
int set_pins;
int set_num;
int sideset_pins;
int sideset_num;
bool sideset_opt;
int sideset_maxval;
int delay_maxval;
uint32_t in_pin_setmask;
uint32_t out_pin_setmask;
uint32_t set_pin_setmask;
uint32_t sideset_pin_setmask;
uint32_t jmp_pin_setmask;
uint32_t in_pin_clrmask;
uint32_t out_pin_clrmask;
uint32_t set_pin_clrmask;
uint32_t sideset_pin_clrmask;
bool isr_shift_right;
bool isr_autopush;
int isr_autopush_threshold;
bool osr_shift_right;
bool osr_autopull;
int osr_autopull_threshold;
int jmp_pin;
bool mov_status_sel;
int mov_status_val;
int wrap_target_line;
int wrap_line;
FILE *fin;
FILE *fout;
FILE *fcode;
FILE *ftrace;
FILE *fasm;
char csvin_cache[PIO_MAX_CSVLINE_LEN];
bool csvin_cache_valid;
_PIO_CSVIN csvin_info;
} _PIO_INFO;
// global
static _PIO_INFO _pio_info_g;
#define PIO_SIMSTATE_CODE_START 0
#define PIO_SIMSTATE_CODE_END 1
#define PIO_SIMSTATE_RUN_START 2
#define PIO_SIMSTATE_RUN_END 3
static int _pio_emu_state_g = PIO_SIMSTATE_CODE_START;
/////////////////////////////////////////////////////////////////////////////////////////
// function body
/////////////////////////////////////////////////////////////////////////////////////////
static uint32_t pio_maxval(unsigned int bitnum)
{
// return (2^bitnum)-1
uint32_t mask;
unsigned int i;
for (i = 0, mask = 0; i <= 32; i++) {
if (i == bitnum)
return mask;
mask = (mask << 1) | 1;
}
return mask;
}
static unsigned int pio_bitnum(uint32_t val)
{
uint32_t mask;
unsigned int i;
for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
if (val < mask)
return i;
}
return 32;
}
static uint32_t _pio_bitreverse(uint32_t val)
{
uint32_t ret;
unsigned int i;
for (i = 0, ret = 0; i < 32; i++) {
ret <<= 1;
if ((val & 1) != 0) {
ret |= 1;
}
val >>= 1;
}
return ret;
}
void pio_code_start(
const char *funcname,
int sm_id, // state machine ID (will be used for IRQ rel)
int in_pins,
int in_num,
int out_pins,
int out_num,
int set_pins,
int set_num,
int sideset_pins,
int sideset_num,
bool sideset_opt,
bool isr_shift_right, // SHIFTCTRL_IN_SHITDIR
bool isr_autopush,
int isr_autopush_threshold, // SHIFTCTRL_PUSH_THRESH
bool osr_shift_right, // SHIFTCTRL_OUT_SHIFTDIR
bool osr_autopull,
int osr_autopull_threshold, // SHIFTCTRL_PULL_THRESH
int jmp_pin, // EXECCTRL_JMP_PIN
bool mov_status_sel, // EXECCTRL_STATUS_SEL
int mov_status_val // EXECCTRL_STATUS_SEL
)
{
int i;
int delay_bitnum;
if (_pio_emu_state_g != PIO_SIMSTATE_CODE_START) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal code-start position.\n");
exit(1);
}
_pio_emu_state_g = PIO_SIMSTATE_CODE_END;
// initialize global info
strcpy(_pio_info_g.func_name, funcname); // NOTE: init value
// state machine ID (for IRQ rel)
if (sm_id < 0 || sm_id > 3) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal state machine id (0-3).\n");
exit(1);
}
_pio_info_g.sm_id = sm_id;
// init internal info
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *p;
p = &(_pio_info_g.asmline[i]);
p->used = false;
p->code_line = 0;
p->addr = 0;
p->inst_type = PIO_UNUSE;
p->operand1 = PIO_UNUSE;
p->operand2 = PIO_UNUSE;
p->operand3 = PIO_UNUSE;
p->operand4 = PIO_UNUSE;
p->operand5 = PIO_UNUSE;
p->sideset = PIO_UNUSE;
p->delay = PIO_UNUSE;
(p->strbuf)[0] = '\0';
(p->asmbuf)[0] = '\0';
}
_pio_info_g.asmline_ptr = 0;
_pio_info_g.origin = PIO_UNUSE;
_pio_info_g.inst_ptr = 0;
_pio_info_g.delay_timer = PIO_UNUSE;
_pio_info_g.x = 0;
_pio_info_g.y = 0;
_pio_info_g.isr = 0;
_pio_info_g.osr = 0;
for (i = 0; i < 4; i++) {
(_pio_info_g.txfifo)[i] = 0;
}
_pio_info_g.txfifo_num = 0;
for (i = 0; i < 4; i++) {
(_pio_info_g.rxfifo)[i] = 0;
}
_pio_info_g.rxfifo_num = 0;
_pio_info_g.in_pins = in_pins; // NOTE: init value
_pio_info_g.in_num = in_num; // NOTE: init value
_pio_info_g.out_pins = out_pins; // NOTE: init value
_pio_info_g.out_num = out_num; // NOTE: init value
_pio_info_g.set_pins = set_pins; // NOTE: init value
_pio_info_g.set_num = set_num; // NOTE: init value
_pio_info_g.sideset_pins = sideset_pins; // NOTE: init value
_pio_info_g.sideset_num = sideset_num; // NOTE: init value
_pio_info_g.sideset_opt = sideset_opt; // NOTE: init value
{
if (in_pins == PIO_UNUSE || in_num == PIO_UNUSE) {
in_pins = 0;
in_num = 0;
}
if (out_pins == PIO_UNUSE || out_num == PIO_UNUSE) {
out_pins = 0;
out_num = 0;
}
if (set_pins == PIO_UNUSE || set_num == PIO_UNUSE) {
set_pins = 0;
set_num = 0;
}
if (sideset_pins == PIO_UNUSE || sideset_num == PIO_UNUSE) {
sideset_pins = 0;
sideset_num = 0;
}
// check pin assign
if (in_pins < 0 || in_num < 0 || (in_pins + in_num) > 29) { // 29 ... GPIO 0-28
fprintf(stderr, "pio_code_start(): ERROR. Illegal number of input pins.\n");
exit(1);
}
if (out_pins < 0 || out_num < 0 || (out_pins + out_num) > 29) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal number of output pins.\n");
exit(1);
}
if (set_pins < 0 || set_num < 0 || (set_pins + set_num) > 29) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal number of set pins.\n");
exit(1);
}
if (sideset_pins < 0 || sideset_num < 0 || (sideset_pins + sideset_num) > 29) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal number of sideset pins.\n");
exit(1);
}
_pio_info_g.in_pin_setmask = (pio_maxval(in_num) << in_pins) & 0x1FFFFFFF; // 0x1FFFFFFF ... GPIO28-0
_pio_info_g.out_pin_setmask = (pio_maxval(out_num) << out_pins) & 0x1FFFFFFF;
_pio_info_g.set_pin_setmask = (pio_maxval(set_num) << set_pins) & 0x1FFFFFFF;
_pio_info_g.sideset_pin_setmask = (pio_maxval(sideset_num) << sideset_pins) & 0x1FFFFFFF;
_pio_info_g.in_pin_clrmask = 0xFFFFFFFF ^ _pio_info_g.in_pin_setmask; // (inversion)
_pio_info_g.out_pin_clrmask = 0xFFFFFFFF ^ _pio_info_g.out_pin_setmask;
_pio_info_g.set_pin_clrmask = 0xFFFFFFFF ^ _pio_info_g.set_pin_setmask;
_pio_info_g.sideset_pin_clrmask = 0xFFFFFFFF ^ _pio_info_g.sideset_pin_setmask;
//fprintf(stderr, "in mask %08x\n", _pio_info_g.in_pin_setmask);
//fprintf(stderr, "out mask %08x\n", _pio_info_g.out_pin_setmask);
//fprintf(stderr, "set mask %08x\n", _pio_info_g.set_pin_setmask);
//fprintf(stderr, "side mask %08x\n", _pio_info_g.sideset_pin_setmask);
// if ((_pio_info_g.in_pin_setmask & _pio_info_g.out_pin_setmask) != 0) {
// fprintf(stderr, "pio_code_start(): ERROR. Pin assignment overlap between input and output.\n");
// exit(1);
// }
// if ((_pio_info_g.in_pin_setmask & _pio_info_g.sideset_pin_setmask) != 0) {
// fprintf(stderr, "pio_code_start(): ERROR. Pin assignment overlap between input and sideset.\n");
// exit(1);
// }
// if ((_pio_info_g.out_pin_setmask & _pio_info_g.sideset_pin_setmask) != 0) {
// fprintf(stderr, "pio_code_start(): ERROR. Pin assignment overlap between output and sideet.\n");
// exit(1);
// }
// if ((_pio_info_g.set_pin_setmask & _pio_info_g.sideset_pin_setmask) != 0) {
// fprintf(stderr, "pio_code_start(): ERROR. Pin assignment overlap between set and sideet.\n");
// exit(1);
// }
}
_pio_info_g.gpio_i = 0;
_pio_info_g.gpio_o = 0;
_pio_info_g.sideset_val = 0;
_pio_info_g.irq_i = 0;
_pio_info_g.irq_o = 0;
_pio_info_g.irq_stalling = false;
_pio_info_g.cycles = 1;
_pio_info_g.isr_shift_right = isr_shift_right; // NOTE: default value
_pio_info_g.osr_shift_right = osr_shift_right; // NOTE: default value
if (isr_autopush_threshold < 0 || isr_autopush_threshold > 32) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal isr_autopush_threshold value.\n");
exit(1);
}
if (osr_autopull_threshold < 0 || osr_autopull_threshold > 32) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal osr_autopull_threshold value.\n");
exit(1);
}
_pio_info_g.isr_autopush = isr_autopush;
_pio_info_g.isr_autopush_threshold = isr_autopush_threshold; // NOTE: default value
_pio_info_g.osr_autopull = osr_autopull;
_pio_info_g.osr_autopull_threshold = osr_autopull_threshold; // NOTE: default value
_pio_info_g.isr_bitctr = 0; // empty
_pio_info_g.osr_bitctr = _pio_info_g.osr_autopull_threshold; // (empty)
{ // check # of sideset num
if (sideset_num > 5 || (sideset_opt == true && sideset_num > 4)) {
fprintf(stderr, "pio_code_start(): ERROR. Too many number of sideset signals.\n");
exit(1);
}
if (sideset_opt == false) {
delay_bitnum = 5 - sideset_num;
}
else {
delay_bitnum = 4 - sideset_num;
}
_pio_info_g.sideset_maxval = pio_maxval(sideset_num);
if (delay_bitnum >= 1)
_pio_info_g.delay_maxval = pio_maxval(delay_bitnum);
else
_pio_info_g.delay_maxval = 0;
}
_pio_info_g.jmp_pin = jmp_pin;
if (jmp_pin != PIO_UNUSE) { // check jmp pin
if (jmp_pin < 0 || jmp_pin > 29) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal jmp_pin assignment.\n");
exit(1);
}
_pio_info_g.jmp_pin_setmask = ((uint32_t)1 << jmp_pin) & 0x1FFFFFFF; // GPIO28-0
if ((_pio_info_g.jmp_pin_setmask & _pio_info_g.out_pin_setmask) != 0) {
fprintf(stderr, "pio_code_start(): ERROR. Jmp_pin is assigned to an output port.\n");
exit(1);
}
if ((_pio_info_g.jmp_pin_setmask & _pio_info_g.sideset_pin_setmask) != 0) {
fprintf(stderr, "pio_code_start(): ERROR. Jmp_pin is assigned to a sideset port.\n");
exit(1);
}
}
_pio_info_g.mov_status_sel = mov_status_sel; // true:TX, false:RX
if (mov_status_val < 1 || mov_status_val > 4) {
fprintf(stderr, "pio_code_start(): ERROR. Illegal mov_status_val (1-4).\n");
exit(1);
}
_pio_info_g.mov_status_val = mov_status_val;
_pio_info_g.wrap_target_line = PIO_UNUSE;
_pio_info_g.wrap_line = PIO_UNUSE;
_pio_info_g.fin = (FILE *)NULL;
_pio_info_g.fout = (FILE *)NULL;
_pio_info_g.fcode = (FILE *)NULL;
_pio_info_g.ftrace = (FILE *)NULL;
_pio_info_g.fasm = (FILE *)NULL;
_pio_info_g.csvin_cache[0] = '\0';
_pio_info_g.csvin_cache_valid = false;
{
_PIO_CSVIN *p;
p = &(_pio_info_g.csvin_info);
p->cycles = PIO_UNUSE;
p->gpio_i = 0;
p->txfifo_push = false;
p->txfifo_val = 0;
p->rxfifo_pop = false;
}
}
void pio_code_start_simple(
const char *funcname,
int sm_id, // state machine ID (will be used for IRQ rel)
int in_pins,
int in_num,
int out_pins,
int out_num,
int set_pins,
int set_num,
int sideset_pins,
int sideset_num,
bool sideset_opt
)
{
pio_code_start(
funcname,
sm_id,
in_pins,
in_num,
out_pins,
out_num,
set_pins,
set_num,
sideset_pins,
sideset_num,
sideset_opt,
false, // bool isr_shift_right, // SHIFTCTRL_IN_SHITDIR
false, // bool isr_autopush,
32, // int isr_autopush_threshold, // SHIFTCTRL_PUSH_THRESH
true, // bool osr_shift_right, // SHIFTCTRL_OUT_SHIFTDIR
false, // bool osr_autopull,
32, // int osr_autopull_threshold, // SHIFTCTRL_PULL_THRESH
PIO_UNUSE, // int jmp_pin, // EXECCTRL_JMP_PIN
true, // bool mov_status_sel, // EXECCTRL_STATUS_SEL
4 // int mov_status_val // EXECCTRL_STATUS_SEL
);
}
static bool pio_get_asmline_pc(int *asmline, int pc)
{
int i;
*asmline = PIO_UNUSE;
pc &= 31;
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *inst;
inst = &(_pio_info_g.asmline[i]);
if (inst->used == false)
return false;
if (inst->addr == pc) {
*asmline = inst->code_line; // IMPORTANT: return assember line
return true;
}
}
return false;
}
static bool pio_get_label_address(int *asmline, char *lbl)
{
int i;
*asmline = PIO_UNUSE;
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *inst;
inst = &(_pio_info_g.asmline[i]);
if (inst->used == false)
return false;
if (inst->inst_type != PIO_INST_LABEL)
{
char buf[10];
sprintf(buf, "@0x%02x", i);
if (!strcmp(lbl, buf)) {
*asmline = inst->code_line; // IMPORTANT: return assember line
return true;
}
continue;
}
if (!strcmp(lbl, inst->strbuf)) {
// *asmline = inst->addr; // BUG
*asmline = inst->code_line; // IMPORTANT: return assember line
return true;
}
}
return false;
}
static void pio_resolve_label_address(void)
{
int i, asmline;
// char tmpbuf[PIO_MAX_ASMLINE_LEN + 1];
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *inst;
inst = &(_pio_info_g.asmline[i]);
if (inst->used == false)
return;
if (inst->inst_type != PIO_INST_JMP)
continue;
if (pio_get_label_address(&asmline, inst->strbuf) == false) {
fprintf(stderr, "pio_resolve_label_address(): ERROR. Undefined label \"%s\" in step %d (\"%s\").\n", inst->strbuf, inst->code_line, inst->asmbuf);
exit(1);
}
else {
inst->operand2 = asmline; // IMPORTANT: assembler line
//fprintf(stderr, "resolve_label step %d jmp to step %d\n", inst->code_line, asmline);
// for debug
// sprintf(tmpbuf, "\t; to addr 0x%02x", addr);
// strcat(inst->asmbuf, tmpbuf);
}
}
}
static void pio_write_assembler_code(const char *file_name_code)
{
int i;
FILE *fp;
if ((_pio_info_g.fcode = fopen(file_name_code, "wt")) == (FILE *)NULL) {
fprintf(stderr, "pio_write_assembler_code(): ERROR. Cannot create output ASM file \"%s\".\n", file_name_code);
exit(1);
}
fp = _pio_info_g.fcode;
fprintf(fp, "; automatically generated by picopio_emu\n");
fprintf(fp, "\n");
// write header
fprintf(fp, ".program %s\n", _pio_info_g.func_name);
if (_pio_info_g.sideset_num > 0) {
fprintf(fp, ".side_set %d", _pio_info_g.sideset_num);
if (_pio_info_g.sideset_opt == true) {
fprintf(fp, " opt");
}
fprintf(fp, "\n\n");
}
// write each instruction
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *inst;
inst = &(_pio_info_g.asmline[i]);
if (inst->used == false)
break;
// write code
if (_pio_info_g.fasm)
fprintf(_pio_info_g.fasm, "%03d (pc=0x%02x):\t", inst->code_line, inst->addr);
if (inst->inst_type != PIO_INST_COMMENT
&& inst->inst_type != PIO_INST_LABEL
&& inst->inst_type != PIO_INST_WRAP_TARGET
&& inst->inst_type != PIO_INST_WRAP
&& inst->inst_type != PIO_INST_ORIGIN) {
fprintf(fp, "\t");
if (_pio_info_g.fasm)
fprintf(_pio_info_g.fasm, "\t");
}
fprintf(fp, "%s\t; line %03d\n", inst->asmbuf, inst->code_line);
if (_pio_info_g.fasm)
fprintf(_pio_info_g.fasm, "%s\n", inst->asmbuf);
}
// write C interface
fprintf(fp, "\n");
fprintf(fp, "%% c-sdk {\n");
fprintf(fp, "// see also (api): https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__pio.html\n");
fprintf(fp, "// see also (pio.h): https://raspberrypi.github.io/pico-sdk-doxygen/rp2__common_2hardware__pio_2include_2hardware_2pio_8h_source.html\n");
fprintf(fp, "// see also (sm): https://raspberrypi.github.io/pico-sdk-doxygen/group__sm__config.html\n");
fprintf(fp, "\n");
fprintf(fp, "void %s_config(\n", _pio_info_g.func_name);
fprintf(fp, " PIO pio,\n");
fprintf(fp, " uint sm,\n");
fprintf(fp, " float clkdiv\n");
fprintf(fp, ")\n");
fprintf(fp, "{\n");
fprintf(fp, " int i;\n");
fprintf(fp, " uint prog_addr;\n");
fprintf(fp, "\n");
fprintf(fp, " if (clkdiv < 1.0) {\n");
fprintf(fp, " printf(\"asm_exec(): too small clkdiv\\n\");\n");
fprintf(fp, " return;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " // load program to PIO\n");
fprintf(fp, " prog_addr = pio_add_program(pio, &%s_program);\n", _pio_info_g.func_name);
fprintf(fp, " pio_sm_config c = %s_program_get_default_config(prog_addr); // addr\n", _pio_info_g.func_name);
fprintf(fp, " sm_config_set_clkdiv(&c, clkdiv); // pio_sm_config *c, float div\n");
fprintf(fp, "\n");
fprintf(fp, " // input\n");
fprintf(fp, " if (%d > 0) {\n", _pio_info_g.in_num);
fprintf(fp, " for (i = 0; i < %d; i++) {\n", _pio_info_g.in_num);
fprintf(fp, " pio_gpio_init(pio, %d + i); // PIO pio, uint pin\n", _pio_info_g.in_pins);
// fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, %d, %d + i, 1, false); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.sm_id, _pio_info_g.in_pins);
fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, sm, %d + i, 1, false); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.in_pins);
fprintf(fp, " }\n");
fprintf(fp, " sm_config_set_in_pins(&c, %d); // pio_sm_config *c, uint in_base\n", _pio_info_g.in_pins);
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " // sideset\n");
fprintf(fp, " if (%d > 0) {\n", _pio_info_g.sideset_num);
fprintf(fp, " for (i = 0; i < %d; i++) {\n", _pio_info_g.sideset_num);
fprintf(fp, " pio_gpio_init(pio, %d + i); // PIO pio, uint pin\n", _pio_info_g.sideset_pins);
// fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, %d, %d + i, 1, true); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.sm_id, _pio_info_g.sideset_pins);
fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, sm, %d + i, 1, true); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.sideset_pins);
fprintf(fp, " }\n");
fprintf(fp, " sm_config_set_sideset_pins(&c, %d); // pio_sm_config *c, uint sideset_base\n", _pio_info_g.sideset_pins);
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " // output\n");
fprintf(fp, " if (%d > 0) {\n", _pio_info_g.out_num);
fprintf(fp, " for (i = 0; i < %d; i++) {\n", _pio_info_g.out_num);
fprintf(fp, " pio_gpio_init(pio, %d + i); // PIO pio, uint pin\n", _pio_info_g.out_pins);
// fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, %d, %d + i, 1, true); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.sm_id, _pio_info_g.out_pins);
fprintf(fp, " pio_sm_set_consecutive_pindirs(pio, sm, %d + i, 1, true); // PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out\n", _pio_info_g.out_pins);
fprintf(fp, " }\n");
fprintf(fp, " if (%d <= 5)", _pio_info_g.out_num);
fprintf(fp, " sm_config_set_set_pins(&c, %d, %d); // pio_sm_config *c, uint set_base, uint set_count\n", _pio_info_g.out_pins, _pio_info_g.out_num);
fprintf(fp, " else\n");
fprintf(fp, " sm_config_set_set_pins(&c, %d, 5); // pio_sm_config *c, uint set_base, uint set_count\n", _pio_info_g.out_pins);
fprintf(fp, " sm_config_set_out_pins(&c, %d, %d); // pio_sm_config *c, uint out_base, uint out_count\n", _pio_info_g.out_pins, _pio_info_g.out_num);
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " // fifo, isr and osr\n");
// fprintf(fp, " sm_config_set_in_shift(&c, false, false, 32); // shift right, auto-push, threshold\n");
fprintf(fp, " sm_config_set_in_shift(&c, ");
if (_pio_info_g.isr_shift_right == false)
fprintf(fp, "false, ");
else
fprintf(fp, "true, ");
if (_pio_info_g.isr_autopush == false)
fprintf(fp, "false, ");
else
fprintf(fp, "true, ");
fprintf(fp, "%d); // pio_sm_config *c, bool shift_right, bool autopush, uint push_threshold\n", _pio_info_g.isr_autopush_threshold);
fprintf(fp, " sm_config_set_out_shift(&c, ");
if (_pio_info_g.osr_shift_right == false)
fprintf(fp, "false, ");
else
fprintf(fp, "true, ");
if (_pio_info_g.osr_autopull == false)
fprintf(fp, "false, ");
else
fprintf(fp, "true, ");
fprintf(fp, "%d); // pio_sm_config *c, bool shift_right, bool autopull, uint pull_threshold\n", _pio_info_g.osr_autopull_threshold);
fprintf(fp, "\n");
if (_pio_info_g.jmp_pin != PIO_UNUSE) {
fprintf(fp, " // \"jmp pin\" instruction\n");
fprintf(fp, " sm_config_set_jmp_pin(&c, %d); // pio_sm_config *c, uint pin\n", _pio_info_g.jmp_pin);
fprintf(fp, "\n");
}
fprintf(fp, " // \"move status\" instruction\n");
fprintf(fp, " sm_config_set_mov_status(&c, ");
if (_pio_info_g.mov_status_sel == true)
fprintf(fp, "STATUS_TX_LESSTHAN, ");
else
fprintf(fp, "STATUS_RX_LESSTHAN, ");
fprintf(fp, "%d); // pio_sm_config *c, enum pio_mov_status_type status_sel, uint status_n\n", _pio_info_g.mov_status_val);
fprintf(fp, "\n");
// fprintf(fp, " pio_sm_clear_fifos(pio, %d); // PIO pio, uint sm\n", _pio_info_g.sm_id);
fprintf(fp, " pio_sm_clear_fifos(pio, sm); // PIO pio, uint sm\n");
// fprintf(fp, " pio_sm_init(pio, %d, prog_addr, &c); // PIO pio, uint sm, uint initial_pc, const pio_sm_config *config\n", _pio_info_g.sm_id);
fprintf(fp, " pio_sm_init(pio, sm, prog_addr, &c); // PIO pio, uint sm, uint initial_pc, const pio_sm_config *config\n");
// fprintf(fp, " pio_sm_set_enabled(pio, sm, true); // PIO pio, uint sm, bool enabled\n");
fprintf(fp, "}\n");
fprintf(fp, "%%}\n");
fclose(_pio_info_g.fcode);
}
void pio_code_end(bool write_code, const char *file_name_code)
{
if (_pio_emu_state_g != PIO_SIMSTATE_CODE_END) {
fprintf(stderr, "pio_code_end(): ERROR. Illegal code-end position.\n");
exit(1);
}
_pio_emu_state_g = PIO_SIMSTATE_RUN_START;
pio_resolve_label_address();
if (write_code == true) {
pio_write_assembler_code(file_name_code); // create output asm
}
}
static int pio_search_wrap_target(void)
{
int i;
for (i = 0; i < PIO_MAX_ASMLINE_NUM; i++) {
_PIO_INST *ap;
ap = &(_pio_info_g.asmline[i]);
if (ap->used == false)
break;
if (ap->inst_type == PIO_INST_WRAP_TARGET)
return i;
}
return 0; // not found: return to top of the list
}
static void pio_read_csv_input_header(void)
{
FILE *fp;
if ((fp = _pio_info_g.fin) == (FILE *)NULL)
return;
if (feof(fp))
return;
// cut 1st line of the csv
fgets(_pio_info_g.csvin_cache, PIO_MAX_CSVLINE_LEN - 1, fp);
_pio_info_g.csvin_cache_valid = false;
}
static void pio_shift_txfifo(void)
{
int i;
for (i = 0; i < 3; i++) {
_pio_info_g.txfifo[i] = _pio_info_g.txfifo[i + 1];
}
_pio_info_g.txfifo[3] = 0;
}
static void pio_shift_rxfifo(void)
{
int i;
for (i = 0; i < 3; i++) {
_pio_info_g.rxfifo[i] = _pio_info_g.rxfifo[i + 1];
}
_pio_info_g.rxfifo[3] = 0;
}
static bool pio_push_txfifo(uint32_t val)
{
switch (_pio_info_g.txfifo_num) {
case 0:
_pio_info_g.txfifo[0] = val;
_pio_info_g.txfifo_num = 1;
return true;
case 1:
_pio_info_g.txfifo[1] = val;
_pio_info_g.txfifo_num = 2;
return true;
case 2:
_pio_info_g.txfifo[2] = val;
_pio_info_g.txfifo_num = 3;
return true;
case 3:
_pio_info_g.txfifo[3] = val;
_pio_info_g.txfifo_num = 4;
return true;
default:
break;
}
return false;
}
static bool pio_pop_txfifo(uint32_t *val)
{
switch (_pio_info_g.txfifo_num) {
case 4:
case 3:
case 2:
case 1:
*val = _pio_info_g.txfifo[0];
(_pio_info_g.txfifo_num)--;
pio_shift_txfifo();
return true;
default:
break;
}
*val = 0;
return false;
}
static bool pio_push_rxfifo(uint32_t val)
{
switch (_pio_info_g.rxfifo_num) {
case 0:
_pio_info_g.rxfifo[0] = val;
_pio_info_g.rxfifo_num = 1;
return true;
case 1:
_pio_info_g.rxfifo[1] = val;
_pio_info_g.rxfifo_num = 2;
return true;
case 2:
_pio_info_g.rxfifo[2] = val;
_pio_info_g.rxfifo_num = 3;