You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-2Lines changed: 10 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@
4
4
5
5
[FiveForths](https://github.com/aw/fiveforths) is a tiny [Forth](https://www.forth.com/starting-forth/) written in hand-coded RISC-V assembly, initially designed to run on the 32-bit [Longan Nano](https://longan.sipeed.com/en/) (GD32VF103) microcontroller.
_FiveForths_ currently uses the _indirect threading_ model and only has 19 built-in primitive words. It is 100% fully functional and can be extended by adding new primitives (in Assembly) or by defining new words (in Forth). This implementation is loosely inspired by [sectorforth](https://github.com/cesarblum/sectorforth), [jonesforth](https://github.com/nornagon/jonesforth), and [derzforth](https://github.com/theandrew168/derzforth).
8
10
9
11
Development progress has been logged regularly in the [devlogs](https://aw.github.io/fiveforths/).
@@ -21,8 +23,8 @@ Development progress has been logged regularly in the [devlogs](https://aw.githu
21
23
22
24
The quickest way to get started is to download and flash one of the firmware binaries listed below:.
Accessing _FiveForths_ through the terminal should look similar to this:
94
95
95
96
```
97
+
$ pyserial-miniterm --eol LF /dev/ttyUSB0 115200
96
98
--- Miniterm on /dev/ttyUSB0 115200,8,N,1 ---
97
99
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
98
-
FiveForths v0.3, Copyright (c) 2021~ Alexander Williams, https://a1w.ca
100
+
FiveForths v0.4, Copyright (c) 2021~ Alexander Williams, https://a1w.ca
99
101
100
102
```
101
103
102
-
Some basic words can then be defined (borrowed from [derzforth prelude.forth](https://github.com/theandrew168/derzforth/blob/main/lexicons/prelude.forth)):
104
+
Some basic words can then be defined (borrowed from [sectorforth hello-world](https://github.com/cesarblum/sectorforth/blob/master/examples/01-helloworld.f) and [planckforth bootstrap](https://github.com/nineties/planckforth/blob/main/bootstrap.fs)):
Of course, it is possible to define many other words to suit your needs.
127
129
130
+
### Toggle an LED
131
+
132
+
The following code can be used to turn on the green and blue LEDs on GPIOA pins 1 and 2:
133
+
134
+
```
135
+
: green_led_on 0x40010800 @ 0xFFFFFF0F and 0x00000030 or 0x40010800 ! ;
136
+
: blue_led_on 0x40010800 @ 0xFFFFF0FF and 0x00000300 or 0x40010800 ! ;
137
+
green_led_on
138
+
blue_led_on
139
+
```
140
+
141
+
And to turn off the same LEDs:
142
+
143
+
```
144
+
: green_led_off 0x40010800 @ 0xFFFFFF0F and 0x00000040 or 0x40010800 ! ;
145
+
: blue_led_off 0x40010800 @ 0xFFFFF0FF and 0x00000400 or 0x40010800 ! ;
146
+
green_led_off
147
+
blue_led_off
148
+
```
149
+
150
+
This requires the above defined words: `invert, over, swap, and, or`.
151
+
152
+
To explain the values:
153
+
154
+
*`0x40010800`: GPIOA base address with offset `0x00` for `CTL0` pins 0-7 (would be `CTL1` with offset `0x04` for pins 8-15).
155
+
*`0xFFFFF0FF`: mask to clear GPIO pin 2 (would be the same for GPIO pin 10, while GPIO pin 1 would be `0xFFFFFF0F` and GPIO pin 8 would be `0xFFFFFFF0`).
156
+
*`0x00000030`: GPIO pin 1 setting `0b0011` which is `push-pull output, max speed 50MHz`.
157
+
*`0x00000040`: GPIO pin 1 setting `0b0100` which is `floating input`.
158
+
*`0x00000300`: GPIO pin 2 setting `0b0011` which is `push-pull output, max speed 50MHz`.
159
+
*`0x00000400`: GPIO pin 2 setting `0b0100` which is `floating input`.
160
+
161
+
The code above uses those pre-calculated values to read the existing GPIOA config from a memory address (with `@`), apply a mask (with `and`), apply the new config (with `or`), then store it back to the memory address (with `!`), thus writing the new GPIOA which toggles the pins low/high (active-low, therefore low turns on the LED, high turns it off).
162
+
128
163
### Adding primitives
129
164
130
165
New primitives can be written in RISC-V Assembly. It is recommended to add them to a **new file** and then include the file at _the end_ of `fiveforths.s`:
0 commit comments