-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforthish.inc
213 lines (198 loc) · 3.95 KB
/
forthish.inc
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
;; Forthish.inc
;; minimal Forth-like stack operation macros for 6502
;;
;; Copyright © 2022-2024 Micah J Cowan
;; All rights reserved
;;
;; Permission is hereby granted, free of charge, to any
;; person obtaining a copy of this software and associated
;; documentation files (the "Software"), to deal in the Software
;; without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to
;; whom the Software is furnished to do so, subject to the
;; following conditions:
;;
;; The above copyright notice and this permission notice shall
;; be included in all copies or substantial portions of the
;; Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
;; KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
;; WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
;; PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
;; OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
;; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
;; OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.ifndef FORTHISH_INC_
FORTHISH_INC_ = 1
.macro maybe_import name
.ifndef FORTHISH_NO_IMPORT
.import name
.endif
.endmacro
;; dup_
;;
;; duplicate the top byte of the stack
.macro dup_
pla
pha
pha
.endmacro
;; swap_
;;
;; swaps the top two bytes of the stack.
.macro swap_
pla
tay
pla
tax
tya
pha
txa
pha
.endmacro
;; swapXY_
;;
;; swaps the byte at stack position marked by register X
;; with the byte at stack position marked by register Y
;;
;; Example:
;; Stack: A (X:)B C (Y:)D E
;; swapXY_
;; Stack: A (X:)D C (Y:)B E
.macro swapXY_
lda $100,x
pha
lda $100,y
sta $100,x
pla
sta $100,y
.endmacro
;; swapW_
;;
;; swaps the top two WORDS of the stack.
.macro swapW_
tsx
inx
txa
tay
iny
iny
swapXY_
inx
iny
swapXY_
.endmacro
;; pick_ N
;;
;; place a copy of the N-1st byte from top-of-stack
;; on the top of the stack. If M is specified,
;; copies M bytes back from that location, to the stack top.
.macro pick_ N, M
.scope
.ifnblank M
_M = M
.else
_M = 1
.endif
num = N+_M-1
tsx
.if num <= 5
.repeat num
inx
.endrepeat
.else
txa
clc
adc #num
tax
.endif
lda $101,x
pha
.if _M > 1
.repeat _M-1
dex
lda $101,x
pha
.endrepeat
.endif
.endscope
.endmacro
;; rot_
;;
;; Rotate the top 3 items of the stack one forward.
;; Equivalent to roll1 N, but faster.
.macro rot_
tsx
dup_
lda $102,x
sta $101,x
lda $103,x
sta $102,x
pla
sta $103,x
.endmacro
;; rotb_
;;
;; Rotate the top 3 items of the stack back one.
;; Very useful for getting at something that was
;; pushed just prior to a JSR.
.macro rotb_
tsx
dup_
lda $103,x
sta $101,x
lda $102,x
sta $103,x
pla
sta $102,x
.endmacro
;; roll1_ N
;;
;; Roll the top N bytes of the stack forward by one, wrapping
;; the topmost byte N-1 spaces back.
;;
;; Example:
;; Stack: A B C D E (top of stack)
;; roll_ 4
;; Stack: A E B C D
maybe_import roll1
maybe_import roll1A
.macro roll1_ N
lda #N
jsr roll1A
.endmacro
;; rollb1_ N
;;
;; Roll the top N bytes of the stack back by one, wrapping
;; the backmost byte to the front
;;
;; Example:
;; Stack: A B C D E (top of stack)
;; roll_ 4
;; Stack: A C D E B
maybe_import rollb1
maybe_import rollb1A
.macro rollb1_ N
lda #N
jsr rollb1A
.endmacro
;; copy_ N
;;
;; Roll the top N bytes of the stack back by one, wrapping
;; the backmost byte to the front
;;
;; Example:
;; Stack: A B C D E (top of stack)
;; roll_ 4
;; Stack: A C D E B
maybe_import copy
maybe_import copyA
.macro copy_ N
lda #N
jsr copyA
.endmacro
.delmacro maybe_import
.endif ; FORTHISH_INC_