Arduino CoreForth
CoreForth-0 for Arduino Uno boards, with simple UART and LED functionality.
::host::
cr
ROM starts at 0x0000 and RAM at 0x4000. ROM is also mapped at 0x8000 for LD/ST instructions.
$00000000 to trom
$00004000 to tram
trom tdp !
tram tvp !
Start at 0x110 to leave space for system variables at 0x100
$110 torg
::itc:: include ../janus/host-itc-avr.ft
::itc:: include ../janus/target-itc-avr.ft
::stc:: include ../janus/host-stc-avr.ft
::stc:: include ../janus/target-stc-avr.ft
::target::
System variables
$8100 constant init-latest
$8102 constant init-dp
$7fff constant r0
$7fbf constant s0
Include the rest of the system:
: emit begin USART0.STATUS c@ %00100000 and until USART0.TXDATAL c! ;
: key? USART0.STATUS c@ %10000000 and ;
: key begin key? until USART0.RXDATAL c@ ;
::itc:: include ../compiler-itc.ft
::stc:: include ../compiler-stc.ft
Hardware initialization
Set the UART to 38400bps at 4MHz
: uart-init %00000011 PORTMUX.USARTROUTEA c!
#416 USART0.BAUD !
%00000011 USART0.CTRLC c!
%00010000 PORTD.DIRSET c!
%00100000 PORTD.DIRCLR c!
%11000000 USART0.CTRLB c! ;
LED at PF5
: led-init %00100000 PORTF.DIRSET c! ;
: +led %00100000 PORTF.OUTCLR c! ;
: ~led %00100000 PORTF.OUTTGL c! ;
: -led %00100000 PORTF.OUTSET c! ;
Initialize the UART and LED
: hw-init $00 NVMCTRL.CTRLB c! uart-init led-init ;
: setup-pointers
init-latest @ latest !
init-dp @ dp !
;
Declare a default application word which starts the interpreter. This can be overridden in files loaded as extras:
: turnkey 0 (source-id) !
::itc:: ." CoreForth-0 AVR ITC ready" cr
::stc:: ." CoreForth-0 AVR STC ready" cr
hex
quit
;
Include any extra files:
include-extras
Cold start of the system:
: cold hw-init
setup-pointers
turnkey
reset ;
Finalize the compilation back in host mode:
::host::
Set the IRQ handler for reset
#1 t' reset-handler irq-handler!
Store the values for latest and here (using the latest allocated address
in RAM), and let execution start with cold:
tlast @ $8000 or $100 >target w!
tvp @ $102 >target w!
::itc:: t' cold $8000 or t' start tcell + >target w!
::stc:: t' cold t' start tcell + >target w!
Clear the first word’s link pointer to finish the dictionary linked list:
0 $110 >target w!
Finally, write out the Intel HEX file with the compiled program:
::itc:: s" curiosity-nano-itc.hex" write-hex
::stc:: s" curiosity-nano-stc.hex" write-hex