Annotation of gforth/fi2c.fs, revision 1.6

1.1       pazsan      1: \ Convert image to C include file
                      2: 
1.2       anton       3: \ Copyright (C) 1998 Free Software Foundation, Inc.
                      4: 
                      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: \ as published by the Free Software Foundation; either version 2
                     10: \ 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: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
1.1       pazsan     21: 0 Value image
                     22: 0 Value bitmap
                     23: 
                     24: Create magicbuf 8 allot
                     25: 
1.3       pazsan     26: Variable bswap?
                     27: Variable tchars
1.4       pazsan     28: Variable tcell
1.3       pazsan     29: 
                     30: : bswap ( n -- n' )  bswap? @ 0= ?EXIT  0
                     31:     over 24 rshift $FF       and or
                     32:     over  8 rshift $FF00     and or
                     33:     over  8 lshift $FF0000   and or
                     34:     over 24 lshift $FF000000 and or nip ;
                     35: 
1.1       pazsan     36: : search-magic ( fd -- )  >r
                     37:     BEGIN  magicbuf 8 r@ read-file throw  8 =  WHILE
1.6     ! pazsan     38:        magicbuf s" Gforth2" tuck compare 0=  UNTIL
1.1       pazsan     39:     ELSE  true abort" No magic found"  THEN
1.6     ! pazsan     40:     1 magicbuf 7 + c@ 5 rshift 3 and lshift tchars !
        !            41:     1 magicbuf 7 + c@ 1 rshift 3 and lshift tcell !
        !            42:     magicbuf 7 + c@ 1 and 0=
        !            43:     [ pad off 1 pad ! pad c@ 1 = ] Literal = bswap? !
1.1       pazsan     44:     rdrop ;
                     45: 
                     46: Create image-header  4 cells allot
                     47: Variable image-cells
                     48: Variable bitmap-chars
                     49: 
                     50: : read-header ( fd -- )
                     51:     image-header 4 cells rot read-file throw drop
1.3       pazsan     52:     image-header 2 cells + @ bswap tchars @ *
                     53:     dup cell / image-cells ! 1- 8 cells / 1+ bitmap-chars !
1.1       pazsan     54:     image-cells @ cells allocate throw to image
                     55:     bitmap-chars @ allocate throw to bitmap ;
                     56: 
                     57: : read-dictionary ( fd -- )  >r
                     58:     image image-cells @ cells r> read-file throw drop ;
                     59: 
                     60: : read-bitmap ( fd -- )  >r
                     61:     bitmap bitmap-chars @ r> read-file throw drop ;
                     62: 
1.4       pazsan     63: : .08x ( n -- ) 0 <# tcell  @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
                     64: : .02x ( n -- ) 0 <# tchars @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
1.1       pazsan     65: 
                     66: : .image ( -- )
                     67:     image-cells @ 0 ?DO
1.3       pazsan     68:        I 4 + I' min I ?DO  space image I cells + @ bswap .08x ." ," LOOP cr
1.1       pazsan     69:        4 +LOOP ;
                     70: 
                     71: : .reloc ( -- )
                     72:     bitmap-chars @ 0 ?DO
1.4       pazsan     73:        I $10 + I' min I ?DO  space
                     74:            0 I tchars @ bounds ?DO  8 lshift bitmap I + c@ +  LOOP
                     75:            .02x ." ," tchars @ +LOOP cr
                     76:        $10 +LOOP ;
1.1       pazsan     77: 
                     78: : read-image ( addr u -- )
                     79:     r/o bin open-file throw >r
                     80:     r@ search-magic
                     81:     r@ file-position throw r@ read-header r@ reposition-file throw
                     82:     r@ read-dictionary r@ read-bitmap r> close-file throw ;
                     83: 
                     84: : .imagesize ( -- )
1.4       pazsan     85:     image-header 3 cells + @ bswap tcell @ / tchars @ * .08x ;
1.1       pazsan     86: 
                     87: : .relocsize ( -- )
1.5       pazsan     88:     bitmap-chars @ 1- tchars @ / 1+ .08x ;
1.1       pazsan     89: 
                     90: : fi2c ( addr u -- )  base @ >r hex
                     91:     read-image
                     92:     ." #include " '" emit ." forth.h" '" emit cr
                     93:     ." Cell image[" .imagesize ." ] = {" cr .image ." };" cr
                     94:     ." const char reloc_bits[" .relocsize ." ] = {" cr .reloc ." };" cr
                     95:     r> base ! ;
                     96: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>