Arrays¶
Create a 2D array from a definition¶
This is Matthias’ code:
: 2d-array ( #x #y -- )
2dup * cells s" buffer: (array) (array)" evaluate
<builds , ( addr ) swap , ( #x ) , ( #y )
does> ( x y -- addr )
>r ( x y R: addr-to-structure )
( check bounds yourself )
r@ cell+ @ ( #x ) * + cells
r> @ ( offset addr ) +
;
Note
“( check bounds yourself )” comment exists as Mecrisp-Stellaris has no ‘throw’ definition. otherwide the commant would probably be “>r ( x y R: addr-to-structure ) throw”
CivilCoder> My takeaway; 1. If you know exactly what data you want stored in an array AND it will never change, then use CREATE 2. If you don’t know excactly what data you want now OR it needs to be changed, use BUFFER: 3. ALLOT is mostly useless since you are reserving FLASH space but can’t write to it. Matthias said it has some uses like reserving a chunk of flash memory to be used as a log area.
Data Types¶
Creating and fetching data from arrays using different data types. Hopefully it clearly shows the methods and results.
Arrays in Forth are just indexes to a named base memory location in the Flash memory space. Arrays can only be created in Flash meaning the data must be initialized.
See also
BUFFERS are used for UNINITIALISED data (Ram).
Data Types¶
Name |
Bits |
Description |
---|---|---|
Word |
32 |
Native Mecrisp-Stellaris data length |
Cell |
32 |
Same as Word above |
Half Word |
16 |
Half length |
Character |
8 |
One Byte |
Bit |
1 |
Smallest |
note: In Forth $ = 0x or hexadecimal
Emulate c,¶
Many MCU’s don’t have a native “c,”. Emulate it as below.
\ Emulate c, which is not available in hardware on some chips.
0 variable c,collection
: c, ( c -- )
c,collection @ ?dup if $FF and swap 8 lshift or h,
0 c,collection !
else $100 or c,collection !
then ;
: calign ( -- )
c,collection @ if 0 c, then ;
Create Array of Characters¶
Four 8 bit or “character” items, $3f, $06, $5b and $66
create array $3f c, $06 c, $5b c, $66 c,
Fetch the array data using different data types¶
Fetch Cells¶
32 bits
: =cells 4 * array + dup hex. @ hex. ;
Fetch Half Words¶
2 bytes or 16 bits
: =halfwords 2 * array + dup hex. h@ hex. ;
Fetch Characters¶
one byte or 8 bits
: =characters array + dup hex. c@ hex. ;
Fetch using different data types¶
Note the address fetch increments.
Cells¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=cells |
200003A4 |
665B063F |
All the 8 bit data is packed into a CELL, very efficient. |
1 |
=cells |
200003A8 |
2000038C |
reading data in memory, not associated with the array |
2 |
=cells |
200003AC |
3D060000 |
reading data in memory, not associated with the array |
3 |
=cells |
200003B0 |
6C6C6563 |
reading data in memory, not associated with the array |
Halfwords¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=halfwords |
200003A4 |
0000063F |
|
1 |
=halfwords |
200003A6 |
0000665B |
|
2 |
=halfwords |
200003A8 |
0000038C |
reading data in memory, not associated with the array |
3 |
=halfwords |
200003AA |
00002000 |
reading data in memory, not associated with the array |
Characters¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=characters |
200003A4 |
0000003F |
|
1 |
=characters |
200003A5 |
00000006 |
|
2 |
=characters |
200003A6 |
0000005B |
|
3 |
=characters |
200003A7 |
00000066 |
Create Array of Halfwords¶
create array $3f4f h, $0607 h, $5b55 h, $6666 h,
Fetch the array data using different data types¶
Fetch Cells¶
32 bits
: =cells 4 * array + dup hex. @ hex. ;
Fetch Half Words¶
2 bytes or 16 bits
: =halfwords 2 * array + dup hex. h@ hex. ;
Fetch Characters¶
one byte or 8 bits
: =characters array + dup hex. c@ hex. ;
Fetch using different data types¶
Note the address fetch increments.
Cells¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=cells |
200003A4 |
06073F4F |
|
1 |
=cells |
200003A8 |
66665B55 |
|
2 |
=cells |
200003AC |
2000038C |
|
3 |
=cells |
200003B0 |
3D060000 |
Halfwords¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=halfwords |
200003A4 |
00003F4F |
|
1 |
=halfwords |
200003A6 |
00000607 |
|
2 |
=halfwords |
200003A8 |
00005B55 |
|
3 |
=halfwords |
200003AA |
00006666 |
Characters¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=characters |
200003A4 |
0000004F |
|
1 |
=characters |
200003A5 |
0000003F |
|
2 |
=characters |
200003A6 |
00000007 |
|
3 |
=characters |
200003A7 |
00000006 |
Create Array of Cells¶
create array $3f4f1234 , $06075678 , $5b558765 , $66664321 ,
Fetch the array data using different data types¶
Fetch Cells¶
32 bits
: =cells 4 * array + dup hex. @ hex. ;
Fetch Half Words¶
2 bytes or 16 bits
: =halfwords 2 * array + dup hex. h@ hex. ;
Fetch Characters¶
one byte or 8 bits
: =characters array + dup hex. c@ hex. ;
Fetch using different data types¶
Note the address fetch increments.
Cells¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=cells |
200003A4 |
3F4F1234 |
|
1 |
=cells |
200003A8 |
06075678 |
|
2 |
=cells |
200003AC |
5B558765 |
|
3 |
=cells |
200003B0 |
66664321 |
Halfwords¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=halfwords |
200003A4 |
00001234 |
|
1 |
=halfwords |
200003A6 |
00003F4F |
|
2 |
=halfwords |
200003A8 |
00005678 |
|
3 |
=halfwords |
200003AA |
00000607 |
Characters¶
Parameter |
Word |
Address |
Data |
Comments |
---|---|---|---|---|
0 |
=characters |
200003A4 |
00000034 |
|
1 |
=characters |
200003A5 |
00000012 |
|
2 |
=characters |
200003A6 |
0000004F |
|
3 |
=characters |
200003A7 |
0000003F |