-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabersoftforth2branches.fsb
263 lines (203 loc) · 7.75 KB
/
abersoftforth2branches.fsb
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
.( AbersoftForth2branches )
\ abersoftforth2branches.fsb
\ Self-disassembly tool for ZX Spectrum Abersoft Forth
\ This file is part of
\ Abersoft Forth disassembled
\ <http://programandala.net.en.program.abersoft_forth.html>.
\ Copyright (C) 2015 Marcos Cruz (programandala.net)
\ Copying and distribution of this file, with or without
\ modification, are permitted in any medium without royalty
\ provided the copyright notice and this notice are
\ preserved. This file is offered as-is, without any
\ warranty.
\ -----------------------------------------------------------
\ Description
\ This tool prints out a Vim program that substitutes the
\ branches of Forth colon words with its definitive format.
\
\ The printout, processed by Vim, is used to recreate the
\ original Z80 source of Abersoft Forth. Ssee <README.adoc>
\ and <Makefile> for more details.
\ -----------------------------------------------------------
\ Requirements
needs +caseins caseins needs /string strings
needs save-string csb needs s+ s-plus
needs ?exit qexit needs 128k? 48kq
\ -----------------------------------------------------------
\ History
\ 2015-05-30: Start, with code extrated from the deprecated
\ tool `dis`.
\
\ 2015-05-31: Fix: `editor` is immediate! It needed a
\ `[compile]`.
\
\ 2015-06-01: Fix: the loop in `print-colon` didn't processed
\ the last word of the definition.
\
\ -----------------------------------------------------------
forth
\ <------------------------------>
128k? dup ?\ ." ERROR: this program needs a" cr
?\ ." ZX Spectrum 128 or later." cr quit
definitions -->
( Boot )
vocabulary print-voc immediate print-voc definitions hex
: printer-width ( b -- ) 5B64 ! ; \ ZX Spectrum 128 only
: program-name ( -- ) ." AbersoftForth2branches" ;
0 variable current-pfa
0 variable mode
\ Print mode:
\ 0 for Vim command to convert branches
\ 1 for destination branch symbols
03 0A thru cls program-name cr cr usage
( usage )
: usage ( -- )
\ <------------------------------>
." If you have not launched this" cr
." program with the provided" cr
." boot shell file for Fuse, first" cr
." make sure the printout file of" cr
." your emulator is empty." cr cr
\ <------------------------------>
." Type 'sym' to print out a" cr
." z80dasm symbols file with the" cr
." branch destination addresses." cr cr
\ <------------------------------>
." Type 'vim' to print out a Vim" cr
." program that tidies the Forth" cr
." branches of the Z80 source." cr quit ;
\ <------------------------------>
( Labels )
: id ( nfa -- ca len )
\ Return the label name of a branch word,
\ as used in the target Z80 source. This way
\ the printout doesn't need to be post-processed.
case
[ ' branch nfa ] literal of s" branch" endof
[ ' 0branch nfa ] literal of s" zero_branch" endof
[ ' (loop) nfa ] literal of s" paren_loop" endof
[ ' (+loop) nfa ] literal of s" paren_plus_loop" endof
cr ." ERROR: Not a branch word" cr quit
endcase ;
: hexa ( a -- ca len )
base @ >r hex s->d <# # # # # #> r> base ! ;
: hexa. ( a -- ) ." 0x" hexa type ;
: cfa-label$ ( cfa -- ca len ) 2+ nfa id s" _cfa" s+ ;
: cfa-label ( cfa -- ) cfa-label$ type ;
: ~ ( -- )
\ Start a comment line of the output file.
cr mode @ if [char] ; else [char] " then emit space ;
( branch-symbol vim-command )
: destination ( pfa+n1 -- pfa+n2 )
\ pfa+n1 = address of a relative jump literal
\ pfa+n2 = address of the correspondant destination address
dup @ + ;
: branch-label ( a -- ) ." branch_destination_" hexa. ;
: branch-symbol ( pfa+n -- pfa+n+2 )
\ Print out a Z80 symbol definition
\ for a Forth branch destination address.
\ pfa+n = address of a branch word
\ pfa+n+2 = address of its relative jump literal
2+ dup destination dup branch-label ." : equ " hexa. ;
: vim-command ( pfa+n -- pfa+n+2 )
\ Print out a Vim command that tidies a Forth branch
\ in the Z80 source.
\ pfa+n = address of a branch word
\ pfa+n+2 = address of its relative jump literal
~ ." Branch word at " dup hexa.
." in " current-pfa @ nfa id.
cr ." if search('defw " dup @ dup >r cfa-label ." \s;\s"
dup hexa. 2+ ." \n\s\+defw " dup @ hexa. ." $','wc')"
cr ." silent s`\(defw " r> cfa-label
." \)\s;\s0x[A-F0-9]\{4}\n\s\+defw \(" dup @ hexa.
." \)$`\1,\2 ; to " dup destination branch-label ." `" cr
." else" cr ." echoerr 'Branch not found'" cr ." wq" cr
." endif" cr ;
( print-branch colon-end? )
: print-branch ( pfa+n -- pfn+n+2 )
\ pfa+n = address of a branch word
\ pfa+n+2 = address of its relative jump literal
cr mode @ if branch-symbol else vim-command then ;
0 constant addr \ used as local variable
: colon-end? ( a -- f )
\ Is the given address the end of a colon definition?
' addr ! true
addr @ [ ' ;s cfa ] literal = ?exit
addr @ [ ' (;code) cfa ] literal = ?exit
addr 6E08 = ?exit \ end of COLD (original, not patched)
addr 6DAE = ?exit \ end of WARM
addr 6D91 = ?exit \ end of ABORT (original, not patched)
addr 6C99 = ?exit \ end of INTERPRET
addr 6B2A = ?exit \ end of ERROR
addr 6D4C = ?exit \ end of QUIT
0= ;
( print-colon )
: skip-string ( pfa+n1 -- pfa+n1+n2 ) 2+ dup c@ + 1- ;
: (print-colon) ( pfa+n1 -- pfa+n1 | pfa+n2 )
dup @ case
[ ' compile cfa ] literal of 2+ endof
[ ' lit cfa ] literal of 2+ endof
[ ' branch cfa ] literal of print-branch endof
[ ' 0branch cfa ] literal of print-branch endof
[ ' (loop) cfa ] literal of print-branch endof
[ ' (+loop) cfa ] literal of print-branch endof
[ ' (.") cfa ] literal of skip-string endof
endcase ;
: print-colon ( pfa --- )
\ Print branches of a colon definition.
\ XXX OLD -- buggy
\ dup current-pfa ! begin dup colon-end? 0=
\ while (print-colon) 2+
\ repeat drop ;
\ XXX NEW
dup current-pfa !
begin dup colon-end? >r (print-colon) 2+ r> until drop ;
( print-pfa )
: colon? ( pfa -- f ) cfa @ ' : cfa @ = ;
: (print-pfa) ( pfa --- )
\ Print branches of a word.
dup colon? if print-colon exit then drop ;
: print-pfa ( pfa -- )
\ Print a word, if it belongs to the original system.
dup [ ' udg 1+ ] literal u<
if (print-pfa) else drop then ;
: print-from-nfa ( nfa -- )
\ Print branches of words starting from a word.
begin pfa dup print-pfa lfa @ dup 0= ?terminal or
until drop ;
: print-context ( -- )
\ Print branches of the context vocabulary,
\ and the vocabularies it's chained to.
context @ @ print-from-nfa ;
( done )
: done ( -- )
cls
\ <------------------------------>
." Done." cr cr
." If you have not launched this" cr
." program with the provided" cr
." boot shell file for Fuse, copy" cr
." the printout of your emulator" cr
." to the file "
mode @ if cr ." <z80dasm_symbols.branches.z80s>."
else ." <tidy_branches.vim>." then cr
." Then close the emulator and use" cr
." <make> to disassemble the code." cr ;
\ <------------------------------>
( run )
: file-header ( -- )
~ ." This file was automatically created by " program-name
~ ." This file is part of Abersoft Forth disassembled"
~ ." By Marcos Cruz (programandala.net), 2015"
~ ." http://programandala.net/en.program.abersoft_forth.html"
cr cr ;
: run ( n -- )
\ n = 0: Print out Vim commands to convert branches.
\ 1: Print out destination branch symbols.
mode !
hex FF printer-width 1 link file-header
[compile] editor print-context [compile] print-voc
0 link decimal cr done ;
: vim ( -- ) 0 run ;
: sym ( -- ) 1 run ;
\ vim: filetype=abersoftforthafera