From 67d25d837ac55f28a366c0a3b262e439a6e75fc3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 19 Aug 2017 12:15:28 +0200 Subject: Add AmForth --- amforth-6.5/common/lib/forth2012/facility/ms.frt | 3 ++ .../lib/forth2012/facility/structures-array.frt | 24 ++++++++++++++++ .../lib/forth2012/facility/structures-test.frt | 16 +++++++++++ .../common/lib/forth2012/facility/structures.frt | 24 ++++++++++++++++ .../lib/forth2012/facility/time-and-date.frt | 33 ++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 amforth-6.5/common/lib/forth2012/facility/ms.frt create mode 100644 amforth-6.5/common/lib/forth2012/facility/structures-array.frt create mode 100644 amforth-6.5/common/lib/forth2012/facility/structures-test.frt create mode 100644 amforth-6.5/common/lib/forth2012/facility/structures.frt create mode 100644 amforth-6.5/common/lib/forth2012/facility/time-and-date.frt (limited to 'amforth-6.5/common/lib/forth2012/facility') diff --git a/amforth-6.5/common/lib/forth2012/facility/ms.frt b/amforth-6.5/common/lib/forth2012/facility/ms.frt new file mode 100644 index 0000000..7dfcd5d --- /dev/null +++ b/amforth-6.5/common/lib/forth2012/facility/ms.frt @@ -0,0 +1,3 @@ + +\ a trivial multitasking friendly ms +: ms 0 ?do pause 1ms loop ; diff --git a/amforth-6.5/common/lib/forth2012/facility/structures-array.frt b/amforth-6.5/common/lib/forth2012/facility/structures-array.frt new file mode 100644 index 0000000..14e62df --- /dev/null +++ b/amforth-6.5/common/lib/forth2012/facility/structures-array.frt @@ -0,0 +1,24 @@ + +begin-structure hash + field: hash.key + field: hash.value +end-structure + +\ inspired by CELLS +: hash-cells hash * ; + +\ define a hash-array +: hash: + hash-cells buffer: + does> + swap hash-cells + +; + +\ define an array of some elements hash'es +4 hash: my-hash +cr 0 my-hash . +cr 1 my-hash . + +\ store a key/value pair +42 3 my-hash hash.key ! +4711 3 my-hash hash.value ! diff --git a/amforth-6.5/common/lib/forth2012/facility/structures-test.frt b/amforth-6.5/common/lib/forth2012/facility/structures-test.frt new file mode 100644 index 0000000..c4e810c --- /dev/null +++ b/amforth-6.5/common/lib/forth2012/facility/structures-test.frt @@ -0,0 +1,16 @@ +\ simple test example for forth200x structures +\ define a new data structure named list. + +begin-structure list + field: l.p \ previous + field: l.n \ next + field: l.d \ data +end-structure + +\ create an instance of the datastructure list +\ named listroot + +list buffer: listroot + +\ access an element from the instance +$55aa listroot l.d ! diff --git a/amforth-6.5/common/lib/forth2012/facility/structures.frt b/amforth-6.5/common/lib/forth2012/facility/structures.frt new file mode 100644 index 0000000..65f8e5e --- /dev/null +++ b/amforth-6.5/common/lib/forth2012/facility/structures.frt @@ -0,0 +1,24 @@ +\ structures according to http://www.forth200x.org/structures.html +\ and http://www.forth200x.org/structures2.html +\ the reference implementation does not work since amforth uses +\ not the unified memory model for dictionary and data + +: +field: ( n1 "name" -- n2 ) + create over , + + does> @i + +; + +: begin-structure + create dp 0 -1 , \ -1 saves a flash erase when end-structure is executed + does> + @i +; + +: end-structure + swap !i +; + +: cfield: 1 +field: ; +: field: 2 +field: ; +\ 2field is not standard, but why not? +: 2field: 4 +field: ; diff --git a/amforth-6.5/common/lib/forth2012/facility/time-and-date.frt b/amforth-6.5/common/lib/forth2012/facility/time-and-date.frt new file mode 100644 index 0000000..aecbeff --- /dev/null +++ b/amforth-6.5/common/lib/forth2012/facility/time-and-date.frt @@ -0,0 +1,33 @@ + + +\ common words for date&time + +\ uses timer interrrupt module to call +\ a background task every second. + +\ holds the ever increasing time ticks +\ unfortunatly, a day has more seconds +\ a 16bit variable can store. +2variable time \ the seconds of the current day +2variable date \ a day number + +\ a background task +: next-second + time 2@ 1. d+ 2dup + 86399. d> if + 2drop 0. + 1. date d+! + then + time 2! +; + +: dateinit + 0. time 2! + 0. date 2! +; + +\ simple world. Every month has 30 days +: time&date ( -- sec min hour day month year ) + date 2@ 365 um/mod 30 /mod ( -- day month year ) + time 2@ 24 um/mod 60 /mod ( -- sec min hour ) +; -- cgit v1.2.3