-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyCPU.py
1533 lines (1265 loc) · 62.9 KB
/
pyCPU.py
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
#####################################
# File: pyCPU.py
# Author: Norbert Feurle
# start Date: 2.2.2012
# latest Edit: 10.2.2013
#
# Description: Python Hardware Processor
# This Code implements a Hardware CPU in myhdl. The code for the CPU is drictly written in python
# and since the hardware description is also in python, the programmcode is automatically loaded into the design
# The Simulation of the cpu code on the hardware can be done in python only (only python needed! and the myhdl python library).
# The bytecode of python is executed in the CPU with normaly one instruction per cycle
# You can simply instantiate more cores on an FPGA with different Programmcode to make them run in parallel
#
# License: Pro life licence.
# You are not allowed to use this code for any purpose which is in any kind disrespectfully to life and/or freedom
# In such a case it remains my property
# for example: You are not allowed to build weapons, or large-scale animal husbandry automation equipment or anything
# that is used to harm, destroy or is used to frighten living beeings at an unacceptable rate, or build hardware
# with it where the workers dont get paid appropriate, or any process where the waste issue isnt solved in a sustainable way,
# etc..
# If you use it respectfully you are allowed to use it for free and are welcome to contribute with your changes/extensions
# You also have to keep this licence.
#################################################################################################
########################################################################################
#### Limitations:
#### no lists, no dicts no tuples, no float, no complex numbers, etc.
#### no classmembers,no multiplication, no exeptions, no for x in xxx:, etc.
########################################################################################
### What is supported:
#---------------------------------------------------
### ints (with x bits), no other python types only plane ints
### if and while (comparisions with <,>, >=, <=,!=,== )
### nested while loops up to CONST_LOOP_DEPTH (Constant Default value = 4)
### assignments to globals or local varibles from const, globals or define local variables
### Operators: +. -, ^ ,~, |, & ,<<,>>
### Simple Digital IOs over PORTS
### Function calls (with consts,and globals, or vars as argument, one intager as return value), no default arguments, no keyword arguments,no other functions as arguments
### up to MAX_NUMBER_FUNCTION_ARGUMENTS (Constant Default value = 10) arguments
### up to CONST_PC_STACK_DEPTH (Constant Default value=5) number of nested function calls
### up to VAR_MEM_DEPTH (Constant Default value=20) local variables
########################################################################################
###################################################################
##### Bug Track / Upcomming Features list :
#---------------------------------------------
# (done) Binary operations remove doesnt removes the operands from stack
# (done but stack ram could be smaller) (but stack_mem is now very big, Solution -> add some cycles for rot_two, rot_three and rot_for and put stak into a RAM,maybe dualported ) dis.opmap['POP_BLOCK'] and dis.opmap['SETUP_LOOP'] clear stack
# (halve done) Global memory initilisation in myhdl
# (done)-> GLOBAL_CONTENT is set to (0,) in this case <- if there is no function exept CPU_main, toVHDL creates an error,probably because Global_mem is empty
# (done) hasattr(eval(currentfunction.func_code.co_names[actual_Argument]), '__call__') througs exeption if PORTA_IN, etc is note defined global (defining PREDEFINED_IO_ADDRESSES should be enough)
# TODO MakeProcessorBytecode must give Error if opcode is not supported
# TODO wheater make PORTS in a all positive range or all with -2**(x-1) to +2**(x-1) conversion from signed to unsigned
# (done) Functions need to be able to use vars from var_mem as arguments (Dual ported var_mem ram)
# TODO calling a function with an function as an argument would not work
# TODO ROT_FOUR is not yet supported, adding a extra cycles is needed
# TODO Test generated VHDL code, and on an FPGA, also cosimulation
# TODO More Programm tests, Clean UP code, for example: eliminate magic numbers,general cleanup
# (done) insert cycle after var_mem is read to make it possible to map it into ram, same for programm_code
# (done) make jumps to addresses possible which exeed the size of VAR_BITWIDTH, for example to make a usable processor with WORD_SZ=4 bit
# TODO generic addable multiplication support
# (done) put constant memory and global memory into one compined memory -> in global mem (saves some luts)
# TODO check when VAR_BITWIDTH is greater then Bitwidth(Prog_mem_SIZE), if everything works when VAR_BITWIDTH is not 8
########info:
# ROT_TWO after SETUP_LOOP would not work but is kind of usless because the new stack block is empty
##########################################
#################################################################
####### Nice to haves:
#-----------------------------
# TODO maybe support List of ints with single integer subscription
# TODO additional SPI,I2C,Timer,etc modules -> (done) RS232
# (done)-> (the complete programm can be loaded via rs232 into the cpu)
# call functions from extern which are not loaded at startup
#################################################################
import sys
import imp
import os
import math
import MakeProcessorBytecode
###### The File with the Prozessor code ######
if len(sys.argv)>1:
Mainfile=sys.argv[1]
else:
Mainfile="main.py"
### load module from a filename
DDk=imp.load_source(os.path.splitext(Mainfile)[0],os.path.expanduser(Mainfile))
### empty function definition (for type checking) ####
def kkkt():pass
##### create a dictionary from module Variables #########
MD=dict([(i,getattr(DDk,i)) for i in dir(DDk) if not i.startswith('_') ])
### assert that the Variable PYCPU_CONFIGS is defined correctly ####
assert MD.has_key("PYCPU_CONFIGS"), "Error, Variable PYCPU_CONFIGS must be defined"
assert type(MD["PYCPU_CONFIGS"]) is type({}), "Error, Variable PYCPU_CONFIGS must be a dictionary"
assert MD["PYCPU_CONFIGS"].has_key("PREDEFINED_IO_ADDRESSES") , "Error: PYCPU_CONFIGS has no PREDEFINED_IO_ADDRESSES defined"
assert MD["PYCPU_CONFIGS"].has_key("Prog_mem_SIZE") , "Error: PYCPU_CONFIGS has no Prog_mem_SIZE defined"
assert MD["PYCPU_CONFIGS"].has_key("Var_mem_SIZE") , "Error: PYCPU_CONFIGS has no Var_mem_SIZE defined"
assert MD["PYCPU_CONFIGS"].has_key("Globals_mem_SIZE") , "Error: PYCPU_CONFIGS has no Globals_mem_SIZE defined"
assert MD["PYCPU_CONFIGS"].has_key("STACK_SIZE") , "Error: PYCPU_CONFIGS has no STACK_SIZE defined"
assert MD["PYCPU_CONFIGS"].has_key("CPU_BITWIDTH") , "Error: PYCPU_CONFIGS has no CPU_BITWIDTH defined"
assert MD["PYCPU_CONFIGS"].has_key("CPU_RS232_BAUDRATE") , "Error: PYCPU_CONFIGS has no CPU_RS232_BAUDRATE defined"
assert MD["PYCPU_CONFIGS"].has_key("NR_FUNCTION_ARGUMENTS") , "Error: PYCPU_CONFIGS has no NR_FUNCTION_ARGUMENTS defined"
assert MD["PYCPU_CONFIGS"].has_key("NR_NESTED_LOOPS") ,"Error: PYCPU_CONFIGS has no NR_NESTED_LOOPS defined"
assert MD["PYCPU_CONFIGS"].has_key("CPU_CLK_FREQ"), "Error: Clk frequenzy not defined"
assert len(MD["PYCPU_CONFIGS"])==10, "Error , PYCPU_CONFIGS must contain all settings"
######All config-strings are loaded into this environment as a real variable
for i in MD["PYCPU_CONFIGS"]:
exec(i+"="+MD["PYCPU_CONFIGS"][i].__repr__())
GLOBAL_MEM_START=max(PREDEFINED_IO_ADDRESSES.values())+1
#assert CPU_RS232_BAUDRATE in serial.baudrate_constants.keys(), "Error, Baudrate is not supported"
Baudrate_const=CPU_RS232_BAUDRATE
### assert that the code in the file/module has a main function ####
assert MD.has_key("main"), "Error: Prozessor Code has no main function defined"
assert type(MD["main"])==type(kkkt), "Error: Prozessor Code has no main function defined"
#print Prog_mem_SIZE
### Make the data to be written into the pyCpu core ####
ARGUMENT_BITLENGTH=int(math.log(Prog_mem_SIZE-1,2)+1)
#print ARGUMENT_BITLENGTH
ProcessorCodeObject=MakeProcessorBytecode.MakeBytecode(MD["main"],PREDEFINED_IO_ADDRESSES,MD,ARGUMENT_BITLENGTH)
THE_FUNCTION_ADRESSES_START=ProcessorCodeObject.GLOBAL_FUNCTION_ADRESSES_START
GLOBALS_MEM_CONTENT=ProcessorCodeObject.GLOBALS_MEM_CONTENT
THE_PROGRAM=ProcessorCodeObject.THE_PROGRAM
THE_STACK_SIZE=max(STACK_SIZE,ProcessorCodeObject.GLOBAL_STACK_SIZE) ### get max stacksize
##exdend programm to Wanted Ramsize
assert Prog_mem_SIZE>=len(THE_PROGRAM)
THE_PROGRAM=THE_PROGRAM+(0,)*(Prog_mem_SIZE-len(THE_PROGRAM))
##exdend constants to Wanted Ramsize
#assert Constants_mem_SIZE>=len(CONSTANTS_MEM_CONTENT)
#CONSTANTS_MEM_CONTENT=CONSTANTS_MEM_CONTENT+(0,)*(Constants_mem_SIZE-len(CONSTANTS_MEM_CONTENT))
##exdend Globals to Wanted Ramsize
GLOBAL_MEM_START=max(PREDEFINED_IO_ADDRESSES.values())+1
assert (Globals_mem_SIZE-GLOBAL_MEM_START)>=len(GLOBALS_MEM_CONTENT)
GLOBALS_MEM_CONTENT=(0,)*GLOBAL_MEM_START+GLOBALS_MEM_CONTENT+(0,)*(Globals_mem_SIZE-len(GLOBALS_MEM_CONTENT)-GLOBAL_MEM_START)
#print GLOBAL_MEM_START
# CONTENT=CONTENT+tuple([0]*(DEPTH-len(CONTENT)))
# if DEPTH!=None:
# assert DEPTH>=len(GLOBAL_CONTENT)
# GLOBAL_CONTENT=GLOBAL_CONTENT+tuple([0]*(DEPTH-len(GLOBAL_CONTENT)))
###### Start of the Processor myhdl implementation ############
import sys
from myhdl import *
import math
def DP_RAM(dout, din, addr_wr,addr_rd, we, clk, WORD_SZ=8, DEPTH=16384):
mem = [Signal(intbv(0,min=-2**(WORD_SZ-1),max=2**(WORD_SZ-1))) for i in range(DEPTH)]
@always(clk.posedge)
def write_read():
if we:
mem[int(addr_wr)].next = din
dout.next = mem[int(addr_rd)]
return write_read
def ROM(clk,rst,dout,addr,CONTENT,WORD_SZ=8):
mem = [Signal(intbv(CONTENT[i])[WORD_SZ:]) for i in range(len(CONTENT))]
@always(clk.posedge)
def rom_logic():
dout.next= mem[addr]
return rom_logic,combi_log
def RAMg(clk,rst,dout,din,we,addr,CONTENT,WORD_SZ=8):
if 9<WORD_SZ<19: #seems to be a lattice error
mem1 = [Signal(intbv(CONTENT[i]&0x1ff)[9:]) for i in range(len(CONTENT))]
mem2 = [Signal(intbv((CONTENT[i]&0xfffe00)>>9)[WORD_SZ-9:]) for i in range(len(CONTENT))]
dout1 = Signal(intbv(0)[9:])
dout2 = Signal(intbv(0)[9:])
dinXX= Signal(intbv(0)[9:])
@always(clk.posedge)
def progrom_logic():
if we:
mem1[addr].next = din[9:0]
mem2[addr].next = dinXX
dout2.next=mem2[addr]
dout1.next=mem1[addr]
#dout.next=concat(mem2[addr],mem1[addr])
@always_comb
def lol():
dinXX.next= din[WORD_SZ:9]
dout.next=concat(dout2[WORD_SZ-9:],dout1).signed()
return progrom_logic,lol
else:
mem = [Signal(intbv(CONTENT[i])[WORD_SZ:]) for i in range(len(CONTENT))]
@always(clk.posedge)
def progrom_logic():
if we:
mem[addr].next = din
dout.next=mem[addr].signed() ###TODO
return progrom_logic
def RAM(clk,rst,dout,din,we,addr,CONTENT,WORD_SZ=8):
if 9<WORD_SZ<19: #seems to be a lattice error
mem1 = [Signal(intbv(CONTENT[i]&0x1ff)[9:]) for i in range(len(CONTENT))]
mem2 = [Signal(intbv((CONTENT[i]&0xfffe00)>>9)[9:]) for i in range(len(CONTENT))]
dout1 = Signal(intbv(0)[9:])
dout2 = Signal(intbv(0)[9:])
dinXX= Signal(intbv(0)[9:])
@always(clk.posedge)
def progrom_logic():
if we:
mem1[addr].next = din[9:0]
mem2[addr].next = dinXX
dout2.next=mem2[addr]
dout1.next=mem1[addr]
#dout.next=concat(mem2[addr],mem1[addr])
@always_comb
def lol():
dinXX.next= din[WORD_SZ:9]
dout.next=concat(dout2[WORD_SZ-9:],dout1)
return progrom_logic,lol
else:
mem = [Signal(intbv(CONTENT[i])[WORD_SZ:]) for i in range(len(CONTENT))]
@always(clk.posedge)
def progrom_logic():
if we:
mem[addr].next = din
dout.next=mem[addr].signed() ###TODO
return progrom_logic
GLOBAL_NUMBERSTACK_OPS=22
STACK_NOP,STACK_ADD,STACK_POSITIVE,STACK_NOT,STACK_NEGATIVE,STACK_INVERT,STACK_RSHIFT,STACK_LSHIFT,STACK_AND,STACK_SUB,STACK_OR,STACK_XOR,STACK_POP,STACK_LOAD,STACK_CMP,STACK_ROT_FOUR, STACK_ROT_TWO, STACK_ROT_THREE_0,STACK_ROT_THREE_1,STACK_DUP_TOP,STACK_SETUP_LOOP,STACK_POP_BLOCK=range(GLOBAL_NUMBERSTACK_OPS)
def Stack(clk,rst,TopData_Out,Data_In,StackOP,CMPmode,WORD_SZ=32,SIZE=4,PROG_SIZE_BITWIDTH=8,NESTED_LOOP_DEPTH=32):
CONST_LOOP_DEPTH=NESTED_LOOP_DEPTH
if SIZE<4:
SIZE=4
#MODULO_CONST=2**WORD_SZ
Stack_mem = [Signal(intbv(0,min=-2**(WORD_SZ-1),max=2**(WORD_SZ-1))) for i in range(SIZE*CONST_LOOP_DEPTH)]
stack_read_addr=Signal(intbv(0,min=0,max=SIZE*CONST_LOOP_DEPTH))
stack_write_addr=Signal(intbv(0,min=0,max=SIZE*CONST_LOOP_DEPTH))
TOF_RAM_Data=Signal(intbv(0,min=-2**(WORD_SZ-1),max=2**(WORD_SZ-1)))
Data_to_REG=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
#Data_to_REG_RD=Signal(intbv(0,min=-2**(WORD_SZ-1),max=2**(WORD_SZ-1)))
REG_TopOfStack_Data=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
TOS_pointer=Signal(intbv(0,min=0,max=SIZE))
TOS_pointer_pre=Signal(intbv(0,min=0,max=SIZE))
#TOS3_pointer= Signal(intbv(0,min=0,max=SIZE))
REG_StackOP=Signal(intbv(0,min=0,max=GLOBAL_NUMBERSTACK_OPS))
REG_CmpMode=Signal(intbv(0,min=0,max=6))
enable_stackpointer_increase=Signal(bool(0))
enable_stackpointer_deacrease=Signal(bool(0))
enable_stack_write_data=Signal(bool(0))
stack_offset=Signal(intbv(0,min=0,max=SIZE*CONST_LOOP_DEPTH))
stack_pos_mem=[Signal(intbv(0,min=0,max=SIZE)) for i in range(CONST_LOOP_DEPTH+1)]
stack_pos_mem_addr=Signal(intbv(0,min=0,max=CONST_LOOP_DEPTH+1))
SaveStack_pos=Signal(bool(0))
ReturnToStack_pos=Signal(bool(0))
#SaveStack_pos_function=Signal(bool(0))
@always(clk.posedge,rst.negedge)
def seq_logic():
if rst == 0:
TOS_pointer.next=0
stack_pos_mem_addr.next=0
stack_offset.next=0
REG_StackOP.next=STACK_NOP
REG_CmpMode.next=0
else:
if enable_stackpointer_increase:
TOS_pointer.next=(TOS_pointer+1)%SIZE
if enable_stackpointer_increase or enable_stack_write_data:
Stack_mem[int(stack_write_addr)].next=Data_to_REG
if enable_stackpointer_deacrease:
TOS_pointer.next=(TOS_pointer-1)%SIZE
if SaveStack_pos:
#print "####### Save stack pos:",TOS_pointer
stack_pos_mem[int(stack_pos_mem_addr)].next=TOS_pointer
stack_pos_mem_addr.next=stack_pos_mem_addr+1
stack_offset.next=stack_offset+SIZE
if ReturnToStack_pos:
#print "####### Return stack pos:",TOS_pointer_pre
TOS_pointer.next=TOS_pointer_pre
stack_pos_mem_addr.next=stack_pos_mem_addr-1
stack_offset.next=stack_offset-SIZE
TOS_pointer_pre.next=(stack_pos_mem[int(stack_pos_mem_addr-1)])
# if StackOP==STACK_LOAD:
# REG_TopOfStack_Data.next=Data_In
#else:
REG_TopOfStack_Data.next=Data_to_REG
REG_StackOP.next=StackOP
REG_CmpMode.next= CMPmode
TOF_RAM_Data.next=Stack_mem[int(stack_read_addr)]
#@always_comb
#def comb_logic2():
#TOS_Data_RD.next#=Stack_mem[int(TOS_pointer+stack_offset)]
#TOS1_Data_RD.next=Stack_mem[int(TOS1_pointer+stack_offset)]
#TOS2_Data_RD.next=Stack_mem[int(TOS2_pointer+stack_offset)]
#TopData_Out.next=Data_to_REG #Stack_mem[int(TOS_pointer+stack_offset)]
@always_comb
def comb_logic():
#Data_to_REG_RD.next=REG_TopOfStack_Data
stack_write_addr.next=TOS_pointer+stack_offset
stack_read_addr.next=((TOS_pointer-1)%SIZE)+stack_offset
#Data_to_stackmem.next=REG_TopOfStack_Data
Data_to_REG.next=REG_TopOfStack_Data
#TOS_Data.next=REG_TopOfStack_Data
TopData_Out.next=REG_TopOfStack_Data
#TopData_Out.next=0
SaveStack_pos.next=False
ReturnToStack_pos.next=False
enable_stackpointer_increase.next=1
enable_stackpointer_deacrease.next=0
enable_stack_write_data.next=0
if REG_StackOP==STACK_LOAD:
Data_to_REG.next=Data_In
TopData_Out.next=Data_In
elif REG_StackOP==STACK_POP:
TopData_Out.next=TOF_RAM_Data
Data_to_REG.next=TOF_RAM_Data
elif REG_StackOP==STACK_ADD:
Data_to_REG.next=TOF_RAM_Data+REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data+REG_TopOfStack_Data
elif REG_StackOP==STACK_POSITIVE: #???
Data_to_REG.next=REG_TopOfStack_Data
TopData_Out.next=REG_TopOfStack_Data
#TOS_Data.next=TOS_Data_RD
elif REG_StackOP==STACK_NOT:
Data_to_REG.next=not REG_TopOfStack_Data
TopData_Out.next=not REG_TopOfStack_Data
#TOS_Data.next=TOS_Data_RD
elif REG_StackOP==STACK_NEGATIVE:
Data_to_REG.next=-REG_TopOfStack_Data
TopData_Out.next=-REG_TopOfStack_Data
#TOS_Data.next=-TOS_Data_RD
elif REG_StackOP==STACK_INVERT:
Data_to_REG.next=~REG_TopOfStack_Data
TopData_Out.next=~REG_TopOfStack_Data
#TOS_Data.next=~TOS_Data_RD
elif REG_StackOP==STACK_RSHIFT:
Data_to_REG.next=TOF_RAM_Data>>REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data>>REG_TopOfStack_Data
#TOS1_Data.next=TOS1_Data_RD>>TOS_Data_RD
elif REG_StackOP==STACK_LSHIFT:
Data_to_REG.next=TOF_RAM_Data<<REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data<<REG_TopOfStack_Data
#TOS1_Data.next=TOS1_Data_RD<<TOS_Data_RD
elif REG_StackOP==STACK_AND:
Data_to_REG.next=TOF_RAM_Data®_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data®_TopOfStack_Data
#TOS1_Data.next=TOS1_Data_RD&TOS_Data_RD
elif REG_StackOP==STACK_SUB:
Data_to_REG.next=TOF_RAM_Data-REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data-REG_TopOfStack_Data
#TOS1_Data.next=TOS1_Data_RD-TOS_Data_RD
elif REG_StackOP==STACK_OR:
Data_to_REG.next=TOF_RAM_Data|REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data|REG_TopOfStack_Data
elif REG_StackOP==STACK_XOR:
Data_to_REG.next=TOF_RAM_Data^REG_TopOfStack_Data
TopData_Out.next=TOF_RAM_Data^REG_TopOfStack_Data
elif REG_StackOP==STACK_ROT_TWO:
TopData_Out.next=TOF_RAM_Data
Data_to_REG.next=TOF_RAM_Data
elif REG_StackOP==STACK_ROT_THREE_1:
TopData_Out.next=TOF_RAM_Data
Data_to_REG.next=TOF_RAM_Data
#elif REG_StackOP==STACK_DUP_TOP:
# TopData_Out.next=REG_TopOfStack_Data #is standard assignment
# Data_to_REG.next=REG_TopOfStack_Data
elif REG_StackOP==STACK_POP_BLOCK:
TopData_Out.next=TOF_RAM_Data
Data_to_REG.next=TOF_RAM_Data
elif REG_StackOP==STACK_CMP:
if REG_CmpMode==0: #operator <
if TOF_RAM_Data<REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if REG_CmpMode==1: #operator <=
if TOF_RAM_Data<=REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if REG_CmpMode==2: #operator ==
if TOF_RAM_Data==REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if REG_CmpMode==3: #operator !=
if TOF_RAM_Data!=REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if REG_CmpMode==4: #operator >
if TOF_RAM_Data>REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if REG_CmpMode==5: #operator >=
if TOF_RAM_Data>=REG_TopOfStack_Data:
Data_to_REG.next=1
TopData_Out.next=1
else:
Data_to_REG.next=0
TopData_Out.next=0
if StackOP==STACK_NOP:
enable_stackpointer_increase.next=0
elif StackOP==STACK_SETUP_LOOP:
enable_stackpointer_increase.next=1
SaveStack_pos.next=True
elif StackOP==STACK_POP_BLOCK:
stack_read_addr.next=((TOS_pointer_pre)%SIZE)+(stack_offset-SIZE)
enable_stackpointer_increase.next=0
ReturnToStack_pos.next=True
elif StackOP==STACK_POSITIVE: #???
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
#TOS_Data.next=TOS_Data_RD
elif StackOP==STACK_NOT:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
#TOS_Data.next=TOS_Data_RD
elif StackOP==STACK_NEGATIVE:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
#TOS_Data.next=-TOS_Data_RD
elif StackOP==STACK_INVERT:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
#TOS_Data.next=~TOS_Data_RD
elif StackOP==STACK_ADD:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD+TOS_Data_RD
elif StackOP==STACK_RSHIFT:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD>>TOS_Data_RD
elif StackOP==STACK_LSHIFT:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD<<TOS_Data_RD
elif StackOP==STACK_AND:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD&TOS_Data_RD
elif StackOP==STACK_SUB:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD-TOS_Data_RD
elif StackOP==STACK_OR:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD|TOS_Data_RD
elif StackOP==STACK_XOR:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
#TOS1_Data.next=TOS1_Data_RD^TOS_Data_RD
elif StackOP==STACK_POP:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=1
# elif StackOP==STACK_CALL_FUNCTION:
# enable_stackpointer_increase.next=0
# enable_stackpointer_deacrease.next=1 #pop function address
# SaveStack_pos_function.next=True
elif StackOP==STACK_LOAD:
enable_stackpointer_increase.next=1
enable_stackpointer_deacrease.next=0
#TopOfStack_Data.next=Data_In
elif StackOP==STACK_ROT_TWO:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
enable_stack_write_data.next=1
stack_write_addr.next=((TOS_pointer-1)%SIZE)+stack_offset
#TOS_Data.next=TOS1_Data_RD
#TOS1_Data.next=TOS_Data_RD
elif StackOP==STACK_ROT_THREE_0:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
enable_stack_write_data.next=1
stack_write_addr.next=((TOS_pointer-2)%SIZE)+stack_offset
stack_read_addr.next=((TOS_pointer-2)%SIZE)+stack_offset
elif StackOP==STACK_ROT_THREE_1:
enable_stackpointer_increase.next=0
enable_stackpointer_deacrease.next=0
enable_stack_write_data.next=1
Data_to_REG.next=TOF_RAM_Data
stack_write_addr.next=((TOS_pointer-1)%SIZE)+stack_offset
stack_read_addr.next=((TOS_pointer-1)%SIZE)+stack_offset
#TOS_Data.next=TOS1_Data_RD
#TOS1_Data.next=TOS2_Data_RD
#TOS2_Data.next=TOS_Data_RD
#elif StackOP==STACK_ROT_FOUR: ##TODO
#TOS_Data.next=Stack_mem[int(TOS_pointer)]
# TOS1_Data.next=Stack_mem[int(TOS1_pointer)]
# TOS2_Data.next=Stack_mem[int(TOS2_pointer)]
elif StackOP==STACK_DUP_TOP:
enable_stackpointer_increase.next=1
enable_stackpointer_deacrease.next=0
elif StackOP==STACK_CMP:
enable_stackpointer_increase.next=1
enable_stackpointer_deacrease.next=0
else:
enable_stackpointer_increase.next=0
return seq_logic,comb_logic#,comb_logic2
def UsignedtoSigned(val,BIT_WIDTH):
return val-(val>>(BIT_WIDTH-1))*2**BIT_WIDTH
import RS232_Norbo
import RS232Programmer
def IOGlobalModule(clk,rst,dout,din,addr,we,\
PORTA_IN,PORTB_IN,PORTC_OUT,PORTD_OUT,\
iData_RS232,WriteEnable_RS232, oWrBuffer_full_RS232,oData_RS232,read_addr_RS232,rx_addr_RS232, # iRX needs to initialized with 1 \
GLOBAL_CONTENT,WIDTH=32,Clkfrequenz=12e6,PROG_SIZE_BITWIDTH=8):
RX_BUFF_LEN=read_addr_RS232.max
Sync_in1_PORTA=Signal(intbv(0)[WIDTH:])
Sync_in2_PORTA=Signal(intbv(0)[WIDTH:])
Sync_in1_PORTB=Signal(intbv(0)[WIDTH:])
Sync_in2_PORTB=Signal(intbv(0)[WIDTH:])
INTERN_PORTC_OUT=Signal(intbv(0)[WIDTH:])
INTERN_PORTD_OUT=Signal(intbv(0)[WIDTH:])
addr_last=Signal(intbv(0,min=addr.min,max=addr.max))
dout_intern=Signal(intbv(0,min=dout.min,max=dout.max))
####Globals memory #####
#TODO unsigned to sind with this bitwidth only fits to addresses groesser function_address_start
#glob_mem = [Signal(intbv(UsignedtoSigned(GLOBAL_CONTENT[i],PROG_SIZE_BITWIDTH),min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1))) for i in range(len(GLOBAL_CONTENT))]
globalRAM_dout=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
globalRAM_din=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
globalRAM_we=Signal(bool(0))
#THA_CONTENT_GLOBAL_MOD= tuple((0,)*10+GLOBAL_CONTENT) #map(lambda val: UsignedtoSigned(val,PROG_SIZE_BITWIDTH),
globalRAM_addr=Signal(intbv(0,min=0,max=len(GLOBAL_CONTENT)))
globalRAM_inst=RAMg(clk,rst,globalRAM_dout,globalRAM_din,globalRAM_we,globalRAM_addr,CONTENT=GLOBAL_CONTENT,WORD_SZ=PROG_SIZE_BITWIDTH)
######RS232 Module#########
##### Signal definitions #####
reg_read_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
##### Instanciate RS232 Module #####
#rs232_instance=RS232_Norbo.RS232_Module(clk,rst,iRX,oTX, iData_RS232,WriteEnable_RS232, \
# oWrBuffer_full_RS232,oData_RS232,read_addr_RS232,rx_addr_RS232,Clkfrequenz=Clk_f, \
# Baudrate=BAUDRATE,RX_BUFFER_LENGTH=RX_BUFF_LEN,TX_BUFFER_LENGTH=TX_BUFF_LEN)
#Bufflength=2
#iRX_9=Signal(bool(1))
#WriteEnable_RS232_9=Signal(bool(0))
#oWrBuffer_full_RS232_9=Signal(bool(0))
#iData_RS232_9=Signal(intbv(0)[9:])
#oData_RS232_9=Signal(intbv(0)[9:])
#read_addr_RS232_9=Signal(intbv(0,min=0,max=Bufflength))
#rx_addr_RS232_9=Signal(intbv(0,min=0,max=Bufflength))
##### Instanziate RS232 9 Bit Module #####
#rs232_instance9=RS232_Bitmode.RS232_Module(clk,rst,iRX_9,oTX_9, iData_RS232_9,WriteEnable_RS232_9, \
# oWrBuffer_full_RS232_9,oData_RS232_9,read_addr_RS232_9,rx_addr_RS232_9,Clkfrequenz=Clkfrequenz, \
# Baudrate=625000,RX_BUFFER_LENGTH=Bufflength,TX_BUFFER_LENGTH=Bufflength)
@always_comb
def rs232_read_buffer_addr():
globalRAM_we.next=we
globalRAM_din.next=din
globalRAM_addr.next=addr
#iRX_9.next=True
if we and (addr==5):
read_addr_RS232.next=din[WIDTH:]%RX_BUFF_LEN
else:
read_addr_RS232.next=reg_read_addr_RS232
if addr_last>=8:
dout.next=globalRAM_dout
else:
dout.next=dout_intern
@always(clk.posedge,rst.negedge)
def IO_write_sync():
if rst==0:
INTERN_PORTC_OUT.next=0
INTERN_PORTD_OUT.next=0
else:
Sync_in1_PORTA.next=PORTA_IN
Sync_in2_PORTA.next=Sync_in1_PORTA
Sync_in1_PORTB.next=PORTB_IN
Sync_in2_PORTB.next=Sync_in1_PORTB
WriteEnable_RS232.next=0
#WriteEnable_RS232_9.next=0
if we:
if addr==2:
INTERN_PORTC_OUT.next=din[WIDTH:]
if addr==3:
INTERN_PORTD_OUT.next=din[WIDTH:]
if addr==4:
WriteEnable_RS232.next=1
iData_RS232.next=din[WIDTH:]
if addr==5:
reg_read_addr_RS232.next=din[WIDTH:]
#if addr==8:
# WriteEnable_RS232_9.next=1
# iData_RS232_9.next=concat(INTERN_PORTC_OUT[7],din[WIDTH:])
#if addr>=10:
# glob_mem[int(addr-10)].next=din
#GLOBAL_CONTENT[int(addr-4)]
@always(clk.posedge)
def IO_read():
PORTC_OUT.next=INTERN_PORTC_OUT
PORTD_OUT.next=INTERN_PORTD_OUT
addr_last.next=addr
dout_intern.next = 0
if addr==0:
dout_intern.next = Sync_in2_PORTA[WIDTH:].signed()
if addr==1:
dout_intern.next = Sync_in2_PORTB[WIDTH:].signed()
if addr==2:
dout_intern.next = INTERN_PORTC_OUT[WIDTH:].signed()
if addr==3:
dout_intern.next = INTERN_PORTD_OUT[WIDTH:].signed()
if addr==4:
dout_intern.next = oData_RS232
if addr==5:
dout_intern.next = reg_read_addr_RS232
if addr==6:
dout_intern.next = rx_addr_RS232
if addr==7:
dout_intern.next = oWrBuffer_full_RS232
#if addr==9:
# dout_intern.next = oWrBuffer_full_RS232_9
#if addr>=10:
# dout_intern.next = glob_mem[int(addr-10)] # globalRAM_dout #
return IO_write_sync,IO_read,rs232_read_buffer_addr,globalRAM_inst
def Processor(clk,rst,CpuUsesRam,PORTA_IN,PORTB_IN,PORTC_OUT,PORTD_OUT,iRX,oTX,CPU_PROGRAM=THE_PROGRAM,CPU_ARG_BITLENGTH=ARGUMENT_BITLENGTH,CPU_GLOBALS=GLOBALS_MEM_CONTENT,CONST_FUNCTION_ADRESSES_START=THE_FUNCTION_ADRESSES_START,VAR_BITWIDTH=CPU_BITWIDTH,VAR_MEM_DEPTH=Var_mem_SIZE,STACK_SIZE=THE_STACK_SIZE,CLK_F=CPU_CLK_FREQ,RS232_BAUDRATE=Baudrate_const,MAX_NUMBER_FUNCTION_ARGUMENTS=NR_FUNCTION_ARGUMENTS,NR_NESTED_LOOPS=NR_NESTED_LOOPS,DEBUG_OUTPUT=False):
CONST_PROGRAMM_LENGTH=len(CPU_PROGRAM)
PROG_SIZE_BITWIDTH=max(int(math.log(CONST_PROGRAMM_LENGTH-1,2))+1,VAR_BITWIDTH)
NUMBER_OF_SELECTS=4
ENUM_SEL_NONE,ENUM_SEL_VARIABLE,ENUM_SEL_CONST,ENUM_SEL_GLOBAL=range(NUMBER_OF_SELECTS)
StackTopSel=Signal(intbv(0,min=0,max=NUMBER_OF_SELECTS))
REG_StackTopSel=Signal(intbv(0,min=0,max=NUMBER_OF_SELECTS))
NUMBER_FROM_SELECTS=4
FROM_NONE,FROM_CONST,FROM_GLOBAL,FROM_VAR=range(NUMBER_FROM_SELECTS)
argnext_store=Signal(intbv(0,min=0,max=NUMBER_FROM_SELECTS))
REG_argnext_store=Signal(intbv(0,min=0,max=NUMBER_FROM_SELECTS))
#### helping signals
#MAX_NUMBER_FUNCTION_ARGUMENTS=14 # magic number
#EnableJump=Signal(bool(0))
Inc_ArgumentCount=Signal(bool(0))
Clear_ArgumentCount=Signal(bool(0))
REG_ArgumentCount=Signal(intbv(0,min=0,max=MAX_NUMBER_FUNCTION_ARGUMENTS+2))
programmRAM_addr=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH+1)) #max=Prog_mem_SIZE
programmRAM_addr_inst=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH+1))
#programmRAM_addr_wr=Signal(intbv(0)[8:])
programmRAM_dout=Signal(intbv(0)[8+CPU_ARG_BITLENGTH:]) #first 8 bit is opcode then comes the argument
programmRAM_din=Signal(intbv(0)[8+CPU_ARG_BITLENGTH:]) ## 8 is opcode length
programmRAM_din_inst=Signal(intbv(0)[8+CPU_ARG_BITLENGTH:])
programmRAM_we=Signal(bool(0))
programmRAM_we_inst=Signal(bool(0))
MAX_SUB_CYCLES=2
REG_sub_pc_count=Signal(intbv(0,min=0,max=MAX_SUB_CYCLES))
sub_pc_count_next=Signal(intbv(0,min=0,max=MAX_SUB_CYCLES))
Push_Programcounter=Signal(bool(0))
Pop_Programcounter=Signal(bool(0))
Old_ProgrammCounter=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH))
REG_PC_offsetValue=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH))
PC_offsetValue=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH))
Old_PC_offsetValue=Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH))
Set_Variables_offset=Signal(bool(0))
#Return_Variables_offset=Signal(bool(0))
#### Programm Memory Signals
Opcode=Signal(intbv(0)[8:])
Arg1=Signal(intbv(0)[CPU_ARG_BITLENGTH:])
REG_ProgramCounter=Signal(intbv(-1,min=-1,max=CONST_PROGRAMM_LENGTH))
#### Constants Memory Signals
#constantsRAM_dout=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1)))
#constantsRAM_addr=Signal(intbv(0,min=0,max=len(CPU_CONSTANTS)))
#constantsRAM_addr_inst=Signal(intbv(0,min=0,max=len(CPU_CONSTANTS)))
#constantsRAM_din=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1)))
#constantsRAM_din_inst=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1)))
#constantsRAM_we=Signal(bool(0))
#constantsRAM_we_inst=Signal(bool(0))
#### Programm Variables RAM Signals
varRAM_dout=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
varRAM_din=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
VariablesAddr=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
varRAM_addr_wr=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
varRAM_addr_rd=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
REG_max_Variables_address=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
Old_max_Variables_address=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
REG_Variables_addr_offset=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
Old_Variables_addr_offset=Signal(intbv(0,min=0,max=VAR_MEM_DEPTH))
varRAM_we=Signal(bool(0))
#### Stack Signals
# STACK_SIZE
#Stack_DataIn=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
#StackValue0=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
### welches ist goesser log(CONST_PROGRAMM_LENGTH,2)+1 or VAR_BITWIDTH
Stack_DataIn=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
StackValue0=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1)))
StackOP=Signal(intbv(0,min=0,max=GLOBAL_NUMBERSTACK_OPS))
StackOP_CMPmode=Signal(intbv(0,min=0,max=6))
#### IO Module Signals
NR_IO_ADDRESSES=max(PREDEFINED_IO_ADDRESSES.values())+1 #
IO_MODULE_LENGTH_MAX=NR_IO_ADDRESSES+len(GLOBALS_MEM_CONTENT)+1
CONST_FUNCTION_ADRESSES_START # <-- is a parameter of the processor unit
IO_dout=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
IO_din=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
IO_addr=Signal(intbv(0,min=0,max=IO_MODULE_LENGTH_MAX))
IO_we=Signal(bool(0))
IO_din_inst=Signal(intbv(0,min=-2**(PROG_SIZE_BITWIDTH-1),max=2**(PROG_SIZE_BITWIDTH-1))) #Signal(intbv(0)[VAR_BITWIDTH:])
IO_addr_inst=Signal(intbv(0,min=0,max=IO_MODULE_LENGTH_MAX))
IO_we_inst=Signal(bool(0))
#########RS232 module###################
RX_BUFF_LEN=8
TX_BUFF_LEN=2
iData_RS232=Signal(intbv(0)[8:])
oData_RS232=Signal(intbv(0)[8:])
WriteEnable_RS232=Signal(bool(0))
oWrBuffer_full_RS232=Signal(bool(0))
read_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
rx_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
##### Instanciate RS232 Module #####
rs232_instance=RS232_Norbo.RS232_Module(clk,rst,iRX,oTX, \
iData_RS232 ,WriteEnable_RS232, oWrBuffer_full_RS232,oData_RS232,read_addr_RS232,rx_addr_RS232, \
Clkfrequenz=CLK_F,Baudrate=RS232_BAUDRATE,RX_BUFFER_LENGTH=RX_BUFF_LEN,TX_BUFFER_LENGTH=TX_BUFF_LEN)
####### end RS232 module #################
####################Data Memories #################################
# Prog_mem_SIZE=
#
#Constants_mem_SIZE=
#Globals_mem_SIZE=
####Variables RAM instantiation
VariablesRAM_inst=DP_RAM(varRAM_dout, varRAM_din, varRAM_addr_wr,varRAM_addr_rd, varRAM_we, clk, WORD_SZ=VAR_BITWIDTH, DEPTH=VAR_MEM_DEPTH)
####Programm Code Memory instantiation
ProgrammCode_inst=RAM(clk,rst,programmRAM_dout,programmRAM_din_inst,programmRAM_we_inst,programmRAM_addr_inst,CONTENT=CPU_PROGRAM , WORD_SZ=8+CPU_ARG_BITLENGTH)
####Constants memory instantiation
#ConstantsRAM_inst=RAM(clk,rst,constantsRAM_dout,constantsRAM_din_inst,constantsRAM_we_inst,constantsRAM_addr_inst,CONTENT=CPU_CONSTANTS,WORD_SZ=VAR_BITWIDTH)
io_iData_RS232=Signal(intbv(0)[8:])
#io_oData_RS232=Signal(intbv(0)[8:])
io_WriteEnable_RS232=Signal(bool(0))
#io_oWrBuffer_full_RS232=Signal(bool(0))
io_read_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
#io_rx_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
###I/O Module GlobalMemory instantiation (IO Port is a global memory)
IOModule_inst=IOGlobalModule(clk,rst,IO_dout,IO_din_inst,IO_addr_inst,IO_we_inst, \
PORTA_IN,PORTB_IN,PORTC_OUT,PORTD_OUT, \
io_iData_RS232,io_WriteEnable_RS232, oWrBuffer_full_RS232,oData_RS232,io_read_addr_RS232,rx_addr_RS232,\
GLOBAL_CONTENT=CPU_GLOBALS,WIDTH=VAR_BITWIDTH,Clkfrequenz=CLK_F,PROG_SIZE_BITWIDTH=PROG_SIZE_BITWIDTH)
#####################End Data Memories ###########################
###The stack
TheStack_inst=Stack(clk,rst,StackValue0,Stack_DataIn,StackOP,StackOP_CMPmode, WORD_SZ=VAR_BITWIDTH,SIZE=STACK_SIZE,PROG_SIZE_BITWIDTH=PROG_SIZE_BITWIDTH,NESTED_LOOP_DEPTH=NR_NESTED_LOOPS)
CONST_PC_STACK_DEPTH=5 #Defines how many nested function are possible TODO magic number
REG_pc_stack_addr=Signal(intbv(0,min=0,max=CONST_PC_STACK_DEPTH))
pc_steck_mem = [Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH)) for i in range(CONST_PC_STACK_DEPTH)]
pc_offsets_mem = [Signal(intbv(0,min=0,max=CONST_PROGRAMM_LENGTH)) for i in range(CONST_PC_STACK_DEPTH)]
max_var_addr_mem=[Signal(intbv(0,min=0,max=VAR_MEM_DEPTH)) for i in range(CONST_PC_STACK_DEPTH)]
WidthInfo=Signal(intbv(0x11)[8:])
RS232programmer_data=Signal(intbv(0)[32:])
RS232programmer_addr=Signal(intbv(0)[32:])
RS232programmer_we=Signal(bool(0))
prog_iData_RS232=Signal(intbv(0)[8:])
#prog_oData_RS232=Signal(intbv(0)[8:])
prog_WriteEnable_RS232=Signal(bool(0))
#prog_oWrBuffer_full_RS232=Signal(bool(0))
prog_read_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
#prog_rx_addr_RS232=Signal(intbv(0,min=0,max=RX_BUFF_LEN))
programmer_enable=Signal(bool(0))
RS232Programmer_inst=RS232Programmer.RS232Programmer(clk,rst,programmer_enable,WidthInfo, \
RS232programmer_data,RS232programmer_addr,RS232programmer_we, \
prog_iData_RS232,prog_WriteEnable_RS232, oWrBuffer_full_RS232,oData_RS232,prog_read_addr_RS232,rx_addr_RS232)
#LENGTH_INFO_REGS=10
INFOREGS_CONTENT= (CONST_FUNCTION_ADRESSES_START,STACK_SIZE,0)
InfoRegsList=[Signal(intbv(INFOREGS_CONTENT[i],min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1))) for i in range(len(INFOREGS_CONTENT))]
infoREGs_din=Signal(intbv(0,min=-2**(VAR_BITWIDTH-1),max=2**(VAR_BITWIDTH-1)))
infoREGs_addr=Signal(intbv(0,min=0,max=len(INFOREGS_CONTENT)))
infoREGs_we=Signal(bool(0))
#InfoRegsList[0]= CONST_FUNCTION_ADRESSES_START
@always_comb
def muxRAMtoRS232Programmer():
IO_din_inst.next=0
IO_addr_inst.next=0
IO_we_inst.next=False
#constantsRAM_din_inst.next=0
#constantsRAM_addr_inst.next=0
#constantsRAM_we_inst.next=False
programmRAM_din_inst.next=0
programmRAM_addr_inst.next=0
programmRAM_we_inst.next=False
infoREGs_din.next=0
infoREGs_addr.next=0
infoREGs_we.next=False
programmer_enable.next=False
iData_RS232.next=io_iData_RS232
WriteEnable_RS232.next=io_WriteEnable_RS232
read_addr_RS232.next=io_read_addr_RS232
if CpuUsesRam==True:
programmer_enable.next=False
iData_RS232.next=io_iData_RS232
WriteEnable_RS232.next=io_WriteEnable_RS232
read_addr_RS232.next=io_read_addr_RS232
IO_din_inst.next=IO_din
IO_addr_inst.next=IO_addr
IO_we_inst.next=IO_we
#constantsRAM_din_inst.next=0 #constantsRAM_din
#constantsRAM_addr_inst.next=constantsRAM_addr
#constantsRAM_we_inst.next=False #constantsRAM_we
programmRAM_din_inst.next=0 #programmRAM_din
programmRAM_addr_inst.next=programmRAM_addr
programmRAM_we_inst.next=False #programmRAM_we
else: ## RS232 programmer accesses ram#
### mux rs232
programmer_enable.next=True
iData_RS232.next=prog_iData_RS232
WriteEnable_RS232.next=prog_WriteEnable_RS232
read_addr_RS232.next=prog_read_addr_RS232
### top to bits are RAM selecters
programmRAM_din_inst.next=RS232programmer_data
programmRAM_addr_inst.next=RS232programmer_addr
#constantsRAM_din_inst.next=RS232programmer_data
#constantsRAM_addr_inst.next=RS232programmer_addr
IO_din_inst.next=RS232programmer_data
IO_addr_inst.next=RS232programmer_addr
infoREGs_din.next=RS232programmer_data
infoREGs_addr.next=RS232programmer_addr
if (WidthInfo[8:6]==0):
programmRAM_we_inst.next=RS232programmer_we
#if (WidthInfo[8:6]==1):
# constantsRAM_we_inst.next=RS232programmer_we
if (WidthInfo[8:6]==2):
IO_we_inst.next=RS232programmer_we
if (WidthInfo[8:6]==3):
infoREGs_we.next=RS232programmer_we