-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapage.c
1970 lines (1725 loc) · 64.8 KB
/
datapage.c
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 : datapage.c
PURPOSE : paged data access runtime routines
MACHINE : Freescale 68HC12 (Target)
LANGUAGE : ANSI-C
HISTORY : 21.7.96 first version created
******************************************************************************/
#include "hidef.h"
#include "non_bank.sgm"
#include "runtime.sgm"
#ifndef __HCS12X__ /* it's different for the HCS12X. See the text below at the #else // __HCS12X__ */
/*
According to the -Cp option of the compiler the
__DPAGE__, __PPAGE__ and __EPAGE__ macros are defined.
If none of them is given as argument, then no page accesses should occur and
this runtime routine should not be used !
To be on the save side, the runtime routines are created anyway.
If some of the -Cp options are given an adapted versions which only covers the
needed cases is produced.
*/
/* if no compiler option -Cp is given, it is assumed that all possible are given : */
/* Compile with option -DHCS12 to activate this code */
#if defined(HCS12) || defined(_HCS12) || defined(__HCS12__) /* HCS12 family has PPAGE register only at 0x30 */
#define PPAGE_ADDR (0x30+REGISTER_BASE)
#ifndef __PPAGE__ /* may be set already by option -CPPPAGE */
#define __PPAGE__
#endif
/* Compile with option -DDG128 to activate this code */
#elif defined DG128 /* HC912DG128 derivative has PPAGE register only at 0xFF */
#define PPAGE_ADDR (0xFF+REGISTER_BASE)
#ifndef __PPAGE__ /* may be set already by option -CPPPAGE */
#define __PPAGE__
#endif
#elif defined(HC812A4)
/* all setting default to A4 already */
#endif
#if !defined(__EPAGE__) && !defined(__PPAGE__) && !defined(__DPAGE__)
/* as default use all page registers */
#define __DPAGE__
#define __EPAGE__
#define __PPAGE__
#endif
/* modify the following defines to your memory configuration */
#define EPAGE_LOW_BOUND 0x400u
#define EPAGE_HIGH_BOUND 0x7ffu
#define DPAGE_LOW_BOUND 0x7000u
#define DPAGE_HIGH_BOUND 0x7fffu
#define PPAGE_LOW_BOUND (DPAGE_HIGH_BOUND+1)
#define PPAGE_HIGH_BOUND 0xBFFFu
#define REGISTER_BASE 0x0u
#ifndef DPAGE_ADDR
#define DPAGE_ADDR (0x34u+REGISTER_BASE)
#endif
#ifndef EPAGE_ADDR
#define EPAGE_ADDR (0x36u+REGISTER_BASE)
#endif
#ifndef PPAGE_ADDR
#define PPAGE_ADDR (0x35u+REGISTER_BASE)
#endif
/*
The following parts about the defines are assumed in the code of _GET_PAGE_REG :
- the memory region controlled by DPAGE is above the area controlled by the EPAGE and
below the area controlled by the PPAGE.
- the lower bound of the PPAGE area is equal to be the higher bound of the DPAGE area + 1
*/
#if EPAGE_LOW_BOUND >= EPAGE_HIGH_BOUND || EPAGE_HIGH_BOUND >= DPAGE_LOW_BOUND || DPAGE_LOW_BOUND >= DPAGE_HIGH_BOUND || DPAGE_HIGH_BOUND >= PPAGE_LOW_BOUND || PPAGE_LOW_BOUND >= PPAGE_HIGH_BOUND
#error /* please adapt _GET_PAGE_REG for this non default page configuration */
#endif
#if DPAGE_HIGH_BOUND+1 != PPAGE_LOW_BOUND
#error /* please adapt _GET_PAGE_REG for this non default page configuration */
#endif
/* this module does either control if any access is in the bounds of the specified page or */
/* ,if only one page is specified, just use this page. */
/* This behavior is controlled by the define USE_SEVERAL_PAGES. */
/* If !USE_SEVERAL_PAGES does increase the performance significantly */
/* NOTE : When !USE_SEVERAL_PAGES, the page is also set for accesses outside of the area controlled */
/* by this single page. But this is should not cause problems because the page is restored to the old value before any other access could occur */
#if !defined(__DPAGE__) && !defined(__EPAGE__) && !defined(__PPAGE__)
/* no page at all is specified */
/* only specifying the right pages will speed up these functions a lot */
#define USE_SEVERAL_PAGES 1
#elif defined(__DPAGE__) && defined(__EPAGE__) || defined(__DPAGE__) && defined(__PPAGE__) || defined(__EPAGE__) && defined(__PPAGE__)
/* more than one page register is used */
#define USE_SEVERAL_PAGES 1
#else
#define USE_SEVERAL_PAGES 0
#if defined(__DPAGE__) /* check which pages are used */
#define PAGE_ADDR PPAGE_ADDR
#elif defined(__EPAGE__)
#define PAGE_ADDR EPAGE_ADDR
#elif defined(__PPAGE__)
#define PAGE_ADDR PPAGE_ADDR
#else /* we do not know which page, decide it at runtime */
#error /* must not happen */
#endif
#endif
#if USE_SEVERAL_PAGES /* only needed for several pages support */
/*--------------------------- _GET_PAGE_REG --------------------------------
Runtime routine to detect the right register depending on the 16 bit offset part
of an address.
This function is only used by the functions below.
Depending on the compiler options -Cp different versions of _GET_PAGE_REG are produced.
Arguments :
- Y : offset part of an address
Result :
if address Y is controlled by a page register :
- X : address of page register if Y is controlled by an page register
- Zero flag cleared
- all other registers remain unchanged
if address Y is not controlled by a page register :
- Zero flag is set
- all registers remain unchanged
--------------------------- _GET_PAGE_REG ----------------------------------*/
#if defined(__DPAGE__)
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
static void NEAR _GET_PAGE_REG(void) { /*lint -esym(528, _GET_PAGE_REG) used in asm code */
__asm {
L_DPAGE:
CPY #DPAGE_LOW_BOUND ;// test of lower bound of DPAGE
#if defined(__EPAGE__)
BLO L_EPAGE ;// EPAGE accesses are possible
#else
BLO L_NOPAGE ;// no paged memory below accesses
#endif
CPY #DPAGE_HIGH_BOUND ;// test of higher bound DPAGE/lower bound PPAGE
#if defined(__PPAGE__)
BHI L_PPAGE ;// EPAGE accesses are possible
#else
BHI L_NOPAGE ;// no paged memory above accesses
#endif
FOUND_DPAGE:
LDX #DPAGE_ADDR ;// load page register address and clear zero flag
RTS
#if defined(__PPAGE__)
L_PPAGE:
CPY #PPAGE_HIGH_BOUND ;// test of higher bound of PPAGE
BHI L_NOPAGE
FOUND_PPAGE:
LDX #PPAGE_ADDR ;// load page register address and clear zero flag
RTS
#endif
#if defined(__EPAGE__)
L_EPAGE:
CPY #EPAGE_LOW_BOUND ;// test of lower bound of EPAGE
BLO L_NOPAGE
CPY #EPAGE_HIGH_BOUND ;// test of higher bound of EPAGE
BHI L_NOPAGE
FOUND_EPAGE:
LDX #EPAGE_ADDR ;// load page register address and clear zero flag
RTS
#endif
L_NOPAGE:
ORCC #0x04 ;// sets zero flag
RTS
}
}
#else /* !defined(__DPAGE__) */
#if defined( __PPAGE__ )
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
static void NEAR _GET_PAGE_REG(void) { /*lint -esym(528, _GET_PAGE_REG) used in asm code */
__asm {
L_PPAGE:
CPY #PPAGE_LOW_BOUND ;// test of lower bound of PPAGE
#if defined( __EPAGE__ )
BLO L_EPAGE
#else
BLO L_NOPAGE ;// no paged memory below
#endif
CPY #PPAGE_HIGH_BOUND ;// test of higher bound PPAGE
BHI L_NOPAGE
FOUND_PPAGE:
LDX #PPAGE_ADDR ;// load page register address and clear zero flag
RTS
#if defined( __EPAGE__ )
L_EPAGE:
CPY #EPAGE_LOW_BOUND ;// test of lower bound of EPAGE
BLO L_NOPAGE
CPY #EPAGE_HIGH_BOUND ;// test of higher bound of EPAGE
BHI L_NOPAGE
FOUND_EPAGE:
LDX #EPAGE_ADDR ;// load page register address and clear zero flag
RTS
#endif
L_NOPAGE: ;// not in any allowed page area
;// its a far access to a non paged variable
ORCC #0x04 ;// sets zero flag
RTS
}
}
#else /* !defined(__DPAGE__ ) && !defined( __PPAGE__) */
#if defined(__EPAGE__)
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
static void NEAR _GET_PAGE_REG(void) { /*lint -esym(528, _GET_PAGE_REG) used in asm code */
__asm {
L_EPAGE:
CPY #EPAGE_LOW_BOUND ;// test of lower bound of EPAGE
BLO L_NOPAGE
CPY #EPAGE_HIGH_BOUND ;// test of higher bound of EPAGE
BHI L_NOPAGE
FOUND_EPAGE:
LDX #EPAGE_ADDR ;// load page register address and clear zero flag
RTS
L_NOPAGE: ;// not in any allowed page area
;// its a far access to a non paged variable
ORCC #0x04 ;// sets zero flag
RTS
}
}
#endif /* defined(__EPAGE__) */
#endif /* defined(__PPAGE__) */
#endif /* defined(__DPAGE__) */
#endif /* USE_SEVERAL_PAGES */
/*--------------------------- _SET_PAGE --------------------------------
Runtime routine to set the right page register. This routine is used if the compiler
does not know the right page register, i.e. if the option -Cp is used for more than
one page register or if the runtime option is used for one of the -Cp options.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
Result :
- page part written into the correct page register.
- the old page register content is destroyed
- all processor registers remains unchanged
--------------------------- _SET_PAGE ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _SET_PAGE(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
STAB 0,X ;// set page register
L_NOPAGE:
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
STAB PAGE_ADDR ;// set page register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _LOAD_FAR_8 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
Result :
- value to be read in the B register
- all other registers remains unchanged
- all page register still contain the same value
--------------------------- _LOAD_FAR_8 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _LOAD_FAR_8(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHA ;// save A register
LDAA 0,X ;// save page register
STAB 0,X ;// set page register
LDAB 0,Y ;// actual load, overwrites page
STAA 0,X ;// restore page register
PULA ;// restore A register
PULX ;// restore X register
RTS
L_NOPAGE:
LDAB 0,Y ;// actual load, overwrites page
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHA ;// save A register
LDAA PAGE_ADDR ;// save page register
STAB PAGE_ADDR ;// set page register
LDAB 0,Y ;// actual load, overwrites page
STAA PAGE_ADDR ;// restore page register
PULA ;// restore A register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _LOAD_FAR_16 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
Result :
- value to be read in the Y register
- all other registers remains unchanged
- all page register still contain the same value
--------------------------- _LOAD_FAR_16 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _LOAD_FAR_16(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHA ;// save A register
LDAA 0,X ;// save page register
STAB 0,X ;// set page register
LDY 0,Y ;// actual load, overwrites address
STAA 0,X ;// restore page register
PULA ;// restore A register
PULX ;// restore X register
RTS
L_NOPAGE:
LDY 0,Y ;// actual load, overwrites address
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHA ;// save A register
LDAA PAGE_ADDR ;// save page register
STAB PAGE_ADDR ;// set page register
LDY 0,Y ;// actual load, overwrites address
STAA PAGE_ADDR ;// restore page register
PULA ;// restore A register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _LOAD_FAR_24 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
Result :
- value to be read in the Y:B registers
- all other registers remains unchanged
- all page register still contain the same value
--------------------------- _LOAD_FAR_24 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _LOAD_FAR_24(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHA ;// save A register
LDAA 0,X ;// save page register
STAB 0,X ;// set page register
LDAB 0,Y ;// actual load, overwrites page of address
LDY 1,Y ;// actual load, overwrites offset of address
STAA 0,X ;// restore page register
PULA ;// restore A register
PULX ;// restore X register
RTS
L_NOPAGE:
LDAB 0,Y ;// actual load, overwrites page of address
LDY 1,Y ;// actual load, overwrites offset of address
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHA ;// save A register
LDAA PAGE_ADDR ;// save page register
STAB PAGE_ADDR ;// set page register
LDAB 0,Y ;// actual load, overwrites page of address
LDY 1,Y ;// actual load, overwrites offset of address
STAA PAGE_ADDR ;// restore page register
PULA ;// restore A register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _LOAD_FAR_32 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
Result :
- low 16 bit of value to be read in the D registers
- high 16 bit of value to be read in the Y registers
- all other registers remains unchanged
- all page register still contain the same value
--------------------------- _LOAD_FAR_32 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _LOAD_FAR_32(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
LDAA 0,X ;// save page register
PSHA ;// put it onto the stack
STAB 0,X ;// set page register
LDD 2,Y ;// actual load, low word
LDY 0,Y ;// actual load, high word
MOVB 1,SP+,0,X ;// restore page register
PULX ;// restore X register
RTS
L_NOPAGE:
LDD 2,Y ;// actual load, low word
LDY 0,Y ;// actual load, high word
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
LDAA PAGE_ADDR ;// save page register
PSHA ;// put it onto the stack
STAB PAGE_ADDR ;// set page register
LDD 2,Y ;// actual load, low word
LDY 0,Y ;// actual load, high word
MOVB 1,SP+,PAGE_ADDR ;// restore page register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _STORE_FAR_8 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
- value to be stored in the B register
Result :
- value stored at the address
- all registers remains unchanged
- all page register still contain the same value
--------------------------- _STORE_FAR_8 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _STORE_FAR_8(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHB ;// save B register
LDAB 0,X ;// save page register
MOVB 0,SP, 0,X ;// set page register
STAA 0,Y ;// store the value passed in A
STAB 0,X ;// restore page register
PULB ;// restore B register
PULX ;// restore X register
RTS
L_NOPAGE:
STAA 0,Y ;// store the value passed in A
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHB ;// save A register
LDAB PAGE_ADDR ;// save page register
MOVB 0,SP,PAGE_ADDR ;// set page register
STAA 0,Y ;// store the value passed in A
STAB PAGE_ADDR ;// restore page register
PULB ;// restore B register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _STORE_FAR_16 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
- value to be stored in the X register
Result :
- value stored at the address
- all registers remains unchanged
- all page register still contain the same value
--------------------------- _STORE_FAR_16 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _STORE_FAR_16(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHA
LDAA 0,X ;// save page register
STAB 0,X ;// set page register
MOVW 1,SP,0,Y ;// store the value passed in X
STAA 0,X ;// restore page register
PULA ;// restore A register
PULX ;// restore X register
RTS
L_NOPAGE:
STX 0,Y ;// store the value passed in X
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHA ;// save A register
LDAA PAGE_ADDR ;// save page register
STAB PAGE_ADDR ;// set page register
STX 0,Y ;// store the value passed in X
STAA PAGE_ADDR ;// restore page register
PULA ;// restore A register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _STORE_FAR_24 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address in the B register
- value to be stored in the X:A registers (X : low 16 bit, A : high 8 bit)
Result :
- value stored at the address
- all registers remains unchanged
- all page register still contain the same value
--------------------------- _STORE_FAR_24 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _STORE_FAR_24(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHA
LDAA 0,X ;// save page register
STAB 0,X ;// set page register
MOVW 1,SP, 1,Y ;// store the value passed in X
MOVB 0,SP, 0,Y ;// store the value passed in A
STAA 0,X ;// restore page register
PULA ;// restore A register
PULX ;// restore X register
RTS
L_NOPAGE:
STX 1,Y ;// store the value passed in X
STAA 0,Y ;// store the value passed in X
PULX ;// restore X register
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHA ;// save A register
LDAA PAGE_ADDR ;// save page register
STAB PAGE_ADDR ;// set page register
MOVB 0,SP, 0,Y ;// store the value passed in A
STX 1,Y ;// store the value passed in X
STAA PAGE_ADDR ;// restore page register
PULA ;// restore A register
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _STORE_FAR_32 --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of an address in the Y register
- page part of an address is on the stack at 3,SP (just below the return address)
- value to be stored in the X:D registers (D : low 16 bit, X : high 16 bit)
Result :
- value stored at the address
- all registers remains unchanged
- the page part is removed from the stack
- all page register still contain the same value
--------------------------- _STORE_FAR_32 ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _STORE_FAR_32(void) {
#if USE_SEVERAL_PAGES
__asm {
PSHX ;// save X register
__PIC_JSR(_GET_PAGE_REG)
BEQ L_NOPAGE
PSHD
LDAA 0,X ;// save page register
MOVB 6,SP, 0,X ;// set page register
MOVW 2,SP, 0,Y ;// store the value passed in X (high word)
MOVW 0,SP, 2,Y ;// store the value passed in D (low word)
STAA 0,X ;// restore page register
PULD ;// restore A register
BRA done
L_NOPAGE:
MOVW 0,SP, 0,Y ;// store the value passed in X (high word)
STD 2,Y ;// store the value passed in D (low word)
done:
PULX ;// restore X register
MOVW 0,SP, 1,+SP ;// move return address
RTS
}
#else /* USE_SEVERAL_PAGES */
__asm {
PSHD ;// save D register
LDAA PAGE_ADDR ;// save page register
LDAB 4,SP ;// load page part of address
STAB PAGE_ADDR ;// set page register
STX 0,Y ;// store the value passed in X
MOVW 0,SP, 2,Y ;// store the value passed in D (low word)
STAA PAGE_ADDR ;// restore page register
PULD ;// restore D register
MOVW 0,SP, 1,+SP ;// move return address
RTS
}
#endif /* USE_SEVERAL_PAGES */
}
/*--------------------------- _FAR_COPY_RC --------------------------------
This runtime routine is used to access paged memory via a runtime function.
It may also be used if the compiler option -Cp is not used with the runtime argument.
Arguments :
- offset part of the source int the X register
- page part of the source in the A register
- offset part of the dest int the Y register
- page part of the dest in the B register
- number of bytes to be copied is defined by the next 2 bytes after the return address.
Result :
- memory area copied
- no registers are saved, i.e. all registers may be destroyed
- all page register still contain the same value as before the call
- the function returns after the constant defining the number of bytes to be copied
stack-structure at the loop-label:
0,SP : destination offset
2,SP : source page
3,SP : destination page
4,SP : source offset
6,SP : points to length to be copied. This function returns after the size
A usual call to this function looks like:
struct Huge src, dest;
; ...
LDX #src
LDAA #PAGE(src)
LDY #dest
LDAB #PAGE(dest)
JSR _FAR_COPY_RC
DC.W sizeof(struct Huge)
; ...
--------------------------- _FAR_COPY_RC ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _FAR_COPY_RC(void) {
#if USE_SEVERAL_PAGES
__asm {
DEX ;// source addr-=1, because loop counter ends at 1
PSHX ;// save source offset
PSHD ;// save both pages
DEY ;// destination addr-=1, because loop counter ends at 1
PSHY ;// save destination offset
LDY 6,SP ;// Load Return address
LDX 2,Y+ ;// Load Size to copy
STY 6,SP ;// Store adjusted return address
loop:
LDD 4,SP ;// load source offset
LEAY D,X ;// calculate actual source address
LDAB 2,SP ;// load source page
__PIC_JSR(_LOAD_FAR_8) ;// load 1 source byte
PSHB ;// save value
LDD 0+1,SP ;// load destination offset
LEAY D,X ;// calculate actual destination address
PULA ;// restore value
LDAB 3,SP ;// load destination page
__PIC_JSR(_STORE_FAR_8) ;// store one byte
DEX
BNE loop
LEAS 6,SP ;// release stack
_SRET ;// debug info only: This is the last instr of a function with a special return
RTS ;// return
}
#else
__asm {
PSHD ;// store page registers
TFR X,D
PSHY ;// temporary space
LDY 4,SP ;// load return address
ADDD 2,Y+ ;// calculate source end address. Increment return address
STY 4,SP
PULY
PSHD ;// store src end address
LDAB 2,SP ;// reload source page
LDAA PAGE_ADDR ;// save page register
PSHA
loop:
STAB PAGE_ADDR ;// set source page
LDAA 1,X+ ;// load value
MOVB 4,SP, PAGE_ADDR ;// set destination page
STAA 1,Y+
CPX 1,SP
BNE loop
LDAA 5,SP+ ;// restore old page value and release stack
STAA PAGE_ADDR ;// store it into page register
_SRET ;// debug info only: This is the last instr of a function with a special return
RTS
}
#endif
}
/*--------------------------- _FAR_COPY --------------------------------
The _FAR_COPY runtime routine was used to copied large memory blocks in previous compiler releases.
However this release now does use _FAR_COPY_RC instead. The only difference is how the size of
the area to be copied is passed into the function. For _FAR_COPY the size is passed on the stack just
above the return address. _FAR_COPY_RC does expect the return address just after the JSR _FAR_COPY_RC call
in the code of the caller. This allows for denser code calling _FAR_COPY_RC but does also need a slightly
larger runtime routine and it is slightly slower.
The _FAR_COPY routine is here now mainly for compatibility with previous releases.
The current compiler does not use it.
--------------------------- _FAR_COPY ----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
#pragma NO_ENTRY
#pragma NO_EXIT
#pragma NO_FRAME
void NEAR _FAR_COPY(void) {
#if USE_SEVERAL_PAGES
__asm {
DEX ;// source addr-=1, because loop counter ends at 1
PSHX ;// save source offset
PSHD ;// save both pages
DEY ;// destination addr-=1, because loop counter ends at 1
PSHY ;// save destination offset
LDX 8,SP ;// load counter, assuming counter > 0
loop:
LDD 4,SP ;// load source offset
LEAY D,X ;// calculate actual source address
LDAB 2,SP ;// load source page
__PIC_JSR(_LOAD_FAR_8) ;// load 1 source byte
PSHB ;// save value
LDD 0+1,SP ;// load destination offset
LEAY D,X ;// calculate actual destination address
PULA ;// restore value
LDAB 3,SP ;// load destination page
__PIC_JSR(_STORE_FAR_8) ;// store one byte
DEX
BNE loop
LDX 6,SP ;// load return address
LEAS 10,SP ;// release stack
JMP 0,X ;// return
}
#else
__asm {
PSHD ;// store page registers
TFR X,D
ADDD 4,SP ;// calculate source end address
STD 4,SP
PULB ;// reload source page
LDAA PAGE_ADDR ;// save page register
PSHA
loop:
STAB PAGE_ADDR ;// set source page
LDAA 1,X+ ;// load value
MOVB 1,SP, PAGE_ADDR ;// set destination page
STAA 1,Y+
CPX 4,SP
BNE loop
LDAA 2,SP+ ;// restore old page value and release stack
STAA PAGE_ADDR ;// store it into page register
LDX 4,SP+ ;// release stack and load return address
JMP 0,X ;// return
}
#endif
}
#else /* __HCS12X__ */
/*
The HCS12X knows two different kind of addresses:
- Logical addresses. E.g.
MOVB #page(var),RPAGE
INC var
- Global addresses E.g.
MOVB #page(var),GPAGE
GLDAA var
INCA
GSTAA var
Global addresses are used with G-Load's and G-Store's, logical addresses are used for all the other instructions
and occasions. As HC12's or HCS12's do not have the G-Load and G-Store instructions,
global addresses are not used with these processor families.
They are only used with HCS12X chips (and maybe future ones deriving from a HCS12X).
Logical and Global addresses can point to the same object, however the global and logical address of an object
are different for most objects (actually for all except the registers from 0 to 0x7FF).
Therefore the compiler needs to transform in between them.
HCS12X Pointer types:
The following are logical addresses:
- all 16 bit pointers
- "char* __near": always.
- "char *" in the small and banked memory model
- 24 bit dpage, epage, ppage or rpage pointers (*1) (note: the first HCS12X compilers may not support these pointer types)
- "char *__dpage": Note this type only exists for
orthogonality with the HC12 A4 chip which has a DPAGE reg.
It does not apply to the HCS12X.
- "char *__epage": 24 bit pointer using the EPAGE register
- "char *__ppage": 24 bit pointer using the PPAGE register.
As the PPAGE is also used for BANKED code,
using this pointer type is only legal from non banked code.
- "char *__rpage": 24 bit pointer using the RPAGE register
The following are global addresses:
"char*": in the large memory model (only HCS12X)
"char* __far": always for HCS12X.
(*1): For the HC12 and HCS12 "char* __far" and "char*" in the large memory model are also logical.
Some notes for the HC12/HCS12 programmers.
The address of a far object for a HC12 and for a HCS12X is different, even if they are at the same place in the memory map.
For the HC12, a far address is using the logical addresses, for the HCS12X however, far addresses are using global addresses.
This does cause troubles for the unaware!
The conversion routines implemented in this file support the special HCS12XE RAM mapping (when RAMHM is set).
To enable this mapping compile this file with the "-MapRAM" compiler option.
HCS12X Logical Memory map
Logical Addresses Used for shadowed at page register Global Address
0x000000 .. 0x0007FF Peripheral Registers Not Paged 0x000000
0x??0800 .. 0x??0BFF Paged EEPROM EPAGE (@0x17) 0x100000+EPAGE*0x0400