| 1 |
Calling conventions, stack frame and zero page: |
| 2 |
|
| 3 |
The stack will first increment, then store values. |
| 4 |
Frame pointer is kept in AC3 and ZP "fp" (or HW FP). |
| 5 |
Stack pointer is kept in ZP "sp". |
| 6 |
Stack reference pointer is kept in ZP "spref". |
| 7 |
Stack reference pointer is the top of stack and used to restore stack |
| 8 |
after a function call. This offset on the stack is unallocated and used |
| 9 |
when calling subroutines (usually to store return address). |
| 10 |
|
| 11 |
0-17 Unused (by us) |
| 12 |
20-26 (Auto-increment), scratch |
| 13 |
27 Stack pointer (if no in hardware) |
| 14 |
30-37 (Auto-decrement), scratch |
| 15 |
40-47 Used by HW stack and MMPU |
| 16 |
50-377 Addresses for subroutines/... |
| 17 |
|
| 18 |
The normal registers (AC0-AC2) are all considered scratch registers. |
| 19 |
|
| 20 |
#ifdef 2bsd |
| 21 |
long data type is "pdp-endian", which means that the low address |
| 22 |
contains the high word, and the high address is the low word. |
| 23 |
The word itself is little-endian, as opposed to the HW addressing |
| 24 |
used on nova with byte instructions (nova3/nova4). |
| 25 |
#endif |
| 26 |
|
| 27 |
Register classes are assigned as: |
| 28 |
AC0-AC2: AREGs. |
| 29 |
AC0+AC1: BREG (long, concatenated). |
| 30 |
|
| 31 |
In byte code the right half of a word is the first byte (little-endian). |
| 32 |
This is different to how the hardware handle it. |
| 33 |
|
| 34 |
Stack frame layout (with frame pointer). 2+1 words to save on stack: |
| 35 |
Stack grows upward |
| 36 |
|
| 37 |
sp -> ! free ! 3 <- also spref |
| 38 |
! w2 ! 2 |
| 39 |
! w1 ! 1 |
| 40 |
fp -> ! ospref! 0 |
| 41 |
! ofp ! -1 |
| 42 |
! arg0 ! -2 <- stack pointer when enter new function |
| 43 |
! arg1 ! -3 |
| 44 |
ospref->! opc ! -4 |
| 45 |
|
| 46 |
Return values are in ac0 (and ac1). |
| 47 |
|
| 48 |
The prolog/epilog (csav/cret) are implemented as subroutines, called |
| 49 |
from the ZP dispatcher (for overlay handling). |
| 50 |
Both can be omitted if the function do not need it, but the FP in AC3 |
| 51 |
must be restored. |
| 52 |
|
| 53 |
sp is either in loc 027 (auto-inc) or in HW stack. |
| 54 |
|
| 55 |
In ZP (from srt0.s) a bunch of words are defined: |
| 56 |
.zrel |
| 57 |
spref: .word 0 |
| 58 |
fp: .word 0 |
| 59 |
curseg: .word 0 |
| 60 |
C0: .word 0 |
| 61 |
C1: .word 1 |
| 62 |
... |
| 63 |
|
| 64 |
calling function 2-arg: (cleanup done in return function) |
| 65 |
sta arg2,@sp / push arg2 |
| 66 |
sta arg1,@sp / push arg1 |
| 67 |
jsr @[.word _fun] / jsr @[.word _fun] |
| 68 |
|
| 69 |
|
| 70 |
in function: (2 words needed on stack) |
| 71 |
_fun: .word 2 / .word 2 |
| 72 |
... |
| 73 |
jmp @cret / jmp @cret |
| 74 |
|
| 75 |
|
| 76 |
"Thunks" generated by the linker in non-overlayed space |
| 77 |
_t_foo: lda 0,C1 # new segment in ac0 |
| 78 |
lda 2,.+2 # destination function address in ac2 |
| 79 |
jmp segcmn |
| 80 |
.word _foo |
| 81 |
|
| 82 |
Switch segment (if needed) and setup for new function |
| 83 |
segcmn: lda 1,curseg |
| 84 |
sta 0,curseg # update before call |
| 85 |
sub# 0,1,szr |
| 86 |
nioc mmu # supervisor call if changing segment |
| 87 |
csav: |
| 88 |
lda 1,0,3 / lda 1,0,3 # fetch # words needed on stack |
| 89 |
inc 3,2 / inc 3,2 # put return address in ac2 |
| 90 |
|
| 91 |
sta 0,@sp / push 0 # store old seg |
| 92 |
lda 0,fp / mffp 0 # fetch fp |
| 93 |
sta 0,@sp / push 0 # ...and push |
| 94 |
lda 0,spref / lda 0,spref # fetch spref |
| 95 |
sta 0,@sp / push 0 # ...and push |
| 96 |
lda 3,sp / mfsp 3 # fetch sp |
| 97 |
sta 3,fp / mtfp 3 # save sp as new fp |
| 98 |
|
| 99 |
add 3,1 / add 3,1 # calc new stack pointer |
| 100 |
sta 1,sp / mtsp 1 # store |
| 101 |
sta 1,spref / sta 1,spref # store spref as well |
| 102 |
jmp 0,2 / jmp 0,2 # return |
| 103 |
|
| 104 |
cret: |
| 105 |
lda 2,0,3 / lda 2,0,3 # old sp/spref |
| 106 |
sta 2,spref / sta 2,spref |
| 107 |
lda 3,-1,3 / lda 3,-1,3 # old fp |
| 108 |
sta 3,fp / mtfp 3 # restore fp |
| 109 |
sta 2,sp / mtsp 2 # cleanup stack |
| 110 |
jmp @0,2 / jmp @0,2 |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
# little-endian bytes. |
| 115 |
# load byte, ptr in ac2, ret in ac0 |
| 116 |
lbyte: sta 3,@spref # save return address |
| 117 |
|
| 118 |
lda 1,[377] # get byte mask |
| 119 |
movr 2,2,szc # skip next if right word |
| 120 |
movs 1,1 # swap mask |
| 121 |
lda 0,,2 # load word into ac0 |
| 122 |
and 1,0,szc # mask our wanted word |
| 123 |
movs 0,0 # swap back if left |
| 124 |
|
| 125 |
lda 3,fp # restore ac3 |
| 126 |
jmp 0,@spref # get back |
| 127 |
|
| 128 |
# save byte, ptr in ac2, byte in right half of ac0 |
| 129 |
sbyte: sta 3,@spref |
| 130 |
|
| 131 |
lda 3,[377] # mask |
| 132 |
and 3,0 # Ensure ac0 high byte is clear |
| 133 |
|
| 134 |
movr 2,2,szc # skip next if left half |
| 135 |
movs 0,0,skp # right half; swap input byte |
| 136 |
movs 3,3 # left half; swap mask |
| 137 |
|
| 138 |
lda 1,,2 # get word |
| 139 |
and 3,1 # mask out saved half |
| 140 |
add 0,1 # merge in other half |
| 141 |
sta 1,2 # write back |
| 142 |
|
| 143 |
lda 3,fp |
| 144 |
jmp @spref |
| 145 |
|
| 146 |
# shift ac0 left ac1 times |
| 147 |
shl: sta 3,@spref |
| 148 |
neg 1,1,snr # negate count. |
| 149 |
jmp out # done if no shift |
| 150 |
|
| 151 |
movzl 0,0 # shift left |
| 152 |
inc 1,1,szr # done? |
| 153 |
jmp .-2 |
| 154 |
out: |
| 155 |
lda 3,fp |
| 156 |
jmp @spref |
| 157 |
|
| 158 |
# struct assignment |
| 159 |
# on entry: src in ac2, dst in ac0, negative cnt in ac1 |
| 160 |
__stcpy: |
| 161 |
sta 3,@spref |
| 162 |
lda 3,fp |
| 163 |
sta 2,20 # dest |
| 164 |
sta 0,21 # src |
| 165 |
dsz 20 |
| 166 |
dsz 21 |
| 167 |
|
| 168 |
again: |
| 169 |
lda 0,@20 # inc first, then load |
| 170 |
sta 0,@21 # inc first, then store |
| 171 |
inc 1,1,szr |
| 172 |
jmp again |
| 173 |
jmp @spref |