aboutsummaryrefslogtreecommitdiff
path: root/docs/arch.rst
blob: 9eededf7f78a0d9ab470d18ede9cf92fff044494 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Architecture abstraction layer
==============================

To enable easier targeting of different execution platforms and provide
decoupling from standard C library (or other dependencies), thin abstraction
layer is required.


Supported target platforms
--------------------------

Constant definition ``LSP_ARCH`` is used as identifier of desired target
platform. This definition should be set during C source preprocessing to
one of supported values:

    * ``LSP_ARCH_POSIX`` (POSIX target)
    * ``LSP_ARCH_AVR8`` (8bit AVR target)

Depending on value of this constant, different platform specific
implementations will be included.


C data types
------------

`arch.h` defines fixed length integers (``lsp_int8_t``, ``lsp_int16_t``,
``lsp_int32_t``, ``lsp_uint8_t``, ``lsp_uint16_t``, ``lsp_uint32_t``)
independent of target platform. If target has available standard C library,
this types are aliases to types defined by `stdint.h`. In case target doesn't
have standard C library available, these types should be defined by
appropriate C integer types (``char``, ``short``, ``int``, ``long``, ...).

Additionally, ``lsp_bool_t`` is defined as alias to ``_Bool``. If ``_Bool``
type is not available, ``lsp_uint8_t`` can be used. Care is taken not to depend
on specifics of integer to ``_Bool`` conversions so that other integer types
could be used as ``lsp_bool_t``.


Platform specific functions
---------------------------

Each platform abstraction layer implementation (`arch/*.c`) is responsible
for functions aliased with following names:

    * ``LSP_ARCH_INIT``

        Function, called before any other function, responsible for
        initialization of platform specific state.

    * ``LSP_ARCH_CREATE_MEM``

        Function responsible for allocating memory that will represent
        ``lsp_mem_t`` structure (described in following chapters) and it's
        initialization.

    * ``LSP_ARCH_FREE_MEM``

        Function responsible for freeing previously allocated memory.

    * ``LSP_ARCH_CREATE_IN_STREAM``

        Function responsible for allocating memory that will represent
        ``lsp_in_stream_t`` structure (described in following chapters) and
        it's initialization.

    * ``LSP_ARCH_FREE_IN_STREAM``

        Function responsible for freeing previously allocated memory.

    * ``LSP_ARCH_CREATE_OUT_STREAM``

        Function responsible for allocating memory that will represent
        ``lsp_out_stream_t`` structure (described in following chapters) and
        it's initialization.

    * ``LSP_ARCH_FREE_OUT_STREAM``

        Function responsible for freeing previously allocated memory.

In case of memory constrained targets (e.g. ``LSP_ARCH_AVR8``), functions 
responsible for allocating memory can return pointers to statically
preallocated memory instead of dynamically allocated. For this target
platforms, associated freeing functions don't implement any functionality.


Input stream
------------

During initialization of input stream, platform specific implementation
is responsible for providing function pointer with signature:

.. code-block:: c

    typedef lsp_int16_t (*lsp_stream_getchar_t)(lsp_in_stream_t *s);

Provided function is responsible for reading single character from input
stream (used by REPL). In case of POSIX, it's behavior corresponds to
``getchar`` provided by standard C library.

Details of input stream implementation are available in following chapters.


Output stream
-------------

During initialization of output stream, platform specific implementation
is responsible for providing function pointer with signature:

.. code-block:: c

    typedef lsp_int16_t (*lsp_stream_putchar_t)(lsp_out_stream_t *s, lsp_int16_t v);

Provided function is responsible for writing single character to output
stream (used by REPL). In case of POSIX, it's behavior corresponds to
``putchar`` provided by standard C library.

Details of output stream implementation are available in following chapters.


Source code
-----------

arch.h
''''''

.. literalinclude:: ../src_c/arch.h
    :language: c


arch/avr8.h
'''''''''''

.. literalinclude:: ../src_c/arch/avr8.h
    :language: c


arch/avr8.c
'''''''''''

.. literalinclude:: ../src_c/arch/avr8.c
    :language: c


arch/posix.h
''''''''''''

.. literalinclude:: ../src_c/arch/posix.h
    :language: c


arch/posix.c
''''''''''''

.. literalinclude:: ../src_c/arch/posix.c
    :language: c