|
31 | 31 | # Contents |
32 | 32 | * [Building ulp-forth](#building-ulp-forth) |
33 | 33 | * [Using ulp-forth](#using-ulp-forth) |
| 34 | +* [Sharing memory](#sharing-memory) |
| 35 | +* [Threading models](#threading-models) |
34 | 36 | * [Assembly words](#assembly-words) |
35 | 37 | * [System words](#system-words) |
36 | 38 | * [Clock words](#clock-words) |
|
40 | 42 | * [Standard Core words](#standard-core-words) |
41 | 43 | * [Standard Core Extension words](#standard-core-extension-words) |
42 | 44 | * [Standard Double words](#standard-double-words) |
43 | | -# [Optimizations](#optimizations) |
| 45 | +* [Optimizations](#optimizations) |
44 | 46 |
|
45 | 47 | # Building ulp-forth |
46 | 48 |
|
@@ -68,21 +70,65 @@ The cross compiler can be run with `ulp-forth build`. The user should pass in th |
68 | 70 | * `--reserved` Number of reserved bytes for the ULP, for use with --assembly flag (default 8176). Note that the Espressif linker has a bug so has 12 less total bytes. Any space not used by code or data is used for the stacks. |
69 | 71 | * `--subroutine` Use the subroutine threading model, see the [threading models](#threading-models) section. |
70 | 72 |
|
| 73 | + |
| 74 | +# Sharing memory |
| 75 | + |
| 76 | +There are words that can be used to share memory with the esp32. When compiled with the `--custom_assembly` or `--assembly` flags, the output assembly will include the `.global` directive for the associated memory. This memory will not be optimized away. |
| 77 | + |
| 78 | +| Shared word | Equivalent word | |
| 79 | +| ------------------ | --------------- | |
| 80 | +| `GLOBAL-VARIABLE` | `VARIABLE` | |
| 81 | +| `GLOBAL-2VARIABLE` | `2VARIABLE` | |
| 82 | +| `GLOBAL-ALLOCATE` | `ALLOCATE` | |
| 83 | + |
| 84 | +Access should be done while holding the mutex, see the [System words](#system-words) section. |
| 85 | + |
| 86 | +Example: |
| 87 | + |
| 88 | +```forth |
| 89 | +global-variable example \ create a global variable named "example" |
| 90 | +
|
| 91 | +\ read an address while holding the mutex |
| 92 | +: global-@ ( address -- n ) |
| 93 | + mutex.take \ take ownership of the mutex |
| 94 | + @ \ read the value at the address |
| 95 | + mutex.give \ release the mutex |
| 96 | +; |
| 97 | +
|
| 98 | +\ write to an address while holding the mutex |
| 99 | +: global-! ( n address -- ) |
| 100 | + mutex.take \ take ownership of the mutex |
| 101 | + ! \ write the value to the address |
| 102 | + mutex.give \ release the mutex |
| 103 | +; |
| 104 | +
|
| 105 | +\ get the value at "example" |
| 106 | +: get-example ( -- ) |
| 107 | + example \ put the address of the memory onto the stack |
| 108 | + global-@ \ read it |
| 109 | +; |
| 110 | +
|
| 111 | +\ set the value at "example" |
| 112 | +: set-example ( n -- ) |
| 113 | + example \ put the address of the memory onto the stack |
| 114 | + global-! \ write to it |
| 115 | +; |
| 116 | +``` |
| 117 | + |
71 | 118 | # Threading models |
72 | 119 |
|
73 | | -There are two threading models for the output ULP code. This is forth definition of "threading" and is not the same as multithreading in other languages. It can be thought of as the execution environment. |
| 120 | +There are two threading models for the output ULP code. This is the forth definition of "threading" and is not the same as multithreading in other languages. It can be thought of as the execution environment. |
74 | 121 |
|
75 | | -Token threading is usually smaller and subroutine threading is usually bigger, but this can vary based on the program and optimizations. |
| 122 | +Token threading is usually smaller and subroutine threading is usually faster, but this can vary based on the program and optimizations. |
76 | 123 |
|
77 | 124 | ## Token threading (default) |
78 | | - |
79 | | -This is the default threading model. This uses a lightweight virtual machine to execute all forth words. This allows for some very compact code, but there is a speed penalty for the virtual machine. |
| 125 | +This uses a lightweight virtual machine to execute all forth words. This allows for some very compact code, but there is a speed penalty for the virtual machine. |
80 | 126 |
|
81 | 127 | Code using this is roughly 20% smaller than subroutine threaded code. |
82 | 128 |
|
83 | 129 | ## Subroutine threading |
84 | 130 |
|
85 | | -This can be enabled with the `--subroutine` flag. It copmiles all forth words into assembly subroutines. This is very fast while executing, but there is a size penalty. |
| 131 | +This can be enabled with the `--subroutine` flag. It compiles all forth words into assembly subroutines. This is very fast while executing, but there is a size penalty. |
86 | 132 |
|
87 | 133 | Code using this is roughly 20% faster than token threaded code. |
88 | 134 |
|
|
0 commit comments