Search code examples
forthgforth

Use constants to name a hardware address in Forth


I'm having some trouble with the following Forth code:

    HEX
    FFFF3E27 CONSTANT SHUTTER 
    DECIMAL

    : OPEN 1 SWAP ! ;
    : CLOSE 0 SWAP ! ;

Now I want to write "1" to the shutter address:

SHUTTER OPEN

This is my error:

:15: Invalid memory address
SHUTTER >>>OPEN<<<
Backtrace:
$10D436398 ! 

Am I missing something?


Solution

  • The code is essentially correct. I believe the problem might be with the address itself and what is being stored at it.

    Specifically, ! stores a cell, which is the normal size of the word on the stack. Given that address size, I'm guessing it's 32 bits.

    Now, the problem is that the address is odd. Many hardware architectures do not allow storing 32-bits words at odd addresses, or anything but addresses divisible by 4.

    If you want to store a single byte, use C! (or some equivalent in your Forth implementation).