summaryrefslogtreecommitdiffstats
path: root/mig_test/software/lib/crt0.S
blob: 9c9e1999ae162bbf32f240d7a5b345f21439948d (plain)
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
/* Startup code for ZPU
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
	.file	"crt0.S"
	

; cleaned BLa
	
	
;	.section ".fixed_vectors","ax"
; KLUDGE!!! we remove the executable bit to avoid relaxation 
	.section ".fixed_vectors","a" 

; DANGER!!!! 
; we need to align these code sections to 32 bytes, which
; means we must not use any assembler instructions that are relaxed
; at linker time
; DANGER!!!! 

	.macro fixedim value
			im \value
	.endm

	.macro  jsr address
			im _memreg     	; save R0
			load
			im _memreg+4	; save R1
			load
			im _memreg+8	; save R2
			load
			fixedim \address
			call
			im _memreg+8
			store		; restore R2
			im _memreg+4
			store		; restore R1
			im _memreg
			store		; restore R0
	.endm


	.macro  jmp address
			fixedim \address
			poppc
	.endm
		

	.macro fast_neg
	not
	im 1
	add
	.endm
	
	.macro cimpl funcname
	; save R0
	im _memreg
	load
	
	; save R1
	im _memreg+4
	load
	
	; save R2
	im _memreg+8
	load
	
	loadsp 20
	loadsp 20
	
	fixedim \funcname
	call

	; destroy arguments on stack
	storesp 0
	storesp 0	
	 
	im _memreg
	load
	
	; poke the result into the right slot
	storesp 24

	; restore R2
	im _memreg+8
	store
	
	; restore R1
	im _memreg+4
	store
	
	; restore r0
	im _memreg
	store
	
	
	storesp 4
	poppc
	.endm

	.macro mult1bit
	; create mask of lowest bit in A
	loadsp 8 ; A
	im 1
	and
	im -1
	add
	not
	loadsp 8 ; B
	and 
	add ; accumulate in C
	
	; shift B left 1 bit
	loadsp 4 ; B
	addsp 0
	storesp 8 ; B
	
	; shift A right 1 bit
	loadsp 8 ; A
	flip
	addsp 0
	flip
	storesp 12 ; A
	.endm



/* vectors */
        .balign 32,0
# offset 0x0000 0000
		.globl _start
_start:
		; intSp must be 0 when we jump to _premain; aha BLa
		
		;im ZPU_ID
		;loadsp 0
		;im _cpu_config
		;store
		;config
		jmp _premain


        .balign 32,0
# offset 0x0000 0020
		.globl _zpu_interrupt_vector
_zpu_interrupt_vector:
		jmp ___zpu_interrupt_vector

# memreg moved Bla
# offset 0x0000 0030
        .balign 16,0
		.globl _memreg
		.weak _memreg
_memreg:



/* instruction emulation code */

# opcode 34
# offset 0x0000 0040
	.balign 32,0
_loadh:
	loadsp 4
	; by not masking out bit 0, we cause a memory access error 
	; on unaligned access
	im ~0x2
	and
	load

	; mult 8	
	loadsp 8
	im 3
	and
	fast_neg
	im 2
	add
	im 3
	ashiftleft
	; shift right addr&3 * 8
	lshiftright
	im 0xffff
	and
	storesp 8
	
	poppc

# opcode 35
# offset 0x0000 0060
	.balign 32,0
_storeh:
	loadsp 4
	; by not masking out bit 0, we cause a memory access error 
	; on unaligned access
	im ~0x2
	and
	load

	; mask
	im 0xffff
	loadsp 12
	im 3
	and
	fast_neg
	im 2
	add
	im 3
	ashiftleft
	ashiftleft
	not

	and

	loadsp 12
	im 0xffff

	nop
		
	fixedim _storehtail
	poppc


# opcode 36
# offset 0x0000 0080
	.balign 32,0
_lessthan:
	loadsp 8
	fast_neg
	loadsp 8
	add

	; DANGER!!!!
	; 0x80000000 will overflow when negated, so we need to mask
	; the result above with the compare positive to negative
	; number case
	loadsp 12
	loadsp 12
	not
	and
	not
	and


	; handle case where we are comparing a negative number
	; and positve number. This can underflow. E.g. consider 0x8000000 < 0x1000
	loadsp 12
	not
	loadsp 12
	and
	
	or



	flip
	im 1
	and	

	
	storesp 12
	storesp 4
	poppc
	

# opcode 37
# offset 0x0000 00a0
	.balign 32,0
_lessthanorequal:
	loadsp 8
	loadsp 8
	lessthan
	loadsp 12
	loadsp 12
	eq
	or
	
	storesp 12
	storesp 4
	poppc

	
# opcode 38
# offset 0x0000 00c0
	.balign 32,0
_ulessthan:
	; fish up arguments 
	loadsp 4
	loadsp 12
	
	/* low: -1 if low bit dif is negative 0 otherwise:  neg (not x&1 and (y&1))
		x&1		y&1		neg (not x&1 and (y&1))
		1		1		0
		1		0 		0
		0		1		-1
		0		0		0
	
	*/
	loadsp 4 
	not
	loadsp 4
	and
	im 1
	and
	neg
	
	
	/* high: upper 31-bit diff is only wrong when diff is 0 and low=-1
		high=x>>1 - y>>1 + low
		
		extremes
		
		0000 - 1111:
		low= neg(not 0 and 1) = 1111 (-1)
		high=000+ neg(111) +low = 000 + 1001 + low = 1000 
		OK
		
		1111 - 0000
		low=neg(not 1 and 0) = 0
		high=111+neg(000) + low = 0111
		OK
		 
		
	 */
	loadsp 8
	
	flip 
	addsp 0
	flip
	
	loadsp 8
	
	flip	
	addsp 0
	flip

	sub

	; if they are equal, then the last bit decides...	
	add
	
	/* test if negative: result = flip(diff) & 1 */
	flip
	im 1
	and

	; destroy a&b which are on stack	
	storesp 4
	storesp 4
	
	storesp 12
	storesp 4
	poppc			

# opcode 39
# offset 0x0000 00e0
	.balign 32,0
_ulessthanorequal:
	loadsp 8
	loadsp 8
	ulessthan
	loadsp 12
	loadsp 12
	eq
	or
	
	storesp 12
	storesp 4
	poppc


# opcode 40
# offset 0x0000 0100
	.balign 32,0
	.globl _swap
_swap:
	breakpoint ; tbd

# opcode 41
# offset 0x0000 0120
	.balign 32,0
_slowmult:
	im _slowmultImpl
	poppc

# opcode 42
# offset 0x0000 0140
	.balign 32,0
_lshiftright:
	loadsp 8
	flip

	loadsp 8
	ashiftleft
	flip
	
	storesp 12
	storesp 4

	poppc
	

# opcode 43
# offset 0x0000 0160
	.balign 32,0
_ashiftleft:
	loadsp 8
	
	loadsp 8
	im 0x1f
	and
	fast_neg
	im _ashiftleftEnd
	add
	poppc
	
	
	
# opcode 44
# offset 0x0000 0180
	.balign 32,0
_ashiftright:
	loadsp 8
	loadsp 8
	lshiftright
	
	; handle signed value
	im -1
	loadsp 12
	im 0x1f
	and
	lshiftright
	not	; now we have an integer on the stack with the signed 
		; bits in the right position

	; mask these bits with the signed bit.
	loadsp 16
	not
	flip
	im 1
	and
	im -1
	add
	
	and	
	
	; stuff in the signed bits...
	or
	
	; store result into correct stack slot	
	storesp 12
	
	; move up return value 
	storesp 4
	poppc

# opcode 45
# offset 0x0000 01a0
	.balign 32,0
_call:
	; fn
	loadsp 4
	
	; return address
	loadsp 4

	; store return address
	storesp 12
	
	; fn to call
	storesp 4
	
	pushsp	; flush internal stack
	popsp
		
	poppc

_storehtail:

	and
	loadsp 12
	im 3
	and
	fast_neg
	im 2
	add
	im 3
	ashiftleft
	nop
	ashiftleft
	
	or
	
	loadsp 8
	im  ~0x3
	and

	store
	
	storesp 4
	storesp 4
	poppc


# opcode 46
# offset 0x0000 01c0
	.balign 32,0
_eq:
	loadsp 8
	fast_neg
	loadsp 8
	add
	
	not 
	loadsp 0
	im 1
	add
	not
	and
	flip
	im 1
	and
	
	storesp 12
	storesp 4
	poppc

# opcode 47
# offset 0x0000 01e0
	.balign 32,0
_neq:
	loadsp 8
	fast_neg
	loadsp 8
	add
	
	not 
	loadsp 0
	im 1
	add
	not
	and
	flip

	not

	im 1
	and
		
	storesp 12
	storesp 4
	poppc
	

# opcode 48
# offset 0x0000 0200
	.balign 32,0
_neg:
	loadsp 4
	not
	im 1
	add
	storesp 8
	
	poppc
	

# opcode 49
# offset 0x0000 0220
	.balign 32,0
_sub:
	loadsp 8
	loadsp 8
	fast_neg
	add
	storesp 12

	storesp 4

	poppc


# opcode 50
# offset 0x0000 0240
	.balign 32,0
_xor:
	loadsp 8
	not
	loadsp 8
	and
	
	loadsp 12
	loadsp 12
	not
	and

	or

	storesp 12
	storesp 4
	poppc

# opcode 51
# offset 0x0000 0260
	.balign 32,0
_loadb:
	loadsp 4
	im ~0x3
	and
	load

	loadsp 8
	im 3
	and
	fast_neg
	im 3
	add
	; x8
	addsp 0
	addsp 0
	addsp 0

	lshiftright

	im 0xff
	and
	storesp 8
	
	poppc


# opcode 52
# offset 0x0000 0280
	.balign 32,0
_storeb:
	loadsp 4
	im ~0x3
	and
	load

	; mask away destination
	im _mask
	loadsp 12
	im 3
	and
	addsp 0
	addsp 0
	add
	load

	and


	im _storebtail
	poppc
	
# opcode 53
# offset 0x0000 02a0
	.balign 32,0
_div:
	jmp ___div
	
# opcode 54
# offset 0x0000 02c0
	.balign 32,0
_mod:
	jmp ___mod

# opcode 55
# offset 0x0000 02e0
	.balign 32,0
	.globl _eqbranch
_eqbranch:
	loadsp 8
	
	; eq

	not 
	loadsp 0
	im 1
	add
	not
	and
	flip
	im 1
	and

	; mask
	im -1
	add
	loadsp 0
	storesp 16

	; no branch address
	loadsp 4
	
	and

	; fetch boolean & neg mask
	loadsp 12
	not
	
	; calc address & mask for branch
	loadsp 8
	loadsp 16
	add
	; subtract 1 to find PC of branch instruction
	im -1
	add
	
	and

	or	
	
	storesp 4
	storesp 4
	storesp 4
	poppc	


# opcode 56
# offset 0x0000 0300
	.balign 32,0
	.globl _neqbranch
_neqbranch:
	loadsp 8
	
	; neq

	not 
	loadsp 0
	im 1
	add
	not
	and
	flip
	
	not
	
	im 1
	and

	; mask
	im -1
	add
	loadsp 0
	storesp 16

	; no branch address
	loadsp 4
	
	and

	; fetch boolean & neg mask
	loadsp 12
	not
	
	; calc address & mask for branch
	loadsp 8
	loadsp 16
	add
	; find address of branch instruction
	im -1
	add
	
	and

	or	
	
	storesp 4
	storesp 4
	storesp 4
	poppc	

# opcode 57
# offset 0x0000 0320
	.balign 32,0
	.globl _poppcrel
_poppcrel:
	add
	; address of poppcrel
	im -1
	add
	poppc
		
# opcode 58
# offset 0x0000 0340
	.balign 32,0
	.globl _config
_config:
	im 1 
	nop
	im _hardware
	store
	storesp 4
	poppc

# opcode 59
# offset 0x0000 0360
	.balign 32,0
_pushpc:
	loadsp 4
	im 1
	add 
	storesp 8
	poppc
	
# opcode 60
# offset 0x0000 0380
	.balign 32,0
_syscall_emulate:
	.byte 0
	
# opcode 61
# offset 0x0000 03a0
	.balign 32,0
_pushspadd:
	pushsp
	im 4
	add
	loadsp 8
	addsp 0
	addsp 0
	add
	storesp 8
	
	poppc

# opcode 62
# offset 0x0000 03c0
	.balign 32,0
_halfmult:
	breakpoint
	
# opcode 63
# offset 0x0000 03e0
	.balign 32,0
_callpcrel:
	loadsp 4
	loadsp 4
	add
	im -1
	add
	loadsp 4
	
	storesp 12	; return address
	storesp 4 
	pushsp		; this will flush the internal stack.
	popsp
	poppc

	.text

	


_ashiftleftBegin:
	.rept 0x1f
	addsp 0
	.endr
_ashiftleftEnd:
	storesp 12
	storesp 4
	poppc
	
_storebtail:
	loadsp 12
	im 0xff
	and
	loadsp 12
	im 3
	and

	fast_neg
	im 3
	add
	; x8
	addsp 0
	addsp 0
	addsp 0

	ashiftleft
	 
	or
	
	loadsp 8
	im  ~0x3
	and

	store
	
	storesp 4
	storesp 4
	poppc
	



; NB! this is not an EMULATE instruction. It is a varargs fn.
	.globl _syscall	
_syscall:
	syscall
	poppc
	
_slowmultImpl:
	
	loadsp 8 ; A
	loadsp 8 ; B
	im 0 ; C

.LmoreMult:
	mult1bit
	
	; cutoff
	loadsp 8
	.byte (.LmoreMult-.Lbranch)&0x7f+0x80
.Lbranch:
	neqbranch

	storesp 4
	storesp 4
	storesp 12
	storesp 4
	poppc

___mod:
	cimpl __modsi3
___div:
	cimpl __divsi3

        .globl ___zpu_interrupt_vector
        .weak ___zpu_interrupt_vector

___zpu_interrupt_vector:
	jsr _zpu_interrupt
	poppc

	.data
	.balign 4,0
_mask:
	.long 0x00ffffff
	.long 0xff00ffff
	.long 0xffff00ff
	.long 0xffffff00

	
	.globl _hardware
_hardware:
	.long 0
	.globl _cpu_config
_cpu_config:
	.long 0

OpenPOWER on IntegriCloud