| 1 : |
pazsan
|
1.1
|
\ Convert image to C include file |
| 2 : |
|
|
|
| 3 : |
anton
|
1.21
|
\ Copyright (C) 1998,1999,2002,2003,2007 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.2
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
anton
|
1.22
|
\ as published by the Free Software Foundation, either version 3 |
| 10 : |
anton
|
1.2
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
anton
|
1.22
|
\ along with this program. If not, see http://www.gnu.org/licenses/. |
| 19 : |
anton
|
1.2
|
|
| 20 : |
pazsan
|
1.15
|
require string.fs |
| 21 : |
|
|
|
| 22 : |
pazsan
|
1.1
|
0 Value image |
| 23 : |
|
|
0 Value bitmap |
| 24 : |
|
|
|
| 25 : |
|
|
Create magicbuf 8 allot |
| 26 : |
|
|
|
| 27 : |
pazsan
|
1.15
|
Create groups 32 0 [DO] 512 cells allocate throw dup 512 cells erase , [LOOP] |
| 28 : |
|
|
|
| 29 : |
pazsan
|
1.16
|
\ we define it ans like... |
| 30 : |
|
|
wordlist Constant target-environment |
| 31 : |
|
|
|
| 32 : |
|
|
\ save information of current dictionary to restore with environ> |
| 33 : |
|
|
Variable env-current |
| 34 : |
|
|
|
| 35 : |
pazsan
|
1.17
|
: >ENVIRON get-current env-current ! target-environment set-current |
| 36 : |
|
|
also target-environment context ! ; |
| 37 : |
|
|
: ENVIRON> previous env-current @ set-current ; |
| 38 : |
pazsan
|
1.16
|
|
| 39 : |
|
|
: t-env? ( addr len -- [ x ] true | false ) |
| 40 : |
|
|
\G returns the content of environment variable and true or |
| 41 : |
|
|
\G false if not present |
| 42 : |
|
|
target-environment search-wordlist |
| 43 : |
|
|
IF EXECUTE true ELSE false THEN ; |
| 44 : |
|
|
|
| 45 : |
|
|
: $has? ( addr u -- x | false ) |
| 46 : |
|
|
\G returns the content of environment variable |
| 47 : |
|
|
\G or false if not present |
| 48 : |
|
|
t-env? dup IF drop THEN ; |
| 49 : |
|
|
|
| 50 : |
|
|
' Value Alias DefaultValue |
| 51 : |
pazsan
|
1.18
|
' Value Alias SetValue |
| 52 : |
pazsan
|
1.16
|
|
| 53 : |
|
|
: kb 1024 * ; |
| 54 : |
|
|
|
| 55 : |
pazsan
|
1.18
|
' noop alias T |
| 56 : |
|
|
' noop alias H |
| 57 : |
|
|
|
| 58 : |
|
|
: has? parse-name 2drop true ; |
| 59 : |
|
|
|
| 60 : |
pazsan
|
1.16
|
include machpc.fs |
| 61 : |
|
|
ENVIRON> |
| 62 : |
|
|
|
| 63 : |
pazsan
|
1.15
|
: prefix? ( string u1 prefix u2 -- flag ) |
| 64 : |
|
|
tuck 2>r umin 2r> str= ; |
| 65 : |
|
|
|
| 66 : |
pazsan
|
1.16
|
s" NULL" groups @ cell+ $! |
| 67 : |
|
|
|
| 68 : |
|
|
: scan-ifs ( fd -- ) >r 1 |
| 69 : |
|
|
BEGIN pad $100 r@ read-line throw WHILE |
| 70 : |
|
|
pad swap |
| 71 : |
|
|
2dup s" #ifdef HAS_" prefix? >r |
| 72 : |
|
|
2dup s" #else" prefix? >r |
| 73 : |
|
|
s" #endif" prefix? r> or r> - + |
| 74 : |
|
|
dup 0= UNTIL THEN rdrop drop ; |
| 75 : |
|
|
|
| 76 : |
pazsan
|
1.15
|
: read-groups ( addr u -- ) |
| 77 : |
pazsan
|
1.16
|
r/o open-file throw >r 0 2 |
| 78 : |
pazsan
|
1.15
|
BEGIN pad $100 r@ read-line throw WHILE |
| 79 : |
pazsan
|
1.16
|
pad swap |
| 80 : |
|
|
2dup s" #ifdef HAS_" prefix? |
| 81 : |
|
|
IF |
| 82 : |
|
|
11 /string $has? 0= IF r@ scan-ifs THEN |
| 83 : |
|
|
ELSE 2dup s" #else" prefix? |
| 84 : |
pazsan
|
1.19
|
IF 2drop r@ scan-ifs |
| 85 : |
pazsan
|
1.16
|
ELSE 2dup s" GROUP(" prefix? |
| 86 : |
|
|
IF 2drop drop 1+ 0 ELSE |
| 87 : |
|
|
2dup s" INST_ADDR(" prefix? |
| 88 : |
|
|
IF &10 /string 2dup ') scan nip - |
| 89 : |
|
|
2over cells swap cells groups + @ + $! |
| 90 : |
|
|
1+ |
| 91 : |
|
|
ELSE 2drop THEN THEN THEN THEN |
| 92 : |
pazsan
|
1.15
|
REPEAT |
| 93 : |
|
|
2drop r> close-file throw ; |
| 94 : |
|
|
s" prim_lab.i" read-groups |
| 95 : |
|
|
|
| 96 : |
pazsan
|
1.3
|
Variable bswap? |
| 97 : |
pazsan
|
1.15
|
Variable endian |
| 98 : |
pazsan
|
1.3
|
Variable tchars |
| 99 : |
pazsan
|
1.4
|
Variable tcell |
| 100 : |
pazsan
|
1.7
|
Variable au |
| 101 : |
pazsan
|
1.3
|
|
| 102 : |
pazsan
|
1.15
|
: t@ ( addr -- x ) |
| 103 : |
|
|
endian @ IF |
| 104 : |
|
|
0 swap tcell @ bounds ?DO |
| 105 : |
|
|
8 lshift I c@ or |
| 106 : |
|
|
LOOP |
| 107 : |
|
|
ELSE |
| 108 : |
|
|
0 swap tcell @ bounds ?DO |
| 109 : |
|
|
8 rshift I c@ 8 tcell @ 1- * lshift or |
| 110 : |
|
|
LOOP |
| 111 : |
|
|
THEN |
| 112 : |
|
|
dup 1 8 tcell @ * 1- lshift and negate or ; |
| 113 : |
|
|
|
| 114 : |
pazsan
|
1.1
|
: search-magic ( fd -- ) >r |
| 115 : |
|
|
BEGIN magicbuf 8 r@ read-file throw 8 = WHILE |
| 116 : |
pazsan
|
1.24
|
magicbuf s" Gforth4" tuck str= UNTIL |
| 117 : |
pazsan
|
1.1
|
ELSE true abort" No magic found" THEN |
| 118 : |
pazsan
|
1.6
|
1 magicbuf 7 + c@ 5 rshift 3 and lshift tchars ! |
| 119 : |
|
|
1 magicbuf 7 + c@ 1 rshift 3 and lshift tcell ! |
| 120 : |
pazsan
|
1.7
|
1 magicbuf 7 + c@ 3 rshift 3 and lshift au ! |
| 121 : |
pazsan
|
1.15
|
magicbuf 7 + c@ 1 and 0= dup endian ! |
| 122 : |
pazsan
|
1.6
|
[ pad off 1 pad ! pad c@ 1 = ] Literal = bswap? ! |
| 123 : |
pazsan
|
1.7
|
." /* Image with " tcell @ . ." bytes cell, " tchars @ . ." bytes per char and " au @ . ." bytes per address unit */" cr |
| 124 : |
pazsan
|
1.1
|
rdrop ; |
| 125 : |
|
|
|
| 126 : |
|
|
Create image-header 4 cells allot |
| 127 : |
|
|
Variable image-cells |
| 128 : |
|
|
Variable bitmap-chars |
| 129 : |
|
|
|
| 130 : |
|
|
: read-header ( fd -- ) |
| 131 : |
pazsan
|
1.15
|
image-header 4 tcell @ * rot read-file throw drop |
| 132 : |
|
|
image-header 2 tcell @ * + t@ tchars @ * tcell @ / au @ / |
| 133 : |
pazsan
|
1.8
|
dup image-cells ! 1- 8 / tchars @ / 1+ bitmap-chars ! |
| 134 : |
pazsan
|
1.15
|
image-cells @ tcell @ * allocate throw to image |
| 135 : |
pazsan
|
1.1
|
bitmap-chars @ allocate throw to bitmap ; |
| 136 : |
|
|
|
| 137 : |
|
|
: read-dictionary ( fd -- ) >r |
| 138 : |
pazsan
|
1.15
|
image image-cells @ tcell @ * r> read-file throw drop ; |
| 139 : |
pazsan
|
1.1
|
|
| 140 : |
|
|
: read-bitmap ( fd -- ) >r |
| 141 : |
pazsan
|
1.8
|
bitmap bitmap-chars @ tchars @ * r> read-file throw drop ; |
| 142 : |
pazsan
|
1.1
|
|
| 143 : |
pazsan
|
1.4
|
: .08x ( n -- ) 0 <# tcell @ 0 ?DO # # LOOP 'x hold '0 hold #> type ; |
| 144 : |
|
|
: .02x ( n -- ) 0 <# tchars @ 0 ?DO # # LOOP 'x hold '0 hold #> type ; |
| 145 : |
pazsan
|
1.1
|
|
| 146 : |
pazsan
|
1.15
|
: bit@ ( addr n -- flag ) |
| 147 : |
|
|
dup 7 and $80 swap rshift >r 3 rshift + c@ r> and |
| 148 : |
|
|
0<> ; |
| 149 : |
|
|
|
| 150 : |
pazsan
|
1.1
|
: .image ( -- ) |
| 151 : |
|
|
image-cells @ 0 ?DO |
| 152 : |
pazsan
|
1.15
|
I 1 + I' min I ?DO space image I tcell @ * + t@ |
| 153 : |
|
|
bitmap I bit@ IF |
| 154 : |
|
|
dup 0< IF |
| 155 : |
pazsan
|
1.16
|
dup -1 = IF |
| 156 : |
|
|
drop ." NULL" |
| 157 : |
|
|
ELSE |
| 158 : |
|
|
negate dup $3E00 and 9 rshift swap $1FF and |
| 159 : |
|
|
over cells groups + @ over $1FF and cells + |
| 160 : |
|
|
dup @ 0= IF drop |
| 161 : |
|
|
2dup 0 8 d= IF 2drop s" doesjump" |
| 162 : |
|
|
ELSE |
| 163 : |
|
|
<# '] hold 0 #S 2drop '[ hold '] hold |
| 164 : |
|
|
0 #S '[ hold #> |
| 165 : |
|
|
THEN |
| 166 : |
pazsan
|
1.15
|
ELSE |
| 167 : |
pazsan
|
1.16
|
>r 2drop r> $@ |
| 168 : |
pazsan
|
1.15
|
THEN |
| 169 : |
pazsan
|
1.16
|
." INST_ADDR(" type ." )" |
| 170 : |
pazsan
|
1.15
|
THEN |
| 171 : |
|
|
ELSE |
| 172 : |
pazsan
|
1.23
|
dup IF ." ((void*)image)+" $10 - THEN .08x |
| 173 : |
pazsan
|
1.15
|
THEN |
| 174 : |
|
|
ELSE |
| 175 : |
pazsan
|
1.16
|
." (void*)" .08x |
| 176 : |
pazsan
|
1.15
|
THEN ." ," |
| 177 : |
|
|
LOOP cr |
| 178 : |
|
|
1 +LOOP ; |
| 179 : |
pazsan
|
1.1
|
|
| 180 : |
|
|
: .reloc ( -- ) |
| 181 : |
pazsan
|
1.8
|
bitmap-chars @ tchars @ * 0 ?DO |
| 182 : |
pazsan
|
1.4
|
I $10 + I' min I ?DO space |
| 183 : |
|
|
0 I tchars @ bounds ?DO 8 lshift bitmap I + c@ + LOOP |
| 184 : |
|
|
.02x ." ," tchars @ +LOOP cr |
| 185 : |
|
|
$10 +LOOP ; |
| 186 : |
pazsan
|
1.1
|
|
| 187 : |
|
|
: read-image ( addr u -- ) |
| 188 : |
|
|
r/o bin open-file throw >r |
| 189 : |
|
|
r@ search-magic |
| 190 : |
|
|
r@ file-position throw r@ read-header r@ reposition-file throw |
| 191 : |
|
|
r@ read-dictionary r@ read-bitmap r> close-file throw ; |
| 192 : |
|
|
|
| 193 : |
|
|
: .imagesize ( -- ) |
| 194 : |
pazsan
|
1.15
|
image-header 3 tcell @ * + t@ tchars @ * tcell @ / au @ / .08x ; |
| 195 : |
pazsan
|
1.1
|
|
| 196 : |
|
|
: .relocsize ( -- ) |
| 197 : |
pazsan
|
1.8
|
bitmap-chars @ .08x ; |
| 198 : |
pazsan
|
1.1
|
|
| 199 : |
|
|
: fi2c ( addr u -- ) base @ >r hex |
| 200 : |
|
|
read-image |
| 201 : |
pazsan
|
1.20
|
\ .\" const static __attribute__ ((__section__ (\".rodata\"))) void* image[" .imagesize ." ] = {" cr .image ." };" cr |
| 202 : |
|
|
.\" static void* image[" .imagesize ." ] = {" cr .image ." };" cr |
| 203 : |
pazsan
|
1.16
|
." #ifdef USE_RELOC" cr |
| 204 : |
pazsan
|
1.1
|
." const char reloc_bits[" .relocsize ." ] = {" cr .reloc ." };" cr |
| 205 : |
pazsan
|
1.16
|
." #endif" cr |
| 206 : |
pazsan
|
1.1
|
r> base ! ; |
| 207 : |
|
|
|