header.s 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ;;; header.s
  2. ;;;
  3. ;;; Copyright (c) 2009 Openmoko Inc.
  4. ;;;
  5. ;;; Authors Christopher Hall <hsw@openmoko.com>
  6. ;;;
  7. ;;; Redistribution and use in source and binary forms, with or without
  8. ;;; modification, are permitted provided that the following conditions are
  9. ;;; met:
  10. ;;;
  11. ;;; 1. Redistributions of source code must retain the above copyright
  12. ;;; notice, this list of conditions and the following disclaimer.
  13. ;;;
  14. ;;; 2. Redistributions in binary form must reproduce the above copyright
  15. ;;; notice, this list of conditions and the following disclaimer in
  16. ;;; the documentation and/or other materials provided with the
  17. ;;; distribution.
  18. ;;;
  19. ;;; THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
  20. ;;; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. ;;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. ;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. ;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  26. ;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  28. ;;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  29. ;;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. ;;; some character constants
  31. backspace = 0x08
  32. line_feed = 0x0a
  33. carriage_return = 0x0d
  34. delete = 0x7f
  35. ;;; some special constants
  36. BYTES_PER_CELL = 4
  37. BITS_PER_BYTE = 8
  38. BITS_PER_CELL = (BITS_PER_BYTE * BYTES_PER_CELL)
  39. ;;; as above but for shift instead of multiply/divide
  40. LOG2_BITE_PER_BYTE = 3
  41. LOG2_BYTES_PER_CELL = 2
  42. ;;; boolean - to match Forth standard
  43. FALSE = 0
  44. TRUE = -1
  45. ;;; header flags
  46. FLAG_IMMEDIATE = 0x01
  47. FLAG_COMPILE_ONLY = 0x02
  48. FLAG_NORMAL = 0
  49. ;;; registers (C preserves r0..r3)
  50. ;;; r0 forth ip
  51. ;;; r1 forth sp
  52. ;;; r2 forth pp
  53. ;;; r3 forth w
  54. ;;; r4 C result low
  55. ;;; r5 C result high
  56. ;;; r6 C argument 1
  57. ;;; r7 C argument 2
  58. ;;; r8 C argument 3
  59. ;;; r9 C argument 4
  60. ;;; r10..r14 used by C and/or extended asm instructions
  61. ;;; r15 __dp for C
  62. .macro NEXT ; inner interpreter
  63. ld.w %r2, [%r0]+ ; incr IP (%r0)
  64. ld.w %r3, [%r2]+ ; %r2 -> param address
  65. ; xcall xdebug
  66. jp %r3 ; execute the code
  67. .endm
  68. ;;; inline forth counted strings
  69. .macro LSTRING, text
  70. .long str_\@_finish - str_\@_start
  71. str_\@_start:
  72. .ascii "\text"
  73. str_\@_finish:
  74. .balign 4
  75. .endm
  76. ;;; macro to create offsets in bytes and cells
  77. .macro MAKE_OFFSET, label, value
  78. .ifnotdef \label\()_BYTES
  79. \label\()_BYTES = \value
  80. \label\()_CELLS = \value / BYTES_PER_CELL
  81. .endif
  82. .endm
  83. ;;; the header
  84. ;;; 0: code address
  85. ;;; 4: param address
  86. ;;; 8: does address
  87. ;;; 12: flags
  88. ;;; 16: link address
  89. ;;; 20: count (name address points here)
  90. ;;; 24: name string (byte count)
  91. ;;; 28+count: (zeros as required to .balign 4)
  92. .section .forth_dict, "wa"
  93. .balign 4
  94. __forth_dict_last_name = 0 ; to link the list
  95. __root_dict_last_name = 0 ; to link the list
  96. __c33_dict_last_name = 0 ; to link the list
  97. .macro HEADER, dict_name, label, name, flags, code
  98. .section .forth_dict
  99. .balign 4
  100. .global \label
  101. \label\():
  102. .long \code ; code
  103. l_param_\@:
  104. .long param_\label ; param
  105. l_does_\@:
  106. .long 0 ; does
  107. l_flags_\@:
  108. .long \flags ; flags
  109. prev_\label = __\()\dict_name\()_last_name
  110. l_link_\@:
  111. .long prev_\label ; link
  112. .global name_\label
  113. name_\label\():
  114. __\()\dict_name\()_last_name = .
  115. LSTRING "\name"
  116. .balign 4
  117. .global param_\label
  118. param_\label\():
  119. ; MAKE_OFFSET DICTIONARY_HEADER, "( name_\label - \label )"
  120. MAKE_OFFSET DICTIONARY_CODE_TO_NAME_OFFSET, "( name_\label - \label )"
  121. MAKE_OFFSET DICTIONARY_CODE_TO_PARAM_OFFSET, "( l_param_\@ - \label )"
  122. MAKE_OFFSET DICTIONARY_CODE_TO_DOES_OFFSET, "( l_does_\@ - \label )"
  123. MAKE_OFFSET DICTIONARY_CODE_TO_LINK_OFFSET, "( l_link_\@ - \label )"
  124. MAKE_OFFSET DICTIONARY_CODE_TO_FLAGS_OFFSET, "( l_flags_\@ - \label )"
  125. ;;; these are for find
  126. MAKE_OFFSET DICTIONARY_CODE_OFFSET, "( name_\label - \label )"
  127. MAKE_OFFSET DICTIONARY_PARAM_OFFSET, "( name_\label - l_param_\@ )"
  128. MAKE_OFFSET DICTIONARY_FLAGS_OFFSET, "( name_\label - l_flags_\@ )"
  129. MAKE_OFFSET DICTIONARY_LINK_OFFSET, "( name_\label - l_link_\@ )"
  130. .endm
  131. ;;; code definitions
  132. .macro CODE, dict_name, name, label, flags
  133. HEADER \dict_name, \label, "\name", \flags, param_\label
  134. .endm
  135. .macro END_CODE
  136. .endm
  137. ;;; colon definitions
  138. .macro COLON, dict_name, name, label, flags
  139. HEADER \dict_name, \label, "\name", \flags, param_paren_colon_paren
  140. .endm
  141. .macro END_COLON
  142. ; .long exit
  143. .long 0
  144. .endm
  145. ;;; variable definitions
  146. .macro VARIABLE, dict_name, name, label, flags
  147. HEADER \dict_name, \label, "\name", \flags, param_paren_var_paren
  148. .endm
  149. .macro CREATE, dict_name, name, label, flags
  150. HEADER \dict_name, \label, "\name", \flags, param_paren_var_paren
  151. .endm
  152. ;;; constant definitions
  153. .macro CONSTANT, dict_name, name, label, flags
  154. HEADER \dict_name, \label, "\name", \flags, param_paren_const_paren
  155. .endm
  156. ;;; miscellaneous variables
  157. .section .bss
  158. .balign 4
  159. initial_argument: ; parameter from boot loader
  160. .space 4
  161. terminal_buffer_start:
  162. .space 65536
  163. terminal_buffer_length = . -terminal_buffer_start
  164. stack_pointer_low_limit:
  165. .space 65536
  166. .global initial_stack_pointer
  167. initial_stack_pointer: ; NOTE: stack underflows over return space!
  168. return_pointer_low_limit:
  169. .space 65536
  170. .global initial_return_pointer
  171. initial_return_pointer:
  172. ;;; Program Code
  173. .section .text
  174. .global main
  175. main:
  176. xld.w %r15, __dp
  177. xld.w %r1, initial_stack_pointer
  178. xld.w %r4, initial_return_pointer
  179. ld.w %sp, %r4
  180. xld.w %r4, initial_argument ; save initial argument
  181. ld.w [%r4], %r6
  182. ld.w %r0, 0 ; clear status register
  183. ld.w %psr, %r0
  184. xcall Vector_initialise ; must be first
  185. xcall Serial_initialise
  186. xcall Button_initialise
  187. xcall CTP_initialise
  188. ;;xcall Contrast_initialise ; ***done by the boot loader**
  189. xcall Analog_initialise
  190. xcall Tick_initialise
  191. ;;xcall Temperature_initialise ; not working yet
  192. xcall FLASH_initialise ; setup FLASH/SPI access
  193. xld.w %r0, cold_start ; initial ip value
  194. ;;; xcall xdebug
  195. NEXT
  196. ;;; headerless code to initially boot the system
  197. .balign 4 ; forth byte code must be aligned
  198. cold_start:
  199. .long cold ; initial forth code
  200. .long branch, cold_start ; just run cold_start in a loop