Skip to content

Commit ee607c9

Browse files
committed
Set compile flags with existing hardware access assembly.
1 parent 98c0544 commit ee607c9

File tree

8 files changed

+57
-36
lines changed

8 files changed

+57
-36
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ The cross compiler includes some optimizations. More may be added later.
555555
* Tail calls. Words may be defined in assembly or forth. If the final word before an `EXIT` (or end of definition) is a forth word, this will instead jump to it. For example, a word that is compiled as `+ forth-word EXIT` will be optimized to `+ jump(forth-word)`. Smaller in token threaded model, faster, saves a stack slot.
556556

557557
To be added later:
558+
* Fallthrough forth words instead of tail call
558559
* Assembly inlining (subroutine threaded only)
559560
* Forth inlining
560561
* Forth common sequence compression

pkg/forth/builtin/11_misc.f

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
: UNHIDE ( xt -- )
1111
FALSE SWAP SET-HIDDEN
1212
;
13+
14+
\ constants used for defining token threaded primitives
15+
0 CONSTANT TOKEN_NEXT_NONSTANDARD
16+
1 CONSTANT TOKEN_NEXT_NORMAL
17+
2 CONSTANT TOKEN_NEXT_SKIP_R2
18+
3 CONSTANT TOKEN_NEXT_SKIP_LOAD

pkg/forth/esp32/01_instructions.f

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
0x3ff48000 CONSTANT DR_REG_RTCCNTL_BASE
1212

1313
: RTC_ADDR_FIX ( addr -- addr-fixed )
14-
DR_REG_RTCCNTL_BASE - \ subtract off the offset
1514
2 RSHIFT \ divide by 4
1615
0x3FF AND \ remove extra bits
1716
;
@@ -28,12 +27,13 @@
2827
7 \ number of inputs
2928
;
3029

31-
: READ_RTC_ADDR_CHANGE ( addr low width -- addr high low )
30+
: READ_RTC_ADDR_CHANGE ( addr low width -- addr-fixed high low )
3231
>R >R
3332
RTC_ADDR_FIX
34-
R> R>
35-
SWAP DUP >R
36-
+ 1- R>
33+
R> R> ( addr-fixed low width )
34+
SWAP DUP >R ( addr-fixed width low R: low )
35+
+ 1 - ( addr-fixed high R: low )
36+
R> ( addr-fixed high low )
3737
;
3838

3939
: READ_RTC_REG.BUILDER ( addr low width "<spaces>name" -- )
@@ -46,15 +46,16 @@
4646
>R >R >R
4747
\ create the token threaded assembly
4848
READ_RTC_REG.BUILDER
49-
C" sub r3, r3, 1\nst r0, r3, 0\njump __next_skip_load" \ increase stack, store result, next
49+
C" sub r3, r3, 1\nst r0, r3, 0" \ increase stack, store result
5050
SWAP 1 + \ number of inputs
5151
\ get the inputs
5252
R> R> R>
5353
\ create the subroutine threaded assembly
5454
READ_RTC_REG.BUILDER
55-
C" sub r3, r3, 1\nst r0, r3, 0\nadd r2, r2, 1\njump r2" \ increase stack, store result, next
55+
C" sub r3, r3, 1\nst r0, r3, 0" \ increase stack, store result
5656
SWAP 1 + \ number of inputs
5757
ASSEMBLY-BOTH
58+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT
5859
;
5960

6061
: REG_WR.BUILDER ( addr high low data -- strn..str0 n )
@@ -71,12 +72,10 @@ \ create the subroutine threaded assembly
7172
9 \ number of inputs
7273
;
7374

74-
: WRITE_RTC_ADDR_CHANGE
75-
>R >R >R
76-
RTC_ADDR_FIX
77-
R> R> R>
78-
>R SWAP DUP >R
79-
+ 1- R> R>
75+
: WRITE_RTC_ADDR_CHANGE ( addr low width data -- addr high low data )
76+
>R \ push data onto return stack
77+
READ_RTC_ADDR_CHANGE \ fix the rest
78+
R> \ pop data
8079
;
8180

8281
: WRITE_RTC_REG.BUILDER
@@ -90,15 +89,12 @@ \ create the subroutine threaded assembly
9089
>R >R >R >R
9190
\ create the token threaded assembly
9291
WRITE_RTC_REG.BUILDER
93-
C" jump __next_skip_load"
94-
SWAP 1 +
9592
\ get the inputs
9693
R> R> R> R>
9794
\ create the subroutine threaded assembly
9895
WRITE_RTC_REG.BUILDER
99-
C" add r2, r2, 1\njump r2"
100-
SWAP 1 +
101-
ASSEMBLY-BOTH
96+
ASSEMBLY-BOTH \ create the assembly
97+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT
10298
;
10399

104100
\ create an assembly word that writes to two RTC registers
@@ -111,16 +107,13 @@ \ create the subroutine threaded assembly
111107
WRITE_RTC_REG.BUILDER >C
112108
R> R> R> R>
113109
WRITE_RTC_REG.BUILDER C> +
114-
C" jump __next_skip_load"
115-
SWAP 1 +
116110
\ get the inputs
117111
R> R> R> R> R> R> R> R>
118112
\ create the subroutine threaded assembly
119113
>R >R >R >R
120114
WRITE_RTC_REG.BUILDER >C
121115
R> R> R> R>
122116
WRITE_RTC_REG.BUILDER C> +
123-
C" add r2, r2, 1\njump r2"
124-
SWAP 1 +
125117
ASSEMBLY-BOTH
118+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT
126119
;

pkg/forth/esp32/10_clock.f

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
RTC_CNTL_TIME0_REG 16 16
2424
READ_RTC_REG.BUILDER >C
2525
\ store result and exit
26-
STRING" st r0, r3, 0\njump __next_skip_load"
26+
STRING" st r0, r3, 0"
2727
5 C> C> C> C> + + + + \ add up the strings and the built instructions
2828
ASSEMBLY RTC_CLOCK \ create RTC_CLOCK
29+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT
2930

3031
\ delay for d rtc_slow ticks
3132
: RTC_CLOCK_DELAY ( d -- )
@@ -52,13 +53,10 @@
5253

5354
\ create the token threaded
5455
BUSY_DELAY.BUILDER
55-
STRING" jump __next_skip_load"
56-
SWAP 1 +
5756
\ create the subroutine threaded
5857
BUSY_DELAY.BUILDER
59-
STRING" add r2, r2, 1\njump r2"
60-
SWAP 1 +
6158
ASSEMBLY-BOTH BUSY_DELAY
59+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT
6260

6361
: DELAY_MS ( n -- )
6462
BEGIN

pkg/forth/esp32/12_system.f

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323

2424
\ token threaded assembly
2525
WAKE.BUILDER
26-
STRING" jump __next_skip_load"
27-
SWAP 1 +
2826
\ subroutine threaded assembly
2927
WAKE.BUILDER
30-
STRING" add r2, r2, 1\njump r2"
31-
SWAP 1 +
3228
ASSEMBLY-BOTH WAKE
29+
TOKEN_NEXT_SKIP_LOAD LAST SET-ULP-ASM-NEXT

pkg/forth/esp32/20_serial.f

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,11 @@
5353
2DUP >R >R \ dup the input
5454
\ token threaded
5555
SERIAL.WRITE_CREATE.BUILDER
56-
C" jump __next_skip_r2"
57-
SWAP 1 +
5856
\ subroutine threaded
5957
R> R>
6058
SERIAL.WRITE_CREATE.BUILDER
61-
C" add r2, r2, 1\njump r2"
62-
SWAP 1 +
6359
ASSEMBLY-BOTH
60+
TOKEN_NEXT_SKIP_R2 LAST SET-ULP-ASM-NEXT
6461
;
6562

6663
\ these were found at 21 C with a logic analyzer

pkg/forth/primitive.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func PrimitiveSetup(vm *VirtualMachine) error {
235235
Ulp: ulpAsm,
236236
UlpSrt: PrimitiveUlpSrt{
237237
Asm: asmSrt,
238-
NonStandardNext: true,
238+
NonStandardNext: false,
239239
},
240240
Entry: &newEntry,
241241
},
@@ -1113,6 +1113,29 @@ func PrimitiveSetup(vm *VirtualMachine) error {
11131113
return nil
11141114
},
11151115
},
1116+
{
1117+
name: "SET-ULP-ASM-NEXT",
1118+
goFunc: func(vm *VirtualMachine, entry *DictionaryEntry) error {
1119+
cell0, err := vm.Stack.Pop()
1120+
if err != nil {
1121+
return PopError(err, entry)
1122+
}
1123+
cellAddr, ok := cell0.(CellAddress)
1124+
if !ok {
1125+
return EntryError(entry, "requires an address cell, found %s type %T", cell0, cell0)
1126+
}
1127+
value, err := vm.Stack.PopNumber()
1128+
if err != nil {
1129+
return JoinEntryError(err, entry, "could not get boolean")
1130+
}
1131+
word, ok := cellAddr.Entry.Word.(*WordPrimitive)
1132+
if !ok {
1133+
return EntryError(entry, "requires a primitive word")
1134+
}
1135+
word.Ulp.Next = TokenNextType(value)
1136+
return nil
1137+
},
1138+
},
11161139
{
11171140
name: "EXIT",
11181141
flag: Flag{
@@ -2425,6 +2448,11 @@ func parseAssembly(vm *VirtualMachine, entry *DictionaryEntry) ([]string, error)
24252448
asmStr := strings.Join(asm, "")
24262449
asmStr = strings.ReplaceAll(asmStr, "\\r", "\r")
24272450
asmStr = strings.ReplaceAll(asmStr, "\\n", "\n")
2451+
asmStr = strings.ReplaceAll(asmStr, "\\n\\n", "\n")
2452+
last := len(asmStr) - 1
2453+
if asmStr[last] == '\n' { // remove a final newline
2454+
asmStr = asmStr[:last]
2455+
}
24282456
asm = strings.Split(asmStr, "\n")
24292457
return asm, nil
24302458
}

pkg/forth/word.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ type PrimitiveGo func(*VirtualMachine, *DictionaryEntry) error
162162
type TokenNextType int
163163

164164
const (
165+
// update the constants in 11_misc.f if this list changes
165166
TokenNextNonstandard TokenNextType = iota
166167
TokenNextNormal
167168
TokenNextSkipR2

0 commit comments

Comments
 (0)